Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions modules/consul/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,14 @@ description = "Testcontainers :: Consul"
dependencies {
api project(':testcontainers')

testRuntimeOnly 'org.junit.platform:junit-platform-launcher:1.11.0'

testImplementation 'org.junit.jupiter:junit-jupiter:5.13.4'
testImplementation 'com.ecwid.consul:consul-api:1.4.5'
testImplementation 'io.rest-assured:rest-assured:5.5.6'
testImplementation 'org.assertj:assertj-core:3.27.4'
}

test {
useJUnitPlatform()
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
import com.ecwid.consul.v1.Response;
import com.ecwid.consul.v1.kv.model.GetValue;
import io.restassured.RestAssured;
import org.junit.ClassRule;
import org.junit.Test;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.testcontainers.containers.GenericContainer;

import java.io.IOException;
Expand All @@ -16,25 +17,30 @@

import static org.assertj.core.api.Assertions.assertThat;

/**
* This test shows the pattern to use the ConsulContainer @ClassRule for a junit test. It also has tests that ensure
* the properties were added correctly by reading from Consul with the CLI and over HTTP.
*/
public class ConsulContainerTest {
class ConsulContainerTest {

@ClassRule
public static ConsulContainer consulContainer = new ConsulContainer("hashicorp/consul:1.15")
private static ConsulContainer consul = new ConsulContainer("hashicorp/consul:1.15")
.withConsulCommand("kv put config/testing1 value123");

@BeforeAll
static void setup() {
consul.start();
}

@AfterAll
static void teardown() {
consul.stop();
}

@Test
public void readFirstPropertyPathWithCli() throws IOException, InterruptedException {
GenericContainer.ExecResult result = consulContainer.execInContainer("consul", "kv", "get", "config/testing1");
void readFirstPropertyPathWithCli() throws IOException, InterruptedException {
GenericContainer.ExecResult result = consul.execInContainer("consul", "kv", "get", "config/testing1");
final String output = result.getStdout().replaceAll("\\r?\\n", "");
assertThat(output).contains("value123");
}

@Test
public void readFirstSecretPathOverHttpApi() {
void readFirstSecretPathOverHttpApi() {
io.restassured.response.Response response = RestAssured
.given()
.when()
Expand All @@ -46,11 +52,8 @@ public void readFirstSecretPathOverHttpApi() {
}

@Test
public void writeAndReadMultipleValuesUsingClient() {
final ConsulClient consulClient = new ConsulClient(
consulContainer.getHost(),
consulContainer.getFirstMappedPort()
);
void writeAndReadMultipleValuesUsingClient() {
final ConsulClient consulClient = new ConsulClient(consul.getHost(), consul.getFirstMappedPort());

final Map<String, String> properties = new HashMap<>();
properties.put("value", "world");
Expand All @@ -70,6 +73,6 @@ public void writeAndReadMultipleValuesUsingClient() {
}

private String getHostAndPort() {
return consulContainer.getHost() + ":" + consulContainer.getMappedPort(8500);
return consul.getHost() + ":" + consul.getMappedPort(8500);
}
}
Loading