Car { public static CarBuilder builder() { return new CarBuilder(); } private  Car(String plate, MonetaryAmount price, String model, String color); @Deprecated  Car(); String getPlate(); MonetaryAmount getPrice(); String getModel(); String getColor(); @Override String toString(); static CarBuilder builder();  }
@Test public void shouldReturnErrorWhenCarCheap() { Car ferrari = Car.builder().withPlate("BRL-1234") .withModel("ferrari") .withPrice(Money.of(99, usd)) .build(); Set<ConstraintViolation<Car>> validate = validator.validate(ferrari); assertFalse(validate.isEmpty()); String message = validate.stream().findFirst() .map(ConstraintViolation::getMessageTemplate) .orElse(null); assertEquals("There is not car cheap like that", message); }
@Test public void shouldReturnErrorWhenCarFancy() { Car ferrari = Car.builder().withPlate("BRL-1234") .withModel("ferrari") .withPrice(Money.of(1_0000_000_000L, usd)) .build(); Set<ConstraintViolation<Car>> validate = validator.validate(ferrari); assertFalse(validate.isEmpty()); String message = validate.stream().findFirst() .map(ConstraintViolation::getMessageTemplate) .orElse(null); assertEquals("The parking does not support fancy car", message); }
@Test public void shouldReturnErrorWhenCurrencyIsNotDollar() { CurrencyUnit real = Monetary.getCurrency("BRL"); Car ferrari = Car.builder().withPlate("BRL-1234") .withModel("ferrari") .withPrice(Money.of(1000, real)) .build(); Set<ConstraintViolation<Car>> validate = validator.validate(ferrari); assertFalse(validate.isEmpty()); String message = validate.stream().findFirst() .map(ConstraintViolation::getMessageTemplate) .orElse(null); assertEquals("The car price must work with USD", message); }
@Test public void shouldCreateInstance() { Car ferrari = Car.builder().withPlate("BRL-1234") .withModel("ferrari") .withPrice(Money.of(1000, usd)) .build(); Set<ConstraintViolation<Car>> validate = validator.validate(ferrari); assertTrue(validate.isEmpty()); }
@Test public void shouldReturnErrorWhenCarInvalidPlate() { Car ferrari = Car.builder().withPlate("23-234") .withModel("ferrari") .withPrice(Money.of(1_0001, usd)) .build(); Set<ConstraintViolation<Car>> validate = validator.validate(ferrari); assertFalse(validate.isEmpty()); String message = validate.stream().findFirst() .map(ConstraintViolation::getMessageTemplate) .orElse(null); assertEquals("Invalid car plate", message); }