Search in sources :

Example 26 with ValidationDemo

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

the class TestRestLiValidationFromClient method testPartialUpdateFailure.

@Test(dataProvider = "providePartialUpdateFailureData")
public void testPartialUpdateFailure(Object builder, String patch, String errorMessage) {
    PatchRequest<ValidationDemo> patchRequest = PatchBuilder.buildPatchFromString(patch);
    ValidationResult result = new RootBuilderWrapper<Integer, ValidationDemo>(builder, ValidationDemo.class).partialUpdate().validateInput(patchRequest);
    Assert.assertEquals(result.isValid(), false);
    Assert.assertEquals(result.getMessages().toString(), errorMessage);
}
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 27 with ValidationDemo

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

the class AutomaticValidationDemoResource method batchUpdate.

@RestMethod.BatchUpdate
public BatchUpdateResult<Integer, ValidationDemo> batchUpdate(final BatchUpdateRequest<Integer, ValidationDemo> entities) {
    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();
        results.put(key, new UpdateResponse(HttpStatus.S_204_NO_CONTENT));
    }
    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) HashMap(java.util.HashMap) Map(java.util.Map) ValidationDemo(com.linkedin.restli.examples.greetings.api.ValidationDemo)

Example 28 with ValidationDemo

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

the class AutomaticValidationDemoResource method get.

@RestMethod.Get
public ValidationDemo get(final Integer key) {
    if (key == 0) {
        throw new RestLiServiceException(HttpStatus.S_400_BAD_REQUEST);
    }
    // Generate an entity that does not conform to the data schema
    ValidationDemo.UnionFieldWithInlineRecord union = new ValidationDemo.UnionFieldWithInlineRecord();
    union.setMyEnum(myEnum.BARBAR);
    ValidationDemo validationDemo = new ValidationDemo().setStringA("stringA is readOnly").setUnionFieldWithInlineRecord(union);
    return validationDemo;
}
Also used : RestLiServiceException(com.linkedin.restli.server.RestLiServiceException) ValidationDemo(com.linkedin.restli.examples.greetings.api.ValidationDemo)

Example 29 with ValidationDemo

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

the class AutomaticValidationDemoResource method batchCreate.

@RestMethod.BatchCreate
@ReturnEntity
public BatchCreateKVResult<Integer, ValidationDemo> batchCreate(final BatchCreateRequest<Integer, ValidationDemo> entities) {
    List<CreateKVResponse<Integer, ValidationDemo>> results = new ArrayList<CreateKVResponse<Integer, ValidationDemo>>();
    int id = 0;
    for (ValidationDemo entity : entities.getInput()) {
        ValidationDemo returnEntity;
        if (entity.getStringB().equals("b1")) {
            // Missing union field.
            returnEntity = new ValidationDemo().setStringA("a").setStringB("b");
        } else if (entity.getStringB().equals("b2")) {
            // Missing foo1 in myRecord.
            ValidationDemo.UnionFieldWithInlineRecord unionField = new ValidationDemo.UnionFieldWithInlineRecord();
            unionField.setMyRecord(new myRecord().setFoo2(2));
            returnEntity = new ValidationDemo().setStringA("a").setStringB("b").setUnionFieldWithInlineRecord(unionField);
        } else {
            returnEntity = _validReturnEntity;
        }
        results.add(new CreateKVResponse<Integer, ValidationDemo>(id, returnEntity));
        id++;
    }
    return new BatchCreateKVResult<Integer, ValidationDemo>(results);
}
Also used : com.linkedin.restli.examples.greetings.api.myRecord(com.linkedin.restli.examples.greetings.api.myRecord) ArrayList(java.util.ArrayList) BatchCreateKVResult(com.linkedin.restli.server.BatchCreateKVResult) CreateKVResponse(com.linkedin.restli.server.CreateKVResponse) ValidationDemo(com.linkedin.restli.examples.greetings.api.ValidationDemo) ReturnEntity(com.linkedin.restli.server.annotations.ReturnEntity)

Example 30 with ValidationDemo

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

the class ValidationDemoResource method batchCreate.

@RestMethod.BatchCreate
public BatchCreateResult<Integer, ValidationDemo> batchCreate(final BatchCreateRequest<Integer, ValidationDemo> entities, @ValidatorParam RestLiDataValidator validator) {
    List<CreateResponse> results = new ArrayList<CreateResponse>();
    int id = 0;
    for (ValidationDemo entity : entities.getInput()) {
        ValidationResult result = validator.validateInput(entity);
        if (result.isValid()) {
            results.add(new CreateResponse(id));
            id++;
        } else {
            results.add(new CreateResponse(new RestLiServiceException(HttpStatus.S_422_UNPROCESSABLE_ENTITY, result.getMessages().toString())));
        }
    }
    return new BatchCreateResult<Integer, ValidationDemo>(results);
}
Also used : RestLiServiceException(com.linkedin.restli.server.RestLiServiceException) CreateResponse(com.linkedin.restli.server.CreateResponse) ArrayList(java.util.ArrayList) BatchCreateResult(com.linkedin.restli.server.BatchCreateResult) ValidationResult(com.linkedin.data.schema.validation.ValidationResult) ValidationDemo(com.linkedin.restli.examples.greetings.api.ValidationDemo)

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