From c2352bac8c8f9603faaf8dc0131b419ad39315f0 Mon Sep 17 00:00:00 2001 From: venkataraopasyavula <126578319+venkataraopasyavula@users.noreply.github.com> Date: Thu, 22 Jun 2023 21:41:48 +0530 Subject: [PATCH] GitHub-issue#253 : Implemented GeoIP Plugin Configuration Code JUnit test cases (#2909) Signed-off-by: venkataraopasyavula --- .../processor/GeoIPProcessorConfigTest.java | 75 ++++++ .../AwsAuthenticationOptionsTest.java | 224 ++++++++++++++++++ .../DatabasePathURLConfigTest.java | 38 +++ .../configuration/KeyConfigTest.java | 72 ++++++ .../configuration/KeysConfigTest.java | 38 +++ .../MaxMindServiceConfigTest.java | 93 ++++++++ .../configuration/ServiceTypeOptionsTest.java | 39 +++ .../loadtype/LoadTypeOptionsTest.java | 30 +++ .../org.mockito.plugins.MockMaker | 3 + 9 files changed, 612 insertions(+) create mode 100644 data-prepper-plugins/geoip-processor/src/test/java/org/opensearch/dataprepper/plugins/processor/GeoIPProcessorConfigTest.java create mode 100644 data-prepper-plugins/geoip-processor/src/test/java/org/opensearch/dataprepper/plugins/processor/configuration/AwsAuthenticationOptionsTest.java create mode 100644 data-prepper-plugins/geoip-processor/src/test/java/org/opensearch/dataprepper/plugins/processor/configuration/DatabasePathURLConfigTest.java create mode 100644 data-prepper-plugins/geoip-processor/src/test/java/org/opensearch/dataprepper/plugins/processor/configuration/KeyConfigTest.java create mode 100644 data-prepper-plugins/geoip-processor/src/test/java/org/opensearch/dataprepper/plugins/processor/configuration/KeysConfigTest.java create mode 100644 data-prepper-plugins/geoip-processor/src/test/java/org/opensearch/dataprepper/plugins/processor/configuration/MaxMindServiceConfigTest.java create mode 100644 data-prepper-plugins/geoip-processor/src/test/java/org/opensearch/dataprepper/plugins/processor/configuration/ServiceTypeOptionsTest.java create mode 100644 data-prepper-plugins/geoip-processor/src/test/java/org/opensearch/dataprepper/plugins/processor/loadtype/LoadTypeOptionsTest.java create mode 100644 data-prepper-plugins/geoip-processor/src/test/resources/mockito-extensions/org.mockito.plugins.MockMaker diff --git a/data-prepper-plugins/geoip-processor/src/test/java/org/opensearch/dataprepper/plugins/processor/GeoIPProcessorConfigTest.java b/data-prepper-plugins/geoip-processor/src/test/java/org/opensearch/dataprepper/plugins/processor/GeoIPProcessorConfigTest.java new file mode 100644 index 0000000000..b1b6d08c1a --- /dev/null +++ b/data-prepper-plugins/geoip-processor/src/test/java/org/opensearch/dataprepper/plugins/processor/GeoIPProcessorConfigTest.java @@ -0,0 +1,75 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ + +package org.opensearch.dataprepper.plugins.processor; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.junit.jupiter.MockitoExtension; +import org.opensearch.dataprepper.plugins.processor.configuration.AwsAuthenticationOptions; +import org.opensearch.dataprepper.plugins.processor.configuration.KeysConfig; +import org.opensearch.dataprepper.plugins.processor.configuration.ServiceTypeOptions; +import org.opensearch.dataprepper.test.helper.ReflectivelySetField; + +import java.util.ArrayList; +import java.util.List; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +@ExtendWith(MockitoExtension.class) +class GeoIPProcessorConfigTest { + + private GeoIPProcessorConfig geoIPProcessorConfig; + + @BeforeEach + void setUp() { + geoIPProcessorConfig = new GeoIPProcessorConfig(); + } + + @Test + void getAwsAuthenticationOptionsTestPositive() throws NoSuchFieldException, IllegalAccessException { + AwsAuthenticationOptions awsAuthenticationOptions = new AwsAuthenticationOptions(); + ReflectivelySetField.setField(GeoIPProcessorConfig.class, + geoIPProcessorConfig, "awsAuthenticationOptions", awsAuthenticationOptions); + assertThat(geoIPProcessorConfig.getAwsAuthenticationOptions(), equalTo(awsAuthenticationOptions)); + } + + @Test + void getAwsAuthenticationOptionsTestNegative() { + assertThat(new GeoIPProcessorConfig().getAwsAuthenticationOptions(), equalTo(null)); + } + + @Test + void getKeysConfigTestPositive() throws NoSuchFieldException, IllegalAccessException { + List keysConfigList = new ArrayList<>(); + keysConfigList.add(new KeysConfig()); + ReflectivelySetField.setField(GeoIPProcessorConfig.class, + geoIPProcessorConfig, "keysConfig", keysConfigList); + assertThat(geoIPProcessorConfig.getKeysConfig(), equalTo(keysConfigList)); + + } + + @Test + void getKeysConfigTestNegative() { + assertThat(new GeoIPProcessorConfig().getKeysConfig(), equalTo(null)); + } + + + @Test + void getServiceTypeTestPositive() throws NoSuchFieldException, IllegalAccessException { + ServiceTypeOptions serviceTypeOptions = new ServiceTypeOptions(); + ReflectivelySetField.setField(GeoIPProcessorConfig.class, + geoIPProcessorConfig, "serviceType", serviceTypeOptions); + assertThat(geoIPProcessorConfig.getServiceType(), equalTo(serviceTypeOptions)); + } + + @Test + void getServiceTypeNegative() { + assertThat(new GeoIPProcessorConfig().getServiceType(), equalTo(null)); + } + +} diff --git a/data-prepper-plugins/geoip-processor/src/test/java/org/opensearch/dataprepper/plugins/processor/configuration/AwsAuthenticationOptionsTest.java b/data-prepper-plugins/geoip-processor/src/test/java/org/opensearch/dataprepper/plugins/processor/configuration/AwsAuthenticationOptionsTest.java new file mode 100644 index 0000000000..a8f125003a --- /dev/null +++ b/data-prepper-plugins/geoip-processor/src/test/java/org/opensearch/dataprepper/plugins/processor/configuration/AwsAuthenticationOptionsTest.java @@ -0,0 +1,224 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ + +package org.opensearch.dataprepper.plugins.processor.configuration; + +import com.fasterxml.jackson.databind.ObjectMapper; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Nested; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; +import org.mockito.ArgumentCaptor; +import org.mockito.MockedStatic; +import org.opensearch.dataprepper.test.helper.ReflectivelySetField; +import software.amazon.awssdk.auth.credentials.AwsCredentialsProvider; +import software.amazon.awssdk.auth.credentials.DefaultCredentialsProvider; +import software.amazon.awssdk.awscore.AwsRequestOverrideConfiguration; +import software.amazon.awssdk.regions.Region; +import software.amazon.awssdk.services.sts.StsClient; +import software.amazon.awssdk.services.sts.StsClientBuilder; +import software.amazon.awssdk.services.sts.model.AssumeRoleRequest; + +import java.util.Collections; +import java.util.Map; +import java.util.UUID; +import java.util.function.Consumer; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.CoreMatchers.instanceOf; +import static org.hamcrest.CoreMatchers.nullValue; +import static org.hamcrest.CoreMatchers.sameInstance; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.mockStatic; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.verifyNoMoreInteractions; +import static org.mockito.Mockito.when; + +class AwsAuthenticationOptionsTest { + private ObjectMapper objectMapper; + + private AwsAuthenticationOptions awsAuthenticationOptions; + private final String TEST_ROLE = "arn:aws:iam::123456789012:role/test-role"; + @BeforeEach + void setUp() { + objectMapper = new ObjectMapper(); + awsAuthenticationOptions = new AwsAuthenticationOptions(); + } + + @ParameterizedTest + @ValueSource(strings = {"us-east-1", "us-west-2", "eu-central-1"}) + void getAwsRegion_returns_Region_of(final String regionString) { + final Region expectedRegionObject = Region.of(regionString); + final Map jsonMap = Map.of("region", regionString); + final AwsAuthenticationOptions objectUnderTest = objectMapper.convertValue(jsonMap, AwsAuthenticationOptions.class); + assertThat(objectUnderTest.getAwsRegion(), equalTo(expectedRegionObject)); + } + + @Test + void getAwsRegion_returns_null_when_region_is_null() { + final Map jsonMap = Collections.emptyMap(); + final AwsAuthenticationOptions objectUnderTest = objectMapper.convertValue(jsonMap, AwsAuthenticationOptions.class); + assertThat(objectUnderTest.getAwsRegion(), nullValue()); + } + + @Test + void authenticateAWSConfiguration_should_return_s3Client_without_sts_role_arn() throws NoSuchFieldException, IllegalAccessException { + ReflectivelySetField.setField(AwsAuthenticationOptions.class, awsAuthenticationOptions, "awsRegion", "us-east-1"); + ReflectivelySetField.setField(AwsAuthenticationOptions.class, awsAuthenticationOptions, "awsStsRoleArn", null); + + final DefaultCredentialsProvider mockedCredentialsProvider = mock(DefaultCredentialsProvider.class); + final AwsCredentialsProvider actualCredentialsProvider; + try (final MockedStatic defaultCredentialsProviderMockedStatic = mockStatic(DefaultCredentialsProvider.class)) { + defaultCredentialsProviderMockedStatic.when(DefaultCredentialsProvider::create) + .thenReturn(mockedCredentialsProvider); + actualCredentialsProvider = awsAuthenticationOptions.authenticateAwsConfiguration(); + } + + assertThat(actualCredentialsProvider, sameInstance(mockedCredentialsProvider)); + } + + @Nested + class WithSts { + private StsClient stsClient; + private StsClientBuilder stsClientBuilder; + + @BeforeEach + void setUp() { + stsClient = mock(StsClient.class); + stsClientBuilder = mock(StsClientBuilder.class); + + when(stsClientBuilder.build()).thenReturn(stsClient); + } + + @Test + void authenticateAWSConfiguration_should_return_s3Client_with_sts_role_arn() throws NoSuchFieldException, IllegalAccessException { + ReflectivelySetField.setField(AwsAuthenticationOptions.class, awsAuthenticationOptions, "awsRegion", "us-east-1"); + ReflectivelySetField.setField(AwsAuthenticationOptions.class, awsAuthenticationOptions, "awsStsRoleArn", TEST_ROLE); + + when(stsClientBuilder.region(Region.US_EAST_1)).thenReturn(stsClientBuilder); + final AssumeRoleRequest.Builder assumeRoleRequestBuilder = mock(AssumeRoleRequest.Builder.class); + when(assumeRoleRequestBuilder.roleSessionName(anyString())) + .thenReturn(assumeRoleRequestBuilder); + when(assumeRoleRequestBuilder.roleArn(anyString())) + .thenReturn(assumeRoleRequestBuilder); + + final AwsCredentialsProvider actualCredentialsProvider; + try (final MockedStatic stsClientMockedStatic = mockStatic(StsClient.class); + final MockedStatic assumeRoleRequestMockedStatic = mockStatic(AssumeRoleRequest.class)) { + stsClientMockedStatic.when(StsClient::builder).thenReturn(stsClientBuilder); + assumeRoleRequestMockedStatic.when(AssumeRoleRequest::builder).thenReturn(assumeRoleRequestBuilder); + actualCredentialsProvider = awsAuthenticationOptions.authenticateAwsConfiguration(); + } + + assertThat(actualCredentialsProvider, instanceOf(AwsCredentialsProvider.class)); + + verify(assumeRoleRequestBuilder).roleArn(TEST_ROLE); + verify(assumeRoleRequestBuilder).roleSessionName(anyString()); + verify(assumeRoleRequestBuilder).build(); + verifyNoMoreInteractions(assumeRoleRequestBuilder); + } + + @Test + void authenticateAWSConfiguration_should_return_s3Client_with_sts_role_arn_when_no_region() throws NoSuchFieldException, IllegalAccessException { + ReflectivelySetField.setField(AwsAuthenticationOptions.class, awsAuthenticationOptions, "awsRegion", null); + ReflectivelySetField.setField(AwsAuthenticationOptions.class, awsAuthenticationOptions, "awsStsRoleArn", TEST_ROLE); + assertThat(awsAuthenticationOptions.getAwsRegion(), equalTo(null)); + + when(stsClientBuilder.region(null)).thenReturn(stsClientBuilder); + + final AwsCredentialsProvider actualCredentialsProvider; + try (final MockedStatic stsClientMockedStatic = mockStatic(StsClient.class)) { + stsClientMockedStatic.when(StsClient::builder).thenReturn(stsClientBuilder); + actualCredentialsProvider = awsAuthenticationOptions.authenticateAwsConfiguration(); + } + + assertThat(actualCredentialsProvider, instanceOf(AwsCredentialsProvider.class)); + } + + @Test + void authenticateAWSConfiguration_should_override_STS_Headers_when_HeaderOverrides_when_set() throws NoSuchFieldException, IllegalAccessException { + final String headerName1 = UUID.randomUUID().toString(); + final String headerValue1 = UUID.randomUUID().toString(); + final String headerName2 = UUID.randomUUID().toString(); + final String headerValue2 = UUID.randomUUID().toString(); + final Map overrideHeaders = Map.of(headerName1, headerValue1, headerName2, headerValue2); + + ReflectivelySetField.setField(AwsAuthenticationOptions.class, awsAuthenticationOptions, "awsRegion", "us-east-1"); + ReflectivelySetField.setField(AwsAuthenticationOptions.class, awsAuthenticationOptions, "awsStsRoleArn", TEST_ROLE); + ReflectivelySetField.setField(AwsAuthenticationOptions.class, awsAuthenticationOptions, "awsStsHeaderOverrides", overrideHeaders); + + when(stsClientBuilder.region(Region.US_EAST_1)).thenReturn(stsClientBuilder); + + final AssumeRoleRequest.Builder assumeRoleRequestBuilder = mock(AssumeRoleRequest.Builder.class); + when(assumeRoleRequestBuilder.roleSessionName(anyString())) + .thenReturn(assumeRoleRequestBuilder); + when(assumeRoleRequestBuilder.roleArn(anyString())) + .thenReturn(assumeRoleRequestBuilder); + when(assumeRoleRequestBuilder.overrideConfiguration(any(Consumer.class))) + .thenReturn(assumeRoleRequestBuilder); + + final AwsCredentialsProvider actualCredentialsProvider; + try (final MockedStatic stsClientMockedStatic = mockStatic(StsClient.class); + final MockedStatic assumeRoleRequestMockedStatic = mockStatic(AssumeRoleRequest.class)) { + stsClientMockedStatic.when(StsClient::builder).thenReturn(stsClientBuilder); + assumeRoleRequestMockedStatic.when(AssumeRoleRequest::builder).thenReturn(assumeRoleRequestBuilder); + actualCredentialsProvider = awsAuthenticationOptions.authenticateAwsConfiguration(); + } + + assertThat(actualCredentialsProvider, instanceOf(AwsCredentialsProvider.class)); + + final ArgumentCaptor> configurationCaptor = ArgumentCaptor.forClass(Consumer.class); + + verify(assumeRoleRequestBuilder).roleArn(TEST_ROLE); + verify(assumeRoleRequestBuilder).roleSessionName(anyString()); + verify(assumeRoleRequestBuilder).overrideConfiguration(configurationCaptor.capture()); + verify(assumeRoleRequestBuilder).build(); + verifyNoMoreInteractions(assumeRoleRequestBuilder); + + final Consumer actualOverride = configurationCaptor.getValue(); + + final AwsRequestOverrideConfiguration.Builder configurationBuilder = mock(AwsRequestOverrideConfiguration.Builder.class); + actualOverride.accept(configurationBuilder); + verify(configurationBuilder).putHeader(headerName1, headerValue1); + verify(configurationBuilder).putHeader(headerName2, headerValue2); + verifyNoMoreInteractions(configurationBuilder); + } + + @Test + void authenticateAWSConfiguration_should_not_override_STS_Headers_when_HeaderOverrides_are_empty() throws NoSuchFieldException, IllegalAccessException { + + ReflectivelySetField.setField(AwsAuthenticationOptions.class, awsAuthenticationOptions, "awsRegion", "us-east-1"); + ReflectivelySetField.setField(AwsAuthenticationOptions.class, awsAuthenticationOptions, "awsStsRoleArn", TEST_ROLE); + ReflectivelySetField.setField(AwsAuthenticationOptions.class, awsAuthenticationOptions, "awsStsHeaderOverrides", Collections.emptyMap()); + + when(stsClientBuilder.region(Region.US_EAST_1)).thenReturn(stsClientBuilder); + final AssumeRoleRequest.Builder assumeRoleRequestBuilder = mock(AssumeRoleRequest.Builder.class); + when(assumeRoleRequestBuilder.roleSessionName(anyString())) + .thenReturn(assumeRoleRequestBuilder); + when(assumeRoleRequestBuilder.roleArn(anyString())) + .thenReturn(assumeRoleRequestBuilder); + + final AwsCredentialsProvider actualCredentialsProvider; + try (final MockedStatic stsClientMockedStatic = mockStatic(StsClient.class); + final MockedStatic assumeRoleRequestMockedStatic = mockStatic(AssumeRoleRequest.class)) { + stsClientMockedStatic.when(StsClient::builder).thenReturn(stsClientBuilder); + assumeRoleRequestMockedStatic.when(AssumeRoleRequest::builder).thenReturn(assumeRoleRequestBuilder); + actualCredentialsProvider = awsAuthenticationOptions.authenticateAwsConfiguration(); + } + + assertThat(actualCredentialsProvider, instanceOf(AwsCredentialsProvider.class)); + + verify(assumeRoleRequestBuilder).roleArn(TEST_ROLE); + verify(assumeRoleRequestBuilder).roleSessionName(anyString()); + verify(assumeRoleRequestBuilder).build(); + verifyNoMoreInteractions(assumeRoleRequestBuilder); + } + } + +} \ No newline at end of file diff --git a/data-prepper-plugins/geoip-processor/src/test/java/org/opensearch/dataprepper/plugins/processor/configuration/DatabasePathURLConfigTest.java b/data-prepper-plugins/geoip-processor/src/test/java/org/opensearch/dataprepper/plugins/processor/configuration/DatabasePathURLConfigTest.java new file mode 100644 index 0000000000..487a0f958d --- /dev/null +++ b/data-prepper-plugins/geoip-processor/src/test/java/org/opensearch/dataprepper/plugins/processor/configuration/DatabasePathURLConfigTest.java @@ -0,0 +1,38 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ + +package org.opensearch.dataprepper.plugins.processor.configuration; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.junit.jupiter.MockitoExtension; +import org.opensearch.dataprepper.test.helper.ReflectivelySetField; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +@ExtendWith(MockitoExtension.class) +class DatabasePathURLConfigTest { + + private static final String URL = "s3://mybucket10012023/GeoLite2"; + private DatabasePathURLConfig databasePathURLConfig; + + @BeforeEach + void setUp() { + databasePathURLConfig = new DatabasePathURLConfig(); + } + + @Test + void getUrlTestPositive() throws NoSuchFieldException, IllegalAccessException { + ReflectivelySetField.setField(DatabasePathURLConfig.class, databasePathURLConfig, "url", URL); + assertThat(databasePathURLConfig.getUrl(), equalTo(URL)); + } + + @Test + void getUrlTestNegative() { + assertThat(new DatabasePathURLConfig().getUrl(), equalTo(null)); + } +} diff --git a/data-prepper-plugins/geoip-processor/src/test/java/org/opensearch/dataprepper/plugins/processor/configuration/KeyConfigTest.java b/data-prepper-plugins/geoip-processor/src/test/java/org/opensearch/dataprepper/plugins/processor/configuration/KeyConfigTest.java new file mode 100644 index 0000000000..c88a8ce655 --- /dev/null +++ b/data-prepper-plugins/geoip-processor/src/test/java/org/opensearch/dataprepper/plugins/processor/configuration/KeyConfigTest.java @@ -0,0 +1,72 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ + +package org.opensearch.dataprepper.plugins.processor.configuration; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.junit.jupiter.MockitoExtension; +import org.opensearch.dataprepper.test.helper.ReflectivelySetField; + +import java.util.ArrayList; +import java.util.List; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +@ExtendWith(MockitoExtension.class) +class KeyConfigTest { + + public static final String SOURCE = "/peer/ip"; + public static final String TARGET = "location"; + private KeyConfig keyConfig; + + @BeforeEach + void setUp() { + keyConfig = new KeyConfig(); + } + + @Test + void getSourceTestPositive() throws NoSuchFieldException, IllegalAccessException { + ReflectivelySetField.setField(KeyConfig.class, keyConfig, "source", SOURCE); + assertThat(keyConfig.getSource(), equalTo(SOURCE)); + } + + @Test + void getSourceTestNegative() { + assertThat(new KeyConfig().getSource(), equalTo(null)); + } + + @Test + void getTargetTestPositive() throws NoSuchFieldException, IllegalAccessException { + ReflectivelySetField.setField(KeyConfig.class, keyConfig, "target", TARGET); + assertThat(keyConfig.getTarget(), equalTo(TARGET)); + } + + @Test + void getTargetTestNegative() { + assertThat(new KeyConfig().getTarget(), equalTo(null)); + } + + @Test + void getAttributesTestPositive() throws NoSuchFieldException, IllegalAccessException { + final List attributes = setAttributes(); + ReflectivelySetField.setField(KeyConfig.class, keyConfig, "attributes", attributes); + assertThat(keyConfig.getAttributes().get(0), equalTo(attributes.get(0))); + } + + @Test + void getAttributesTestNegative() { + assertThat(new KeyConfig().getAttributes(), equalTo(null)); + } + + private List setAttributes() { + final List attributes = new ArrayList<>(); + attributes.add("city_name"); + attributes.add("country_name"); + return attributes; + } +} \ No newline at end of file diff --git a/data-prepper-plugins/geoip-processor/src/test/java/org/opensearch/dataprepper/plugins/processor/configuration/KeysConfigTest.java b/data-prepper-plugins/geoip-processor/src/test/java/org/opensearch/dataprepper/plugins/processor/configuration/KeysConfigTest.java new file mode 100644 index 0000000000..952ea174ce --- /dev/null +++ b/data-prepper-plugins/geoip-processor/src/test/java/org/opensearch/dataprepper/plugins/processor/configuration/KeysConfigTest.java @@ -0,0 +1,38 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ + +package org.opensearch.dataprepper.plugins.processor.configuration; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.junit.jupiter.MockitoExtension; +import org.opensearch.dataprepper.test.helper.ReflectivelySetField; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +@ExtendWith(MockitoExtension.class) +class KeysConfigTest { + + private KeysConfig keysConfig; + private final KeyConfig keyConfig = new KeyConfig(); + + @BeforeEach + void setUp() { + keysConfig = new KeysConfig(); + } + + @Test + void getKeyConfigTestPositive() throws NoSuchFieldException, IllegalAccessException { + ReflectivelySetField.setField(KeysConfig.class, keysConfig, "keyConfig", keyConfig); + assertThat(keysConfig.getKeyConfig(), equalTo(keyConfig)); + } + + @Test + void getKeyConfigTestNegative() { + assertThat(new KeysConfig().getKeyConfig(), equalTo(null)); + } +} diff --git a/data-prepper-plugins/geoip-processor/src/test/java/org/opensearch/dataprepper/plugins/processor/configuration/MaxMindServiceConfigTest.java b/data-prepper-plugins/geoip-processor/src/test/java/org/opensearch/dataprepper/plugins/processor/configuration/MaxMindServiceConfigTest.java new file mode 100644 index 0000000000..7a6811f8fb --- /dev/null +++ b/data-prepper-plugins/geoip-processor/src/test/java/org/opensearch/dataprepper/plugins/processor/configuration/MaxMindServiceConfigTest.java @@ -0,0 +1,93 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ + +package org.opensearch.dataprepper.plugins.processor.configuration; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.junit.jupiter.MockitoExtension; +import org.opensearch.dataprepper.plugins.processor.loadtype.LoadTypeOptions; +import org.opensearch.dataprepper.test.helper.ReflectivelySetField; + +import java.time.Duration; +import java.util.ArrayList; +import java.util.List; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +@ExtendWith(MockitoExtension.class) +class MaxMindServiceConfigTest { + + private static final String URL = "s3://mybucket10012023/GeoLite2"; + private MaxMindServiceConfig maxMindServiceConfig; + + @BeforeEach + void setUp() { + maxMindServiceConfig = new MaxMindServiceConfig(); + } + + @Test + void getDatabasePathTestPositive() throws NoSuchFieldException, IllegalAccessException { + List databasePathURLConfigList = setDatabasePath(); + ReflectivelySetField.setField(MaxMindServiceConfig.class, + maxMindServiceConfig, "databasePath", databasePathURLConfigList); + assertThat(maxMindServiceConfig.getDatabasePath(), equalTo(databasePathURLConfigList)); + } + + @Test + void getDatabasePathTestNegative() { + assertThat(new MaxMindServiceConfig().getDatabasePath(), equalTo(null)); + } + + @Test + void getLoadTypeTestPositive() throws NoSuchFieldException, IllegalAccessException { + LoadTypeOptions loadTypeOptions = LoadTypeOptions.INMEMORY; + ReflectivelySetField.setField(MaxMindServiceConfig.class, + maxMindServiceConfig, "loadType", loadTypeOptions); + assertThat(maxMindServiceConfig.getLoadType(), equalTo(loadTypeOptions)); + } + + @Test + void getLoadTypeTestNegative() { + assertThat(new MaxMindServiceConfig().getLoadType(), equalTo(null)); + } + + @Test + void getCacheSizeTestPositive() throws NoSuchFieldException, IllegalAccessException { + Integer cacheSize = 8192; + ReflectivelySetField.setField(MaxMindServiceConfig.class, + maxMindServiceConfig, "cacheSize", cacheSize); + assertThat(maxMindServiceConfig.getCacheSize(), equalTo(cacheSize)); + } + + @Test + void getCacheSizeTestNegative() { + assertThat(new MaxMindServiceConfig().getCacheSize(), equalTo(null)); + } + + @Test + void getCacheRefreshScheduleTestPositive() throws NoSuchFieldException, IllegalAccessException { + Duration cacheRefreshSchedule = Duration.parse("PT3M"); + ReflectivelySetField.setField(MaxMindServiceConfig.class, + maxMindServiceConfig, "cacheRefreshSchedule", cacheRefreshSchedule); + assertThat(maxMindServiceConfig.getCacheRefreshSchedule(), equalTo(cacheRefreshSchedule)); + } + + @Test + void getCacheRefreshScheduleTestNegative() { + assertThat(new MaxMindServiceConfig().getCacheRefreshSchedule(), equalTo(null)); + } + + private List setDatabasePath() throws NoSuchFieldException, IllegalAccessException { + + List databasePathURLConfigList = new ArrayList<>(); + DatabasePathURLConfig databasePathURLConfig = new DatabasePathURLConfig(); + ReflectivelySetField.setField(DatabasePathURLConfig.class, databasePathURLConfig, "url", URL); + databasePathURLConfigList.add(databasePathURLConfig); + return databasePathURLConfigList; + } +} diff --git a/data-prepper-plugins/geoip-processor/src/test/java/org/opensearch/dataprepper/plugins/processor/configuration/ServiceTypeOptionsTest.java b/data-prepper-plugins/geoip-processor/src/test/java/org/opensearch/dataprepper/plugins/processor/configuration/ServiceTypeOptionsTest.java new file mode 100644 index 0000000000..94f8fa73b7 --- /dev/null +++ b/data-prepper-plugins/geoip-processor/src/test/java/org/opensearch/dataprepper/plugins/processor/configuration/ServiceTypeOptionsTest.java @@ -0,0 +1,39 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ + +package org.opensearch.dataprepper.plugins.processor.configuration; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.junit.jupiter.MockitoExtension; +import org.opensearch.dataprepper.test.helper.ReflectivelySetField; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +@ExtendWith(MockitoExtension.class) +class ServiceTypeOptionsTest { + + private ServiceTypeOptions serviceTypeOptions; + + @BeforeEach + void setUp() { + serviceTypeOptions = new ServiceTypeOptions(); + } + + @Test + void getMaxMindServiceTestPositive() throws NoSuchFieldException, IllegalAccessException { + MaxMindServiceConfig maxMindServiceConfig = new MaxMindServiceConfig(); + ReflectivelySetField.setField(ServiceTypeOptions.class, + serviceTypeOptions, "maxMindService", maxMindServiceConfig); + assertThat(serviceTypeOptions.getMaxMindService(), equalTo(maxMindServiceConfig)); + } + + @Test + void getMaxMindServiceTestNegative() { + assertThat(new ServiceTypeOptions().getMaxMindService(), equalTo(null)); + } +} diff --git a/data-prepper-plugins/geoip-processor/src/test/java/org/opensearch/dataprepper/plugins/processor/loadtype/LoadTypeOptionsTest.java b/data-prepper-plugins/geoip-processor/src/test/java/org/opensearch/dataprepper/plugins/processor/loadtype/LoadTypeOptionsTest.java new file mode 100644 index 0000000000..50c901cd5c --- /dev/null +++ b/data-prepper-plugins/geoip-processor/src/test/java/org/opensearch/dataprepper/plugins/processor/loadtype/LoadTypeOptionsTest.java @@ -0,0 +1,30 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ + +package org.opensearch.dataprepper.plugins.processor.loadtype; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.junit.jupiter.MockitoExtension; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.jupiter.api.Assertions.assertNotNull; + +@ExtendWith(MockitoExtension.class) +class LoadTypeOptionsTest { + + @Test + void notNull_test() { + assertNotNull(LoadTypeOptions.INMEMORY); + } + + @Test + void fromOptionValue_test() { + LoadTypeOptions loadTypeOptions = LoadTypeOptions.fromOptionValue("in_memory"); + assertNotNull(loadTypeOptions); + assertThat(loadTypeOptions.toString(), equalTo("INMEMORY")); + } +} diff --git a/data-prepper-plugins/geoip-processor/src/test/resources/mockito-extensions/org.mockito.plugins.MockMaker b/data-prepper-plugins/geoip-processor/src/test/resources/mockito-extensions/org.mockito.plugins.MockMaker new file mode 100644 index 0000000000..23c33feb6d --- /dev/null +++ b/data-prepper-plugins/geoip-processor/src/test/resources/mockito-extensions/org.mockito.plugins.MockMaker @@ -0,0 +1,3 @@ +# To enable mocking of final classes with vanilla Mockito +# https://github.com/mockito/mockito/wiki/What%27s-new-in-Mockito-2#mock-the-unmockable-opt-in-mocking-of-final-classesmethods +mock-maker-inline