Skip to content

Commit

Permalink
Remove unused test dependencies org.reflections & maven-rewrite (O…
Browse files Browse the repository at this point in the history
…penAPITools#18798)

* Specify exact relative path to parent pom.xml

* Remove org.reflections from the test stack

* Remove unused parameter

* Remove unused method

* Fix raw use of parameterized variable warning

* Use Files.readString instead of converting raw bytes

* Add test coverage for TestUtils.validatePomXmlFiles(List)

* Reimplement TestUtils.validatePomXmlFiles(List) with Jackson XML mapper

* Remove maven-rewrite dependency
  • Loading branch information
Philzen authored and welshm committed Jun 5, 2024
1 parent 435eef2 commit a1dafd0
Show file tree
Hide file tree
Showing 9 changed files with 224 additions and 86 deletions.
2 changes: 1 addition & 1 deletion modules/openapi-generator-cli/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<!-- RELEASE_VERSION -->
<version>7.7.0-SNAPSHOT</version>
<!-- /RELEASE_VERSION -->
<relativePath>../..</relativePath>
<relativePath>../../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>openapi-generator-cli</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion modules/openapi-generator-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<!-- RELEASE_VERSION -->
<version>7.7.0-SNAPSHOT</version>
<!-- /RELEASE_VERSION -->
<relativePath>../..</relativePath>
<relativePath>../../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
2 changes: 1 addition & 1 deletion modules/openapi-generator-gradle-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<!-- RELEASE_VERSION -->
<version>7.7.0-SNAPSHOT</version>
<!-- /RELEASE_VERSION -->
<relativePath>../..</relativePath>
<relativePath>../../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
2 changes: 1 addition & 1 deletion modules/openapi-generator-maven-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<!-- RELEASE_VERSION -->
<version>7.7.0-SNAPSHOT</version>
<!-- /RELEASE_VERSION -->
<relativePath>../..</relativePath>
<relativePath>../../pom.xml</relativePath>
</parent>
<artifactId>openapi-generator-maven-plugin</artifactId>
<name>openapi-generator (maven-plugin)</name>
Expand Down
2 changes: 1 addition & 1 deletion modules/openapi-generator-online/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<!-- RELEASE_VERSION -->
<version>7.7.0-SNAPSHOT</version>
<!-- /RELEASE_VERSION -->
<relativePath>../..</relativePath>
<relativePath>../../pom.xml</relativePath>
</parent>
<artifactId>openapi-generator-online</artifactId>
<packaging>jar</packaging>
Expand Down
19 changes: 7 additions & 12 deletions modules/openapi-generator/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<!-- RELEASE_VERSION -->
<version>7.7.0-SNAPSHOT</version>
<!-- /RELEASE_VERSION -->
<relativePath>../..</relativePath>
<relativePath>../../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>openapi-generator</artifactId>
Expand Down Expand Up @@ -349,17 +349,6 @@
<version>3.24.9</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.openrewrite</groupId>
<artifactId>rewrite-maven</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.reflections</groupId>
<artifactId>reflections</artifactId>
<version>${reflections.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.googlecode.java-diff-utils</groupId>
<artifactId>diffutils</artifactId>
Expand Down Expand Up @@ -392,6 +381,12 @@
<artifactId>jackson-databind</artifactId>
<version>${jackson-databind.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
<version>${jackson.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-joda</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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 {

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -135,69 +123,57 @@ public static void ensureDoesNotContainsFile(List<File> 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<String, String> fileMap) {
fileMap.forEach( (fileName, fileContents) -> {
if ("pom.xml".equals(fileName)) {
assertValidPomXml(fileContents);
}
});
}

public static void validatePomXmlFiles(final List<File> 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<String, String> fileMap) {
fileMap.forEach( (fileName, fileContents) -> {
if (fileName.endsWith(".java")) {
assertValidJavaSourceCode(fileContents, fileName);
assertValidJavaSourceCode(fileContents);
}
}
);
}

public static void validateJavaSourceFiles(List<File> 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);
Expand All @@ -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)
Expand All @@ -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);
}
Expand All @@ -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.
Expand All @@ -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) {
Expand Down
Loading

0 comments on commit a1dafd0

Please sign in to comment.