From 23034c2cf970910224cfa3fb297af902aabcb14d Mon Sep 17 00:00:00 2001 From: Ivan Yurchenko Date: Sat, 30 Sep 2023 17:28:45 +0300 Subject: [PATCH] Allow setting external URL explicitly --- build.gradle | 2 ++ settings.gradle | 2 ++ .../fakegcsserver/FakeGcsServerContainer.java | 24 +++++++++++++++++-- .../FakeGcsServerContainerTest.java | 20 ++++++++++++++++ 4 files changed, 46 insertions(+), 2 deletions(-) diff --git a/build.gradle b/build.gradle index 7544630..aec30b9 100644 --- a/build.gradle +++ b/build.gradle @@ -58,6 +58,8 @@ dependencies { testImplementation libs.testcontainers.junit.jupiter + testImplementation libs.assertj + testImplementation libs.gcs testRuntimeOnly libs.slf4j.log4j12 } diff --git a/settings.gradle b/settings.gradle index 374f4a3..ca5a3ff 100644 --- a/settings.gradle +++ b/settings.gradle @@ -28,6 +28,8 @@ dependencyResolutionManagement { library('junit-jupiter-engine', 'org.junit.jupiter', 'junit-jupiter-engine').versionRef('junit') library('junit-jupiter-params', 'org.junit.jupiter', 'junit-jupiter-params').versionRef('junit') + library('assertj', 'org.assertj:assertj-core:3.24.2') + library('slf4j-log4j12', 'org.slf4j:slf4j-log4j12:1.7.36') library('gcs', 'com.google.cloud:google-cloud-storage:2.26.1') diff --git a/src/main/java/io/aiven/testcontainers/fakegcsserver/FakeGcsServerContainer.java b/src/main/java/io/aiven/testcontainers/fakegcsserver/FakeGcsServerContainer.java index 9e8e28a..78017e5 100644 --- a/src/main/java/io/aiven/testcontainers/fakegcsserver/FakeGcsServerContainer.java +++ b/src/main/java/io/aiven/testcontainers/fakegcsserver/FakeGcsServerContainer.java @@ -21,18 +21,21 @@ import java.net.http.HttpClient; import java.net.http.HttpRequest; import java.net.http.HttpResponse; +import java.util.Objects; import org.testcontainers.containers.GenericContainer; import org.testcontainers.utility.DockerImageName; public class FakeGcsServerContainer extends GenericContainer { - private static final int PORT = 4443; + public static final int PORT = 4443; public static final String DEFAULT_VERSION = "1.47.4"; public static final DockerImageName DEFAULT_IMAGE_NAME = DockerImageName.parse("fsouza/fake-gcs-server:" + DEFAULT_VERSION); + private String externalUrl = null; + public FakeGcsServerContainer() { this(DEFAULT_IMAGE_NAME); } @@ -47,20 +50,37 @@ public FakeGcsServerContainer(final DockerImageName dockerImageName) { addExposedPorts(PORT); } + public FakeGcsServerContainer withExternalURL(final String externalUrl) { + this.externalUrl = externalUrl; + return this; + } + @Override public void start() { super.start(); updateExternalUrl(); } + /** + * The URL based on the host name and mapped port. + */ public String url() { return "http://" + getHost() + ":" + getMappedPort(PORT); } + /** + * The URL based on the defined external URL. + * + *

Defaults to {@link FakeGcsServerContainer#url()}. + */ + public String externalUrl() { + return Objects.requireNonNullElseGet(externalUrl, this::url); + } + private void updateExternalUrl() { final String modifyExternalUrlRequestUri = url() + "/_internal/config"; final String updateExternalUrlJson = "{" - + "\"externalUrl\": \"" + url() + "\"" + + "\"externalUrl\": \"" + externalUrl() + "\"" + "}"; final HttpRequest req = HttpRequest.newBuilder() diff --git a/src/test/java/io/aiven/testcontainers/fakegcsserver/FakeGcsServerContainerTest.java b/src/test/java/io/aiven/testcontainers/fakegcsserver/FakeGcsServerContainerTest.java index 38eaf6c..cc7d8ac 100644 --- a/src/test/java/io/aiven/testcontainers/fakegcsserver/FakeGcsServerContainerTest.java +++ b/src/test/java/io/aiven/testcontainers/fakegcsserver/FakeGcsServerContainerTest.java @@ -24,11 +24,14 @@ import com.google.cloud.storage.Storage; import com.google.cloud.storage.StorageOptions; import org.junit.jupiter.api.Named; +import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.MethodSource; import org.testcontainers.utility.DockerImageName; +import static org.assertj.core.api.Assertions.assertThat; + public class FakeGcsServerContainerTest { static final BucketInfo TEST_BUCKET = BucketInfo.of("test-bucket"); @@ -60,4 +63,21 @@ private static Stream provideTestParams() { new FakeGcsServerContainer(DockerImageName.parse("fsouza/fake-gcs-server:1.47.3")))) ); } + + @Test + void testExternalUrlNotSet() { + try (final var fakeGcsServerContainer = new FakeGcsServerContainer()) { + fakeGcsServerContainer.start(); + assertThat(fakeGcsServerContainer.externalUrl()).isEqualTo(fakeGcsServerContainer.url()); + } + } + + @Test + void testExternalUrlSet() { + try (final var fakeGcsServerContainer = new FakeGcsServerContainer().withExternalURL("http://aaa:1234")) { + fakeGcsServerContainer.start(); + assertThat(fakeGcsServerContainer.externalUrl()).isEqualTo("http://aaa:1234"); + assertThat(fakeGcsServerContainer.externalUrl()).isNotEqualTo(fakeGcsServerContainer.url()); + } + } }