Skip to content

Commit

Permalink
BVTCK-158 Add a test for cascading validation with 2 inner extractions
Browse files Browse the repository at this point in the history
  • Loading branch information
gsmet authored and gunnarmorling committed Jun 23, 2017
1 parent 9e38d1a commit c636ae8
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 0 deletions.
Expand Up @@ -12,6 +12,7 @@
import static org.hibernate.beanvalidation.tck.util.ConstraintViolationAssert.violationOf;

import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
Expand All @@ -27,6 +28,8 @@

import org.hibernate.beanvalidation.tck.beanvalidation.Sections;
import org.hibernate.beanvalidation.tck.tests.AbstractTCKTest;
import org.hibernate.beanvalidation.tck.tests.validation.graphnavigation.containerelement.model.Address;
import org.hibernate.beanvalidation.tck.tests.validation.graphnavigation.containerelement.model.AddressType;
import org.hibernate.beanvalidation.tck.tests.validation.graphnavigation.containerelement.model.Cinema;
import org.hibernate.beanvalidation.tck.tests.validation.graphnavigation.containerelement.model.EmailAddress;
import org.hibernate.beanvalidation.tck.tests.validation.graphnavigation.containerelement.model.Reference;
Expand Down Expand Up @@ -81,6 +84,40 @@ public void testNestedValid() {
);
}

@Test
@SpecAssertion(section = Sections.CONSTRAINTDECLARATIONVALIDATIONPROCESS_REQUIREMENTS_GRAPHVALIDATION, id = "l")
@SpecAssertion(section = Sections.CONSTRAINTDECLARATIONVALIDATIONPROCESS_REQUIREMENTS_GRAPHVALIDATION, id = "m")
public void testNestedValidWithTwoInnerExtractions() {
Validator validator = getValidator();

Set<ConstraintViolation<AddressBook>> constraintViolations = validator.validate( AddressBook.valid() );

assertNumberOfViolations( constraintViolations, 0 );

constraintViolations = validator.validate( AddressBook.invalid() );

assertThat( constraintViolations ).containsOnlyViolations(
violationOf( NotBlank.class )
.withPropertyPath( pathWith()
.property( "addressesPerTypePerPerson" )
.containerElement( "<map value>", true, "Firstname Lastname", null, Map.class, 1 )
.property( "type", true, AddressType.invalid(), null, Map.class, 0 )
),
violationOf( NotBlank.class )
.withPropertyPath( pathWith()
.property( "addressesPerTypePerPerson" )
.containerElement( "<map value>", true, "Firstname Lastname", null, Map.class, 1 )
.property( "zipCode", true, AddressType.invalid(), null, Map.class, 1 )
),
violationOf( NotBlank.class )
.withPropertyPath( pathWith()
.property( "addressesPerTypePerPerson" )
.containerElement( "<map value>", true, "Firstname Lastname", null, Map.class, 1 )
.property( "zipCode", true, AddressType.valid(), null, Map.class, 1 )
)
);
}

@Test
@SpecAssertion(section = Sections.CONSTRAINTDECLARATIONVALIDATIONPROCESS_REQUIREMENTS_GRAPHVALIDATION, id = "l")
@SpecAssertion(section = Sections.CONSTRAINTDECLARATIONVALIDATIONPROCESS_REQUIREMENTS_GRAPHVALIDATION, id = "m")
Expand Down Expand Up @@ -269,4 +306,31 @@ private static NestedCascadingListWithValidAllAlongTheWay withNullList() {
return valid;
}
}

private static class AddressBook {

private Map<String, Map<@Valid AddressType, @Valid Address>> addressesPerTypePerPerson = new HashMap<>();

private static AddressBook valid() {
Map<AddressType, Address> addressesPerType = new HashMap<>();
addressesPerType.put( AddressType.valid(), Address.valid() );

AddressBook addressBook = new AddressBook();
addressBook.addressesPerTypePerPerson.put( "Firstname Lastname", addressesPerType );

return addressBook;
}

private static AddressBook invalid() {
Map<AddressType, Address> addressesPerType = new HashMap<>();
addressesPerType.put( AddressType.valid(), Address.valid() );
addressesPerType.put( AddressType.valid(), Address.invalid() );
addressesPerType.put( AddressType.invalid(), Address.invalid() );

AddressBook addressBook = new AddressBook();
addressBook.addressesPerTypePerPerson.put( "Firstname Lastname", addressesPerType );

return addressBook;
}
}
}
@@ -0,0 +1,35 @@
/**
* Bean Validation TCK
*
* License: Apache License, Version 2.0
* See the license.txt file in the root directory or <http://www.apache.org/licenses/LICENSE-2.0>.
*/
package org.hibernate.beanvalidation.tck.tests.validation.graphnavigation.containerelement.model;

import javax.validation.constraints.NotBlank;

public class Address {

@NotBlank
private final String street;

@NotBlank
private final String zipCode;

@NotBlank
private final String city;

private Address(String street, String zipCode, String city) {
this.street = street;
this.zipCode = zipCode;
this.city = city;
}

public static Address invalid() {
return new Address( "avenue Félix Faure", "", "Lyon" );
}

public static Address valid() {
return new Address( "avenue Félix Faure", "69003", "Lyon" );
}
}
@@ -0,0 +1,31 @@
/**
* Bean Validation TCK
*
* License: Apache License, Version 2.0
* See the license.txt file in the root directory or <http://www.apache.org/licenses/LICENSE-2.0>.
*/
package org.hibernate.beanvalidation.tck.tests.validation.graphnavigation.containerelement.model;

import javax.validation.constraints.NotBlank;

public class AddressType {

private static final AddressType INVALID = new AddressType( "" );

private static final AddressType VALID = new AddressType( "Personal" );

@NotBlank
private final String type;

private AddressType(String type) {
this.type = type;
}

public static AddressType invalid() {
return INVALID;
}

public static AddressType valid() {
return VALID;
}
}

0 comments on commit c636ae8

Please sign in to comment.