Ottomata has submitted this change and it was merged.
Change subject: Fix NPE when Country Geocoding fails to detect country
......................................................................
Fix NPE when Country Geocoding fails to detect country
Change-Id: I36c279ac67dfd20d4024b6bfd0c441302304accc
---
M refinery-core/src/main/java/org/wikimedia/analytics/refinery/core/Geocode.java
M
refinery-hive/src/test/java/org/wikimedia/analytics/refinery/hive/TestGeocodedCountryUDF.java
2 files changed, 22 insertions(+), 1 deletion(-)
Approvals:
Ottomata: Verified; Looks good to me, approved
diff --git
a/refinery-core/src/main/java/org/wikimedia/analytics/refinery/core/Geocode.java
b/refinery-core/src/main/java/org/wikimedia/analytics/refinery/core/Geocode.java
index 6dccf0d..9abf990 100644
---
a/refinery-core/src/main/java/org/wikimedia/analytics/refinery/core/Geocode.java
+++
b/refinery-core/src/main/java/org/wikimedia/analytics/refinery/core/Geocode.java
@@ -82,7 +82,11 @@
InetAddress ipAddress = InetAddress.getByName(ip);
CountryResponse response =
countryDatabaseReader.country(ipAddress);
Country country = response.getCountry();
- return country.getIsoCode();
+ String ret = country.getIsoCode();
+ if (ret == null) {
+ ret = UNKNOWN_COUNTRY_CODE;
+ }
+ return ret;
} catch (UnknownHostException hEx) {
LOG.warn(hEx);
return UNKNOWN_COUNTRY_CODE;
diff --git
a/refinery-hive/src/test/java/org/wikimedia/analytics/refinery/hive/TestGeocodedCountryUDF.java
b/refinery-hive/src/test/java/org/wikimedia/analytics/refinery/hive/TestGeocodedCountryUDF.java
index d9d6ff3..bc0a9ea 100644
---
a/refinery-hive/src/test/java/org/wikimedia/analytics/refinery/hive/TestGeocodedCountryUDF.java
+++
b/refinery-hive/src/test/java/org/wikimedia/analytics/refinery/hive/TestGeocodedCountryUDF.java
@@ -96,4 +96,21 @@
result = (Text)geocodedCountryUDF.evaluate(args);
assertEquals("ISO country code check", "--", result.toString());
}
+
+ @Test
+ public void testIPWithoutIsoCode() throws HiveException {
+ ObjectInspector value1 =
PrimitiveObjectInspectorFactory.javaStringObjectInspector;
+ ObjectInspector[] initArguments = new ObjectInspector[]{value1};
+ GeocodedCountryUDF geocodedCountryUDF = new GeocodedCountryUDF();
+
+ geocodedCountryUDF.initialize(initArguments);
+
+ String ip = "2a02:d500::"; // IP for EU
+ DeferredObject[] args = new DeferredObject[] { new
DeferredJavaObject(ip) };
+ Text result = (Text)geocodedCountryUDF.evaluate(args);
+ assertEquals("ISO country code check", "--", result.toString());
+
+ // Consistency trumps good style. Hence not closing the UDF, as other
+ // UDFs in this test case aren't closed either :-(
+ }
}
--
To view, visit https://gerrit.wikimedia.org/r/188013
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: I36c279ac67dfd20d4024b6bfd0c441302304accc
Gerrit-PatchSet: 3
Gerrit-Project: analytics/refinery/source
Gerrit-Branch: master
Gerrit-Owner: QChris <[email protected]>
Gerrit-Reviewer: Ananthrk <[email protected]>
Gerrit-Reviewer: Ottomata <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits