Search in sources :

Example 41 with ServerResourceContext

use of com.linkedin.restli.internal.server.ServerResourceContext 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);
    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();
    RoutingResult result = _router.process(request, new RequestContext(), requestAttachments);
    assertNotNull(result);
    final ServerResourceContext resourceContext = (ServerResourceContext) result.getContext();
    if (requestAttachments != null) {
        Assert.assertEquals(resourceContext.getRequestAttachmentReader(), requestAttachments);
    } else {
        Assert.assertNull(resourceContext.getRequestAttachmentReader());
    }
    if (acceptHeader != null && acceptHeader.contains("multipart/related")) {
        Assert.assertTrue(resourceContext.responseAttachmentsSupported());
    } else {
        Assert.assertFalse(resourceContext.responseAttachmentsSupported());
    }
}
Also used : RoutingResult(com.linkedin.restli.internal.server.RoutingResult) 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) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) RequestContext(com.linkedin.r2.message.RequestContext) URI(java.net.URI) Test(org.testng.annotations.Test)

Example 42 with ServerResourceContext

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

the class TestArgumentBuilder method testQueryParameterType.

@Test
public void testQueryParameterType() {
    String testParamKey = "testParam";
    String expectedTestParamValue = "testParamValue";
    ServerResourceContext mockResourceContext = EasyMock.createMock(ServerResourceContext.class);
    EasyMock.expect(mockResourceContext.getParameter(testParamKey)).andReturn(expectedTestParamValue).anyTimes();
    EasyMock.expect(mockResourceContext.getRequestAttachmentReader()).andReturn(null);
    EasyMock.replay(mockResourceContext);
    Parameter<String> param = new Parameter<String>(testParamKey, String.class, DataSchemaConstants.STRING_DATA_SCHEMA, false, null, Parameter.ParamType.QUERY, false, AnnotationSet.EMPTY);
    List<Parameter<?>> parameters = Collections.<Parameter<?>>singletonList(param);
    Object[] results = ArgumentBuilder.buildArgs(new Object[0], getMockResourceMethod(parameters), mockResourceContext, null);
    Assert.assertEquals(results[0], expectedTestParamValue);
}
Also used : ServerResourceContext(com.linkedin.restli.internal.server.ServerResourceContext) Parameter(com.linkedin.restli.internal.server.model.Parameter) Test(org.testng.annotations.Test)

Example 43 with ServerResourceContext

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

the class TestArgumentBuilder method testPostParameterType.

@Test
public void testPostParameterType() {
    String testParamKey = "testParam";
    String expectedTestParamValue = "testParamValue";
    ServerResourceContext mockResourceContext = EasyMock.createMock(ServerResourceContext.class);
    DataMap entityBody = new DataMap();
    entityBody.put(testParamKey, expectedTestParamValue);
    DynamicRecordTemplate template = new DynamicRecordTemplate(entityBody, null);
    Parameter<String> param = new Parameter<String>(testParamKey, String.class, DataSchemaConstants.STRING_DATA_SCHEMA, false, null, Parameter.ParamType.POST, false, AnnotationSet.EMPTY);
    List<Parameter<?>> parameters = Collections.<Parameter<?>>singletonList(param);
    EasyMock.expect(mockResourceContext.getRequestAttachmentReader()).andReturn(null);
    Object[] results = ArgumentBuilder.buildArgs(new Object[0], getMockResourceMethod(parameters), mockResourceContext, template);
    Assert.assertEquals(results[0], expectedTestParamValue);
}
Also used : DynamicRecordTemplate(com.linkedin.data.template.DynamicRecordTemplate) ServerResourceContext(com.linkedin.restli.internal.server.ServerResourceContext) Parameter(com.linkedin.restli.internal.server.model.Parameter) DataMap(com.linkedin.data.DataMap) Test(org.testng.annotations.Test)

Example 44 with ServerResourceContext

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

the class TestArgumentBuilder method testRestLiAttachmentsParam.

@Test
public void testRestLiAttachmentsParam() {
    ServerResourceContext mockResourceContext = EasyMock.createMock(ServerResourceContext.class);
    final RestLiAttachmentReader restLiAttachmentReader = new RestLiAttachmentReader(null);
    EasyMock.expect(mockResourceContext.getRequestAttachmentReader()).andReturn(restLiAttachmentReader);
    EasyMock.replay(mockResourceContext);
    @SuppressWarnings({ "unchecked", "rawtypes" }) final Parameter<RestLiAttachmentReader> param = new Parameter("RestLi Attachment Reader", RestLiAttachmentReader.class, null, false, null, Parameter.ParamType.RESTLI_ATTACHMENTS_PARAM, false, AnnotationSet.EMPTY);
    List<Parameter<?>> parameters = Collections.<Parameter<?>>singletonList(param);
    Object[] results = ArgumentBuilder.buildArgs(new Object[0], getMockResourceMethod(parameters), mockResourceContext, null);
    Assert.assertEquals(results[0], restLiAttachmentReader);
}
Also used : ServerResourceContext(com.linkedin.restli.internal.server.ServerResourceContext) Parameter(com.linkedin.restli.internal.server.model.Parameter) RestLiAttachmentReader(com.linkedin.restli.common.attachments.RestLiAttachmentReader) Test(org.testng.annotations.Test)

Example 45 with ServerResourceContext

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

the class TestArgumentBuilder method testPathKeyParameterType.

@Test
@SuppressWarnings("deprecation")
public void testPathKeyParameterType() {
    String testParamKey = "testParam";
    String expectedTestParamValue = "testParamValue";
    // mock setup
    ServerResourceContext mockResourceContext = EasyMock.createMock(ServerResourceContext.class);
    MutablePathKeys mockPathKeys = EasyMock.createMock(MutablePathKeys.class);
    EasyMock.expect(mockPathKeys.get(testParamKey)).andReturn(expectedTestParamValue).anyTimes();
    EasyMock.expect(mockResourceContext.getPathKeys()).andReturn(mockPathKeys).anyTimes();
    EasyMock.expect(mockResourceContext.getRequestAttachmentReader()).andReturn(null);
    EasyMock.replay(mockResourceContext, mockPathKeys);
    List<Parameter<?>> parameters = new ArrayList<Parameter<?>>();
    Parameter<String> param1 = new Parameter<String>(testParamKey, String.class, null, false, null, Parameter.ParamType.KEY, false, AnnotationSet.EMPTY);
    Parameter<String> param2 = new Parameter<String>(testParamKey, String.class, null, false, null, Parameter.ParamType.ASSOC_KEY_PARAM, false, AnnotationSet.EMPTY);
    Parameter<PathKeys> param3 = new Parameter<PathKeys>(testParamKey, PathKeys.class, null, false, null, Parameter.ParamType.PATH_KEYS, false, AnnotationSet.EMPTY);
    Parameter<PathKeys> param4 = new Parameter<PathKeys>(testParamKey, PathKeys.class, null, false, null, Parameter.ParamType.PATH_KEYS_PARAM, false, AnnotationSet.EMPTY);
    parameters.add(param1);
    parameters.add(param2);
    parameters.add(param3);
    parameters.add(param4);
    Object[] results = ArgumentBuilder.buildArgs(new Object[0], getMockResourceMethod(parameters), mockResourceContext, null);
    Assert.assertEquals(results[0], expectedTestParamValue);
    Assert.assertEquals(results[1], expectedTestParamValue);
    Assert.assertEquals(results[2], mockPathKeys);
    Assert.assertEquals(results[3], mockPathKeys);
}
Also used : PathKeys(com.linkedin.restli.server.PathKeys) MutablePathKeys(com.linkedin.restli.internal.server.MutablePathKeys) MutablePathKeys(com.linkedin.restli.internal.server.MutablePathKeys) ServerResourceContext(com.linkedin.restli.internal.server.ServerResourceContext) ArrayList(java.util.ArrayList) Parameter(com.linkedin.restli.internal.server.model.Parameter) Test(org.testng.annotations.Test)

Aggregations

ServerResourceContext (com.linkedin.restli.internal.server.ServerResourceContext)55 Test (org.testng.annotations.Test)28 RoutingResult (com.linkedin.restli.internal.server.RoutingResult)18 Parameter (com.linkedin.restli.internal.server.model.Parameter)15 ResourceContextImpl (com.linkedin.restli.internal.server.ResourceContextImpl)14 DataMap (com.linkedin.data.DataMap)13 RestLiServiceException (com.linkedin.restli.server.RestLiServiceException)12 HashMap (java.util.HashMap)11 RequestContext (com.linkedin.r2.message.RequestContext)10 PathKeysImpl (com.linkedin.restli.internal.server.PathKeysImpl)10 ResourceMethodDescriptor (com.linkedin.restli.internal.server.model.ResourceMethodDescriptor)9 RestRequest (com.linkedin.r2.message.rest.RestRequest)8 RestLiAttachmentReader (com.linkedin.restli.common.attachments.RestLiAttachmentReader)7 URI (java.net.URI)7 ByteString (com.linkedin.data.ByteString)6 RestRequestBuilder (com.linkedin.r2.message.rest.RestRequestBuilder)6 RestResponse (com.linkedin.r2.message.rest.RestResponse)6 RoutingException (com.linkedin.restli.server.RoutingException)6 MaskTree (com.linkedin.data.transform.filter.request.MaskTree)5 ResourceModel (com.linkedin.restli.internal.server.model.ResourceModel)5