use of com.linkedin.data.schema.validation.ValidationResult in project rest.li by linkedin.
the class TestRestLiValidation method testCustomValidatorMap.
@Test
public void testCustomValidatorMap() {
// Provide Rest.li annotations manually since the validator is not called from the server or through generated request builders.
Map<String, List<String>> annotations = new HashMap<>();
annotations.put("createOnly", Arrays.asList("stringB", "intB", "UnionFieldWithInlineRecord/com.linkedin.restli.examples.greetings.api.myRecord/foo2", "MapWithTyperefs/*/id"));
annotations.put("readOnly", Arrays.asList("stringA", "intA", "UnionFieldWithInlineRecord/com.linkedin.restli.examples.greetings.api.myRecord/foo1", "ArrayWithInlineRecord/*/bar1", "validationDemoNext/stringB", "validationDemoNext/UnionFieldWithInlineRecord"));
// Invalid entity, because intB is not a multiple of seven.
ValidationDemo.UnionFieldWithInlineRecord unionField1 = new ValidationDemo.UnionFieldWithInlineRecord();
unionField1.setMyEnum(myEnum.FOOFOO);
ValidationDemo entity = new ValidationDemo().setIntB(24).setStringB("some string").setUnionFieldWithInlineRecord(unionField1);
// Validate without the class map
RestLiDataValidator validator = new RestLiDataValidator(annotations, ValidationDemo.class, ResourceMethod.CREATE);
ValidationResult result = validator.validateInput(entity);
Assert.assertTrue(result.isValid());
// Validate with the class map
Map<String, Class<? extends Validator>> validatorClassMap = new HashMap<>();
validatorClassMap.put("seven", SevenValidator.class);
validator = new RestLiDataValidator(annotations, ValidationDemo.class, ResourceMethod.CREATE, validatorClassMap);
result = validator.validateInput(entity);
Assert.assertFalse(result.isValid());
Assert.assertEquals(result.getMessages().size(), 1);
for (Message message : result.getMessages()) {
Assert.assertTrue(message.toString().contains("24 is not a multiple of seven"));
}
}
use of com.linkedin.data.schema.validation.ValidationResult in project rest.li by linkedin.
the class TestRestLiValidationFromClient method testBatchPartialUpdate.
@Test(dataProvider = "provideBatchPartialUpdateData")
public void testBatchPartialUpdate(Object builder, Map<Integer, PatchRequest<ValidationDemo>> inputs, Map<Integer, String> errorMessages) {
for (Map.Entry<Integer, PatchRequest<ValidationDemo>> entry : inputs.entrySet()) {
ValidationResult result = new RootBuilderWrapper<Integer, ValidationDemo>(builder, ValidationDemo.class).batchPartialUpdate().validateInput(entry.getValue());
String expected = errorMessages.get(entry.getKey());
if (expected.isEmpty()) {
Assert.assertEquals(result.isValid(), true);
} else {
Assert.assertEquals(result.isValid(), false);
Assert.assertEquals(result.getMessages().toString(), expected);
}
}
}
use of com.linkedin.data.schema.validation.ValidationResult in project rest.li by linkedin.
the class TestRestLiValidationFromClient method testUpdateSuccess.
@Test(dataProvider = "provideUpdateSuccessData")
public void testUpdateSuccess(Object builder, ValidationDemo validationDemo) {
ValidationResult result = new RootBuilderWrapper<Integer, ValidationDemo>(builder, ValidationDemo.class).update().validateInput(validationDemo);
Assert.assertEquals(result.isValid(), true);
}
use of com.linkedin.data.schema.validation.ValidationResult in project rest.li by linkedin.
the class TestRestLiValidationFromClient method testBatchCreateFailure.
@Test(dataProvider = "provideBatchCreateFailureData")
public void testBatchCreateFailure(Object builder, List<ValidationDemo> validationDemos, List<String> errorMessages) {
int i = 0;
for (ValidationDemo input : validationDemos) {
ValidationResult result = new RootBuilderWrapper<Integer, ValidationDemo>(builder, ValidationDemo.class).batchCreate().validateInput(input);
Assert.assertEquals(result.isValid(), false);
Assert.assertTrue(result.getMessages().toString().contains(errorMessages.get(i++)));
}
}
use of com.linkedin.data.schema.validation.ValidationResult in project rest.li by linkedin.
the class TestRestLiValidationFromClient method testPartialUpdateSuccess.
@Test(dataProvider = "providePartialUpdateSuccessData")
public void testPartialUpdateSuccess(Object builder, String patch) {
PatchRequest<ValidationDemo> patchRequest = PatchBuilder.buildPatchFromString(patch);
ValidationResult result = new RootBuilderWrapper<Integer, ValidationDemo>(builder, ValidationDemo.class).partialUpdate().validateInput(patchRequest);
Assert.assertEquals(result.isValid(), true);
}
Aggregations