use of com.linkedin.restli.common.CollectionMetadata in project rest.li by linkedin.
the class TestDataMapConverter method createTestDataMap.
private DataMap createTestDataMap() {
CollectionMetadata someRecord = new CollectionMetadata();
someRecord.setCount(1);
someRecord.setStart(0);
someRecord.setTotal(10);
LinkArray links = new LinkArray();
Link link = new Link();
link.setHref("prevUri");
link.setRel("prev");
link.setType("en");
links.add(link);
someRecord.setLinks(links);
return someRecord.data();
}
use of com.linkedin.restli.common.CollectionMetadata in project rest.li by linkedin.
the class TestGreetingsClient method testSearchWithPostFilter.
@Test(dataProvider = com.linkedin.restli.internal.common.TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "requestBuilderWithResourceNameDataProvider")
public void testSearchWithPostFilter(RootBuilderWrapper<Long, Greeting> builders, String resourceName, ProtocolVersion protocolVersion) throws RemoteInvocationException {
Request<CollectionResponse<Greeting>> findRequest = builders.findBy("SearchWithPostFilter").paginate(0, 5).build();
CollectionResponse<Greeting> entity = getClient().sendRequest(findRequest).getResponse().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
//"/" + resourceName + "?count=5&start=5&q=searchWithPostFilter";
final Map<String, String> queryParamsMap = new HashMap<String, String>();
queryParamsMap.put("count", "5");
queryParamsMap.put("start", "5");
queryParamsMap.put("q", "searchWithPostFilter");
final URIDetails uriDetails = new URIDetails(protocolVersion, "/" + resourceName, null, queryParamsMap, null);
URIDetails.testUriGeneration(next.getHref(), uriDetails);
}
use of com.linkedin.restli.common.CollectionMetadata in project rest.li by linkedin.
the class TestGreetingsClient method TestPagination.
@Test(dataProvider = com.linkedin.restli.internal.common.TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "requestBuilderDataProvider")
public void TestPagination(RootBuilderWrapper<Long, Greeting> builders) throws RemoteInvocationException {
Request<CollectionResponse<Greeting>> findRequest = builders.findBy("SearchWithPostFilter").paginateStart(1).build();
CollectionResponse<Greeting> entity = getClient().sendRequest(findRequest).getResponse().getEntity();
CollectionMetadata paging = entity.getPaging();
Assert.assertEquals(paging.getStart().intValue(), 1);
Assert.assertEquals(paging.getCount().intValue(), 10);
// expected to be 9 instead of 10 because of post filter
Assert.assertEquals(entity.getElements().size(), 9);
findRequest = builders.findBy("SearchWithPostFilter").paginateCount(5).build();
entity = getClient().sendRequest(findRequest).getResponse().getEntity();
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);
}
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<Foo, Foo>(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;
return new Object[][] { // auto projection for data and metadata with null projection masks
{ generatedList, null, generatedList, collectionMetadata1, null, null, null, auto, auto }, { collectionResult, metadata.data(), collectionResult.getElements(), collectionMetadata2, null, null, null, auto, auto }, // manual projection for data and metadata with null projection masks
{ generatedList, null, generatedList, collectionMetadata1, null, null, null, manual, manual }, { collectionResult, metadata.data(), collectionResult.getElements(), collectionMetadata2, null, null, null, manual, manual }, // manual projection for data and metadata with non-null projection masks
{ generatedList, null, generatedList, collectionMetadataWithProjection, dataMaskTree, metadataMaskTree, pagingMaskTree, manual, manual }, { collectionResult, metadata.data(), collectionResult.getElements(), collectionMetadataWithProjection, dataMaskTree, metadataMaskTree, pagingMaskTree, manual, manual }, // auto projection for data with non-null data and paging projection masks
{ generatedList, null, testListWithProjection, collectionMetadataWithProjection, dataMaskTree, null, pagingMaskTree, auto, auto }, // auto projection for data and metadata with non-null projection masks
{ collectionResult, projectedMetadata.data(), testListWithProjection, collectionMetadataWithProjection, dataMaskTree, metadataMaskTree, pagingMaskTree, auto, auto }, // auto data projection, manual metadata projection, and auto (default) paging projection
{ collectionResult, metadata.data(), testListWithProjection, collectionMetadataWithProjection, dataMaskTree, metadataMaskTree, pagingMaskTree, auto, manual } };
}
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) {
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();
}
}
Aggregations