use of com.linkedin.restli.internal.server.ResourceContextImpl in project rest.li by linkedin.
the class TestRestLiRouting method testStreamingResourceContext.
// This test verifies that the router can create the correct resource context based on attachments being
// present in the request or an accept type indicating a desire to receive response attachments.
@Test(dataProvider = TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "routingDetailsSimpleStreaming")
public void testStreamingResourceContext(ProtocolVersion version, String uri, String acceptHeader, RestLiAttachmentReader requestAttachments) throws Exception {
Map<String, ResourceModel> pathRootResourceMap = buildResourceModels(TrendingResource.class);
_router = new RestLiRouter(pathRootResourceMap, new RestLiConfig());
final RestRequestBuilder requestBuilder = new RestRequestBuilder(new URI(uri)).setMethod("GET").setHeader(RestConstants.HEADER_RESTLI_PROTOCOL_VERSION, version.toString());
if (acceptHeader != null) {
requestBuilder.setHeader(RestConstants.HEADER_ACCEPT, acceptHeader);
}
final RestRequest request = requestBuilder.build();
ServerResourceContext context = new ResourceContextImpl(new PathKeysImpl(), request, new RequestContext());
context.setRequestAttachmentReader(requestAttachments);
ResourceMethodDescriptor method = _router.process(context);
assertNotNull(method);
if (requestAttachments != null) {
Assert.assertEquals(context.getRequestAttachmentReader(), requestAttachments);
} else {
Assert.assertNull(context.getRequestAttachmentReader());
}
if (acceptHeader != null && acceptHeader.contains("multipart/related")) {
Assert.assertTrue(context.responseAttachmentsSupported());
} else {
Assert.assertFalse(context.responseAttachmentsSupported());
}
}
use of com.linkedin.restli.internal.server.ResourceContextImpl in project rest.li by linkedin.
the class TestRestLiRouting method testRoutingDetailsAssociationGet.
@Test(dataProvider = TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "routingDetailsAssociationEntity")
public void testRoutingDetailsAssociationGet(ProtocolVersion version, String uri) throws Exception {
Map<String, ResourceModel> pathRootResourceMap = buildResourceModels(FollowsAssociativeResource.class);
_router = new RestLiRouter(pathRootResourceMap, new RestLiConfig());
RestRequest request = createRequest(uri, "GET", version);
ServerResourceContext context = new ResourceContextImpl(new PathKeysImpl(), request, new RequestContext());
ResourceMethodDescriptor resourceMethodDescriptor = _router.process(context);
assertNotNull(resourceMethodDescriptor);
assertEquals(resourceMethodDescriptor.getType(), ResourceMethod.GET);
assertNull(resourceMethodDescriptor.getActionName());
assertNull(resourceMethodDescriptor.getFinderName());
assertEquals(resourceMethodDescriptor.getMethod().getDeclaringClass(), FollowsAssociativeResource.class);
assertEquals(resourceMethodDescriptor.getMethod().getName(), "get");
assertEquals(resourceMethodDescriptor.getMethod().getParameterTypes(), new Class<?>[] { CompoundKey.class });
assertEquals(resourceMethodDescriptor.getResourceModel().getName(), "follows");
PathKeys keys = context.getPathKeys();
assertEquals(keys.getAsLong("followerID"), Long.valueOf(1L));
assertEquals(keys.getAsLong("followeeID"), Long.valueOf(2L));
}
use of com.linkedin.restli.internal.server.ResourceContextImpl 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.findFinderMethod("search");
RestLiResponse response;
// #1 check datamap/entity structure
ServerResourceContext context = new ResourceContextImpl();
RestUtils.validateRequestHeadersAndUpdateResourceContext(acceptTypeData.acceptHeaders, Collections.emptySet(), context);
RoutingResult routingResult = new RoutingResult(context, methodDescriptor);
response = buildPartialRestResponse(buildRequest(acceptTypeData.acceptHeaders, protocolVersion), routingResult, buildStatusList(3));
checkResponse(response, 200, 1, true, errorResponseHeaderName);
String baseUri = "/test?someParam=foo";
// #1.1 using CollectionResult
response = invokeResponseHandler(baseUri + "&start=0&count=5", methodDescriptor, new BasicCollectionResult<>(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<>(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<>(buildStatusList(5), 10, metadata), acceptTypeData.acceptHeaders, protocolVersion);
checkCollectionResponse(response, 5, 0, 5, 1, 10, null, null, acceptTypeData);
DataMap dataMap = response.getDataMap();
CollectionResponse<Status> collectionResponse = new CollectionResponse<>(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<>();
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<>();
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<>();
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<>();
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<>(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<>();
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<>();
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<>(buildStatusList(4), 14), acceptTypeData.acceptHeaders, protocolVersion);
// "/test?count=5&start=5&someParam=foo"
final Map<String, String> queryParamsMap7prev = new HashMap<>();
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.internal.server.ResourceContextImpl in project rest.li by linkedin.
the class TestRestLiResponseHandler method testApplicationSpecifiedHeaders.
@Test(dataProvider = TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "basicData")
public void testApplicationSpecifiedHeaders(AcceptTypeData acceptTypeData, ProtocolVersion protocolVersion, String errorResponseHeaderName) throws Exception {
String testHeaderName = "X-LI-TEST-HEADER";
String testHeaderValue = "test";
ResourceModel resourceModel = buildResourceModel(StatusCollectionResource.class);
ResourceMethodDescriptor methodDescriptor = resourceModel.findFinderMethod("search");
ResourceContextImpl context = new ResourceContextImpl();
context.setResponseHeader(testHeaderName, testHeaderValue);
RestUtils.validateRequestHeadersAndUpdateResourceContext(acceptTypeData.acceptHeaders, Collections.emptySet(), context);
RoutingResult routingResult = new RoutingResult(context, methodDescriptor);
RestLiResponse response = buildPartialRestResponse(buildRequest(acceptTypeData.acceptHeaders, protocolVersion), routingResult, buildStatusList(3));
Assert.assertEquals(response.getHeader(testHeaderName), testHeaderValue);
}
use of com.linkedin.restli.internal.server.ResourceContextImpl in project rest.li by linkedin.
the class TestRestLiResponseHandler method testSetResponseCookies.
@Test(dataProvider = TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "basicData")
public void testSetResponseCookies(AcceptTypeData acceptTypeData, ProtocolVersion protocolVersion, String errorResponseHeaderName) throws Exception {
String testHeaderName = "XXX";
String testHeaderValue = "head";
ResourceModel resourceModel = buildResourceModel(StatusCollectionResource.class);
ResourceMethodDescriptor methodDescriptor = resourceModel.findFinderMethod("search");
ResourceContextImpl context = new ResourceContextImpl();
context.setResponseHeader(testHeaderName, testHeaderValue);
context.addResponseCookie(new HttpCookie("cook1", "value1"));
context.addResponseCookie(new HttpCookie("cook2", "value2"));
RestUtils.validateRequestHeadersAndUpdateResourceContext(acceptTypeData.acceptHeaders, Collections.emptySet(), context);
RoutingResult routingResult = new RoutingResult(context, methodDescriptor);
// this is a valid response
RestLiResponse response = buildPartialRestResponse(buildRequest(acceptTypeData.acceptHeaders, protocolVersion), routingResult, buildStatusList(1));
List<HttpCookie> cookies = Arrays.asList(new HttpCookie("cook1", "value1"), new HttpCookie("cook2", "value2"));
Assert.assertEquals(response.getCookies(), cookies);
response = buildPartialRestResponse(buildRequest(acceptTypeData.acceptHeaders, protocolVersion), routingResult, // this is an invalid response
new RestLiServiceException(HttpStatus.S_404_NOT_FOUND));
// but the cookie should still be valid
Assert.assertEquals(response.getCookies(), cookies);
}
Aggregations