Search in sources :

Example 6 with CollectionMetadata

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

the class TestPagingProjection method testMetadataAutomaticPagingAutomaticPartialNullIncorrectSingleMetaPagingTotal.

/**
   * Calls the resource method metadataAutomaticPagingAutomaticPartialNullIncorrect with a single metadata field and a single paging
   * field for 'total'.
   */
@Test(dataProvider = com.linkedin.restli.internal.common.TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "requestBuilderDataProvider")
public void testMetadataAutomaticPagingAutomaticPartialNullIncorrectSingleMetaPagingTotal(RootBuilderWrapper<Long, Greeting> builders) throws RemoteInvocationException {
    final Request<CollectionResponse<Greeting>> request = builders.findBy("metadataAutomaticPagingAutomaticPartialNullIncorrect").metadataFields(Greeting.fields().message()).pagingFields(CollectionMetadata.fields().total()).build();
    final Response<CollectionResponse<Greeting>> response = getClient().sendRequest(request).getResponse();
    final Greeting metadataGreeting = new Greeting(response.getEntity().getMetadataRaw());
    assertGreeting(metadataGreeting, false, /*hasTone*/
    true, /*hasMessage*/
    false);
    Assert.assertTrue(response.getEntity().hasPaging(), "We must have paging!");
    //Resource method on the server specified null so we shouldn't get anything back
    assertPaging(response.getEntity().getPaging(), false, /*hasTotal*/
    false, /*hasStart*/
    false, /*hasCount*/
    false);
    CollectionMetadata paging = response.getEntity().getPaging();
    final int pagingTotal = paging.getTotal();
    Assert.assertEquals(pagingTotal, 0, "We should still get the default of 0");
}
Also used : Greeting(com.linkedin.restli.examples.greetings.api.Greeting) CollectionMetadata(com.linkedin.restli.common.CollectionMetadata) CollectionResponse(com.linkedin.restli.common.CollectionResponse) Test(org.testng.annotations.Test)

Example 7 with CollectionMetadata

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

the class TestMockCollectionResponseFactory method testCreate.

@Test
public void testCreate() {
    Greeting g1 = new Greeting().setId(1L).setMessage("g1");
    Greeting g2 = new Greeting().setId(2L).setMessage("g2");
    List<Greeting> greetings = Arrays.asList(g1, g2);
    CollectionMetadata metadata = new CollectionMetadata().setCount(2).setStart(0).setTotal(2);
    CollectionResponse<Greeting> collectionResponse = MockCollectionResponseFactory.create(Greeting.class, greetings, metadata);
    Assert.assertEquals(collectionResponse.getElements(), greetings);
    Assert.assertEquals(collectionResponse.getPaging(), metadata);
}
Also used : Greeting(com.linkedin.restli.examples.greetings.api.Greeting) CollectionMetadata(com.linkedin.restli.common.CollectionMetadata) Test(org.testng.annotations.Test)

Example 8 with CollectionMetadata

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 = RestConstants.HEADER_VALUE_APPLICATION_JSON;
    if (resourceContext.getRawRequest() != null) {
        bestEncoding = pickBestEncoding(resourceContext.getRequestHeaders().get(RestConstants.HEADER_ACCEPT));
    }
    //links use count as the step interval, so links don't make sense with count==0
    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);
        }
        metadata.setLinks(links);
    }
    return metadata;
}
Also used : CollectionMetadata(com.linkedin.restli.common.CollectionMetadata) PagingContext(com.linkedin.restli.server.PagingContext) LinkArray(com.linkedin.restli.common.LinkArray) Parameter(com.linkedin.restli.internal.server.model.Parameter) Link(com.linkedin.restli.common.Link)

Example 9 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<String, String>();
    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 10 with CollectionMetadata

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

the class TestRestLiResponseHandler method testCollections.

@Test(dataProvider = TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "basicData")
public void testCollections(AcceptTypeData acceptTypeData, ProtocolVersion protocolVersion, String errorResponseHeaderName) throws Exception {
    ResourceModel resourceModel = buildResourceModel(StatusCollectionResource.class);
    ResourceMethodDescriptor methodDescriptor = resourceModel.findNamedMethod("search");
    RestResponse response;
    // #1 check datamap/entity structure
    ServerResourceContext context = new ResourceContextImpl();
    RestUtils.validateRequestHeadersAndUpdateResourceContext(acceptTypeData.acceptHeaders, context);
    response = _responseHandler.buildResponse(buildRequest(acceptTypeData.acceptHeaders, protocolVersion), new RoutingResult(context, methodDescriptor), buildStatusList(3));
    checkResponse(response, 200, 2, acceptTypeData.responseContentType, CollectionResponse.class.getName(), Status.class.getName(), true, errorResponseHeaderName);
    String baseUri = "/test?someParam=foo";
    // #1.1 using CollectionResult
    response = invokeResponseHandler(baseUri + "&start=0&count=5", methodDescriptor, new BasicCollectionResult<Status>(buildStatusList(5)), acceptTypeData.acceptHeaders, protocolVersion);
    checkCollectionResponse(response, 5, 0, 5, 1, null, null, null, acceptTypeData);
    // #1.1 using CollectionResult (with total)
    response = invokeResponseHandler(baseUri + "&start=0&count=5", methodDescriptor, new BasicCollectionResult<Status>(buildStatusList(5), 10), acceptTypeData.acceptHeaders, protocolVersion);
    checkCollectionResponse(response, 5, 0, 5, 1, 10, null, null, acceptTypeData);
    // using CollectionResult with metadata RecordTemplate
    CollectionMetadata metadata = new CollectionMetadata();
    metadata.setCount(42);
    response = invokeResponseHandler(baseUri + "&start=0&count=5", methodDescriptor, new CollectionResult<Status, CollectionMetadata>(buildStatusList(5), 10, metadata), acceptTypeData.acceptHeaders, protocolVersion);
    checkCollectionResponse(response, 5, 0, 5, 1, 10, null, null, acceptTypeData);
    DataMap dataMap = acceptTypeData.dataCodec.readMap(response.getEntity().asInputStream());
    CollectionResponse<Status> collectionResponse = new CollectionResponse<Status>(dataMap, Status.class);
    assertEquals(new CollectionMetadata(collectionResponse.getMetadataRaw()), metadata);
    // #2 pagination: first page, no next
    response = invokeResponseHandler(baseUri + "&start=0&count=5", methodDescriptor, buildStatusList(3), acceptTypeData.acceptHeaders, protocolVersion);
    checkCollectionResponse(response, 3, 0, 5, 0, null, null, null, acceptTypeData);
    // #3 pagination: first page, has next (boundary case)
    response = invokeResponseHandler(baseUri + "&start=0&count=5", methodDescriptor, buildStatusList(5), acceptTypeData.acceptHeaders, protocolVersion);
    //"/test?count=5&start=5&someParam=foo"
    final Map<String, String> queryParamsMap3next = new HashMap<String, String>();
    queryParamsMap3next.put("count", "5");
    queryParamsMap3next.put("start", "5");
    queryParamsMap3next.put("someParam", "foo");
    final URIDetails expectedURIDetails3next = new URIDetails(protocolVersion, "/test", null, queryParamsMap3next, null);
    checkCollectionResponse(response, 5, 0, 5, 1, null, null, expectedURIDetails3next, acceptTypeData);
    // #4 pagination: second page, has prev/ext
    response = invokeResponseHandler(baseUri + "&start=5&count=5", methodDescriptor, buildStatusList(5), acceptTypeData.acceptHeaders, protocolVersion);
    //"/test?count=5&start=0&someParam=foo", "/test?count=5&start=10&someParam=foo",
    final Map<String, String> queryParamsMap4prev = new HashMap<String, String>();
    queryParamsMap4prev.put("count", "5");
    queryParamsMap4prev.put("start", "0");
    queryParamsMap4prev.put("someParam", "foo");
    final URIDetails expectedURIDetails4prev = new URIDetails(protocolVersion, "/test", null, queryParamsMap4prev, null);
    final Map<String, String> queryParamsMap4next = new HashMap<String, String>();
    queryParamsMap4next.put("count", "5");
    queryParamsMap4next.put("start", "10");
    queryParamsMap4next.put("someParam", "foo");
    final URIDetails expectedURIDetails4next = new URIDetails(protocolVersion, "/test", null, queryParamsMap4next, null);
    checkCollectionResponse(response, 5, 5, 5, 2, null, expectedURIDetails4prev, expectedURIDetails4next, acceptTypeData);
    // #5 pagination:last page, has prev
    response = invokeResponseHandler(baseUri + "&start=10&count=5", methodDescriptor, buildStatusList(4), acceptTypeData.acceptHeaders, protocolVersion);
    //"/test?count=5&start=5&someParam=foo"
    final Map<String, String> queryParamsMap5prev = new HashMap<String, String>();
    queryParamsMap5prev.put("count", "5");
    queryParamsMap5prev.put("start", "5");
    queryParamsMap5prev.put("someParam", "foo");
    final URIDetails expectedURIDetails5prev = new URIDetails(protocolVersion, "/test", null, queryParamsMap5prev, null);
    checkCollectionResponse(response, 4, 10, 5, 1, null, expectedURIDetails5prev, null, acceptTypeData);
    response = invokeResponseHandler(baseUri + "&start=10&count=5", methodDescriptor, new BasicCollectionResult<Status>(buildStatusList(4), 15), acceptTypeData.acceptHeaders, protocolVersion);
    //"/test?count=5&start=5&someParam=foo", "/test?count=5&start=14&someParam=foo"
    final Map<String, String> queryParamsMap6prev = new HashMap<String, String>();
    queryParamsMap6prev.put("count", "5");
    queryParamsMap6prev.put("start", "5");
    queryParamsMap6prev.put("someParam", "foo");
    final URIDetails expectedURIDetails6prev = new URIDetails(protocolVersion, "/test", null, queryParamsMap6prev, null);
    final Map<String, String> queryParamsMap6next = new HashMap<String, String>();
    queryParamsMap6next.put("count", "5");
    queryParamsMap6next.put("start", "14");
    queryParamsMap6next.put("someParam", "foo");
    final URIDetails expectedURIDetails6next = new URIDetails(protocolVersion, "/test", null, queryParamsMap6next, null);
    checkCollectionResponse(response, 4, 10, 5, 2, 15, expectedURIDetails6prev, expectedURIDetails6next, acceptTypeData);
    response = invokeResponseHandler(baseUri + "&start=10&count=5", methodDescriptor, new BasicCollectionResult<Status>(buildStatusList(4), 14), acceptTypeData.acceptHeaders, protocolVersion);
    //"/test?count=5&start=5&someParam=foo"
    final Map<String, String> queryParamsMap7prev = new HashMap<String, String>();
    queryParamsMap7prev.put("count", "5");
    queryParamsMap7prev.put("start", "5");
    queryParamsMap7prev.put("someParam", "foo");
    final URIDetails expectedURIDetails7prev = new URIDetails(protocolVersion, "/test", null, queryParamsMap7prev, null);
    checkCollectionResponse(response, 4, 10, 5, 1, 14, expectedURIDetails7prev, null, acceptTypeData);
}
Also used : UpdateStatus(com.linkedin.restli.common.UpdateStatus) CreateStatus(com.linkedin.restli.common.CreateStatus) Status(com.linkedin.restli.server.twitter.TwitterTestDataModels.Status) HttpStatus(com.linkedin.restli.common.HttpStatus) CollectionMetadata(com.linkedin.restli.common.CollectionMetadata) URIDetails(com.linkedin.restli.internal.testutils.URIDetails) HashMap(java.util.HashMap) RestResponse(com.linkedin.r2.message.rest.RestResponse) PartialRestResponse(com.linkedin.restli.internal.server.response.PartialRestResponse) CollectionResponse(com.linkedin.restli.common.CollectionResponse) ResourceMethodDescriptor(com.linkedin.restli.internal.server.model.ResourceMethodDescriptor) ByteString(com.linkedin.data.ByteString) DataMap(com.linkedin.data.DataMap) RoutingResult(com.linkedin.restli.internal.server.RoutingResult) CollectionResult(com.linkedin.restli.server.CollectionResult) BasicCollectionResult(com.linkedin.restli.server.BasicCollectionResult) ServerResourceContext(com.linkedin.restli.internal.server.ServerResourceContext) BasicCollectionResult(com.linkedin.restli.server.BasicCollectionResult) ResourceModel(com.linkedin.restli.internal.server.model.ResourceModel) RestLiTestHelper.buildResourceModel(com.linkedin.restli.server.test.RestLiTestHelper.buildResourceModel) ResourceContextImpl(com.linkedin.restli.internal.server.ResourceContextImpl) Test(org.testng.annotations.Test)

Aggregations

CollectionMetadata (com.linkedin.restli.common.CollectionMetadata)15 Test (org.testng.annotations.Test)11 CollectionResponse (com.linkedin.restli.common.CollectionResponse)6 Greeting (com.linkedin.restli.examples.greetings.api.Greeting)5 HashMap (java.util.HashMap)5 DataMap (com.linkedin.data.DataMap)4 RecordTemplate (com.linkedin.data.template.RecordTemplate)4 Link (com.linkedin.restli.common.Link)4 EmptyRecord (com.linkedin.restli.common.EmptyRecord)3 LinkArray (com.linkedin.restli.common.LinkArray)3 URIDetails (com.linkedin.restli.internal.testutils.URIDetails)3 RestLiServiceException (com.linkedin.restli.server.RestLiServiceException)3 ArrayList (java.util.ArrayList)3 RestResponse (com.linkedin.r2.message.rest.RestResponse)2 ResponseType (com.linkedin.restli.internal.server.ResponseType)2 AnyRecord (com.linkedin.restli.internal.server.methods.AnyRecord)2 CollectionResult (com.linkedin.restli.server.CollectionResult)2 List (java.util.List)2 ByteString (com.linkedin.data.ByteString)1 PathSpec (com.linkedin.data.schema.PathSpec)1