Search in sources :

Example 16 with CollectionMetadata

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
    }
}
Also used : EmptyRecord(com.linkedin.restli.common.EmptyRecord) CollectionMetadata(com.linkedin.restli.common.CollectionMetadata) ResponseType(com.linkedin.restli.internal.server.ResponseType) Test(org.testng.annotations.Test)

Example 17 with CollectionMetadata

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);
}
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 18 with CollectionMetadata

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);
}
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 19 with CollectionMetadata

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 } };
}
Also used : CollectionMetadata(com.linkedin.restli.common.CollectionMetadata) LinkArray(com.linkedin.restli.common.LinkArray) ArrayList(java.util.ArrayList) LowLevelRecordWithDefault(com.linkedin.restli.examples.defaults.api.LowLevelRecordWithDefault) HighLevelRecordWithDefault(com.linkedin.restli.examples.defaults.api.HighLevelRecordWithDefault) DataProvider(org.testng.annotations.DataProvider)

Example 20 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 = 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)

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