ServiceClassParser { public ServiceModel parseServiceClass(Class<?> mainServiceClass, TypescriptServiceGeneratorConfiguration settings, Class<?>... serviceClassesToMerge) { List<Class<?>> serviceClazzes = Lists.newArrayList(mainServiceClass); serviceClazzes.addAll(Lists.newArrayList(serviceClassesToMerge)); ImmutableServiceModel.Builder serviceModel = ImmutableServiceModel.builder(); serviceModel.name(mainServiceClass.getSimpleName()); for (Class<?> serviceClass : serviceClazzes) { ImmutableInnerServiceModel.Builder innerServiceModel = ImmutableInnerServiceModel.builder(); Path servicePathAnnotation = serviceClass.getAnnotation(Path.class); innerServiceModel.servicePath(servicePathAnnotation == null ? "" : PathUtils.trimSlashes(servicePathAnnotation.value())); innerServiceModel.name(serviceClass.getSimpleName()); Set<Method> serviceMethods = getAllServiceMethods(serviceClass, settings.methodFilter()); Set<Type> referencedTypes = Sets.newHashSet(); for (Method method : serviceMethods) { referencedTypes.addAll(getTypesFromEndpoint(method, settings)); } serviceModel.addAllReferencedTypes(referencedTypes); ModelCompiler compiler = new TypeScriptGenerator(settings.getSettings()).getModelCompiler(); List<ServiceEndpointModel> endpointModels = Lists.newArrayList(); endpointModels = computeEndpointModels(serviceMethods, compiler, settings); Collections.sort(endpointModels); innerServiceModel.endpointModels(endpointModels); serviceModel.addInnerServiceModels(innerServiceModel.build()); } return serviceModel.build(); }  Set<Method> getAllServiceMethods(Class<?> serviceClass, MethodFilter methodFilter); ServiceModel parseServiceClass(Class<?> mainServiceClass, TypescriptServiceGeneratorConfiguration settings, Class<?>... serviceClassesToMerge);  }
@Test public void parseSimpleClassTest() { ServiceModel model = serviceClassParser.parseServiceClass(TestServiceClass.class, settings); assertEquals(1, model.innerServiceModels().size()); assertEquals(ImmutableSet.of(String.class), model.referencedTypes()); assertEquals("TestServiceClass", model.name()); assertEquals("testService", model.innerServiceModels().get(0).servicePath()); ServiceEndpointParameterModel aParam = ImmutableServiceEndpointParameterModel.builder().pathParam("a").javaType(String.class).tsType(TsType.String).build(); ServiceEndpointParameterModel bParam = ImmutableServiceEndpointParameterModel.builder().pathParam("b").javaType(String.class).tsType(TsType.String).build(); ImmutableServiceEndpointModel stringGetterEndpointModel = ImmutableServiceEndpointModel.builder() .javaReturnType(String.class) .tsReturnType(TsType.String) .parameters(Lists.newArrayList(aParam, bParam)) .endpointName("stringGetter") .endpointPath("stringGetter/{a}/{b}") .endpointMethodType("GET") .build(); assertEquals(model.innerServiceModels().get(0).endpointModels(), Lists.newArrayList(stringGetterEndpointModel)); }
@Test public void parseComplexClassTest() throws NoSuchMethodException, SecurityException { ServiceModel model = serviceClassParser.parseServiceClass(TestComplexServiceClass.class, settings); assertEquals(1, model.innerServiceModels().size()); Type genericReturnType = TestComplexServiceClass.class.getMethod("allOptionsPost", String.class, Integer.class, DataObject.class).getGenericReturnType(); assertEquals(ImmutableSet.of(Boolean.class, ImmutablesObject.class, MyObject.class, GenericObject.class, Integer.class, String.class, DataObject.class, genericReturnType), model.referencedTypes()); assertEquals("TestComplexServiceClass", model.name()); assertEquals("testComplexService", model.innerServiceModels().get(0).servicePath()); List<ServiceEndpointModel> endpoints = Lists.newArrayList(); { ServiceEndpointParameterModel aParam = ImmutableServiceEndpointParameterModel.builder().pathParam("a").javaType(String.class).tsType(TsType.String).build(); ServiceEndpointParameterModel bParam = ImmutableServiceEndpointParameterModel.builder().queryParam("b").javaType(Integer.class).tsType(TsType.Number).build(); ServiceEndpointParameterModel dataParam = ImmutableServiceEndpointParameterModel.builder().javaType(DataObject.class).tsType(new TsType.ReferenceType(new Symbol("DataObject"))).build(); endpoints.add(ImmutableServiceEndpointModel.builder().javaReturnType(genericReturnType) .tsReturnType(new TsType.ReferenceType(new Symbol("GenericObject<MyObject>"))) .parameters(Lists.newArrayList(aParam, dataParam, bParam)) .endpointName("allOptionsPost") .endpointPath("allOptionsPost/{a}") .endpointMethodType("POST") .endpointRequestMediaType(MediaType.APPLICATION_JSON) .build()); } { ServiceEndpointParameterModel xParam = ImmutableServiceEndpointParameterModel.builder().queryParam("x").javaType(Boolean.class).tsType(TsType.Boolean).build(); endpoints.add(ImmutableServiceEndpointModel.builder().javaReturnType(MyObject.class) .tsReturnType(new TsType.ReferenceType(new Symbol("MyObject"))) .parameters(Lists.newArrayList(xParam)) .endpointName("queryGetter") .endpointPath("queryGetter") .endpointMethodType("GET") .build()); } { ServiceEndpointParameterModel dataParam = ImmutableServiceEndpointParameterModel.builder().javaType(DataObject.class).tsType(new TsType.ReferenceType(new Symbol("DataObject"))).build(); endpoints.add(ImmutableServiceEndpointModel.builder().javaReturnType(ImmutablesObject.class) .tsReturnType(new TsType.ReferenceType(new Symbol("ImmutablesObject"))) .parameters(Lists.newArrayList(dataParam)) .endpointName("simplePut") .endpointPath("simplePut") .endpointMethodType("PUT") .build()); } assertEquals(endpoints.toString(), model.innerServiceModels().get(0).endpointModels().toString()); }
@Test public void parseIgnoredTest() { Mockito.when(settings.ignoredAnnotations()).thenReturn(Sets.newHashSet(CheckForNull.class)); ServiceModel model = serviceClassParser.parseServiceClass(IgnoredParametersClass.class, settings); assertEquals(1, model.innerServiceModels().size()); assertEquals(ImmutableSet.of(String.class), model.referencedTypes()); assertEquals("IgnoredParametersClass", model.name()); assertEquals("ignoredParameters", model.innerServiceModels().get(0).servicePath()); ServiceEndpointParameterModel aParam = ImmutableServiceEndpointParameterModel.builder().pathParam("a").javaType(String.class).tsType(TsType.String).build(); ServiceEndpointParameterModel bParam = ImmutableServiceEndpointParameterModel.builder().pathParam("b").javaType(String.class).tsType(TsType.String).build(); ImmutableServiceEndpointModel stringGetterEndpointModel = ImmutableServiceEndpointModel.builder() .javaReturnType(String.class) .tsReturnType(TsType.String) .parameters(Lists.newArrayList(aParam, bParam)) .endpointName("stringGetter") .endpointPath("stringGetter/{a}/{b}") .endpointMethodType("GET") .build(); assertEquals(model.innerServiceModels().get(0).endpointModels(), Lists.newArrayList(stringGetterEndpointModel)); }
@Test public void duplicateNameResolutionTest() { Mockito.when(settings.duplicateEndpointNameResolver()).thenReturn(new DuplicateMethodNameResolver() { @Override public Map<Method, String> resolveDuplicateNames(List<Method> methodsWithSameName) { Map<Method, String> result = Maps.newHashMap(); for (Method method : methodsWithSameName) { if (method.getParameterTypes().length > 0) { result.put(method, "nonZeroParameters"); } else { result.put(method, "zeroParameters"); } } return result; } }); ServiceModel model = serviceClassParser.parseServiceClass(DuplicateMethodNamesService.class, settings); assertEquals(1, model.innerServiceModels().size()); List<ServiceEndpointModel> endpointModels = Lists.newArrayList(model.innerServiceModels().get(0).endpointModels().iterator()); Collections.sort(endpointModels); assertEquals(Lists.newArrayList("nonZeroParameters", "zeroParameters"), endpointModels.stream().map(endpoint -> endpoint.endpointName()).collect(Collectors.toList())); assertTrue(endpointModels.get(0).parameters().size() > 0); assertTrue(endpointModels.get(1).parameters().size() == 0); }
@Test public void filterMethodTest() { Mockito.when(settings.methodFilter()).thenReturn(new MethodFilter() { @Override public boolean shouldGenerateMethod(Class<?> parentClass, Method method) { return false; } }); ServiceModel model = serviceClassParser.parseServiceClass(SimpleService1.class, settings); assertEquals("SimpleService1", model.name()); InnerServiceModel innerService1 = ImmutableInnerServiceModel.builder() .servicePath("simple1") .name("SimpleService1") .build(); ServiceModel expectedServiceModel = ImmutableServiceModel.builder() .addInnerServiceModels(innerService1) .name("SimpleService1") .build(); assertEquals(expectedServiceModel, model); }
@Test public void multipleServiceClassParseTest() { ServiceModel model = serviceClassParser.parseServiceClass(SimpleService1.class, settings, SimpleService2.class); assertEquals(2, model.innerServiceModels().size()); assertEquals(ImmutableSet.of(String.class), model.referencedTypes()); assertEquals("SimpleService1", model.name()); ImmutableServiceEndpointModel method1Model = ImmutableServiceEndpointModel.builder() .javaReturnType(String.class) .tsReturnType(TsType.String) .parameters(Lists.newArrayList()) .endpointName("method1") .endpointPath("method1") .endpointMethodType("GET") .build(); ImmutableServiceEndpointModel method2Model = ImmutableServiceEndpointModel.builder() .javaReturnType(String.class) .tsReturnType(TsType.String) .parameters(Lists.newArrayList()) .endpointName("method2") .endpointPath("method2") .endpointMethodType("GET") .build(); InnerServiceModel innerService1 = ImmutableInnerServiceModel.builder() .addEndpointModels(method1Model) .servicePath("simple1") .name("SimpleService1") .build(); InnerServiceModel innerService2 = ImmutableInnerServiceModel.builder() .addEndpointModels(method2Model) .servicePath("simple2") .name("SimpleService2") .build(); ServiceModel expectedServiceModel = ImmutableServiceModel.builder() .addInnerServiceModels(innerService1, innerService2) .name("SimpleService1") .addReferencedTypes(String.class) .build(); assertEquals(expectedServiceModel, model); }
@Test public void plainTextTest() { ServiceModel model = serviceClassParser.parseServiceClass(PlainTextService.class, settings); ImmutableServiceEndpointParameterModel expectedParameterModel = ImmutableServiceEndpointParameterModel.builder() .javaType(String.class) .tsType(TsType.String) .build(); ImmutableServiceEndpointModel expectedEndpointModel = ImmutableServiceEndpointModel.builder() .javaReturnType(String.class) .tsReturnType(TsType.String) .addParameters(expectedParameterModel) .endpointName("plainText") .endpointPath("plainText") .endpointMethodType("GET") .endpointRequestMediaType(MediaType.TEXT_PLAIN) .endpointResponseMediaType(MediaType.TEXT_PLAIN) .build(); InnerServiceModel innerServiceModel = ImmutableInnerServiceModel.builder() .addEndpointModels(expectedEndpointModel) .servicePath("plainTextService") .name("PlainTextService") .build(); ServiceModel expectedServiceModel = ImmutableServiceModel.builder() .addInnerServiceModels(innerServiceModel) .name("PlainTextService") .addReferencedTypes(String.class) .build(); assertEquals(expectedServiceModel, model); }
@Test public void noServiceClassPathTest() { ServiceModel model = serviceClassParser.parseServiceClass(TestUtils.NoPathService.class, settings); assertEquals(1, model.innerServiceModels().size()); assertEquals(1, model.innerServiceModels().get(0).endpointModels().size()); assertEquals("", model.innerServiceModels().get(0).servicePath()); }