Search in sources :

Example 21 with Parameter

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;
}
Also used : CollectionMetadata(com.linkedin.restli.common.CollectionMetadata) PagingContext(com.linkedin.restli.server.PagingContext) LinkArray(com.linkedin.restli.common.LinkArray) Parameter(com.linkedin.restli.internal.server.model.Parameter) Link(com.linkedin.restli.common.Link)

Example 22 with Parameter

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);
    }
}
Also used : CombinedResources(com.linkedin.restli.server.combined.CombinedResources) ResourceMethodDescriptor(com.linkedin.restli.internal.server.model.ResourceMethodDescriptor) ResourceModel(com.linkedin.restli.internal.server.model.ResourceModel) RestLiTestHelper.buildResourceModel(com.linkedin.restli.server.test.RestLiTestHelper.buildResourceModel) Parameter(com.linkedin.restli.internal.server.model.Parameter) ResourceMethod(com.linkedin.restli.common.ResourceMethod) Test(org.testng.annotations.Test)

Example 23 with Parameter

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);
}
Also used : ResourceMethodDescriptor(com.linkedin.restli.internal.server.model.ResourceMethodDescriptor) ResourceModel(com.linkedin.restli.internal.server.model.ResourceModel) RestLiTestHelper.buildResourceModel(com.linkedin.restli.server.test.RestLiTestHelper.buildResourceModel) Parameter(com.linkedin.restli.internal.server.model.Parameter) Test(org.testng.annotations.Test)

Example 24 with Parameter

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);
}
Also used : ServerResourceContext(com.linkedin.restli.internal.server.ServerResourceContext) Parameter(com.linkedin.restli.internal.server.model.Parameter) RestLiAttachmentReader(com.linkedin.restli.common.attachments.RestLiAttachmentReader) Test(org.testng.annotations.Test)

Example 25 with Parameter

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);
}
Also used : DynamicRecordTemplate(com.linkedin.data.template.DynamicRecordTemplate) ServerResourceContext(com.linkedin.restli.internal.server.ServerResourceContext) Parameter(com.linkedin.restli.internal.server.model.Parameter) DataMap(com.linkedin.data.DataMap) Test(org.testng.annotations.Test)

Aggregations

Parameter (com.linkedin.restli.internal.server.model.Parameter)42 ServerResourceContext (com.linkedin.restli.internal.server.ServerResourceContext)29 Test (org.testng.annotations.Test)29 ResourceMethodDescriptor (com.linkedin.restli.internal.server.model.ResourceMethodDescriptor)17 AnnotationSet (com.linkedin.restli.internal.server.model.AnnotationSet)14 ArrayList (java.util.ArrayList)13 RoutingResult (com.linkedin.restli.internal.server.RoutingResult)12 ResourceModel (com.linkedin.restli.internal.server.model.ResourceModel)11 RestRequest (com.linkedin.r2.message.rest.RestRequest)10 RestLiRequestData (com.linkedin.restli.server.RestLiRequestData)10 Annotation (java.lang.annotation.Annotation)10 DataMap (com.linkedin.data.DataMap)9 MyComplexKey (com.linkedin.restli.common.test.MyComplexKey)8 PagingContext (com.linkedin.restli.server.PagingContext)6 Callback (com.linkedin.common.callback.Callback)5 IntegerDataSchema (com.linkedin.data.schema.IntegerDataSchema)5 ComplexResourceKey (com.linkedin.restli.common.ComplexResourceKey)5 CompoundKey (com.linkedin.restli.common.CompoundKey)5 Key (com.linkedin.restli.server.Key)5 HashMap (java.util.HashMap)5