use of com.linkedin.restli.common.CollectionResponse in project rest.li by linkedin.
the class TestContexts method testFinder.
@Test(dataProvider = com.linkedin.restli.internal.common.TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "requestBuilderDataProvider")
public void testFinder(RootBuilderWrapper<Long, Greeting> builders) throws RemoteInvocationException {
Request<CollectionResponse<Greeting>> requestWithProjection = builders.findBy("Finder").fields(Greeting.fields().message(), Greeting.fields().tone()).setHeader("Expected-Header", HEADER_MESSAGE).build();
List<Greeting> projectionGreetings = getClient().sendRequest(requestWithProjection).getResponse().getEntity().getElements();
// test projection and keys
Greeting projectionGreeting = projectionGreetings.get(0);
Assert.assertEquals(projectionGreeting.getMessage(), PROJECTION_MESSAGE);
Assert.assertEquals(projectionGreeting.getTone(), NO_KEYS);
// test header
Greeting headerGreeting = projectionGreetings.get(1);
Assert.assertEquals(headerGreeting.getMessage(), HEADER_MESSAGE);
Request<CollectionResponse<Greeting>> requestNoProjection = builders.findBy("Finder").build();
List<Greeting> noProjectionGreetings = getClient().sendRequest(requestNoProjection).getResponse().getEntity().getElements();
// test projection and keys
Greeting noProjectionGreeting = noProjectionGreetings.get(0);
Assert.assertEquals(noProjectionGreeting.getMessage(), NO_PROJECTION_MESSAGE);
Assert.assertEquals(noProjectionGreeting.getTone(), NO_KEYS);
// test header
Greeting noHeaderGreeting = noProjectionGreetings.get(1);
Assert.assertEquals(noHeaderGreeting.getMessage(), NO_HEADER_MESSAGE);
}
use of com.linkedin.restli.common.CollectionResponse in project rest.li by linkedin.
the class TestComplexArrayResource method testFinder.
@Test(dataProvider = com.linkedin.restli.internal.common.TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "requestBuilderDataProvider")
public void testFinder(RootBuilderWrapper<ComplexResourceKey<ComplexArray, ComplexArray>, Greeting> builders) throws RemoteInvocationException {
LongArray singleton = new LongArray();
singleton.add(1L);
ComplexArray next = new ComplexArray().setArray(singleton);
ComplexArray array = new ComplexArray().setArray(singleton).setNext(next);
Request<CollectionResponse<Greeting>> request = builders.findBy("Finder").setQueryParam("array", array).build();
getClient().sendRequest(request).getResponse().getEntity();
}
use of com.linkedin.restli.common.CollectionResponse 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);
}
use of com.linkedin.restli.common.CollectionResponse in project rest.li by linkedin.
the class TestRestLiResponseHandler method testFieldProjection_collections_CollectionResult.
@Test(dataProvider = TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "basicData")
public void testFieldProjection_collections_CollectionResult(AcceptTypeData acceptTypeData, ProtocolVersion protocolVersion, String errorResponseHeaderName) throws Exception {
RestResponse response;
BasicCollectionResult<Status> statusCollection = buildStatusCollectionResult(10, "f1", "f2", "f3");
RestRequest request = buildRequest("/test?fields=f1,f2", acceptTypeData.acceptHeaders, protocolVersion);
response = _responseHandler.buildResponse(request, buildRoutingResultFinder(request, acceptTypeData.acceptHeaders), statusCollection);
checkResponse(response, 200, 2, acceptTypeData.responseContentType, CollectionResponse.class.getName(), null, true, errorResponseHeaderName);
DataMap dataMap = acceptTypeData.dataCodec.readMap(response.getEntity().asInputStream());
CollectionResponse<Status> collectionResponse = new CollectionResponse<Status>(dataMap, Status.class);
assertEquals(collectionResponse.getElements().size(), 10);
for (Status status : collectionResponse.getElements()) {
assertTrue(status.data().containsKey("f1"));
assertTrue(status.data().containsKey("f2"));
assertFalse(status.data().containsKey("f3"));
}
// ensure that output status objects were not modified by rest.li!
Status status1 = statusCollection.getElements().get(1);
assertNotNull(status1);
assertTrue(status1.data().containsKey("f3"));
}
use of com.linkedin.restli.common.CollectionResponse in project rest.li by linkedin.
the class TestRestLiResponseHandler method checkCollectionResponse.
private void checkCollectionResponse(RestResponse response, int numElements, int start, int count, int numLinks, Integer total, URIDetails prevLink, URIDetails nextLink, AcceptTypeData acceptTypeData) throws Exception {
DataMap dataMap = acceptTypeData.dataCodec.readMap(response.getEntity().asInputStream());
CollectionResponse<Status> collectionResponse = new CollectionResponse<Status>(dataMap, Status.class);
assertEquals(collectionResponse.getElements().size(), numElements);
assertEquals(collectionResponse.getPaging().getStart().intValue(), start);
assertEquals(collectionResponse.getPaging().getCount().intValue(), count);
assertEquals(collectionResponse.getPaging().getLinks().size(), numLinks);
if (total == null) {
assertFalse(collectionResponse.getPaging().hasTotal());
} else {
assertEquals(collectionResponse.getPaging().getTotal().intValue(), total.intValue());
}
if (prevLink != null) {
checkLink(collectionResponse.getPaging().getLinks().get(0), "prev", prevLink, acceptTypeData.responseContentType);
}
if (nextLink != null) {
int idx = prevLink != null ? 1 : 0;
checkLink(collectionResponse.getPaging().getLinks().get(idx), "next", nextLink, acceptTypeData.responseContentType);
}
}
Aggregations