From 4f3ab967a14ba23d3957ad6295da0467282cf029 Mon Sep 17 00:00:00 2001 From: David Venable Date: Thu, 28 Mar 2024 14:06:31 -0500 Subject: [PATCH] Adds integration tests for the geoip processor (#4353) Adds integration tests for the geoip processor. Signed-off-by: Asif Sohail Mohammed Signed-off-by: David Venable Co-authored-by: Asif Sohail Mohammed --- .../geoip-processor/build.gradle | 41 ++- .../geoip/processor/GeoIPProcessorIT.java | 255 ++++++++++++++++++ .../plugins/processor/GeoIPInputJson.java | 26 -- .../processor/GeoIPProcessorUrlServiceIT.java | 104 ------- .../dataprepper/plugins/processor/Peer.java | 26 -- .../geoip/extension/MaxMindConfig.java | 3 +- .../geoip/extension/MaxMindConfigTest.java | 21 +- 7 files changed, 307 insertions(+), 169 deletions(-) create mode 100644 data-prepper-plugins/geoip-processor/src/integrationTest/java/org/opensearch/dataprepper/plugins/geoip/processor/GeoIPProcessorIT.java delete mode 100644 data-prepper-plugins/geoip-processor/src/integrationTest/java/org/opensearch/dataprepper/plugins/processor/GeoIPInputJson.java delete mode 100644 data-prepper-plugins/geoip-processor/src/integrationTest/java/org/opensearch/dataprepper/plugins/processor/GeoIPProcessorUrlServiceIT.java delete mode 100644 data-prepper-plugins/geoip-processor/src/integrationTest/java/org/opensearch/dataprepper/plugins/processor/Peer.java diff --git a/data-prepper-plugins/geoip-processor/build.gradle b/data-prepper-plugins/geoip-processor/build.gradle index da09a39e64..d1ebc0c971 100644 --- a/data-prepper-plugins/geoip-processor/build.gradle +++ b/data-prepper-plugins/geoip-processor/build.gradle @@ -48,7 +48,7 @@ databaseNames.forEach { databaseName -> { def gradleName = databaseName.replaceAll('-', '') def downloadTask = tasks.register("download${gradleName}", Download) { src(url) - dest "build/resources/test/mmdb-files/geo-lite2/${databaseName}.mmdb" + dest "${project.layout.buildDirectory.get()}/resources/test/mmdb-files/geo-lite2/${databaseName}.mmdb" overwrite true } downloadFiles.get().dependsOn downloadTask @@ -64,22 +64,56 @@ enterpriseDatabaseNames.forEach { enterpriseDatabaseName -> { def gradleName = enterpriseDatabaseName.replaceAll('-', '') def downloadEnterpriseTask = tasks.register("download${gradleName}", Download) { src(url) - dest "build/resources/test/mmdb-files/geo-ip2/${enterpriseDatabaseName}.mmdb" + dest "${project.layout.buildDirectory.get()}/resources/test/mmdb-files/geo-ip2/${enterpriseDatabaseName}.mmdb" overwrite true } downloadFiles.get().dependsOn downloadEnterpriseTask }} +sourceSets { + integrationTest { + java { + compileClasspath += main.output + test.output + runtimeClasspath += main.output + test.output + srcDir file('src/integrationTest/java') + } + } +} + +configurations { + integrationTestImplementation.extendsFrom testImplementation + integrationTestRuntime.extendsFrom testRuntime +} + +task integrationTest(type: Test) { + group = 'verification' + testClassesDirs = sourceSets.integrationTest.output.classesDirs + + useJUnitPlatform() + + classpath = sourceSets.integrationTest.runtimeClasspath + + systemProperty 'data-prepper.dir', project.layout.buildDirectory.get().getAsFile().absolutePath + + filter { + includeTestsMatching '*IT' + } +} + test { useJUnitPlatform() dependsOn(downloadFiles) + + systemProperty 'data-prepper.dir', project.layout.buildDirectory.get().getAsFile().absolutePath } checkstyleTest { dependsOn(downloadFiles) } +compileIntegrationTestJava.dependsOn downloadFiles + jacocoTestCoverageVerification { dependsOn jacocoTestReport violationRules { @@ -91,4 +125,5 @@ jacocoTestCoverageVerification { } } -check.dependsOn jacocoTestCoverageVerification \ No newline at end of file +check.dependsOn jacocoTestCoverageVerification +check.dependsOn integrationTest \ No newline at end of file diff --git a/data-prepper-plugins/geoip-processor/src/integrationTest/java/org/opensearch/dataprepper/plugins/geoip/processor/GeoIPProcessorIT.java b/data-prepper-plugins/geoip-processor/src/integrationTest/java/org/opensearch/dataprepper/plugins/geoip/processor/GeoIPProcessorIT.java new file mode 100644 index 0000000000..57de2a3185 --- /dev/null +++ b/data-prepper-plugins/geoip-processor/src/integrationTest/java/org/opensearch/dataprepper/plugins/geoip/processor/GeoIPProcessorIT.java @@ -0,0 +1,255 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ + +package org.opensearch.dataprepper.plugins.geoip.processor; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.dataformat.yaml.YAMLFactory; +import com.fasterxml.jackson.dataformat.yaml.YAMLGenerator; +import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; +import io.micrometer.core.instrument.Counter; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.ArgumentCaptor; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; +import org.opensearch.dataprepper.expression.ExpressionEvaluator; +import org.opensearch.dataprepper.metrics.PluginMetrics; +import org.opensearch.dataprepper.model.event.Event; +import org.opensearch.dataprepper.model.event.JacksonEvent; +import org.opensearch.dataprepper.model.plugin.ExtensionPoints; +import org.opensearch.dataprepper.model.plugin.ExtensionProvider; +import org.opensearch.dataprepper.model.record.Record; +import org.opensearch.dataprepper.plugins.geoip.extension.GeoIpConfigExtension; +import org.opensearch.dataprepper.plugins.geoip.extension.GeoIpServiceConfig; +import org.opensearch.dataprepper.plugins.geoip.extension.api.GeoIpConfigSupplier; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.Mockito.lenient; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.verifyNoMoreInteractions; +import static org.mockito.Mockito.when; +import static org.opensearch.dataprepper.plugins.geoip.processor.GeoIPProcessor.GEO_IP_EVENTS_FAILED; +import static org.opensearch.dataprepper.plugins.geoip.processor.GeoIPProcessor.GEO_IP_EVENTS_FAILED_ENGINE_EXCEPTION; +import static org.opensearch.dataprepper.plugins.geoip.processor.GeoIPProcessor.GEO_IP_EVENTS_FAILED_IP_NOT_FOUND; +import static org.opensearch.dataprepper.plugins.geoip.processor.GeoIPProcessor.GEO_IP_EVENTS_PROCESSED; +import static org.opensearch.dataprepper.plugins.geoip.processor.GeoIPProcessor.GEO_IP_EVENTS_SUCCEEDED; + +@ExtendWith(MockitoExtension.class) +public class GeoIPProcessorIT { + + private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper(new YAMLFactory().enable(YAMLGenerator.Feature.USE_PLATFORM_LINE_BREAKS)) + .registerModule(new JavaTimeModule()); + private static final String COUNTRY_URL ="https://geoip.maps.opensearch.org/v1/mmdb/geolite2-country/manifest.json"; + private static final String CITY_URL = "https://geoip.maps.opensearch.org/v1/mmdb/geolite2-city/manifest.json"; + private static final String ASN_URL = "https://geoip.maps.opensearch.org/v1/mmdb/geolite2-asn/manifest.json"; + private static GeoIPProcessorConfig geoipProcessorConfig; + private static GeoIpConfigSupplier defaultGeoIpConfigSupplier; + @Mock + private PluginMetrics pluginMetrics; + @Mock + private ExpressionEvaluator expressionEvaluator; + @Mock + private Counter geoIpEventsProcessed; + @Mock + private Counter geoIpEventsFailed; + @Mock + private Counter geoIpEventsSucceeded; + @Mock + private Counter geoIpEventsFailedEngineException; + @Mock + private Counter geoIpEventsFailedIPNotFound; + + @BeforeAll + public static void downloadDatabases() throws JsonProcessingException { + String geoipProcessorConfigYaml = " entries:\n" + + " - source: \"peer/ip\"\n" + + " target: \"geo\"\n" + + " include_fields: [\"country_name\", \"continent_name\", \"location\", \"asn\"]\n" + + " geoip_when: '/peer/status == \"success\"'\n" + + " tags_on_no_valid_ip: [\"private_ip\", \"invalid_ip\"]"; + String geoipServiceConfigYaml = " maxmind:\n" + + " databases:\n" + + " country: " + COUNTRY_URL + "\n" + + " city: " + CITY_URL + "\n" + + " asn: " + ASN_URL + "\n" + + " cache_count: 1024\n" + + " database_refresh_interval: P3D"; + + geoipProcessorConfig = OBJECT_MAPPER.readValue(geoipProcessorConfigYaml, GeoIPProcessorConfig.class); + GeoIpServiceConfig geoipServiceConfig = OBJECT_MAPPER.readValue(geoipServiceConfigYaml, GeoIpServiceConfig.class); + + System.setProperty("data-prepper.dir", "build"); + final ExtensionPoints extensionPoints = mock(ExtensionPoints.class); + final GeoIpConfigExtension geoIpConfigExtension = new GeoIpConfigExtension(geoipServiceConfig); + geoIpConfigExtension.apply(extensionPoints); + final ArgumentCaptor> extensionProviderArgumentCaptor = ArgumentCaptor.forClass(ExtensionProvider.class); + verify(extensionPoints).addExtensionProvider(extensionProviderArgumentCaptor.capture()); + final ExtensionProvider extensionProvider = extensionProviderArgumentCaptor.getValue(); + defaultGeoIpConfigSupplier = extensionProvider.provideInstance(null).get(); + } + + @BeforeEach + public void setUp() { + lenient().when(pluginMetrics.counter(GEO_IP_EVENTS_PROCESSED)).thenReturn(geoIpEventsProcessed); + lenient().when(pluginMetrics.counter(GEO_IP_EVENTS_SUCCEEDED)).thenReturn(geoIpEventsSucceeded); + lenient().when(pluginMetrics.counter(GEO_IP_EVENTS_FAILED)).thenReturn(geoIpEventsFailed); + lenient().when(pluginMetrics.counter(GEO_IP_EVENTS_FAILED_ENGINE_EXCEPTION)).thenReturn(geoIpEventsFailedEngineException); + lenient().when(pluginMetrics.counter(GEO_IP_EVENTS_FAILED_IP_NOT_FOUND)).thenReturn(geoIpEventsFailedIPNotFound); + } + + public GeoIPProcessor createObjectUnderTest() { + return new GeoIPProcessor(pluginMetrics, geoipProcessorConfig, defaultGeoIpConfigSupplier, expressionEvaluator); + } + + @AfterEach + void tearDown() { + verifyNoMoreInteractions(geoIpEventsProcessed); + verifyNoMoreInteractions(geoIpEventsSucceeded); + verifyNoMoreInteractions(geoIpEventsFailed); + verifyNoMoreInteractions(geoIpEventsFailedEngineException); + verifyNoMoreInteractions(geoIpEventsFailedIPNotFound); + } + + + @Test + void test_public_ipv4() { + final Collection> inputRecords = generateEventData("8.8.8.8"); + for (Record record: inputRecords) { + when(expressionEvaluator.evaluateConditional("/peer/status == \"success\"", record.getData())).thenReturn(true); + } + final GeoIPProcessor objectUnderTest = createObjectUnderTest(); + + final Collection> records = objectUnderTest.doExecute(inputRecords); + verifyGeoDataAndMetricsOnSuccess(records); + } + + @Test + void test_public_ipv6() { + final Collection> inputRecords = generateEventData("2001:4860:4860::8888"); + for (Record record: inputRecords) { + when(expressionEvaluator.evaluateConditional("/peer/status == \"success\"", record.getData())).thenReturn(true); + } + final GeoIPProcessor objectUnderTest = createObjectUnderTest(); + + final Collection> records = objectUnderTest.doExecute(inputRecords); + verifyGeoDataAndMetricsOnSuccess(records); + } + + @Test + void test_private_ipv4_should_not_add_geodata_but_adds_tags() { + final List tags = List.of("private_ip", "invalid_ip"); + final Collection> inputRecords = generateEventData("192.168.255.255"); + for (Record record: inputRecords) { + when(expressionEvaluator.evaluateConditional("/peer/status == \"success\"", record.getData())).thenReturn(true); + } + final GeoIPProcessor objectUnderTest = createObjectUnderTest(); + + final Collection> records = objectUnderTest.doExecute(inputRecords); + for (Record record: records) { + final Event event = record.getData(); + assertFalse(event.containsKey("geo")); + assertTrue(event.getMetadata().hasTags(tags)); + + verify(geoIpEventsProcessed).increment(); + verify(geoIpEventsFailed).increment(); + } + } + + @Test + void test_private_ipv6_should_not_add_geodata_but_adds_tags() { + final List tags = List.of("private_ip", "invalid_ip"); + final Collection> inputRecords = generateEventData("FD00::BE2:54:34:2/7"); + for (Record record: inputRecords) { + when(expressionEvaluator.evaluateConditional("/peer/status == \"success\"", record.getData())).thenReturn(true); + } + final GeoIPProcessor objectUnderTest = createObjectUnderTest(); + + final Collection> records = objectUnderTest.doExecute(inputRecords); + for (Record record: records) { + final Event event = record.getData(); + assertFalse(event.containsKey("geo")); + assertTrue(event.getMetadata().hasTags(tags)); + + verify(geoIpEventsProcessed).increment(); + verify(geoIpEventsFailed).increment(); + } + } + + @Test + void test_geoip_doExecute_should_add_geodata_if_when_condition_is_true() { + final GeoIPProcessor objectUnderTest = createObjectUnderTest(); + final Collection> inputRecords = generateEventData("8.8.8.8"); + for (Record record: inputRecords) { + when(expressionEvaluator.evaluateConditional("/peer/status == \"success\"", record.getData())).thenReturn(true); + } + + final Collection> records = objectUnderTest.doExecute(inputRecords); + verifyGeoDataAndMetricsOnSuccess(records); + } + + @Test + void test_geoip_doExecute_should_not_add_geodata_if_when_condition_is_false() { + final GeoIPProcessor objectUnderTest = createObjectUnderTest(); + final Collection> inputRecords = generateEventData("8.8.8.8"); + for (Record record: inputRecords) { + when(expressionEvaluator.evaluateConditional("/peer/status == \"success\"", record.getData())).thenReturn(false); + } + + final Collection> records = objectUnderTest.doExecute(inputRecords); + for (Record record: records) { + final Event event = record.getData(); + assertFalse(event.containsKey("geo")); + } + } + + private void verifyGeoDataAndMetricsOnSuccess(Collection> records) { + for (Record record: records) { + final Event event = record.getData(); + assertTrue(event.containsKey("geo")); + assertTrue(event.containsKey("geo/continent_name")); + assertTrue(event.containsKey("geo/country_name")); + assertTrue(event.containsKey("geo/location")); + assertTrue(event.containsKey("geo/asn")); + + verify(geoIpEventsProcessed).increment(); + verify(geoIpEventsSucceeded).increment(); + } + } + + private Collection> generateEventData(final String ipAddress) { + final List> records = new ArrayList<>(); + final List ips = List.of(ipAddress); + for (final String ip: ips) { + Map innerMap = new HashMap<>(); + innerMap.put("ip", ip); + innerMap.put("host", "example.org"); + innerMap.put("status", "success"); + + final Map eventMap1 = new HashMap<>(); + eventMap1.put("peer", innerMap); + + final Event firstEvent = JacksonEvent.builder() + .withData(eventMap1) + .withEventType("event") + .build(); + + records.add(new Record<>(firstEvent)); + } + return records; + } +} diff --git a/data-prepper-plugins/geoip-processor/src/integrationTest/java/org/opensearch/dataprepper/plugins/processor/GeoIPInputJson.java b/data-prepper-plugins/geoip-processor/src/integrationTest/java/org/opensearch/dataprepper/plugins/processor/GeoIPInputJson.java deleted file mode 100644 index 9e84788bea..0000000000 --- a/data-prepper-plugins/geoip-processor/src/integrationTest/java/org/opensearch/dataprepper/plugins/processor/GeoIPInputJson.java +++ /dev/null @@ -1,26 +0,0 @@ -/* - * Copyright OpenSearch Contributors - * SPDX-License-Identifier: Apache-2.0 - */ - -package org.opensearch.dataprepper.plugins.processor; - -public class GeoIPInputJson { - Peer PeerObject; - private String status; - - // Getter Methods - public Peer getPeer() { - return PeerObject; - } - public String getStatus() { - return status; - } - // Setter Methods - public void setPeer( Peer peerObject ) { - this.PeerObject = peerObject; - } - public void setStatus( String status ) { - this.status = status; - } -} diff --git a/data-prepper-plugins/geoip-processor/src/integrationTest/java/org/opensearch/dataprepper/plugins/processor/GeoIPProcessorUrlServiceIT.java b/data-prepper-plugins/geoip-processor/src/integrationTest/java/org/opensearch/dataprepper/plugins/processor/GeoIPProcessorUrlServiceIT.java deleted file mode 100644 index 3623a81dd3..0000000000 --- a/data-prepper-plugins/geoip-processor/src/integrationTest/java/org/opensearch/dataprepper/plugins/processor/GeoIPProcessorUrlServiceIT.java +++ /dev/null @@ -1,104 +0,0 @@ -/* - * Copyright OpenSearch Contributors - * SPDX-License-Identifier: Apache-2.0 - */ - -package org.opensearch.dataprepper.plugins.processor; - -import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.dataformat.yaml.YAMLFactory; -import com.fasterxml.jackson.dataformat.yaml.YAMLGenerator; -import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; -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.extension.GeoIPProcessorService; -import org.opensearch.dataprepper.plugins.processor.utils.IPValidationCheck; - -import java.io.File; -import java.net.InetAddress; -import java.net.UnknownHostException; -import java.util.HashMap; -import java.util.Map; - -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.equalTo; - -@ExtendWith(MockitoExtension.class) -public class GeoIPProcessorUrlServiceIT { - - private ObjectMapper objectMapper = new ObjectMapper(new YAMLFactory().enable(YAMLGenerator.Feature.USE_PLATFORM_LINE_BREAKS)); - private String tempPath; - private GeoIPProcessorConfig geoIPProcessorConfig; - private String maxmindLicenseKey; - private GeoIPProcessorService geoIPProcessorService; - private GeoIPInputJson geoIPInputJson; - private String jsonInput; - private static final String TEMP_PATH_FOLDER = "GeoIP"; - public static final String DATABASE_1 = "blue_database"; - public static final String URL_SUFFIX = "&suffix=tar.gz"; - - @BeforeEach - public void setUp() throws JsonProcessingException { - - maxmindLicenseKey = System.getProperty("tests.geoipProcessor.maxmindLicenseKey"); - - jsonInput = "{\"peer\": {\"ip\": \"8.8.8.8\", \"host\": \"example.org\" }, \"status\": \"success\"}"; - - geoIPInputJson = objectMapper.readValue(jsonInput, GeoIPInputJson.class); - tempPath = System.getProperty("java.io.tmpdir")+ File.separator + TEMP_PATH_FOLDER; - - String asnUrl = "https://download.maxmind.com/app/geoip_download?edition_id=GeoLite2-ASN&license_key=" + maxmindLicenseKey + URL_SUFFIX; - String cityUrl = "https://download.maxmind.com/app/geoip_download?edition_id=GeoLite2-City&license_key=" + maxmindLicenseKey + URL_SUFFIX; - String countryUrl = "https://download.maxmind.com/app/geoip_download?edition_id=GeoLite2-Country&license_key=" + maxmindLicenseKey + URL_SUFFIX; - - String pipelineConfig = " aws:\n" + - " region: us-east-2\n" + - " sts_role_arn: \"arn:aws:iam::123456789:role/data-prepper-execution-role\"\n" + - " keys:\n" + - " - key:\n" + - " source: \"/peer/ip\"\n" + - " target: \"target1\"\n" + - " - key:\n" + - " source: \"/peer/ip2\"\n" + - " target: \"target2\"\n" + - " attributes: [\"city_name\",\"country_name\"]\n" + - " service_type:\n" + - " maxmind:\n" + - " database_path:\n" + - " - url: " + asnUrl + "\n" + - " - url: " + cityUrl + "\n" + - " - url: " + countryUrl + "\n" + - " load_type: \"cache\"\n" + - " cache_size: 8192\n" + - " cache_refresh_schedule: PT3M"; - - objectMapper.registerModule(new JavaTimeModule()); - this.geoIPProcessorConfig = objectMapper.readValue(pipelineConfig, GeoIPProcessorConfig.class); - } - - public GeoIPProcessorService createObjectUnderTest() { - // TODO: pass in geoIpServiceConfig object - return new GeoIPProcessorService(null, null, null); - } - - @Test - void verify_enrichment_of_data_from_maxmind_url() throws UnknownHostException { - - Map geoData = new HashMap<>(); - this.geoIPProcessorService = createObjectUnderTest(); - String ipAddress = geoIPInputJson.getPeer().getIp(); - if (IPValidationCheck.isPublicIpAddress(ipAddress)) { - InetAddress inetAddress = InetAddress.getByName(ipAddress); - //All attributes are considered by default with the null value -// geoData = geoIPProcessorService.getGeoData(inetAddress, null); - - assertThat(geoData.get("country_iso_code"), equalTo("US")); - assertThat(geoData.get("ip"), equalTo("8.8.8.8")); - assertThat(geoData.get("country_name"), equalTo("United States")); - assertThat(geoData.get("organization_name"), equalTo("GOOGLE")); - } - } -} \ No newline at end of file diff --git a/data-prepper-plugins/geoip-processor/src/integrationTest/java/org/opensearch/dataprepper/plugins/processor/Peer.java b/data-prepper-plugins/geoip-processor/src/integrationTest/java/org/opensearch/dataprepper/plugins/processor/Peer.java deleted file mode 100644 index bd0c0da9b6..0000000000 --- a/data-prepper-plugins/geoip-processor/src/integrationTest/java/org/opensearch/dataprepper/plugins/processor/Peer.java +++ /dev/null @@ -1,26 +0,0 @@ -/* - * Copyright OpenSearch Contributors - * SPDX-License-Identifier: Apache-2.0 - */ - -package org.opensearch.dataprepper.plugins.processor; - -public class Peer { - private String ip; - private String host; - - // Getter Methods - public String getIp() { - return ip; - } - public String getHost() { - return host; - } - // Setter Methods - public void setIp( String ip ) { - this.ip = ip; - } - public void setHost( String host ) { - this.host = host; - } -} \ No newline at end of file diff --git a/data-prepper-plugins/geoip-processor/src/main/java/org/opensearch/dataprepper/plugins/geoip/extension/MaxMindConfig.java b/data-prepper-plugins/geoip-processor/src/main/java/org/opensearch/dataprepper/plugins/geoip/extension/MaxMindConfig.java index 0205b9353d..24d47715a2 100644 --- a/data-prepper-plugins/geoip-processor/src/main/java/org/opensearch/dataprepper/plugins/geoip/extension/MaxMindConfig.java +++ b/data-prepper-plugins/geoip-processor/src/main/java/org/opensearch/dataprepper/plugins/geoip/extension/MaxMindConfig.java @@ -25,7 +25,6 @@ public class MaxMindConfig { private static final String S3_PREFIX = "s3://"; private static final Duration DEFAULT_DATABASE_REFRESH_INTERVAL = Duration.ofDays(7); private static final int DEFAULT_CACHE_COUNT = 4096; - static final String DEFAULT_DATABASE_DESTINATION = System.getProperty("data-prepper.dir") + File.separator + "data" + File.separator + "geoip"; @Valid @JsonProperty("databases") @@ -49,7 +48,7 @@ public class MaxMindConfig { private boolean insecure = DEFAULT_INSECURE; @JsonProperty("database_destination") - private String databaseDestination = DEFAULT_DATABASE_DESTINATION; + private String databaseDestination = System.getProperty("data-prepper.dir") + File.separator + "data" + File.separator + "geoip"; public MaxMindConfig() { // This default constructor is used if maxmind is not configured diff --git a/data-prepper-plugins/geoip-processor/src/test/java/org/opensearch/dataprepper/plugins/geoip/extension/MaxMindConfigTest.java b/data-prepper-plugins/geoip-processor/src/test/java/org/opensearch/dataprepper/plugins/geoip/extension/MaxMindConfigTest.java index 1fc82b151c..4da14fe339 100644 --- a/data-prepper-plugins/geoip-processor/src/test/java/org/opensearch/dataprepper/plugins/geoip/extension/MaxMindConfigTest.java +++ b/data-prepper-plugins/geoip-processor/src/test/java/org/opensearch/dataprepper/plugins/geoip/extension/MaxMindConfigTest.java @@ -5,7 +5,7 @@ package org.opensearch.dataprepper.plugins.geoip.extension; -import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.RepeatedTest; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.junit.jupiter.params.ParameterizedTest; @@ -14,38 +14,41 @@ import org.mockito.junit.jupiter.MockitoExtension; import org.opensearch.dataprepper.test.helper.ReflectivelySetField; +import java.io.File; import java.net.URISyntaxException; import java.time.Duration; import java.util.Map; +import java.util.UUID; import static org.hamcrest.CoreMatchers.equalTo; import static org.hamcrest.CoreMatchers.instanceOf; import static org.hamcrest.MatcherAssert.assertThat; import static org.mockito.Mockito.when; -import static org.opensearch.dataprepper.plugins.geoip.extension.MaxMindConfig.DEFAULT_DATABASE_DESTINATION; @ExtendWith(MockitoExtension.class) class MaxMindConfigTest { - private MaxMindConfig maxMindConfig; @Mock private MaxMindDatabaseConfig maxMindDatabaseConfig; - @BeforeEach - void setup() { - maxMindConfig = new MaxMindConfig(); + private MaxMindConfig createObjectUnderTest() { + return new MaxMindConfig(); } - @Test + @RepeatedTest(2) void testDefaultConfig() { + final String dataPrepperDirectory = UUID.randomUUID().toString(); + System.setProperty("data-prepper.dir", dataPrepperDirectory); + final MaxMindConfig maxMindConfig = createObjectUnderTest(); assertThat(maxMindConfig.getDatabaseRefreshInterval(), equalTo(Duration.ofDays(7))); assertThat(maxMindConfig.getCacheSize(), equalTo(4096)); assertThat(maxMindConfig.getAwsAuthenticationOptionsConfig(), equalTo(null)); - assertThat(maxMindConfig.getDatabaseDestination(), equalTo(DEFAULT_DATABASE_DESTINATION)); + assertThat(maxMindConfig.getDatabaseDestination(), equalTo(dataPrepperDirectory + File.separator + "data" + File.separator + "geoip")); assertThat(maxMindConfig.getMaxMindDatabaseConfig(), instanceOf(MaxMindDatabaseConfig.class)); } @Test void testCustomConfig() throws NoSuchFieldException, IllegalAccessException { + final MaxMindConfig maxMindConfig = createObjectUnderTest(); ReflectivelySetField.setField(MaxMindConfig.class, maxMindConfig, "databaseRefreshInterval", Duration.ofDays(10)); ReflectivelySetField.setField(MaxMindConfig.class, maxMindConfig, "cacheSize", 2048); ReflectivelySetField.setField(MaxMindConfig.class, maxMindConfig, "maxMindDatabaseConfig", maxMindDatabaseConfig); @@ -69,6 +72,7 @@ void testCustomConfig() throws NoSuchFieldException, IllegalAccessException { "http://download.maxmind.com/, true, true"}) void testSecureEndpoint(final String databasePath, final boolean insecure, final boolean result) throws NoSuchFieldException, IllegalAccessException, URISyntaxException { + final MaxMindConfig maxMindConfig = createObjectUnderTest(); when(maxMindDatabaseConfig.getDatabasePaths()).thenReturn(Map.of("name", databasePath)); ReflectivelySetField.setField(MaxMindConfig.class, maxMindConfig, "maxMindDatabaseConfig", maxMindDatabaseConfig); ReflectivelySetField.setField(MaxMindConfig.class, maxMindConfig, "insecure", insecure); @@ -83,6 +87,7 @@ void testSecureEndpoint(final String databasePath, final boolean insecure, final "s3://geoip/data, true, true"}) void testValidPaths(final String databasePath, final boolean awsConfig, final boolean result) throws NoSuchFieldException, IllegalAccessException { + final MaxMindConfig maxMindConfig = createObjectUnderTest(); when(maxMindDatabaseConfig.getDatabasePaths()).thenReturn(Map.of("name", databasePath)); ReflectivelySetField.setField(MaxMindConfig.class, maxMindConfig, "maxMindDatabaseConfig", maxMindDatabaseConfig); if (awsConfig) {