|
| 1 | +package org.baeldung; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertEquals; |
| 4 | + |
| 5 | +import java.util.Collections; |
| 6 | +import java.util.OptionalInt; |
| 7 | +import java.util.Set; |
| 8 | + |
| 9 | +import javax.validation.ConstraintViolation; |
| 10 | +import javax.validation.Validation; |
| 11 | +import javax.validation.Validator; |
| 12 | +import javax.validation.ValidatorFactory; |
| 13 | +import org.baeldung.valueextractors.ProfileValueExtractor; |
| 14 | + |
| 15 | +import org.junit.Before; |
| 16 | +import org.junit.Test; |
| 17 | + |
| 18 | +public class ContainerValidationIntegrationTest { |
| 19 | + private Validator validator; |
| 20 | + |
| 21 | + @Before |
| 22 | + public void setup() { |
| 23 | + ValidatorFactory factory = Validation.byDefaultProvider().configure() |
| 24 | + .addValueExtractor(new ProfileValueExtractor()).buildValidatorFactory(); |
| 25 | + validator = factory.getValidator(); |
| 26 | + } |
| 27 | + |
| 28 | + @Test |
| 29 | + public void whenEmptyAddress_thenValidationFails() { |
| 30 | + Customer customer = new Customer(); |
| 31 | + customer.setName("John"); |
| 32 | + customer.setAddresses(Collections.singletonList(" ")); |
| 33 | + Set<ConstraintViolation<Customer>> violations = validator.validate(customer); |
| 34 | + assertEquals(1, violations.size()); |
| 35 | + assertEquals("Address must not be blank", violations.iterator() |
| 36 | + .next() |
| 37 | + .getMessage()); |
| 38 | + } |
| 39 | + |
| 40 | + @Test |
| 41 | + public void whenInvalidEmail_thenValidationFails() { |
| 42 | + CustomerMap map = new CustomerMap(); |
| 43 | + map.setCustomers(Collections.singletonMap("john", new Customer())); |
| 44 | + Set<ConstraintViolation<CustomerMap>> violations = validator.validate(map); |
| 45 | + assertEquals(1, violations.size()); |
| 46 | + assertEquals("Must be a valid email", violations.iterator() |
| 47 | + .next() |
| 48 | + .getMessage()); |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + public void whenAgeTooLow_thenValidationFails() { |
| 53 | + Customer customer = new Customer(); |
| 54 | + customer.setName("John"); |
| 55 | + customer.setAge(15); |
| 56 | + Set<ConstraintViolation<Customer>> violations = validator.validate(customer); |
| 57 | + assertEquals(1, violations.size()); |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + public void whenAgeNull_thenValidationSucceeds() { |
| 62 | + Customer customer = new Customer(); |
| 63 | + customer.setName("John"); |
| 64 | + Set<ConstraintViolation<Customer>> violations = validator.validate(customer); |
| 65 | + assertEquals(0, violations.size()); |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + public void whenNumberOrdersValid_thenValidationSucceeds() { |
| 70 | + Customer customer = new Customer(); |
| 71 | + customer.setName("John"); |
| 72 | + customer.setNumberOfOrders(OptionalInt.of(1)); |
| 73 | + Set<ConstraintViolation<Customer>> violations = validator.validate(customer); |
| 74 | + assertEquals(0, violations.size()); |
| 75 | + } |
| 76 | + |
| 77 | + //@Test |
| 78 | + public void whenProfileCompanyNameBlank_thenValidationFails() { |
| 79 | + Customer customer = new Customer(); |
| 80 | + customer.setName("John"); |
| 81 | + Profile profile = new Profile(); |
| 82 | + profile.setCompanyName(" "); |
| 83 | + customer.setProfile(profile); |
| 84 | + Set<ConstraintViolation<Customer>> violations = validator.validate(customer); |
| 85 | + assertEquals(1, violations.size()); |
| 86 | + } |
| 87 | + |
| 88 | +} |
0 commit comments