Search in sources :

Example 1 with Link

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

the class TestGreetingsClientAcceptTypes method testFinder.

@Test(dataProvider = com.linkedin.restli.internal.common.TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "clientDataDataProvider")
public void testFinder(RestClient restClient, String expectedContentType, RootBuilderWrapper<Long, Greeting> builders) throws RemoteInvocationException {
    Request<CollectionResponse<Greeting>> request = builders.findBy("Search").setQueryParam("tone", Tone.SINCERE).paginate(1, 2).build();
    Response<CollectionResponse<Greeting>> response = restClient.sendRequest(request).getResponse();
    Assert.assertEquals(response.getHeader("Content-Type"), expectedContentType);
    CollectionResponse<Greeting> collectionResponse = response.getEntity();
    List<Greeting> greetings = collectionResponse.getElements();
    for (Greeting g : greetings) {
        Assert.assertEquals(g.getTone(), Tone.SINCERE);
    }
    collectionResponse.getPaging().getLinks();
    for (Link link : collectionResponse.getPaging().getLinks()) {
        Assert.assertEquals(link.getType(), expectedContentType);
    }
}
Also used : Greeting(com.linkedin.restli.examples.greetings.api.Greeting) CollectionResponse(com.linkedin.restli.common.CollectionResponse) Link(com.linkedin.restli.common.Link) Test(org.testng.annotations.Test)

Example 2 with Link

use of com.linkedin.restli.common.Link 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);
}
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 3 with Link

use of com.linkedin.restli.common.Link 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;
}
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 4 with Link

use of com.linkedin.restli.common.Link 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();
}
Also used : CollectionMetadata(com.linkedin.restli.common.CollectionMetadata) LinkArray(com.linkedin.restli.common.LinkArray) Link(com.linkedin.restli.common.Link)

Example 5 with Link

use of com.linkedin.restli.common.Link 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)

Aggregations

Link (com.linkedin.restli.common.Link)5 CollectionMetadata (com.linkedin.restli.common.CollectionMetadata)4 CollectionResponse (com.linkedin.restli.common.CollectionResponse)3 Greeting (com.linkedin.restli.examples.greetings.api.Greeting)3 Test (org.testng.annotations.Test)3 LinkArray (com.linkedin.restli.common.LinkArray)2 URIDetails (com.linkedin.restli.internal.testutils.URIDetails)2 HashMap (java.util.HashMap)2 Parameter (com.linkedin.restli.internal.server.model.Parameter)1 PagingContext (com.linkedin.restli.server.PagingContext)1