Skip to content

Commit

Permalink
BVTCK-170 Add tests for nested container element traversing
Browse files Browse the repository at this point in the history
  • Loading branch information
gsmet committed Jun 29, 2017
1 parent 87ef2a9 commit 112dda5
Showing 1 changed file with 99 additions and 11 deletions.
Expand Up @@ -11,8 +11,10 @@
import static org.hibernate.beanvalidation.tck.util.ConstraintViolationAssert.pathWith;
import static org.hibernate.beanvalidation.tck.util.ConstraintViolationAssert.violationOf;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
Expand Down Expand Up @@ -47,6 +49,10 @@ public static WebArchive createTestArchive() {

@Test
@SpecAssertion(section = Sections.CONSTRAINTDECLARATIONVALIDATIONPROCESS_CONTAINERELEMENTCONSTRAINTS, id = "f")
@SpecAssertion(section = Sections.VALIDATIONAPI_CONSTRAINTVIOLATION, id = "ac")
@SpecAssertion(section = Sections.VALIDATIONAPI_CONSTRAINTVIOLATION, id = "ad")
@SpecAssertion(section = Sections.VALIDATIONAPI_CONSTRAINTVIOLATION, id = "af")
@SpecAssertion(section = Sections.VALIDATIONAPI_CONSTRAINTVIOLATION, id = "ag")
public void validation_of_nested_type_arguments_works_with_map_of_list_of_optional() {
Set<ConstraintViolation<MapOfLists>> constraintViolations = getValidator().validate( MapOfLists.valid() );
assertNumberOfViolations( constraintViolations, 0 );
Expand Down Expand Up @@ -108,6 +114,11 @@ public void validation_of_nested_type_arguments_works_with_map_of_list_of_option

@Test
@SpecAssertion(section = Sections.CONSTRAINTDECLARATIONVALIDATIONPROCESS_CONTAINERELEMENTCONSTRAINTS, id = "f")
@SpecAssertion(section = Sections.VALIDATIONAPI_CONSTRAINTVIOLATION, id = "ac")
@SpecAssertion(section = Sections.VALIDATIONAPI_CONSTRAINTVIOLATION, id = "ad")
@SpecAssertion(section = Sections.VALIDATIONAPI_CONSTRAINTVIOLATION, id = "ae")
@SpecAssertion(section = Sections.VALIDATIONAPI_CONSTRAINTVIOLATION, id = "af")
@SpecAssertion(section = Sections.VALIDATIONAPI_CONSTRAINTVIOLATION, id = "ag")
public void validation_of_nested_type_arguments_works_with_map_of_list_of_stringproperty() {
Set<ConstraintViolation<MapOfListsWithAutomaticUnwrapping>> constraintViolations = getValidator().validate( MapOfListsWithAutomaticUnwrapping.valid() );
assertNumberOfViolations( constraintViolations, 0 );
Expand Down Expand Up @@ -154,6 +165,47 @@ public void validation_of_nested_type_arguments_works_on_getter_with_map_of_list
);
}

@Test
@SpecAssertion(section = Sections.CONSTRAINTDECLARATIONVALIDATIONPROCESS_CONTAINERELEMENTCONSTRAINTS, id = "f")
@SpecAssertion(section = Sections.VALIDATIONAPI_CONSTRAINTVIOLATION, id = "ac")
@SpecAssertion(section = Sections.VALIDATIONAPI_CONSTRAINTVIOLATION, id = "ae")
@SpecAssertion(section = Sections.VALIDATIONAPI_CONSTRAINTVIOLATION, id = "af")
@SpecAssertion(section = Sections.VALIDATIONAPI_CONSTRAINTVIOLATION, id = "ag")
public void validation_of_nested_type_arguments_works_with_list_of_maps() {
Set<ConstraintViolation<ListOfMaps>> constraintViolations = getValidator().validate( ListOfMaps.valid() );
assertNumberOfViolations( constraintViolations, 0 );

constraintViolations = getValidator().validate( ListOfMaps.invalidValue() );
assertThat( constraintViolations ).containsOnlyViolations(
violationOf( Size.class )
.withPropertyPath( pathWith()
.property( "list" )
.containerElement( "<list element>", true, null, 0, List.class, 0 )
.containerElement( "<map value>", true, "key", null, Map.class, 1 )
)
);
}

@Test
@SpecAssertion(section = Sections.CONSTRAINTDECLARATIONVALIDATIONPROCESS_CONTAINERELEMENTCONSTRAINTS, id = "f")
@SpecAssertion(section = Sections.VALIDATIONAPI_CONSTRAINTVIOLATION, id = "ac")
@SpecAssertion(section = Sections.VALIDATIONAPI_CONSTRAINTVIOLATION, id = "af")
@SpecAssertion(section = Sections.VALIDATIONAPI_CONSTRAINTVIOLATION, id = "ag")
public void validation_of_nested_type_arguments_works_with_list_of_iterables() {
Set<ConstraintViolation<ListOfIterables>> constraintViolations = getValidator().validate( ListOfIterables.valid() );
assertNumberOfViolations( constraintViolations, 0 );

constraintViolations = getValidator().validate( ListOfIterables.invalid() );
assertThat( constraintViolations ).containsOnlyViolations(
violationOf( Size.class )
.withPropertyPath( pathWith()
.property( "list" )
.containerElement( "<list element>", true, null, 0, List.class, 0 )
.containerElement( "<iterable element>", true, null, null, Set.class, 0 )
)
);
}

private static class MapOfLists {

private Map<@Size(min = 2) String, @NotNull @Size(min = 2) List<Optional<@Size(min = 3) String>>> map;
Expand Down Expand Up @@ -262,25 +314,61 @@ private static MapOfListsWithAutomaticUnwrapping invalidListElement() {
}
}

@SuppressWarnings({ "unchecked", "unused" })
private static class ArrayOfOptionalsWithAutomaticUnwrapping {
private static class ListOfMaps {

private List<Map<@Size(min = 2) String, @NotNull @Size(min = 2) String>> list;

private static ListOfMaps valid() {
ListOfMaps foo = new ListOfMaps();

Map<String, String> map = new HashMap<>();
map.put( "key", "value" );

foo.list = new ArrayList<>();
foo.list.add( map );

return foo;
}

private static ListOfMaps invalidValue() {
ListOfMaps foo = new ListOfMaps();

Map<String, String> map = new HashMap<>();
map.put( "key", "v" );

foo.list = new ArrayList<>();
foo.list.add( map );

return foo;
}
}

private static class ListOfIterables {

private List<Set<@Size(min = 2) String>> list;

private Optional<@Size(min = 3) StringProperty> @NotNull [] array;
private static ListOfIterables valid() {
ListOfIterables foo = new ListOfIterables();

private static ArrayOfOptionalsWithAutomaticUnwrapping valid() {
ArrayOfOptionalsWithAutomaticUnwrapping baz = new ArrayOfOptionalsWithAutomaticUnwrapping();
Set<String> set = new HashSet<>();
set.add( "value" );

baz.array = new Optional[] { Optional.of( new SimpleStringProperty( "string1" ) ), Optional.of( new SimpleStringProperty( "string2" ) ) };
foo.list = new ArrayList<>();
foo.list.add( set );

return baz;
return foo;
}

private static ArrayOfOptionalsWithAutomaticUnwrapping invalidArray() {
ArrayOfOptionalsWithAutomaticUnwrapping baz = new ArrayOfOptionalsWithAutomaticUnwrapping();
private static ListOfIterables invalid() {
ListOfIterables foo = new ListOfIterables();

baz.array = new Optional[] { null, Optional.of( new SimpleStringProperty( "st" ) ) };
Set<String> set = new HashSet<>();
set.add( "v" );

return baz;
foo.list = new ArrayList<>();
foo.list.add( set );

return foo;
}
}
}

0 comments on commit 112dda5

Please sign in to comment.