Search in sources :

Example 21 with ServerResourceContext

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

the class TestArgumentBuilder method testNoOpParamType.

@Test(dataProvider = "noOpParameterData")
public void testNoOpParamType(Class<?> dataType, Parameter.ParamType paramType) {
    String paramKey = "testParam";
    ServerResourceContext mockResourceContext = EasyMock.createMock(ServerResourceContext.class);
    @SuppressWarnings({ "unchecked", "rawtypes" }) Parameter<?> param = new Parameter(paramKey, dataType, null, false, null, paramType, 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], null);
}
Also used : ServerResourceContext(com.linkedin.restli.internal.server.ServerResourceContext) Parameter(com.linkedin.restli.internal.server.model.Parameter) Test(org.testng.annotations.Test)

Example 22 with ServerResourceContext

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

the class TestArgumentBuilder method testRestLiAttachmentsParamResourceNotExpect.

@Test
public void testRestLiAttachmentsParamResourceNotExpect() {
    //This test makes sure that if the resource method did not expect attachments but there were attachments present
    //in the request, that an exception is thrown.
    ServerResourceContext mockResourceContext = EasyMock.createMock(ServerResourceContext.class);
    final RestLiAttachmentReader restLiAttachmentReader = new RestLiAttachmentReader(null);
    EasyMock.expect(mockResourceContext.getRequestAttachmentReader()).andReturn(restLiAttachmentReader);
    EasyMock.replay(mockResourceContext);
    List<Parameter<?>> parameters = Collections.<Parameter<?>>emptyList();
    try {
        ArgumentBuilder.buildArgs(new Object[0], getMockResourceMethod(parameters), mockResourceContext, null);
        Assert.fail();
    } catch (RestLiServiceException restLiServiceException) {
        Assert.assertEquals(restLiServiceException.getStatus(), HttpStatus.S_400_BAD_REQUEST);
        Assert.assertEquals(restLiServiceException.getMessage(), "Resource method endpoint invoked does not accept any request attachments.");
    }
}
Also used : RestLiServiceException(com.linkedin.restli.server.RestLiServiceException) 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 23 with ServerResourceContext

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

the class TestArgumentBuilder method testRestLiAttachmentsParamResourceExpectNotPresent.

@Test
public void testRestLiAttachmentsParamResourceExpectNotPresent() {
    //This test makes sure that a resource method that expects attachments, but none are present in the request,
    //is given a null for the RestLiAttachmentReader.
    ServerResourceContext mockResourceContext = EasyMock.createMock(ServerResourceContext.class);
    EasyMock.expect(mockResourceContext.getRequestAttachmentReader()).andReturn(null);
    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], null);
}
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 24 with ServerResourceContext

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

the class TestBatchUpdateResponseBuilder method testContextErrors.

@Test
public void testContextErrors() {
    BatchUpdateResponseBuilder builder = new BatchUpdateResponseBuilder(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 envelope = builder.buildRestLiResponseData(null, routingResult, new BatchUpdateResult<Object, Integer>(Collections.<Object, UpdateResponse>emptyMap()), Collections.<String, String>emptyMap(), Collections.<HttpCookie>emptyList());
    Assert.assertEquals(envelope.getBatchResponseEnvelope().getBatchResponseMap().get("foo").getException(), exception);
    Assert.assertEquals(envelope.getBatchResponseEnvelope().getBatchResponseMap().size(), 1);
}
Also used : RoutingResult(com.linkedin.restli.internal.server.RoutingResult) UpdateResponse(com.linkedin.restli.server.UpdateResponse) 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 25 with ServerResourceContext

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

the class TestCreateResponseBuilder method getMockResourceContext.

private static ResourceContext getMockResourceContext(ProtocolVersion protocolVersion, String altKeyName) {
    ServerResourceContext mockContext = EasyMock.createMock(ServerResourceContext.class);
    EasyMock.expect(mockContext.getRestliProtocolVersion()).andReturn(protocolVersion).once();
    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)

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