container elements validation ex (#2707)

* container elements validation ex

* comment custom container validation
This commit is contained in:
lor6 2017-10-04 21:00:44 +03:00 committed by Grzegorz Piwowarek
parent dcc7fa65dd
commit 8a77746e4d
6 changed files with 204 additions and 0 deletions

View File

@ -0,0 +1,66 @@
package org.baeldung;
import java.util.List;
import java.util.Optional;
import java.util.OptionalInt;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.PositiveOrZero;
public class Customer {
@NotBlank(message="Name cannot be empty")
private String name;
private List<@NotBlank(message="Address must not be blank") String> addresses;
private Integer age;
@PositiveOrZero
private OptionalInt numberOfOrders;
//@NotBlank
private Profile profile;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<String> getAddresses() {
return addresses;
}
public void setAddresses(List<String> addresses) {
this.addresses = addresses;
}
public Optional<@Min(18) Integer> getAge() {
return Optional.ofNullable(age);
}
public void setAge(Integer age) {
this.age = age;
}
public OptionalInt getNumberOfOrders() {
return numberOfOrders;
}
public void setNumberOfOrders(OptionalInt numberOfOrders) {
this.numberOfOrders = numberOfOrders;
}
public Profile getProfile() {
return profile;
}
public void setProfile(Profile profile) {
this.profile = profile;
}
}

View File

@ -0,0 +1,19 @@
package org.baeldung;
import java.util.Map;
import javax.validation.constraints.Email;
import javax.validation.constraints.NotNull;
public class CustomerMap {
private Map<@Email(message="Must be a valid email") String, @NotNull Customer> customers;
public Map<String, Customer> getCustomers() {
return customers;
}
public void setCustomers(Map<String, Customer> customers) {
this.customers = customers;
}
}

View File

@ -0,0 +1,13 @@
package org.baeldung;
public class Profile {
private String companyName;
public String getCompanyName() {
return companyName;
}
public void setCompanyName(String companyName) {
this.companyName = companyName;
}
}

View File

@ -0,0 +1,17 @@
package org.baeldung.valueextractors;
import javax.validation.valueextraction.ExtractedValue;
import javax.validation.valueextraction.UnwrapByDefault;
import javax.validation.valueextraction.ValueExtractor;
import org.baeldung.Profile;
@UnwrapByDefault
public class ProfileValueExtractor implements ValueExtractor<@ExtractedValue(type = String.class) Profile> {
@Override
public void extractValues(Profile originalValue, ValueExtractor.ValueReceiver receiver) {
receiver.value(null, originalValue.getCompanyName());
}
}

View File

@ -0,0 +1 @@
org.baeldung.valueextractors.ProfileValueExtractor

View File

@ -0,0 +1,88 @@
package org.baeldung;
import static org.junit.Assert.assertEquals;
import java.util.Collections;
import java.util.OptionalInt;
import java.util.Set;
import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;
import org.baeldung.valueextractors.ProfileValueExtractor;
import org.junit.Before;
import org.junit.Test;
public class ContainerValidationIntegrationTest {
private Validator validator;
@Before
public void setup() {
ValidatorFactory factory = Validation.byDefaultProvider().configure()
.addValueExtractor(new ProfileValueExtractor()).buildValidatorFactory();
validator = factory.getValidator();
}
@Test
public void whenEmptyAddress_thenValidationFails() {
Customer customer = new Customer();
customer.setName("John");
customer.setAddresses(Collections.singletonList(" "));
Set<ConstraintViolation<Customer>> violations = validator.validate(customer);
assertEquals(1, violations.size());
assertEquals("Address must not be blank", violations.iterator()
.next()
.getMessage());
}
@Test
public void whenInvalidEmail_thenValidationFails() {
CustomerMap map = new CustomerMap();
map.setCustomers(Collections.singletonMap("john", new Customer()));
Set<ConstraintViolation<CustomerMap>> violations = validator.validate(map);
assertEquals(1, violations.size());
assertEquals("Must be a valid email", violations.iterator()
.next()
.getMessage());
}
@Test
public void whenAgeTooLow_thenValidationFails() {
Customer customer = new Customer();
customer.setName("John");
customer.setAge(15);
Set<ConstraintViolation<Customer>> violations = validator.validate(customer);
assertEquals(1, violations.size());
}
@Test
public void whenAgeNull_thenValidationSucceeds() {
Customer customer = new Customer();
customer.setName("John");
Set<ConstraintViolation<Customer>> violations = validator.validate(customer);
assertEquals(0, violations.size());
}
@Test
public void whenNumberOrdersValid_thenValidationSucceeds() {
Customer customer = new Customer();
customer.setName("John");
customer.setNumberOfOrders(OptionalInt.of(1));
Set<ConstraintViolation<Customer>> violations = validator.validate(customer);
assertEquals(0, violations.size());
}
//@Test
public void whenProfileCompanyNameBlank_thenValidationFails() {
Customer customer = new Customer();
customer.setName("John");
Profile profile = new Profile();
profile.setCompanyName(" ");
customer.setProfile(profile);
Set<ConstraintViolation<Customer>> violations = validator.validate(customer);
assertEquals(1, violations.size());
}
}