Skip to content

Commit 69c2067

Browse files
committed
Refactor Integration Tests.
Signed-off-by: Rahul Krishna <[email protected]>
1 parent c3bbc67 commit 69c2067

File tree

2 files changed

+64
-118
lines changed

2 files changed

+64
-118
lines changed

src/test/java/com/ibm/cldk/CodeAnalyzerIntegrationTest.java

Lines changed: 64 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import org.testcontainers.containers.GenericContainer;
88
import org.testcontainers.junit.jupiter.Container;
99
import org.testcontainers.junit.jupiter.Testcontainers;
10+
import org.testcontainers.utility.MountableFile;
1011

1112
import java.io.File;
1213
import java.io.FileInputStream;
@@ -16,6 +17,8 @@
1617
import java.text.MessageFormat;
1718
import java.util.Properties;
1819

20+
import static org.junit.jupiter.api.Assertions.assertThrows;
21+
1922
@Testcontainers
2023
@SuppressWarnings("resource")
2124
public class CodeAnalyzerIntegrationTest {
@@ -46,7 +49,19 @@ public class CodeAnalyzerIntegrationTest {
4649
.withFileSystemBind(
4750
String.valueOf(Paths.get(System.getProperty("user.dir")).resolve("build/libs")),
4851
"/opt/jars",
49-
BindMode.READ_WRITE);
52+
BindMode.READ_WRITE)
53+
.withCopyFileToContainer(MountableFile.forHostPath(Paths.get(System.getProperty("user.dir")).resolve("build/libs")), "/opt/jars")
54+
.withCopyFileToContainer(MountableFile.forHostPath(Paths.get(System.getProperty("user.dir")).resolve("src/test/resources/test-applications/mvnw-corrupt-test")), "/test-applications/mvnw-corrupt-test")
55+
.withCopyFileToContainer(MountableFile.forHostPath(Paths.get(System.getProperty("user.dir")).resolve("src/test/resources/test-applications/mvnw-working-test")), "/test-applications/mvnw-working-test");
56+
57+
@Container
58+
static final GenericContainer<?> mavenContainer = new GenericContainer<>("maven:3.8.3-openjdk-17")
59+
.withCreateContainerCmdModifier(cmd -> cmd.withEntrypoint("sh"))
60+
.withCommand("-c", "while true; do sleep 1; done")
61+
.withCopyFileToContainer(MountableFile.forHostPath(Paths.get(System.getProperty("user.dir")).resolve("build/libs")), "/opt/jars")
62+
.withCopyFileToContainer(MountableFile.forHostPath(Paths.get(System.getProperty("user.dir")).resolve("src/test/resources/test-applications/mvnw-corrupt-test")), "/test-applications/mvnw-corrupt-test")
63+
.withCopyFileToContainer(MountableFile.forHostPath(Paths.get(System.getProperty("user.dir")).resolve("src/test/resources/test-applications/mvnw-working-test")), "/test-applications/mvnw-working-test");
64+
5065

5166
@BeforeAll
5267
static void setUp() {
@@ -73,7 +88,8 @@ void shouldHaveCodeAnalyzerJar() throws Exception {
7388
Assertions.assertTrue(dirContents.getStdout().contains("codeanalyzer"), "Codeanalyzer.jar not found in the container.");
7489
}
7590

76-
@Test void shouldBeAbleToRunCodeAnalyzer() throws Exception {
91+
@Test
92+
void shouldBeAbleToRunCodeAnalyzer() throws Exception {
7793
var runCodeAnalyzerJar = container.execInContainer(
7894
"java",
7995
"-jar",
@@ -84,5 +100,50 @@ void shouldHaveCodeAnalyzerJar() throws Exception {
84100
Assertions.assertEquals(0, runCodeAnalyzerJar.getExitCode(),
85101
"Command should execute successfully");
86102
Assertions.assertTrue(runCodeAnalyzerJar.getStdout().length() > 0,
87-
"Should have some output"); }
103+
"Should have some output");
104+
}
105+
106+
@Test
107+
void corruptMavenShouldNotBuildWithWrapper() throws IOException, InterruptedException {
108+
// Make executable
109+
mavenContainer.execInContainer("chmod", "+x", "/test-applications/mvnw-corrupt-test/mvnw");
110+
// Let's start by building the project by itself
111+
var mavenProjectBuildWithWrapper = mavenContainer.withWorkingDirectory("/test-applications/mvnw-corrupt-test").execInContainer("/test-applications/mvnw-corrupt-test/mvnw", "clean", "compile");
112+
Assertions.assertNotEquals(0, mavenProjectBuildWithWrapper.getExitCode());
113+
}
114+
115+
// @Test
116+
// void corruptMavenShouldProduceAnalysisArtifactsWhenMVNCommandIsInPath() throws IOException, InterruptedException {
117+
// // Let's start by building the project by itself
118+
// var corruptMavenProjectBuild = mavenContainer.withWorkingDirectory("/test-applications/mvnw-corrupt-test").execInContainer("mvn", "-f", "/test-applications/mvnw-corrupt-test/pom.xml", "clean", "compile");
119+
// Assertions.assertEquals(0, corruptMavenProjectBuild.getExitCode(), "Failed to build the project with system's default Maven.");
120+
// // NOw run codeanalyzer and assert if analysis.json is generated.
121+
// mavenContainer.execInContainer("java", "-jar", String.format("/opt/jars/codeanalyzer-%s.jar", codeanalyzerVersion), "--input=/test-applications/mvnw-corrupt-test", "--output=/tmp/", "--analysis-level=2", "--no-build");
122+
// var codeAnalyzerOutputDirContents = mavenContainer.execInContainer("ls", "/tmp/analysis.json");
123+
// String codeAnalyzerOutputDirContentsStdOut = codeAnalyzerOutputDirContents.getStdout();
124+
// Assertions.assertTrue(codeAnalyzerOutputDirContentsStdOut.length() > 0, "Could not find 'analysis.json'.");
125+
// Assertions.assertTrue(codeAnalyzerOutputDirContentsStdOut.contains("Building the project using") && codeAnalyzerOutputDirContentsStdOut.contains("/mvn."));
126+
// Assertions.assertFalse(codeAnalyzerOutputDirContentsStdOut.contains("Building the project using") && codeAnalyzerOutputDirContentsStdOut.contains("/test-applications/mvnw-corrupt-test/mvnw."));
127+
// }
128+
//
129+
// @Test
130+
// void corruptMavenShouldNotTerminateWithErrorWhenMavenIsNotPresentUnlessAnalysisLevel2() throws IOException, InterruptedException {
131+
// // When analysis level 2, we should get a Runtime Exception
132+
// assertThrows(RuntimeException.class, () ->
133+
// container.execInContainer(
134+
// "java",
135+
// "-jar",
136+
// String.format("/opt/jars/codeanalyzer-%s.jar", codeanalyzerVersion),
137+
// "--input=/test-applications/mvnw-corrupt-test",
138+
// "--output=/tmp/",
139+
// "--analysis-level=2"
140+
// )
141+
// );
142+
// // When analysis level is 1, we should still be able to generate an analysis.json file.
143+
// container.execInContainer("java", "-jar", String.format("/opt/jars/codeanalyzer-%s.jar", codeanalyzerVersion), "--input=/test-applications/mvnw-corrupt-test", "--output=/tmp/", "--analysis-level=1");
144+
// var codeAnalyzerOutputDirContents = container.execInContainer("ls", "/tmp/analysis.json");
145+
// String codeAnalyzerOutputDirContentsStdOut = codeAnalyzerOutputDirContents.getStdout();
146+
// Assertions.assertTrue(codeAnalyzerOutputDirContentsStdOut.length() > 0, "Could not find 'analysis.json'.");
147+
// Assertions.assertTrue(codeAnalyzerOutputDirContentsStdOut.contains("Could not find Maven or a valid Maven Wrapper"));
148+
// }
88149
}

src/test/java/com/ibm/cldk/MavenApplicationIntegrationTest.java

Lines changed: 0 additions & 115 deletions
This file was deleted.

0 commit comments

Comments
 (0)