use of com.linkedin.restli.internal.server.model.Parameter in project rest.li by linkedin.
the class RestUtils method buildMetadata.
public static CollectionMetadata buildMetadata(final URI requestUri, final ResourceContext resourceContext, final ResourceMethodDescriptor methodDescriptor, final List<?> resultElements, final PageIncrement pageIncrement, final Integer totalResults) {
CollectionMetadata metadata = new CollectionMetadata();
List<Parameter<?>> pagingContextParams = methodDescriptor.getParametersWithType(Parameter.ParamType.PAGING_CONTEXT_PARAM);
PagingContext defaultPagingContext = pagingContextParams.isEmpty() ? null : (PagingContext) pagingContextParams.get(0).getDefaultValue();
PagingContext pagingContext = getPagingContext(resourceContext, defaultPagingContext);
metadata.setCount(pagingContext.getCount());
metadata.setStart(pagingContext.getStart());
if (totalResults != null) {
metadata.setTotal(totalResults);
} else {
metadata.removeTotal();
}
LinkArray links = new LinkArray();
String bestEncoding = pickBestEncoding(resourceContext.getRequestHeaders().get(RestConstants.HEADER_ACCEPT), Collections.emptySet());
if (pagingContext.getCount() > 0) {
// prev link
if (pagingContext.getStart() > 0) {
int prevStart = Math.max(0, pagingContext.getStart() - pagingContext.getCount());
String prevUri = buildPaginatedUri(requestUri, prevStart, pagingContext.getCount());
Link prevLink = new Link();
prevLink.setRel("prev");
prevLink.setHref(prevUri);
prevLink.setType(bestEncoding);
links.add(prevLink);
}
// next link if there are more results, or we returned a full page
Integer nextStart = getNextPageStart(resultElements.size(), totalResults, pagingContext, pageIncrement);
if (nextStart != null) {
// R2 doesn't expose host/port => can't build absolute URI (this is ok, as
// relative URIs internally
String nextUri = buildPaginatedUri(requestUri, nextStart, pagingContext.getCount());
Link nextLink = new Link();
nextLink.setRel("next");
nextLink.setHref(nextUri);
nextLink.setType(bestEncoding);
links.add(nextLink);
}
}
// even when we are getting count = 0, we should honor that links
// is a required field in the CollectionMetadata record
metadata.setLinks(links);
return metadata;
}
use of com.linkedin.restli.internal.server.model.Parameter in project rest.li by linkedin.
the class TestRestLiResourceModels method testCustomCrudParams.
@Test
public void testCustomCrudParams() throws Exception {
ResourceModel model = buildResourceModel(CombinedResources.CollectionWithCustomCrudParams.class);
checkCollectionModel(model, "test", String.class, "testId", Foo.class, false, CombinedResources.CollectionWithCustomCrudParams.class);
checkEntityModel(model, String.class, "testId", Foo.class, Collections.<String, Class<?>>emptyMap());
ResourceMethod[] crudMethods = { ResourceMethod.GET, ResourceMethod.CREATE, ResourceMethod.UPDATE, ResourceMethod.PARTIAL_UPDATE, ResourceMethod.DELETE, ResourceMethod.BATCH_GET, ResourceMethod.BATCH_CREATE, ResourceMethod.BATCH_DELETE, ResourceMethod.BATCH_UPDATE, ResourceMethod.BATCH_PARTIAL_UPDATE };
assertHasMethods(model, crudMethods);
for (ResourceMethod method : crudMethods) {
ResourceMethodDescriptor descriptor = model.findMethod(method);
List<Parameter<?>> params = descriptor.getParameters();
boolean foundIntParam = false;
boolean foundStringParam = false;
for (Parameter<?> param : params) {
if (param.getName().equals("intParam")) {
foundIntParam = true;
Assert.assertEquals(param.getType(), int.class);
Assert.assertTrue(param.isOptional());
Assert.assertEquals(param.getDefaultValue(), Integer.valueOf(42));
} else if (param.getName().equals("stringParam")) {
foundStringParam = true;
Assert.assertEquals(param.getType(), String.class);
Assert.assertFalse(param.isOptional());
Assert.assertFalse(param.hasDefaultValue());
}
}
Assert.assertTrue(foundIntParam);
Assert.assertTrue(foundStringParam);
}
}
use of com.linkedin.restli.internal.server.model.Parameter in project rest.li by linkedin.
the class TestRestLiResourceModels method testCollectionUnstructuredDataResource.
@Test
public void testCollectionUnstructuredDataResource() throws Exception {
ResourceModel resourceModel = buildResourceModel(FeedDownloadResource.class);
assertEquals(resourceModel.getResourceType(), ResourceType.COLLECTION);
assertEquals(resourceModel.getResourceMethodDescriptors().size(), 1);
final ResourceMethodDescriptor getMethod = resourceModel.findMethod(ResourceMethod.GET);
assertNotNull(getMethod);
List<Parameter<?>> parameters = getMethod.getParameters();
Parameter<?> firstParam = parameters.get(0);
assertNotNull(firstParam);
assertEquals(firstParam.getName(), "feedId");
assertEquals(firstParam.getType(), Long.class);
assertFalse(firstParam.isOptional());
assertFalse(firstParam.hasDefaultValue());
assertNull(firstParam.getDefaultValue());
Parameter<?> secondParam = parameters.get(1);
assertNotNull(secondParam);
assertEquals(secondParam.getName(), "RestLi Unstructured Data Writer");
assertEquals(secondParam.getType(), UnstructuredDataWriter.class);
assertFalse(secondParam.isOptional());
assertFalse(secondParam.hasDefaultValue());
assertNull(secondParam.getDefaultValue());
assertEquals(resourceModel.getResourceEntityType(), ResourceEntityType.UNSTRUCTURED_DATA);
}
use of com.linkedin.restli.internal.server.model.Parameter in project rest.li by linkedin.
the class TestArgumentBuilder method testRestLiAttachmentsParam.
@Test
public void testRestLiAttachmentsParam() {
ServerResourceContext mockResourceContext = EasyMock.createMock(ServerResourceContext.class);
final RestLiAttachmentReader restLiAttachmentReader = new RestLiAttachmentReader(null);
EasyMock.expect(mockResourceContext.getRequestAttachmentReader()).andReturn(restLiAttachmentReader);
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.singletonList(param);
Object[] results = ArgumentBuilder.buildArgs(new Object[0], getMockResourceMethod(parameters), mockResourceContext, null, getMockResourceMethodConfig(false));
Assert.assertEquals(results[0], restLiAttachmentReader);
}
use of com.linkedin.restli.internal.server.model.Parameter in project rest.li by linkedin.
the class TestArgumentBuilder method testPostParameterType.
@Test
public void testPostParameterType() {
String testParamKey = "testParam";
String expectedTestParamValue = "testParamValue";
ServerResourceContext mockResourceContext = EasyMock.createMock(ServerResourceContext.class);
DataMap entityBody = new DataMap();
entityBody.put(testParamKey, expectedTestParamValue);
DynamicRecordTemplate template = new DynamicRecordTemplate(entityBody, null);
Parameter<String> param = new Parameter<>(testParamKey, String.class, DataSchemaConstants.STRING_DATA_SCHEMA, false, null, Parameter.ParamType.POST, false, AnnotationSet.EMPTY);
List<Parameter<?>> parameters = Collections.singletonList(param);
EasyMock.expect(mockResourceContext.getRequestAttachmentReader()).andReturn(null);
Object[] results = ArgumentBuilder.buildArgs(new Object[0], getMockResourceMethod(parameters), mockResourceContext, template, getMockResourceMethodConfig(false));
Assert.assertEquals(results[0], expectedTestParamValue);
}
Aggregations