|
24 | 24 | import com.github.javaparser.StaticJavaParser; |
25 | 25 | import com.github.javaparser.ast.CompilationUnit; |
26 | 26 | import com.github.javaparser.ast.ImportDeclaration; |
| 27 | +import com.github.javaparser.ast.Node; |
27 | 28 | import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration; |
28 | 29 | import com.github.javaparser.ast.body.EnumConstantDeclaration; |
29 | 30 | import com.github.javaparser.ast.body.FieldDeclaration; |
|
32 | 33 | import com.github.javaparser.ast.body.VariableDeclarator; |
33 | 34 | import com.github.javaparser.ast.expr.AnnotationExpr; |
34 | 35 | import com.github.javaparser.ast.expr.SingleMemberAnnotationExpr; |
| 36 | +import com.github.javaparser.ast.nodeTypes.NodeWithName; |
| 37 | +import com.github.javaparser.ast.type.Type; |
35 | 38 |
|
36 | 39 | import io.quarkiverse.openapi.generator.annotations.GeneratedClass; |
37 | 40 | import io.quarkiverse.openapi.generator.annotations.GeneratedMethod; |
@@ -353,6 +356,87 @@ void shouldReplaceFileImportWithInputStream() throws URISyntaxException, FileNot |
353 | 356 | assertThat(imports).doesNotContain("java.io.File"); |
354 | 357 | } |
355 | 358 |
|
| 359 | + @Test |
| 360 | + void withoutAnyTypeOrImportMappingsItShouldGenerateUsingJava8DatesAndTimes() |
| 361 | + throws URISyntaxException, FileNotFoundException { |
| 362 | + List<File> generatedFiles = createGeneratorWrapper("datetime-regression.yml") |
| 363 | + .generate("org.datetime.regression"); |
| 364 | + |
| 365 | + Optional<File> file = generatedFiles.stream() |
| 366 | + .filter(f -> f.getName().endsWith("SomeName.java")) |
| 367 | + .findAny(); |
| 368 | + assertThat(file).isNotEmpty(); |
| 369 | + CompilationUnit compilationUnit = StaticJavaParser.parse(file.orElseThrow()); |
| 370 | + List<ClassOrInterfaceDeclaration> classes = compilationUnit.findAll(ClassOrInterfaceDeclaration.class); |
| 371 | + assertThat(classes).hasSize(1); |
| 372 | + ClassOrInterfaceDeclaration generatedPojoClass = classes.get(0); |
| 373 | + |
| 374 | + verifyGeneratedDateAndTimeTypes( |
| 375 | + generatedPojoClass, |
| 376 | + Map.of( |
| 377 | + "someDate", "LocalDate", |
| 378 | + "someDateTime", "OffsetDateTime", |
| 379 | + "dateArray", "List<LocalDate>", |
| 380 | + "dateTimeArray", "List<OffsetDateTime>", |
| 381 | + "dateSet", "Set<LocalDate>", |
| 382 | + "dateTimeSet", "Set<OffsetDateTime>", |
| 383 | + "dateMap", "Map<String,LocalDate>", |
| 384 | + "dateTimeMap", "Map<String,OffsetDateTime>")); |
| 385 | + assertThat(compilationUnit.getImports().stream().map(NodeWithName::getNameAsString)) |
| 386 | + .contains("java.time.LocalDate", "java.time.OffsetDateTime"); |
| 387 | + } |
| 388 | + |
| 389 | + @Test |
| 390 | + void shouldBeAbleToAddCustomDateAndTimeMappings() throws URISyntaxException, FileNotFoundException { |
| 391 | + List<File> generatedFiles = createGeneratorWrapper("datetime-regression.yml") |
| 392 | + .withTypeMappings(Map.of( |
| 393 | + "date", "ThaiBuddhistDate", |
| 394 | + "DateTime", "Instant")) |
| 395 | + .withImportMappings(Map.of( |
| 396 | + "ThaiBuddhistDate", "java.time.chrono.ThaiBuddhistDate", |
| 397 | + "Instant", "java.time.Instant")) |
| 398 | + .generate("org.datetime.mappings"); |
| 399 | + Optional<File> file = generatedFiles.stream() |
| 400 | + .filter(f -> f.getName().endsWith("SomeName.java")) |
| 401 | + .findAny(); |
| 402 | + assertThat(file).isNotEmpty(); |
| 403 | + CompilationUnit compilationUnit = StaticJavaParser.parse(file.orElseThrow()); |
| 404 | + List<ClassOrInterfaceDeclaration> classes = compilationUnit.findAll(ClassOrInterfaceDeclaration.class); |
| 405 | + assertThat(classes).hasSize(1); |
| 406 | + ClassOrInterfaceDeclaration generatedPojoClass = classes.get(0); |
| 407 | + |
| 408 | + verifyGeneratedDateAndTimeTypes( |
| 409 | + generatedPojoClass, |
| 410 | + Map.of( |
| 411 | + "someDate", "ThaiBuddhistDate", |
| 412 | + "someDateTime", "Instant", |
| 413 | + "dateArray", "List<ThaiBuddhistDate>", |
| 414 | + "dateTimeArray", "List<Instant>", |
| 415 | + "dateSet", "Set<ThaiBuddhistDate>", |
| 416 | + "dateTimeSet", "Set<Instant>", |
| 417 | + "dateMap", "Map<String,ThaiBuddhistDate>", |
| 418 | + "dateTimeMap", "Map<String,Instant>")); |
| 419 | + assertThat(compilationUnit.getImports().stream().map(NodeWithName::getNameAsString)) |
| 420 | + .contains("java.time.chrono.ThaiBuddhistDate", "java.time.Instant"); |
| 421 | + } |
| 422 | + |
| 423 | + private void verifyGeneratedDateAndTimeTypes( |
| 424 | + ClassOrInterfaceDeclaration classDeclaration, |
| 425 | + Map<String, String> expectedFieldsAndTypes) { |
| 426 | + expectedFieldsAndTypes.forEach((fieldName, expectedFieldType) -> { |
| 427 | + Optional<FieldDeclaration> fieldDeclaration = classDeclaration.getFieldByName(fieldName); |
| 428 | + assertThat(fieldDeclaration).isPresent(); |
| 429 | + |
| 430 | + Optional<Node> fieldVariableDeclaration = fieldDeclaration.orElseThrow().getChildNodes().stream() |
| 431 | + .filter(it -> it instanceof VariableDeclarator) |
| 432 | + .findFirst(); |
| 433 | + assertThat(fieldVariableDeclaration).isPresent(); |
| 434 | + |
| 435 | + Type fieldType = ((VariableDeclarator) fieldVariableDeclaration.orElseThrow()).getType(); |
| 436 | + assertThat(fieldType.asString()).isEqualTo(expectedFieldType); |
| 437 | + }); |
| 438 | + } |
| 439 | + |
356 | 440 | @Test |
357 | 441 | void verifyAdditionalModelTypeAnnotations() throws URISyntaxException { |
358 | 442 | List<File> generatedFiles = createGeneratorWrapper("petstore-openapi.json") |
|
0 commit comments