Search in sources :

Example 31 with ValidationDemo

use of com.linkedin.restli.examples.greetings.api.ValidationDemo in project rest.li by linkedin.

the class ValidationDemoResource method batchUpdate.

@RestMethod.BatchPartialUpdate
public BatchUpdateResult<Integer, ValidationDemo> batchUpdate(final BatchPatchRequest<Integer, ValidationDemo> entityUpdates, @ValidatorParam RestLiDataValidator validator) {
    Map<Integer, UpdateResponse> results = new HashMap<Integer, UpdateResponse>();
    Map<Integer, RestLiServiceException> errors = new HashMap<Integer, RestLiServiceException>();
    for (Map.Entry<Integer, PatchRequest<ValidationDemo>> entry : entityUpdates.getData().entrySet()) {
        Integer key = entry.getKey();
        PatchRequest<ValidationDemo> patch = entry.getValue();
        ValidationResult result = validator.validateInput(patch);
        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);
}
Also used : HashMap(java.util.HashMap) PatchRequest(com.linkedin.restli.common.PatchRequest) BatchPatchRequest(com.linkedin.restli.server.BatchPatchRequest) ValidationResult(com.linkedin.data.schema.validation.ValidationResult) ValidationDemo(com.linkedin.restli.examples.greetings.api.ValidationDemo) UpdateResponse(com.linkedin.restli.server.UpdateResponse) RestLiServiceException(com.linkedin.restli.server.RestLiServiceException) BatchUpdateResult(com.linkedin.restli.server.BatchUpdateResult) HashMap(java.util.HashMap) Map(java.util.Map)

Example 32 with ValidationDemo

use of com.linkedin.restli.examples.greetings.api.ValidationDemo in project rest.li by linkedin.

the class ValidationDemoResource method get.

@RestMethod.Get
public ValidationDemo get(final Integer key, @ValidatorParam RestLiDataValidator validator) {
    // Generate an entity that does not conform to the data schema
    ValidationDemo.UnionFieldWithInlineRecord union = new ValidationDemo.UnionFieldWithInlineRecord();
    union.setMyEnum(myEnum.BARBAR);
    ValidationDemo validationDemo = new ValidationDemo().setStringA("stringA is readOnly").setUnionFieldWithInlineRecord(union);
    // Validate the entity
    ValidationResult result = validator.validateOutput(validationDemo);
    check(!result.isValid());
    String errorMessages = result.getMessages().toString();
    check(errorMessages.contains("/stringA :: length of \"stringA is readOnly\" is out of range 1...10"));
    check(errorMessages.contains("/stringB :: field is required but not found"));
    // Fix the entity
    validationDemo.setStringA("abcd").setStringB("stringB");
    // Validate the entity again
    result = validator.validateOutput(validationDemo);
    check(result.isValid());
    return validationDemo;
}
Also used : ValidationResult(com.linkedin.data.schema.validation.ValidationResult) ValidationDemo(com.linkedin.restli.examples.greetings.api.ValidationDemo)

Aggregations

ValidationDemo (com.linkedin.restli.examples.greetings.api.ValidationDemo)32 Test (org.testng.annotations.Test)13 ValidationResult (com.linkedin.data.schema.validation.ValidationResult)11 com.linkedin.restli.examples.greetings.api.myRecord (com.linkedin.restli.examples.greetings.api.myRecord)7 RestLiServiceException (com.linkedin.restli.server.RestLiServiceException)7 RootBuilderWrapper (com.linkedin.restli.test.util.RootBuilderWrapper)7 HashMap (java.util.HashMap)7 ArrayList (java.util.ArrayList)5 CollectionResponse (com.linkedin.restli.common.CollectionResponse)4 MyItemArray (com.linkedin.restli.examples.greetings.api.MyItemArray)4 com.linkedin.restli.examples.greetings.api.myItem (com.linkedin.restli.examples.greetings.api.myItem)4 AutoValidationDemosBuilders (com.linkedin.restli.examples.greetings.client.AutoValidationDemosBuilders)4 AutoValidationDemosRequestBuilders (com.linkedin.restli.examples.greetings.client.AutoValidationDemosRequestBuilders)4 BatchUpdateResult (com.linkedin.restli.server.BatchUpdateResult)4 UpdateResponse (com.linkedin.restli.server.UpdateResponse)4 Map (java.util.Map)4 RestLiResponseException (com.linkedin.restli.client.RestLiResponseException)3 RestLiDataValidator (com.linkedin.restli.common.validation.RestLiDataValidator)3 Greeting (com.linkedin.restli.examples.greetings.api.Greeting)3 GreetingMap (com.linkedin.restli.examples.greetings.api.GreetingMap)3