@Test
  public void shouldFindCarsByExample() throws Exception {
    String uniqueColor = "unique-color-" + UUID.randomUUID().toString();
    Car exampleCar =
        CarBuilder.aCar()
            .withManufacturer(UUID.randomUUID().toString())
            .withColor(uniqueColor)
            .build();
    saveNewCopiesOfCarToRepository(exampleCar, 10);

    Car secondExampleCar =
        CarBuilder.aCar()
            .withManufacturer(UUID.randomUUID().toString())
            .withColor(uniqueColor)
            .build();
    saveNewCopiesOfCarToRepository(secondExampleCar, 5);

    Car searchByManufacturerExampleCar = new Car(null, 0, exampleCar.getManufacturer(), null);
    List<Car> foundCars = carRepository.findByExample(searchByManufacturerExampleCar);
    assertThat(foundCars).isNotNull().hasSize(10);

    Car searchByColorExampleCar = new Car(null, 0, null, uniqueColor);
    List<Car> foundCarsByColor = carRepository.findByExample(searchByColorExampleCar);
    assertThat(foundCarsByColor).isNotNull().hasSize(15);

    Car searchByManufacturerAndColorExampleCar =
        new Car(null, 0, exampleCar.getManufacturer(), uniqueColor);
    List<Car> foundCarsByManufacturerAndColor =
        carRepository.findByExample(searchByManufacturerAndColorExampleCar);
    assertThat(foundCarsByManufacturerAndColor).isNotNull().hasSize(10);
  }
  @Test
  public void shouldCountCarsWithFindCommand() throws Exception {
    String uniqueColorForTest = "color-shouldCountCarsWithFindCommand";
    Car exampleCar =
        CarBuilder.aCar()
            .withManufacturer(UUID.randomUUID().toString())
            .withColor(uniqueColorForTest)
            .build();
    saveNewCopiesOfCarToRepository(exampleCar, 10);

    Car secondExampleCar =
        CarBuilder.aCar()
            .withManufacturer(UUID.randomUUID().toString())
            .withColor(uniqueColorForTest)
            .build();
    saveNewCopiesOfCarToRepository(secondExampleCar, 5);

    //		Car searchByManufacturerExampleCar = new Car(null, 0, exampleCar.getManufacturer(), null);
    FindCommand<Car> findCommand = FindCommand.create(Car.class);
    findCommand.withSearchFilter("manufacturer", exampleCar.getManufacturer());
    long foundCars = carRepository.findTotalItemCount(findCommand);
    assertThat(foundCars).isEqualTo(10);

    FindCommand<Car> findCommand2 = FindCommand.create(Car.class);
    findCommand2.withSearchFilter("color", uniqueColorForTest);
    long foundCarsByColor = carRepository.findTotalItemCount(findCommand2);
    assertThat(foundCarsByColor).isEqualTo(15);

    FindCommand<Car> findCommand3 = FindCommand.create(Car.class);
    findCommand3.withSearchFilter("manufacturer", exampleCar.getManufacturer());
    findCommand3.withSearchFilter("color", uniqueColorForTest);
    long foundCarsByManufacturerAndColor = carRepository.findTotalItemCount(findCommand3);
    assertThat(foundCarsByManufacturerAndColor).isEqualTo(10);
  }
  @Test
  public void shouldFindCarsWithFindCommandAndNamedQuery() throws Exception {
    //		String uniqueColorForTest = "black-and-pink-with-ruby-named-query";
    Car exampleCar =
        CarBuilder.aCar().withManufacturer(UUID.randomUUID().toString()).withYear(2000).build();
    saveNewCopiesOfCarToRepository(exampleCar, 10);

    Car secondExampleCar =
        CarBuilder.aCar().withManufacturer(UUID.randomUUID().toString()).withYear(2005).build();
    saveNewCopiesOfCarToRepository(secondExampleCar, 5);

    //		Car searchByManufacturerExampleCar = new Car(null, 0, exampleCar.getManufacturer(), null);
    FindCommand<Car> findCommand = FindCommand.create(Car.class);
    findCommand.withSearchFilter("year", "2000").withNamedQuery("Car.findGreaterThanYear");
    List<Car> foundCars = carRepository.findItemsWith(findCommand);
    assertThat(foundCars).isNotNull().hasSize(10);

    FindCommand<Car> findCommand2 =
        FindCommand.create(Car.class)
            .withSearchFilter("year", "2005")
            .withNamedQuery("Car.findGreaterThanYear");

    List<Car> foundCarsByColor = carRepository.findItemsWith(findCommand2);
    assertThat(foundCarsByColor).isNotNull().hasSize(5);

    /*FindCommand<Car> findCommand3 = FindCommand.create(Car.class)
    		.withNamedQuery("Car.findGreaterThanYear");
    findCommand3.withSearchFilter("manufacturer", exampleCar.getManufacturer());
    findCommand3.withSearchFilter("color", uniqueColorForTest);
    List<Car> foundCarsByManufacturerAndColor = carRepository.findItemsWith(findCommand3);
    assertThat(foundCarsByManufacturerAndColor).isNotNull().hasSize(10);*/
  }
 public void testGetById() {
   try {
     CarRepository carRepository = getInstanceFor(CarRepository.class);
     carRepository.getById(-1);
   } catch (ContractException e) {
     System.out.println(e.getMessage());
     return;
   }
   fail("ContractException not thrown!");
 }
  @Test
  public void shouldFindCarsAndRelatedTyresWithFindCommand() throws Exception {
    String uniqueColorForTest = "black-and-pink-with-ruby-and-related-tyres";
    String tyreManufacturer = "tyre-manufacturer-1";
    Car exampleCar =
        CarBuilder.aCar()
            .withManufacturer(UUID.randomUUID().toString())
            .withColor(uniqueColorForTest)
            .withTyre(tyreManufacturer, 5)
            .withTyre(tyreManufacturer, 5)
            .withTyre(tyreManufacturer, 5)
            //				.withTyre(tyreManufacturer, 5)
            //				.withTyre(tyreManufacturer, 5)
            //				.withTyre("tyre-manufacturer-2", 5)
            .build();
    saveNewCopiesOfCarToRepository(exampleCar, 1);

    /*Car secondExampleCar = CarBuilder.aCar().withManufacturer(UUID.randomUUID().toString())
    		.withColor(uniqueColorForTest).build();
    saveNewCopiesOfCarToRepository(secondExampleCar, 5);*/

    //		Car searchByManufacturerExampleCar = new Car(null, 0, exampleCar.getManufacturer(), null);
    FindCommand<Car> findCommand = FindCommand.create(Car.class);
    findCommand.withSearchFilter("manufacturer", exampleCar.getManufacturer());
    findCommand.withSearchFilter("tyreList.manufacturer", tyreManufacturer);
    //		List<Car> foundCars = carRepository.findItemsWith(findCommand);

    //		int numberOfTries = 1;
    //		for (int i = 0; i < numberOfTries; i++)
    {
      //			log.info("testNumber=[{}]", i);
      //			List<Car> foundCars = carRepository.tempfindItemsWithOneToMany(findCommand);
      int expectedSize = 1;

      List<Car> foundCars = carRepository.findItemsWith(findCommand);
      log.info("foundCars={}", foundCars);
      assertThat(foundCars).isNotNull().hasSize(expectedSize);

      long count = carRepository.findTotalItemCount(findCommand);
      assertThat(count).isNotNull().isEqualTo(expectedSize);
    }

    /*FindCommand<Car> findCommand2 = FindCommand.create(Car.class);
    findCommand2.withSearchFilter("color", uniqueColorForTest);
    List<Car> foundCarsByColor = carRepository.findItemsWith(findCommand2);
    assertThat(foundCarsByColor).isNotNull().hasSize(15);

    FindCommand<Car> findCommand3 = FindCommand.create(Car.class);
    findCommand3.withSearchFilter("manufacturer", exampleCar.getManufacturer());
    findCommand3.withSearchFilter("color", uniqueColorForTest);
    List<Car> foundCarsByManufacturerAndColor = carRepository.findItemsWith(findCommand3);
    assertThat(foundCarsByManufacturerAndColor).isNotNull().hasSize(10);*/
  }
 private void saveDomainEntityToRepositoryWithTransaction(Car domainEntity)
     throws NotSupportedException, SystemException, RollbackException, HeuristicMixedException,
         HeuristicRollbackException {
   userTransaction.begin();
   carRepository.save(domainEntity);
   userTransaction.commit();
 }
  @Test(expected = RuntimeException.class)
  public void shouldThrowErrorWhenFindCarWithInvalidId() {
    // setup test?
    // Use import.sql for hibernate auto-insert?

    Car car = carRepository.findById(-1L);
  }
  @Test
  public void shouldFindCarsAndPaginateWithFindCommand() throws Exception {
    String uniqueColorForTest = "black-and-pink-with-ruby";
    Car exampleCar =
        CarBuilder.aCar()
            .withManufacturer(UUID.randomUUID().toString())
            .withColor(uniqueColorForTest)
            .build();
    saveNewCopiesOfCarToRepository(exampleCar, 10);

    Car secondExampleCar =
        CarBuilder.aCar()
            .withManufacturer(UUID.randomUUID().toString())
            .withColor(uniqueColorForTest)
            .build();
    saveNewCopiesOfCarToRepository(secondExampleCar, 5);

    FindCommand<Car> findCommand = FindCommand.create(Car.class);
    findCommand
        .withSearchFilter("manufacturer", exampleCar.getManufacturer())
        .forCurrentPageFirstIndex(0)
        .forPageSize(7);
    //		carRepository.findTotalItemCount(findCommand);
    List<Car> foundCars = carRepository.findItemsWith(findCommand);
    assertThat(foundCars).isNotNull().hasSize(7);

    findCommand.forCurrentPageFirstIndex(1);
    foundCars = carRepository.findItemsWith(findCommand);
    assertThat(foundCars).isNotNull().hasSize(7);

    findCommand.forCurrentPageFirstIndex(9);
    foundCars = carRepository.findItemsWith(findCommand);
    assertThat(foundCars).isNotNull().hasSize(1);

    FindCommand<Car> findCommand3 = FindCommand.create(Car.class);
    findCommand3
        .withSearchFilter("manufacturer", exampleCar.getManufacturer())
        .withSearchFilter("color", uniqueColorForTest)
        .forPageSize(5)
        .forCurrentPageFirstIndex(7);
    List<Car> foundCarsByManufacturerAndColor = carRepository.findItemsWith(findCommand3);
    assertThat(foundCarsByManufacturerAndColor).isNotNull().hasSize(3);

    findCommand3.forPageSize(2);
    List<Car> foundCarsByManufacturerAndColor2 = carRepository.findItemsWith(findCommand3);
    assertThat(foundCarsByManufacturerAndColor2).isNotNull().hasSize(2);
  }
  @RequestMapping(method = RequestMethod.GET)
  public List<Car> getAllCars(HttpServletRequest request) {

    Cookie[] cookies = request.getCookies();

    List<Car> cars = repository.findAll();
    return cars;
  }
  @Test
  public void shouldReturnNonNullWhenFindCarWithValidId() throws Exception {
    Car car = CarBuilder.aCar().build();
    saveDomainEntityToRepositoryWithTransaction(car);

    Car foundCar = carRepository.findById(car.getId());
    assertThat(foundCar).isNotNull();
  }
 @Test(/* expected = RuntimeException.class */ )
 public void shouldReturnNullWhenFindCarWithInvalidManufacturer() {
   // setup test?
   // Use import.sql for hibernate auto-insert?
   Car exampleCar = CarBuilder.aCar().withManufacturer("invalid-manufacturer").build();
   List<Car> foundCars = carRepository.findByExample(exampleCar);
   assertThat(foundCars).isEmpty();
 }
  @Test(expected = Exception.class)
  public void shouldThrowErrorWhenFindCarWithNullId() {
    // Car car = new Car(CarProcessState.AwaitingCapture);
    // Car carResponse = applicationProcessService.moveProcessForward(car);

    // assertThat(carResponse).isNotNull();
    // log.info("applicationSearchService={}", applicationSearchService);
    // log.info("carResponse={}", carResponse);
    // log.info("carResponse.getId()={}", carResponse.getId());
    Car carFound = carRepository.findById(null);
  }
  @Test
  public void shouldReturnWithoutErrorWhenSavingExistingCar()
      throws NotSupportedException, SystemException, SecurityException, IllegalStateException,
          RollbackException, HeuristicMixedException, HeuristicRollbackException {
    Car car = CarBuilder.aCar().build();
    saveDomainEntityToRepositoryWithTransaction(car);
    car = carRepository.findById(car.getId());

    car.setPrice(28);
    saveDomainEntityToRepositoryWithTransaction(car);

    assertThat(car).isNotNull();
  }
  @Test
  public void shouldFindCarsAndRelatedDriversWithFindCommand() throws Exception {
    String uniqueColorForTest = "black-and-pink-with-ruby-and-related-drivers";
    Car exampleCar =
        CarBuilder.aCar()
            .withManufacturer(UUID.randomUUID().toString())
            .withColor(uniqueColorForTest)
            .build();
    saveNewCopiesOfCarToRepository(exampleCar, 10);

    Car secondExampleCar =
        CarBuilder.aCar()
            .withManufacturer(UUID.randomUUID().toString())
            .withColor(uniqueColorForTest)
            .build();
    saveNewCopiesOfCarToRepository(secondExampleCar, 5);

    //		Car searchByManufacturerExampleCar = new Car(null, 0, exampleCar.getManufacturer(), null);
    FindCommand<Car> findCommand = FindCommand.create(Car.class);
    findCommand.withSearchFilter("manufacturer", exampleCar.getManufacturer());
    findCommand.withSearchFilter("driver.name", exampleCar.getDriverName());
    List<Car> foundCars = carRepository.findItemsWith(findCommand);
    //		List<Car> foundCars = carRepository.tempfindItems(findCommand);
    assertThat(foundCars).isNotNull().hasSize(10);

    /*FindCommand<Car> findCommand2 = FindCommand.create(Car.class);
    findCommand2.withSearchFilter("color", uniqueColorForTest);
    List<Car> foundCarsByColor = carRepository.findItemsWith(findCommand2);
    assertThat(foundCarsByColor).isNotNull().hasSize(15);

    FindCommand<Car> findCommand3 = FindCommand.create(Car.class);
    findCommand3.withSearchFilter("manufacturer", exampleCar.getManufacturer());
    findCommand3.withSearchFilter("color", uniqueColorForTest);
    List<Car> foundCarsByManufacturerAndColor = carRepository.findItemsWith(findCommand3);
    assertThat(foundCarsByManufacturerAndColor).isNotNull().hasSize(10);*/
  }
 @Before
 public void initialise() {
   persistenceUnitUtil = carRepository.getEm().getEntityManagerFactory().getPersistenceUnitUtil();
 }
 @RequestMapping(method = RequestMethod.DELETE)
 public Object deleteCar(@RequestParam("id") String id) {
   repository.delete(id);
   return new EmptyJson();
 }
 @RequestMapping(method = RequestMethod.GET, value = "{id}")
 public Car getCarByID(@PathVariable String id) {
   Car car = repository.findOne(id);
   return car;
 }
 @RequestMapping(method = RequestMethod.POST)
 public Car addCar(@RequestBody @Valid final Car car) {
   Car saved = repository.save(car);
   return car;
 }