Search in sources :

Example 71 with RestRequest

use of com.linkedin.r2.message.rest.RestRequest in project rest.li by linkedin.

the class TestCreateResponseBuilder method testCreateResponseException.

@Test
public void testCreateResponseException() throws URISyntaxException {
    CreateResponse createResponse = new CreateResponse(new RestLiServiceException(HttpStatus.S_400_BAD_REQUEST));
    RestRequest restRequest = new RestRequestBuilder(new URI("/foo")).build();
    RestLiResponseData envelope = new CreateResponseBuilder().buildRestLiResponseData(restRequest, null, createResponse, Collections.<String, String>emptyMap(), Collections.<HttpCookie>emptyList());
    Assert.assertTrue(envelope.isErrorResponse());
}
Also used : RestRequest(com.linkedin.r2.message.rest.RestRequest) RestLiServiceException(com.linkedin.restli.server.RestLiServiceException) CreateResponse(com.linkedin.restli.server.CreateResponse) RestLiResponseData(com.linkedin.restli.server.RestLiResponseData) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) URI(java.net.URI) Test(org.testng.annotations.Test)

Example 72 with RestRequest

use of com.linkedin.r2.message.rest.RestRequest in project rest.li by linkedin.

the class TestCreateResponseBuilder method testBuilder.

@Test(dataProvider = "testData")
public void testBuilder(ProtocolVersion protocolVersion, Object expectedId, String expectedLocation, String expectedHeaderId, String altKeyName, Map<String, AlternativeKey<?, ?>> alternativeKeyMap) throws URISyntaxException {
    CompoundKey compoundKey = new CompoundKey().append("a", "a").append("b", 1);
    CreateResponse createResponse = new CreateResponse(compoundKey);
    IdResponse<?> expectedIdResponse = new IdResponse<Object>(expectedId);
    RestRequest restRequest = new RestRequestBuilder(new URI("/foo")).build();
    Map<String, String> headers = ResponseBuilderUtil.getHeaders();
    headers.put(RestConstants.HEADER_RESTLI_PROTOCOL_VERSION, protocolVersion.toString());
    // the headers passed in are modified
    Map<String, String> expectedHeaders = new HashMap<String, String>(headers);
    ResourceMethodDescriptor mockDescriptor = getMockResourceMethodDescriptor(alternativeKeyMap);
    ResourceContext mockContext = getMockResourceContext(protocolVersion, altKeyName);
    RoutingResult routingResult = new RoutingResult(mockContext, mockDescriptor);
    CreateResponseBuilder createResponseBuilder = new CreateResponseBuilder();
    RestLiResponseData responseData = createResponseBuilder.buildRestLiResponseData(restRequest, routingResult, createResponse, headers, Collections.<HttpCookie>emptyList());
    Assert.assertFalse(responseData.getCreateResponseEnvelope().isGetAfterCreate());
    PartialRestResponse partialRestResponse = createResponseBuilder.buildResponse(routingResult, responseData);
    expectedHeaders.put(RestConstants.HEADER_LOCATION, expectedLocation);
    if (protocolVersion.equals(AllProtocolVersions.RESTLI_PROTOCOL_1_0_0.getProtocolVersion())) {
        expectedHeaders.put(RestConstants.HEADER_ID, expectedHeaderId);
    } else {
        expectedHeaders.put(RestConstants.HEADER_RESTLI_ID, expectedHeaderId);
    }
    EasyMock.verify(mockContext, mockDescriptor);
    ResponseBuilderUtil.validateHeaders(partialRestResponse, expectedHeaders);
    Assert.assertEquals(partialRestResponse.getStatus(), HttpStatus.S_201_CREATED);
    Assert.assertEquals(partialRestResponse.getEntity(), expectedIdResponse);
}
Also used : IdResponse(com.linkedin.restli.common.IdResponse) ResourceContext(com.linkedin.restli.server.ResourceContext) ServerResourceContext(com.linkedin.restli.internal.server.ServerResourceContext) CompoundKey(com.linkedin.restli.common.CompoundKey) HashMap(java.util.HashMap) CreateResponse(com.linkedin.restli.server.CreateResponse) ResourceMethodDescriptor(com.linkedin.restli.internal.server.model.ResourceMethodDescriptor) RestLiResponseData(com.linkedin.restli.server.RestLiResponseData) URI(java.net.URI) RoutingResult(com.linkedin.restli.internal.server.RoutingResult) RestRequest(com.linkedin.r2.message.rest.RestRequest) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) Test(org.testng.annotations.Test)

Example 73 with RestRequest

use of com.linkedin.r2.message.rest.RestRequest in project rest.li by linkedin.

the class TestCreateResponseBuilder method testBuilderException.

@Test
public void testBuilderException() throws URISyntaxException {
    CompoundKey compoundKey = new CompoundKey().append("a", "a").append("b", 1);
    CreateResponse createResponse = new CreateResponse(compoundKey, null);
    RestRequest restRequest = new RestRequestBuilder(new URI("/foo")).build();
    ProtocolVersion protocolVersion = AllProtocolVersions.RESTLI_PROTOCOL_1_0_0.getProtocolVersion();
    Map<String, String> headers = ResponseBuilderUtil.getHeaders();
    headers.put(RestConstants.HEADER_RESTLI_PROTOCOL_VERSION, protocolVersion.toString());
    ResourceMethodDescriptor mockDescriptor = getMockResourceMethodDescriptor(null);
    ResourceContext mockContext = getMockResourceContext(protocolVersion, null);
    RoutingResult routingResult = new RoutingResult(mockContext, mockDescriptor);
    CreateResponseBuilder createResponseBuilder = new CreateResponseBuilder();
    try {
        createResponseBuilder.buildRestLiResponseData(restRequest, routingResult, createResponse, headers, Collections.<HttpCookie>emptyList());
        Assert.fail("buildRestLiResponseData should have thrown an exception because the status is null!");
    } catch (RestLiServiceException e) {
        Assert.assertTrue(e.getMessage().contains("Unexpected null encountered. HttpStatus is null inside of a CreateResponse from the resource method: "));
    }
}
Also used : ResourceContext(com.linkedin.restli.server.ResourceContext) ServerResourceContext(com.linkedin.restli.internal.server.ServerResourceContext) CompoundKey(com.linkedin.restli.common.CompoundKey) CreateResponse(com.linkedin.restli.server.CreateResponse) ResourceMethodDescriptor(com.linkedin.restli.internal.server.model.ResourceMethodDescriptor) ProtocolVersion(com.linkedin.restli.common.ProtocolVersion) URI(java.net.URI) RoutingResult(com.linkedin.restli.internal.server.RoutingResult) RestRequest(com.linkedin.r2.message.rest.RestRequest) RestLiServiceException(com.linkedin.restli.server.RestLiServiceException) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) Test(org.testng.annotations.Test)

Example 74 with RestRequest

use of com.linkedin.r2.message.rest.RestRequest in project rest.li by linkedin.

the class TestUpdateArgumentBuilder method testArgumentBuilderSuccess.

@Test(dataProvider = "argumentData")
public void testArgumentBuilderSuccess(List<Parameter<?>> params, Key key, String keyName, Object keyValue) {
    RestRequest request = RestLiArgumentBuilderTestHelper.getMockRequest(false, "{\"a\":\"xyz\",\"b\":123}", 1);
    ResourceModel model = RestLiArgumentBuilderTestHelper.getMockResourceModel(MyComplexKey.class, key, true);
    ResourceMethodDescriptor descriptor;
    if (key != null) {
        descriptor = RestLiArgumentBuilderTestHelper.getMockResourceMethodDescriptor(model, 3, params);
    } else {
        descriptor = RestLiArgumentBuilderTestHelper.getMockResourceMethodDescriptor(model, 2, params);
    }
    ResourceContext context = RestLiArgumentBuilderTestHelper.getMockResourceContext(keyName, keyValue, null, true);
    RoutingResult routingResult;
    if (key != null) {
        routingResult = RestLiArgumentBuilderTestHelper.getMockRoutingResult(descriptor, 4, context, 2);
    } else {
        routingResult = RestLiArgumentBuilderTestHelper.getMockRoutingResult(descriptor, 3, context, 1);
    }
    RestLiArgumentBuilder argumentBuilder = new UpdateArgumentBuilder();
    RestLiRequestData requestData = argumentBuilder.extractRequestData(routingResult, request);
    Object[] args = argumentBuilder.buildArguments(requestData, routingResult);
    if (keyValue != null) {
        assertEquals(args.length, 2);
        assertEquals(args[0], keyValue);
    }
    assertTrue(args[args.length - 1] instanceof MyComplexKey);
    assertEquals(((MyComplexKey) args[args.length - 1]).getA(), "xyz");
    assertEquals((long) ((MyComplexKey) args[args.length - 1]).getB(), 123L);
    verify(request, model, descriptor, context, routingResult);
}
Also used : RoutingResult(com.linkedin.restli.internal.server.RoutingResult) RestRequest(com.linkedin.r2.message.rest.RestRequest) ResourceContext(com.linkedin.restli.server.ResourceContext) MyComplexKey(com.linkedin.restli.common.test.MyComplexKey) ResourceMethodDescriptor(com.linkedin.restli.internal.server.model.ResourceMethodDescriptor) ResourceModel(com.linkedin.restli.internal.server.model.ResourceModel) RestLiRequestData(com.linkedin.restli.server.RestLiRequestData) Test(org.testng.annotations.Test)

Example 75 with RestRequest

use of com.linkedin.r2.message.rest.RestRequest in project rest.li by linkedin.

the class TestUpdateArgumentBuilder method testFailure.

@Test(dataProvider = "failureEntityData", dataProviderClass = RestLiArgumentBuilderTestHelper.class)
public void testFailure(String entity) {
    RestRequest request = RestLiArgumentBuilderTestHelper.getMockRequest(false, entity, 1);
    ResourceModel model = RestLiArgumentBuilderTestHelper.getMockResourceModel(MyComplexKey.class, null, false);
    ResourceMethodDescriptor descriptor = RestLiArgumentBuilderTestHelper.getMockResourceMethodDescriptor(model, 1, null);
    RoutingResult routingResult = RestLiArgumentBuilderTestHelper.getMockRoutingResult(descriptor, 1, null, 0);
    RestLiArgumentBuilder argumentBuilder = new UpdateArgumentBuilder();
    try {
        argumentBuilder.extractRequestData(routingResult, request);
        fail("Expected RoutingException");
    } catch (RoutingException e) {
        assertTrue(e.getMessage().contains("Error parsing entity body"));
    }
    verify(request, model, descriptor, routingResult);
}
Also used : RoutingResult(com.linkedin.restli.internal.server.RoutingResult) RoutingException(com.linkedin.restli.server.RoutingException) RestRequest(com.linkedin.r2.message.rest.RestRequest) ResourceMethodDescriptor(com.linkedin.restli.internal.server.model.ResourceMethodDescriptor) ResourceModel(com.linkedin.restli.internal.server.model.ResourceModel) Test(org.testng.annotations.Test)

Aggregations

RestRequest (com.linkedin.r2.message.rest.RestRequest)293 Test (org.testng.annotations.Test)243 RestRequestBuilder (com.linkedin.r2.message.rest.RestRequestBuilder)174 RequestContext (com.linkedin.r2.message.RequestContext)154 RestResponse (com.linkedin.r2.message.rest.RestResponse)147 URI (java.net.URI)124 ByteString (com.linkedin.data.ByteString)65 RoutingResult (com.linkedin.restli.internal.server.RoutingResult)62 FutureCallback (com.linkedin.common.callback.FutureCallback)50 RestException (com.linkedin.r2.message.rest.RestException)49 ResourceMethodDescriptor (com.linkedin.restli.internal.server.model.ResourceMethodDescriptor)44 ExecutionException (java.util.concurrent.ExecutionException)43 StreamResponse (com.linkedin.r2.message.stream.StreamResponse)38 ResourceModel (com.linkedin.restli.internal.server.model.ResourceModel)36 HashMap (java.util.HashMap)35 StreamRequest (com.linkedin.r2.message.stream.StreamRequest)33 Map (java.util.Map)28 Callback (com.linkedin.common.callback.Callback)27 URISyntaxException (java.net.URISyntaxException)25 ResourceContext (com.linkedin.restli.server.ResourceContext)24