Skip to content

Commit 71dc6de

Browse files
eddumelendezkkocel
andauthored
Add NATS example (#6230)
Demostrate how to use NATS along with GenericContainer. Co-authored-by: Krzysztof Kocel <[email protected]>
1 parent a93183f commit 71dc6de

File tree

5 files changed

+89
-0
lines changed

5 files changed

+89
-0
lines changed

docs/examples.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@ Examples of different use cases provided by Testcontainers can be found below:
1616
- [TestNG](https://github.com/testcontainers/testcontainers-java/tree/main/examples/redis-backed-cache-testng)
1717
- [ImmuDb](https://github.com/testcontainers/testcontainers-java/tree/main/examples/immudb)
1818
- [Zookeeper](https://github.com/testcontainers/testcontainers-java/tree/main/examples/zookeeper)
19+
- [NATS](https://github.com/testcontainers/testcontainers-java/tree/main/examples/nats)

examples/nats/build.gradle

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
plugins {
2+
id 'java'
3+
}
4+
5+
repositories {
6+
mavenCentral()
7+
}
8+
9+
dependencies {
10+
testImplementation 'org.assertj:assertj-core:3.23.1'
11+
testImplementation 'org.testcontainers:testcontainers'
12+
testImplementation 'io.nats:jnats:2.16.4'
13+
testImplementation 'ch.qos.logback:logback-classic:1.3.4'
14+
testImplementation 'org.apache.httpcomponents:httpclient:4.5.13'
15+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.example;
2+
3+
import io.nats.client.Connection;
4+
import io.nats.client.Nats;
5+
import io.nats.client.Options;
6+
import org.apache.http.HttpResponse;
7+
import org.apache.http.HttpStatus;
8+
import org.apache.http.client.methods.HttpGet;
9+
import org.apache.http.client.methods.HttpUriRequest;
10+
import org.apache.http.impl.client.HttpClientBuilder;
11+
import org.junit.Test;
12+
import org.testcontainers.containers.GenericContainer;
13+
14+
import java.io.IOException;
15+
16+
import static org.assertj.core.api.Assertions.assertThat;
17+
18+
public class NatsContainerTest {
19+
20+
public static final Integer NATS_PORT = 4222;
21+
22+
public static final Integer NATS_MGMT_PORT = 8222;
23+
24+
@Test
25+
public void test() throws IOException, InterruptedException {
26+
try (
27+
GenericContainer<?> nats = new GenericContainer<>("nats:2.9.8-alpine3.16")
28+
.withExposedPorts(NATS_PORT, NATS_MGMT_PORT)
29+
) {
30+
nats.start();
31+
32+
Connection connection = Nats.connect(
33+
new Options.Builder().server("nats://" + nats.getHost() + ":" + nats.getMappedPort(NATS_PORT)).build()
34+
);
35+
36+
assertThat(connection.getStatus()).isEqualTo(Connection.Status.CONNECTED);
37+
}
38+
}
39+
40+
@Test
41+
public void testServerStatus() throws IOException {
42+
try (
43+
GenericContainer<?> nats = new GenericContainer<>("nats:2.9.8-alpine3.16")
44+
.withExposedPorts(NATS_PORT, NATS_MGMT_PORT)
45+
) {
46+
nats.start();
47+
48+
HttpUriRequest request = new HttpGet(
49+
String.format("http://%s:%d/varz", nats.getHost(), nats.getMappedPort(NATS_MGMT_PORT))
50+
);
51+
HttpResponse httpResponse = HttpClientBuilder.create().build().execute(request);
52+
53+
assertThat(httpResponse.getStatusLine().getStatusCode()).isEqualTo(HttpStatus.SC_OK);
54+
}
55+
}
56+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<configuration>
2+
3+
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
4+
<!-- encoders are assigned the type
5+
ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
6+
<encoder>
7+
<pattern>%d{HH:mm:ss.SSS} %-5level %logger - %msg%n</pattern>
8+
</encoder>
9+
</appender>
10+
11+
<root level="INFO">
12+
<appender-ref ref="STDOUT"/>
13+
</root>
14+
15+
<logger name="org.testcontainers" level="INFO"/>
16+
</configuration>

examples/settings.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ include 'spring-boot-kotlin-redis'
3333
include 'immudb'
3434
include 'zookeeper'
3535
include 'hazelcast'
36+
include 'nats'
3637

3738
ext.isCI = System.getenv("CI") != null
3839

0 commit comments

Comments
 (0)