public void testCity() throws Exception {
    InputStream database = getDatabaseFileInputStream("/GeoLite2-City.mmdb.gz");
    GeoIpProcessor processor =
        new GeoIpProcessor(
            randomAsciiOfLength(10),
            "source_field",
            new DatabaseReader.Builder(database).build(),
            "target_field",
            EnumSet.allOf(GeoIpProcessor.Property.class));

    Map<String, Object> document = new HashMap<>();
    document.put("source_field", "8.8.8.8");
    IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), document);
    processor.execute(ingestDocument);

    assertThat(ingestDocument.getSourceAndMetadata().get("source_field"), equalTo("8.8.8.8"));
    @SuppressWarnings("unchecked")
    Map<String, Object> geoData =
        (Map<String, Object>) ingestDocument.getSourceAndMetadata().get("target_field");
    assertThat(geoData.size(), equalTo(8));
    assertThat(geoData.get("ip"), equalTo("8.8.8.8"));
    assertThat(geoData.get("country_iso_code"), equalTo("US"));
    assertThat(geoData.get("country_name"), equalTo("United States"));
    assertThat(geoData.get("continent_name"), equalTo("North America"));
    assertThat(geoData.get("region_name"), equalTo("California"));
    assertThat(geoData.get("city_name"), equalTo("Mountain View"));
    assertThat(geoData.get("timezone"), equalTo("America/Los_Angeles"));
    Map<String, Object> location = new HashMap<>();
    location.put("lat", 37.386d);
    location.put("lon", -122.0838d);
    assertThat(geoData.get("location"), equalTo(location));
  }
  public void testCountry() throws Exception {
    InputStream database = getDatabaseFileInputStream("/GeoLite2-Country.mmdb.gz");
    GeoIpProcessor processor =
        new GeoIpProcessor(
            randomAsciiOfLength(10),
            "source_field",
            new DatabaseReader.Builder(database).build(),
            "target_field",
            EnumSet.allOf(GeoIpProcessor.Property.class));

    Map<String, Object> document = new HashMap<>();
    document.put("source_field", "82.170.213.79");
    IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), document);
    processor.execute(ingestDocument);

    assertThat(ingestDocument.getSourceAndMetadata().get("source_field"), equalTo("82.170.213.79"));
    @SuppressWarnings("unchecked")
    Map<String, Object> geoData =
        (Map<String, Object>) ingestDocument.getSourceAndMetadata().get("target_field");
    assertThat(geoData.size(), equalTo(4));
    assertThat(geoData.get("ip"), equalTo("82.170.213.79"));
    assertThat(geoData.get("country_iso_code"), equalTo("NL"));
    assertThat(geoData.get("country_name"), equalTo("Netherlands"));
    assertThat(geoData.get("continent_name"), equalTo("Europe"));
  }
  public void testAddressIsNotInTheDatabase() throws Exception {
    InputStream database = getDatabaseFileInputStream("/GeoLite2-City.mmdb.gz");
    GeoIpProcessor processor =
        new GeoIpProcessor(
            randomAsciiOfLength(10),
            "source_field",
            new DatabaseReader.Builder(database).build(),
            "target_field",
            EnumSet.allOf(GeoIpProcessor.Property.class));

    Map<String, Object> document = new HashMap<>();
    document.put("source_field", "127.0.0.1");
    IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), document);
    processor.execute(ingestDocument);
    @SuppressWarnings("unchecked")
    Map<String, Object> geoData =
        (Map<String, Object>) ingestDocument.getSourceAndMetadata().get("target_field");
    assertThat(geoData.size(), equalTo(0));
  }
  /** Don't silently do DNS lookups or anything trappy on bogus data */
  public void testInvalid() throws Exception {
    InputStream database = getDatabaseFileInputStream("/GeoLite2-City.mmdb.gz");
    GeoIpProcessor processor =
        new GeoIpProcessor(
            randomAsciiOfLength(10),
            "source_field",
            new DatabaseReader.Builder(database).build(),
            "target_field",
            EnumSet.allOf(GeoIpProcessor.Property.class));

    Map<String, Object> document = new HashMap<>();
    document.put("source_field", "www.google.com");
    IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), document);
    try {
      processor.execute(ingestDocument);
      fail("did not get expected exception");
    } catch (IllegalArgumentException expected) {
      assertNotNull(expected.getMessage());
      assertThat(expected.getMessage(), containsString("not an IP string literal"));
    }
  }