diff --git a/modules/openapi-generator-cli/pom.xml b/modules/openapi-generator-cli/pom.xml index d11fc287ecae..878c7ff74e0a 100644 --- a/modules/openapi-generator-cli/pom.xml +++ b/modules/openapi-generator-cli/pom.xml @@ -6,7 +6,7 @@ 7.7.0-SNAPSHOT - ../.. + ../../pom.xml 4.0.0 openapi-generator-cli diff --git a/modules/openapi-generator-core/pom.xml b/modules/openapi-generator-core/pom.xml index fec9fba5535c..5096fd7096f3 100644 --- a/modules/openapi-generator-core/pom.xml +++ b/modules/openapi-generator-core/pom.xml @@ -8,7 +8,7 @@ 7.7.0-SNAPSHOT - ../.. + ../../pom.xml 4.0.0 diff --git a/modules/openapi-generator-gradle-plugin/pom.xml b/modules/openapi-generator-gradle-plugin/pom.xml index 07521222d478..e551fc10fa6c 100644 --- a/modules/openapi-generator-gradle-plugin/pom.xml +++ b/modules/openapi-generator-gradle-plugin/pom.xml @@ -6,7 +6,7 @@ 7.7.0-SNAPSHOT - ../.. + ../../pom.xml 4.0.0 diff --git a/modules/openapi-generator-maven-plugin/pom.xml b/modules/openapi-generator-maven-plugin/pom.xml index cf86efe11033..828fee9093f8 100644 --- a/modules/openapi-generator-maven-plugin/pom.xml +++ b/modules/openapi-generator-maven-plugin/pom.xml @@ -7,7 +7,7 @@ 7.7.0-SNAPSHOT - ../.. + ../../pom.xml openapi-generator-maven-plugin openapi-generator (maven-plugin) diff --git a/modules/openapi-generator-online/pom.xml b/modules/openapi-generator-online/pom.xml index fdf1637d661c..55f76df63c80 100644 --- a/modules/openapi-generator-online/pom.xml +++ b/modules/openapi-generator-online/pom.xml @@ -6,7 +6,7 @@ 7.7.0-SNAPSHOT - ../.. + ../../pom.xml openapi-generator-online jar diff --git a/modules/openapi-generator/pom.xml b/modules/openapi-generator/pom.xml index 9f4117a07111..f173b71f0ee9 100644 --- a/modules/openapi-generator/pom.xml +++ b/modules/openapi-generator/pom.xml @@ -6,7 +6,7 @@ 7.7.0-SNAPSHOT - ../.. + ../../pom.xml 4.0.0 openapi-generator @@ -349,17 +349,6 @@ 3.24.9 test - - org.openrewrite - rewrite-maven - test - - - org.reflections - reflections - ${reflections.version} - test - com.googlecode.java-diff-utils diffutils @@ -392,6 +381,12 @@ jackson-databind ${jackson-databind.version} + + com.fasterxml.jackson.dataformat + jackson-dataformat-xml + ${jackson.version} + test + com.fasterxml.jackson.datatype jackson-datatype-joda diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/TestUtils.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/TestUtils.java index 045e88985745..36c78ab272ff 100644 --- a/modules/openapi-generator/src/test/java/org/openapitools/codegen/TestUtils.java +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/TestUtils.java @@ -1,14 +1,12 @@ package org.openapitools.codegen; -import static org.testng.Assert.assertNotNull; -import static org.testng.Assert.fail; -import static org.testng.Assert.assertTrue; -import static org.testng.Assert.assertFalse; - +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.dataformat.xml.XmlMapper; import com.github.javaparser.JavaParser; -import com.github.javaparser.ParserConfiguration; import com.github.javaparser.ParseResult; +import com.github.javaparser.ParserConfiguration; import com.github.javaparser.ast.CompilationUnit; +import com.google.common.collect.ImmutableMap; import io.swagger.parser.OpenAPIParser; import io.swagger.v3.oas.models.Components; import io.swagger.v3.oas.models.OpenAPI; @@ -17,30 +15,20 @@ import io.swagger.v3.oas.models.media.Schema; import io.swagger.v3.oas.models.servers.Server; import io.swagger.v3.parser.core.models.ParseOptions; - -import org.apache.commons.io.IOUtils; import org.openapitools.codegen.MockDefaultGenerator.WrittenTemplateBasedFile; import org.openapitools.codegen.java.assertions.JavaFileAssert; import org.openapitools.codegen.model.ModelMap; import org.openapitools.codegen.model.ModelsMap; import org.openapitools.codegen.utils.ModelUtils; -import org.openrewrite.maven.internal.RawPom; import org.testng.Assert; -import java.io.ByteArrayInputStream; import java.io.File; import java.io.IOException; -import java.io.InputStream; -import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; -import java.util.Map; -import java.util.Optional; +import java.util.*; -import com.google.common.collect.ImmutableMap; +import static org.testng.Assert.*; public class TestUtils { @@ -100,7 +88,7 @@ public static OpenAPI createOpenAPI() { return openAPI; } - public static OpenAPI createOpenAPIWithOneSchema(String name, Schema schema) { + public static OpenAPI createOpenAPIWithOneSchema(String name, Schema schema) { OpenAPI openAPI = createOpenAPI(); openAPI.setComponents(new Components()); openAPI.getComponents().addSchemas(name, schema); @@ -135,47 +123,36 @@ public static void ensureDoesNotContainsFile(List generatedFiles, File roo assertFalse(generatedFiles.contains(path.toFile()), "File '" + path.toAbsolutePath() + "' was found in the list of generated files"); } - public static void validatePomXmlFiles(final Map fileMap) { - fileMap.forEach( (fileName, fileContents) -> { - if ("pom.xml".equals(fileName)) { - assertValidPomXml(fileContents); - } - }); - } - public static void validatePomXmlFiles(final List files) { - files.forEach( f -> { - String fileName = f.getName(); - if ("pom.xml".equals(fileName)) { - try { - String fileContents = new String(Files.readAllBytes(f.toPath()), StandardCharsets.UTF_8); - assertValidPomXml(fileContents); - } catch (IOException exception) { - throw new RuntimeException(exception); - } - } - } - ); + if (files == null + || files.isEmpty() + || files.stream().noneMatch(f -> f.getName().equals("pom.xml"))) return; + + final XmlMapper mapper = new XmlMapper(); + for (File file : files) { + if (!"pom.xml".equals(file.getName())) continue; + + try { + JsonNode pomContents = mapper.readTree(file); + assertValidPomXml(pomContents); + } catch (IOException exception) { + throw new RuntimeException(exception); + } + }; } - private static void assertValidPomXml(final String fileContents) { - final InputStream input = new ByteArrayInputStream(fileContents.getBytes(StandardCharsets.UTF_8)); - try { - RawPom pom = RawPom.parse(input, null); - assertTrue(pom.getDependencies().getDependencies().size() > 0); - assertNotNull(pom.getName()); - assertNotNull(pom.getArtifactId()); - assertNotNull(pom.getGroupId()); - assertNotNull(pom.getVersion()); - } finally { - IOUtils.closeQuietly(input); - } + private static void assertValidPomXml(final JsonNode pom) { + assertFalse(pom.path("dependencies").isEmpty()); + assertNotNull(pom.get("name")); + assertNotNull(pom.get("artifactId")); + assertNotNull(pom.get("groupId")); + assertNotNull(pom.get("version")); } public static void validateJavaSourceFiles(Map fileMap) { fileMap.forEach( (fileName, fileContents) -> { if (fileName.endsWith(".java")) { - assertValidJavaSourceCode(fileContents, fileName); + assertValidJavaSourceCode(fileContents); } } ); @@ -183,21 +160,20 @@ public static void validateJavaSourceFiles(Map fileMap) { public static void validateJavaSourceFiles(List files) { files.forEach( f -> { - String fileName = f.getName(); - if (fileName.endsWith(".java")) { + if (f.getName().endsWith(".java")) { String fileContents = ""; try { - fileContents = new String(Files.readAllBytes(f.toPath()), StandardCharsets.UTF_8); + fileContents = Files.readString(f.toPath()); } catch (IOException ignored) { } - assertValidJavaSourceCode(fileContents, fileName); + assertValidJavaSourceCode(fileContents); } } ); } - public static void assertValidJavaSourceCode(String javaSourceCode, String filename) { + public static void assertValidJavaSourceCode(String javaSourceCode) { ParserConfiguration config = new ParserConfiguration(); config.setLanguageLevel(ParserConfiguration.LanguageLevel.JAVA_11); JavaParser parser = new JavaParser(config); @@ -207,7 +183,7 @@ public static void assertValidJavaSourceCode(String javaSourceCode, String filen public static void assertFileContains(Path path, String... lines) { try { - String generatedFile = new String(Files.readAllBytes(path), StandardCharsets.UTF_8); + String generatedFile = Files.readString(path); String file = linearize(generatedFile); assertNotNull(file); for (String line : lines) @@ -224,7 +200,7 @@ public static String linearize(String target) { public static void assertFileNotContains(Path path, String... lines) { String generatedFile = null; try { - generatedFile = new String(Files.readAllBytes(path), StandardCharsets.UTF_8); + generatedFile = Files.readString(path); } catch (IOException e) { fail("Unable to evaluate file " + path); } @@ -236,7 +212,7 @@ public static void assertFileNotContains(Path path, String... lines) { public static void assertFileNotExists(Path path) { try { - new String(Files.readAllBytes(path), StandardCharsets.UTF_8); + Files.readString(path); fail("File exists when it should not: " + path); } catch (IOException e) { // File exists, pass. @@ -246,7 +222,7 @@ public static void assertFileNotExists(Path path) { public static void assertFileExists(Path path) { try { - new String(Files.readAllBytes(path), StandardCharsets.UTF_8); + Files.readString(path); // File exists, pass. assertTrue(true); } catch (IOException e) { diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/TestUtilsTest.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/TestUtilsTest.java new file mode 100644 index 000000000000..1bc8ba5090f9 --- /dev/null +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/TestUtilsTest.java @@ -0,0 +1,175 @@ +package org.openapitools.codegen; + +import org.testng.annotations.Test; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.List; +import java.util.Locale; + +import static org.assertj.core.api.Assertions.assertThatCode; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +@SuppressWarnings("NewClassNamingConvention") +public class TestUtilsTest { + + // forbiddenapis check will fail if we don't explicitly define localization when using String.format + static final Locale L = null; + + public static class validatePomXmlFiles { + + static final String POM_SCAFFOLD = "%s%s"; + static final String POM_PROJECT_INFO = "foofoo.barfoobar13.12"; + + @Test void doesNotThrow_ifNotPom_doesNotExist() { + final var vaporFile = newTempDir().resolve("other.xml").toFile(); + + assertThatCode(() -> TestUtils.validatePomXmlFiles(List.of(vaporFile))) + .doesNotThrowAnyException(); + } + + @Test void throwsRuntimeException_ifPomDoesNotExist() { + final var vaporBom = newTempDir().resolve("pom.xml").toFile(); + + assertThatThrownBy(() -> TestUtils.validatePomXmlFiles(List.of(vaporBom))) + .isExactlyInstanceOf(RuntimeException.class); + } + + @Test void throwsRuntimeException_ifXmlIsJson() { + Path testFile = newPomXmlFile("{\"not_xml\": 12345}"); + + assertThatThrownBy(() -> TestUtils.validatePomXmlFiles(List.of(testFile.toFile()))) + .isExactlyInstanceOf(RuntimeException.class); + } + + @Test void throwsRuntimeException_ifXmlIsInvalid() { + final Path testFile = newPomXmlFile(""); + + assertThatThrownBy(() -> TestUtils.validatePomXmlFiles(List.of(testFile.toFile()))) + .isExactlyInstanceOf(RuntimeException.class); + } + + @Test void throwsAssertionError_ifNameTagIsMissing() { + final Path testFile = newPomXmlFile(replaceTag("name", "", getMinimalValidPomContent())); + + assertThatThrownBy(() -> TestUtils.validatePomXmlFiles(List.of(testFile.toFile()))) + .isExactlyInstanceOf(AssertionError.class); + } + + @Test void doesNotThrow_ifNameIsEmpty() { + final Path testFile = newPomXmlFile( + replaceTag("name", "", getMinimalValidPomContent()) + ); + + assertThatCode(() -> TestUtils.validatePomXmlFiles(List.of(testFile.toFile()))) + .doesNotThrowAnyException(); + } + + @Test void throwsAssertionError_ifArtifactIdTagIsMissing() { + final Path testFile = newPomXmlFile(replaceTag("artifactId", "", getMinimalValidPomContent())); + + assertThatThrownBy(() -> TestUtils.validatePomXmlFiles(List.of(testFile.toFile()))) + .isExactlyInstanceOf(AssertionError.class); + } + + @Test void doesNotThrow_ifArtifactIdIsEmpty() { + final Path testFile = newPomXmlFile( + replaceTag("artifactId", "", getMinimalValidPomContent()) + ); + + assertThatCode(() -> TestUtils.validatePomXmlFiles(List.of(testFile.toFile()))) + .doesNotThrowAnyException(); + } + + @Test void throwsAssertionError_ifGroupIdTagIsMissing() { + final Path testFile = newPomXmlFile(replaceTag("groupId", "", getMinimalValidPomContent())); + + assertThatThrownBy(() -> TestUtils.validatePomXmlFiles(List.of(testFile.toFile()))) + .isExactlyInstanceOf(AssertionError.class); + } + + @Test void doesNotThrow_ifGroupIdIsEmpty() { + final Path testFile = newPomXmlFile( + replaceTag("groupId", "", getMinimalValidPomContent()) + ); + + assertThatCode(() -> TestUtils.validatePomXmlFiles(List.of(testFile.toFile()))) + .doesNotThrowAnyException(); + } + + @Test void throwsAssertionError_ifVersionTagIsMissing() { + final Path testFile = newPomXmlFile(replaceTag("version", "", getMinimalValidPomContent())); + + assertThatThrownBy(() -> TestUtils.validatePomXmlFiles(List.of(testFile.toFile()))) + .isExactlyInstanceOf(AssertionError.class); + } + + @Test void doesNotThrow_ifVersionIsEmpty() { + final Path testFile = newPomXmlFile( + replaceTag("version", "", getMinimalValidPomContent()) + ); + + assertThatCode(() -> TestUtils.validatePomXmlFiles(List.of(testFile.toFile()))) + .doesNotThrowAnyException(); + } + + @Test void throwsAssertionError_ifZeroDependencies() { + final Path testFile = newPomXmlFile(String.format(L, POM_SCAFFOLD, POM_PROJECT_INFO, "")); + + assertThatThrownBy(() -> TestUtils.validatePomXmlFiles(List.of(testFile.toFile()))) + .isExactlyInstanceOf(AssertionError.class); + } + + @Test void doesNotThrow_ifAtLeastOneDependency() { + final Path testFile = newPomXmlFile(String.format(L, POM_SCAFFOLD, POM_PROJECT_INFO, "")); + + assertThatCode(() -> TestUtils.validatePomXmlFiles(List.of(testFile.toFile()))) + .doesNotThrowAnyException(); + } + + @Test void throwsAssertionError_withTwoValidPoms_whereSecondHasNoName() { + final File testFile1 = newPomXmlFile(getMinimalValidPomContent()).toFile(); + final File testFile2 = newPomXmlFile(replaceTag("name", "", getMinimalValidPomContent())).toFile(); + + assertThatThrownBy(() -> TestUtils.validatePomXmlFiles(List.of(testFile1, testFile2))) + .isExactlyInstanceOf(AssertionError.class); + } + + @Test void doesNotThrow_withTwoValidPoms() { + final File testFile1 = newPomXmlFile(getMinimalValidPomContent()).toFile(); + final File testFile2 = newPomXmlFile(getMinimalValidPomContent()).toFile(); + + assertThatCode(() -> TestUtils.validatePomXmlFiles(List.of(testFile1, testFile2))) + .doesNotThrowAnyException(); + } + + private String replaceTag(String tagToReplace, String replaceWith, String xml) { + return xml.replaceAll("<" + tagToReplace + ">[\\s\\S]*?", replaceWith); + } + + private String getMinimalValidPomContent() { + return String.format(L, POM_SCAFFOLD, POM_PROJECT_INFO, ""); + } + + private Path newPomXmlFile(String content) { + try { + return Files.writeString(newTempDir().resolve("pom.xml"), content); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + private Path newTempDir() { + final Path tempDir; + try { + tempDir = Files.createTempDirectory("test"); + } catch (IOException e) { + throw new RuntimeException(e); + } + tempDir.toFile().deleteOnExit(); + return tempDir; + } + } +} \ No newline at end of file diff --git a/pom.xml b/pom.xml index 74f9e116296e..fdb4cd90d1af 100644 --- a/pom.xml +++ b/pom.xml @@ -1198,12 +1198,6 @@ ${testng.version} test - - org.openrewrite - rewrite-maven - ${openrewrite.version} - test - @@ -1248,9 +1242,7 @@ 4.0.0-M8 3.2.5 4.10.0 - 8.8.3 3.12.0 - 0.10.2 1.9.18 1.4 4.6.1