use of com.linkedin.restli.examples.greetings.client.PartialUpdateGreetingRequestBuilders in project rest.li by linkedin.
the class TestReturnEntityWithBatchPartialUpdate method testBatchPartialUpdateEntities.
/**
* Sends batch partial update requests to the server and ensures that the returned entities for each request are
* consistent with the expected state of the server's entities.
*
* @param patches patches to send for this request
* @param expectedGreetings expected response entities for this request
*/
@Test(dataProvider = "batchPartialUpdateData")
public void testBatchPartialUpdateEntities(Map<Long, PatchRequest<Greeting>> patches, Map<Long, Greeting> expectedGreetings) throws RemoteInvocationException {
BatchPartialUpdateEntityRequest<Long, Greeting> request = new PartialUpdateGreetingRequestBuilders().batchPartialUpdateAndGet().inputs(patches).build();
Response<BatchKVResponse<Long, UpdateEntityStatus<Greeting>>> response = getClient().sendRequest(request).getResponse();
Assert.assertNotNull(response);
BatchKVResponse<Long, UpdateEntityStatus<Greeting>> batchKVResponse = response.getEntity();
Assert.assertNotNull(batchKVResponse);
Assert.assertTrue(batchKVResponse.getErrors().isEmpty());
Map<Long, UpdateEntityStatus<Greeting>> greetings = batchKVResponse.getResults();
Assert.assertNotNull(greetings);
for (Long key : greetings.keySet()) {
Assert.assertTrue(expectedGreetings.containsKey(key));
UpdateEntityStatus<Greeting> updateEntityStatus = greetings.get(key);
Assert.assertNotNull(updateEntityStatus);
Assert.assertEquals(updateEntityStatus.getStatus().intValue(), HttpStatus.S_200_OK.getCode());
Assert.assertTrue(updateEntityStatus.hasEntity());
Assert.assertFalse(updateEntityStatus.hasError());
Greeting greeting = updateEntityStatus.getEntity();
Greeting expectedGreeting = expectedGreetings.get(key);
Assert.assertNotNull(greeting);
Assert.assertNotNull(expectedGreeting);
Assert.assertEquals(greeting.getId(), expectedGreeting.getId());
Assert.assertEquals(greeting.getMessage(), expectedGreeting.getMessage());
Assert.assertEquals(greeting.getTone(), expectedGreeting.getTone());
}
}
use of com.linkedin.restli.examples.greetings.client.PartialUpdateGreetingRequestBuilders in project rest.li by linkedin.
the class TestReturnEntityWithBatchPartialUpdate method testInvalidReturnEntityParameter.
/**
* Ensures that using an invalid value for the {@link RestConstants#RETURN_ENTITY_PARAM} query parameter results
* in a 400 bad request error response for BATCH_PARTIAL_UPDATE.
*/
@Test
@SuppressWarnings({ "Duplicates" })
public void testInvalidReturnEntityParameter() throws RemoteInvocationException {
final long expectedId = 8L;
Greeting expectedGreeting = new Greeting();
expectedGreeting.setMessage("Message " + expectedId);
expectedGreeting.setTone(Tone.FRIENDLY);
final String invalidParamValue = "NOTaBoolean";
BatchPartialUpdateEntityRequest<Long, Greeting> request = new PartialUpdateGreetingRequestBuilders().batchPartialUpdateAndGet().input(expectedId, PatchRequest.createFromEmptyPatchDocument()).setParam(RestConstants.RETURN_ENTITY_PARAM, invalidParamValue).build();
try {
getClient().sendRequest(request).getResponse();
Assert.fail(String.format("Query parameter should cause an exception: %s=%s", RestConstants.RETURN_ENTITY_PARAM, invalidParamValue));
} catch (RestLiResponseException e) {
Assert.assertEquals(e.getStatus(), HttpStatus.S_400_BAD_REQUEST.getCode(), "Invalid response status.");
Assert.assertTrue(e.getServiceErrorMessage().contains(String.format("Invalid \"%s\" parameter: %s", RestConstants.RETURN_ENTITY_PARAM, invalidParamValue)), "Invalid error response message");
}
}
use of com.linkedin.restli.examples.greetings.client.PartialUpdateGreetingRequestBuilders in project rest.li by linkedin.
the class TestReturnEntityWithBatchPartialUpdate method testBatchPartialUpdateEntitiesWithProjection.
/**
* Same as {@link this#testBatchPartialUpdateEntities}, except the fields of the returned entities are projected.
*
* @param patches patches to send for this request
* @param expectedGreetings expected response entities for this request
*/
@Test(dataProvider = "batchPartialUpdateData")
public void testBatchPartialUpdateEntitiesWithProjection(Map<Long, PatchRequest<Greeting>> patches, Map<Long, Greeting> expectedGreetings) throws RemoteInvocationException {
final Greeting.Fields fields = Greeting.fields();
BatchPartialUpdateEntityRequest<Long, Greeting> request = new PartialUpdateGreetingRequestBuilders().batchPartialUpdateAndGet().inputs(patches).fields(fields.id(), fields.message()).build();
Response<BatchKVResponse<Long, UpdateEntityStatus<Greeting>>> response = getClient().sendRequest(request).getResponse();
Assert.assertNotNull(response);
BatchKVResponse<Long, UpdateEntityStatus<Greeting>> batchKVResponse = response.getEntity();
Assert.assertNotNull(batchKVResponse);
Assert.assertTrue(batchKVResponse.getErrors().isEmpty());
Map<Long, UpdateEntityStatus<Greeting>> greetings = batchKVResponse.getResults();
Assert.assertNotNull(greetings);
for (Long key : greetings.keySet()) {
Assert.assertTrue(expectedGreetings.containsKey(key));
UpdateEntityStatus<Greeting> updateEntityStatus = greetings.get(key);
Assert.assertNotNull(updateEntityStatus);
Assert.assertEquals(updateEntityStatus.getStatus().intValue(), HttpStatus.S_200_OK.getCode());
Assert.assertTrue(updateEntityStatus.hasEntity());
Assert.assertFalse(updateEntityStatus.hasError());
Greeting greeting = updateEntityStatus.getEntity();
Greeting expectedGreeting = expectedGreetings.get(key);
Assert.assertNotNull(greeting);
Assert.assertNotNull(expectedGreeting);
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.");
}
}
use of com.linkedin.restli.examples.greetings.client.PartialUpdateGreetingRequestBuilders in project rest.li by linkedin.
the class TestReturnEntityWithBatchPartialUpdate method testBatchPartialUpdateError.
/**
* Ensures that uncaught errors and handled correctly by the server resource and sent back as error responses.
* This test coerces the server resource to throw an uncaught 500 error.
*/
@Test
public void testBatchPartialUpdateError() throws RemoteInvocationException, MimeTypeParseException, IOException {
BatchPartialUpdateEntityRequest<Long, Greeting> request = new PartialUpdateGreetingRequestBuilders().batchPartialUpdateAndGet().input(3L, makeGreetingMessagePatch(";DROP TABLE")).build();
try {
getClient().sendRequest(request).getResponse();
Assert.fail("Expected error response.");
} catch (RestLiResponseException e) {
Assert.assertEquals(e.getStatus(), HttpStatus.S_500_INTERNAL_SERVER_ERROR.getCode());
}
}
use of com.linkedin.restli.examples.greetings.client.PartialUpdateGreetingRequestBuilders in project rest.li by linkedin.
the class TestReturnEntityWithPartialUpdate method testPartialUpdateEntity.
/**
* Sends partial update requests to the server and ensures that the returned entity for each request is
* consistent with the expected state of the server's entities.
*
* @param patch patch to send for this request
* @param expectedGreeting expected response entity for this request
*/
@Test(dataProvider = "partialUpdateData")
public void testPartialUpdateEntity(PatchRequest<Greeting> patch, Greeting expectedGreeting) throws RemoteInvocationException {
PartialUpdateEntityRequest<Greeting> request = new PartialUpdateGreetingRequestBuilders().partialUpdateAndGet().id(1L).input(patch).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.assertEquals(greeting.getId(), expectedGreeting.getId());
Assert.assertEquals(greeting.getMessage(), expectedGreeting.getMessage());
Assert.assertEquals(greeting.getTone(), expectedGreeting.getTone());
}
Aggregations