use of com.linkedin.r2.message.rest.RestRequestBuilder in project rest.li by linkedin.
the class TestCreateResponseBuilder method testReturnEntityException.
/**
* We want to ensure that trying to create a Rest.li response from an empty {@link CreateKVResponse} causes an exception.
*/
@Test
public void testReturnEntityException() throws URISyntaxException {
ServerResourceContext mockContext = EasyMock.createMock(ServerResourceContext.class);
EasyMock.expect(mockContext.isReturnEntityRequested()).andReturn(true);
EasyMock.expect(mockContext.getProjectionMask()).andReturn(null);
EasyMock.expect(mockContext.getProjectionMode()).andReturn(ProjectionMode.AUTOMATIC);
EasyMock.replay(mockContext);
RoutingResult routingResult = new RoutingResult(mockContext, null);
CreateKVResponse<Integer, Foo> createKVResponse = new CreateKVResponse<>(null, null);
try {
CreateResponseBuilder responseBuilder = new CreateResponseBuilder();
RestLiResponseData<CreateResponseEnvelope> envelope = responseBuilder.buildRestLiResponseData(new RestRequestBuilder(new URI("/foo")).build(), routingResult, createKVResponse, Collections.emptyMap(), Collections.emptyList());
Assert.fail("Attempting to build RestLi response data with a null entity here should cause an exception.");
} catch (RestLiServiceException e) {
Assert.assertEquals(e.getStatus(), HttpStatus.S_500_INTERNAL_SERVER_ERROR, "");
Assert.assertTrue(e.getMessage().contains("Unexpected null encountered. Entity is null inside of a CreateKVResponse"), "Invalid exception message: \"" + e.getMessage() + "\"");
}
}
use of com.linkedin.r2.message.rest.RestRequestBuilder in project rest.li by linkedin.
the class TestCreateResponseBuilder method testBuilder.
@Test(dataProvider = "testData")
public void testBuilder(ProtocolVersion protocolVersion, String uriString, 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<>(expectedId);
RestRequest restRequest = new RestRequestBuilder(new URI(uriString)).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<>(headers);
ResourceMethodDescriptor mockDescriptor = getMockResourceMethodDescriptor(alternativeKeyMap);
ServerResourceContext mockContext = getMockResourceContext(protocolVersion, altKeyName);
RoutingResult routingResult = new RoutingResult(mockContext, mockDescriptor);
CreateResponseBuilder createResponseBuilder = new CreateResponseBuilder();
RestLiResponseData<CreateResponseEnvelope> responseData = createResponseBuilder.buildRestLiResponseData(restRequest, routingResult, createResponse, headers, Collections.emptyList());
Assert.assertFalse(responseData.getResponseEnvelope().isGetAfterCreate());
RestLiResponse restLiResponse = 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(restLiResponse, expectedHeaders);
Assert.assertEquals(restLiResponse.getStatus(), HttpStatus.S_201_CREATED);
Assert.assertEquals(restLiResponse.getEntity(), expectedIdResponse);
}
use of com.linkedin.r2.message.rest.RestRequestBuilder 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.emptyMap(), Collections.emptyList());
Assert.assertTrue(envelope.getResponseEnvelope().isErrorResponse());
}
use of com.linkedin.r2.message.rest.RestRequestBuilder 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);
ServerResourceContext mockContext = getMockResourceContext(protocolVersion, null);
RoutingResult routingResult = new RoutingResult(mockContext, mockDescriptor);
CreateResponseBuilder createResponseBuilder = new CreateResponseBuilder();
try {
createResponseBuilder.buildRestLiResponseData(restRequest, routingResult, createResponse, headers, Collections.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: "));
}
}
use of com.linkedin.r2.message.rest.RestRequestBuilder in project rest.li by linkedin.
the class TestBatchCreateResponseBuilder method testBuilderExceptions.
@Test(dataProvider = "exceptionTestData")
public void testBuilderExceptions(Object result, String expectedErrorMessage) throws URISyntaxException {
Map<String, String> headers = ResponseBuilderUtil.getHeaders();
ResourceMethodDescriptor mockDescriptor = getMockResourceMethodDescriptor(null);
ServerResourceContext mockContext = getMockResourceContext(null);
RoutingResult routingResult = new RoutingResult(mockContext, mockDescriptor);
BatchCreateResponseBuilder responseBuilder = new BatchCreateResponseBuilder(null);
RestRequest request = new RestRequestBuilder(new URI("/foo")).build();
try {
responseBuilder.buildRestLiResponseData(request, routingResult, result, headers, Collections.emptyList());
Assert.fail("buildRestLiResponseData should have thrown an exception because of null elements");
} catch (RestLiServiceException e) {
Assert.assertTrue(e.getMessage().contains(expectedErrorMessage));
}
}
Aggregations