Skip to content

Commit f5c22bd

Browse files
committed
Reimplement TestUtils.validatePomXmlFiles(List) with Jackson XML mapper
1 parent d083fbc commit f5c22bd

2 files changed

Lines changed: 31 additions & 47 deletions

File tree

modules/openapi-generator/src/test/java/org/openapitools/codegen/TestUtils.java

Lines changed: 27 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
package org.openapitools.codegen;
22

3-
import static org.testng.Assert.assertNotNull;
4-
import static org.testng.Assert.fail;
5-
import static org.testng.Assert.assertTrue;
6-
import static org.testng.Assert.assertFalse;
7-
3+
import com.fasterxml.jackson.databind.JsonNode;
4+
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
85
import com.github.javaparser.JavaParser;
9-
import com.github.javaparser.ParserConfiguration;
106
import com.github.javaparser.ParseResult;
7+
import com.github.javaparser.ParserConfiguration;
118
import com.github.javaparser.ast.CompilationUnit;
9+
import com.google.common.collect.ImmutableMap;
1210
import io.swagger.parser.OpenAPIParser;
1311
import io.swagger.v3.oas.models.Components;
1412
import io.swagger.v3.oas.models.OpenAPI;
@@ -17,30 +15,20 @@
1715
import io.swagger.v3.oas.models.media.Schema;
1816
import io.swagger.v3.oas.models.servers.Server;
1917
import io.swagger.v3.parser.core.models.ParseOptions;
20-
21-
import org.apache.commons.io.IOUtils;
2218
import org.openapitools.codegen.MockDefaultGenerator.WrittenTemplateBasedFile;
2319
import org.openapitools.codegen.java.assertions.JavaFileAssert;
2420
import org.openapitools.codegen.model.ModelMap;
2521
import org.openapitools.codegen.model.ModelsMap;
2622
import org.openapitools.codegen.utils.ModelUtils;
27-
import org.openrewrite.maven.internal.RawPom;
2823
import org.testng.Assert;
2924

30-
import java.io.ByteArrayInputStream;
3125
import java.io.File;
3226
import java.io.IOException;
33-
import java.io.InputStream;
34-
import java.nio.charset.StandardCharsets;
3527
import java.nio.file.Files;
3628
import java.nio.file.Path;
37-
import java.util.ArrayList;
38-
import java.util.Collections;
39-
import java.util.List;
40-
import java.util.Map;
41-
import java.util.Optional;
29+
import java.util.*;
4230

43-
import com.google.common.collect.ImmutableMap;
31+
import static org.testng.Assert.*;
4432

4533
public class TestUtils {
4634

@@ -136,32 +124,29 @@ public static void ensureDoesNotContainsFile(List<File> generatedFiles, File roo
136124
}
137125

138126
public static void validatePomXmlFiles(final List<File> files) {
139-
files.forEach( f -> {
140-
String fileName = f.getName();
141-
if ("pom.xml".equals(fileName)) {
142-
try {
143-
String fileContents = Files.readString(f.toPath());
144-
assertValidPomXml(fileContents);
145-
} catch (IOException exception) {
146-
throw new RuntimeException(exception);
147-
}
148-
}
149-
}
150-
);
127+
if (files == null
128+
|| files.isEmpty()
129+
|| files.stream().noneMatch(f -> f.getName().equals("pom.xml"))) return;
130+
131+
final XmlMapper mapper = new XmlMapper();
132+
for (File file : files) {
133+
if (!"pom.xml".equals(file.getName())) continue;
134+
135+
try {
136+
JsonNode pomContents = mapper.readTree(file);
137+
assertValidPomXml(pomContents);
138+
} catch (IOException exception) {
139+
throw new RuntimeException(exception);
140+
}
141+
};
151142
}
152143

153-
private static void assertValidPomXml(final String fileContents) {
154-
final InputStream input = new ByteArrayInputStream(fileContents.getBytes(StandardCharsets.UTF_8));
155-
try {
156-
RawPom pom = RawPom.parse(input, null);
157-
assertTrue(pom.getDependencies().getDependencies().size() > 0);
158-
assertNotNull(pom.getName());
159-
assertNotNull(pom.getArtifactId());
160-
assertNotNull(pom.getGroupId());
161-
assertNotNull(pom.getVersion());
162-
} finally {
163-
IOUtils.closeQuietly(input);
164-
}
144+
private static void assertValidPomXml(final JsonNode pom) {
145+
assertFalse(pom.path("dependencies").isEmpty());
146+
assertNotNull(pom.get("name"));
147+
assertNotNull(pom.get("artifactId"));
148+
assertNotNull(pom.get("groupId"));
149+
assertNotNull(pom.get("version"));
165150
}
166151

167152
public static void validateJavaSourceFiles(Map<String, String> fileMap) {

modules/openapi-generator/src/test/java/org/openapitools/codegen/TestUtilsTest.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
import java.io.File;
66
import java.io.IOException;
7-
import java.io.UncheckedIOException;
87
import java.nio.file.Files;
98
import java.nio.file.Path;
109
import java.util.List;
@@ -38,18 +37,18 @@ public static class validatePomXmlFiles {
3837
.isExactlyInstanceOf(RuntimeException.class);
3938
}
4039

41-
@Test void throwsUncheckedIoException_ifXmlIsJson() {
40+
@Test void throwsRuntimeException_ifXmlIsJson() {
4241
Path testFile = newPomXmlFile("{\"not_xml\": 12345}");
4342

4443
assertThatThrownBy(() -> TestUtils.validatePomXmlFiles(List.of(testFile.toFile())))
45-
.isExactlyInstanceOf(UncheckedIOException.class);
44+
.isExactlyInstanceOf(RuntimeException.class);
4645
}
4746

48-
@Test void throwsUncheckedIoException_ifXmlIsInvalid() {
47+
@Test void throwsRuntimeException_ifXmlIsInvalid() {
4948
final Path testFile = newPomXmlFile("<IAmNotClosed>");
5049

5150
assertThatThrownBy(() -> TestUtils.validatePomXmlFiles(List.of(testFile.toFile())))
52-
.isExactlyInstanceOf(UncheckedIOException.class);
51+
.isExactlyInstanceOf(RuntimeException.class);
5352
}
5453

5554
@Test void throwsAssertionError_ifNameTagIsMissing() {

0 commit comments

Comments
 (0)