Search in sources :

Example 41 with CollectionResponse

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

the class MockCollectionResponseFactory method create.

/**
   * Creates a {@link com.linkedin.restli.common.CollectionResponse}
   *
   * @param entryClass the class of the objects being stored in the {@link com.linkedin.restli.common.CollectionResponse}
   * @param recordTemplates the objects that will be stored in the {@link com.linkedin.restli.common.CollectionResponse}
   * @param <T> the class of the objects being stored in the {@link com.linkedin.restli.common.CollectionResponse}
   * @return a {@link com.linkedin.restli.common.CollectionResponse} with the above properties
   */
public static <T extends RecordTemplate> CollectionResponse<T> create(Class<T> entryClass, Collection<T> recordTemplates) {
    List<DataMap> dataMapsOfRecordTemplates = new ArrayList<DataMap>();
    for (T recordTemplate : recordTemplates) {
        dataMapsOfRecordTemplates.add(recordTemplate.data());
    }
    DataMap dataMapCollection = new DataMap();
    dataMapCollection.put(CollectionResponse.ELEMENTS, new DataList(dataMapsOfRecordTemplates));
    return new CollectionResponse<T>(dataMapCollection, entryClass);
}
Also used : DataList(com.linkedin.data.DataList) CollectionResponse(com.linkedin.restli.common.CollectionResponse) ArrayList(java.util.ArrayList) DataMap(com.linkedin.data.DataMap)

Example 42 with CollectionResponse

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

the class TestCompressionServer method testSearchWithTones.

@Test(dataProvider = com.linkedin.restli.internal.common.TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "clientsCompressedResponsesBuilderDataProvider")
public void testSearchWithTones(RestClient client, String operationsForCompression, RootBuilderWrapper<Long, Greeting> builders, ProtocolVersion protocolVersion) throws RemoteInvocationException {
    Request<CollectionResponse<Greeting>> req = builders.findBy("SearchWithTones").setQueryParam("tones", Arrays.asList(Tone.SINCERE, Tone.INSULTING)).build();
    ResponseFuture<CollectionResponse<Greeting>> future = client.sendRequest(req);
    Response<CollectionResponse<Greeting>> response = future.getResponse();
    checkHeaderForCompression(response, operationsForCompression, "finder:" + req.getMethodName());
    List<Greeting> greetings = response.getEntity().getElements();
    for (Greeting greeting : greetings) {
        Assert.assertTrue(greeting.hasTone());
        Tone tone = greeting.getTone();
        Assert.assertTrue(Tone.SINCERE.equals(tone) || Tone.INSULTING.equals(tone));
    }
}
Also used : Greeting(com.linkedin.restli.examples.greetings.api.Greeting) Tone(com.linkedin.restli.examples.greetings.api.Tone) CollectionResponse(com.linkedin.restli.common.CollectionResponse) Test(org.testng.annotations.Test)

Example 43 with CollectionResponse

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

the class TestCompressionServer method testSearchWithoutDecompression.

@Test(dataProvider = com.linkedin.restli.internal.common.TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "clientsCompressedResponsesBuilderDataProvider")
public void testSearchWithoutDecompression(RestClient client, String operationsForCompression, RootBuilderWrapper<Long, Greeting> builders, ProtocolVersion protocolVersion) throws RemoteInvocationException {
    Request<CollectionResponse<Greeting>> findRequest = builders.findBy("Search").setQueryParam("tone", Tone.FRIENDLY).build();
    RequestContext requestContext = new RequestContext();
    RequestContextUtil.turnOffResponseDecompression(requestContext);
    Map<String, Set<String>> methodsAndFamilies = getCompressionMethods(operationsForCompression);
    Set<String> methods = methodsAndFamilies.get("methods");
    Set<String> families = methodsAndFamilies.get("families");
    if (shouldCompress(families, methods, "finder:search")) {
        // The server sends a compressed response, but the client does not decompress it so it cannot read the response.
        try {
            client.sendRequest(findRequest, requestContext).getResponse();
            Assert.fail("Expected RemoteInvocationException, but getResponse() succeeded.");
        } catch (RemoteInvocationException e) {
            Assert.assertEquals(e.getCause().getMessage(), "Could not decode REST response");
        }
    } else {
        // The server doesn't compress the response in the first place, so the client can read the response.
        client.sendRequest(findRequest, requestContext).getResponse();
    }
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) CollectionResponse(com.linkedin.restli.common.CollectionResponse) RequestContext(com.linkedin.r2.message.RequestContext) RemoteInvocationException(com.linkedin.r2.RemoteInvocationException) Test(org.testng.annotations.Test)

Example 44 with CollectionResponse

use of com.linkedin.restli.common.CollectionResponse 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 45 with CollectionResponse

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

the class TestCompressionServer method testSearchFacets.

@Test(dataProvider = com.linkedin.restli.internal.common.TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "clientsCompressedResponsesBuilderDataProvider")
public void testSearchFacets(RestClient client, String operationsForCompression, RootBuilderWrapper<Long, Greeting> builders, ProtocolVersion protocolVersion) throws RemoteInvocationException {
    Request<CollectionResponse<Greeting>> req = builders.findBy("SearchWithFacets").setQueryParam("tone", Tone.SINCERE).build();
    ResponseFuture<CollectionResponse<Greeting>> future = client.sendRequest(req);
    Response<CollectionResponse<Greeting>> response = future.getResponse();
    checkHeaderForCompression(response, operationsForCompression, "finder:" + req.getMethodName());
    SearchMetadata metadata = new SearchMetadata(response.getEntity().getMetadataRaw());
    Assert.assertTrue(metadata.getFacets().size() > 0);
// "randomly" generated data is guaranteed to have positive number of each tone
}
Also used : CollectionResponse(com.linkedin.restli.common.CollectionResponse) SearchMetadata(com.linkedin.restli.examples.greetings.api.SearchMetadata) Test(org.testng.annotations.Test)

Aggregations

CollectionResponse (com.linkedin.restli.common.CollectionResponse)85 Test (org.testng.annotations.Test)77 Greeting (com.linkedin.restli.examples.greetings.api.Greeting)58 CreateStatus (com.linkedin.restli.common.CreateStatus)11 ArrayList (java.util.ArrayList)7 DataMap (com.linkedin.data.DataMap)6 CollectionMetadata (com.linkedin.restli.common.CollectionMetadata)6 BatchKVResponse (com.linkedin.restli.client.response.BatchKVResponse)5 CreateIdStatus (com.linkedin.restli.common.CreateIdStatus)5 UpdateStatus (com.linkedin.restli.common.UpdateStatus)5 HttpStatus (com.linkedin.restli.common.HttpStatus)4 CustomLong (com.linkedin.restli.examples.custom.types.CustomLong)4 ValidationDemo (com.linkedin.restli.examples.greetings.api.ValidationDemo)4 RootBuilderWrapper (com.linkedin.restli.test.util.RootBuilderWrapper)4 HashMap (java.util.HashMap)4 RestResponse (com.linkedin.r2.message.rest.RestResponse)3 Link (com.linkedin.restli.common.Link)3 Message (com.linkedin.restli.examples.greetings.api.Message)3 AutoValidationWithProjectionBuilders (com.linkedin.restli.examples.greetings.client.AutoValidationWithProjectionBuilders)3 URIDetails (com.linkedin.restli.internal.testutils.URIDetails)3