Search in sources :

Example 1 with PartialUpdateGreetingRequestBuilders

use of com.linkedin.restli.examples.greetings.client.PartialUpdateGreetingRequestBuilders in project rest.li by linkedin.

the class TestRestLiScatterGather method testSendSGPartialUpdateEntityRequests.

// BatchPartialUpdateEntityRequest
private static void testSendSGPartialUpdateEntityRequests(RestClient restClient, Map<Long, PatchRequest<Greeting>> inputs) throws RemoteInvocationException {
    BatchPartialUpdateEntityRequest<Long, Greeting> request = new PartialUpdateGreetingRequestBuilders().batchPartialUpdateAndGet().inputs(inputs).setParam("foo", "bar").returnEntity(true).build();
    BatchKVResponse<Long, UpdateEntityStatus<Greeting>> result = restClient.sendRequest(request).getResponse().getEntity();
    Assert.assertEquals(result.getResults().size(), inputs.size());
    UpdateEntityStatus<Greeting> item = result.getResults().values().iterator().next();
    Assert.assertNotNull(item.getEntity());
    Assert.assertFalse(item.hasError());
    Assert.assertEquals(result.getErrors().size(), 0);
}
Also used : Greeting(com.linkedin.restli.examples.greetings.api.Greeting) UpdateEntityStatus(com.linkedin.restli.common.UpdateEntityStatus) PartialUpdateGreetingRequestBuilders(com.linkedin.restli.examples.greetings.client.PartialUpdateGreetingRequestBuilders)

Example 2 with PartialUpdateGreetingRequestBuilders

use of com.linkedin.restli.examples.greetings.client.PartialUpdateGreetingRequestBuilders 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 3 with PartialUpdateGreetingRequestBuilders

use of com.linkedin.restli.examples.greetings.client.PartialUpdateGreetingRequestBuilders 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)

Example 4 with PartialUpdateGreetingRequestBuilders

use of com.linkedin.restli.examples.greetings.client.PartialUpdateGreetingRequestBuilders in project rest.li by linkedin.

the class TestReturnEntityWithPartialUpdate method testPartialUpdateEntityWithProjection.

/**
 * Same as {@link this#testPartialUpdateEntity}, except the fields of the returned entity are projected.
 *
 * @param patch patch to send for this request
 * @param expectedGreeting expected response entity for this request
 */
@Test(dataProvider = "partialUpdateData")
public void testPartialUpdateEntityWithProjection(PatchRequest<Greeting> patch, Greeting expectedGreeting) throws RemoteInvocationException {
    final Greeting.Fields fields = Greeting.fields();
    PartialUpdateEntityRequest<Greeting> request = new PartialUpdateGreetingRequestBuilders().partialUpdateAndGet().id(1L).input(patch).fields(fields.id(), fields.message()).build();
    Response<Greeting> response = getClient().sendRequest(request).getResponse();
    Assert.assertNotNull(response, "Response should not be null.");
    Greeting greeting = response.getEntity();
    Assert.assertNotNull(greeting, "Response record should not be null.");
    Assert.assertNotNull(expectedGreeting, "Expected record from data provider should not be null.");
    Assert.assertTrue(greeting.hasId(), "Response record should include an id field.");
    Assert.assertTrue(greeting.hasMessage(), "Response record should include a message field.");
    Assert.assertFalse(greeting.hasTone(), "Response record should not include a tone field due to projection.");
    Assert.assertEquals(greeting.getId(), expectedGreeting.getId());
    Assert.assertEquals(greeting.getMessage(), expectedGreeting.getMessage());
    Assert.assertNull(greeting.getTone(GetMode.NULL), "Response record should have a null tone field due to projection.");
}
Also used : Greeting(com.linkedin.restli.examples.greetings.api.Greeting) PartialUpdateGreetingRequestBuilders(com.linkedin.restli.examples.greetings.client.PartialUpdateGreetingRequestBuilders) Test(org.testng.annotations.Test)

Example 5 with PartialUpdateGreetingRequestBuilders

use of com.linkedin.restli.examples.greetings.client.PartialUpdateGreetingRequestBuilders in project rest.li by linkedin.

the class TestReturnEntityWithPartialUpdate method testReturnEntityOnDemand.

/**
 * Ensures that different usages of {@link PartialUpdateEntityRequestBuilder#returnEntity(boolean)} are handled
 * correctly and that the response appropriately contains the entity or nothing depending on how and if the provided
 * method is used.
 */
@Test(dataProvider = "returnEntityOnDemandData")
public void testReturnEntityOnDemand(Boolean returnEntity, boolean expectReturnEntity) throws RemoteInvocationException {
    final long expectedId = 8L;
    Greeting expectedGreeting = new Greeting();
    expectedGreeting.setMessage("Message " + expectedId);
    expectedGreeting.setTone(Tone.FRIENDLY);
    PartialUpdateEntityRequestBuilder<Long, Greeting> requestBuilder = new PartialUpdateGreetingRequestBuilders().partialUpdateAndGet().id(expectedId).input(PatchRequest.createFromEmptyPatchDocument());
    if (returnEntity != null) {
        requestBuilder.returnEntity(returnEntity);
    }
    PartialUpdateEntityRequest<Greeting> request = requestBuilder.build();
    Response<Greeting> response = getClient().sendRequest(request).getResponse();
    Assert.assertEquals(response.getStatus(), HttpStatus.S_200_OK.getCode());
    if (expectReturnEntity) {
        Greeting returnedEntity = response.getEntity();
        Assert.assertNotNull(returnedEntity, "RecordTemplate entity in response should not be null.");
        Assert.assertEquals((long) returnedEntity.getId(), expectedId, "Expect returned entity ID to match original.");
        Assert.assertEquals(returnedEntity.getMessage(), expectedGreeting.getMessage(), "Expect returned entity message to match original.");
        Assert.assertEquals(returnedEntity.getTone(), expectedGreeting.getTone(), "Expect returned entity tone to match original.");
    } else {
        Assert.assertNull(response.getEntity(), "RecordTemplate entity in response should be null.");
    }
}
Also used : Greeting(com.linkedin.restli.examples.greetings.api.Greeting) PartialUpdateGreetingRequestBuilders(com.linkedin.restli.examples.greetings.client.PartialUpdateGreetingRequestBuilders) Test(org.testng.annotations.Test)

Aggregations

Greeting (com.linkedin.restli.examples.greetings.api.Greeting)11 PartialUpdateGreetingRequestBuilders (com.linkedin.restli.examples.greetings.client.PartialUpdateGreetingRequestBuilders)11 Test (org.testng.annotations.Test)10 UpdateEntityStatus (com.linkedin.restli.common.UpdateEntityStatus)5 BatchKVResponse (com.linkedin.restli.client.response.BatchKVResponse)4 RestLiResponseException (com.linkedin.restli.client.RestLiResponseException)3 ByteString (com.linkedin.data.ByteString)2 PatchRequest (com.linkedin.restli.common.PatchRequest)2 HashMap (java.util.HashMap)2 ErrorResponse (com.linkedin.restli.common.ErrorResponse)1