Search in sources :

Example 1 with CreateGreetingBatchCreateAndGetRequestBuilder

use of com.linkedin.restli.examples.greetings.client.CreateGreetingBatchCreateAndGetRequestBuilder 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.");
        }
    }
}
Also used : Greeting(com.linkedin.restli.examples.greetings.api.Greeting) CreateIdEntityStatus(com.linkedin.restli.common.CreateIdEntityStatus) ArrayList(java.util.ArrayList) CreateGreetingBatchCreateAndGetRequestBuilder(com.linkedin.restli.examples.greetings.client.CreateGreetingBatchCreateAndGetRequestBuilder) BatchCreateIdEntityResponse(com.linkedin.restli.common.BatchCreateIdEntityResponse) CreateGreetingRequestBuilders(com.linkedin.restli.examples.greetings.client.CreateGreetingRequestBuilders) Test(org.testng.annotations.Test)

Aggregations

BatchCreateIdEntityResponse (com.linkedin.restli.common.BatchCreateIdEntityResponse)1 CreateIdEntityStatus (com.linkedin.restli.common.CreateIdEntityStatus)1 Greeting (com.linkedin.restli.examples.greetings.api.Greeting)1 CreateGreetingBatchCreateAndGetRequestBuilder (com.linkedin.restli.examples.greetings.client.CreateGreetingBatchCreateAndGetRequestBuilder)1 CreateGreetingRequestBuilders (com.linkedin.restli.examples.greetings.client.CreateGreetingRequestBuilders)1 ArrayList (java.util.ArrayList)1 Test (org.testng.annotations.Test)1