Search in sources :

Example 16 with GeoIpCache

use of org.opensearch.ingest.geoip.IngestGeoIpPlugin.GeoIpCache in project OpenSearch by opensearch-project.

the class GeoIpProcessorTests method testListNoMatches.

public void testListNoMatches() throws Exception {
    GeoIpProcessor processor = new GeoIpProcessor(randomAlphaOfLength(10), null, "source_field", loader("/GeoLite2-City.mmdb"), "target_field", EnumSet.allOf(GeoIpProcessor.Property.class), false, new GeoIpCache(1000), false);
    Map<String, Object> document = new HashMap<>();
    document.put("source_field", Arrays.asList("127.0.0.1", "127.0.0.1"));
    IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), document);
    processor.execute(ingestDocument);
    assertFalse(ingestDocument.hasField("target_field"));
}
Also used : HashMap(java.util.HashMap) IngestDocumentMatcher.assertIngestDocument(org.opensearch.ingest.IngestDocumentMatcher.assertIngestDocument) IngestDocument(org.opensearch.ingest.IngestDocument) Matchers.containsString(org.hamcrest.Matchers.containsString) GeoIpCache(org.opensearch.ingest.geoip.IngestGeoIpPlugin.GeoIpCache)

Example 17 with GeoIpCache

use of org.opensearch.ingest.geoip.IngestGeoIpPlugin.GeoIpCache in project OpenSearch by opensearch-project.

the class GeoIpProcessorFactoryTests method testBuildIllegalFieldOption.

public void testBuildIllegalFieldOption() throws Exception {
    GeoIpProcessor.Factory factory = new GeoIpProcessor.Factory(databaseReaders, new GeoIpCache(1000));
    Map<String, Object> config1 = new HashMap<>();
    config1.put("field", "_field");
    config1.put("properties", Collections.singletonList("invalid"));
    Exception e = expectThrows(OpenSearchParseException.class, () -> factory.create(null, null, null, config1));
    assertThat(e.getMessage(), equalTo("[properties] illegal property value [invalid]. valid values are [IP, COUNTRY_ISO_CODE, " + "COUNTRY_NAME, CONTINENT_NAME, REGION_ISO_CODE, REGION_NAME, CITY_NAME, TIMEZONE, LOCATION]"));
    Map<String, Object> config2 = new HashMap<>();
    config2.put("field", "_field");
    config2.put("properties", "invalid");
    e = expectThrows(OpenSearchParseException.class, () -> factory.create(null, null, null, config2));
    assertThat(e.getMessage(), equalTo("[properties] property isn't a list, but of type [java.lang.String]"));
}
Also used : OpenSearchParseException(org.opensearch.OpenSearchParseException) HashMap(java.util.HashMap) Matchers.hasToString(org.hamcrest.Matchers.hasToString) Matchers.containsString(org.hamcrest.Matchers.containsString) GeoIpCache(org.opensearch.ingest.geoip.IngestGeoIpPlugin.GeoIpCache) OpenSearchParseException(org.opensearch.OpenSearchParseException) IOException(java.io.IOException)

Example 18 with GeoIpCache

use of org.opensearch.ingest.geoip.IngestGeoIpPlugin.GeoIpCache in project OpenSearch by opensearch-project.

the class GeoIpProcessorFactoryTests method testCountryBuildDefaults.

public void testCountryBuildDefaults() throws Exception {
    GeoIpProcessor.Factory factory = new GeoIpProcessor.Factory(databaseReaders, new GeoIpCache(1000));
    Map<String, Object> config = new HashMap<>();
    config.put("field", "_field");
    config.put("database_file", "GeoLite2-Country.mmdb");
    String processorTag = randomAlphaOfLength(10);
    GeoIpProcessor processor = factory.create(null, processorTag, null, config);
    assertThat(processor.getTag(), equalTo(processorTag));
    assertThat(processor.getField(), equalTo("_field"));
    assertThat(processor.getTargetField(), equalTo("geoip"));
    assertThat(processor.getDatabaseType(), equalTo("GeoLite2-Country"));
    assertThat(processor.getProperties(), sameInstance(GeoIpProcessor.Factory.DEFAULT_COUNTRY_PROPERTIES));
    assertFalse(processor.isIgnoreMissing());
}
Also used : HashMap(java.util.HashMap) Matchers.hasToString(org.hamcrest.Matchers.hasToString) Matchers.containsString(org.hamcrest.Matchers.containsString) GeoIpCache(org.opensearch.ingest.geoip.IngestGeoIpPlugin.GeoIpCache)

Example 19 with GeoIpCache

use of org.opensearch.ingest.geoip.IngestGeoIpPlugin.GeoIpCache in project OpenSearch by opensearch-project.

the class IngestGeoIpPluginTests method testThrowsFunctionsException.

public void testThrowsFunctionsException() {
    GeoIpCache cache = new GeoIpCache(1);
    IllegalArgumentException ex = expectThrows(IllegalArgumentException.class, () -> cache.putIfAbsent(InetAddresses.forString("127.0.0.1"), AbstractResponse.class, ip -> {
        throw new IllegalArgumentException("bad");
    }));
    assertEquals("bad", ex.getMessage());
}
Also used : InetAddresses(org.opensearch.common.network.InetAddresses) OpenSearchTestCase(org.opensearch.test.OpenSearchTestCase) AbstractResponse(com.maxmind.geoip2.model.AbstractResponse) GeoIpCache(org.opensearch.ingest.geoip.IngestGeoIpPlugin.GeoIpCache) Mockito.mock(org.mockito.Mockito.mock) AbstractResponse(com.maxmind.geoip2.model.AbstractResponse) GeoIpCache(org.opensearch.ingest.geoip.IngestGeoIpPlugin.GeoIpCache)

Example 20 with GeoIpCache

use of org.opensearch.ingest.geoip.IngestGeoIpPlugin.GeoIpCache in project OpenSearch by opensearch-project.

the class IngestGeoIpPluginTests method testCachesAndEvictsResults.

public void testCachesAndEvictsResults() {
    GeoIpCache cache = new GeoIpCache(1);
    AbstractResponse response1 = mock(AbstractResponse.class);
    AbstractResponse response2 = mock(AbstractResponse.class);
    // add a key
    AbstractResponse cachedResponse = cache.putIfAbsent(InetAddresses.forString("127.0.0.1"), AbstractResponse.class, ip -> response1);
    assertSame(cachedResponse, response1);
    assertSame(cachedResponse, cache.putIfAbsent(InetAddresses.forString("127.0.0.1"), AbstractResponse.class, ip -> response1));
    assertSame(cachedResponse, cache.get(InetAddresses.forString("127.0.0.1"), AbstractResponse.class));
    // evict old key by adding another value
    cachedResponse = cache.putIfAbsent(InetAddresses.forString("127.0.0.2"), AbstractResponse.class, ip -> response2);
    assertSame(cachedResponse, response2);
    assertSame(cachedResponse, cache.putIfAbsent(InetAddresses.forString("127.0.0.2"), AbstractResponse.class, ip -> response2));
    assertSame(cachedResponse, cache.get(InetAddresses.forString("127.0.0.2"), AbstractResponse.class));
    assertNotSame(response1, cache.get(InetAddresses.forString("127.0.0.1"), AbstractResponse.class));
}
Also used : InetAddresses(org.opensearch.common.network.InetAddresses) OpenSearchTestCase(org.opensearch.test.OpenSearchTestCase) AbstractResponse(com.maxmind.geoip2.model.AbstractResponse) GeoIpCache(org.opensearch.ingest.geoip.IngestGeoIpPlugin.GeoIpCache) Mockito.mock(org.mockito.Mockito.mock) AbstractResponse(com.maxmind.geoip2.model.AbstractResponse) GeoIpCache(org.opensearch.ingest.geoip.IngestGeoIpPlugin.GeoIpCache)

Aggregations

GeoIpCache (org.opensearch.ingest.geoip.IngestGeoIpPlugin.GeoIpCache)33 HashMap (java.util.HashMap)26 Matchers.containsString (org.hamcrest.Matchers.containsString)26 IngestDocument (org.opensearch.ingest.IngestDocument)19 IngestDocumentMatcher.assertIngestDocument (org.opensearch.ingest.IngestDocumentMatcher.assertIngestDocument)17 Matchers.hasToString (org.hamcrest.Matchers.hasToString)13 Map (java.util.Map)9 IOException (java.io.IOException)7 OpenSearchParseException (org.opensearch.OpenSearchParseException)4 AbstractResponse (com.maxmind.geoip2.model.AbstractResponse)2 Path (java.nio.file.Path)2 List (java.util.List)2 Mockito.mock (org.mockito.Mockito.mock)2 InetAddresses (org.opensearch.common.network.InetAddresses)2 OpenSearchTestCase (org.opensearch.test.OpenSearchTestCase)2 ArrayList (java.util.ArrayList)1