1+ package org .openapitools .codegen ;
2+
3+ import org .testng .annotations .Test ;
4+
5+ import java .io .File ;
6+ import java .io .IOException ;
7+ import java .io .UncheckedIOException ;
8+ import java .nio .file .Files ;
9+ import java .nio .file .Path ;
10+ import java .util .List ;
11+ import java .util .Locale ;
12+
13+ import static org .assertj .core .api .Assertions .assertThatCode ;
14+ import static org .assertj .core .api .Assertions .assertThatThrownBy ;
15+
16+ @ SuppressWarnings ("NewClassNamingConvention" )
17+ public class TestUtilsTest {
18+
19+ // forbiddenapis check will fail if we don't explicitly define localization when using String.format
20+ static final Locale L = null ;
21+
22+ public static class validatePomXmlFiles {
23+
24+ static final String POM_SCAFFOLD = "<project>%s<dependencies>%s</dependencies></project>" ;
25+ static final String POM_PROJECT_INFO = "<name>foo</name><artifactId>foo.bar</artifactId><groupId>foobar</groupId><version>13.12</version>" ;
26+
27+ @ Test void doesNotThrow_ifNotPom_doesNotExist () {
28+ final var vaporFile = newTempDir ().resolve ("other.xml" ).toFile ();
29+
30+ assertThatCode (() -> TestUtils .validatePomXmlFiles (List .of (vaporFile )))
31+ .doesNotThrowAnyException ();
32+ }
33+
34+ @ Test void throwsRuntimeException_ifPomDoesNotExist () {
35+ final var vaporBom = newTempDir ().resolve ("pom.xml" ).toFile ();
36+
37+ assertThatThrownBy (() -> TestUtils .validatePomXmlFiles (List .of (vaporBom )))
38+ .isExactlyInstanceOf (RuntimeException .class );
39+ }
40+
41+ @ Test void throwsUncheckedIoException_ifXmlIsJson () {
42+ Path testFile = newPomXmlFile ("{\" not_xml\" : 12345}" );
43+
44+ assertThatThrownBy (() -> TestUtils .validatePomXmlFiles (List .of (testFile .toFile ())))
45+ .isExactlyInstanceOf (UncheckedIOException .class );
46+ }
47+
48+ @ Test void throwsUncheckedIoException_ifXmlIsInvalid () {
49+ final Path testFile = newPomXmlFile ("<IAmNotClosed>" );
50+
51+ assertThatThrownBy (() -> TestUtils .validatePomXmlFiles (List .of (testFile .toFile ())))
52+ .isExactlyInstanceOf (UncheckedIOException .class );
53+ }
54+
55+ @ Test void throwsAssertionError_ifNameTagIsMissing () {
56+ final Path testFile = newPomXmlFile (replaceTag ("name" , "" , getMinimalValidPomContent ()));
57+
58+ assertThatThrownBy (() -> TestUtils .validatePomXmlFiles (List .of (testFile .toFile ())))
59+ .isExactlyInstanceOf (AssertionError .class );
60+ }
61+
62+ @ Test void doesNotThrow_ifNameIsEmpty () {
63+ final Path testFile = newPomXmlFile (
64+ replaceTag ("name" , "<name></name>" , getMinimalValidPomContent ())
65+ );
66+
67+ assertThatCode (() -> TestUtils .validatePomXmlFiles (List .of (testFile .toFile ())))
68+ .doesNotThrowAnyException ();
69+ }
70+
71+ @ Test void throwsAssertionError_ifArtifactIdTagIsMissing () {
72+ final Path testFile = newPomXmlFile (replaceTag ("artifactId" , "" , getMinimalValidPomContent ()));
73+
74+ assertThatThrownBy (() -> TestUtils .validatePomXmlFiles (List .of (testFile .toFile ())))
75+ .isExactlyInstanceOf (AssertionError .class );
76+ }
77+
78+ @ Test void doesNotThrow_ifArtifactIdIsEmpty () {
79+ final Path testFile = newPomXmlFile (
80+ replaceTag ("artifactId" , "<artifactId></artifactId>" , getMinimalValidPomContent ())
81+ );
82+
83+ assertThatCode (() -> TestUtils .validatePomXmlFiles (List .of (testFile .toFile ())))
84+ .doesNotThrowAnyException ();
85+ }
86+
87+ @ Test void throwsAssertionError_ifGroupIdTagIsMissing () {
88+ final Path testFile = newPomXmlFile (replaceTag ("groupId" , "" , getMinimalValidPomContent ()));
89+
90+ assertThatThrownBy (() -> TestUtils .validatePomXmlFiles (List .of (testFile .toFile ())))
91+ .isExactlyInstanceOf (AssertionError .class );
92+ }
93+
94+ @ Test void doesNotThrow_ifGroupIdIsEmpty () {
95+ final Path testFile = newPomXmlFile (
96+ replaceTag ("groupId" , "<groupId></groupId>" , getMinimalValidPomContent ())
97+ );
98+
99+ assertThatCode (() -> TestUtils .validatePomXmlFiles (List .of (testFile .toFile ())))
100+ .doesNotThrowAnyException ();
101+ }
102+
103+ @ Test void throwsAssertionError_ifVersionTagIsMissing () {
104+ final Path testFile = newPomXmlFile (replaceTag ("version" , "" , getMinimalValidPomContent ()));
105+
106+ assertThatThrownBy (() -> TestUtils .validatePomXmlFiles (List .of (testFile .toFile ())))
107+ .isExactlyInstanceOf (AssertionError .class );
108+ }
109+
110+ @ Test void doesNotThrow_ifVersionIsEmpty () {
111+ final Path testFile = newPomXmlFile (
112+ replaceTag ("version" , "<version></version>" , getMinimalValidPomContent ())
113+ );
114+
115+ assertThatCode (() -> TestUtils .validatePomXmlFiles (List .of (testFile .toFile ())))
116+ .doesNotThrowAnyException ();
117+ }
118+
119+ @ Test void throwsAssertionError_ifZeroDependencies () {
120+ final Path testFile = newPomXmlFile (String .format (L , POM_SCAFFOLD , POM_PROJECT_INFO , "" ));
121+
122+ assertThatThrownBy (() -> TestUtils .validatePomXmlFiles (List .of (testFile .toFile ())))
123+ .isExactlyInstanceOf (AssertionError .class );
124+ }
125+
126+ @ Test void doesNotThrow_ifAtLeastOneDependency () {
127+ final Path testFile = newPomXmlFile (String .format (L , POM_SCAFFOLD , POM_PROJECT_INFO , "<dependency />" ));
128+
129+ assertThatCode (() -> TestUtils .validatePomXmlFiles (List .of (testFile .toFile ())))
130+ .doesNotThrowAnyException ();
131+ }
132+
133+ @ Test void throwsAssertionError_withTwoValidPoms_whereSecondHasNoName () {
134+ final File testFile1 = newPomXmlFile (getMinimalValidPomContent ()).toFile ();
135+ final File testFile2 = newPomXmlFile (replaceTag ("name" , "" , getMinimalValidPomContent ())).toFile ();
136+
137+ assertThatThrownBy (() -> TestUtils .validatePomXmlFiles (List .of (testFile1 , testFile2 )))
138+ .isExactlyInstanceOf (AssertionError .class );
139+ }
140+
141+ @ Test void doesNotThrow_withTwoValidPoms () {
142+ final File testFile1 = newPomXmlFile (getMinimalValidPomContent ()).toFile ();
143+ final File testFile2 = newPomXmlFile (getMinimalValidPomContent ()).toFile ();
144+
145+ assertThatCode (() -> TestUtils .validatePomXmlFiles (List .of (testFile1 , testFile2 )))
146+ .doesNotThrowAnyException ();
147+ }
148+
149+ private String replaceTag (String tagToReplace , String replaceWith , String xml ) {
150+ return xml .replaceAll ("<" + tagToReplace + ">[\\ s\\ S]*?</" + tagToReplace + ">" , replaceWith );
151+ }
152+
153+ private String getMinimalValidPomContent () {
154+ return String .format (L , POM_SCAFFOLD , POM_PROJECT_INFO , "<dependency />" );
155+ }
156+
157+ private Path newPomXmlFile (String content ) {
158+ try {
159+ return Files .writeString (newTempDir ().resolve ("pom.xml" ), content );
160+ } catch (IOException e ) {
161+ throw new RuntimeException (e );
162+ }
163+ }
164+
165+ private Path newTempDir () {
166+ final Path tempDir ;
167+ try {
168+ tempDir = Files .createTempDirectory ("test" );
169+ } catch (IOException e ) {
170+ throw new RuntimeException (e );
171+ }
172+ tempDir .toFile ().deleteOnExit ();
173+ return tempDir ;
174+ }
175+ }
176+ }
0 commit comments