Skip to content

Commit ee2d27b

Browse files
committed
✨ Implemented TestContainers startup
This is the initial first step in getting a more self-contained integration test. The code now starts up a docker container containing AEM (assuming that one is available). Next step is to deploy sample files to the container before runnning the tests. The code assumes that an AEM container is available (in this case one that is hosted by the 4PointSolutions-PS GitHub org. The container code can be turned off by a constant in AbstractAemContainerTest. When turned off, the behaviour reverts to previous behaviour (i.e. it assumes a copy of AEM is running locally),
1 parent 200a215 commit ee2d27b

5 files changed

Lines changed: 114 additions & 8 deletions

File tree

rest-services/it.tests/pom.xml

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@
3333

3434
<properties>
3535
<!-- AEM may be running in Java 8, so compile for that until it's no longer supported. -->
36-
<maven.compiler.source>11</maven.compiler.source>
37-
<maven.compiler.target>11</maven.compiler.target>
38-
<maven.compiler.release>11</maven.compiler.release>
36+
<maven.compiler.source>17</maven.compiler.source>
37+
<maven.compiler.target>17</maven.compiler.target>
38+
<maven.compiler.release>17</maven.compiler.release>
3939
</properties>
4040

4141
<dependencies>
@@ -85,6 +85,16 @@
8585
<artifactId>jcabi-xml</artifactId>
8686
<scope>test</scope>
8787
</dependency>
88+
<dependency>
89+
<groupId>org.testcontainers</groupId>
90+
<artifactId>junit-jupiter</artifactId>
91+
<scope>test</scope>
92+
</dependency>
93+
<dependency>
94+
<groupId>org.awaitility</groupId>
95+
<artifactId>awaitility</artifactId>
96+
<scope>test</scope>
97+
</dependency>
8898
</dependencies>
8999

90100
</project>
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package com._4point.aem.docservices.rest_services.it_tests;
2+
3+
import java.io.IOException;
4+
import java.net.URI;
5+
import java.net.http.HttpClient;
6+
import java.net.http.HttpRequest;
7+
import java.net.http.HttpResponse;
8+
import java.net.http.HttpResponse.BodyHandlers;
9+
import java.util.Base64;
10+
import java.util.concurrent.TimeUnit;
11+
12+
import org.awaitility.Awaitility;
13+
import org.junit.jupiter.api.AutoClose;
14+
import org.junit.jupiter.api.BeforeAll;
15+
import org.testcontainers.containers.GenericContainer;
16+
import org.testcontainers.utility.DockerImageName;
17+
18+
public abstract class AbstractAemContainerTest {
19+
private static final boolean useTestContainers = true;
20+
21+
@SuppressWarnings("resource")
22+
@AutoClose
23+
static GenericContainer<?> aemContainer = useTestContainers ? new GenericContainer<>(DockerImageName.parse("ghcr.io/4pointsolutions-ps/aem:aem65sp21"))
24+
.withExposedPorts(4502)
25+
: null
26+
;
27+
28+
static {
29+
aemContainer.start();
30+
}
31+
32+
@BeforeAll
33+
static void setup() {
34+
// Make sure AEM is up before running the tests.
35+
Integer mappedPort = aemPort();
36+
System.out.println(String.format("Waiting for AEM to be available on port %d.", mappedPort));
37+
HttpClient client = HttpClient.newHttpClient();
38+
HttpRequest request = HttpRequest.newBuilder()
39+
.uri(URI.create(String.format("http://localhost:%d/", mappedPort)))
40+
.header("Authorization", encodeBasic(TestUtils.TEST_USER, TestUtils.TEST_USER_PASSWORD))
41+
.GET()
42+
.build();
43+
44+
Awaitility.await()
45+
.atMost(5, TimeUnit.MINUTES)
46+
.pollDelay(50, TimeUnit.SECONDS)
47+
.pollInterval(10, TimeUnit.SECONDS)
48+
.until(()->aemIsUp(client, request));
49+
System.out.println("AEM is available, starting tests...");
50+
}
51+
52+
protected static String aemHost() {
53+
return useTestContainers ? "localhost" : TestUtils.TEST_MACHINE_NAME;
54+
}
55+
56+
protected static Integer aemPort() {
57+
return useTestContainers ? aemContainer.getMappedPort(4502) : Integer.parseInt(TestUtils.TEST_MACHINE_PORT_STR) ;
58+
}
59+
60+
private static boolean aemIsUp(HttpClient restClient, HttpRequest request) {
61+
try {
62+
HttpResponse<Void> result = restClient.send(request, BodyHandlers.discarding());
63+
int statusCode = result.statusCode();
64+
System.out.println(String.format("AEM returned status code %d.", statusCode));
65+
return statusCode >= 200 || statusCode <= 399; // Wait for successful status code.
66+
} catch (InterruptedException e) {
67+
return false;
68+
} catch (IOException e) {
69+
e.printStackTrace();
70+
return false;
71+
}
72+
}
73+
74+
private static String encodeBasic(String username, String password) {
75+
return "Basic "+ Base64
76+
.getEncoder()
77+
.encodeToString((username+":"+password).getBytes());
78+
}
79+
80+
}

rest-services/it.tests/src/test/java/com/_4point/aem/docservices/rest_services/it_tests/client/output/GeneratePdfOutputTest.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
import com._4point.aem.docservices.rest_services.client.jersey.JerseyRestClient;
1515
import com._4point.aem.docservices.rest_services.client.output.RestServicesOutputServiceAdapter;
16+
import com._4point.aem.docservices.rest_services.it_tests.AbstractAemContainerTest;
1617
import com._4point.aem.docservices.rest_services.it_tests.Pdf;
1718
import com._4point.aem.docservices.rest_services.it_tests.TestUtils;
1819
import com._4point.aem.fluentforms.api.Document;
@@ -23,7 +24,7 @@
2324
import com._4point.aem.fluentforms.impl.output.OutputServiceImpl;
2425
import com.adobe.fd.output.api.AcrobatVersion;
2526

26-
class GeneratePdfOutputTest {
27+
class GeneratePdfOutputTest extends AbstractAemContainerTest {
2728

2829
private static final String CRX_CONTENT_ROOT = "crx:/content/dam/formsanddocuments/sample-forms";
2930

@@ -32,8 +33,8 @@ class GeneratePdfOutputTest {
3233
@BeforeEach
3334
void setUp() throws Exception {
3435
RestServicesOutputServiceAdapter adapter = RestServicesOutputServiceAdapter.builder(JerseyRestClient.factory())
35-
.machineName("172.18.110.22")
36-
.port(4502)
36+
.machineName(aemHost())
37+
.port(aemPort())
3738
.basicAuthentication(TEST_USER, TEST_USER_PASSWORD)
3839
.useSsl(false)
3940
.aemServerType(TEST_MACHINE_AEM_TYPE)

rest-services/it.tests/src/test/java/com/_4point/aem/docservices/rest_services/it_tests/server/output/GeneratePdfOutputTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,13 @@
2222
import org.junit.jupiter.api.BeforeEach;
2323
import org.junit.jupiter.api.Test;
2424

25+
import com._4point.aem.docservices.rest_services.it_tests.AbstractAemContainerTest;
2526
import com._4point.aem.docservices.rest_services.it_tests.TestUtils;
2627

27-
class GeneratePdfOutputTest {
28+
class GeneratePdfOutputTest extends AbstractAemContainerTest {
2829

2930
private static final String GENERATE_PDF_OUTPUT_PATH = TEST_MACHINE_AEM_TYPE.pathPrefix() + "/services/OutputService/GeneratePdfOutput";
30-
private static final String GENERATE_PDF_OUTPUT_URL = "http://" + TEST_MACHINE_NAME + ":" + TEST_MACHINE_PORT_STR + GENERATE_PDF_OUTPUT_PATH;
31+
private static final String GENERATE_PDF_OUTPUT_URL = "http://" + aemHost() + ":" + aemPort() + GENERATE_PDF_OUTPUT_PATH;
3132
private static final MediaType APPLICATION_PDF = new MediaType("application", "pdf");
3233
private static final MediaType APPLICATION_XDP = new MediaType("application", "vnd.adobe.xdp+xml");
3334
private static final String CRX_CONTENT_ROOT = "crx:/content/dam/formsanddocuments/sample-forms";

rest-services/pom.xml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@
102102
<wiremock.version>3.4.2</wiremock.version>
103103
<_4point-hamcrest-matchers.version>0.0.1-SNAPSHOT</_4point-hamcrest-matchers.version>
104104
<junit-addons.version>1.4</junit-addons.version>
105+
<testcontainers.version>1.20.6</testcontainers.version>
106+
<awaitility.version>4.3.0</awaitility.version>
105107
</properties>
106108

107109
<build>
@@ -519,6 +521,18 @@
519521
</exclusion>
520522
</exclusions>
521523
</dependency>
524+
<dependency>
525+
<groupId>org.testcontainers</groupId>
526+
<artifactId>junit-jupiter</artifactId>
527+
<version>${testcontainers.version}</version>
528+
<scope>test</scope>
529+
</dependency>
530+
<dependency>
531+
<groupId>org.awaitility</groupId>
532+
<artifactId>awaitility</artifactId>
533+
<version>${awaitility.version}</version>
534+
<scope>test</scope>
535+
</dependency>
522536
</dependencies>
523537
</dependencyManagement>
524538

0 commit comments

Comments
 (0)