11package 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 ;
85import com .github .javaparser .JavaParser ;
9- import com .github .javaparser .ParserConfiguration ;
106import com .github .javaparser .ParseResult ;
7+ import com .github .javaparser .ParserConfiguration ;
118import com .github .javaparser .ast .CompilationUnit ;
9+ import com .google .common .collect .ImmutableMap ;
1210import io .swagger .parser .OpenAPIParser ;
1311import io .swagger .v3 .oas .models .Components ;
1412import io .swagger .v3 .oas .models .OpenAPI ;
1715import io .swagger .v3 .oas .models .media .Schema ;
1816import io .swagger .v3 .oas .models .servers .Server ;
1917import io .swagger .v3 .parser .core .models .ParseOptions ;
20-
21- import org .apache .commons .io .IOUtils ;
2218import org .openapitools .codegen .MockDefaultGenerator .WrittenTemplateBasedFile ;
2319import org .openapitools .codegen .java .assertions .JavaFileAssert ;
2420import org .openapitools .codegen .model .ModelMap ;
2521import org .openapitools .codegen .model .ModelsMap ;
2622import org .openapitools .codegen .utils .ModelUtils ;
27- import org .openrewrite .maven .internal .RawPom ;
2823import org .testng .Assert ;
2924
30- import java .io .ByteArrayInputStream ;
3125import java .io .File ;
3226import java .io .IOException ;
33- import java .io .InputStream ;
34- import java .nio .charset .StandardCharsets ;
3527import java .nio .file .Files ;
3628import 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
4533public class TestUtils {
4634
@@ -100,7 +88,7 @@ public static OpenAPI createOpenAPI() {
10088 return openAPI ;
10189 }
10290
103- public static OpenAPI createOpenAPIWithOneSchema (String name , Schema schema ) {
91+ public static OpenAPI createOpenAPIWithOneSchema (String name , Schema <?> schema ) {
10492 OpenAPI openAPI = createOpenAPI ();
10593 openAPI .setComponents (new Components ());
10694 openAPI .getComponents ().addSchemas (name , schema );
@@ -135,69 +123,57 @@ public static void ensureDoesNotContainsFile(List<File> generatedFiles, File roo
135123 assertFalse (generatedFiles .contains (path .toFile ()), "File '" + path .toAbsolutePath () + "' was found in the list of generated files" );
136124 }
137125
138- public static void validatePomXmlFiles (final Map <String , String > fileMap ) {
139- fileMap .forEach ( (fileName , fileContents ) -> {
140- if ("pom.xml" .equals (fileName )) {
141- assertValidPomXml (fileContents );
142- }
143- });
144- }
145-
146126 public static void validatePomXmlFiles (final List <File > files ) {
147- files .forEach ( f -> {
148- String fileName = f .getName ();
149- if ("pom.xml" .equals (fileName )) {
150- try {
151- String fileContents = new String (Files .readAllBytes (f .toPath ()), StandardCharsets .UTF_8 );
152- assertValidPomXml (fileContents );
153- } catch (IOException exception ) {
154- throw new RuntimeException (exception );
155- }
156- }
157- }
158- );
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+ };
159142 }
160143
161- private static void assertValidPomXml (final String fileContents ) {
162- final InputStream input = new ByteArrayInputStream (fileContents .getBytes (StandardCharsets .UTF_8 ));
163- try {
164- RawPom pom = RawPom .parse (input , null );
165- assertTrue (pom .getDependencies ().getDependencies ().size () > 0 );
166- assertNotNull (pom .getName ());
167- assertNotNull (pom .getArtifactId ());
168- assertNotNull (pom .getGroupId ());
169- assertNotNull (pom .getVersion ());
170- } finally {
171- IOUtils .closeQuietly (input );
172- }
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" ));
173150 }
174151
175152 public static void validateJavaSourceFiles (Map <String , String > fileMap ) {
176153 fileMap .forEach ( (fileName , fileContents ) -> {
177154 if (fileName .endsWith (".java" )) {
178- assertValidJavaSourceCode (fileContents , fileName );
155+ assertValidJavaSourceCode (fileContents );
179156 }
180157 }
181158 );
182159 }
183160
184161 public static void validateJavaSourceFiles (List <File > files ) {
185162 files .forEach ( f -> {
186- String fileName = f .getName ();
187- if (fileName .endsWith (".java" )) {
163+ if (f .getName ().endsWith (".java" )) {
188164 String fileContents = "" ;
189165 try {
190- fileContents = new String ( Files .readAllBytes (f .toPath ()), StandardCharsets . UTF_8 );
166+ fileContents = Files .readString (f .toPath ());
191167 } catch (IOException ignored ) {
192168
193169 }
194- assertValidJavaSourceCode (fileContents , fileName );
170+ assertValidJavaSourceCode (fileContents );
195171 }
196172 }
197173 );
198174 }
199175
200- public static void assertValidJavaSourceCode (String javaSourceCode , String filename ) {
176+ public static void assertValidJavaSourceCode (String javaSourceCode ) {
201177 ParserConfiguration config = new ParserConfiguration ();
202178 config .setLanguageLevel (ParserConfiguration .LanguageLevel .JAVA_11 );
203179 JavaParser parser = new JavaParser (config );
@@ -207,7 +183,7 @@ public static void assertValidJavaSourceCode(String javaSourceCode, String filen
207183
208184 public static void assertFileContains (Path path , String ... lines ) {
209185 try {
210- String generatedFile = new String ( Files .readAllBytes (path ), StandardCharsets . UTF_8 );
186+ String generatedFile = Files .readString (path );
211187 String file = linearize (generatedFile );
212188 assertNotNull (file );
213189 for (String line : lines )
@@ -224,7 +200,7 @@ public static String linearize(String target) {
224200 public static void assertFileNotContains (Path path , String ... lines ) {
225201 String generatedFile = null ;
226202 try {
227- generatedFile = new String ( Files .readAllBytes (path ), StandardCharsets . UTF_8 );
203+ generatedFile = Files .readString (path );
228204 } catch (IOException e ) {
229205 fail ("Unable to evaluate file " + path );
230206 }
@@ -236,7 +212,7 @@ public static void assertFileNotContains(Path path, String... lines) {
236212
237213 public static void assertFileNotExists (Path path ) {
238214 try {
239- new String ( Files .readAllBytes (path ), StandardCharsets . UTF_8 );
215+ Files .readString (path );
240216 fail ("File exists when it should not: " + path );
241217 } catch (IOException e ) {
242218 // File exists, pass.
@@ -246,7 +222,7 @@ public static void assertFileNotExists(Path path) {
246222
247223 public static void assertFileExists (Path path ) {
248224 try {
249- new String ( Files .readAllBytes (path ), StandardCharsets . UTF_8 );
225+ Files .readString (path );
250226 // File exists, pass.
251227 assertTrue (true );
252228 } catch (IOException e ) {
0 commit comments