use of com.linkedin.restli.examples.greetings.client.CreateGreetingRequestBuilders in project rest.li by linkedin.
the class TestReturnEntityWithCreate method testReturnEntityOnDemand.
/**
* Ensures that different usages of {@link com.linkedin.restli.client.CreateIdEntityRequestBuilder#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 {
Greeting greeting = new Greeting();
greeting.setMessage("second time!");
greeting.setTone(Tone.FRIENDLY);
CreateGreetingRequestBuilders builders = new CreateGreetingRequestBuilders();
CreateGreetingCreateAndGetRequestBuilder builder = builders.createAndGet().input(greeting);
if (returnEntity != null) {
builder.returnEntity(returnEntity);
}
CreateIdEntityRequest<Long, Greeting> createIdEntityRequest = builder.build();
Response<IdEntityResponse<Long, Greeting>> response = getClient().sendRequest(createIdEntityRequest).getResponse();
long id = response.getEntity().getId();
@SuppressWarnings("deprecation") String stringId = response.getId();
Assert.assertEquals(response.getStatus(), HttpStatus.S_201_CREATED.getCode());
Assert.assertEquals(response.getHeader(RestConstants.HEADER_LOCATION), "/" + CreateGreetingRequestBuilders.getPrimaryResource() + "/" + id);
Assert.assertEquals(id, Long.parseLong(stringId));
if (expectReturnEntity) {
Greeting returnedEntity = response.getEntity().getEntity();
Assert.assertNotNull(returnedEntity, "RecordTemplate entity in response should not be null.");
Assert.assertEquals(returnedEntity.getMessage(), greeting.getMessage(), "Expect returned entity message to match original.");
Assert.assertEquals(returnedEntity.getTone(), greeting.getTone(), "Expect returned entity tone to match original.");
} else {
Assert.assertNull(response.getEntity().getEntity(), "RecordTemplate entity in response should be null.");
}
}
use of com.linkedin.restli.examples.greetings.client.CreateGreetingRequestBuilders in project rest.li by linkedin.
the class TestReturnEntityWithCreate 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 CREATE.
*/
@Test
@SuppressWarnings({ "Duplicates" })
public void testInvalidReturnEntityParameter() throws RemoteInvocationException {
Greeting greeting = new Greeting();
greeting.setMessage("second time!");
greeting.setTone(Tone.FRIENDLY);
final String invalidParamValue = "NOTaBoolean";
CreateGreetingRequestBuilders builders = new CreateGreetingRequestBuilders();
CreateIdEntityRequest<Long, Greeting> createIdEntityRequest = builders.createAndGet().input(greeting).setParam(RestConstants.RETURN_ENTITY_PARAM, invalidParamValue).build();
try {
getClient().sendRequest(createIdEntityRequest).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.CreateGreetingRequestBuilders in project rest.li by linkedin.
the class TestReturnEntityWithCreate method testBatchCreateInvalidReturnEntityParameter.
/**
* 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_CREATE.
*/
@Test
@SuppressWarnings({ "Duplicates" })
public void testBatchCreateInvalidReturnEntityParameter() throws RemoteInvocationException {
Greeting greeting = new Greeting();
greeting.setMessage("second time!");
greeting.setTone(Tone.FRIENDLY);
Greeting greeting2 = new Greeting();
greeting2.setMessage("first time!");
greeting2.setTone(Tone.FRIENDLY);
List<Greeting> greetings = new ArrayList<>();
greetings.add(greeting);
greetings.add(greeting2);
final String invalidParamValue = "NOTaBoolean";
CreateGreetingRequestBuilders builders = new CreateGreetingRequestBuilders();
BatchCreateIdEntityRequest<Long, Greeting> batchCreateIdEntityRequest = builders.batchCreateAndGet().inputs(greetings).setParam(RestConstants.RETURN_ENTITY_PARAM, invalidParamValue).build();
try {
getClient().sendRequest(batchCreateIdEntityRequest).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.CreateGreetingRequestBuilders in project rest.li by linkedin.
the class TestReturnEntityWithCreate method testBatchCreateReturnEntityOnDemand.
/**
* Ensures that different usages of {@link com.linkedin.restli.client.CreateIdEntityRequestBuilder#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.
*/
@Test(dataProvider = "returnEntityOnDemandData")
public void testBatchCreateReturnEntityOnDemand(Boolean returnEntity, boolean expectReturnEntity) throws RemoteInvocationException {
Greeting greeting = new Greeting();
greeting.setMessage("second time!");
greeting.setTone(Tone.FRIENDLY);
Greeting greeting2 = new Greeting();
greeting2.setMessage("first time!");
greeting2.setTone(Tone.FRIENDLY);
List<Greeting> greetings = new ArrayList<>();
greetings.add(greeting);
greetings.add(greeting2);
CreateGreetingRequestBuilders builders = new CreateGreetingRequestBuilders();
CreateGreetingBatchCreateAndGetRequestBuilder builder = builders.batchCreateAndGet().inputs(greetings);
if (returnEntity != null) {
builder.returnEntity(returnEntity);
}
BatchCreateIdEntityRequest<Long, Greeting> batchCreateIdEntityRequest = builder.build();
Response<BatchCreateIdEntityResponse<Long, Greeting>> response = getClient().sendRequest(batchCreateIdEntityRequest).getResponse();
List<CreateIdEntityStatus<Long, Greeting>> createIdEntityStatuses = response.getEntity().getElements();
Assert.assertEquals(createIdEntityStatuses.size(), greetings.size(), "Expected size of batch response list to match size of input entity list.");
for (int i = 0; i < createIdEntityStatuses.size(); i++) {
CreateIdEntityStatus<Long, Greeting> createIdEntityStatus = createIdEntityStatuses.get(i);
Greeting expectedGreeting = greetings.get(i);
long id = createIdEntityStatus.getKey();
Assert.assertEquals((int) createIdEntityStatus.getStatus(), HttpStatus.S_201_CREATED.getCode());
Assert.assertEquals(createIdEntityStatus.getLocation(), "/" + CreateGreetingRequestBuilders.getPrimaryResource() + "/" + id);
if (expectReturnEntity) {
Greeting returnedEntity = createIdEntityStatus.getEntity();
Assert.assertNotNull(returnedEntity, "RecordTemplate entity in response should not be null.");
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(createIdEntityStatus.getEntity(), "RecordTemplate entity in response should be null.");
}
}
}
Aggregations