Skip to content

Commit

Permalink
[test-stack] Implement inner class assertion and fix typo "doesNotCon…
Browse files Browse the repository at this point in the history
…tains" (#18921)

* Implement InnerClassAssert for easy inner class testing

* Suppress "'Optional.get()' without 'isPresent()' check" warning

For now, until https://youtrack.jetbrains.com/issue/IDEA-354935
is solved.

* Apply DRY refactoring

* Move newTempFolder helper method into TestUtils

* Fix typo `doesNotContains`

* Pluralize assertion classes that extend ListAssert

* Add short-hand has/doesNotHaveAnnotation methods to Method assertions

So .hasAssertion(String) can replace .assertMethodAnnotations().containsWithName(String)

* Refactor tests, simplifying setup using CodegenConfigurator

* Harmonize API, removing confusion between has… and assert…

This makes our the assertion API more consistent, in the way that
assertSomething("") will always return a different assertion type,
while hasSomething("") will always return the same type.

* Use simplified hasAnnotation/doesNotHaveAnnotation assertions
  • Loading branch information
Philzen authored Jun 16, 2024
1 parent 3d93862 commit ec8998b
Show file tree
Hide file tree
Showing 26 changed files with 1,202 additions and 1,086 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2592,9 +2592,9 @@ public void testFreeFormSchemas() throws Exception {
DefaultGenerator generator = new DefaultGenerator();
List<File> files = generator.opts(clientOptInput).generate();

TestUtils.ensureDoesNotContainsFile(files, output, "src/main/java/org/openapitools/client/model/FreeFormWithValidation.java");
TestUtils.ensureDoesNotContainsFile(files, output, "src/main/java/org/openapitools/client/model/FreeFormInterface.java");
TestUtils.ensureDoesNotContainsFile(files, output, "src/main/java/org/openapitools/client/model/FreeForm.java");
TestUtils.ensureDoesNotContainFile(files, output, "src/main/java/org/openapitools/client/model/FreeFormWithValidation.java");
TestUtils.ensureDoesNotContainFile(files, output, "src/main/java/org/openapitools/client/model/FreeFormInterface.java");
TestUtils.ensureDoesNotContainFile(files, output, "src/main/java/org/openapitools/client/model/FreeForm.java");
output.deleteOnExit();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,29 +86,29 @@ public void testIgnoreFileProcessing() throws IOException {
TestUtils.ensureContainsFile(files, output, "build.gradle");
Assert.assertTrue(new File(output, "build.gradle").exists());

TestUtils.ensureDoesNotContainsFile(files, output, "api/openapi.yaml");
TestUtils.ensureDoesNotContainFile(files, output, "api/openapi.yaml");
Assert.assertFalse(new File(output, "api").exists());

TestUtils.ensureDoesNotContainsFile(files, output, ".github/workflows/");
TestUtils.ensureDoesNotContainFile(files, output, ".github/workflows/");
Assert.assertFalse(new File(output, ".github").exists());

// Check excluded files
TestUtils.ensureDoesNotContainsFile(files, output, ".travis.yml");
TestUtils.ensureDoesNotContainFile(files, output, ".travis.yml");
Assert.assertFalse(new File(output, ".travis.yml").exists());

TestUtils.ensureDoesNotContainsFile(files, output, "build.sbt");
TestUtils.ensureDoesNotContainFile(files, output, "build.sbt");
Assert.assertFalse(new File(output, "build.sbt").exists());

TestUtils.ensureDoesNotContainsFile(files, output, "src/main/AndroidManifest.xml");
TestUtils.ensureDoesNotContainFile(files, output, "src/main/AndroidManifest.xml");
Assert.assertFalse(new File(output, "src/main/AndroidManifest.xml").exists());

TestUtils.ensureDoesNotContainsFile(files, output, "pom.xml");
TestUtils.ensureDoesNotContainFile(files, output, "pom.xml");
Assert.assertFalse(new File(output, "pom.xml").exists());

TestUtils.ensureDoesNotContainsFile(files, output, "src/test/java/org/openapitools/client/model/CategoryTest.java");
TestUtils.ensureDoesNotContainFile(files, output, "src/test/java/org/openapitools/client/model/CategoryTest.java");
Assert.assertFalse(new File(output, "src/test/java/org/openapitools/client/model/CategoryTest.java").exists());

TestUtils.ensureDoesNotContainsFile(files, output, "src/main/java/org/openapitools/client/api/UserApi.java");
TestUtils.ensureDoesNotContainFile(files, output, "src/main/java/org/openapitools/client/api/UserApi.java");
Assert.assertFalse(new File(output, "src/main/java/org/openapitools/client/api/UserApi.java").exists());
} finally {
output.deleteOnExit();
Expand Down Expand Up @@ -162,7 +162,7 @@ public void testFilesAreNeverOverwritten() throws IOException {
TestUtils.ensureContainsFile(files, output, "src/main/java/org/openapitools/client/api/PetApi.java");
Assert.assertTrue(new File(output, "src/main/java/org/openapitools/client/api/PetApi.java").exists());

TestUtils.ensureDoesNotContainsFile(files, output, apiTestRelativePath);
TestUtils.ensureDoesNotContainFile(files, output, apiTestRelativePath);
Assert.assertTrue(apiTestFile.exists());
String apiTestContents = Files.readAllLines(apiTestFile.toPath()).get(0);
Assert.assertEquals(apiTestContents, "empty", "Expected test file to retain original contents.");
Expand All @@ -171,7 +171,7 @@ public void testFilesAreNeverOverwritten() throws IOException {
TestUtils.ensureContainsFile(files, output, "src/main/java/org/openapitools/client/model/Category.java");
Assert.assertTrue(new File(output, "src/test/java/org/openapitools/client/model/CategoryTest.java").exists());

TestUtils.ensureDoesNotContainsFile(files, output, modelTestRelativePath);
TestUtils.ensureDoesNotContainFile(files, output, modelTestRelativePath);
Assert.assertTrue(modelTestFile.exists());
String modelTestContents = Files.readAllLines(modelTestFile.toPath()).get(0);
Assert.assertEquals(modelTestContents, "empty", "Expected test file to retain original contents.");
Expand Down Expand Up @@ -827,19 +827,19 @@ public void testGenerateRecursiveDependentModelsBackwardCompatibilityIssue18444(

// Check not generated cause backwards compatibility files
String ft1FileName = "src/main/java/org/openapitools/model/FT1.java";
TestUtils.ensureDoesNotContainsFile(files, output, ft1FileName);
TestUtils.ensureDoesNotContainFile(files, output, ft1FileName);
Assert.assertFalse(new File(output, ft1FileName).exists());

String ft2FileName = "src/main/java/org/openapitools/model/FT2.java";
TestUtils.ensureDoesNotContainsFile(files, output, ft2FileName);
TestUtils.ensureDoesNotContainFile(files, output, ft2FileName);
Assert.assertFalse(new File(output, ft2FileName).exists());

String ft3FileName = "src/main/java/org/openapitools/model/FT3.java";
TestUtils.ensureDoesNotContainsFile(files, output, ft3FileName);
TestUtils.ensureDoesNotContainFile(files, output, ft3FileName);
Assert.assertFalse(new File(output, ft3FileName).exists());

String bttFileName = "src/main/java/org/openapitools/model/BTT.java";
TestUtils.ensureDoesNotContainsFile(files, output, bttFileName);
TestUtils.ensureDoesNotContainFile(files, output, bttFileName);
Assert.assertFalse(new File(output, bttFileName).exists());

} finally {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public static void ensureContainsFile(List<File> generatedFiles, File root, Stri
assertTrue(generatedFiles.contains(path.toFile()), "File '" + path.toAbsolutePath() + "' was not found in the list of generated files");
}

public static void ensureDoesNotContainsFile(List<File> generatedFiles, File root, String filename) {
public static void ensureDoesNotContainFile(List<File> generatedFiles, File root, String filename) {
Path path = root.toPath().resolve(filename);
assertFalse(generatedFiles.contains(path.toFile()), "File '" + path.toAbsolutePath() + "' was found in the list of generated files");
}
Expand Down Expand Up @@ -217,7 +217,7 @@ public static void assertExtraAnnotationFiles(String baseOutputPath) {
.containsWithName("javax.persistence.Entity")
.containsWithNameAndAttributes("javax.persistence.Table", ImmutableMap.of("name", "\"employees\""))
.toType()
.hasProperty("assignments")
.assertProperty("assignments")
.assertPropertyAnnotations()
.containsWithNameAndAttributes("javax.persistence.OneToMany", ImmutableMap.of("mappedBy", "\"employee\""))
.toProperty()
Expand All @@ -227,17 +227,17 @@ public static void assertExtraAnnotationFiles(String baseOutputPath) {
.assertTypeAnnotations()
.containsWithName("javax.persistence.MappedSuperclass")
.toType()
.hasProperty("id")
.assertProperty("id")
.assertPropertyAnnotations()
.containsWithName("javax.persistence.Id")
.toProperty()
.toType()
.hasProperty("email")
.assertProperty("email")
.assertPropertyAnnotations()
.containsWithName("org.hibernate.annotations.Formula")
.toProperty()
.toType()
.hasProperty("hasAcceptedTerms")
.assertProperty("hasAcceptedTerms")
.assertPropertyAnnotations()
.containsWithName("javax.persistence.Transient")
.toProperty()
Expand All @@ -248,13 +248,13 @@ public static void assertExtraAnnotationFiles(String baseOutputPath) {
.containsWithName("javax.persistence.Entity")
.containsWithNameAndAttributes("javax.persistence.Table", ImmutableMap.of("name", "\"survey_groups\""))
.toType()
.hasProperty("assignments")
.assertProperty("assignments")
.assertPropertyAnnotations()
.containsWithName("javax.persistence.OneToMany")
.containsWithNameAndAttributes("javax.persistence.JoinColumn", ImmutableMap.of("name", "\"survey_group_id\""))
.toProperty()
.toType()
.hasProperty("disabled")
.assertProperty("disabled")
.assertPropertyAnnotations()
.containsWithNameAndAttributes("javax.persistence.Column", ImmutableMap.of("nullable", "false"))
.toProperty()
Expand All @@ -265,47 +265,47 @@ public static void assertExtraAnnotationFiles(String baseOutputPath) {
.containsWithName("javax.persistence.MappedSuperclass")
.containsWithName("javax.persistence.EntityListeners")
.toType()
.hasProperty("id")
.assertProperty("id")
.assertPropertyAnnotations()
.containsWithName("javax.persistence.Id")
.containsWithNameAndAttributes("javax.persistence.GeneratedValue", ImmutableMap.of("generator", "\"UUID\""))
.containsWithNameAndAttributes("org.hibernate.annotations.GenericGenerator", ImmutableMap.of("name", "\"UUID\"","strategy", "\"org.hibernate.id.UUIDGenerator\""))
.containsWithNameAndAttributes("javax.persistence.Column", ImmutableMap.of("name", "\"id\"","updatable", "false","nullable", "false"))
.toProperty()
.toType()
.hasProperty("createdDate")
.assertProperty("createdDate")
.assertPropertyAnnotations()
.containsWithName("org.springframework.data.annotation.CreatedDate")
.toProperty()
.toType()
.hasProperty("createdBy")
.assertProperty("createdBy")
.assertPropertyAnnotations()
.containsWithName("org.springframework.data.annotation.CreatedBy")
.toProperty()
.toType()
.hasProperty("modifiedDate")
.assertProperty("modifiedDate")
.assertPropertyAnnotations()
.containsWithName("org.springframework.data.annotation.LastModifiedDate")
.toProperty()
.toType()
.hasProperty("modifiedBy")
.assertProperty("modifiedBy")
.assertPropertyAnnotations()
.containsWithName("org.springframework.data.annotation.LastModifiedBy")
.toProperty()
.toType()
.hasProperty("opportunityId")
.assertProperty("opportunityId")
.assertPropertyAnnotations()
.containsWithNameAndAttributes("javax.persistence.Column", ImmutableMap.of("unique", "true"))
.toProperty()
.toType()
.hasProperty("submissionStatus")
.assertProperty("submissionStatus")
.assertPropertyAnnotations()
.containsWithName("javax.persistence.Transient")
.toProperty()
.toType();

JavaFileAssert.assertThat(java.nio.file.Paths.get(baseOutputPath + "/CompanyDto.java"))
.hasProperty("priceCategory")
.assertProperty("priceCategory")
.assertPropertyAnnotations()
.containsWithNameAndAttributes("IgnoreForRoles", ImmutableMap.of("value", "\"MEDIA_ADMIN\""));
}
Expand All @@ -319,4 +319,16 @@ public static ModelsMap createCodegenModelWrapper(CodegenModel cm) {
objs.setModels(modelMaps);
return objs;
}

public static Path newTempFolder() {
final Path tempDir;
try {
tempDir = Files.createTempDirectory("test");
} catch (IOException e) {
throw new RuntimeException(e);
}
tempDir.toFile().deleteOnExit();

return tempDir;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ private void assertFilesFromMergedSpec(String mergedSpec) throws IOException {
.assertMethodAnnotations()
.containsWithNameAndAttributes("RequestMapping", ImmutableMap.of("value", "\"/spec1/complex/{param1}/path\""))
.toMethod()
.hasParameter("param1")
.withType("String")
.assertParameter("param1")
.hasType("String")
.assertParameterAnnotations()
.containsWithNameAndAttributes("PathVariable", ImmutableMap.of("value", "\"param1\""));

Expand All @@ -92,4 +92,4 @@ private void assertFilesFromMergedSpec(String mergedSpec) throws IOException {
.assertMethod("getSpec2Field").hasReturnType("BigDecimal");
}

}
}
Loading

0 comments on commit ec8998b

Please sign in to comment.