use of com.linkedin.restli.internal.server.model.ResourceModel in project rest.li by linkedin.
the class ArgumentBuilder method parseEntityStringKey.
/**
* Parses the provided string key value and returns its corresponding typed key instance. This method should only be
* used to parse keys which appear in the request body.
*
* @param stringKey Key string from the entity body
* @param routingResult {@link RoutingResult} instance for the current request
* @param version {@link ProtocolVersion} instance of the current request
* @return An instance of key's corresponding type
*/
static Object parseEntityStringKey(final String stringKey, final RoutingResult routingResult, final ProtocolVersion version) {
ResourceModel resourceModel = routingResult.getResourceMethod().getResourceModel();
ResourceContext resourceContext = routingResult.getContext();
try {
Key primaryKey = resourceModel.getPrimaryKey();
String altKeyName = resourceContext.getParameter(RestConstants.ALT_KEY_PARAM);
if (altKeyName != null) {
return ArgumentUtils.translateFromAlternativeKey(ArgumentUtils.parseAlternativeKey(stringKey, altKeyName, resourceModel, version), altKeyName, resourceModel);
} else if (ComplexResourceKey.class.equals(primaryKey.getType())) {
return ComplexResourceKey.parseString(stringKey, resourceModel.getKeyKeyClass(), resourceModel.getKeyParamsClass(), version);
} else if (CompoundKey.class.equals(primaryKey.getType())) {
return ArgumentUtils.parseCompoundKey(stringKey, resourceModel.getKeys(), version);
} else {
// The conversion of simple keys doesn't include URL decoding as the current version of Rest.li clients don't
// encode simple keys which appear in the request body for BATCH UPDATE and BATCH PATCH requests.
Key key = resourceModel.getPrimaryKey();
return ArgumentUtils.convertSimpleValue(stringKey, key.getDataSchema(), key.getType());
}
} catch (InvalidAlternativeKeyException | AlternativeKeyCoercerException | PathSegment.PathSegmentSyntaxException | IllegalArgumentException e) {
throw new RoutingException(String.format("Invalid key: '%s'", stringKey), HttpStatus.S_400_BAD_REQUEST.getCode());
}
}
use of com.linkedin.restli.internal.server.model.ResourceModel 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);
}
use of com.linkedin.restli.internal.server.model.ResourceModel in project rest.li by linkedin.
the class TestRestLiMethodInvocation method testExecutionReport.
@Test
public void testExecutionReport() throws RestLiSyntaxException, URISyntaxException {
Map<String, ResourceModel> resourceModelMap = buildResourceModels(StatusCollectionResource.class, AsyncStatusCollectionResource.class, PromiseStatusCollectionResource.class, TaskStatusCollectionResource.class);
ResourceModel statusResourceModel = resourceModelMap.get("/statuses");
ResourceModel asyncStatusResourceModel = resourceModelMap.get("/asyncstatuses");
ResourceModel promiseStatusResourceModel = resourceModelMap.get("/promisestatuses");
ResourceModel taskStatusResourceModel = resourceModelMap.get("/taskstatuses");
ResourceMethodDescriptor methodDescriptor;
StatusCollectionResource statusResource;
AsyncStatusCollectionResource asyncStatusResource;
PromiseStatusCollectionResource promiseStatusResource;
TaskStatusCollectionResource taskStatusResource;
// #1: Sync Method Execution
methodDescriptor = statusResourceModel.findMethod(ResourceMethod.GET);
statusResource = getMockResource(StatusCollectionResource.class);
EasyMock.expect(statusResource.get(eq(1L))).andReturn(null).once();
checkInvocation(statusResource, methodDescriptor, "GET", version, "/statuses/1", null, buildPathKeys("statusID", 1L), new RequestExecutionCallback<RestResponse>() {
//A 404 is considered an error by rest.li
@Override
public void onError(Throwable e, RequestExecutionReport executionReport, RestLiAttachmentReader requestAttachmentReader, RestLiResponseAttachments responseAttachments) {
Assert.assertNull(executionReport.getParseqTrace(), "There should be no parseq trace!");
}
@Override
public void onSuccess(RestResponse result, RequestExecutionReport executionReport, RestLiResponseAttachments responseAttachments) {
Assert.fail("Request failed unexpectedly.");
}
}, true, false);
// #2: Callback based Async Method Execution
Capture<RequestExecutionReport> requestExecutionReportCapture = new Capture<RequestExecutionReport>();
RestLiCallback<?> callback = getCallback(requestExecutionReportCapture);
methodDescriptor = asyncStatusResourceModel.findMethod(ResourceMethod.GET);
asyncStatusResource = getMockResource(AsyncStatusCollectionResource.class);
asyncStatusResource.get(eq(1L), EasyMock.<Callback<Status>>anyObject());
EasyMock.expectLastCall().andAnswer(new IAnswer<Object>() {
@Override
public Object answer() throws Throwable {
@SuppressWarnings("unchecked") Callback<Status> callback = (Callback<Status>) EasyMock.getCurrentArguments()[1];
callback.onSuccess(null);
return null;
}
});
EasyMock.replay(asyncStatusResource);
checkAsyncInvocation(asyncStatusResource, callback, methodDescriptor, "GET", version, "/asyncstatuses/1", null, buildPathKeys("statusID", 1L), true);
Assert.assertNull(requestExecutionReportCapture.getValue().getParseqTrace());
// #3: Promise based Async Method Execution
methodDescriptor = promiseStatusResourceModel.findMethod(ResourceMethod.GET);
promiseStatusResource = getMockResource(PromiseStatusCollectionResource.class);
EasyMock.expect(promiseStatusResource.get(eq(1L))).andReturn(Promises.<Status>value(null)).once();
checkInvocation(promiseStatusResource, methodDescriptor, "GET", version, "/promisestatuses/1", null, buildPathKeys("statusID", 1L), new RequestExecutionCallback<RestResponse>() {
//A 404 is considered an error by rest.li
@Override
public void onError(Throwable e, RequestExecutionReport executionReport, RestLiAttachmentReader requestAttachmentReader, RestLiResponseAttachments responseAttachments) {
Assert.assertNotNull(executionReport.getParseqTrace(), "There should be a valid parseq trace!");
}
@Override
public void onSuccess(RestResponse result, RequestExecutionReport executionReport, RestLiResponseAttachments responseAttachments) {
Assert.fail("Request failed unexpectedly.");
}
}, true, false);
// #4: Task based Async Method Execution
methodDescriptor = taskStatusResourceModel.findMethod(ResourceMethod.GET);
taskStatusResource = getMockResource(TaskStatusCollectionResource.class);
EasyMock.expect(taskStatusResource.get(eq(1L))).andReturn(Task.callable("myTask", new Callable<Status>() {
@Override
public Status call() throws Exception {
return new Status();
}
})).once();
checkInvocation(taskStatusResource, methodDescriptor, "GET", version, "/taskstatuses/1", null, buildPathKeys("statusID", 1L), new RequestExecutionCallback<RestResponse>() {
@Override
public void onError(Throwable e, RequestExecutionReport executionReport, RestLiAttachmentReader requestAttachmentReader, RestLiResponseAttachments responseAttachments) {
Assert.fail("Request failed unexpectedly.");
}
@Override
public void onSuccess(RestResponse result, RequestExecutionReport executionReport, RestLiResponseAttachments responseAttachments) {
Assert.assertNotNull(executionReport.getParseqTrace());
}
}, true, false);
}
use of com.linkedin.restli.internal.server.model.ResourceModel in project rest.li by linkedin.
the class TestRestLiResponseHandler method buildRoutingResult.
private final RoutingResult buildRoutingResult(ResourceMethod resourceMethod, RestRequest request, Map<String, String> acceptHeaders) throws SecurityException, NoSuchMethodException, RestLiSyntaxException {
Method method = ProjectionTestFixture.class.getMethod("batchGet", Set.class);
ResourceModel model = RestLiTestHelper.buildResourceModel(StatusCollectionResource.class);
ResourceMethodDescriptor methodDescriptor = ResourceMethodDescriptor.createForRestful(resourceMethod, method, InterfaceType.SYNC);
model.addResourceMethodDescriptor(methodDescriptor);
ServerResourceContext context = new ResourceContextImpl(new PathKeysImpl(), request, new RequestContext());
RestUtils.validateRequestHeadersAndUpdateResourceContext(acceptHeaders, context);
return new RoutingResult(context, methodDescriptor);
}
use of com.linkedin.restli.internal.server.model.ResourceModel in project rest.li by linkedin.
the class TestPhotoResource method initResource.
// function annotated with @BeforeMethod will run once before each test starts
@BeforeMethod
public void initResource() {
// the photo resource requires dependency injection to work
// we use InjectResourceFactory from pegasus to manually inject the dependency
SimpleBeanProvider beanProvider = new SimpleBeanProvider();
final PhotoDatabase photoDb = new PhotoDatabaseImpl(10);
beanProvider.add("photoDb", photoDb);
beanProvider.add("albumDb", new AlbumDatabaseImpl(10));
beanProvider.add("albumEntryDb", new AlbumEntryDatabaseImpl(photoDb, 3));
final InjectResourceFactory factory = new InjectResourceFactory(beanProvider);
final Map<String, ResourceModel> pathRootResourceMap = buildResourceModels(PhotoResource.class);
factory.setRootResources(pathRootResourceMap);
_res = factory.create(PhotoResource.class);
Assert.assertNotNull(_res);
Assert.assertNotNull(_res.getDb());
}
Aggregations