Skip to content

Instantly share code, notes, and snippets.

@Hromenique
Created October 30, 2017 15:54
Show Gist options
  • Save Hromenique/6fe54a874a52b95f8bd6b61477d8303d to your computer and use it in GitHub Desktop.
Save Hromenique/6fe54a874a52b95f8bd6b61477d8303d to your computer and use it in GitHub Desktop.
Exemplo de adição de constraint validation com Bean Validaton
public class ConstraintValidatorContextImplTest {
private static String message = "message";
@Test
public void testIterableIndexed() {
ConstraintValidatorContextImpl context = createEmptyConstraintValidatorContextImpl();
context.buildConstraintViolationWithTemplate( message )
.addPropertyNode( "foo" )
.addPropertyNode( "bar" ).inIterable().atIndex( 3 )
.addConstraintViolation();
List<MessageAndPath> messageAndPathList = context.getMessageAndPathList();
assertMessageAndPath( messageAndPathList.get( 0 ), message, "foo[3].bar" );
}
@Test
public void testIterableKeyed() {
ConstraintValidatorContextImpl context = createEmptyConstraintValidatorContextImpl();
context.buildConstraintViolationWithTemplate( message )
.addPropertyNode( "foo" )
.addPropertyNode( null ).inIterable().atKey( "test" )
.addConstraintViolation();
List<MessageAndPath> messageAndPathList = context.getMessageAndPathList();
assertMessageAndPath( messageAndPathList.get( 0 ), message, "foo[test]" );
}
@Test
public void testIterableWithKeyFollowedBySimpleNodes() {
ConstraintValidatorContextImpl context = createEmptyConstraintValidatorContextImpl();
context.buildConstraintViolationWithTemplate( message )
.addPropertyNode( "foo" )
.addPropertyNode( "bar" ).inIterable().atKey( "test" )
.addPropertyNode( "fubar" )
.addConstraintViolation();
List<MessageAndPath> messageAndPathList = context.getMessageAndPathList();
assertMessageAndPath( messageAndPathList.get( 0 ), message, "foo[test].bar.fubar" );
}
@Test
public void testIterableKeyedAndIndexed() {
ConstraintValidatorContextImpl context = createEmptyConstraintValidatorContextImpl();
context.buildConstraintViolationWithTemplate( message )
.addPropertyNode( "foo" )
.addPropertyNode( "bar" ).inIterable().atKey( "test" )
.addPropertyNode( "fubar" ).inIterable().atIndex( 10 )
.addConstraintViolation();
List<MessageAndPath> messageAndPathList = context.getMessageAndPathList();
assertMessageAndPath( messageAndPathList.get( 0 ), message, "foo[test].bar[10].fubar" );
}
@Test
public void testMultipleInIterable() {
ConstraintValidatorContextImpl context = createEmptyConstraintValidatorContextImpl();
context.buildConstraintViolationWithTemplate( message )
.addPropertyNode( "foo" )
.addPropertyNode( "bar" ).inIterable().atKey( "test" )
.addPropertyNode( "fubar" ).inIterable()
.addConstraintViolation();
List<MessageAndPath> messageAndPathList = context.getMessageAndPathList();
assertMessageAndPath( messageAndPathList.get( 0 ), message, "foo[test].bar[].fubar" );
}
@Test
public void testMultipleSimpleNodes() {
ConstraintValidatorContextImpl context = createEmptyConstraintValidatorContextImpl();
context.buildConstraintViolationWithTemplate( message )
.addPropertyNode( "foo" )
.addPropertyNode( "bar" )
.addPropertyNode( "test" )
.addConstraintViolation();
List<MessageAndPath> messageAndPathList = context.getMessageAndPathList();
assertMessageAndPath( messageAndPathList.get( 0 ), message, "foo.bar.test" );
}
@Test
public void testLongPath() {
ConstraintValidatorContextImpl context = createEmptyConstraintValidatorContextImpl();
context.buildConstraintViolationWithTemplate( message )
.addPropertyNode( "a" )
.addPropertyNode( "b" ).inIterable().atKey( "key1" )
.addPropertyNode( "c" ).inIterable()
.addPropertyNode( "d" )
.addPropertyNode( "e" )
.addPropertyNode( "f" ).inIterable().atKey( "key2" )
.addConstraintViolation();
List<MessageAndPath> messageAndPathList = context.getMessageAndPathList();
assertMessageAndPath( messageAndPathList.get( 0 ), message, "a[key1].b[].c.d.e[key2].f" );
}
@Test
public void testMultipleMessages() {
String message1 = "message1";
String message2 = "message2";
ConstraintValidatorContextImpl context = createEmptyConstraintValidatorContextImpl();
context.buildConstraintViolationWithTemplate( message1 )
.addPropertyNode( "foo" )
.addPropertyNode( "bar" ).inIterable().atKey( "key" )
.addConstraintViolation();
context.buildConstraintViolationWithTemplate( message2 )
.addConstraintViolation();
List<MessageAndPath> messageAndPathList = context.getMessageAndPathList();
assertTrue( messageAndPathList.size() == 2 );
assertMessageAndPath( messageAndPathList.get( 0 ), message1, "foo[key].bar" );
assertMessageAndPath( messageAndPathList.get( 1 ), message2, "" );
}
@Test(expectedExceptions = ValidationException.class)
public void testUnwrapToImplementationCausesValidationException() {
ConstraintValidatorContext context = createEmptyConstraintValidatorContextImpl();
context.unwrap( ConstraintValidatorContextImpl.class );
}
@Test
public void testUnwrapToPublicTypesSucceeds() {
ConstraintValidatorContext context = createEmptyConstraintValidatorContextImpl();
ConstraintValidatorContext asConstraintValidatorContext = context.unwrap( ConstraintValidatorContext.class );
assertSame( asConstraintValidatorContext, context );
java.lang.Object asObject = context.unwrap( java.lang.Object.class );
assertSame( asObject, context );
}
private ConstraintValidatorContextImpl createEmptyConstraintValidatorContextImpl() {
PathImpl path = PathImpl.createRootPath();
path.addBeanNode();
ConstraintValidatorContextImpl context = new ConstraintValidatorContextImpl(
null, path, null
);
context.disableDefaultConstraintViolation();
return context;
}
private void assertMessageAndPath(MessageAndPath messageAndPath, String expectedMessage, String expectedPath) {
assertTrue(
pathsAreEqual( messageAndPath.getPath(), PathImpl.createPathFromString( expectedPath ) ),
"Path do not match. Actual: '" + messageAndPath.getPath() + "' Expected: '" + expectedPath + "'"
);
assertEquals( messageAndPath.getMessage(), expectedMessage, "Wrong message" );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment