|
1 | 1 | package org.openapitools.codegen; |
2 | 2 |
|
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; |
8 | 5 | import com.github.javaparser.JavaParser; |
9 | | -import com.github.javaparser.ParserConfiguration; |
10 | 6 | import com.github.javaparser.ParseResult; |
| 7 | +import com.github.javaparser.ParserConfiguration; |
11 | 8 | import com.github.javaparser.ast.CompilationUnit; |
| 9 | +import com.google.common.collect.ImmutableMap; |
12 | 10 | import io.swagger.parser.OpenAPIParser; |
13 | 11 | import io.swagger.v3.oas.models.Components; |
14 | 12 | import io.swagger.v3.oas.models.OpenAPI; |
|
17 | 15 | import io.swagger.v3.oas.models.media.Schema; |
18 | 16 | import io.swagger.v3.oas.models.servers.Server; |
19 | 17 | import io.swagger.v3.parser.core.models.ParseOptions; |
20 | | - |
21 | | -import org.apache.commons.io.IOUtils; |
22 | 18 | import org.openapitools.codegen.MockDefaultGenerator.WrittenTemplateBasedFile; |
23 | 19 | import org.openapitools.codegen.java.assertions.JavaFileAssert; |
24 | 20 | import org.openapitools.codegen.model.ModelMap; |
25 | 21 | import org.openapitools.codegen.model.ModelsMap; |
26 | 22 | import org.openapitools.codegen.utils.ModelUtils; |
27 | | -import org.openrewrite.maven.internal.RawPom; |
28 | 23 | import org.testng.Assert; |
29 | 24 |
|
30 | | -import java.io.ByteArrayInputStream; |
31 | 25 | import java.io.File; |
32 | 26 | import java.io.IOException; |
33 | | -import java.io.InputStream; |
34 | | -import java.nio.charset.StandardCharsets; |
35 | 27 | import java.nio.file.Files; |
36 | 28 | 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.*; |
42 | 30 |
|
43 | | -import com.google.common.collect.ImmutableMap; |
| 31 | +import static org.testng.Assert.*; |
44 | 32 |
|
45 | 33 | public class TestUtils { |
46 | 34 |
|
@@ -136,32 +124,29 @@ public static void ensureDoesNotContainsFile(List<File> generatedFiles, File roo |
136 | 124 | } |
137 | 125 |
|
138 | 126 | 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 | + }; |
151 | 142 | } |
152 | 143 |
|
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")); |
165 | 150 | } |
166 | 151 |
|
167 | 152 | public static void validateJavaSourceFiles(Map<String, String> fileMap) { |
|
0 commit comments