use of com.linkedin.restli.internal.server.PathKeysImpl in project rest.li by linkedin.
the class TestFilterRequestContextInternalImpl method testFilterRequestContextAdapter.
@Test
public void testFilterRequestContextAdapter() throws Exception {
final String resourceName = "resourceName";
final String resourceNamespace = "resourceNamespace";
final ResourceMethod methodType = ResourceMethod.GET;
final DataMap customAnnotations = new DataMap();
customAnnotations.put("foo", "Bar");
final ProjectionMode projectionMode = ProjectionMode.AUTOMATIC;
final MaskTree maskTree = new MaskTree();
final MutablePathKeys pathKeys = new PathKeysImpl();
final Map<String, String> requestHeaders = new HashMap<String, String>();
requestHeaders.put("Key1", "Value1");
final URI requestUri = new URI("foo.bar.com");
final ProtocolVersion protoVersion = AllProtocolVersions.BASELINE_PROTOCOL_VERSION;
final DataMap queryParams = new DataMap();
queryParams.put("Param1", "Val1");
final Map<String, Object> localAttrs = new HashMap<>();
localAttrs.put("Key1", "Val1");
final RequestContext r2RequestContext = new RequestContext();
r2RequestContext.putLocalAttr("Key1", "Val1");
final String finderName = UUID.randomUUID().toString();
final String actionName = UUID.randomUUID().toString();
when(resourceModel.getName()).thenReturn(resourceName);
when(resourceModel.getNamespace()).thenReturn(resourceNamespace);
when(resourceMethod.getResourceModel()).thenReturn(resourceModel);
when(resourceMethod.getMethodType()).thenReturn(methodType);
when(resourceMethod.getFinderName()).thenReturn(finderName);
when(resourceMethod.getActionName()).thenReturn(actionName);
when(resourceMethod.getCustomAnnotationData()).thenReturn(customAnnotations);
when(resourceMethod.getMethod()).thenReturn(null);
when(context.getProjectionMode()).thenReturn(projectionMode);
when(context.getProjectionMask()).thenReturn(maskTree);
when(context.getPathKeys()).thenReturn(pathKeys);
when(context.getRequestHeaders()).thenReturn(requestHeaders);
when(context.getRequestURI()).thenReturn(requestUri);
when(context.getRestliProtocolVersion()).thenReturn(protoVersion);
when(context.getParameters()).thenReturn(queryParams);
when(context.getRawRequestContext()).thenReturn(r2RequestContext);
FilterRequestContextInternalImpl filterContext = new FilterRequestContextInternalImpl(context, resourceMethod);
Object spValue = new Object();
String spKey = UUID.randomUUID().toString();
filterContext.getFilterScratchpad().put(spKey, spValue);
assertEquals(filterContext.getFilterResourceModel().getResourceName(), resourceName);
assertEquals(filterContext.getFilterResourceModel().getResourceNamespace(), resourceNamespace);
assertEquals(filterContext.getMethodType(), methodType);
assertEquals(filterContext.getCustomAnnotations(), customAnnotations);
assertEquals(filterContext.getProjectionMode(), projectionMode);
assertEquals(filterContext.getProjectionMask(), maskTree);
assertEquals(filterContext.getPathKeys(), pathKeys);
assertEquals(filterContext.getRequestHeaders(), requestHeaders);
assertEquals(filterContext.getRequestURI(), requestUri);
assertEquals(filterContext.getRestliProtocolVersion(), protoVersion);
assertEquals(filterContext.getQueryParameters(), queryParams);
assertEquals(filterContext.getActionName(), actionName);
assertEquals(filterContext.getFinderName(), finderName);
assertEquals(filterContext.getRequestContextLocalAttrs(), localAttrs);
assertNull(filterContext.getMethod());
assertTrue(filterContext.getFilterScratchpad().get(spKey) == spValue);
filterContext.getRequestHeaders().put("header2", "value2");
assertEquals(requestHeaders.get("header2"), "value2");
verify(resourceModel).getName();
verify(resourceModel).getNamespace();
verify(resourceMethod).getMethodType();
verify(resourceMethod).getResourceModel();
verify(resourceMethod).getCustomAnnotationData();
verify(resourceMethod).getFinderName();
verify(resourceMethod).getActionName();
verify(resourceMethod).getMethod();
verify(context).getProjectionMode();
verify(context).getProjectionMask();
verify(context).getPathKeys();
verify(context, times(2)).getRequestHeaders();
verify(context).getRequestURI();
verify(context).getRestliProtocolVersion();
verify(context).getParameters();
verify(context).getRawRequestContext();
verify(resourceMethod).getFinderMetadataType();
verifyNoMoreInteractions(context, resourceMethod, resourceModel);
}
use of com.linkedin.restli.internal.server.PathKeysImpl in project rest.li by linkedin.
the class TestResourceContext method testResourceContextGetProjectionMask.
@Test(dataProvider = TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "projectionMask")
public void testResourceContextGetProjectionMask(ProtocolVersion version, String stringUri) throws Exception {
URI uri = URI.create(stringUri);
Map<String, String> headers = new HashMap<String, String>(1);
headers.put(RestConstants.HEADER_RESTLI_PROTOCOL_VERSION, version.toString());
ResourceContext context = new ResourceContextImpl(new PathKeysImpl(), new MockRequest(uri, headers), new RequestContext());
final MaskTree entityMask = context.getProjectionMask();
final DataMap expectedEntityMaskMap = new DataMap();
expectedEntityMaskMap.put("locale", 1);
expectedEntityMaskMap.put("state", 1);
Assert.assertEquals(entityMask.getDataMap(), expectedEntityMaskMap, "The generated DataMap for the MaskTree should be correct");
final MaskTree metadataMask = context.getMetadataProjectionMask();
final DataMap expectedMetadataMaskMap = new DataMap();
expectedMetadataMaskMap.put("region", 1);
expectedMetadataMaskMap.put("city", 1);
Assert.assertEquals(metadataMask.getDataMap(), expectedMetadataMaskMap, "The generated DataMap for the MaskTree should be correct");
final MaskTree pagingMask = context.getPagingProjectionMask();
final DataMap expectedPagingMaskMap = new DataMap();
expectedPagingMaskMap.put("start", 1);
expectedPagingMaskMap.put("links", 1);
Assert.assertEquals(pagingMask.getDataMap(), expectedPagingMaskMap, "The generated DataMap for the MaskTree should be correct");
}
use of com.linkedin.restli.internal.server.PathKeysImpl in project rest.li by linkedin.
the class TestCollectionArgumentBuilder method argumentData.
@DataProvider(name = "argumentData")
private Object[][] argumentData() {
List<Parameter<?>> getAllParams = new ArrayList<Parameter<?>>();
getAllParams.add(getPagingContextParam());
Map<String, String> getAllContextParams = new HashMap<String, String>();
getAllContextParams.put("start", "33");
getAllContextParams.put("count", "444");
Map<String, String> finderContextParams = new HashMap<String, String>();
finderContextParams.put("start", "33");
finderContextParams.put("count", "444");
finderContextParams.put("required", "777");
finderContextParams.put("optional", null);
Map<String, String> finderContextParamsWithOptionalString = new HashMap<String, String>(finderContextParams);
finderContextParamsWithOptionalString.put("optional", "someString");
List<Parameter<?>> finderWithAssocKeyParams = new ArrayList<Parameter<?>>();
finderWithAssocKeyParams.add(new Parameter<String>("string1", String.class, new StringDataSchema(), false, null, Parameter.ParamType.ASSOC_KEY_PARAM, true, new AnnotationSet(new Annotation[] {})));
return new Object[][] { { getAllParams, getAllContextParams, null, new Object[] { new PagingContext(33, 444) } }, { getFinderParams(), finderContextParams, null, new Object[] { new PagingContext(33, 444), new Integer(777), null } }, { getFinderParams(), finderContextParamsWithOptionalString, null, new Object[] { new PagingContext(33, 444), new Integer(777), "someString" } }, { finderWithAssocKeyParams, null, new PathKeysImpl().append("string1", "testString"), new Object[] { "testString" } } };
}
use of com.linkedin.restli.internal.server.PathKeysImpl in project rest.li by linkedin.
the class TestRestLiMethodInvocation method testInvokeWithUnsupportedAcceptMimeType.
@Test
public void testInvokeWithUnsupportedAcceptMimeType() throws Exception {
RestRequestBuilder builder = new RestRequestBuilder(new URI("")).addHeaderValue("Accept", "text/plain").setHeader(RestConstants.HEADER_RESTLI_PROTOCOL_VERSION, version.toString());
RestRequest request = builder.build();
final RestLiAttachmentReader attachmentReader = new RestLiAttachmentReader(null);
final CountDownLatch latch = new CountDownLatch(1);
RestLiResponseHandler restLiResponseHandler = new RestLiResponseHandler.Builder().build();
RequestExecutionCallback<RestResponse> executionCallback = new RequestExecutionCallback<RestResponse>() {
@Override
public void onError(Throwable e, RequestExecutionReport executionReport, RestLiAttachmentReader requestAttachmentReader, RestLiResponseAttachments responseAttachments) {
latch.countDown();
Assert.assertTrue(e instanceof RestException);
RestException ex = (RestException) e;
Assert.assertEquals(ex.getResponse().getStatus(), HttpStatus.S_406_NOT_ACCEPTABLE.getCode());
Assert.assertEquals(requestAttachmentReader, attachmentReader);
Assert.assertNull(responseAttachments);
}
@Override
public void onSuccess(RestResponse result, RequestExecutionReport executionReport, RestLiResponseAttachments responseAttachments) {
Assert.fail();
}
};
ServerResourceContext resourceContext = new ResourceContextImpl(new PathKeysImpl(), new RestRequestBuilder(URI.create("")).setHeader(RestConstants.HEADER_RESTLI_PROTOCOL_VERSION, AllProtocolVersions.LATEST_PROTOCOL_VERSION.toString()).build(), new RequestContext(), false, attachmentReader);
try {
RoutingResult routingResult = new RoutingResult(resourceContext, null);
RestUtils.validateRequestHeadersAndUpdateResourceContext(request.getHeaders(), (ServerResourceContext) routingResult.getContext());
FilterChainCallback filterChainCallback = new FilterChainCallbackImpl(null, _invoker, null, null, null, restLiResponseHandler, executionCallback);
final RestLiCallback<Object> callback = new RestLiCallback<Object>(null, new RestLiFilterResponseContextFactory<Object>(request, null, restLiResponseHandler), new RestLiFilterChain(null, filterChainCallback));
_invoker.invoke(null, routingResult, null, callback, null);
latch.await();
} catch (Exception e) {
// exception is expected
Assert.assertTrue(e instanceof RestLiServiceException);
}
Assert.assertNull(resourceContext.getResponseMimeType());
}
use of com.linkedin.restli.internal.server.PathKeysImpl in project rest.li by linkedin.
the class TestRestLiMethodInvocation method testActionsOnResource.
@Test
public void testActionsOnResource() throws Exception {
ResourceModel repliesResourceModel = buildResourceModel(RepliesCollectionResource.class);
ResourceModel locationResourceModel = buildResourceModel(LocationResource.class);
ResourceModel discoveredItemsResourceModel = buildResourceModel(DiscoveredItemsResource.class);
ResourceMethodDescriptor methodDescriptor;
RepliesCollectionResource repliesResource;
LocationResource locationResource;
DiscoveredItemsResource discoveredItemsResource;
// #1 Action on collection resource
methodDescriptor = repliesResourceModel.findActionMethod("replyToAll", ResourceLevel.COLLECTION);
repliesResource = getMockResource(RepliesCollectionResource.class);
repliesResource.replyToAll("hello");
EasyMock.expectLastCall().once();
String jsonEntityBody = RestLiTestHelper.doubleQuote("{'status': 'hello'}");
MutablePathKeys pathKeys = new PathKeysImpl();
pathKeys.append("statusID", 1L);
checkInvocation(repliesResource, methodDescriptor, "POST", version, "/statuses/1/replies?action=replyToAll", jsonEntityBody, pathKeys);
// #2 Action on simple resource
methodDescriptor = locationResourceModel.findActionMethod("new_status_from_location", ResourceLevel.ENTITY);
locationResource = getMockResource(LocationResource.class);
locationResource.newStatusFromLocation(eq("hello"));
EasyMock.expectLastCall().once();
jsonEntityBody = RestLiTestHelper.doubleQuote("{'status': 'hello'}");
pathKeys = new PathKeysImpl();
pathKeys.append("statusID", 1L);
checkInvocation(locationResource, methodDescriptor, "POST", version, "/statuses/1/location?action=new_status_from_location", jsonEntityBody, pathKeys);
// #3 Action on complex-key resource
methodDescriptor = discoveredItemsResourceModel.findActionMethod("purge", ResourceLevel.COLLECTION);
discoveredItemsResource = getMockResource(DiscoveredItemsResource.class);
discoveredItemsResource.purge(12L);
EasyMock.expectLastCall().once();
jsonEntityBody = RestLiTestHelper.doubleQuote("{'user': 12}");
checkInvocation(discoveredItemsResource, methodDescriptor, "POST", version, "/discovereditems/action=purge", jsonEntityBody, buildPathKeys());
}
Aggregations