Search in sources :

Example 41 with ResourceContextImpl

use of com.linkedin.restli.internal.server.ResourceContextImpl in project rest.li by linkedin.

the class TestRestLiResponseHandler method testGetRequestCookies.

@Test
public void testGetRequestCookies() throws URISyntaxException, RestLiSyntaxException {
    List<HttpCookie> cookies = Arrays.asList(new HttpCookie("cook1", "value1"), new HttpCookie("cook2", "value2"));
    RestRequest request = new RestRequestBuilder(new URI("http://www.abc.org/")).setMethod("DONT_CARE").setHeaders(new TreeMap<>(String.CASE_INSENSITIVE_ORDER)).setCookies(CookieUtil.encodeCookies(cookies)).build();
    ServerResourceContext resourceContext = new ResourceContextImpl(new PathKeysImpl(), request, new RequestContext());
    Assert.assertEquals(resourceContext.getRequestCookies(), cookies);
}
Also used : RestRequest(com.linkedin.r2.message.rest.RestRequest) ServerResourceContext(com.linkedin.restli.internal.server.ServerResourceContext) PathKeysImpl(com.linkedin.restli.internal.server.PathKeysImpl) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) RequestContext(com.linkedin.r2.message.RequestContext) HttpCookie(java.net.HttpCookie) URI(java.net.URI) ResourceContextImpl(com.linkedin.restli.internal.server.ResourceContextImpl) Test(org.testng.annotations.Test)

Example 42 with ResourceContextImpl

use of com.linkedin.restli.internal.server.ResourceContextImpl in project rest.li by linkedin.

the class TestRestLiRouting method checkResult.

private void checkResult(String uri, ProtocolVersion version, String httpMethod, String restliMethod, ResourceMethod method, Class<?> resourceClass, String methodName, boolean hasBatchKeys, String... expectedPathKeys) throws URISyntaxException, RestLiSyntaxException {
    RestRequestBuilder builder = createRequestBuilder(uri, httpMethod, version);
    if (restliMethod != null) {
        builder.setHeader("X-RestLi-Method", restliMethod);
    }
    RestRequest request = builder.build();
    ServerResourceContext context = new ResourceContextImpl(new PathKeysImpl(), request, new RequestContext());
    ResourceMethodDescriptor methodDescriptor = _router.process(context);
    assertEquals(methodDescriptor.getType(), method);
    assertEquals(methodDescriptor.getResourceModel().getResourceClass(), resourceClass);
    assertEquals(methodDescriptor.getMethod().getName(), methodName);
    // If hasBatchKeys, there are batch keys in the context, and if not, there are none.
    assertEquals(hasBatchKeys, context.getPathKeys().getBatchIds() != null);
    for (String pathKey : expectedPathKeys) {
        assertNotNull(context.getPathKeys().get(pathKey));
    }
    if (method != null) {
        String expectedOperationName;
        switch(method) {
            case ACTION:
                expectedOperationName = "action:" + methodDescriptor.getActionName();
                break;
            case FINDER:
                expectedOperationName = "finder:" + methodDescriptor.getFinderName();
                break;
            case BATCH_FINDER:
                expectedOperationName = "batch_finder:" + methodDescriptor.getBatchFinderName();
                break;
            default:
                expectedOperationName = method.toString().toLowerCase();
        }
        assertEquals(context.getRawRequestContext().getLocalAttr(R2Constants.OPERATION), expectedOperationName);
    }
}
Also used : RestRequest(com.linkedin.r2.message.rest.RestRequest) ServerResourceContext(com.linkedin.restli.internal.server.ServerResourceContext) PathKeysImpl(com.linkedin.restli.internal.server.PathKeysImpl) ResourceMethodDescriptor(com.linkedin.restli.internal.server.model.ResourceMethodDescriptor) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) RequestContext(com.linkedin.r2.message.RequestContext) ResourceContextImpl(com.linkedin.restli.internal.server.ResourceContextImpl)

Example 43 with ResourceContextImpl

use of com.linkedin.restli.internal.server.ResourceContextImpl in project rest.li by linkedin.

the class TestRestLiRouting method testRoutingDetailsAssociationBatchGet.

@Test(dataProvider = TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "routingDetailsAssociationBatch")
public void testRoutingDetailsAssociationBatchGet(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.BATCH_GET);
    assertNull(resourceMethodDescriptor.getActionName());
    assertNull(resourceMethodDescriptor.getFinderName());
    assertEquals(resourceMethodDescriptor.getMethod().getDeclaringClass(), FollowsAssociativeResource.class);
    assertEquals(resourceMethodDescriptor.getMethod().getName(), "batchGet");
    assertEquals(resourceMethodDescriptor.getMethod().getParameterTypes(), new Class<?>[] { Set.class });
    assertEquals(resourceMethodDescriptor.getResourceModel().getName(), "follows");
    PathKeys keys = context.getPathKeys();
    assertNull(keys.getAsString("followerID"));
    assertNull(keys.getAsString("followeeID"));
    CompoundKey key1 = new CompoundKey();
    key1.append("followerID", 1L);
    key1.append("followeeID", 2L);
    CompoundKey key2 = new CompoundKey();
    key2.append("followerID", 3L);
    key2.append("followeeID", 4L);
    Set<CompoundKey> expectedBatchKeys = new HashSet<>();
    expectedBatchKeys.add(key1);
    expectedBatchKeys.add(key2);
    assertEquals(keys.getBatchIds().size(), 2);
    for (CompoundKey batchKey : keys.<CompoundKey>getBatchIds()) {
        assertTrue(expectedBatchKeys.contains(batchKey));
        expectedBatchKeys.remove(batchKey);
    }
    assertEquals(expectedBatchKeys.size(), 0);
}
Also used : CompoundKey(com.linkedin.restli.common.CompoundKey) PathKeysImpl(com.linkedin.restli.internal.server.PathKeysImpl) ResourceMethodDescriptor(com.linkedin.restli.internal.server.model.ResourceMethodDescriptor) PathKeys(com.linkedin.restli.server.PathKeys) RestLiRouter(com.linkedin.restli.internal.server.RestLiRouter) RestRequest(com.linkedin.r2.message.rest.RestRequest) ServerResourceContext(com.linkedin.restli.internal.server.ServerResourceContext) ResourceModel(com.linkedin.restli.internal.server.model.ResourceModel) RequestContext(com.linkedin.r2.message.RequestContext) RestLiConfig(com.linkedin.restli.server.RestLiConfig) ResourceContextImpl(com.linkedin.restli.internal.server.ResourceContextImpl) HashSet(java.util.HashSet) Test(org.testng.annotations.Test)

Example 44 with ResourceContextImpl

use of com.linkedin.restli.internal.server.ResourceContextImpl in project rest.li by linkedin.

the class TestRestLiRouting method expectRoutingExceptionWithStatus.

private void expectRoutingExceptionWithStatus(String uri, ProtocolVersion version, String httpMethod, String restliMethod, HttpStatus status) throws URISyntaxException {
    RestRequestBuilder builder = createRequestBuilder(uri, httpMethod, version);
    if (restliMethod != null) {
        builder.setHeader("X-RestLi-Method", restliMethod);
    }
    RestRequest request = builder.build();
    try {
        try {
            ServerResourceContext context = new ResourceContextImpl(new PathKeysImpl(), request, new RequestContext());
            ResourceMethodDescriptor method = _router.process(context);
            fail("Expected RoutingException, got: " + method.toString());
        } catch (RestLiSyntaxException e) {
            throw new RoutingException(e.getMessage(), HttpStatus.S_400_BAD_REQUEST.getCode());
        }
    } catch (RoutingException e) {
        // expected a certain httpStatus code
        assertEquals(e.getStatus(), status.getCode());
    }
}
Also used : RoutingException(com.linkedin.restli.server.RoutingException) RestRequest(com.linkedin.r2.message.rest.RestRequest) RestLiSyntaxException(com.linkedin.restli.internal.server.util.RestLiSyntaxException) ServerResourceContext(com.linkedin.restli.internal.server.ServerResourceContext) PathKeysImpl(com.linkedin.restli.internal.server.PathKeysImpl) ResourceMethodDescriptor(com.linkedin.restli.internal.server.model.ResourceMethodDescriptor) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) RequestContext(com.linkedin.r2.message.RequestContext) ResourceContextImpl(com.linkedin.restli.internal.server.ResourceContextImpl)

Example 45 with ResourceContextImpl

use of com.linkedin.restli.internal.server.ResourceContextImpl in project rest.li by linkedin.

the class TestRestLiRouting method testRoutingDetailsSimplePartialUpdate.

@Test(dataProvider = TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "routingDetailsSimple")
public void testRoutingDetailsSimplePartialUpdate(ProtocolVersion version, String uri) throws Exception {
    Map<String, ResourceModel> pathRootResourceMap = buildResourceModels(TrendingResource.class);
    _router = new RestLiRouter(pathRootResourceMap, new RestLiConfig());
    RestRequest request = createRequest(uri, "POST", version);
    ServerResourceContext context = new ResourceContextImpl(new PathKeysImpl(), request, new RequestContext());
    ResourceMethodDescriptor resourceMethodDescriptor = _router.process(context);
    assertNotNull(resourceMethodDescriptor);
    assertEquals(resourceMethodDescriptor.getType(), ResourceMethod.PARTIAL_UPDATE);
    assertNull(resourceMethodDescriptor.getActionName());
    assertNull(resourceMethodDescriptor.getFinderName());
    assertEquals(resourceMethodDescriptor.getMethod().getDeclaringClass(), TrendingResource.class);
    assertEquals(resourceMethodDescriptor.getMethod().getName(), "update");
    assertEquals(resourceMethodDescriptor.getMethod().getParameterTypes(), new Class<?>[] { PatchRequest.class });
    assertEquals(resourceMethodDescriptor.getResourceModel().getName(), "trending");
    PathKeys keys = context.getPathKeys();
    assertNull(keys.getBatchIds());
}
Also used : PathKeys(com.linkedin.restli.server.PathKeys) RestLiRouter(com.linkedin.restli.internal.server.RestLiRouter) RestRequest(com.linkedin.r2.message.rest.RestRequest) ServerResourceContext(com.linkedin.restli.internal.server.ServerResourceContext) PathKeysImpl(com.linkedin.restli.internal.server.PathKeysImpl) ResourceMethodDescriptor(com.linkedin.restli.internal.server.model.ResourceMethodDescriptor) ResourceModel(com.linkedin.restli.internal.server.model.ResourceModel) RequestContext(com.linkedin.r2.message.RequestContext) RestLiConfig(com.linkedin.restli.server.RestLiConfig) ResourceContextImpl(com.linkedin.restli.internal.server.ResourceContextImpl) Test(org.testng.annotations.Test)

Aggregations

ResourceContextImpl (com.linkedin.restli.internal.server.ResourceContextImpl)52 RequestContext (com.linkedin.r2.message.RequestContext)41 ServerResourceContext (com.linkedin.restli.internal.server.ServerResourceContext)40 PathKeysImpl (com.linkedin.restli.internal.server.PathKeysImpl)34 Test (org.testng.annotations.Test)31 RestRequest (com.linkedin.r2.message.rest.RestRequest)29 ResourceMethodDescriptor (com.linkedin.restli.internal.server.model.ResourceMethodDescriptor)29 ResourceModel (com.linkedin.restli.internal.server.model.ResourceModel)25 RoutingResult (com.linkedin.restli.internal.server.RoutingResult)20 URI (java.net.URI)15 RestRequestBuilder (com.linkedin.r2.message.rest.RestRequestBuilder)14 RestLiRouter (com.linkedin.restli.internal.server.RestLiRouter)14 RestLiConfig (com.linkedin.restli.server.RestLiConfig)14 ByteString (com.linkedin.data.ByteString)11 RestLiTestHelper.buildResourceModel (com.linkedin.restli.server.test.RestLiTestHelper.buildResourceModel)11 RestLiSyntaxException (com.linkedin.restli.internal.server.util.RestLiSyntaxException)10 PathKeys (com.linkedin.restli.server.PathKeys)10 RoutingException (com.linkedin.restli.server.RoutingException)9 DataMap (com.linkedin.data.DataMap)7 FilterRequestContext (com.linkedin.restli.server.filter.FilterRequestContext)7