|
13 | 13 | import java.nio.file.Paths; |
14 | 14 | import java.util.List; |
15 | 15 | import java.util.Map; |
16 | | -import java.util.Objects; |
17 | 16 | import java.util.Optional; |
18 | 17 |
|
19 | 18 | import org.junit.jupiter.api.Test; |
20 | 19 |
|
21 | 20 | import com.github.javaparser.StaticJavaParser; |
22 | 21 | import com.github.javaparser.ast.CompilationUnit; |
| 22 | +import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration; |
23 | 23 | import com.github.javaparser.ast.body.EnumConstantDeclaration; |
24 | 24 | import com.github.javaparser.ast.body.MethodDeclaration; |
| 25 | +import com.github.javaparser.ast.body.Parameter; |
25 | 26 |
|
26 | 27 | public class OpenApiClientGeneratorWrapperTest { |
27 | 28 |
|
28 | 29 | @Test |
29 | 30 | void verifyCommonGenerated() throws URISyntaxException { |
30 | | - final Path petstoreOpenApi = Path |
31 | | - .of(requireNonNull(this.getClass().getResource("/openapi/petstore-openapi.json")).toURI()); |
32 | | - final Path targetPath = Paths.get(getTargetDir(), "openapi-gen"); |
33 | | - final OpenApiClientGeneratorWrapper generatorWrapper = new OpenApiClientGeneratorWrapper(petstoreOpenApi, targetPath); |
34 | | - final List<File> generatedFiles = generatorWrapper.generate(); |
| 31 | + final List<File> generatedFiles = createGeneratorWrapper("petstore-openapi.json").generate(); |
35 | 32 | assertNotNull(generatedFiles); |
36 | 33 | assertFalse(generatedFiles.isEmpty()); |
37 | 34 | } |
38 | 35 |
|
39 | 36 | @Test |
40 | 37 | void verifyAuthBasicGenerated() throws URISyntaxException { |
41 | | - final Path petstoreOpenApi = Path |
42 | | - .of(requireNonNull(this.getClass().getResource("/openapi/petstore-openapi-httpbasic.json")).toURI()); |
43 | | - final Path targetPath = Paths.get(getTargetDir(), "openapi-gen"); |
44 | | - final OpenApiClientGeneratorWrapper generatorWrapper = new OpenApiClientGeneratorWrapper(petstoreOpenApi, targetPath); |
45 | | - final List<File> generatedFiles = generatorWrapper.generate(); |
| 38 | + final List<File> generatedFiles = createGeneratorWrapper("petstore-openapi-httpbasic.json").generate(); |
46 | 39 | assertTrue(generatedFiles.stream().anyMatch(f -> f.getName().equals("CompositeAuthenticationProvider.java"))); |
47 | 40 | } |
48 | 41 |
|
49 | 42 | @Test |
50 | 43 | void verifyAuthBearerGenerated() throws URISyntaxException { |
51 | | - final Path petstoreOpenApi = Path |
52 | | - .of(requireNonNull(this.getClass().getResource("/openapi/petstore-openapi-bearer.json")).toURI()); |
53 | | - final Path targetPath = Paths.get(getTargetDir(), "openapi-gen"); |
54 | | - final OpenApiClientGeneratorWrapper generatorWrapper = new OpenApiClientGeneratorWrapper(petstoreOpenApi, targetPath); |
55 | | - final List<File> generatedFiles = generatorWrapper.generate(); |
| 44 | + final List<File> generatedFiles = createGeneratorWrapper("petstore-openapi-bearer.json").generate(); |
56 | 45 | assertTrue(generatedFiles.stream().anyMatch(f -> f.getName().equals("CompositeAuthenticationProvider.java"))); |
57 | 46 | } |
58 | 47 |
|
59 | 48 | @Test |
60 | 49 | void verifyEnumGeneration() throws URISyntaxException, FileNotFoundException { |
61 | | - final Path issue28Path = Path |
62 | | - .of(requireNonNull(this.getClass().getResource("/openapi/issue-28.yaml")).toURI()); |
63 | | - final Path targetPath = Paths.get(getTargetDir(), "openapi-gen"); |
64 | | - final OpenApiClientGeneratorWrapper generatorWrapper = new OpenApiClientGeneratorWrapper(issue28Path, targetPath) |
65 | | - .withBasePackage("org.issue28"); |
66 | | - |
67 | | - final List<File> generatedFiles = generatorWrapper.generate(); |
| 50 | + final List<File> generatedFiles = createGeneratorWrapper("issue-28.yaml") |
| 51 | + .withBasePackage("org.issue28") |
| 52 | + .generate(); |
68 | 53 | final Optional<File> enumFile = generatedFiles.stream() |
69 | 54 | .filter(f -> f.getName().endsWith("ConnectorNamespaceState.java")).findFirst(); |
70 | 55 | assertThat(enumFile).isPresent(); |
@@ -114,15 +99,72 @@ void circuitBreaker() throws URISyntaxException, FileNotFoundException { |
114 | 99 | } |
115 | 100 |
|
116 | 101 | private List<File> generateRestClientFiles() throws URISyntaxException { |
117 | | - Path targetPath = Paths.get(getTargetDir(), "openapi-gen"); |
118 | | - |
119 | | - Path simpleOpenApiFile = Path.of(Objects.requireNonNull(getClass().getResource("/openapi/simple-openapi.json")) |
120 | | - .toURI()); |
121 | | - |
122 | | - OpenApiClientGeneratorWrapper generatorWrapper = new OpenApiClientGeneratorWrapper(simpleOpenApiFile, targetPath) |
| 102 | + OpenApiClientGeneratorWrapper generatorWrapper = createGeneratorWrapper("simple-openapi.json") |
123 | 103 | .withCircuitBreakerConfiguration(Map.of( |
124 | 104 | "org.openapitools.client.api.DefaultApi", List.of("opThatDoesNotExist", "byeGet"))); |
125 | 105 |
|
126 | 106 | return generatorWrapper.generate(); |
127 | 107 | } |
| 108 | + |
| 109 | + private OpenApiClientGeneratorWrapper createGeneratorWrapper(String specFileName) throws URISyntaxException { |
| 110 | + final Path openApiSpec = Path |
| 111 | + .of(requireNonNull(this.getClass().getResource(String.format("/openapi/%s", specFileName))).toURI()); |
| 112 | + final Path targetPath = Paths.get(getTargetDir(), "openapi-gen"); |
| 113 | + |
| 114 | + return new OpenApiClientGeneratorWrapper(openApiSpec, targetPath); |
| 115 | + } |
| 116 | + |
| 117 | + @Test |
| 118 | + void verifyMultipartFormAnnotationIsGeneratedForParameter() throws URISyntaxException, FileNotFoundException { |
| 119 | + List<File> generatedFiles = createGeneratorWrapper("multipart-openapi.yml") |
| 120 | + .withSkipFormModelConfig("false") |
| 121 | + .generate(); |
| 122 | + assertThat(generatedFiles).isNotEmpty(); |
| 123 | + |
| 124 | + Optional<File> file = generatedFiles.stream() |
| 125 | + .filter(f -> f.getName().endsWith("UserProfileDataApi.java")) |
| 126 | + .findAny(); |
| 127 | + assertThat(file).isPresent(); |
| 128 | + |
| 129 | + CompilationUnit compilationUnit = StaticJavaParser.parse(file.orElseThrow()); |
| 130 | + List<MethodDeclaration> methodDeclarations = compilationUnit.findAll(MethodDeclaration.class); |
| 131 | + assertThat(methodDeclarations).isNotEmpty(); |
| 132 | + |
| 133 | + Optional<MethodDeclaration> multipartPostMethod = methodDeclarations.stream() |
| 134 | + .filter(m -> m.getNameAsString().equals("postUserProfileData")) |
| 135 | + .findAny(); |
| 136 | + assertThat(multipartPostMethod).isPresent(); |
| 137 | + |
| 138 | + List<Parameter> parameters = multipartPostMethod.orElseThrow().getParameters(); |
| 139 | + assertThat(parameters).hasSize(1); |
| 140 | + |
| 141 | + Parameter param = parameters.get(0); |
| 142 | + assertThat(param.getAnnotationByName("MultipartForm")).isPresent(); |
| 143 | + } |
| 144 | + |
| 145 | + @Test |
| 146 | + void verifyMultipartPojoGeneratedAndFieldsHaveAnnotations() throws URISyntaxException, FileNotFoundException { |
| 147 | + List<File> generatedFiles = createGeneratorWrapper("multipart-openapi.yml") |
| 148 | + .withSkipFormModelConfig("false") |
| 149 | + .generate(); |
| 150 | + assertFalse(generatedFiles.isEmpty()); |
| 151 | + |
| 152 | + Optional<File> file = generatedFiles.stream() |
| 153 | + .filter(f -> f.getName().endsWith("UserProfileDataApi.java")) |
| 154 | + .findAny(); |
| 155 | + assertThat(file).isNotEmpty(); |
| 156 | + |
| 157 | + CompilationUnit compilationUnit = StaticJavaParser.parse(file.orElseThrow()); |
| 158 | + Optional<ClassOrInterfaceDeclaration> multipartPojo = compilationUnit.findAll(ClassOrInterfaceDeclaration.class) |
| 159 | + .stream() |
| 160 | + .filter(c -> c.getNameAsString().equals("PostUserProfileDataMultipartForm")) |
| 161 | + .findAny(); |
| 162 | + assertThat(multipartPojo).isNotEmpty(); |
| 163 | + |
| 164 | + assertThat(multipartPojo.orElseThrow().getFields()).hasSize(3); |
| 165 | + multipartPojo.orElseThrow().getFields().forEach(field -> { |
| 166 | + assertThat(field.getAnnotationByName("FormParam")).isPresent(); |
| 167 | + assertThat(field.getAnnotationByName("PartType")).isPresent(); |
| 168 | + }); |
| 169 | + } |
128 | 170 | } |
0 commit comments