GeoIpOperation implements EventOperation { @Override public InternalEvent perform(InternalEvent ievent) { String ipStr = null; try { ipStr = ievent.getEventObj().getFieldAsString(this.pathToIpAddress); } catch (FieldNotFoundException e) { if (!this.required) { return ievent; } throw new OperationException("ip address field " + this.pathToIpAddress + " does not exist"); } if (ipStr == null) { if (!this.required) { return ievent; } throw new OperationException("ip address field " + this.pathToIpAddress + " was null"); } if (!ipStr.isEmpty() && ipStr.contains(",")) { ipStr = ipStr.split(",")[0]; } InetAddress ipAddress = null; try { ipAddress = InetAddress.getByName(ipStr); } catch (UnknownHostException e) { if (!this.required) { return ievent; } throw new OperationException(e); } if (ipAddress == null) { if (!this.required) { return ievent; } throw new OperationException("ip address " + ipStr + " did not resolve"); } CityResponse response = null; try { response = this.databaseReader.city(ipAddress); } catch (IOException | GeoIp2Exception e) { if (!this.required) { return ievent; } throw new OperationException(e); } HashMap<String, Object> geo = new HashMap<String, Object>(1); for (GeoProperty property : this.geoProperties) { switch (property) { case COUNTRY_NAME: if (response.getCountry() == null) { if (!this.required) { return ievent; } throw new OperationException("country returned null"); } geo.put("country_name", response.getCountry().getName()); break; case COUNTRY_ISO_CODE: if (response.getCountry() == null) { if (!this.required) { return ievent; } throw new OperationException("country returned null"); } geo.put("country_iso_code", response.getCountry().getIsoCode()); break; case SUBDIVISION_NAME: if (response.getMostSpecificSubdivision() == null) { if (!this.required) { return ievent; } throw new OperationException("MostSpecificSubdivision returned null"); } geo.put("subdivision_name", response.getMostSpecificSubdivision().getName()); break; case SUBDIVISION_ISO_CODE: if (response.getMostSpecificSubdivision() == null) { if (!this.required) { return ievent; } throw new OperationException("MostSpecificSubdivision returned null"); } geo.put("subdivision_iso_code", response.getMostSpecificSubdivision().getIsoCode()); break; case CITY_NAME: if (response.getCity() == null) { if (!this.required) { return ievent; } throw new OperationException("city returned null"); } geo.put("city_name", response.getCity().getName()); break; case POSTAL_CODE: if (response.getPostal() == null) { if (!this.required) { return ievent; } throw new OperationException("postal returned null"); } geo.put("postal_code", response.getPostal().getCode()); break; case LOCATION: if (response.getLocation() == null) { if (!this.required) { return ievent; } throw new OperationException("location returned null"); } Double lat = response.getLocation().getLatitude(); Double lon = response.getLocation().getLongitude(); if (lat == null || lon == null) { if (!this.required) { return ievent; } throw new OperationException("error getting lat/lon"); } HashMap<String, Object> location = new HashMap<String, Object>(2); location.put("lat", lat); location.put("lon", lon); geo.put("location", location); break; } } try { ievent.getEventObj().setField(this.destFieldName, geo); } catch (FieldNotFoundException e) { throw new OperationException(e); } return ievent; } GeoIpOperation(String pathToIpAddress, String destFieldName, DatabaseReader databaseReader,
      List<GeoProperty> geoProperties, boolean required); @Override InternalEvent perform(InternalEvent ievent);  }
@Test public void testUnkownIpPass() throws Throwable { GeoIpOperation op = setup(Arrays.asList(GeoProperty.LOCATION), false); DummpyMapEvent devent = new DummpyMapEvent(); devent.setField("ip_address", "10.0.0.1"); InternalEvent ievent = new InternalEvent("", null, 0); ievent.setEventObj(devent); op.perform(ievent); }
@Test(expected = AddressNotFoundException.class) public void testUnkownIpRequired() throws Throwable { GeoIpOperation op = setup(Arrays.asList(GeoProperty.LOCATION), true); DummpyMapEvent devent = new DummpyMapEvent(); devent.setField("ip_address", "10.0.0.1"); InternalEvent ievent = new InternalEvent("", null, 0); ievent.setEventObj(devent); try { op.perform(ievent); } catch (OperationException e) { throw e.getCause(); } }
@Test public void testInvalidIp() throws Throwable { GeoIpOperation op = setup(Arrays.asList(GeoProperty.LOCATION), false); DummpyMapEvent devent = new DummpyMapEvent(); devent.setField("ip_address", "noanip"); InternalEvent ievent = new InternalEvent("", null, 0); ievent.setEventObj(devent); op.perform(ievent); }
@Test(expected = UnknownHostException.class) public void testInvalidIpRequired() throws Throwable { GeoIpOperation op = setup(Arrays.asList(GeoProperty.LOCATION), true); DummpyMapEvent devent = new DummpyMapEvent(); devent.setField("ip_address", "noanip"); InternalEvent ievent = new InternalEvent("", null, 0); ievent.setEventObj(devent); try { op.perform(ievent); } catch (OperationException e) { throw e.getCause(); } }
@Test(expected = OperationException.class) public void testNullIpRequired() throws Throwable { GeoIpOperation op = setup(Arrays.asList(GeoProperty.LOCATION), true); DummpyMapEvent devent = new DummpyMapEvent(); devent.setField("ip_address", null); InternalEvent ievent = new InternalEvent("", null, 0); ievent.setEventObj(devent); op.perform(ievent); }
@Test(expected = OperationException.class) public void testMissingField() throws Throwable { GeoIpOperation op = setup(Arrays.asList(GeoProperty.LOCATION), true); DummpyMapEvent devent = spy(new DummpyMapEvent()); doThrow(FieldNotFoundException.class).when(devent).getFieldAsString("ip_address"); InternalEvent ievent = new InternalEvent("", null, 0); ievent.setEventObj(devent); op.perform(ievent); }
@Test public void testNullIp() throws Throwable { GeoIpOperation op = setup(Arrays.asList(GeoProperty.LOCATION), false); DummpyMapEvent devent = new DummpyMapEvent(); devent.setField("ip_address", null); InternalEvent ievent = new InternalEvent("", null, 0); ievent.setEventObj(devent); op.perform(ievent); }
@Test public void testKnownIpLocation() throws Throwable { GeoIpOperation op = setup(Arrays.asList(GeoProperty.LOCATION), true); DummpyMapEvent devent = new DummpyMapEvent(); devent.setField("ip_address", "5.5.5.5"); InternalEvent ievent = new InternalEvent("", null, 0); ievent.setEventObj(devent); op.perform(ievent); HashMap<String, Object> expected = new HashMap<String, Object>(); expected.put("ip_address", "5.5.5.5"); HashMap<String, Object> expectedLoc = new HashMap<String, Object>(); expectedLoc.put("lat", new Double("51.75")); expectedLoc.put("lon", new Double("2.25")); Map<String, Object> expectedGeo = new HashMap<String, Object>(); expectedGeo.put("location", expectedLoc); expected.put("geo_ip", expectedGeo); assertEquals(expected, ievent.getEventObj().getPayload()); }
@Test public void testAllKnownFields() throws Throwable { GeoIpOperation op = setup(Arrays.asList(GeoProperty.COUNTRY_NAME, GeoProperty.COUNTRY_ISO_CODE, GeoProperty.SUBDIVISION_NAME, GeoProperty.SUBDIVISION_ISO_CODE, GeoProperty.CITY_NAME, GeoProperty.POSTAL_CODE, GeoProperty.LOCATION), true); DummpyMapEvent devent = new DummpyMapEvent(); devent.setField("ip_address", "5.5.5.5"); InternalEvent ievent = new InternalEvent("", null, 0); ievent.setEventObj(devent); op.perform(ievent); HashMap<String, Object> expected = new HashMap<String, Object>(); expected.put("ip_address", "5.5.5.5"); HashMap<String, Object> expectedLoc = new HashMap<String, Object>(); expectedLoc.put("lat", new Double("51.75")); expectedLoc.put("lon", new Double("2.25")); Map<String, Object> expectedGeo = new HashMap<String, Object>(); expectedGeo.put("location", expectedLoc); expectedGeo.put("country_name", "Eriador"); expectedGeo.put("country_iso_code", "ER"); expectedGeo.put("subdivision_name", "Rivendell"); expectedGeo.put("subdivision_iso_code", "ENG"); expectedGeo.put("city_name", "Rivendell"); expectedGeo.put("postal_code", "1234"); expected.put("geo_ip", expectedGeo); assertEquals(expected, ievent.getEventObj().getPayload()); }
@Test public void testIpList() throws Throwable { GeoIpOperation op = setup(Arrays.asList(GeoProperty.LOCATION), true); DummpyMapEvent devent = new DummpyMapEvent(); devent.setField("ip_address", "5.5.5.5, 10.10.10.10"); InternalEvent ievent = new InternalEvent("", null, 0); ievent.setEventObj(devent); op.perform(ievent); HashMap<String, Object> expected = new HashMap<String, Object>(); expected.put("ip_address", "5.5.5.5, 10.10.10.10"); HashMap<String, Object> expectedLoc = new HashMap<String, Object>(); expectedLoc.put("lat", new Double("51.75")); expectedLoc.put("lon", new Double("2.25")); Map<String, Object> expectedGeo = new HashMap<String, Object>(); expectedGeo.put("location", expectedLoc); expected.put("geo_ip", expectedGeo); assertEquals(expected, ievent.getEventObj().getPayload()); }