use of com.linkedin.restli.internal.server.methods.DefaultMethodAdapterProvider in project rest.li by linkedin.
the class TestCustomMethodAdapterProvider method initClass.
@BeforeClass
public void initClass() throws Exception {
RestLiConfig config = new RestLiConfig();
config.setMethodAdapterProvider(new DefaultMethodAdapterProvider(new ErrorResponseBuilder(ErrorResponseFormat.MESSAGE_AND_SERVICECODE)) {
@Override
public RestLiArgumentBuilder getArgumentBuilder(ResourceMethod resourceMethod) {
// Override the behavior of the CREATE argument builder
if (resourceMethod == ResourceMethod.CREATE) {
return new CreateArgumentBuilder() {
@Override
public RestLiRequestData extractRequestData(RoutingResult routingResult, DataMap dataMap) {
// Always use the FRIENDLY record regardless of the actual data
return new RestLiRequestDataImpl.Builder().entity(FRIENDLY).build();
}
};
} else {
return super.getArgumentBuilder(resourceMethod);
}
}
});
super.init(false, config);
}
use of com.linkedin.restli.internal.server.methods.DefaultMethodAdapterProvider in project rest.li by linkedin.
the class TestResponseMetadata method testMetadata.
@Test(dataProvider = "dataProvider")
public void testMetadata(ResourceMethod resourceMethod, Object responseObject, boolean hasEntity, String responseString) throws Exception {
final RestRequest mockRequest = mock(RestRequest.class);
final RoutingResult mockRoutingResult = mock(RoutingResult.class);
final ResourceMethodDescriptor mockResourceMethodDescriptor = mock(ResourceMethodDescriptor.class);
final ServerResourceContext mockContext = mock(ServerResourceContext.class);
final ProtocolVersion mockProtocolVersion = AllProtocolVersions.LATEST_PROTOCOL_VERSION;
final URI mockURI = new URI("http://fake.com");
when(mockRequest.getURI()).thenReturn(mockURI);
when(mockResourceMethodDescriptor.getMethodType()).thenReturn(resourceMethod);
when(mockRoutingResult.getResourceMethod()).thenReturn(mockResourceMethodDescriptor);
when(mockResourceMethodDescriptor.getType()).thenReturn(resourceMethod);
when(mockRoutingResult.getContext()).thenReturn(mockContext);
when(mockContext.getRestliProtocolVersion()).thenReturn(mockProtocolVersion);
when(mockContext.getRawRequestContext()).thenReturn(new RequestContext());
final ErrorResponseBuilder errorResponseBuilder = new ErrorResponseBuilder();
final RestLiResponseHandler responseHandler = new RestLiResponseHandler(new DefaultMethodAdapterProvider(errorResponseBuilder), errorResponseBuilder);
// Test success path
RestLiResponseData<?> responseData = responseHandler.buildRestLiResponseData(mockRequest, mockRoutingResult, responseObject);
responseData.getResponseEnvelope().getResponseMetadata().put(TEST_META_DATA_ELEMENT_KEY, TEST_META_DATA_ELEMENT);
RestLiResponse response = responseHandler.buildPartialResponse(mockRoutingResult, responseData);
assertEquals(response.getEntity() != null, hasEntity);
if (hasEntity) {
if (response.getEntity() instanceof IdResponse) {
assertNotNull(((IdResponse) response.getEntity()).getId());
} else {
assertEquals(response.getEntity().data().get(RestConstants.METADATA_RESERVED_FIELD), TEST_META_DATA);
assertEquals(response.getEntity().toString(), responseString);
}
}
// Verify metadata is cleared when an exception is set by a filter
responseData.getResponseEnvelope().setExceptionInternal(new RestLiServiceException(HttpStatus.S_500_INTERNAL_SERVER_ERROR));
assertEquals(responseData.getResponseEnvelope().getResponseMetadata().size(), 0);
RestLiResponse errorResponse = responseHandler.buildPartialResponse(mockRoutingResult, responseData);
assertNull(errorResponse.getEntity().data().get(RestConstants.METADATA_RESERVED_FIELD));
// Test case where resource method returns exception path
RestLiResponseData<?> errorResponseData = responseHandler.buildExceptionResponseData(mockRoutingResult, new RestLiServiceException(HttpStatus.S_500_INTERNAL_SERVER_ERROR), new HashMap<>(), new ArrayList<>());
responseData.getResponseEnvelope().getResponseMetadata().put(TEST_META_DATA_ELEMENT_KEY, TEST_META_DATA_ELEMENT);
errorResponse = responseHandler.buildPartialResponse(mockRoutingResult, responseData);
assertNull(errorResponse.getEntity().data().get(RestConstants.METADATA_RESERVED_FIELD));
}
use of com.linkedin.restli.internal.server.methods.DefaultMethodAdapterProvider in project rest.li by linkedin.
the class TestRestLiMethodInvocation method setUp.
@BeforeTest
protected void setUp() {
_scheduler = Executors.newSingleThreadScheduledExecutor();
_engine = new EngineBuilder().setTaskExecutor(_scheduler).setTimerScheduler(_scheduler).build();
_resourceFactory = new EasyMockResourceFactory();
_errorResponseBuilder = new ErrorResponseBuilder();
_methodAdapterProvider = new DefaultMethodAdapterProvider(_errorResponseBuilder);
// Add filters to the invoker.
_invoker = new RestLiMethodInvoker(_resourceFactory, _engine, ErrorResponseBuilder.DEFAULT_INTERNAL_ERROR_MESSAGE);
}
Aggregations