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);
}
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));
}
}
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();
}
}
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);
}
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
}
Aggregations