Search in sources :

Example 11 with AnyRecord

use of com.linkedin.restli.internal.server.methods.AnyRecord in project rest.li by linkedin.

the class TestRestLiResponseEnvelope method testSetNewEnvelopeData.

@Test(dataProvider = "envelopeResourceMethodDataProvider")
public void testSetNewEnvelopeData(RestLiResponseEnvelope responseEnvelope, ResourceMethod resourceMethod) {
    ResponseType responseType = ResponseType.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<String>(new DataMap(), "key");
            RestLiServiceException newException = new RestLiServiceException(HttpStatus.S_500_INTERNAL_SERVER_ERROR);
            BatchCreateResponseEnvelope.CollectionCreateResponseItem successCreateItem = new BatchCreateResponseEnvelope.CollectionCreateResponseItem(newCreateIdStatus);
            BatchCreateResponseEnvelope.CollectionCreateResponseItem exceptionCreateItem = new BatchCreateResponseEnvelope.CollectionCreateResponseItem(newException, "id2");
            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 firstItem = batchCreateResponseEnvelope.getCreateResponses().get(0);
            Assert.assertNull(firstItem.getId());
            Assert.assertEquals(firstItem.getRecord(), newCreateIdStatus);
            Assert.assertFalse(firstItem.isErrorResponse());
            Assert.assertNull(firstItem.getException());
            BatchCreateResponseEnvelope.CollectionCreateResponseItem secondItem = batchCreateResponseEnvelope.getCreateResponses().get(1);
            Assert.assertEquals(secondItem.getId(), "id2");
            Assert.assertNull(secondItem.getRecord());
            Assert.assertTrue(secondItem.isErrorResponse());
            Assert.assertEquals(secondItem.getException(), newException);
            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<String, BatchResponseEnvelope.BatchResponseEntry>();
            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 STATUS_ONLY:
            // status only envelopes are blank by default since they have no data fields
            break;
        default:
            throw new IllegalStateException();
    }
}
Also used : EmptyRecord(com.linkedin.restli.common.EmptyRecord) CollectionMetadata(com.linkedin.restli.common.CollectionMetadata) AnyRecord(com.linkedin.restli.internal.server.methods.AnyRecord) HashMap(java.util.HashMap) CreateIdStatus(com.linkedin.restli.common.CreateIdStatus) ResponseType(com.linkedin.restli.internal.server.ResponseType) DataMap(com.linkedin.data.DataMap) RestLiServiceException(com.linkedin.restli.server.RestLiServiceException) RecordTemplate(com.linkedin.data.template.RecordTemplate) Test(org.testng.annotations.Test)

Aggregations

AnyRecord (com.linkedin.restli.internal.server.methods.AnyRecord)11 DataMap (com.linkedin.data.DataMap)9 RecordTemplate (com.linkedin.data.template.RecordTemplate)7 RestLiServiceException (com.linkedin.restli.server.RestLiServiceException)5 HashMap (java.util.HashMap)5 ServerResourceContext (com.linkedin.restli.internal.server.ServerResourceContext)4 ResourceContext (com.linkedin.restli.server.ResourceContext)4 ProtocolVersion (com.linkedin.restli.common.ProtocolVersion)3 BatchResponseEntry (com.linkedin.restli.internal.server.response.BatchResponseEnvelope.BatchResponseEntry)3 Map (java.util.Map)3 CollectionMetadata (com.linkedin.restli.common.CollectionMetadata)2 CreateIdStatus (com.linkedin.restli.common.CreateIdStatus)2 HttpStatus (com.linkedin.restli.common.HttpStatus)2 UpdateStatus (com.linkedin.restli.common.UpdateStatus)2 CreateKVResponse (com.linkedin.restli.server.CreateKVResponse)2 CreateResponse (com.linkedin.restli.server.CreateResponse)2 ArrayList (java.util.ArrayList)2 Test (org.testng.annotations.Test)2 DataList (com.linkedin.data.DataList)1 UriBuilder (com.linkedin.jersey.api.uri.UriBuilder)1