Search in sources :

Example 11 with ValidationResult

use of com.linkedin.data.schema.validation.ValidationResult 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 12 with ValidationResult

use of com.linkedin.data.schema.validation.ValidationResult 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 13 with ValidationResult

use of com.linkedin.data.schema.validation.ValidationResult 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 14 with ValidationResult

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"));
    }
}
Also used : Message(com.linkedin.data.message.Message) HashMap(java.util.HashMap) ValidationResult(com.linkedin.data.schema.validation.ValidationResult) ValidationDemo(com.linkedin.restli.examples.greetings.api.ValidationDemo) RestLiDataValidator(com.linkedin.restli.common.validation.RestLiDataValidator) List(java.util.List) ArrayList(java.util.ArrayList) BeforeClass(org.testng.annotations.BeforeClass) AfterClass(org.testng.annotations.AfterClass) Validator(com.linkedin.data.schema.validator.Validator) AbstractValidator(com.linkedin.data.schema.validator.AbstractValidator) RestLiDataValidator(com.linkedin.restli.common.validation.RestLiDataValidator) Test(org.testng.annotations.Test)

Example 15 with ValidationResult

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);
        }
    }
}
Also used : RootBuilderWrapper(com.linkedin.restli.test.util.RootBuilderWrapper) PatchRequest(com.linkedin.restli.common.PatchRequest) ValidationResult(com.linkedin.data.schema.validation.ValidationResult) DataMap(com.linkedin.data.DataMap) Map(java.util.Map) Test(org.testng.annotations.Test)

Aggregations

ValidationResult (com.linkedin.data.schema.validation.ValidationResult)38 Test (org.testng.annotations.Test)17 ValidationOptions (com.linkedin.data.schema.validation.ValidationOptions)16 DataMap (com.linkedin.data.DataMap)12 ValidationDemo (com.linkedin.restli.examples.greetings.api.ValidationDemo)11 RootBuilderWrapper (com.linkedin.restli.test.util.RootBuilderWrapper)8 DataSchema (com.linkedin.data.schema.DataSchema)7 ArrayList (java.util.ArrayList)7 RecordDataSchema (com.linkedin.data.schema.RecordDataSchema)6 HashMap (java.util.HashMap)6 Message (com.linkedin.data.message.Message)5 RestLiServiceException (com.linkedin.restli.server.RestLiServiceException)5 Map (java.util.Map)5 PatchRequest (com.linkedin.restli.common.PatchRequest)4 TestUtil.dataMapFromString (com.linkedin.data.TestUtil.dataMapFromString)3 TestUtil.dataSchemaFromString (com.linkedin.data.TestUtil.dataSchemaFromString)3 DataElement (com.linkedin.data.element.DataElement)3 SimpleDataElement (com.linkedin.data.element.SimpleDataElement)3 NamedDataSchema (com.linkedin.data.schema.NamedDataSchema)3 ResourceMethod (com.linkedin.restli.common.ResourceMethod)3