Search in sources :

Example 26 with CollectionMetadata

use of com.linkedin.restli.common.CollectionMetadata in project rest.li by linkedin.

the class TestCompressionServer method testSearchWithPostFilter.

@Test(dataProvider = com.linkedin.restli.internal.common.TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "clientsCompressedResponsesBuilderDataProvider")
public void testSearchWithPostFilter(RestClient client, String operationsForCompression, RootBuilderWrapper<Long, Greeting> builders, ProtocolVersion protocolVersion) throws RemoteInvocationException {
    Request<CollectionResponse<Greeting>> findRequest = builders.findBy("SearchWithPostFilter").paginate(0, 5).build();
    Response<CollectionResponse<Greeting>> response = client.sendRequest(findRequest).getResponse();
    checkHeaderForCompression(response, operationsForCompression, "finder:" + findRequest.getMethodName());
    CollectionResponse<Greeting> entity = response.getEntity();
    CollectionMetadata paging = entity.getPaging();
    Assert.assertEquals(paging.getStart().intValue(), 0);
    Assert.assertEquals(paging.getCount().intValue(), 5);
    // expected to be 4 instead of 5 because of post filter
    Assert.assertEquals(entity.getElements().size(), 4);
    // to accommodate post filtering, even though 4 are returned, next page should be 5-10.
    Link next = paging.getLinks().get(0);
    Assert.assertEquals(next.getRel(), "next");
    // Query parameter order is non deterministic
    // greetings?count=5&start=5&q=searchWithPostFilter"
    final Map<String, String> queryParamsMap = new HashMap<>();
    queryParamsMap.put("count", "5");
    queryParamsMap.put("start", "5");
    queryParamsMap.put("q", "searchWithPostFilter");
    final URIDetails uriDetails = new URIDetails(protocolVersion, "/greetings", null, queryParamsMap, null);
    URIDetails.testUriGeneration(next.getHref(), uriDetails);
}
Also used : Greeting(com.linkedin.restli.examples.greetings.api.Greeting) CollectionMetadata(com.linkedin.restli.common.CollectionMetadata) URIDetails(com.linkedin.restli.internal.testutils.URIDetails) HashMap(java.util.HashMap) CollectionResponse(com.linkedin.restli.common.CollectionResponse) Link(com.linkedin.restli.common.Link) Test(org.testng.annotations.Test)

Example 27 with CollectionMetadata

use of com.linkedin.restli.common.CollectionMetadata 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();
        }
    }
}
Also used : EmptyRecord(com.linkedin.restli.common.EmptyRecord) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) DataMap(com.linkedin.data.DataMap) RestLiServiceException(com.linkedin.restli.server.RestLiServiceException) RecordTemplate(com.linkedin.data.template.RecordTemplate) CollectionMetadata(com.linkedin.restli.common.CollectionMetadata) AnyRecord(com.linkedin.restli.internal.server.methods.AnyRecord) CreateIdStatus(com.linkedin.restli.common.CreateIdStatus) ResponseType(com.linkedin.restli.internal.server.ResponseType) Test(org.testng.annotations.Test)

Example 28 with CollectionMetadata

use of com.linkedin.restli.common.CollectionMetadata in project rest.li by linkedin.

the class TestCollectionResponseBuilder method dataProvider.

@DataProvider(name = "testData")
public Object[][] dataProvider() throws CloneNotSupportedException {
    Foo metadata = new Foo().setStringField("metadata").setIntField(7);
    Foo projectedMetadata = new Foo().setIntField(7);
    final List<Foo> generatedList = generateTestList();
    final List<Foo> testListWithProjection = generateTestListWithProjection();
    CollectionResult<Foo, Foo> collectionResult = new CollectionResult<>(generatedList, generatedList.size(), metadata);
    DataMap dataProjectionDataMap = new DataMap();
    dataProjectionDataMap.put("stringField", MaskOperation.POSITIVE_MASK_OP.getRepresentation());
    MaskTree dataMaskTree = new MaskTree(dataProjectionDataMap);
    DataMap metadataProjectionDataMap = new DataMap();
    metadataProjectionDataMap.put("intField", MaskOperation.POSITIVE_MASK_OP.getRepresentation());
    MaskTree metadataMaskTree = new MaskTree(metadataProjectionDataMap);
    DataMap pagingProjectDataMap = new DataMap();
    pagingProjectDataMap.put("count", MaskOperation.POSITIVE_MASK_OP.getRepresentation());
    MaskTree pagingMaskTree = new MaskTree(pagingProjectDataMap);
    CollectionMetadata collectionMetadata1 = new CollectionMetadata().setCount(10).setStart(0).setLinks(new LinkArray());
    CollectionMetadata collectionMetadata2 = collectionMetadata1.clone().setTotal(2);
    CollectionMetadata collectionMetadataWithProjection = new CollectionMetadata().setCount(10);
    ProjectionMode auto = ProjectionMode.AUTOMATIC;
    ProjectionMode manual = ProjectionMode.MANUAL;
    List<Object[]> data = new ArrayList<>();
    for (ResourceMethod resourceMethod : BUILDERS.keySet()) {
        // auto projection for data and metadata with null projection masks
        data.add(new Object[] { generatedList, null, generatedList, collectionMetadata1, null, null, null, auto, auto, resourceMethod });
        data.add(new Object[] { collectionResult, metadata.data(), collectionResult.getElements(), collectionMetadata2, null, null, null, auto, auto, resourceMethod });
        // manual projection for data and metadata with null projection masks
        data.add(new Object[] { generatedList, null, generatedList, collectionMetadata1, null, null, null, manual, manual, resourceMethod });
        data.add(new Object[] { collectionResult, metadata.data(), collectionResult.getElements(), collectionMetadata2, null, null, null, manual, manual, resourceMethod });
        // NOTE - we always apply projections to the CollectionMetaData if the paging MaskTree is non-null
        // since ProjectionMode.AUTOMATIC is used.
        // manual projection for data and metadata with non-null projection masks
        data.add(new Object[] { generatedList, null, generatedList, collectionMetadataWithProjection, dataMaskTree, metadataMaskTree, pagingMaskTree, manual, manual, resourceMethod });
        data.add(new Object[] { collectionResult, metadata.data(), collectionResult.getElements(), collectionMetadataWithProjection, dataMaskTree, metadataMaskTree, pagingMaskTree, manual, manual, resourceMethod });
        // auto projection for data with non-null data and paging projection masks
        data.add(new Object[] { generatedList, null, testListWithProjection, collectionMetadataWithProjection, dataMaskTree, null, pagingMaskTree, auto, auto, resourceMethod });
        // auto projection for data and metadata with non-null projection masks
        data.add(new Object[] { collectionResult, projectedMetadata.data(), testListWithProjection, collectionMetadataWithProjection, dataMaskTree, metadataMaskTree, pagingMaskTree, auto, auto, resourceMethod });
        // auto data projection, manual metadata projection, and auto (default) paging projection
        data.add(new Object[] { collectionResult, metadata.data(), testListWithProjection, collectionMetadataWithProjection, dataMaskTree, metadataMaskTree, pagingMaskTree, auto, manual, resourceMethod });
    }
    return data.toArray(new Object[data.size()][]);
}
Also used : CollectionResult(com.linkedin.restli.server.CollectionResult) CollectionMetadata(com.linkedin.restli.common.CollectionMetadata) MaskTree(com.linkedin.data.transform.filter.request.MaskTree) LinkArray(com.linkedin.restli.common.LinkArray) ProjectionMode(com.linkedin.restli.server.ProjectionMode) Foo(com.linkedin.pegasus.generator.examples.Foo) ArrayList(java.util.ArrayList) ResourceMethod(com.linkedin.restli.common.ResourceMethod) DataMap(com.linkedin.data.DataMap) DataProvider(org.testng.annotations.DataProvider)

Example 29 with CollectionMetadata

use of com.linkedin.restli.common.CollectionMetadata in project rest.li by linkedin.

the class TestRestLiResponseData method collectionResponseEnvelopesProvider.

@DataProvider
public static Object[][] collectionResponseEnvelopesProvider() {
    RestLiResponseData<?> getAllResponseData = ResponseDataBuilderUtil.buildGetAllResponseData(HttpStatus.S_200_OK, Collections.emptyList(), new CollectionMetadata(), new EmptyRecord());
    RestLiResponseData<?> finderResponseData = ResponseDataBuilderUtil.buildFinderResponseData(HttpStatus.S_200_OK, Collections.emptyList(), new CollectionMetadata(), new EmptyRecord());
    return new Object[][] { { getAllResponseData }, { finderResponseData } };
}
Also used : EmptyRecord(com.linkedin.restli.common.EmptyRecord) CollectionMetadata(com.linkedin.restli.common.CollectionMetadata) DataProvider(org.testng.annotations.DataProvider)

Example 30 with CollectionMetadata

use of com.linkedin.restli.common.CollectionMetadata in project rest.li by linkedin.

the class TestRestLiResponseData method testCollectionResponseEnvelopeUpdates.

@Test(dataProvider = "collectionResponseEnvelopesProvider")
@SuppressWarnings("unchecked")
public void testCollectionResponseEnvelopeUpdates(RestLiResponseData<? extends CollectionResponseEnvelope> responseData) {
    CollectionResponseEnvelope responseEnvelope = responseData.getResponseEnvelope();
    Assert.assertFalse(responseData.getResponseEnvelope().isErrorResponse());
    Assert.assertEquals(responseEnvelope.getCollectionResponse(), Collections.emptyList());
    Assert.assertEquals(responseEnvelope.getCollectionResponsePaging(), new CollectionMetadata());
    Assert.assertEquals(responseEnvelope.getCollectionResponseCustomMetadata(), new EmptyRecord());
    // Swap to exception
    responseData.getResponseEnvelope().setExceptionInternal(exception500);
    Assert.assertNull(responseEnvelope.getCollectionResponse());
    Assert.assertNull(responseEnvelope.getCollectionResponseCustomMetadata());
    Assert.assertNull(responseEnvelope.getCollectionResponsePaging());
    Assert.assertEquals(responseData.getResponseEnvelope().getException(), exception500);
    Assert.assertEquals(responseData.getResponseEnvelope().getStatus(), HttpStatus.S_500_INTERNAL_SERVER_ERROR);
    // Swap back
    responseEnvelope.setCollectionResponse(new ArrayList<>(), new CollectionMetadata(), new EmptyRecord(), HttpStatus.S_200_OK);
    Assert.assertFalse(responseData.getResponseEnvelope().isErrorResponse());
    Assert.assertEquals(responseEnvelope.getCollectionResponse(), Collections.emptyList());
    Assert.assertEquals(responseEnvelope.getCollectionResponsePaging(), new CollectionMetadata());
    Assert.assertEquals(responseEnvelope.getCollectionResponseCustomMetadata(), new EmptyRecord());
    // Check mutability when available
    List<EmptyRecord> temp = (List<EmptyRecord>) responseEnvelope.getCollectionResponse();
    temp.add(new EmptyRecord());
    Assert.assertEquals(responseEnvelope.getCollectionResponse().size(), 1);
}
Also used : EmptyRecord(com.linkedin.restli.common.EmptyRecord) CollectionMetadata(com.linkedin.restli.common.CollectionMetadata) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.testng.annotations.Test)

Aggregations

CollectionMetadata (com.linkedin.restli.common.CollectionMetadata)30 Test (org.testng.annotations.Test)18 ArrayList (java.util.ArrayList)13 DataMap (com.linkedin.data.DataMap)9 CollectionResponse (com.linkedin.restli.common.CollectionResponse)8 Greeting (com.linkedin.restli.examples.greetings.api.Greeting)8 RecordTemplate (com.linkedin.data.template.RecordTemplate)7 HashMap (java.util.HashMap)7 EmptyRecord (com.linkedin.restli.common.EmptyRecord)6 LinkArray (com.linkedin.restli.common.LinkArray)6 RestLiServiceException (com.linkedin.restli.server.RestLiServiceException)6 List (java.util.List)6 DataProvider (org.testng.annotations.DataProvider)5 Link (com.linkedin.restli.common.Link)4 AnyRecord (com.linkedin.restli.internal.server.methods.AnyRecord)4 HighLevelRecordWithDefault (com.linkedin.restli.examples.defaults.api.HighLevelRecordWithDefault)3 LowLevelRecordWithDefault (com.linkedin.restli.examples.defaults.api.LowLevelRecordWithDefault)3 ResponseType (com.linkedin.restli.internal.server.ResponseType)3 URIDetails (com.linkedin.restli.internal.testutils.URIDetails)3 DataList (com.linkedin.data.DataList)2