Search in sources :

Example 11 with ValidationDemo

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

the class TestRestLiValidation method testBatchGetAutoWithException.

@Test
public void testBatchGetAutoWithException() throws RemoteInvocationException {
    // The resource returns an error for id=0 but a normal result for id=1
    ValidationDemo.UnionFieldWithInlineRecord union = new ValidationDemo.UnionFieldWithInlineRecord();
    union.setMyRecord(new myRecord().setFoo1(100).setFoo2(200));
    ValidationDemo expectedResult = new ValidationDemo().setStringA("a").setStringB("b").setUnionFieldWithInlineRecord(union);
    BatchGetRequest<ValidationDemo> request = new AutoValidationDemosBuilders().batchGet().ids(0, 1).build();
    Response<BatchResponse<ValidationDemo>> response = _restClientAuto.sendRequest(request).getResponse();
    Assert.assertEquals(response.getStatus(), HttpStatus.S_200_OK.getCode());
    Assert.assertEquals((int) response.getEntity().getErrors().get("0").getStatus(), HttpStatus.S_400_BAD_REQUEST.getCode());
    Assert.assertEquals(response.getEntity().getResults().get("1"), expectedResult);
    BatchGetEntityRequest<Integer, ValidationDemo> request2 = new AutoValidationDemosRequestBuilders().batchGet().ids(0, 1).build();
    Response<BatchKVResponse<Integer, EntityResponse<ValidationDemo>>> response2 = _restClientAuto.sendRequest(request2).getResponse();
    Assert.assertEquals(response2.getStatus(), HttpStatus.S_200_OK.getCode());
    Assert.assertEquals((int) response2.getEntity().getErrors().get(0).getStatus(), HttpStatus.S_400_BAD_REQUEST.getCode());
    Assert.assertEquals(response2.getEntity().getResults().get(1).getEntity(), expectedResult);
}
Also used : com.linkedin.restli.examples.greetings.api.myRecord(com.linkedin.restli.examples.greetings.api.myRecord) AutoValidationDemosRequestBuilders(com.linkedin.restli.examples.greetings.client.AutoValidationDemosRequestBuilders) BatchResponse(com.linkedin.restli.common.BatchResponse) BatchKVResponse(com.linkedin.restli.client.response.BatchKVResponse) ValidationDemo(com.linkedin.restli.examples.greetings.api.ValidationDemo) AutoValidationDemosBuilders(com.linkedin.restli.examples.greetings.client.AutoValidationDemosBuilders) Test(org.testng.annotations.Test)

Example 12 with ValidationDemo

use of com.linkedin.restli.examples.greetings.api.ValidationDemo 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 13 with ValidationDemo

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

the class TestRestLiValidationFromClient method testInvalidInputEntityValidation.

@Test
public void testInvalidInputEntityValidation() {
    try {
        ValidationDemosCreateRequestBuilder.validateInput(null);
        Assert.fail("Expected IllegalArgumentException.");
    } catch (IllegalArgumentException e) {
        Assert.assertEquals(e.getMessage(), "Record template is null.");
    }
    try {
        ValidationDemosCreateRequestBuilder.validateInput(new ValidationDemo(null));
        Assert.fail("Expected IllegalArgumentException.");
    } catch (IllegalArgumentException e) {
        Assert.assertEquals(e.getMessage(), "Record template does not have data.");
    }
    try {
        RestLiDataValidator anyRecordValidator = new RestLiDataValidator(Collections.<String, List<String>>emptyMap(), AnyRecord.class, ResourceMethod.CREATE);
        anyRecordValidator.validateInput(new AnyRecord(new DataMap()));
        Assert.fail("Expected IllegalArgumentException.");
    } catch (IllegalArgumentException e) {
        Assert.assertEquals(e.getMessage(), "Record template does not have a schema.");
    }
}
Also used : AnyRecord(com.linkedin.restli.internal.server.methods.AnyRecord) RestLiDataValidator(com.linkedin.restli.common.validation.RestLiDataValidator) ValidationDemo(com.linkedin.restli.examples.greetings.api.ValidationDemo) DataMap(com.linkedin.data.DataMap) Test(org.testng.annotations.Test)

Example 14 with ValidationDemo

use of com.linkedin.restli.examples.greetings.api.ValidationDemo 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++)));
    }
}
Also used : RootBuilderWrapper(com.linkedin.restli.test.util.RootBuilderWrapper) ValidationResult(com.linkedin.data.schema.validation.ValidationResult) ValidationDemo(com.linkedin.restli.examples.greetings.api.ValidationDemo) Test(org.testng.annotations.Test)

Example 15 with ValidationDemo

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