use of com.linkedin.restli.common.CollectionMetadata in project rest.li by linkedin.
the class TestRestLiResponseData method testSetNullStatus.
@Test(dataProvider = "envelopeResourceMethodDataProvider")
@SuppressWarnings("deprecation")
public void testSetNullStatus(RestLiResponseData<?> responseData, ResourceMethod resourceMethod) {
try {
// If response type is dynamically determined, set HTTP status through resource method response envelope
if (ResponseTypeUtil.isDynamicallyDetermined(resourceMethod)) {
switch(resourceMethod) {
case PARTIAL_UPDATE:
responseData.getPartialUpdateResponseEnvelope().setStatus(null);
break;
default:
Assert.fail();
}
} else // Otherwise, set HTTP status through response type response envelope
{
ResponseType responseType = ResponseTypeUtil.fromMethodType(resourceMethod);
switch(responseType) {
case SINGLE_ENTITY:
responseData.getRecordResponseEnvelope().setRecord(new EmptyRecord(), null);
Assert.fail();
break;
case BATCH_ENTITIES:
responseData.getBatchResponseEnvelope().setBatchResponseMap(Collections.emptyMap(), null);
Assert.fail();
break;
case CREATE_COLLECTION:
responseData.getBatchCreateResponseEnvelope().setCreateResponse(Collections.emptyList(), null);
Assert.fail();
break;
case GET_COLLECTION:
responseData.getCollectionResponseEnvelope().setCollectionResponse(Collections.emptyList(), new CollectionMetadata(), new EmptyRecord(), null);
break;
case STATUS_ONLY:
responseData.getEmptyResponseEnvelope().setStatus(null);
break;
default:
Assert.fail();
}
}
Assert.fail();
} catch (AssertionError e) {
// expected
}
}
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 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<>();
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 TestFillInDefaultValue method testGetAllDataWithoutRequireDefault.
@DataProvider(name = "testGetAllDataWithoutRequireDefault")
private Object[][] testGetAllDataWithoutRequireDefault() throws CloneNotSupportedException {
final int count = 3;
List<HighLevelRecordWithDefault> elements = new ArrayList<>();
for (int i = 0; i < count; i++) {
elements.add(new HighLevelRecordWithDefault().setNoDefaultFieldA(i));
}
CollectionMetadata collectionMetadata = new CollectionMetadata().setLinks(new LinkArray()).setCount(10).setStart(0).setTotal(3);
LowLevelRecordWithDefault metadata = new LowLevelRecordWithDefault();
return new Object[][] { { elements, collectionMetadata, metadata } };
}
use of com.linkedin.restli.common.CollectionMetadata in project rest.li by linkedin.
the class RestUtils method buildMetadata.
public static CollectionMetadata buildMetadata(final URI requestUri, final ResourceContext resourceContext, final ResourceMethodDescriptor methodDescriptor, final List<?> resultElements, final PageIncrement pageIncrement, final Integer totalResults) {
CollectionMetadata metadata = new CollectionMetadata();
List<Parameter<?>> pagingContextParams = methodDescriptor.getParametersWithType(Parameter.ParamType.PAGING_CONTEXT_PARAM);
PagingContext defaultPagingContext = pagingContextParams.isEmpty() ? null : (PagingContext) pagingContextParams.get(0).getDefaultValue();
PagingContext pagingContext = getPagingContext(resourceContext, defaultPagingContext);
metadata.setCount(pagingContext.getCount());
metadata.setStart(pagingContext.getStart());
if (totalResults != null) {
metadata.setTotal(totalResults);
} else {
metadata.removeTotal();
}
LinkArray links = new LinkArray();
String bestEncoding = pickBestEncoding(resourceContext.getRequestHeaders().get(RestConstants.HEADER_ACCEPT), Collections.emptySet());
if (pagingContext.getCount() > 0) {
// prev link
if (pagingContext.getStart() > 0) {
int prevStart = Math.max(0, pagingContext.getStart() - pagingContext.getCount());
String prevUri = buildPaginatedUri(requestUri, prevStart, pagingContext.getCount());
Link prevLink = new Link();
prevLink.setRel("prev");
prevLink.setHref(prevUri);
prevLink.setType(bestEncoding);
links.add(prevLink);
}
// next link if there are more results, or we returned a full page
Integer nextStart = getNextPageStart(resultElements.size(), totalResults, pagingContext, pageIncrement);
if (nextStart != null) {
// R2 doesn't expose host/port => can't build absolute URI (this is ok, as
// relative URIs internally
String nextUri = buildPaginatedUri(requestUri, nextStart, pagingContext.getCount());
Link nextLink = new Link();
nextLink.setRel("next");
nextLink.setHref(nextUri);
nextLink.setType(bestEncoding);
links.add(nextLink);
}
}
// even when we are getting count = 0, we should honor that links
// is a required field in the CollectionMetadata record
metadata.setLinks(links);
return metadata;
}
Aggregations