Search in sources :

Example 21 with ResourceMethod

use of com.linkedin.restli.common.ResourceMethod in project rest.li by linkedin.

the class TestCollectionResponseBuilder method testProjectionInBuildRestliResponseData.

@Test
@SuppressWarnings("unchecked")
public <D extends RestLiResponseData<? extends CollectionResponseEnvelope>> void testProjectionInBuildRestliResponseData() throws URISyntaxException {
    for (Map.Entry<ResourceMethod, CollectionResponseBuilder<?>> entry : BUILDERS.entrySet()) {
        ResourceMethod resourceMethod = entry.getKey();
        MaskTree maskTree = new MaskTree();
        maskTree.addOperation(new PathSpec("fruitsField"), MaskOperation.POSITIVE_MASK_OP);
        ServerResourceContext mockContext = getMockResourceContext(maskTree, null, null, ProjectionMode.AUTOMATIC, ProjectionMode.AUTOMATIC);
        RoutingResult routingResult = new RoutingResult(mockContext, getMockResourceMethodDescriptor());
        List<RecordTemplate> values = new ArrayList<>();
        Foo value = new Foo().setStringField("value").setFruitsField(Fruits.APPLE);
        values.add(value);
        CollectionResponseBuilder<D> responseBuilder = (CollectionResponseBuilder<D>) entry.getValue();
        D responseData = responseBuilder.buildRestLiResponseData(getRestRequest(), routingResult, values, Collections.emptyMap(), Collections.emptyList());
        RecordTemplate record = responseData.getResponseEnvelope().getCollectionResponse().get(0);
        Assert.assertEquals(record.data().size(), 1);
        Assert.assertEquals(record.data().get("fruitsField"), Fruits.APPLE.toString());
    }
}
Also used : Foo(com.linkedin.pegasus.generator.examples.Foo) ArrayList(java.util.ArrayList) PathSpec(com.linkedin.data.schema.PathSpec) RoutingResult(com.linkedin.restli.internal.server.RoutingResult) MaskTree(com.linkedin.data.transform.filter.request.MaskTree) ServerResourceContext(com.linkedin.restli.internal.server.ServerResourceContext) RecordTemplate(com.linkedin.data.template.RecordTemplate) HashMap(java.util.HashMap) DataMap(com.linkedin.data.DataMap) Map(java.util.Map) ResourceMethod(com.linkedin.restli.common.ResourceMethod) Test(org.testng.annotations.Test)

Example 22 with ResourceMethod

use of com.linkedin.restli.common.ResourceMethod in project rest.li by linkedin.

the class TestRestLiResponseData method provideAllBaseObjects.

@DataProvider(name = "baseClassOperations")
public Object[][] provideAllBaseObjects() {
    RestLiServiceException exception = new RestLiServiceException(HttpStatus.S_500_INTERNAL_SERVER_ERROR);
    ResourceMethod[] methods = ResourceMethod.class.getEnumConstants();
    Object[][] sampleResponseData = new Object[methods.length * 2][1];
    for (int i = 0; i < methods.length; i++) {
        RestLiResponseData<?> successResponseData = buildResponseData(methods[i], HttpStatus.S_200_OK);
        RestLiResponseData<?> errorResponseData = ErrorResponseBuilder.buildErrorResponseData(methods[i], exception, Collections.emptyMap(), Collections.emptyList());
        sampleResponseData[i * 2] = new Object[] { successResponseData };
        sampleResponseData[i * 2 + 1] = new Object[] { errorResponseData };
    }
    return sampleResponseData;
}
Also used : RestLiServiceException(com.linkedin.restli.server.RestLiServiceException) ResourceMethod(com.linkedin.restli.common.ResourceMethod) DataProvider(org.testng.annotations.DataProvider)

Example 23 with ResourceMethod

use of com.linkedin.restli.common.ResourceMethod in project rest.li by linkedin.

the class TestRestLiResponseHandler method buildRoutingResult.

private RoutingResult buildRoutingResult(ResourceMethod resourceMethod, RestRequest request, Map<String, String> acceptHeaders, Set<String> customTypes) 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, customTypes, context);
    return new RoutingResult(context, methodDescriptor);
}
Also used : RoutingResult(com.linkedin.restli.internal.server.RoutingResult) ServerResourceContext(com.linkedin.restli.internal.server.ServerResourceContext) PathKeysImpl(com.linkedin.restli.internal.server.PathKeysImpl) 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) ResourceMethod(com.linkedin.restli.common.ResourceMethod) Method(java.lang.reflect.Method) RequestContext(com.linkedin.r2.message.RequestContext) ResourceContextImpl(com.linkedin.restli.internal.server.ResourceContextImpl)

Example 24 with ResourceMethod

use of com.linkedin.restli.common.ResourceMethod in project rest.li by linkedin.

the class TestRequestBuilderSpecGenerator method testSimpleResource.

@Test
public void testSimpleResource() throws Exception {
    String idl = moduleDir + FS + IDLS_DIR + FS + "testSimple.restspec.json";
    Set<BuilderSpec> builderSpecs = generateBuilderSpec(new String[] { idl });
    Assert.assertNotNull(builderSpecs);
    Assert.assertTrue(builderSpecs.size() == 6);
    Map<String, String> methodMap = new HashMap<>();
    methodMap.put("get", "Gets the greeting.");
    methodMap.put("delete", "Deletes the greeting.");
    methodMap.put("update", "Updates the greeting.");
    for (BuilderSpec spec : builderSpecs) {
        Assert.assertTrue(spec instanceof RootBuilderSpec || spec instanceof RestMethodBuilderSpec);
        if (spec instanceof RootBuilderSpec) {
            Assert.assertTrue(spec instanceof SimpleRootBuilderSpec);
            SimpleRootBuilderSpec simpleSpec = (SimpleRootBuilderSpec) spec;
            if (simpleSpec.getResourcePath().indexOf('/') >= 0) {
                Assert.assertEquals(simpleSpec.getSourceIdlName(), idl);
                Assert.assertEquals(simpleSpec.getResourcePath(), "testSimple/testSimpleSub");
                Assert.assertNotNull(simpleSpec.getRestMethods());
                Assert.assertTrue(simpleSpec.getRestMethods().size() == 1);
                Assert.assertEquals("get", simpleSpec.getRestMethods().get(0).getName());
                Assert.assertTrue(simpleSpec.getResourceActions().isEmpty());
                Assert.assertTrue(simpleSpec.getSubresources().isEmpty());
            } else {
                Assert.assertTrue(simpleSpec.getResourceActions().isEmpty());
                Assert.assertTrue(simpleSpec.getSubresources().size() == 1);
                Assert.assertEquals(simpleSpec.getSourceIdlName(), idl);
                Assert.assertEquals(simpleSpec.getResourcePath(), "testSimple");
                Assert.assertNotNull(simpleSpec.getRestMethods());
                Assert.assertTrue(simpleSpec.getRestMethods().size() == 3);
                List<RootBuilderMethodSpec> restMethods = simpleSpec.getRestMethods();
                for (RootBuilderMethodSpec method : restMethods) {
                    Assert.assertTrue(method.getReturn() instanceof RestMethodBuilderSpec);
                    Assert.assertEquals(method.getReturn().getRootBuilderMethod(), method);
                    Assert.assertTrue(methodMap.containsKey(method.getName()));
                    Assert.assertEquals(methodMap.get(method.getName()), method.getDoc());
                }
            }
        } else if (spec instanceof RestMethodBuilderSpec) {
            RestMethodBuilderSpec builderSpec = (RestMethodBuilderSpec) spec;
            ResourceMethod method = builderSpec.getResourceMethod();
            Assert.assertTrue(methodMap.containsKey(method.toString()));
            Assert.assertFalse(builderSpec.hasBindingMethods());
        }
    }
}
Also used : HashMap(java.util.HashMap) RootBuilderMethodSpec(com.linkedin.restli.tools.clientgen.builderspec.RootBuilderMethodSpec) RestMethodBuilderSpec(com.linkedin.restli.tools.clientgen.builderspec.RestMethodBuilderSpec) RootBuilderSpec(com.linkedin.restli.tools.clientgen.builderspec.RootBuilderSpec) SimpleRootBuilderSpec(com.linkedin.restli.tools.clientgen.builderspec.SimpleRootBuilderSpec) ActionSetRootBuilderSpec(com.linkedin.restli.tools.clientgen.builderspec.ActionSetRootBuilderSpec) CollectionRootBuilderSpec(com.linkedin.restli.tools.clientgen.builderspec.CollectionRootBuilderSpec) BuilderSpec(com.linkedin.restli.tools.clientgen.builderspec.BuilderSpec) RestMethodBuilderSpec(com.linkedin.restli.tools.clientgen.builderspec.RestMethodBuilderSpec) RootBuilderSpec(com.linkedin.restli.tools.clientgen.builderspec.RootBuilderSpec) ActionBuilderSpec(com.linkedin.restli.tools.clientgen.builderspec.ActionBuilderSpec) SimpleRootBuilderSpec(com.linkedin.restli.tools.clientgen.builderspec.SimpleRootBuilderSpec) ActionSetRootBuilderSpec(com.linkedin.restli.tools.clientgen.builderspec.ActionSetRootBuilderSpec) FinderBuilderSpec(com.linkedin.restli.tools.clientgen.builderspec.FinderBuilderSpec) CollectionRootBuilderSpec(com.linkedin.restli.tools.clientgen.builderspec.CollectionRootBuilderSpec) SimpleRootBuilderSpec(com.linkedin.restli.tools.clientgen.builderspec.SimpleRootBuilderSpec) ResourceMethod(com.linkedin.restli.common.ResourceMethod) Test(org.testng.annotations.Test)

Example 25 with ResourceMethod

use of com.linkedin.restli.common.ResourceMethod in project rest.li by linkedin.

the class TestRequestBuilderSpecGenerator method generateBuilderSpec.

private Set<BuilderSpec> generateBuilderSpec(String[] sources) {
    final DataSchemaParser schemaParser = new DataSchemaParser.Builder(RESOLVER_DIR).build();
    final TemplateSpecGenerator specGenerator = new TemplateSpecGenerator(schemaParser.getSchemaResolver());
    final RestSpecParser parser = new RestSpecParser();
    final Map<ResourceMethod, String> builderBaseMap = new HashMap<>();
    builderBaseMap.put(ResourceMethod.GET, "GetRequestBuilder");
    builderBaseMap.put(ResourceMethod.DELETE, "DeleteRequestBuilder");
    builderBaseMap.put(ResourceMethod.UPDATE, "UpdateRequestBuilder");
    builderBaseMap.put(ResourceMethod.CREATE, "CreateIdRequestBuilder");
    builderBaseMap.put(ResourceMethod.PARTIAL_UPDATE, "PartialUpdateRequestBuilder");
    builderBaseMap.put(ResourceMethod.GET_ALL, "GetAllRequestBuilder");
    builderBaseMap.put(ResourceMethod.OPTIONS, "OptionsRequestBuilder");
    builderBaseMap.put(ResourceMethod.ACTION, "ActionRequestBuilder");
    builderBaseMap.put(ResourceMethod.FINDER, "FinderRequestBuilder");
    builderBaseMap.put(ResourceMethod.BATCH_GET, "BatchGetRequestBuilder");
    builderBaseMap.put(ResourceMethod.BATCH_UPDATE, "BatchUpdateRequestBuilder");
    builderBaseMap.put(ResourceMethod.BATCH_PARTIAL_UPDATE, "BatchPartialUpdateRequestBuilder");
    builderBaseMap.put(ResourceMethod.BATCH_DELETE, "BatchDeleteRequestBuilder");
    builderBaseMap.put(ResourceMethod.BATCH_CREATE, "BatchCreateIdRequestBuilder");
    final RequestBuilderSpecGenerator builderSpecGenerator = new RequestBuilderSpecGenerator(schemaParser.getSchemaResolver(), specGenerator, RestliVersion.RESTLI_2_0_0, builderBaseMap);
    // parse idl to ResourceSchemas
    final RestSpecParser.ParseResult parseResult = parser.parseSources(sources);
    // generate Builder specs from ResourceSchema
    for (CodeUtil.Pair<ResourceSchema, File> pair : parseResult.getSchemaAndFiles()) {
        builderSpecGenerator.generate(pair.first, pair.second);
    }
    return builderSpecGenerator.getBuilderSpec();
}
Also used : ResourceSchema(com.linkedin.restli.restspec.ResourceSchema) HashMap(java.util.HashMap) CodeUtil(com.linkedin.pegasus.generator.CodeUtil) DataSchemaParser(com.linkedin.pegasus.generator.DataSchemaParser) TemplateSpecGenerator(com.linkedin.pegasus.generator.TemplateSpecGenerator) File(java.io.File) ResourceMethod(com.linkedin.restli.common.ResourceMethod)

Aggregations

ResourceMethod (com.linkedin.restli.common.ResourceMethod)54 Test (org.testng.annotations.Test)19 HashMap (java.util.HashMap)18 ResourceSpecImpl (com.linkedin.restli.common.ResourceSpecImpl)13 DataMap (com.linkedin.data.DataMap)12 TestRecord (com.linkedin.restli.client.test.TestRecord)12 DynamicRecordMetadata (com.linkedin.data.template.DynamicRecordMetadata)10 ArrayList (java.util.ArrayList)8 Map (java.util.Map)8 ProtocolVersion (com.linkedin.restli.common.ProtocolVersion)6 RestLiServiceException (com.linkedin.restli.server.RestLiServiceException)6 ResourceMethodDescriptor (com.linkedin.restli.internal.server.model.ResourceMethodDescriptor)5 ValidationResult (com.linkedin.data.schema.validation.ValidationResult)4 RecordTemplate (com.linkedin.data.template.RecordTemplate)4 Foo (com.linkedin.pegasus.generator.examples.Foo)4 RoutingResult (com.linkedin.restli.internal.server.RoutingResult)4 ResourceSchemaArray (com.linkedin.restli.restspec.ResourceSchemaArray)4 JClass (com.sun.codemodel.JClass)4 JDefinedClass (com.sun.codemodel.JDefinedClass)4 ByteString (com.linkedin.data.ByteString)3