Search in sources :

Example 11 with ServerResourceContext

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

the class TestBatchCreateResponseBuilder method getMockResourceContext.

private static ResourceContext getMockResourceContext(String altKeyName) {
    ServerResourceContext mockContext = EasyMock.createMock(ServerResourceContext.class);
    EasyMock.expect(mockContext.hasParameter(RestConstants.ALT_KEY_PARAM)).andReturn(altKeyName != null).atLeastOnce();
    if (altKeyName != null) {
        EasyMock.expect(mockContext.getParameter(RestConstants.ALT_KEY_PARAM)).andReturn(altKeyName).atLeastOnce();
    }
    EasyMock.replay(mockContext);
    return mockContext;
}
Also used : ServerResourceContext(com.linkedin.restli.internal.server.ServerResourceContext)

Example 12 with ServerResourceContext

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

the class TestBatchGetResponseBuilder method getMockResourceContext.

private static ResourceContext getMockResourceContext(ProtocolVersion protocolVersion, Map<Object, RestLiServiceException> exceptions, String altKeyName, MaskTree maskTree, ProjectionMode projectionMode) {
    ServerResourceContext mockContext = EasyMock.createMock(ServerResourceContext.class);
    EasyMock.expect(mockContext.getBatchKeyErrors()).andReturn(exceptions).once();
    EasyMock.expect(mockContext.getProjectionMode()).andReturn(projectionMode).times(2);
    EasyMock.expect(mockContext.getProjectionMask()).andReturn(maskTree).times(2);
    EasyMock.expect(mockContext.getRestliProtocolVersion()).andReturn(protocolVersion).once();
    EasyMock.expect(mockContext.hasParameter(RestConstants.ALT_KEY_PARAM)).andReturn(altKeyName != null).anyTimes();
    if (altKeyName != null) {
        EasyMock.expect(mockContext.getParameter(RestConstants.ALT_KEY_PARAM)).andReturn(altKeyName).atLeastOnce();
    }
    EasyMock.replay(mockContext);
    return mockContext;
}
Also used : ServerResourceContext(com.linkedin.restli.internal.server.ServerResourceContext)

Example 13 with ServerResourceContext

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

the class TestBatchGetResponseBuilder method testProjectionInBuildRestliResponseData.

@Test
public void testProjectionInBuildRestliResponseData() {
    MaskTree maskTree = new MaskTree();
    maskTree.addOperation(new PathSpec("fruitsField"), MaskOperation.POSITIVE_MASK_OP);
    ServerResourceContext mockContext = EasyMock.createMock(ServerResourceContext.class);
    EasyMock.expect(mockContext.hasParameter(RestConstants.ALT_KEY_PARAM)).andReturn(false);
    EasyMock.expect(mockContext.getProjectionMode()).andReturn(ProjectionMode.AUTOMATIC);
    EasyMock.expect(mockContext.getProjectionMask()).andReturn(maskTree);
    EasyMock.expect(mockContext.getBatchKeyErrors()).andReturn(Collections.<Object, RestLiServiceException>emptyMap()).once();
    EasyMock.replay(mockContext);
    ResourceMethodDescriptor mockDescriptor = getMockResourceMethodDescriptor(null);
    RoutingResult routingResult = new RoutingResult(mockContext, mockDescriptor);
    Map<Integer, Foo> results = new HashMap<Integer, Foo>();
    Foo value = new Foo().setStringField("value").setFruitsField(Fruits.APPLE);
    results.put(1, value);
    BatchGetResponseBuilder responseBuilder = new BatchGetResponseBuilder(new ErrorResponseBuilder());
    RestLiResponseData responseData = responseBuilder.buildRestLiResponseData(null, routingResult, results, Collections.<String, String>emptyMap(), Collections.<HttpCookie>emptyList());
    RecordTemplate record = responseData.getBatchResponseEnvelope().getBatchResponseMap().get(1).getRecord();
    Assert.assertEquals(record.data().size(), 1);
    Assert.assertEquals(record.data().get("fruitsField"), Fruits.APPLE.toString());
    EasyMock.verify(mockContext);
}
Also used : HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ResourceMethodDescriptor(com.linkedin.restli.internal.server.model.ResourceMethodDescriptor) Foo(com.linkedin.pegasus.generator.examples.Foo) RestLiResponseData(com.linkedin.restli.server.RestLiResponseData) PathSpec(com.linkedin.data.schema.PathSpec) RoutingResult(com.linkedin.restli.internal.server.RoutingResult) RestLiServiceException(com.linkedin.restli.server.RestLiServiceException) MaskTree(com.linkedin.data.transform.filter.request.MaskTree) ServerResourceContext(com.linkedin.restli.internal.server.ServerResourceContext) RecordTemplate(com.linkedin.data.template.RecordTemplate) Test(org.testng.annotations.Test)

Example 14 with ServerResourceContext

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

the class TestBatchGetResponseBuilder method testContextErrors.

@Test
public void testContextErrors() {
    BatchGetResponseBuilder builder = new BatchGetResponseBuilder(new ErrorResponseBuilder());
    ServerResourceContext context = EasyMock.createMock(ServerResourceContext.class);
    Map<Object, RestLiServiceException> errors = new HashMap<Object, RestLiServiceException>();
    RestLiServiceException exception = new RestLiServiceException(HttpStatus.S_402_PAYMENT_REQUIRED);
    errors.put("foo", exception);
    EasyMock.expect(context.hasParameter("altkey")).andReturn(false);
    EasyMock.expect(context.getBatchKeyErrors()).andReturn(errors);
    EasyMock.replay(context);
    RoutingResult routingResult = new RoutingResult(context, null);
    RestLiResponseData responseData = builder.buildRestLiResponseData(null, routingResult, new BatchResult<Object, EmptyRecord>(Collections.<Object, EmptyRecord>emptyMap(), Collections.<Object, RestLiServiceException>emptyMap()), Collections.<String, String>emptyMap(), Collections.<HttpCookie>emptyList());
    Assert.assertEquals(responseData.getBatchResponseEnvelope().getBatchResponseMap().get("foo").getException(), exception);
    Assert.assertEquals(responseData.getBatchResponseEnvelope().getBatchResponseMap().size(), 1);
}
Also used : RoutingResult(com.linkedin.restli.internal.server.RoutingResult) EmptyRecord(com.linkedin.restli.common.EmptyRecord) RestLiServiceException(com.linkedin.restli.server.RestLiServiceException) ServerResourceContext(com.linkedin.restli.internal.server.ServerResourceContext) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) RestLiResponseData(com.linkedin.restli.server.RestLiResponseData) Test(org.testng.annotations.Test)

Example 15 with ServerResourceContext

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

the class TestArgumentBuilder method testBuildOptionalArg.

// utility method for reuse
private Object[] testBuildOptionalArg(Parameter<?> param) {
    String paramKey = param.getName();
    Class<?> dataType = param.getType();
    Parameter.ParamType paramType = param.getParamType();
    // mock resource context
    ServerResourceContext mockResourceContext = EasyMock.createMock(ServerResourceContext.class);
    DynamicRecordTemplate template = null;
    if (paramType == Parameter.ParamType.POST) {
        template = new DynamicRecordTemplate(new DataMap(), null);
    } else {
        MutablePathKeys mockPathKeys = EasyMock.createMock(MutablePathKeys.class);
        EasyMock.expect(mockPathKeys.get(paramKey)).andReturn(null);
        EasyMock.expect(mockResourceContext.getPathKeys()).andReturn(mockPathKeys);
        EasyMock.expect(mockResourceContext.getRequestAttachmentReader()).andReturn(null);
        if (DataTemplate.class.isAssignableFrom(dataType)) {
            EasyMock.expect(mockResourceContext.getStructuredParameter(paramKey)).andReturn(null);
        } else {
            EasyMock.expect(mockResourceContext.getParameter(paramKey)).andReturn(null);
        }
        EasyMock.replay(mockResourceContext);
    }
    // invoke buildArgs
    List<Parameter<?>> parameters = Collections.<Parameter<?>>singletonList(param);
    return ArgumentBuilder.buildArgs(new Object[0], getMockResourceMethod(parameters), mockResourceContext, template);
}
Also used : DynamicRecordTemplate(com.linkedin.data.template.DynamicRecordTemplate) MutablePathKeys(com.linkedin.restli.internal.server.MutablePathKeys) ServerResourceContext(com.linkedin.restli.internal.server.ServerResourceContext) Parameter(com.linkedin.restli.internal.server.model.Parameter) DataMap(com.linkedin.data.DataMap)

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