Skip to content

Commit

Permalink
Update unit test try block coverage for staic mocked object
Browse files Browse the repository at this point in the history
Signed-off-by: Dinu John <[email protected]>
  • Loading branch information
dinujoh committed Feb 26, 2024
1 parent 5ccfc0d commit da8c684
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<TrustStoreProvider> 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<TrustStoreProvider> 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);
Expand All @@ -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<TrustStoreProvider> 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));
Expand Down

0 comments on commit da8c684

Please sign in to comment.