Skip to content

Commit a8e2cd8

Browse files
Streamline tests
1 parent 6449fd6 commit a8e2cd8

1 file changed

Lines changed: 83 additions & 111 deletions

File tree

modules/openapi-generator/src/test/java/org/openapitools/codegen/kotlin/spring/KotlinSpringServerCodegenTest.java

Lines changed: 83 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import io.swagger.v3.parser.core.models.ParseOptions;
88
import java.util.Arrays;
99
import java.util.HashMap;
10+
import java.util.Objects;
1011
import java.util.function.Consumer;
1112
import java.util.stream.Stream;
1213
import org.apache.commons.io.FileUtils;
@@ -40,6 +41,9 @@
4041
import static org.assertj.core.api.Assertions.assertThat;
4142
import static org.openapitools.codegen.TestUtils.assertFileContains;
4243
import static org.openapitools.codegen.TestUtils.assertFileNotContains;
44+
import static org.openapitools.codegen.languages.KotlinSpringServerCodegen.REACTIVE;
45+
import static org.openapitools.codegen.languages.KotlinSpringServerCodegen.SERVICE_IMPLEMENTATION;
46+
import static org.openapitools.codegen.languages.SpringCodegen.DELEGATE_PATTERN;
4347
import static org.openapitools.codegen.languages.SpringCodegen.SPRING_BOOT;
4448
import static org.openapitools.codegen.languages.features.DocumentationProviderFeatures.ANNOTATION_LIBRARY;
4549
import static org.openapitools.codegen.languages.features.DocumentationProviderFeatures.DOCUMENTATION_PROVIDER;
@@ -210,7 +214,7 @@ public void testSettersForConfigValues() throws Exception {
210214
Assert.assertTrue(codegen.getServiceInterface());
211215
Assert.assertEquals(codegen.additionalProperties().get(KotlinSpringServerCodegen.SERVICE_INTERFACE), true);
212216
Assert.assertTrue(codegen.getServiceImplementation());
213-
Assert.assertEquals(codegen.additionalProperties().get(KotlinSpringServerCodegen.SERVICE_IMPLEMENTATION), true);
217+
Assert.assertEquals(codegen.additionalProperties().get(SERVICE_IMPLEMENTATION), true);
214218
Assert.assertFalse(codegen.getUseBeanValidation());
215219
Assert.assertEquals(codegen.additionalProperties().get(KotlinSpringServerCodegen.USE_BEANVALIDATION), false);
216220
Assert.assertFalse(codegen.isReactive());
@@ -229,7 +233,7 @@ public void testAdditionalPropertiesPutForConfigValues() throws Exception {
229233
codegen.additionalProperties().put(KotlinSpringServerCodegen.EXCEPTION_HANDLER, false);
230234
codegen.additionalProperties().put(KotlinSpringServerCodegen.GRADLE_BUILD_FILE, false);
231235
codegen.additionalProperties().put(KotlinSpringServerCodegen.SERVICE_INTERFACE, true);
232-
codegen.additionalProperties().put(KotlinSpringServerCodegen.SERVICE_IMPLEMENTATION, true);
236+
codegen.additionalProperties().put(SERVICE_IMPLEMENTATION, true);
233237
codegen.additionalProperties().put(KotlinSpringServerCodegen.USE_BEANVALIDATION, false);
234238
codegen.additionalProperties().put(KotlinSpringServerCodegen.REACTIVE, false);
235239
codegen.processOpts();
@@ -255,7 +259,7 @@ public void testAdditionalPropertiesPutForConfigValues() throws Exception {
255259
Assert.assertTrue(codegen.getServiceInterface());
256260
Assert.assertEquals(codegen.additionalProperties().get(KotlinSpringServerCodegen.SERVICE_INTERFACE), true);
257261
Assert.assertTrue(codegen.getServiceImplementation());
258-
Assert.assertEquals(codegen.additionalProperties().get(KotlinSpringServerCodegen.SERVICE_IMPLEMENTATION), true);
262+
Assert.assertEquals(codegen.additionalProperties().get(SERVICE_IMPLEMENTATION), true);
259263
Assert.assertFalse(codegen.getUseBeanValidation());
260264
Assert.assertEquals(codegen.additionalProperties().get(KotlinSpringServerCodegen.USE_BEANVALIDATION), false);
261265
Assert.assertFalse(codegen.isReactive());
@@ -792,143 +796,118 @@ public void contractWithResolvedInnerEnumContainsEnumConverter() throws IOExcept
792796

793797
@Test
794798
public void givenNonRequiredMultipartFileArray_whenGenerateDelegateAndService_thenParameterIsCreatedAsNullableListOfMultipartFile() throws IOException {
795-
File output = Files.createTempDirectory("test").toFile().getCanonicalFile();
796-
output.deleteOnExit();
797-
String outputPath = output.getAbsolutePath().replace('\\', '/');
798-
799-
final OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/3_0/kotlin/petstore-with-tags.yaml");
800-
final KotlinSpringServerCodegen codegen = new KotlinSpringServerCodegen();
801-
codegen.setOpenAPI(openAPI);
802-
codegen.setOutputDir(output.getAbsolutePath());
803-
codegen.setDelegatePattern(true);
804-
// this will generate the service interface & implementation files
805-
codegen.setServiceImplementation(true);
806-
807-
ClientOptInput input = new ClientOptInput();
808-
input.openAPI(openAPI);
809-
input.config(codegen);
810-
811-
DefaultGenerator generator = new DefaultGenerator();
812-
813-
generator.setGeneratorPropertyDefault(CodegenConstants.MODELS, "false");
814-
generator.setGeneratorPropertyDefault(CodegenConstants.MODEL_TESTS, "false");
815-
generator.setGeneratorPropertyDefault(CodegenConstants.MODEL_DOCS, "false");
816-
generator.setGeneratorPropertyDefault(CodegenConstants.APIS, "true");
817-
generator.setGeneratorPropertyDefault(CodegenConstants.SUPPORTING_FILES, "false");
818-
819-
generator.opts(input).generate();
799+
Map<String, Object> additionalProperties = new HashMap<>();
800+
additionalProperties.put(DELEGATE_PATTERN, true);
801+
additionalProperties.put(SERVICE_IMPLEMENTATION, true);
820802

821-
Path delegateFile = Paths.get(outputPath + "/src/main/kotlin/org/openapitools/api/PetApiDelegate.kt");
822-
assertFileContains(delegateFile, "additionalMetadata: kotlin.String?");
823-
assertFileContains(delegateFile, "images: Array<org.springframework.web.multipart.MultipartFile>?)");
803+
Map<String, String> generatorPropertyDefaults = new HashMap<>();
804+
generatorPropertyDefaults.put(CodegenConstants.MODELS, "false");
805+
generatorPropertyDefaults.put(CodegenConstants.MODEL_TESTS, "false");
806+
generatorPropertyDefaults.put(CodegenConstants.MODEL_DOCS, "false");
807+
generatorPropertyDefaults.put(CodegenConstants.APIS, "true");
808+
generatorPropertyDefaults.put(CodegenConstants.SUPPORTING_FILES, "false");
824809

825-
Path controllerFile = Paths.get(outputPath + "/src/main/kotlin/org/openapitools/api/PetApi.kt");
826-
assertFileContains(controllerFile, "additionalMetadata: kotlin.String?");
827-
assertFileContains(controllerFile, "images: Array<org.springframework.web.multipart.MultipartFile>?)");
828-
829-
Path serviceFile = Paths.get(outputPath + "/src/main/kotlin/org/openapitools/api/PetApiService.kt");
830-
assertFileContains(serviceFile, "additionalMetadata: kotlin.String?");
831-
assertFileContains(serviceFile, "images: Array<org.springframework.web.multipart.MultipartFile>?)");
810+
Map<String, File> files = generateFromContract(
811+
"src/test/resources/3_0/kotlin/petstore-with-tags.yaml",
812+
additionalProperties,
813+
generatorPropertyDefaults
814+
);
832815

833-
Path serviceImplFile = Paths.get(outputPath + "/src/main/kotlin/org/openapitools/api/PetApiServiceImpl.kt");
834-
assertFileContains(serviceImplFile, "additionalMetadata: kotlin.String?");
835-
assertFileContains(serviceImplFile, "images: Array<org.springframework.web.multipart.MultipartFile>?)");
816+
validateMultipartFiles(
817+
files,
818+
"Pet",
819+
"additionalMetadata: kotlin.String?",
820+
"images: Array<org.springframework.web.multipart.MultipartFile>?)"
821+
);
836822
}
837823

838824
@Test
839825
public void givenMultipartBinaryArray_whenGenerateDelegateAndService_correctMultipartFileIsCreated() throws IOException {
840-
File output = Files.createTempDirectory("test").toFile().getCanonicalFile();
841-
output.deleteOnExit();
842-
String outputPath = output.getAbsolutePath().replace('\\', '/');
843-
844-
final OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/3_0/form-multipart-binary-array.yaml");
845-
final KotlinSpringServerCodegen codegen = new KotlinSpringServerCodegen();
846-
codegen.setOpenAPI(openAPI);
847-
codegen.setOutputDir(output.getAbsolutePath());
848-
codegen.setDelegatePattern(true);
849-
// this will generate the service interface & implementation files
850-
codegen.setServiceImplementation(true);
826+
Map<String, Object> additionalProperties = new HashMap<>();
827+
additionalProperties.put(DELEGATE_PATTERN, true);
828+
additionalProperties.put(SERVICE_IMPLEMENTATION, true);
851829

852-
ClientOptInput input = new ClientOptInput();
853-
input.openAPI(openAPI);
854-
input.config(codegen);
830+
Map<String, String> generatorPropertyDefaults = new HashMap<>();
831+
generatorPropertyDefaults.put(CodegenConstants.MODELS, "false");
832+
generatorPropertyDefaults.put(CodegenConstants.MODEL_TESTS, "false");
833+
generatorPropertyDefaults.put(CodegenConstants.MODEL_DOCS, "false");
834+
generatorPropertyDefaults.put(CodegenConstants.APIS, "true");
835+
generatorPropertyDefaults.put(CodegenConstants.SUPPORTING_FILES, "false");
855836

856-
DefaultGenerator generator = new DefaultGenerator();
857-
858-
generator.setGeneratorPropertyDefault(CodegenConstants.MODELS, "false");
859-
generator.setGeneratorPropertyDefault(CodegenConstants.MODEL_TESTS, "false");
860-
generator.setGeneratorPropertyDefault(CodegenConstants.MODEL_DOCS, "false");
861-
generator.setGeneratorPropertyDefault(CodegenConstants.APIS, "true");
862-
generator.setGeneratorPropertyDefault(CodegenConstants.SUPPORTING_FILES, "false");
863-
864-
generator.opts(input).generate();
837+
Map<String, File> files = generateFromContract(
838+
"src/test/resources/3_0/form-multipart-binary-array.yaml",
839+
additionalProperties,
840+
generatorPropertyDefaults
841+
);
865842

866843
validateMultipartFiles(
867-
outputPath + "/src/main/kotlin/org/openapitools/api/MultipartArray",
844+
files,
845+
"MultipartArray",
868846
"files: Array<org.springframework.web.multipart.MultipartFile>?)"
869847
);
870848

871849
validateMultipartFiles(
872-
outputPath + "/src/main/kotlin/org/openapitools/api/MultipartMixed",
850+
files,
851+
"MultipartMixed",
873852
"file: org.springframework.web.multipart.MultipartFile,",
874853
"marker: MultipartMixedRequestMarker?",
875854
"statusArray: kotlin.collections.List<MultipartMixedStatus>?"
876855
);
877856

878857
validateMultipartFiles(
879-
outputPath + "/src/main/kotlin/org/openapitools/api/MultipartSingle",
858+
files,
859+
"MultipartSingle",
880860
"file: org.springframework.web.multipart.MultipartFile?"
881861
);
882862
}
883863

884864
@Test
885865
public void givenMultipartBinaryArray_whenGenerateReactiveDelegateAndService_correctPartIsCreated() throws IOException {
886-
File output = Files.createTempDirectory("test").toFile().getCanonicalFile();
887-
output.deleteOnExit();
888-
String outputPath = output.getAbsolutePath().replace('\\', '/');
889-
890-
final OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/3_0/form-multipart-binary-array.yaml");
891-
final KotlinSpringServerCodegen codegen = new KotlinSpringServerCodegen();
892-
codegen.setOpenAPI(openAPI);
893-
codegen.setOutputDir(output.getAbsolutePath());
894-
codegen.setDelegatePattern(true);
895-
// this will generate the service interface & implementation files
896-
codegen.setServiceImplementation(true);
897-
codegen.setReactive(true);
866+
Map<String, Object> additionalProperties = new HashMap<>();
867+
additionalProperties.put(DELEGATE_PATTERN, true);
868+
additionalProperties.put(SERVICE_IMPLEMENTATION, true);
869+
additionalProperties.put(REACTIVE, true);
870+
871+
Map<String, String> generatorPropertyDefaults = new HashMap<>();
872+
generatorPropertyDefaults.put(CodegenConstants.MODELS, "false");
873+
generatorPropertyDefaults.put(CodegenConstants.MODEL_TESTS, "false");
874+
generatorPropertyDefaults.put(CodegenConstants.MODEL_DOCS, "false");
875+
generatorPropertyDefaults.put(CodegenConstants.APIS, "true");
876+
generatorPropertyDefaults.put(CodegenConstants.SUPPORTING_FILES, "false");
898877

899-
ClientOptInput input = new ClientOptInput();
900-
input.openAPI(openAPI);
901-
input.config(codegen);
902-
903-
DefaultGenerator generator = new DefaultGenerator();
904-
905-
generator.setGeneratorPropertyDefault(CodegenConstants.MODELS, "false");
906-
generator.setGeneratorPropertyDefault(CodegenConstants.MODEL_TESTS, "false");
907-
generator.setGeneratorPropertyDefault(CodegenConstants.MODEL_DOCS, "false");
908-
generator.setGeneratorPropertyDefault(CodegenConstants.APIS, "true");
909-
// generator.setGeneratorPropertyDefault(CodegenConstants.SUPPORTING_FILES, "false");
910-
911-
generator.opts(input).generate();
878+
Map<String, File> files = generateFromContract(
879+
"src/test/resources/3_0/form-multipart-binary-array.yaml",
880+
additionalProperties,
881+
generatorPropertyDefaults
882+
);
912883

913884
validateMultipartFiles(
914-
outputPath + "/src/main/kotlin/org/openapitools/api/MultipartArray",
885+
files,
886+
"MultipartArray",
915887
"files: Flux<org.springframework.http.codec.multipart.Part>?)"
916888
);
917889

918890
validateMultipartFiles(
919-
outputPath + "/src/main/kotlin/org/openapitools/api/MultipartMixed",
891+
files,
892+
"MultipartMixed",
920893
"file: org.springframework.http.codec.multipart.Part,",
921894
"marker: MultipartMixedRequestMarker?",
922895
"statusArray: kotlin.collections.List<MultipartMixedStatus>?"
923896
);
924897

925898
validateMultipartFiles(
926-
outputPath + "/src/main/kotlin/org/openapitools/api/MultipartSingle",
899+
files,
900+
"MultipartSingle",
927901
"file: org.springframework.http.codec.multipart.Part?"
928902
);
929903
}
930904

905+
/**
906+
* Utility function to help validate that all delegate, service & api code generated for
907+
* schemas with multipart-form-data have the same lines.
908+
*/
931909
private void validateMultipartFiles(
910+
Map<String, File> files,
932911
String filePrefix,
933912
String... lines
934913
) {
@@ -938,25 +917,18 @@ private void validateMultipartFiles(
938917
filePrefix + "ApiServiceImpl.kt",
939918
filePrefix + "Api.kt"
940919
)
941-
.map(Paths::get)
920+
.map(files::get)
921+
.map(Objects::requireNonNull)
922+
.map(File::toPath)
942923
.forEach(path -> {
943924
try {
944-
validateMultipartFile(path, lines);
925+
TestUtils.assertFileContains(path, lines);
945926
} catch (AssertionError e) {
946927
throw new AssertionError(path.toString() + " does not contain a line", e);
947928
}
948929
});
949930
}
950931

951-
private void validateMultipartFile(
952-
Path filePath,
953-
String... lines
954-
) {
955-
for(String line : lines) {
956-
assertFileContains(filePath, line);
957-
}
958-
}
959-
960932
@Test
961933
public void givenOctetStreamResponseType_whenGenerateServer_thenReturnTypeIsResource() throws IOException {
962934
File output = Files.createTempDirectory("test").toFile().getCanonicalFile();
@@ -1090,7 +1062,7 @@ public void reactiveWithoutFlow() throws Exception {
10901062
codegen.additionalProperties().put(KotlinSpringServerCodegen.REACTIVE, true);
10911063
codegen.additionalProperties().put(KotlinSpringServerCodegen.USE_FLOW_FOR_ARRAY_RETURN_TYPE, false);
10921064
codegen.additionalProperties().put(KotlinSpringServerCodegen.USE_TAGS, true);
1093-
codegen.additionalProperties().put(KotlinSpringServerCodegen.SERVICE_IMPLEMENTATION, true);
1065+
codegen.additionalProperties().put(SERVICE_IMPLEMENTATION, true);
10941066
codegen.additionalProperties().put(KotlinSpringServerCodegen.DELEGATE_PATTERN, true);
10951067

10961068
List<File> files = new DefaultGenerator()
@@ -1135,7 +1107,7 @@ public void reactiveWithFlow() throws Exception {
11351107
codegen.additionalProperties().put(KotlinSpringServerCodegen.REACTIVE, true);
11361108
codegen.additionalProperties().put(KotlinSpringServerCodegen.USE_FLOW_FOR_ARRAY_RETURN_TYPE, true);
11371109
codegen.additionalProperties().put(KotlinSpringServerCodegen.USE_TAGS, true);
1138-
codegen.additionalProperties().put(KotlinSpringServerCodegen.SERVICE_IMPLEMENTATION, true);
1110+
codegen.additionalProperties().put(SERVICE_IMPLEMENTATION, true);
11391111
codegen.additionalProperties().put(KotlinSpringServerCodegen.DELEGATE_PATTERN, true);
11401112

11411113
List<File> files = new DefaultGenerator()
@@ -1181,7 +1153,7 @@ public void reactiveWithDefaultValueFlow() throws Exception {
11811153
// should use default 'true' instead
11821154
// codegen.additionalProperties().put(KotlinSpringServerCodegen.USE_FLOW_FOR_ARRAY_RETURN_TYPE, true);
11831155
codegen.additionalProperties().put(KotlinSpringServerCodegen.USE_TAGS, true);
1184-
codegen.additionalProperties().put(KotlinSpringServerCodegen.SERVICE_IMPLEMENTATION, true);
1156+
codegen.additionalProperties().put(SERVICE_IMPLEMENTATION, true);
11851157
codegen.additionalProperties().put(KotlinSpringServerCodegen.DELEGATE_PATTERN, true);
11861158

11871159
List<File> files = new DefaultGenerator()
@@ -1226,7 +1198,7 @@ public void nonReactiveWithoutFlow() throws Exception {
12261198
codegen.additionalProperties().put(KotlinSpringServerCodegen.REACTIVE, false);
12271199
codegen.additionalProperties().put(KotlinSpringServerCodegen.USE_FLOW_FOR_ARRAY_RETURN_TYPE, false);
12281200
codegen.additionalProperties().put(KotlinSpringServerCodegen.USE_TAGS, true);
1229-
codegen.additionalProperties().put(KotlinSpringServerCodegen.SERVICE_IMPLEMENTATION, true);
1201+
codegen.additionalProperties().put(SERVICE_IMPLEMENTATION, true);
12301202
codegen.additionalProperties().put(KotlinSpringServerCodegen.DELEGATE_PATTERN, true);
12311203

12321204
List<File> files = new DefaultGenerator()
@@ -1271,7 +1243,7 @@ public void nonReactiveWithFlow() throws Exception {
12711243
codegen.additionalProperties().put(KotlinSpringServerCodegen.REACTIVE, false);
12721244
codegen.additionalProperties().put(KotlinSpringServerCodegen.USE_FLOW_FOR_ARRAY_RETURN_TYPE, true);
12731245
codegen.additionalProperties().put(KotlinSpringServerCodegen.USE_TAGS, true);
1274-
codegen.additionalProperties().put(KotlinSpringServerCodegen.SERVICE_IMPLEMENTATION, true);
1246+
codegen.additionalProperties().put(SERVICE_IMPLEMENTATION, true);
12751247
codegen.additionalProperties().put(KotlinSpringServerCodegen.DELEGATE_PATTERN, true);
12761248

12771249
List<File> files = new DefaultGenerator()

0 commit comments

Comments
 (0)