Search in sources :

Example 1 with ValidationDemoCriteria

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

the class TestRestLiValidation method testBatchFinderAutoWithErrorCriteriaResult.

@Test(dataProvider = "autoBuilders")
public void testBatchFinderAutoWithErrorCriteriaResult(Object builder) throws RemoteInvocationException {
    ValidationDemoCriteria c1 = new ValidationDemoCriteria().setIntA(5555).setStringB("hello");
    ValidationDemoCriteria c2 = new ValidationDemoCriteria().setIntA(4444).setStringB("world");
    Request<BatchCollectionResponse<ValidationDemo>> request = new RootBuilderWrapper<Integer, ValidationDemo>(builder).batchFindBy("searchValidationDemos").setQueryParam("criteria", Arrays.asList(c1, c2)).build();
    _restClientAuto.sendRequest(request).getResponse();
    Response<BatchCollectionResponse<ValidationDemo>> response = _restClientAuto.sendRequest(request).getResponse();
    Assert.assertEquals(response.getStatus(), HttpStatus.S_200_OK.getCode());
}
Also used : BatchCollectionResponse(com.linkedin.restli.common.BatchCollectionResponse) ValidationDemoCriteria(com.linkedin.restli.examples.greetings.api.ValidationDemoCriteria) ValidationDemo(com.linkedin.restli.examples.greetings.api.ValidationDemo) Test(org.testng.annotations.Test)

Example 2 with ValidationDemoCriteria

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

the class TestRestLiValidation method testBatchFinderAutoWithOverLengthField.

@Test(dataProvider = "autoBuilders")
public void testBatchFinderAutoWithOverLengthField(Object builder) throws RemoteInvocationException {
    try {
        ValidationDemoCriteria c1 = new ValidationDemoCriteria().setIntA(2222).setStringB("hello");
        ValidationDemoCriteria c2 = new ValidationDemoCriteria().setIntA(4444).setStringB("world");
        Request<BatchCollectionResponse<ValidationDemo>> request = new RootBuilderWrapper<Integer, ValidationDemo>(builder).batchFindBy("searchValidationDemos").setQueryParam("criteria", Arrays.asList(c1, c2)).build();
        _restClientAuto.sendRequest(request).getResponse();
        Assert.fail("Expected RestLiResponseException");
    } catch (RestLiResponseException e) {
        Assert.assertEquals(e.getServiceErrorMessage(), "BatchCriteria: 0 Element: 0 ERROR :: /stringA :: length of \"longLengthValueA\" is out of range 1...10\n" + "BatchCriteria: 0 Element: 1 ERROR :: /stringA :: length of \"longLengthValueA\" is out of range 1...10\n" + "BatchCriteria: 0 Element: 2 ERROR :: /stringA :: length of \"longLengthValueA\" is out of range 1...10\n");
    }
}
Also used : BatchCollectionResponse(com.linkedin.restli.common.BatchCollectionResponse) ValidationDemoCriteria(com.linkedin.restli.examples.greetings.api.ValidationDemoCriteria) RestLiResponseException(com.linkedin.restli.client.RestLiResponseException) ValidationDemo(com.linkedin.restli.examples.greetings.api.ValidationDemo) Test(org.testng.annotations.Test)

Example 3 with ValidationDemoCriteria

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

the class TestRestLiValidation method testBatchFinderAutoWithMissingField.

@Test(dataProvider = "autoBuilders")
public void testBatchFinderAutoWithMissingField(Object builder) throws RemoteInvocationException {
    try {
        ValidationDemoCriteria c1 = new ValidationDemoCriteria().setIntA(1111).setStringB("hello");
        ValidationDemoCriteria c2 = new ValidationDemoCriteria().setIntA(4444).setStringB("world");
        Request<BatchCollectionResponse<ValidationDemo>> request = new RootBuilderWrapper<Integer, ValidationDemo>(builder).batchFindBy("searchValidationDemos").setQueryParam("criteria", Arrays.asList(c1, c2)).build();
        _restClientAuto.sendRequest(request).getResponse();
        Assert.fail("Expected RestLiResponseException");
    } catch (RestLiResponseException e) {
        Assert.assertEquals(e.getServiceErrorMessage(), "BatchCriteria: 0 Element: 0 ERROR :: /stringB :: field is required but not found and has no default value\n" + "BatchCriteria: 0 Element: 1 ERROR :: /stringB :: field is required but not found and has no default value\n" + "BatchCriteria: 0 Element: 2 ERROR :: /stringB :: field is required but not found and has no default value\n");
    }
}
Also used : BatchCollectionResponse(com.linkedin.restli.common.BatchCollectionResponse) ValidationDemoCriteria(com.linkedin.restli.examples.greetings.api.ValidationDemoCriteria) RestLiResponseException(com.linkedin.restli.client.RestLiResponseException) ValidationDemo(com.linkedin.restli.examples.greetings.api.ValidationDemo) Test(org.testng.annotations.Test)

Example 4 with ValidationDemoCriteria

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

the class ValidationDemoResource method searchValidationDemos.

@BatchFinder(value = "searchValidationDemos", batchParam = "criteria")
public BatchFinderResult<ValidationDemoCriteria, ValidationDemo, Empty> searchValidationDemos(@PagingContextParam PagingContext context, @QueryParam("criteria") ValidationDemoCriteria[] criteria, @ValidatorParam RestLiDataValidator validator) {
    BatchFinderResult<ValidationDemoCriteria, ValidationDemo, Empty> batchFinderResult = new BatchFinderResult<>();
    for (ValidationDemoCriteria currentCriteria : criteria) {
        List<ValidationDemo> validationDemos = new ArrayList<>();
        // 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(currentCriteria.getIntA()).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());
        }
        CollectionResult<ValidationDemo, Empty> cr = new CollectionResult<>(validationDemos, validationDemos.size());
        batchFinderResult.putResult(currentCriteria, cr);
    }
    return batchFinderResult;
}
Also used : ArrayList(java.util.ArrayList) BatchFinderResult(com.linkedin.restli.server.BatchFinderResult) ValidationResult(com.linkedin.data.schema.validation.ValidationResult) ValidationDemo(com.linkedin.restli.examples.greetings.api.ValidationDemo) CollectionResult(com.linkedin.restli.server.CollectionResult) Empty(com.linkedin.restli.examples.greetings.api.Empty) ValidationDemoCriteria(com.linkedin.restli.examples.greetings.api.ValidationDemoCriteria) BatchFinder(com.linkedin.restli.server.annotations.BatchFinder)

Example 5 with ValidationDemoCriteria

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

the class TestRestLiValidation method testBatchFinder.

@Test(dataProvider = "manualBuilders")
public void testBatchFinder(Object builder) throws RemoteInvocationException {
    ValidationDemoCriteria c1 = new ValidationDemoCriteria().setIntA(1111).setStringB("hello");
    ValidationDemoCriteria c2 = new ValidationDemoCriteria().setIntA(1111).setStringB("world");
    Request<BatchCollectionResponse<ValidationDemo>> request = new RootBuilderWrapper<Integer, ValidationDemo>(builder).batchFindBy("searchValidationDemos").setQueryParam("criteria", Arrays.asList(c1, c2)).build();
    Response<BatchCollectionResponse<ValidationDemo>> response = _restClientManual.sendRequest(request).getResponse();
    Assert.assertEquals(response.getStatus(), HttpStatus.S_200_OK.getCode());
}
Also used : BatchCollectionResponse(com.linkedin.restli.common.BatchCollectionResponse) ValidationDemoCriteria(com.linkedin.restli.examples.greetings.api.ValidationDemoCriteria) ValidationDemo(com.linkedin.restli.examples.greetings.api.ValidationDemo) Test(org.testng.annotations.Test)

Aggregations

ValidationDemo (com.linkedin.restli.examples.greetings.api.ValidationDemo)7 ValidationDemoCriteria (com.linkedin.restli.examples.greetings.api.ValidationDemoCriteria)7 BatchCollectionResponse (com.linkedin.restli.common.BatchCollectionResponse)5 Test (org.testng.annotations.Test)5 RestLiResponseException (com.linkedin.restli.client.RestLiResponseException)3 Empty (com.linkedin.restli.examples.greetings.api.Empty)2 BatchFinderResult (com.linkedin.restli.server.BatchFinderResult)2 CollectionResult (com.linkedin.restli.server.CollectionResult)2 BatchFinder (com.linkedin.restli.server.annotations.BatchFinder)2 ArrayList (java.util.ArrayList)2 ValidationResult (com.linkedin.data.schema.validation.ValidationResult)1 RestLiServiceException (com.linkedin.restli.server.RestLiServiceException)1