From a576a83f995cd01e34846cb3bd0283c9f0a461b4 Mon Sep 17 00:00:00 2001 From: "tomasz.kowalczewski" Date: Thu, 5 May 2022 21:42:50 +0200 Subject: [PATCH] Re #87: Testing ClientConfigurationBuilder and fixing bugs. --- .../http/ClientConfigurationBuilder.java | 28 +++++- .../http/ClientConfigurationBuilderTest.java | 90 +++++++++++++++++++ 2 files changed, 114 insertions(+), 4 deletions(-) create mode 100644 core/src/test/java/pl/tkowalcz/tjahzi/http/ClientConfigurationBuilderTest.java diff --git a/core/src/main/java/pl/tkowalcz/tjahzi/http/ClientConfigurationBuilder.java b/core/src/main/java/pl/tkowalcz/tjahzi/http/ClientConfigurationBuilder.java index 6137a31..4059653 100644 --- a/core/src/main/java/pl/tkowalcz/tjahzi/http/ClientConfigurationBuilder.java +++ b/core/src/main/java/pl/tkowalcz/tjahzi/http/ClientConfigurationBuilder.java @@ -6,7 +6,9 @@ import java.net.URL; public class ClientConfigurationBuilder { public static final int HTTPS_PORT = 443; + public static final int HTTP_PORT = 80; public static final String HTTPS_STRING = "https"; + public static final String HTTP_STRING = "http"; public static final String DEFAULT_LOG_ENDPOINT = "/loki/api/v1/push"; @@ -17,7 +19,7 @@ public class ClientConfigurationBuilder { public static final int DEFAULT_MAX_RETRIES = 0; - private String logEndpoint = DEFAULT_LOG_ENDPOINT; + private String logEndpoint; private String url; @@ -117,20 +119,38 @@ public class ClientConfigurationBuilder { "Current configuration sets url to '" + url + "' and host to '" + host + "'"); } + if (url == null && host == null) { + throw new IllegalArgumentException("One of 'url' or 'host' must be configured."); + } + if (host != null) { validateAndConfigureConnectionParametersNoUrl(); - } else if (url != null) { - validateAndConfigureConnectionParametersWithUrl(); } + + validateAndConfigureConnectionParametersWithUrl(); } private void validateAndConfigureConnectionParametersWithUrl() { try { URL parsedUrl = new URL(url); + + String protocolScheme = parsedUrl.getProtocol(); + if (!HTTPS_STRING.equalsIgnoreCase(protocolScheme) && !HTTP_STRING.equalsIgnoreCase(protocolScheme)) { + throw new IllegalArgumentException("Unknown protocol scheme, must be one of 'https' or 'http' (case insensitive). Provided: '" + protocolScheme + "'"); + } + host = parsedUrl.getHost(); port = parsedUrl.getPort(); - if (parsedUrl.getPath() != null) { + if (port == -1) { + if (parsedUrl.getProtocol().equalsIgnoreCase(HTTPS_STRING)) { + port = HTTPS_PORT; + } else { + port = HTTP_PORT; + } + } + + if (parsedUrl.getPath() != null && !parsedUrl.getPath().isEmpty()) { if (logEndpoint != null) { throw new IllegalArgumentException("If Loki connection URL contains path part then you cannot at the " + "same time define log endpoint. Url: '" + url + "', log endpoint: '" + logEndpoint + "'"); diff --git a/core/src/test/java/pl/tkowalcz/tjahzi/http/ClientConfigurationBuilderTest.java b/core/src/test/java/pl/tkowalcz/tjahzi/http/ClientConfigurationBuilderTest.java new file mode 100644 index 0000000..58b6049 --- /dev/null +++ b/core/src/test/java/pl/tkowalcz/tjahzi/http/ClientConfigurationBuilderTest.java @@ -0,0 +1,90 @@ +package pl.tkowalcz.tjahzi.http; + +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 java.util.stream.Stream; + +import static org.assertj.core.api.Assertions.assertThat; + +class ClientConfigurationBuilderTest { + + static Stream configOptions() { + return Stream.of( + Arguments.of( + "Should take arguments from url", + "https://example.net:8443/monitoring/loki/api/v1/push", + "example.net", + 8443, + "/monitoring/loki/api/v1/push", + true + ), + Arguments.of( + "Should not enable ssl if prefix is not https", + "http://example.net:8443/monitoring/loki/api/v1/push", + "example.net", + 8443, + "/monitoring/loki/api/v1/push", + false + ), + Arguments.of( + "Should use default logEndpoint is none is provided", + "http://example.net:8443", + "example.net", + 8443, + "/loki/api/v1/push", + false + ), + Arguments.of( + "Should use default logEndpoint is none is provided", + "http://example.net", + "example.net", + 8443, + "/loki/api/v1/push", + false + ), + Arguments.of( + "Should reject unknown protocol", + "loki://example.net" + ) + ); + } + + @ParameterizedTest(name = "{0}") + @MethodSource("configOptions") + void shouldGetConnectionParamsFromUrl( + String testCase, + String url, + String expectedHost, + int expectedPort, + String expectedLogEndpoint, + boolean expectedUseSSL + ) { + // Given + ClientConfigurationBuilder configurationBuilder = new ClientConfigurationBuilder(); + + // When + ClientConfiguration configuration = configurationBuilder + .withUrl(url) + .build(); + + // Then + assertThat(configuration.getHost()).isEqualTo(expectedHost); + assertThat(configuration.getPort()).isEqualTo(expectedPort); + assertThat(configuration.getLogEndpoint()).isEqualTo(expectedLogEndpoint); + assertThat(configuration.isUseSSL()).isEqualTo(expectedUseSSL); + } + + @Test + void shouldRejectUnknownProtocolScheme() { + // Given + ClientConfigurationBuilder configurationBuilder = new ClientConfigurationBuilder(); + + // When + ClientConfiguration configuration = configurationBuilder + .withUrl("loki://example.net:8443/monitoring/loki/api/v1/push") + .build(); + } +}