Search in sources :

Example 6 with ValidationDemo

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

the class TestRestLiValidation method testBatchCreateManualFailure.

@Test(dataProvider = "batchCreateFailureData")
public void testBatchCreateManualFailure(List<ValidationDemo> validationDemos, List<String> errorMessages) throws RemoteInvocationException {
    Response<CollectionResponse<CreateStatus>> response = _restClientManual.sendRequest(new RootBuilderWrapper<Integer, ValidationDemo>(new ValidationDemosBuilders()).batchCreate().inputs(validationDemos).build()).getResponse();
    List<CreateStatus> results = response.getEntity().getElements();
    int i = 0;
    for (CreateStatus result : results) {
        Assert.assertEquals((int) result.getStatus(), HttpStatus.S_422_UNPROCESSABLE_ENTITY.getCode());
        Assert.assertTrue(result.getError().getMessage().contains(errorMessages.get(i++)));
    }
    response = _restClientManual.sendRequest(new RootBuilderWrapper<Integer, ValidationDemo>(new ValidationDemosRequestBuilders()).batchCreate().inputs(validationDemos).build()).getResponse();
    @SuppressWarnings("unchecked") List<CreateIdStatus<Integer>> results2 = ((BatchCreateIdResponse<Integer>) (Object) response.getEntity()).getElements();
    i = 0;
    for (CreateIdStatus<Integer> result : results2) {
        Assert.assertEquals((int) result.getStatus(), HttpStatus.S_422_UNPROCESSABLE_ENTITY.getCode());
        Assert.assertTrue(result.getError().getMessage().contains(errorMessages.get(i++)));
    }
}
Also used : CreateStatus(com.linkedin.restli.common.CreateStatus) CollectionResponse(com.linkedin.restli.common.CollectionResponse) RootBuilderWrapper(com.linkedin.restli.test.util.RootBuilderWrapper) CreateIdStatus(com.linkedin.restli.common.CreateIdStatus) ValidationDemo(com.linkedin.restli.examples.greetings.api.ValidationDemo) BatchCreateIdResponse(com.linkedin.restli.common.BatchCreateIdResponse) ValidationDemosBuilders(com.linkedin.restli.examples.greetings.client.ValidationDemosBuilders) AutoValidationDemosBuilders(com.linkedin.restli.examples.greetings.client.AutoValidationDemosBuilders) AutoValidationDemosRequestBuilders(com.linkedin.restli.examples.greetings.client.AutoValidationDemosRequestBuilders) ValidationDemosRequestBuilders(com.linkedin.restli.examples.greetings.client.ValidationDemosRequestBuilders) Test(org.testng.annotations.Test)

Example 7 with ValidationDemo

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

the class TestRestLiValidation method batchCreateAndGetFailures.

private static Object[][] batchCreateAndGetFailures() {
    ValidationDemo.UnionFieldWithInlineRecord unionField = new ValidationDemo.UnionFieldWithInlineRecord();
    unionField.setMyEnum(myEnum.FOOFOO);
    return new Object[][] { { new ValidationDemo().setStringB("b1").setUnionFieldWithInlineRecord(unionField), "ERROR :: /UnionFieldWithInlineRecord :: field is required but not found and has no default value" }, { new ValidationDemo().setStringB("b2").setUnionFieldWithInlineRecord(unionField), "ERROR :: /UnionFieldWithInlineRecord/com.linkedin.restli.examples.greetings.api.myRecord/foo1 " + ":: field is required but not found and has no default value" } };
}
Also used : ValidationDemo(com.linkedin.restli.examples.greetings.api.ValidationDemo)

Example 8 with ValidationDemo

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

the class TestRestLiValidation method createFailures.

public static Object[][] createFailures() {
    ValidationDemo.UnionFieldWithInlineRecord unionField = new ValidationDemo.UnionFieldWithInlineRecord();
    unionField.setMyRecord(new myRecord().setFoo1(111));
    MyItemArray myItems = new MyItemArray();
    myItems.add(new myItem().setBar1("bar1"));
    GreetingMap greetingMap = new GreetingMap();
    greetingMap.put("key1", new Greeting());
    return new Object[][] { // ReadOnly fields should not be specified in a create request, whether they are required or optional
    { new ValidationDemo().setStringA("aaa"), "/stringA :: ReadOnly field present in a create request" }, { new ValidationDemo().setIntA(1234), "/intA :: ReadOnly field present in a create request" }, { new ValidationDemo().setUnionFieldWithInlineRecord(unionField), "/UnionFieldWithInlineRecord/com.linkedin.restli.examples.greetings.api.myRecord/foo1 :: ReadOnly field present in a create request" }, { new ValidationDemo().setArrayWithInlineRecord(myItems), "/ArrayWithInlineRecord/0/bar1 :: ReadOnly field present in a create request" }, { new ValidationDemo().setValidationDemoNext(new ValidationDemo().setStringB("stringB")), "/validationDemoNext/stringB :: ReadOnly field present in a create request" }, { new ValidationDemo().setValidationDemoNext(new ValidationDemo().setUnionFieldWithInlineRecord(unionField)), "/validationDemoNext/UnionFieldWithInlineRecord :: ReadOnly field present in a create request" }, // A field that is CreateOnly and required has to be present in a create request
    { new ValidationDemo(), "/stringB :: field is required but not found and has no default value" }, { new ValidationDemo().setStringB("bbb"), "/UnionFieldWithInlineRecord :: field is required but not found and has no default value" }, // Required fields without Rest.li data annotations should be present in a create request
    { new ValidationDemo().setArrayWithInlineRecord(myItems), "/ArrayWithInlineRecord/0/bar2 :: field is required but not found and has no default value" }, { new ValidationDemo().setMapWithTyperefs(greetingMap), "/MapWithTyperefs/key1/id :: field is required but not found and has no default value" }, { new ValidationDemo().setValidationDemoNext(new ValidationDemo()), "/validationDemoNext/stringA :: field is required but not found and has no default value" }, { new ValidationDemo(), "/UnionFieldWithInlineRecord :: field is required but not found and has no default value" } };
}
Also used : com.linkedin.restli.examples.greetings.api.myRecord(com.linkedin.restli.examples.greetings.api.myRecord) MyItemArray(com.linkedin.restli.examples.greetings.api.MyItemArray) Greeting(com.linkedin.restli.examples.greetings.api.Greeting) com.linkedin.restli.examples.greetings.api.myItem(com.linkedin.restli.examples.greetings.api.myItem) GreetingMap(com.linkedin.restli.examples.greetings.api.GreetingMap) ValidationDemo(com.linkedin.restli.examples.greetings.api.ValidationDemo)

Example 9 with ValidationDemo

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

the class TestRestLiValidation method updateSuccesses.

public static Object[] updateSuccesses() {
    ValidationDemo.UnionFieldWithInlineRecord unionField = new ValidationDemo.UnionFieldWithInlineRecord();
    unionField.setMyRecord(new myRecord().setFoo1(1));
    ValidationDemo validationDemo1 = new ValidationDemo().setStringA("aaa").setStringB("bbb").setUnionFieldWithInlineRecord(unionField);
    ValidationDemo.UnionFieldWithInlineRecord unionField2 = new ValidationDemo.UnionFieldWithInlineRecord();
    unionField2.setMyEnum(myEnum.BARBAR);
    MyItemArray array = new MyItemArray();
    array.add(new myItem().setBar1("BAR1").setBar2("BAR2"));
    array.add(new myItem().setBar1("BAR11").setBar2("BAR22"));
    GreetingMap map = new GreetingMap();
    map.put("key1", new Greeting().setId(1).setMessage("msg").setTone(Tone.FRIENDLY));
    return new Object[] { // All required fields have to be present, regardless of ReadOnly or CreateOnly annotations
    validationDemo1, new ValidationDemo().setStringA("aaa").setStringB("bbb").setUnionFieldWithInlineRecord(unionField2).setIntA(1234).setIntB(5678).setArrayWithInlineRecord(array).setMapWithTyperefs(map).setValidationDemoNext(validationDemo1) };
}
Also used : com.linkedin.restli.examples.greetings.api.myRecord(com.linkedin.restli.examples.greetings.api.myRecord) MyItemArray(com.linkedin.restli.examples.greetings.api.MyItemArray) Greeting(com.linkedin.restli.examples.greetings.api.Greeting) com.linkedin.restli.examples.greetings.api.myItem(com.linkedin.restli.examples.greetings.api.myItem) GreetingMap(com.linkedin.restli.examples.greetings.api.GreetingMap) ValidationDemo(com.linkedin.restli.examples.greetings.api.ValidationDemo)

Example 10 with ValidationDemo

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

the class TestRestLiValidation method testCreateAndGetAutoSuccess.

@Test(dataProvider = "provideCreateAndGetSuccessData")
@SuppressWarnings("unchecked")
public void testCreateAndGetAutoSuccess(RestClient restClient, Object builder, ValidationDemo validationDemo) throws RemoteInvocationException {
    Request<IdEntityResponse<Integer, ValidationDemo>> createRequest = new RootBuilderWrapper<Integer, ValidationDemo>(builder).createAndGet().input(validationDemo).build();
    Response<IdEntityResponse<Integer, ValidationDemo>> response = restClient.sendRequest(createRequest).getResponse();
    ValidationDemo.UnionFieldWithInlineRecord unionField = new ValidationDemo.UnionFieldWithInlineRecord();
    unionField.setMyEnum(myEnum.FOOFOO);
    ValidationDemo expected = new ValidationDemo().setStringA("a").setStringB("b").setUnionFieldWithInlineRecord(unionField);
    Assert.assertEquals(response.getStatus(), HttpStatus.S_201_CREATED.getCode());
    Assert.assertEquals(response.getEntity().getEntity(), expected);
}
Also used : IdEntityResponse(com.linkedin.restli.common.IdEntityResponse) 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