Search in sources :

Example 16 with AnnotationSet

use of com.linkedin.restli.internal.server.model.AnnotationSet in project rest.li by linkedin.

the class TestBatchPatchArgumentBuilder method testArgumentBuilderSuccess.

@Test(dataProvider = "argumentData")
public void testArgumentBuilderSuccess(ProtocolVersion version, Key primaryKey, Key[] associationKeys, String requestEntity, Object[] keys, PatchRequest<MyComplexKey>[] patches) {
    Set<Object> batchKeys = new HashSet<Object>(Arrays.asList(keys));
    RestRequest request = RestLiArgumentBuilderTestHelper.getMockRequest(requestEntity, version);
    ResourceModel model = RestLiArgumentBuilderTestHelper.getMockResourceModel(MyComplexKey.class, primaryKey, associationKeys, batchKeys);
    @SuppressWarnings("rawtypes") Parameter<BatchPatchRequest> param = new Parameter<BatchPatchRequest>("", BatchPatchRequest.class, null, false, null, Parameter.ParamType.BATCH, false, new AnnotationSet(new Annotation[] {}));
    ResourceMethodDescriptor descriptor = RestLiArgumentBuilderTestHelper.getMockResourceMethodDescriptor(model, 2, Collections.singletonList(param));
    ResourceContext context = RestLiArgumentBuilderTestHelper.getMockResourceContext(batchKeys, true, false);
    RoutingResult routingResult = RestLiArgumentBuilderTestHelper.getMockRoutingResult(descriptor, context);
    RestLiArgumentBuilder argumentBuilder = new BatchPatchArgumentBuilder();
    RestLiRequestData requestData = argumentBuilder.extractRequestData(routingResult, request);
    Object[] args = argumentBuilder.buildArguments(requestData, routingResult);
    assertEquals(args.length, 1);
    assertTrue(args[0] instanceof BatchPatchRequest);
    Map<?, ?> data = ((BatchPatchRequest) args[0]).getData();
    assertEquals(data.size(), keys.length);
    for (int i = 0; i < keys.length; i++) {
        assertEquals(data.get(keys[i]), patches[i]);
    }
    verify(request, descriptor, context, routingResult);
}
Also used : ResourceContext(com.linkedin.restli.server.ResourceContext) ServerResourceContext(com.linkedin.restli.internal.server.ServerResourceContext) BatchPatchRequest(com.linkedin.restli.server.BatchPatchRequest) ResourceMethodDescriptor(com.linkedin.restli.internal.server.model.ResourceMethodDescriptor) AnnotationSet(com.linkedin.restli.internal.server.model.AnnotationSet) Annotation(java.lang.annotation.Annotation) RoutingResult(com.linkedin.restli.internal.server.RoutingResult) RestRequest(com.linkedin.r2.message.rest.RestRequest) ResourceModel(com.linkedin.restli.internal.server.model.ResourceModel) Parameter(com.linkedin.restli.internal.server.model.Parameter) HashSet(java.util.HashSet) RestLiRequestData(com.linkedin.restli.server.RestLiRequestData) Test(org.testng.annotations.Test)

Example 17 with AnnotationSet

use of com.linkedin.restli.internal.server.model.AnnotationSet in project rest.li by linkedin.

the class Jsr330Adapter method scanInjectableConstructors.

private void scanInjectableConstructors(Class<?> beanClazz) {
    int annotatedConstructors = 0;
    for (Constructor<?> constructor : beanClazz.getConstructors()) {
        Inject injectAnnotation = constructor.getAnnotation(Inject.class);
        if (injectAnnotation != null) {
            ++annotatedConstructors;
            if (annotatedConstructors > 1) {
                throw new RestLiInternalException("Found multiple constructors annotated with @Inject in " + "class '" + beanClazz.getCanonicalName() + "'.  At most one constructor can be annotated " + "with @Inject.");
            }
            Class<?>[] parameters = constructor.getParameterTypes();
            Annotation[][] parameterAnnotations = constructor.getParameterAnnotations();
            List<DependencyDecl> parameterDecls = new ArrayList<DependencyDecl>(parameters.length);
            for (int i = 0; i < parameters.length; ++i) {
                Class<?> parameter = parameters[i];
                AnnotationSet annotations = new AnnotationSet(parameterAnnotations[i]);
                Named namedAnno = annotations.get(Named.class);
                parameterDecls.add(new DependencyDecl(parameter, namedAnno != null ? namedAnno.value() : null));
            }
            constructor.setAccessible(true);
            _constructorParameterDependencies.put(beanClazz, new InjectableConstructor(constructor, parameterDecls));
        }
    }
    if (annotatedConstructors == 0) {
        try {
            Constructor<?> defaultConstructor = beanClazz.getConstructor();
            defaultConstructor.setAccessible(true);
            _constructorParameterDependencies.put(beanClazz, new InjectableConstructor(defaultConstructor, Collections.<DependencyDecl>emptyList()));
        } catch (NoSuchMethodException e) {
            throw new RestLiInternalException(String.format("No injectable constructor defined for class %s.  Classes must define" + " either a default constructor or a constructor annotated " + "with @Inject.", beanClazz.getName()), e);
        }
    }
}
Also used : Inject(javax.inject.Inject) Named(javax.inject.Named) ArrayList(java.util.ArrayList) AnnotationSet(com.linkedin.restli.internal.server.model.AnnotationSet) RestLiInternalException(com.linkedin.restli.internal.server.RestLiInternalException)

Aggregations

AnnotationSet (com.linkedin.restli.internal.server.model.AnnotationSet)17 Parameter (com.linkedin.restli.internal.server.model.Parameter)15 Annotation (java.lang.annotation.Annotation)10 ResourceContext (com.linkedin.restli.server.ResourceContext)9 Test (org.testng.annotations.Test)9 RestRequest (com.linkedin.r2.message.rest.RestRequest)8 RoutingResult (com.linkedin.restli.internal.server.RoutingResult)8 ResourceMethodDescriptor (com.linkedin.restli.internal.server.model.ResourceMethodDescriptor)8 RestLiRequestData (com.linkedin.restli.server.RestLiRequestData)8 ArrayList (java.util.ArrayList)8 MyComplexKey (com.linkedin.restli.common.test.MyComplexKey)7 ResourceModel (com.linkedin.restli.internal.server.model.ResourceModel)5 HashMap (java.util.HashMap)5 DataProvider (org.testng.annotations.DataProvider)5 IntegerDataSchema (com.linkedin.data.schema.IntegerDataSchema)4 ServerResourceContext (com.linkedin.restli.internal.server.ServerResourceContext)4 HashSet (java.util.HashSet)4 StringDataSchema (com.linkedin.data.schema.StringDataSchema)3 ComplexResourceKey (com.linkedin.restli.common.ComplexResourceKey)3 CompoundKey (com.linkedin.restli.common.CompoundKey)3