use of com.linkedin.restli.common.CreateIdEntityStatus 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.");
}
}
}
use of com.linkedin.restli.common.CreateIdEntityStatus in project rest.li by linkedin.
the class TestParseqBasedFluentClientApi method testBatchCreateReturnEntity.
@Test
public void testBatchCreateReturnEntity() throws Exception {
CreateGreeting greetings = new CreateGreetingFluentClient(_parSeqRestliClient, _parSeqUnitTestHelper.getEngine());
String msg1 = Double.toString(Math.random());
String msg2 = Double.toString(Math.random());
CompletionStage<List<CreateIdEntityStatus<Long, Greeting>>> result = greetings.batchCreateAndGet(Arrays.asList(getGreeting(msg1), getGreeting(msg2)));
CompletableFuture<List<CreateIdEntityStatus<Long, Greeting>>> future = result.toCompletableFuture();
List<CreateIdEntityStatus<Long, Greeting>> entities = future.get(5000, TimeUnit.MILLISECONDS);
Assert.assertEquals(entities.size(), 2);
Assert.assertNotNull(entities.get(0).getEntity());
Assert.assertEquals(msg1, entities.get(0).getEntity().getMessage());
Assert.assertNotNull(entities.get(1).getEntity());
Assert.assertEquals(msg2, entities.get(1).getEntity().getMessage());
}
use of com.linkedin.restli.common.CreateIdEntityStatus in project rest.li by linkedin.
the class TestBatchCreateResponseBuilder method testCreateKVResultBuilder.
@Test(dataProvider = "createKVResultBuilderTestData")
public void testCreateKVResultBuilder(String altKeyName, Map<String, AlternativeKey<?, ?>> alternativeKeyMap, List<CreateIdEntityStatus<?, Foo>> expectedResponses) throws URISyntaxException {
List<CreateKVResponse<Long, Foo>> createKVResponses = new ArrayList<>(2);
Foo foo1 = new Foo();
foo1.setStringField("foo1");
Foo foo2 = new Foo();
foo2.setStringField("foo2");
createKVResponses.add(new CreateKVResponse<>(1L, foo1));
createKVResponses.add(new CreateKVResponse<>(2L, foo2));
BatchCreateKVResult<Long, Foo> results = new BatchCreateKVResult<>(createKVResponses);
Map<String, String> headers = ResponseBuilderUtil.getHeaders();
ResourceMethodDescriptor mockDescriptor = getMockResourceMethodDescriptor(alternativeKeyMap);
ServerResourceContext mockContext = getMockKVResourceContext(altKeyName);
RoutingResult routingResult = new RoutingResult(mockContext, mockDescriptor);
BatchCreateResponseBuilder responseBuilder = new BatchCreateResponseBuilder(null);
RestRequest request = new RestRequestBuilder(new URI("/foo")).build();
RestLiResponseData<BatchCreateResponseEnvelope> responseData = responseBuilder.buildRestLiResponseData(request, routingResult, results, headers, Collections.emptyList());
RestLiResponse restResponse = responseBuilder.buildResponse(routingResult, responseData);
EasyMock.verify(mockDescriptor);
ResponseBuilderUtil.validateHeaders(restResponse, headers);
Assert.assertTrue(responseData.getResponseEnvelope().isGetAfterCreate());
List<CreateIdEntityStatus<Object, Foo>> items = new ArrayList<>();
for (BatchCreateResponseEnvelope.CollectionCreateResponseItem item : responseData.getResponseEnvelope().getCreateResponses()) {
@SuppressWarnings("unchecked") CreateIdEntityStatus<Object, Foo> record = (CreateIdEntityStatus<Object, Foo>) item.getRecord();
items.add(record);
}
Assert.assertEquals(items, expectedResponses);
Assert.assertEquals(restResponse.getStatus(), HttpStatus.S_200_OK);
}
use of com.linkedin.restli.common.CreateIdEntityStatus in project rest.li by linkedin.
the class TestBatchCreateResponseBuilder method testReturnEntityInBuildRestLiResponseData.
@Test(dataProvider = "returnEntityData")
@SuppressWarnings({ "Duplicates", "unchecked" })
public void testReturnEntityInBuildRestLiResponseData(Object batchCreateResult, List<CreateResponse> createResponses, boolean isReturnEntityRequested, boolean expectEntityReturned) throws URISyntaxException {
ServerResourceContext mockContext = EasyMock.createMock(ServerResourceContext.class);
EasyMock.expect(mockContext.hasParameter(RestConstants.ALT_KEY_PARAM)).andReturn(false).atLeastOnce();
EasyMock.expect(mockContext.isReturnEntityRequested()).andReturn(isReturnEntityRequested);
EasyMock.expect(mockContext.getProjectionMode()).andReturn(ProjectionMode.AUTOMATIC);
EasyMock.expect(mockContext.getProjectionMask()).andReturn(null);
EasyMock.expect(mockContext.getRawRequestContext()).andReturn(new RequestContext()).anyTimes();
EasyMock.expect(mockContext.getAlwaysProjectedFields()).andReturn(Collections.emptySet()).anyTimes();
EasyMock.replay(mockContext);
ResourceMethodDescriptor mockDescriptor = getMockResourceMethodDescriptor(null);
RoutingResult routingResult = new RoutingResult(mockContext, mockDescriptor);
BatchCreateResponseBuilder responseBuilder = new BatchCreateResponseBuilder(new ErrorResponseBuilder());
RestRequest request = new RestRequestBuilder(new URI("/foo")).build();
RestLiResponseData<BatchCreateResponseEnvelope> responseData = responseBuilder.buildRestLiResponseData(request, routingResult, batchCreateResult, Collections.emptyMap(), Collections.emptyList());
BatchCreateResponseEnvelope responseEnvelope = responseData.getResponseEnvelope();
Assert.assertEquals(responseEnvelope.isGetAfterCreate(), expectEntityReturned);
Assert.assertEquals(responseEnvelope.getCreateResponses().size(), createResponses.size());
for (int i = 0; i < createResponses.size(); i++) {
CreateIdStatus<Long> createIdStatus = (CreateIdStatus<Long>) responseEnvelope.getCreateResponses().get(i).getRecord();
CreateResponse createResponse = createResponses.get(i);
Assert.assertEquals(createIdStatus.getStatus().intValue(), HttpStatus.S_201_CREATED.getCode());
Assert.assertEquals(createIdStatus.getLocation(), "/foo/" + createResponse.getId());
if (expectEntityReturned) {
CreateIdEntityStatus<Long, Foo> createIdEntityStatus = (CreateIdEntityStatus<Long, Foo>) createIdStatus;
Assert.assertEquals(createIdEntityStatus.getEntity(), ((CreateKVResponse) createResponse).getEntity());
}
}
}
use of com.linkedin.restli.common.CreateIdEntityStatus in project rest.li by linkedin.
the class TestBatchCreateResponseBuilder method createKVResultBuilderTestData.
@DataProvider(name = "createKVResultBuilderTestData")
public Object[][] createKVResultBuilderTestData() {
Map<String, AlternativeKey<?, ?>> alternativeKeyMap = new HashMap<>();
alternativeKeyMap.put("alt", new AlternativeKey<>(new TestKeyCoercer(), String.class, new StringDataSchema()));
Foo foo1 = new Foo();
foo1.setStringField("foo1");
Foo foo2 = new Foo();
foo2.setStringField("foo2");
List<CreateIdEntityStatus<Long, Foo>> expectedResponses = new ArrayList<>(2);
expectedResponses.add(new CreateIdEntityStatus<>(201, 1L, foo1, "/foo/1", null, AllProtocolVersions.RESTLI_PROTOCOL_2_0_0.getProtocolVersion()));
expectedResponses.add(new CreateIdEntityStatus<>(201, 2L, foo2, "/foo/2", null, AllProtocolVersions.RESTLI_PROTOCOL_2_0_0.getProtocolVersion()));
List<CreateIdEntityStatus<String, Foo>> expectedAltResponses = new ArrayList<>(2);
expectedAltResponses.add(new CreateIdEntityStatus<>(201, "Alt1", foo1, "/foo/Alt1?altkey=alt", null, AllProtocolVersions.RESTLI_PROTOCOL_2_0_0.getProtocolVersion()));
expectedAltResponses.add(new CreateIdEntityStatus<>(201, "Alt2", foo2, "/foo/Alt2?altkey=alt", null, AllProtocolVersions.RESTLI_PROTOCOL_2_0_0.getProtocolVersion()));
return new Object[][] { { null, null, expectedResponses }, { "alt", alternativeKeyMap, expectedAltResponses } };
}
Aggregations