From da8c684e0971c162c18218357184d5b01750e17b Mon Sep 17 00:00:00 2001 From: Dinu John <86094133+dinujoh@users.noreply.github.com> Date: Mon, 26 Feb 2024 13:37:02 -0600 Subject: [PATCH] Update unit test try block coverage for staic mocked object Signed-off-by: Dinu John <86094133+dinujoh@users.noreply.github.com> --- .../ConnectionConfiguration.java | 9 ++++ .../ConnectionConfigurationTest.java | 17 +++++++ .../client/OpenSearchClientFactoryTest.java | 48 +++++++++---------- 3 files changed, 50 insertions(+), 24 deletions(-) diff --git a/data-prepper-plugins/opensearch/src/main/java/org/opensearch/dataprepper/plugins/source/opensearch/configuration/ConnectionConfiguration.java b/data-prepper-plugins/opensearch/src/main/java/org/opensearch/dataprepper/plugins/source/opensearch/configuration/ConnectionConfiguration.java index 8883b78158..8e4dc3f4d6 100644 --- a/data-prepper-plugins/opensearch/src/main/java/org/opensearch/dataprepper/plugins/source/opensearch/configuration/ConnectionConfiguration.java +++ b/data-prepper-plugins/opensearch/src/main/java/org/opensearch/dataprepper/plugins/source/opensearch/configuration/ConnectionConfiguration.java @@ -5,6 +5,7 @@ package org.opensearch.dataprepper.plugins.source.opensearch.configuration; import com.fasterxml.jackson.annotation.JsonProperty; +import jakarta.validation.constraints.AssertTrue; import java.nio.file.Path; import java.time.Duration; @@ -45,4 +46,12 @@ public Duration getConnectTimeout() { public boolean isInsecure() { return insecure; } + + @AssertTrue(message = "Certificate file path and certificate content both are configured. " + + "Please use only one configuration.") + boolean certificateFileAndContentAreMutuallyExclusive() { + if(certPath == null && certificateContent == null) + return true; + return certPath != null ^ certificateContent != null; + } } diff --git a/data-prepper-plugins/opensearch/src/test/java/org/opensearch/dataprepper/plugins/source/opensearch/configuration/ConnectionConfigurationTest.java b/data-prepper-plugins/opensearch/src/test/java/org/opensearch/dataprepper/plugins/source/opensearch/configuration/ConnectionConfigurationTest.java index 75e34a08dd..97d5152080 100644 --- a/data-prepper-plugins/opensearch/src/test/java/org/opensearch/dataprepper/plugins/source/opensearch/configuration/ConnectionConfigurationTest.java +++ b/data-prepper-plugins/opensearch/src/test/java/org/opensearch/dataprepper/plugins/source/opensearch/configuration/ConnectionConfigurationTest.java @@ -14,6 +14,7 @@ import static org.hamcrest.CoreMatchers.equalTo; import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.CoreMatchers.is; public class ConnectionConfigurationTest { private final ObjectMapper objectMapper = new ObjectMapper(new YAMLFactory().enable(YAMLGenerator.Feature.USE_PLATFORM_LINE_BREAKS)); @@ -39,4 +40,20 @@ void connection_configuration_values_test() throws JsonProcessingException { assertThat(connectionConfig.getConnectTimeout(),equalTo(null)); assertThat(connectionConfig.isInsecure(),equalTo(true)); } + + @Test + void connection_configuration_certificate_values_test() throws JsonProcessingException { + + final String connectionYaml = + " cert: \"cert\"\n" + + " certificate_content: \"certificate content\"\n" + + " insecure: true\n"; + final ConnectionConfiguration connectionConfig = objectMapper.readValue(connectionYaml, ConnectionConfiguration.class); + assertThat(connectionConfig.getCertPath(),equalTo(Path.of("cert"))); + assertThat(connectionConfig.getCertificateContent(),equalTo("certificate content")); + assertThat(connectionConfig.certificateFileAndContentAreMutuallyExclusive(), is(false)); + assertThat(connectionConfig.getSocketTimeout(),equalTo(null)); + assertThat(connectionConfig.getConnectTimeout(),equalTo(null)); + assertThat(connectionConfig.isInsecure(),equalTo(true)); + } } diff --git a/data-prepper-plugins/opensearch/src/test/java/org/opensearch/dataprepper/plugins/source/opensearch/worker/client/OpenSearchClientFactoryTest.java b/data-prepper-plugins/opensearch/src/test/java/org/opensearch/dataprepper/plugins/source/opensearch/worker/client/OpenSearchClientFactoryTest.java index dc5e8ad17e..a357451147 100644 --- a/data-prepper-plugins/opensearch/src/test/java/org/opensearch/dataprepper/plugins/source/opensearch/worker/client/OpenSearchClientFactoryTest.java +++ b/data-prepper-plugins/opensearch/src/test/java/org/opensearch/dataprepper/plugins/source/opensearch/worker/client/OpenSearchClientFactoryTest.java @@ -220,32 +220,32 @@ void provideOpenSearchClient_with_aws_auth_and_serverless_flag_true() { @Test void provideOpenSearchClient_with_self_signed_certificate() { + final Path path = mock(Path.class); + final SSLContext sslContext = mock(SSLContext.class); + final String username = UUID.randomUUID().toString(); + final String password = UUID.randomUUID().toString(); + when(openSearchSourceConfiguration.getUsername()).thenReturn(username); + when(openSearchSourceConfiguration.getPassword()).thenReturn(password); + when(connectionConfiguration.getCertPath()).thenReturn(path); try (MockedStatic trustStoreProviderMockedStatic = mockStatic(TrustStoreProvider.class)) { - final Path path = mock(Path.class); - final SSLContext sslContext = mock(SSLContext.class); - final String username = UUID.randomUUID().toString(); - final String password = UUID.randomUUID().toString(); - when(openSearchSourceConfiguration.getUsername()).thenReturn(username); - when(openSearchSourceConfiguration.getPassword()).thenReturn(password); - when(connectionConfiguration.getCertPath()).thenReturn(path); trustStoreProviderMockedStatic.when(() -> TrustStoreProvider.createSSLContext(path)) .thenReturn(sslContext); final OpenSearchClient openSearchClient = createObjectUnderTest().provideOpenSearchClient(openSearchSourceConfiguration); - assertThat(openSearchClient, notNullValue()); trustStoreProviderMockedStatic.verify(() -> TrustStoreProvider.createSSLContext(path)); + assertThat(openSearchClient, notNullValue()); } } @Test void provideElasticSearchClient_with_self_signed_certificate() { + final Path path = mock(Path.class); + final SSLContext sslContext = mock(SSLContext.class); + final String username = UUID.randomUUID().toString(); + final String password = UUID.randomUUID().toString(); + when(openSearchSourceConfiguration.getUsername()).thenReturn(username); + when(openSearchSourceConfiguration.getPassword()).thenReturn(password); + when(connectionConfiguration.getCertPath()).thenReturn(path); try (MockedStatic trustStoreProviderMockedStatic = mockStatic(TrustStoreProvider.class)) { - final Path path = mock(Path.class); - final SSLContext sslContext = mock(SSLContext.class); - final String username = UUID.randomUUID().toString(); - final String password = UUID.randomUUID().toString(); - when(openSearchSourceConfiguration.getUsername()).thenReturn(username); - when(openSearchSourceConfiguration.getPassword()).thenReturn(password); - when(connectionConfiguration.getCertPath()).thenReturn(path); trustStoreProviderMockedStatic.when(() -> TrustStoreProvider.createSSLContext(path)) .thenReturn(sslContext); final ElasticsearchClient elasticsearchClient = createObjectUnderTest().provideElasticSearchClient(openSearchSourceConfiguration); @@ -257,16 +257,16 @@ void provideElasticSearchClient_with_self_signed_certificate() { @Test void createSdkAsyncHttpClient_with_self_signed_certificate() { + final Path path = mock(Path.class); + final Duration duration = mock(Duration.class); + final String username = UUID.randomUUID().toString(); + final String password = UUID.randomUUID().toString(); + lenient().when(openSearchSourceConfiguration.getUsername()).thenReturn(username); + lenient().when(openSearchSourceConfiguration.getPassword()).thenReturn(password); + lenient().when(connectionConfiguration.getConnectTimeout()).thenReturn(duration); + lenient().when(openSearchSourceConfiguration.getConnectionConfiguration()).thenReturn(connectionConfiguration); + lenient().when(connectionConfiguration.getCertPath()).thenReturn(path); try (MockedStatic trustStoreProviderMockedStatic = mockStatic(TrustStoreProvider.class)) { - final Path path = mock(Path.class); - final Duration duration = mock(Duration.class); - final String username = UUID.randomUUID().toString(); - final String password = UUID.randomUUID().toString(); - lenient().when(openSearchSourceConfiguration.getUsername()).thenReturn(username); - lenient().when(openSearchSourceConfiguration.getPassword()).thenReturn(password); - lenient().when(connectionConfiguration.getConnectTimeout()).thenReturn(duration); - lenient().when(openSearchSourceConfiguration.getConnectionConfiguration()).thenReturn(connectionConfiguration); - lenient().when(connectionConfiguration.getCertPath()).thenReturn(path); final SdkAsyncHttpClient sdkAsyncHttpClient = createObjectUnderTest().createSdkAsyncHttpClient(openSearchSourceConfiguration); assertThat(sdkAsyncHttpClient, notNullValue()); trustStoreProviderMockedStatic.verify(() -> TrustStoreProvider.createTrustManager(path));