Search in sources :

Example 6 with PatchRequest

use of com.linkedin.restli.common.PatchRequest in project rest.li by linkedin.

the class TestParseqBasedFluentClientApi method testBatchPartialUpdateAndGetWithErrors.

@Test
public void testBatchPartialUpdateAndGetWithErrors() throws Exception {
    PartialUpdateGreeting greetings = new PartialUpdateGreetingFluentClient(_parSeqRestliClient, _parSeqUnitTestHelper.getEngine());
    Map<Long, PatchRequest<Greeting>> inputs = new HashMap<>();
    Greeting original = getGreeting();
    String message = "Edited message: fluent api test partialUpdateAndGet";
    Greeting update = getGreeting(message);
    inputs.put(21L, PatchGenerator.diff(original, update));
    inputs.put(-2L, PatchGenerator.diff(original, update));
    CompletionStage<Map<Long, UpdateEntityStatus<Greeting>>> result = greetings.batchPartialUpdateAndGet(inputs);
    CompletableFuture<Map<Long, UpdateEntityStatus<Greeting>>> future = result.toCompletableFuture();
    Assert.assertNotNull(future.get(5000, TimeUnit.MILLISECONDS));
    Assert.assertEquals(future.get().get(21L).getStatus().intValue(), 200);
    Assert.assertEquals(future.get().get(21L).getEntity().getId().longValue(), 21L);
    Assert.assertEquals(future.get().get(21L).getEntity().getMessage(), message);
    Assert.assertEquals(future.get().get(-2L).getStatus().intValue(), 404);
    Assert.assertFalse(future.get().get(-2L).hasEntity());
}
Also used : CreateGreeting(com.linkedin.restli.examples.greetings.client.CreateGreeting) Greeting(com.linkedin.restli.examples.greetings.api.Greeting) PartialUpdateGreeting(com.linkedin.restli.examples.greetings.client.PartialUpdateGreeting) HashMap(java.util.HashMap) CustomLong(com.linkedin.restli.examples.custom.types.CustomLong) PartialUpdateGreetingFluentClient(com.linkedin.restli.examples.greetings.client.PartialUpdateGreetingFluentClient) PatchRequest(com.linkedin.restli.common.PatchRequest) Map(java.util.Map) HashMap(java.util.HashMap) StringMap(com.linkedin.data.template.StringMap) PartialUpdateGreeting(com.linkedin.restli.examples.greetings.client.PartialUpdateGreeting) Test(org.testng.annotations.Test)

Example 7 with PatchRequest

use of com.linkedin.restli.common.PatchRequest in project rest.li by linkedin.

the class TestParseqBasedFluentClientApi method testBatchPartialUpdateAndGet.

@Test
public void testBatchPartialUpdateAndGet() throws Exception {
    PartialUpdateGreeting greetings = new PartialUpdateGreetingFluentClient(_parSeqRestliClient, _parSeqUnitTestHelper.getEngine());
    Map<Long, PatchRequest<Greeting>> inputs = new HashMap<>();
    Greeting original = getGreeting();
    String message = "Edited message: fluent api test partialUpdateAndGet";
    Greeting update = getGreeting(message);
    inputs.put(21L, PatchGenerator.diff(original, update));
    inputs.put(22L, PatchGenerator.diff(original, update));
    CompletionStage<Map<Long, UpdateEntityStatus<Greeting>>> result = greetings.batchPartialUpdateAndGet(inputs);
    CompletableFuture<Map<Long, UpdateEntityStatus<Greeting>>> future = result.toCompletableFuture();
    Assert.assertNotNull(future.get(5000, TimeUnit.MILLISECONDS));
    Assert.assertEquals(future.get().get(21L).getEntity().getId().longValue(), 21L);
    Assert.assertEquals(future.get().get(21L).getEntity().getMessage(), message);
    Assert.assertEquals(future.get().get(22L).getEntity().getId().longValue(), 22L);
    Assert.assertEquals(future.get().get(22L).getEntity().getMessage(), message);
}
Also used : CreateGreeting(com.linkedin.restli.examples.greetings.client.CreateGreeting) Greeting(com.linkedin.restli.examples.greetings.api.Greeting) PartialUpdateGreeting(com.linkedin.restli.examples.greetings.client.PartialUpdateGreeting) HashMap(java.util.HashMap) CustomLong(com.linkedin.restli.examples.custom.types.CustomLong) PartialUpdateGreetingFluentClient(com.linkedin.restli.examples.greetings.client.PartialUpdateGreetingFluentClient) PatchRequest(com.linkedin.restli.common.PatchRequest) Map(java.util.Map) HashMap(java.util.HashMap) StringMap(com.linkedin.data.template.StringMap) PartialUpdateGreeting(com.linkedin.restli.examples.greetings.client.PartialUpdateGreeting) Test(org.testng.annotations.Test)

Example 8 with PatchRequest

use of com.linkedin.restli.common.PatchRequest 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)

Example 9 with PatchRequest

use of com.linkedin.restli.common.PatchRequest in project rest.li by linkedin.

the class TestReturnEntityWithBatchPartialUpdate method testReturnEntityOnDemand.

/**
 * Ensures that different usages of {@link BatchPartialUpdateEntityRequestBuilder#returnEntity(boolean)} are handled
 * correctly and that the response appropriately contains the entities or nothing depending on how and if the provided
 * method is used.
 * @param returnEntity value of the {@link RestConstants#RETURN_ENTITY_PARAM} parameter of this request
 * @param expectReturnEntities whether or not the patched entities are expected in the response
 */
@Test(dataProvider = "returnEntityOnDemandData")
public void testReturnEntityOnDemand(Boolean returnEntity, boolean expectReturnEntities) throws RemoteInvocationException {
    final long expectedId1 = 8L;
    final long expectedId2 = 9L;
    Map<Long, PatchRequest<Greeting>> patches = new HashMap<>();
    patches.put(expectedId1, PatchRequest.createFromEmptyPatchDocument());
    patches.put(expectedId2, PatchRequest.createFromEmptyPatchDocument());
    Map<Long, Greeting> expectedGreetings = new HashMap<>();
    expectedGreetings.put(expectedId1, new Greeting().setId(expectedId1).setMessage("Message " + expectedId1).setTone(Tone.FRIENDLY));
    expectedGreetings.put(expectedId2, new Greeting().setId(expectedId2).setMessage("Message " + expectedId2).setTone(Tone.FRIENDLY));
    BatchPartialUpdateEntityRequestBuilder<Long, Greeting> requestBuilder = new PartialUpdateGreetingRequestBuilders().batchPartialUpdateAndGet().inputs(patches);
    if (returnEntity != null) {
        requestBuilder.returnEntity(returnEntity);
    }
    BatchPartialUpdateEntityRequest<Long, Greeting> request = requestBuilder.build();
    Response<BatchKVResponse<Long, UpdateEntityStatus<Greeting>>> response = getClient().sendRequest(request).getResponse();
    BatchKVResponse<Long, UpdateEntityStatus<Greeting>> batchKVResponse = response.getEntity();
    Assert.assertNotNull(batchKVResponse);
    Map<Long, UpdateEntityStatus<Greeting>> greetings = batchKVResponse.getResults();
    Assert.assertNotNull(greetings);
    for (Long key : greetings.keySet()) {
        Assert.assertTrue(expectedGreetings.containsKey(key), "Encountered unexpected ID in batch response.");
        UpdateEntityStatus<Greeting> updateEntityStatus = greetings.get(key);
        Assert.assertNotNull(updateEntityStatus);
        Assert.assertEquals(updateEntityStatus.getStatus().intValue(), HttpStatus.S_200_OK.getCode());
        Assert.assertFalse(updateEntityStatus.hasError());
        if (expectReturnEntities) {
            Assert.assertTrue(updateEntityStatus.hasEntity());
            Greeting returnedEntity = updateEntityStatus.getEntity();
            Greeting expectedEntity = expectedGreetings.get(key);
            Assert.assertNotNull(returnedEntity, "RecordTemplate entity in response should not be null.");
            Assert.assertEquals(returnedEntity.getId(), expectedEntity.getId(), "Expected returned entity ID to match original.");
            Assert.assertEquals(returnedEntity.getMessage(), expectedEntity.getMessage(), "Expected returned entity message to match original.");
            Assert.assertEquals(returnedEntity.getTone(), expectedEntity.getTone(), "Expected returned entity tone to match original.");
        } else {
            Assert.assertFalse(updateEntityStatus.hasEntity());
            Assert.assertNull(updateEntityStatus.getEntity());
        }
    }
}
Also used : Greeting(com.linkedin.restli.examples.greetings.api.Greeting) UpdateEntityStatus(com.linkedin.restli.common.UpdateEntityStatus) HashMap(java.util.HashMap) PatchRequest(com.linkedin.restli.common.PatchRequest) BatchKVResponse(com.linkedin.restli.client.response.BatchKVResponse) PartialUpdateGreetingRequestBuilders(com.linkedin.restli.examples.greetings.client.PartialUpdateGreetingRequestBuilders) Test(org.testng.annotations.Test)

Example 10 with PatchRequest

use of com.linkedin.restli.common.PatchRequest in project rest.li by linkedin.

the class TestReturnEntityWithBatchPartialUpdate method testBatchPartialUpdateErrorMap.

/**
 * Ensures that individual errors are handled correctly and sent back to the client in the batch response.
 * This test coerces the server resource to return 404 errors after trying to patch nonexistent entities.
 */
@Test
public void testBatchPartialUpdateErrorMap() throws RemoteInvocationException {
    Map<Long, PatchRequest<Greeting>> patches = new HashMap<>();
    patches.put(2147L, PatchRequest.createFromEmptyPatchDocument());
    patches.put(2148L, PatchRequest.createFromEmptyPatchDocument());
    BatchPartialUpdateEntityRequest<Long, Greeting> request = new PartialUpdateGreetingRequestBuilders().batchPartialUpdateAndGet().inputs(patches).returnEntity(true).build();
    Response<BatchKVResponse<Long, UpdateEntityStatus<Greeting>>> response = getClient().sendRequest(request).getResponse();
    Assert.assertNotNull(response);
    BatchKVResponse<Long, UpdateEntityStatus<Greeting>> batchKVResponse = response.getEntity();
    Assert.assertNotNull(batchKVResponse);
    Map<Long, UpdateEntityStatus<Greeting>> greetings = batchKVResponse.getResults();
    Assert.assertNotNull(greetings);
    for (UpdateEntityStatus<Greeting> updateEntityStatus : batchKVResponse.getResults().values()) {
        Assert.assertFalse(updateEntityStatus.hasEntity());
        Assert.assertEquals(updateEntityStatus.getStatus().intValue(), HttpStatus.S_404_NOT_FOUND.getCode());
        Assert.assertTrue(updateEntityStatus.hasError());
        ErrorResponse error = updateEntityStatus.getError();
        Assert.assertNotNull(error);
        Assert.assertEquals(updateEntityStatus.getError().getStatus().intValue(), HttpStatus.S_404_NOT_FOUND.getCode());
    }
    Map<Long, ErrorResponse> errors = batchKVResponse.getErrors();
    Assert.assertNotNull(errors);
    for (ErrorResponse error : errors.values()) {
        Assert.assertEquals(error.getStatus().intValue(), HttpStatus.S_404_NOT_FOUND.getCode());
    }
}
Also used : Greeting(com.linkedin.restli.examples.greetings.api.Greeting) UpdateEntityStatus(com.linkedin.restli.common.UpdateEntityStatus) HashMap(java.util.HashMap) PatchRequest(com.linkedin.restli.common.PatchRequest) BatchKVResponse(com.linkedin.restli.client.response.BatchKVResponse) ErrorResponse(com.linkedin.restli.common.ErrorResponse) PartialUpdateGreetingRequestBuilders(com.linkedin.restli.examples.greetings.client.PartialUpdateGreetingRequestBuilders) Test(org.testng.annotations.Test)

Aggregations

PatchRequest (com.linkedin.restli.common.PatchRequest)32 HashMap (java.util.HashMap)26 Test (org.testng.annotations.Test)22 Map (java.util.Map)14 Greeting (com.linkedin.restli.examples.greetings.api.Greeting)11 DataMap (com.linkedin.data.DataMap)9 BatchKVResponse (com.linkedin.restli.client.response.BatchKVResponse)8 UpdateStatus (com.linkedin.restli.common.UpdateStatus)6 BatchPatchRequest (com.linkedin.restli.server.BatchPatchRequest)6 ComplexResourceKey (com.linkedin.restli.common.ComplexResourceKey)5 CompoundKey (com.linkedin.restli.common.CompoundKey)5 CustomLong (com.linkedin.restli.examples.custom.types.CustomLong)5 CreateGreeting (com.linkedin.restli.examples.greetings.client.CreateGreeting)5 PartialUpdateGreeting (com.linkedin.restli.examples.greetings.client.PartialUpdateGreeting)5 PartialUpdateGreetingFluentClient (com.linkedin.restli.examples.greetings.client.PartialUpdateGreetingFluentClient)5 ValidationResult (com.linkedin.data.schema.validation.ValidationResult)4 StringMap (com.linkedin.data.template.StringMap)4 KeyValueRecord (com.linkedin.restli.common.KeyValueRecord)4 BatchUpdateResult (com.linkedin.restli.server.BatchUpdateResult)4 RestLiServiceException (com.linkedin.restli.server.RestLiServiceException)4