ApiConfigAnnotationReader implements ApiConfigSource { @Override public void loadEndpointMethods(ServiceContext serviceContext, Class<?> endpointClass, ApiClassConfig.MethodConfigMap methodConfigMap) throws ApiConfigException { try { readEndpointMethods(endpointClass, methodConfigMap); } catch (InvocationTargetException | NoSuchMethodException | IllegalAccessException e) { throw new ApiConfigException(e); } } ApiConfigAnnotationReader();  ApiConfigAnnotationReader(Map<String, Class<? extends Annotation>> annotationTypes); @Override void loadEndpointClass(ServiceContext serviceContext, Class<?> endpointClass,
      ApiConfig config); @Override void loadEndpointMethods(ServiceContext serviceContext, Class<?> endpointClass,
      ApiClassConfig.MethodConfigMap methodConfigMap); @Override boolean isStaticConfig(ApiConfig config);  }
@Test public void testEndpointWithNoPublicMethods() throws Exception { ApiConfig config = createConfig(Endpoint2.class); annotationReader.loadEndpointMethods(serviceContext, Endpoint2.class, config.getApiClassConfig().getMethods()); assertEquals(0, config.getApiClassConfig().getMethods().size()); }
@Test public void testDuplicateMethodEndpoint() throws Exception { ApiConfig config = createConfig(DuplicateMethodEndpoint.class); annotationReader.loadEndpointMethods(serviceContext, DuplicateMethodEndpoint.class, config.getApiClassConfig().getMethods()); ApiMethodConfig method1 = config.getApiClassConfig().getMethods().get(methodToEndpointMethod( DuplicateMethodEndpoint.class.getMethod("foo", String.class))); assertEquals("api.foos.fn1", method1.getName()); ApiMethodConfig method2 = config.getApiClassConfig().getMethods().get(methodToEndpointMethod( DuplicateMethodEndpoint.class.getMethod("foo", Integer.class))); assertEquals("api.foos.fn2", method2.getName()); }
@Test public void testSimpleOverloadEndpoint() throws Exception { ApiConfig config = createConfig(SimpleOverloadEndpoint.class); annotationReader.loadEndpointMethods(serviceContext, SimpleOverloadEndpoint.class, config.getApiClassConfig().getMethods()); ApiMethodConfig method1 = config.getApiClassConfig().getMethods().get(methodToEndpointMethod( SimpleOverloadEndpoint.class.getMethod("foo", Integer.class))); assertEquals("api.foos.fn", method1.getName()); ApiMethodConfig method2 = config.getApiClassConfig().getMethods().get(methodToEndpointMethod( SimpleOverloadEndpoint.class.getSuperclass().getMethod("foo", String.class))); assertEquals("api.foos.base", method2.getName()); }
@Test public void testSimpleCovarianceEndpoint() throws Exception { ApiConfig config = createConfig(SimpleCovarianceEndpoint.class); annotationReader.loadEndpointMethods(serviceContext, SimpleCovarianceEndpoint.class, config.getApiClassConfig().getMethods()); ApiMethodConfig method1 = config.getApiClassConfig().getMethods().get(methodToEndpointMethod( SimpleCovarianceEndpoint.class.getMethod("foo", String.class))); assertEquals("api.foos.fn", method1.getName()); ApiMethodConfig method2 = config.getApiClassConfig().getMethods().get(methodToEndpointMethod( SimpleCovarianceEndpoint.class.getSuperclass().getMethod("foo", Object.class))); assertEquals("api.foos.base", method2.getName()); }
@Test public void testSimpleContravarianceEndpoint() throws Exception { ApiConfig config = createConfig(SimpleContravarianceEndpoint.class); annotationReader.loadEndpointMethods(serviceContext, SimpleContravarianceEndpoint.class, config.getApiClassConfig().getMethods()); ApiMethodConfig method1 = config.getApiClassConfig().getMethods().get(methodToEndpointMethod( SimpleContravarianceEndpoint.class.getMethod("foo", Object.class))); assertEquals("api.foos.fn", method1.getName()); ApiMethodConfig method2 = config.getApiClassConfig().getMethods().get(methodToEndpointMethod( SimpleContravarianceEndpoint.class.getSuperclass().getMethod("foo", Number.class))); assertEquals("api.foos.base", method2.getName()); }
@Test public void testCollectionCovarianceEndpoint() throws Exception { ApiConfig config = createConfig(CollectionCovarianceEndpoint.class); annotationReader.loadEndpointMethods(serviceContext, CollectionCovarianceEndpoint.class, config.getApiClassConfig().getMethods()); ApiMethodConfig method1 = config.getApiClassConfig().getMethods().get(methodToEndpointMethod( CollectionCovarianceEndpoint.class.getMethod("foo", List.class))); assertEquals("api.foos.fn", method1.getName()); ApiMethodConfig method2 = config.getApiClassConfig().getMethods().get(methodToEndpointMethod( CollectionCovarianceEndpoint.class.getSuperclass().getMethod("foo", Collection.class))); assertEquals("api.foos.base", method2.getName()); }
@Test public void testFullySpecializedDateEndpoint() throws Exception { ApiConfig config = createConfig(RestfulResourceEndpointBase.FullySpecializedEndpoint.class); annotationReader.loadEndpointMethods(serviceContext, RestfulResourceEndpointBase.FullySpecializedEndpoint.class, config.getApiClassConfig().getMethods()); assertEquals(6, config.getApiClassConfig().getMethods().size()); }
@Test public void testGenericBasePartiallySpecializedEndpoint() throws Exception { ApiConfig config = createConfig(RestfulResourceEndpointBase.PartiallySpecializedEndpoint.class); annotationReader.loadEndpointMethods(serviceContext, RestfulResourceEndpointBase.PartiallySpecializedEndpoint.class, config.getApiClassConfig().getMethods()); assertEquals(6, config.getApiClassConfig().getMethods().size()); assertNull(config.getApiClassConfig().getMethods().get(methodToEndpointMethod( RestfulResourceEndpointBase.FullySpecializedEndpoint.class.getMethod("list")))); assertNotNull(config.getApiClassConfig().getMethods().get(methodToEndpointMethod( RestfulResourceEndpointBase.PartiallySpecializedEndpoint.class, RestfulResourceEndpointBase.PartiallySpecializedEndpoint.class.getMethod( "get", long.class)))); assertNotNull(config.getApiClassConfig().getMethods().get(methodToEndpointMethod( RestfulResourceEndpointBase.PartiallySpecializedEndpoint.class, RestfulResourceEndpointBase.class.getMethod("list")))); }
@Test public void testDeepGenericHierarchySuccessEndpoint() throws Exception { ApiConfig config = createConfig(DeepGenericHierarchySuccessEndpoint.class); annotationReader.loadEndpointMethods(serviceContext, DeepGenericHierarchySuccessEndpoint.class, config.getApiClassConfig().getMethods()); assertEquals(1, config.getApiClassConfig().getMethods().size()); assertNotNull(config.getApiClassConfig().getMethods().get(methodToEndpointMethod( DeepGenericHierarchySuccessEndpoint.class.getMethod( "foo", String.class, Integer.class, Boolean.class)))); }
@Test public void testBoundedGenericEndpoint() throws Exception { ApiConfig config = createConfig(BoundedGenericEndpoint.class); annotationReader.loadEndpointMethods(serviceContext, BoundedGenericEndpoint.class, config.getApiClassConfig().getMethods()); assertEquals(1, config.getApiClassConfig().getMethods().size()); }
@Test public void testMethodDescription() throws Exception { @Api final class MethodDescriptionEndpoint { public void noAnnotation() {} @ApiMethod public void noDescription() {} @ApiMethod(description = "description") public void withDescription() {} } ApiConfig config = createConfig(MethodDescriptionEndpoint.class); annotationReader.loadEndpointMethods(serviceContext, MethodDescriptionEndpoint.class, config.getApiClassConfig().getMethods()); ApiMethodConfig method1 = config.getApiClassConfig().getMethods().get(methodToEndpointMethod( MethodDescriptionEndpoint.class.getMethod("noAnnotation"))); assertNull(method1.getDescription()); ApiMethodConfig method2 = config.getApiClassConfig().getMethods().get(methodToEndpointMethod( MethodDescriptionEndpoint.class.getMethod("noDescription"))); assertNull(method2.getDescription()); ApiMethodConfig method3 = config.getApiClassConfig().getMethods().get(methodToEndpointMethod( MethodDescriptionEndpoint.class.getMethod("withDescription"))); assertEquals("description", method3.getDescription()); }
@Test public void testWildcardParameterTypes() throws Exception { @Api final class WildcardEndpoint { @SuppressWarnings("unused") public void foo(Map<String, ? extends Integer> map) {} } try { ApiConfig config = createConfig(WildcardEndpoint.class); annotationReader.loadEndpointMethods(serviceContext, WildcardEndpoint.class, config.getApiClassConfig().getMethods()); fail("Config generation for service class with wildcard parameter type should have failed"); } catch (IllegalArgumentException e) { } }
@Test public void testDeepGenericHierarchyFailEndpoint() throws Exception { ApiConfig config = createConfig(DeepGenericHierarchyFailEndpoint.class); annotationReader.loadEndpointMethods(serviceContext, DeepGenericHierarchyFailEndpoint.class, config.getApiClassConfig().getMethods()); ApiMethodConfig method1 = config.getApiClassConfig().getMethods().get(methodToEndpointMethod( DeepGenericHierarchyFailEndpoint.class, DeepGenericHierarchyFailEndpoint.class.getMethod("foo", String.class, Integer.class, Collection.class))); assertEquals("DeepGenericHierarchyFailEndpoint.foo", method1.getName()); ApiMethodConfig method2 = config.getApiClassConfig().getMethods().get(methodToEndpointMethod( DeepGenericHierarchyFailEndpoint.class, DeepGenericHierarchyFailEndpoint.class.getSuperclass().getSuperclass().getSuperclass() .getMethod("foo", Object.class, Object.class, Object.class))); assertEquals("Endpoint3.foo", method2.getName()); }
@Test public void testParameterDescription() throws Exception { @Api final class TestParameterDescription { public void foo(@Description("desc") String param) {} } ApiConfig config = createConfig(TestParameterDescription.class); annotationReader.loadEndpointMethods(serviceContext, TestParameterDescription.class, config.getApiClassConfig().getMethods()); ApiMethodConfig methodConfig = Iterables.getOnlyElement(config.getApiClassConfig().getMethods().values()); ApiParameterConfig parameterConfig = Iterables.getOnlyElement(methodConfig.getParameterConfigs()); assertEquals("desc", parameterConfig.getDescription()); }
@Test public void testGenericParameterTypes() throws Exception { @Api final class Test <T> { @SuppressWarnings("unused") public void setT(T t) {} } ApiConfig config = createConfig(Test.class); annotationReader.loadEndpointMethods(serviceContext, Test.class, config.getApiClassConfig().getMethods()); ApiParameterConfig parameter = config.getApiClassConfig().getMethods() .get(methodToEndpointMethod(Test.class.getDeclaredMethod("setT", Object.class))) .getParameterConfigs() .get(0); assertEquals(ApiParameterConfig.Classification.UNKNOWN, parameter.getClassification()); }
@Test public void testKnownParameterizedType() throws Exception { @Api class Bar <T1> { @SuppressWarnings("unused") public void bar(T1 t1) {} } class Foo extends Bar<Integer> {} ApiConfig config = createConfig(Foo.class); annotationReader.loadEndpointMethods(serviceContext, Foo.class, config.getApiClassConfig().getMethods()); ApiParameterConfig parameter = config.getApiClassConfig().getMethods() .get(methodToEndpointMethod( Foo.class.getSuperclass().getDeclaredMethod("bar", Object.class))) .getParameterConfigs() .get(0); assertEquals(ApiParameterConfig.Classification.API_PARAMETER, parameter.getClassification()); }