use of com.linkedin.restli.common.CreateIdStatus in project rest.li by linkedin.
the class TestComplexKeysResource method testBatchCreateMain.
private void testBatchCreateMain(BatchCreateRequestBuilder<ComplexResourceKey<TwoPartKey, TwoPartKey>, Message> batchCreateRequestBuilder, BatchGetRequestBuilder<ComplexResourceKey<TwoPartKey, TwoPartKey>, Message> batchGetRequestBuilder) throws RemoteInvocationException {
final String messageText1 = "firstMessage";
Message message1 = new Message();
message1.setMessage(messageText1);
final String messageText2 = "secondMessage";
Message message2 = new Message();
message2.setMessage(messageText2);
List<Message> messages = new ArrayList<>(2);
messages.add(message1);
messages.add(message2);
ComplexResourceKey<TwoPartKey, TwoPartKey> expectedComplexKey1 = getComplexKey(messageText1, messageText1);
ComplexResourceKey<TwoPartKey, TwoPartKey> expectedComplexKey2 = getComplexKey(messageText2, messageText2);
// test build
Request<CollectionResponse<CreateStatus>> request = batchCreateRequestBuilder.inputs(messages).build();
ResponseFuture<CollectionResponse<CreateStatus>> future = getClient().sendRequest(request);
Response<CollectionResponse<CreateStatus>> response = future.getResponse();
Assert.assertEquals(response.getStatus(), 200);
Set<ComplexResourceKey<TwoPartKey, TwoPartKey>> expectedComplexKeys = new HashSet<>(2);
expectedComplexKeys.add(expectedComplexKey1);
expectedComplexKeys.add(expectedComplexKey2);
for (CreateStatus createStatus : response.getEntity().getElements()) {
@SuppressWarnings("unchecked") CreateIdStatus<ComplexResourceKey<TwoPartKey, TwoPartKey>> createIdStatus = (CreateIdStatus<ComplexResourceKey<TwoPartKey, TwoPartKey>>) createStatus;
Assert.assertEquals(createIdStatus.getStatus(), Integer.valueOf(201));
Assert.assertTrue(expectedComplexKeys.contains(createIdStatus.getKey()));
try {
@SuppressWarnings("deprecation") String id = createIdStatus.getId();
Assert.fail("buildReadOnlyId should throw an exception on ComplexKeys");
} catch (UnsupportedOperationException e) {
// expected
}
expectedComplexKeys.remove(createIdStatus.getKey());
}
Assert.assertTrue(expectedComplexKeys.isEmpty());
// attempt to batch get created records
List<ComplexResourceKey<TwoPartKey, TwoPartKey>> createdKeys = new ArrayList<>(2);
createdKeys.add(expectedComplexKey1);
createdKeys.add(expectedComplexKey2);
BatchGetKVRequest<ComplexResourceKey<TwoPartKey, TwoPartKey>, Message> getRequest = batchGetRequestBuilder.ids(createdKeys).buildKV();
ResponseFuture<BatchKVResponse<ComplexResourceKey<TwoPartKey, TwoPartKey>, Message>> getFuture = getClient().sendRequest(getRequest);
Response<BatchKVResponse<ComplexResourceKey<TwoPartKey, TwoPartKey>, Message>> getResponse = getFuture.getResponse();
Map<ComplexResourceKey<TwoPartKey, TwoPartKey>, Message> getResults = getResponse.getEntity().getResults();
Assert.assertEquals(getResults.get(expectedComplexKey1), message1);
Assert.assertEquals(getResults.get(expectedComplexKey2), message2);
Assert.assertEquals(getResults.size(), 2);
}
use of com.linkedin.restli.common.CreateIdStatus in project rest.li by linkedin.
the class TestCustomTypesClient method testCollectionBatchCreateId.
@Test(dataProvider = com.linkedin.restli.internal.common.TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "requestOptionsDataProvider")
public void testCollectionBatchCreateId(RestliRequestOptions options) throws RemoteInvocationException {
CustomTypes2RequestBuilders builders = new CustomTypes2RequestBuilders(options);
BatchCreateIdRequest<CustomLong, Greeting> request = builders.batchCreate().input(new Greeting().setId(1)).input(new Greeting().setId(2)).build();
Response<BatchCreateIdResponse<CustomLong>> response = getClient().sendRequest(request).getResponse();
List<CreateIdStatus<CustomLong>> results = response.getEntity().getElements();
Set<CustomLong> expectedKeys = new HashSet<>();
expectedKeys.add(new CustomLong(1L));
expectedKeys.add(new CustomLong(2L));
for (CreateIdStatus<CustomLong> status : results) {
Assert.assertEquals(status.getStatus().intValue(), HttpStatus.S_204_NO_CONTENT.getCode());
Assert.assertTrue(expectedKeys.contains(status.getKey()));
@SuppressWarnings("deprecation") String id = status.getId();
Assert.assertEquals(BatchResponse.keyToString(status.getKey(), ProtocolVersionUtil.extractProtocolVersion(response.getHeaders())), id);
expectedKeys.remove(status.getKey());
}
Assert.assertTrue(expectedKeys.isEmpty());
}
use of com.linkedin.restli.common.CreateIdStatus in project rest.li by linkedin.
the class TestCustomTypesClient method testCollectionBatchCreate.
@Test(dataProvider = com.linkedin.restli.internal.common.TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "requestOptionsDataProvider")
public void testCollectionBatchCreate(RestliRequestOptions options) throws RemoteInvocationException {
CustomTypes2Builders builders = new CustomTypes2Builders(options);
BatchCreateRequest<Greeting> request = builders.batchCreate().input(new Greeting().setId(1)).input(new Greeting().setId(2)).build();
Response<CollectionResponse<CreateStatus>> response = getClient().sendRequest(request).getResponse();
List<CreateStatus> results = response.getEntity().getElements();
Set<CustomLong> expectedKeys = new HashSet<>();
expectedKeys.add(new CustomLong(1L));
expectedKeys.add(new CustomLong(2L));
for (CreateStatus status : results) {
@SuppressWarnings("unchecked") CreateIdStatus<CustomLong> createIdStatus = (CreateIdStatus<CustomLong>) status;
Assert.assertEquals(createIdStatus.getStatus().intValue(), HttpStatus.S_204_NO_CONTENT.getCode());
Assert.assertTrue(expectedKeys.contains(createIdStatus.getKey()));
@SuppressWarnings("deprecation") String id = createIdStatus.getId();
Assert.assertEquals(BatchResponse.keyToString(createIdStatus.getKey(), ProtocolVersionUtil.extractProtocolVersion(response.getHeaders())), id);
expectedKeys.remove(createIdStatus.getKey());
}
Assert.assertTrue(expectedKeys.isEmpty());
}
use of com.linkedin.restli.common.CreateIdStatus in project rest.li by linkedin.
the class BatchCreateResponseBuilder method buildRestLiResponseData.
/**
* {@inheritDoc}
*
* @param result The result for a Rest.li BATCH_CREATE method. It's an instance of {@link BatchCreateResult}, if the
* BATCH_CREATE method doesn't return the entity; or an instance of {@link BatchCreateKVResult}, if it
* does.
*/
@Override
public RestLiResponseData<BatchCreateResponseEnvelope> buildRestLiResponseData(Request request, RoutingResult routingResult, Object result, Map<String, String> headers, List<HttpCookie> cookies) {
Object altKey = null;
if (routingResult.getContext().hasParameter(RestConstants.ALT_KEY_PARAM)) {
altKey = routingResult.getContext().getParameter(RestConstants.ALT_KEY_PARAM);
}
final ProtocolVersion protocolVersion = ProtocolVersionUtil.extractProtocolVersion(headers);
final ResourceContext resourceContext = routingResult.getContext();
if (result instanceof BatchCreateKVResult && resourceContext.isReturnEntityRequested()) {
BatchCreateKVResult<?, ?> list = (BatchCreateKVResult<?, ?>) result;
if (list.getResults() == null) {
throw new RestLiServiceException(HttpStatus.S_500_INTERNAL_SERVER_ERROR, "Unexpected null encountered. Null List inside of a BatchCreateKVResult returned by the resource method: " + routingResult.getResourceMethod());
}
List<BatchCreateResponseEnvelope.CollectionCreateResponseItem> collectionCreateList = new ArrayList<>(list.getResults().size());
TimingContextUtil.beginTiming(routingResult.getContext().getRawRequestContext(), FrameworkTimingKeys.SERVER_RESPONSE_RESTLI_PROJECTION_APPLY.key());
for (CreateKVResponse<?, ?> createKVResponse : list.getResults()) {
if (createKVResponse == null) {
throw new RestLiServiceException(HttpStatus.S_500_INTERNAL_SERVER_ERROR, "Unexpected null encountered. Null element inside of List inside of a BatchCreateKVResult returned by the resource method: " + routingResult.getResourceMethod());
} else {
Object id = ResponseUtils.translateCanonicalKeyToAlternativeKeyIfNeeded(createKVResponse.getId(), routingResult);
if (createKVResponse.getError() == null) {
DataMap entityData = createKVResponse.getEntity() != null ? createKVResponse.getEntity().data() : null;
final DataMap data = RestUtils.projectFields(entityData, resourceContext);
CreateIdEntityStatus<Object, RecordTemplate> entry = new CreateIdEntityStatus<>(createKVResponse.getStatus().getCode(), id, new AnyRecord(data), // location uri
getLocationUri(request, id, altKey, protocolVersion), null, protocolVersion);
collectionCreateList.add(new BatchCreateResponseEnvelope.CollectionCreateResponseItem(entry));
} else {
collectionCreateList.add(new BatchCreateResponseEnvelope.CollectionCreateResponseItem(createKVResponse.getError()));
}
}
}
TimingContextUtil.endTiming(routingResult.getContext().getRawRequestContext(), FrameworkTimingKeys.SERVER_RESPONSE_RESTLI_PROJECTION_APPLY.key());
return new RestLiResponseDataImpl<>(new BatchCreateResponseEnvelope(HttpStatus.S_200_OK, collectionCreateList, true), headers, cookies);
} else {
List<? extends CreateResponse> createResponses = extractCreateResponseList(result);
// Verify that a null list was not passed into the BatchCreateResult. If so, this is a developer error.
if (createResponses == null) {
throw new RestLiServiceException(HttpStatus.S_500_INTERNAL_SERVER_ERROR, "Unexpected null encountered. Null List inside of a BatchCreateResult returned by the resource method: " + routingResult.getResourceMethod());
}
List<BatchCreateResponseEnvelope.CollectionCreateResponseItem> collectionCreateList = new ArrayList<>(createResponses.size());
for (CreateResponse createResponse : createResponses) {
// Verify that a null element was not passed into the BatchCreateResult list. If so, this is a developer error.
if (createResponse == null) {
throw new RestLiServiceException(HttpStatus.S_500_INTERNAL_SERVER_ERROR, "Unexpected null encountered. Null element inside of List inside of a BatchCreateResult returned by the resource method: " + routingResult.getResourceMethod());
} else {
Object id = ResponseUtils.translateCanonicalKeyToAlternativeKeyIfNeeded(createResponse.getId(), routingResult);
if (createResponse.getError() == null) {
CreateIdStatus<Object> entry = new CreateIdStatus<>(createResponse.getStatus().getCode(), id, // location uri
getLocationUri(request, id, altKey, protocolVersion), null, protocolVersion);
collectionCreateList.add(new BatchCreateResponseEnvelope.CollectionCreateResponseItem(entry));
} else {
collectionCreateList.add(new BatchCreateResponseEnvelope.CollectionCreateResponseItem(createResponse.getError()));
}
}
}
return new RestLiResponseDataImpl<>(new BatchCreateResponseEnvelope(HttpStatus.S_200_OK, collectionCreateList, false), headers, cookies);
}
}
use of com.linkedin.restli.common.CreateIdStatus in project rest.li by linkedin.
the class TestRestLiResponseData method testCreateCollectionResponseEnvelopeUpdates.
@Test(dataProvider = "createCollectionResponseEnvelopesProvider")
public void testCreateCollectionResponseEnvelopeUpdates(RestLiResponseDataImpl responseData) {
BatchCreateResponseEnvelope responseEnvelope = responseData.getBatchCreateResponseEnvelope();
Assert.assertNull(responseData.getServiceException());
Assert.assertEquals(responseEnvelope.getCreateResponses(), Collections.emptyList());
Assert.assertFalse(responseData.isErrorResponse());
responseData.setException(exception500);
Assert.assertNull(responseEnvelope.getCreateResponses());
responseEnvelope.setCreateResponse(new ArrayList<BatchCreateResponseEnvelope.CollectionCreateResponseItem>(), HttpStatus.S_200_OK);
Assert.assertNull(responseData.getServiceException());
Assert.assertEquals(responseEnvelope.getCreateResponses().size(), 0);
responseEnvelope.getCreateResponses().add(new BatchCreateResponseEnvelope.CollectionCreateResponseItem(new CreateIdStatus<Object>(new DataMap(), new Object())));
responseEnvelope.getCreateResponses().add(new BatchCreateResponseEnvelope.CollectionCreateResponseItem(exception500, 2));
Assert.assertEquals(responseEnvelope.getCreateResponses().size(), 2);
}
Aggregations