use of com.linkedin.restli.examples.greetings.api.ValidationDemo in project rest.li by linkedin.
the class ValidationDemoResource method getAll.
@RestMethod.GetAll
public List<ValidationDemo> getAll(@ValidatorParam RestLiDataValidator validator) {
List<ValidationDemo> validationDemos = new ArrayList<ValidationDemo>();
// Generate entities with stringA fields that are too long
for (int i = 0; i < 10; i++) {
ValidationDemo.UnionFieldWithInlineRecord union = new ValidationDemo.UnionFieldWithInlineRecord();
union.setMyEnum(myEnum.FOOFOO);
validationDemos.add(new ValidationDemo().setStringA("This string is too long to pass validation.").setStringB("stringB").setUnionFieldWithInlineRecord(union));
}
// Validate outgoing data
for (ValidationDemo entity : validationDemos) {
ValidationResult result = validator.validateOutput(entity);
check(!result.isValid());
check(result.getMessages().toString().contains("/stringA :: length of \"This string is too long to pass validation.\" is out of range 1...10"));
}
// Fix entities
for (ValidationDemo validationDemo : validationDemos) {
validationDemo.setStringA("short str");
}
// Validate again
for (ValidationDemo entity : validationDemos) {
ValidationResult result = validator.validateOutput(entity);
check(result.isValid());
}
return validationDemos;
}
use of com.linkedin.restli.examples.greetings.api.ValidationDemo in project rest.li by linkedin.
the class ValidationDemoResource method search.
@Finder("search")
public List<ValidationDemo> search(@QueryParam("intA") Integer intA, @ValidatorParam RestLiDataValidator validator) {
List<ValidationDemo> validationDemos = new ArrayList<ValidationDemo>();
// Generate entities that are missing stringB fields
for (int i = 0; i < 3; i++) {
ValidationDemo.UnionFieldWithInlineRecord union = new ValidationDemo.UnionFieldWithInlineRecord();
union.setMyEnum(myEnum.FOOFOO);
validationDemos.add(new ValidationDemo().setStringA("valueA").setIntA(intA).setUnionFieldWithInlineRecord(union));
}
// Validate outgoing data
for (ValidationDemo entity : validationDemos) {
ValidationResult result = validator.validateOutput(entity);
check(!result.isValid());
check(result.getMessages().toString().contains("/stringB :: field is required but not found"));
}
// Fix entities
for (ValidationDemo validationDemo : validationDemos) {
validationDemo.setStringB("valueB");
}
// Validate again
for (ValidationDemo entity : validationDemos) {
ValidationResult result = validator.validateOutput(entity);
check(result.isValid());
}
return validationDemos;
}
use of com.linkedin.restli.examples.greetings.api.ValidationDemo in project rest.li by linkedin.
the class ValidationDemoResource method batchGet.
@RestMethod.BatchGet
public Map<Integer, ValidationDemo> batchGet(Set<Integer> ids, @ValidatorParam RestLiDataValidator validator) {
Map<Integer, ValidationDemo> resultMap = new HashMap<Integer, ValidationDemo>();
// Generate entities that are missing a required field
for (Integer id : ids) {
ValidationDemo.UnionFieldWithInlineRecord union = new ValidationDemo.UnionFieldWithInlineRecord();
union.setMyRecord(new myRecord());
ValidationDemo validationDemo = new ValidationDemo().setStringA("a").setStringB("b").setUnionFieldWithInlineRecord(union);
resultMap.put(id, validationDemo);
}
;
// Validate outgoing data
for (ValidationDemo entity : resultMap.values()) {
ValidationResult result = validator.validateOutput(entity);
check(!result.isValid());
check(result.getMessages().toString().contains("/UnionFieldWithInlineRecord/com.linkedin.restli.examples.greetings.api.myRecord/foo1 :: field is required but not found"));
}
// Fix entities
for (Integer id : ids) {
resultMap.get(id).getUnionFieldWithInlineRecord().getMyRecord().setFoo1(1234);
}
// Validate again
for (ValidationDemo entity : resultMap.values()) {
ValidationResult result = validator.validateOutput(entity);
check(result.isValid());
}
return resultMap;
}
use of com.linkedin.restli.examples.greetings.api.ValidationDemo in project rest.li by linkedin.
the class ValidationDemoResource method batchUpdate.
@RestMethod.BatchUpdate
public BatchUpdateResult<Integer, ValidationDemo> batchUpdate(final BatchUpdateRequest<Integer, ValidationDemo> entities, @ValidatorParam RestLiDataValidator validator) {
Map<Integer, UpdateResponse> results = new HashMap<Integer, UpdateResponse>();
Map<Integer, RestLiServiceException> errors = new HashMap<Integer, RestLiServiceException>();
for (Map.Entry<Integer, ValidationDemo> entry : entities.getData().entrySet()) {
Integer key = entry.getKey();
ValidationDemo entity = entry.getValue();
ValidationResult result = validator.validateInput(entity);
if (result.isValid()) {
results.put(key, new UpdateResponse(HttpStatus.S_204_NO_CONTENT));
} else {
errors.put(key, new RestLiServiceException(HttpStatus.S_422_UNPROCESSABLE_ENTITY, result.getMessages().toString()));
}
}
return new BatchUpdateResult<Integer, ValidationDemo>(results, errors);
}
use of com.linkedin.restli.examples.greetings.api.ValidationDemo in project rest.li by linkedin.
the class TestResLiValidationWithProjection method testProjectionWithInvalidFields.
@Test
public void testProjectionWithInvalidFields() throws RemoteInvocationException {
RootBuilderWrapper<Integer, ValidationDemo> wrapper = new RootBuilderWrapper<Integer, ValidationDemo>(new AutoValidationWithProjectionBuilders());
Request<CollectionResponse<ValidationDemo>> request = wrapper.findBy("searchWithProjection").fields(//invalid
ValidationDemo.fields().stringA(), ValidationDemo.fields().stringB(), //invalid
ValidationDemo.fields().includedA(), //invalid
ValidationDemo.fields().UnionFieldWithInlineRecord().MyRecord().foo1(), ValidationDemo.fields().UnionFieldWithInlineRecord().MyRecord().foo2(), ValidationDemo.fields().ArrayWithInlineRecord().items().bar1(), //invalid
ValidationDemo.fields().ArrayWithInlineRecord().items().bar2(), ValidationDemo.fields().MapWithTyperefs().values().id(), //invalid
ValidationDemo.fields().MapWithTyperefs().values().tone(), //invalid
ValidationDemo.fields().validationDemoNext().stringA()).build();
try {
_restClientAuto.sendRequest(request).getResponse();
} catch (RestLiResponseException e) {
Assert.assertEquals(e.getServiceErrorMessage(), "ERROR :: /validationDemoNext/stringA :: length of \"invalid, length is larger than the max\" is out of range 1...10\n" + "ERROR :: /includedA :: length of \"invalid, length is larger than the max\" is out of range 1...10\n" + "ERROR :: /UnionFieldWithInlineRecord/com.linkedin.restli.examples.greetings.api.myRecord/foo1 :: field is required but not found and has no default value\n" + "ERROR :: /ArrayWithInlineRecord/0/bar2 :: field is required but not found and has no default value\n" + "ERROR :: /MapWithTyperefs/foo/tone :: field is required but not found and has no default value\n" + "ERROR :: /stringA :: field is required but not found and has no default value\n");
}
}
Aggregations