Search in sources :

Example 1 with ValidationDemo

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;
}
Also used : ArrayList(java.util.ArrayList) ValidationResult(com.linkedin.data.schema.validation.ValidationResult) ValidationDemo(com.linkedin.restli.examples.greetings.api.ValidationDemo)

Example 2 with ValidationDemo

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;
}
Also used : ArrayList(java.util.ArrayList) ValidationResult(com.linkedin.data.schema.validation.ValidationResult) ValidationDemo(com.linkedin.restli.examples.greetings.api.ValidationDemo) Finder(com.linkedin.restli.server.annotations.Finder)

Example 3 with ValidationDemo

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;
}
Also used : com.linkedin.restli.examples.greetings.api.myRecord(com.linkedin.restli.examples.greetings.api.myRecord) HashMap(java.util.HashMap) ValidationResult(com.linkedin.data.schema.validation.ValidationResult) ValidationDemo(com.linkedin.restli.examples.greetings.api.ValidationDemo)

Example 4 with ValidationDemo

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);
}
Also used : UpdateResponse(com.linkedin.restli.server.UpdateResponse) RestLiServiceException(com.linkedin.restli.server.RestLiServiceException) BatchUpdateResult(com.linkedin.restli.server.BatchUpdateResult) HashMap(java.util.HashMap) ValidationResult(com.linkedin.data.schema.validation.ValidationResult) HashMap(java.util.HashMap) Map(java.util.Map) ValidationDemo(com.linkedin.restli.examples.greetings.api.ValidationDemo)

Example 5 with ValidationDemo

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");
    }
}
Also used : AutoValidationWithProjectionBuilders(com.linkedin.restli.examples.greetings.client.AutoValidationWithProjectionBuilders) CollectionResponse(com.linkedin.restli.common.CollectionResponse) RootBuilderWrapper(com.linkedin.restli.test.util.RootBuilderWrapper) RestLiResponseException(com.linkedin.restli.client.RestLiResponseException) ValidationDemo(com.linkedin.restli.examples.greetings.api.ValidationDemo) Test(org.testng.annotations.Test)

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