Skip to content

Commit 19639aa

Browse files
committed
Add test coverage for TestUtils.validatePomXmlFiles(List)
1 parent 821f645 commit 19639aa

1 file changed

Lines changed: 171 additions & 0 deletions

File tree

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

0 commit comments

Comments
 (0)