use of com.linkedin.restli.common.CreateIdStatus in project rest.li by linkedin.
the class TestRestLiResponseEnvelope method testSetNewEnvelopeData.
@Test(dataProvider = "envelopeResourceMethodDataProvider")
public void testSetNewEnvelopeData(RestLiResponseEnvelope responseEnvelope, ResourceMethod resourceMethod) {
// If response type is dynamically determined, directly cast to resource method response envelope
if (ResponseTypeUtil.isDynamicallyDetermined(resourceMethod)) {
switch(resourceMethod) {
case PARTIAL_UPDATE:
PartialUpdateResponseEnvelope partialUpdateResponseEnvelope = (PartialUpdateResponseEnvelope) responseEnvelope;
RecordTemplate oldRecord = partialUpdateResponseEnvelope.getRecord();
RecordTemplate newRecord = new AnyRecord(new DataMap());
newRecord.data().put("test", "testing");
partialUpdateResponseEnvelope.setRecord(newRecord, HttpStatus.S_200_OK);
Assert.assertNotEquals(partialUpdateResponseEnvelope.getRecord(), oldRecord);
break;
default:
throw new IllegalStateException();
}
} else // Otherwise, cast to response type response envelope
{
ResponseType responseType = ResponseTypeUtil.fromMethodType(resourceMethod);
switch(responseType) {
case SINGLE_ENTITY:
RecordResponseEnvelope recordResponseEnvelope = (RecordResponseEnvelope) responseEnvelope;
RecordTemplate oldRecord = recordResponseEnvelope.getRecord();
RecordTemplate newRecord = new AnyRecord(new DataMap());
newRecord.data().put("test", "testing");
recordResponseEnvelope.setRecord(newRecord, HttpStatus.S_200_OK);
Assert.assertNotEquals(recordResponseEnvelope.getRecord(), oldRecord);
break;
case GET_COLLECTION:
CollectionResponseEnvelope collectionResponseEnvelope = (CollectionResponseEnvelope) responseEnvelope;
List<? extends RecordTemplate> oldResponses = collectionResponseEnvelope.getCollectionResponse();
RecordTemplate oldResponseMetadata = collectionResponseEnvelope.getCollectionResponseCustomMetadata();
CollectionMetadata oldPagingMetadata = collectionResponseEnvelope.getCollectionResponsePaging();
RecordTemplate newResponseMetadata = new AnyRecord(new DataMap());
newResponseMetadata.data().put("test", "testing");
CollectionMetadata newResponsesPaging = new CollectionMetadata();
List<? extends RecordTemplate> newResponses = Arrays.asList(new AnyRecord(new DataMap()));
collectionResponseEnvelope.setCollectionResponse(newResponses, newResponsesPaging, newResponseMetadata, HttpStatus.S_200_OK);
Assert.assertNotEquals(collectionResponseEnvelope.getCollectionResponse(), oldResponses);
Assert.assertNotEquals(collectionResponseEnvelope.getCollectionResponseCustomMetadata(), oldResponseMetadata);
Assert.assertNotEquals(collectionResponseEnvelope.getCollectionResponsePaging(), oldPagingMetadata);
Assert.assertEquals(collectionResponseEnvelope.getCollectionResponse(), newResponses);
Assert.assertEquals(collectionResponseEnvelope.getCollectionResponseCustomMetadata(), newResponseMetadata);
Assert.assertEquals(collectionResponseEnvelope.getCollectionResponsePaging(), newResponsesPaging);
break;
case CREATE_COLLECTION:
BatchCreateResponseEnvelope batchCreateResponseEnvelope = (BatchCreateResponseEnvelope) responseEnvelope;
List<BatchCreateResponseEnvelope.CollectionCreateResponseItem> oldCreateResponses = batchCreateResponseEnvelope.getCreateResponses();
CreateIdStatus<String> newCreateIdStatus = new CreateIdStatus<>(HttpStatus.S_201_CREATED.getCode(), "key", null, AllProtocolVersions.RESTLI_PROTOCOL_2_0_0.getProtocolVersion());
RestLiServiceException newException = new RestLiServiceException(HttpStatus.S_500_INTERNAL_SERVER_ERROR);
BatchCreateResponseEnvelope.CollectionCreateResponseItem successCreateItem = new BatchCreateResponseEnvelope.CollectionCreateResponseItem(newCreateIdStatus);
BatchCreateResponseEnvelope.CollectionCreateResponseItem exceptionCreateItem = new BatchCreateResponseEnvelope.CollectionCreateResponseItem(newException);
List<BatchCreateResponseEnvelope.CollectionCreateResponseItem> newCreateResponses = Arrays.asList(successCreateItem, exceptionCreateItem);
batchCreateResponseEnvelope.setCreateResponse(newCreateResponses, HttpStatus.S_200_OK);
Assert.assertNotEquals(batchCreateResponseEnvelope.getCreateResponses(), oldCreateResponses);
Assert.assertEquals(batchCreateResponseEnvelope.getCreateResponses(), newCreateResponses);
BatchCreateResponseEnvelope.CollectionCreateResponseItem successItem = batchCreateResponseEnvelope.getCreateResponses().get(0);
Assert.assertEquals(successItem.getRecord(), newCreateIdStatus);
Assert.assertEquals(successItem.getId(), "key");
Assert.assertFalse(successItem.isErrorResponse());
Assert.assertNull(successItem.getException());
Assert.assertEquals(successItem.getStatus(), HttpStatus.S_201_CREATED);
BatchCreateResponseEnvelope.CollectionCreateResponseItem errorItem = batchCreateResponseEnvelope.getCreateResponses().get(1);
Assert.assertNull(errorItem.getRecord());
Assert.assertNull(errorItem.getId());
Assert.assertTrue(errorItem.isErrorResponse());
Assert.assertEquals(errorItem.getException(), newException);
Assert.assertEquals(errorItem.getStatus(), HttpStatus.S_500_INTERNAL_SERVER_ERROR);
break;
case BATCH_ENTITIES:
BatchResponseEnvelope batchResponseEnvelope = (BatchResponseEnvelope) responseEnvelope;
Map<?, BatchResponseEnvelope.BatchResponseEntry> oldBatchResponses = batchResponseEnvelope.getBatchResponseMap();
RecordTemplate newResponseRecord = new EmptyRecord();
RestLiServiceException newResponseException = new RestLiServiceException(HttpStatus.S_500_INTERNAL_SERVER_ERROR);
Map<String, BatchResponseEnvelope.BatchResponseEntry> newBatchResponses = new HashMap<>();
newBatchResponses.put("id1", new BatchResponseEnvelope.BatchResponseEntry(HttpStatus.S_200_OK, newResponseRecord));
newBatchResponses.put("id2", new BatchResponseEnvelope.BatchResponseEntry(HttpStatus.S_500_INTERNAL_SERVER_ERROR, newResponseException));
batchResponseEnvelope.setBatchResponseMap(newBatchResponses, HttpStatus.S_200_OK);
Map<?, BatchResponseEnvelope.BatchResponseEntry> envelopeMap = batchResponseEnvelope.getBatchResponseMap();
Assert.assertNotEquals(envelopeMap, oldBatchResponses);
Assert.assertEquals(envelopeMap, newBatchResponses);
BatchResponseEnvelope.BatchResponseEntry id1Entry = envelopeMap.get("id1");
Assert.assertEquals(id1Entry.getStatus(), HttpStatus.S_200_OK);
Assert.assertEquals(id1Entry.getRecord(), newResponseRecord);
Assert.assertFalse(id1Entry.hasException());
Assert.assertNull(id1Entry.getException());
BatchResponseEnvelope.BatchResponseEntry id2Entry = envelopeMap.get("id2");
Assert.assertEquals(id2Entry.getStatus(), HttpStatus.S_500_INTERNAL_SERVER_ERROR);
Assert.assertNull(id2Entry.getRecord());
Assert.assertTrue(id2Entry.hasException());
Assert.assertEquals(id2Entry.getException(), newResponseException);
break;
case BATCH_COLLECTION:
BatchFinderResponseEnvelope batchFinderResponseEnvelope = (BatchFinderResponseEnvelope) responseEnvelope;
List<BatchFinderResponseEnvelope.BatchFinderEntry> oldItems = batchFinderResponseEnvelope.getItems();
List<BatchFinderResponseEnvelope.BatchFinderEntry> newItems = new ArrayList<>(2);
RestLiServiceException newBFResponseException = new RestLiServiceException(HttpStatus.S_500_INTERNAL_SERVER_ERROR);
newItems.add(new BatchFinderResponseEnvelope.BatchFinderEntry(newBFResponseException));
RecordTemplate newBFResponseRecord = new EmptyRecord();
List<RecordTemplate> elements = Arrays.asList(newBFResponseRecord);
RecordTemplate newBFResponseMetadata = new AnyRecord(new DataMap());
newBFResponseMetadata.data().put("test", "testing");
CollectionMetadata newBFResponsesPaging = new CollectionMetadata();
newItems.add(new BatchFinderResponseEnvelope.BatchFinderEntry(elements, newBFResponsesPaging, newBFResponseMetadata));
batchFinderResponseEnvelope.setItems(newItems);
Assert.assertNotEquals(batchFinderResponseEnvelope.getItems(), oldItems);
Assert.assertEquals(batchFinderResponseEnvelope.getItems(), newItems);
Assert.assertEquals(batchFinderResponseEnvelope.getItems().get(0).getException(), newBFResponseException);
Assert.assertEquals(batchFinderResponseEnvelope.getItems().get(1).getElements(), elements);
Assert.assertEquals(batchFinderResponseEnvelope.getItems().get(1).getPaging(), newBFResponsesPaging);
Assert.assertEquals(batchFinderResponseEnvelope.getItems().get(1).getCustomMetadata(), newBFResponseMetadata);
break;
case STATUS_ONLY:
// status only envelopes are blank by default since they have no data fields
break;
default:
throw new IllegalStateException();
}
}
}
use of com.linkedin.restli.common.CreateIdStatus in project rest.li by linkedin.
the class BatchCreateResponseBuilder method buildResponse.
@Override
@SuppressWarnings("unchecked")
public RestLiResponse buildResponse(RoutingResult routingResult, RestLiResponseData<BatchCreateResponseEnvelope> responseData) {
List<BatchCreateResponseEnvelope.CollectionCreateResponseItem> collectionCreateResponses = responseData.getResponseEnvelope().getCreateResponses();
List<CreateIdStatus<Object>> formattedResponses = new ArrayList<>(collectionCreateResponses.size());
// Otherwise, add the result as is.
for (BatchCreateResponseEnvelope.CollectionCreateResponseItem response : collectionCreateResponses) {
if (response.isErrorResponse()) {
RestLiServiceException exception = response.getException();
formattedResponses.add(new CreateIdStatus<>(exception.getStatus().getCode(), response.getId(), _errorResponseBuilder.buildErrorResponse(exception), ProtocolVersionUtil.extractProtocolVersion(responseData.getHeaders())));
} else {
formattedResponses.add((CreateIdStatus<Object>) response.getRecord());
}
}
RestLiResponse.Builder builder = new RestLiResponse.Builder();
BatchCreateIdResponse<Object> batchCreateIdResponse = new BatchCreateIdResponse<>(formattedResponses);
return builder.headers(responseData.getHeaders()).cookies(responseData.getCookies()).entity(batchCreateIdResponse).build();
}
Aggregations