diff --git a/device-detection/device-detection.cloud/src/main/java/fiftyone/devicedetection/cloud/flowelements/DeviceDetectionCloudEngine.java b/device-detection/device-detection.cloud/src/main/java/fiftyone/devicedetection/cloud/flowelements/DeviceDetectionCloudEngine.java index 400ca1dad..128e4ebc2 100644 --- a/device-detection/device-detection.cloud/src/main/java/fiftyone/devicedetection/cloud/flowelements/DeviceDetectionCloudEngine.java +++ b/device-detection/device-detection.cloud/src/main/java/fiftyone/devicedetection/cloud/flowelements/DeviceDetectionCloudEngine.java @@ -43,6 +43,7 @@ import java.util.ArrayList; import java.util.List; +import java.util.Locale; import java.util.Map; import java.util.Objects; import java.util.TreeMap; @@ -146,6 +147,12 @@ protected void processEngine(FlowData data, DeviceDataCloud aspectData) { new TreeMap<>(String.CASE_INSENSITIVE_ORDER); for (AspectPropertyMetaData property : getProperties()) { + // A property the response leaves out has no entry, so + // reading it throws a PropertyMissingException. + if (deviceObj.has( + property.getName().toLowerCase(Locale.ROOT)) == false) { + continue; + } String type = property.getType().getSimpleName(); switch(type) { case ("List"): @@ -389,6 +396,12 @@ private AspectPropertyValue getStringAspectPropertyValue( return stringValue; } + /** + * The message for a property with no value when the response gives no + * reason. + */ + private static final String UNKNOWN_NULL_REASON = "Unknown"; + /** * Get the reason for the value of a property not being present in the cloud * engine's JSON response. @@ -398,7 +411,9 @@ private AspectPropertyValue getStringAspectPropertyValue( */ private String getNoValueReason(JSONObject deviceObj, String key){ Object reason = tryToGet(deviceObj, key + "nullreason"); - return reason == null ? null : reason.toString(); + return reason == null || reason == JSONObject.NULL ? + UNKNOWN_NULL_REASON : + reason.toString(); } /** diff --git a/device-detection/device-detection.cloud/src/test/java/fiftyone/devicedetection/cloud/flowelements/ProductMissingTests.java b/device-detection/device-detection.cloud/src/test/java/fiftyone/devicedetection/cloud/flowelements/ProductMissingTests.java index 2cd708eb6..163f9b00a 100644 --- a/device-detection/device-detection.cloud/src/test/java/fiftyone/devicedetection/cloud/flowelements/ProductMissingTests.java +++ b/device-detection/device-detection.cloud/src/test/java/fiftyone/devicedetection/cloud/flowelements/ProductMissingTests.java @@ -99,19 +99,64 @@ public void ProductMissing_ProcessLogsAndThrowsNothing() throws Exception { /** * With the Device Detection product included, a response that holds no - * part for it leaves the element data without values and throws - * nothing, as the Processing section of the cloud aspect engine - * specification says. + * part for it raises nothing while processing, and reading a property + * throws PropertyMissingException, as for any property the response + * leaves out. */ @Test - public void ProductPresent_ResponseWithoutPart_NoValues() + public void ProductPresent_ResponseWithoutPart_PropertyIsMissing() throws Exception { - try (DeviceDetectionCloudEngine engine = buildEngine("device", "hardwarevendor")) { + try (DeviceDetectionCloudEngine engine = + buildEngine("device", "hardwarevendor")) { DeviceDataCloud data = process( engine, "{ 'ip': { 'registeredname': 'Example' }, " + "'javascriptProperties': [] }"); + try { + data.getHardwareVendor(); + fail("A PropertyMissingException should have been thrown"); + } catch (PropertyMissingException e) { + // The response held nothing for the product. + } + } + } + + /** + * A property the response's part leaves out has no entry, so reading it + * throws PropertyMissingException rather than giving no value. + */ + @Test + public void ProductPresent_PropertyLeftOut_PropertyIsMissing() + throws Exception { + try (DeviceDetectionCloudEngine engine = + buildEngine("device", "hardwarevendor")) { + DeviceDataCloud data = process( + engine, + "{ 'device': { 'another': 'Example' }, " + + "'javascriptProperties': [] }"); + try { + data.getHardwareVendor(); + fail("A PropertyMissingException should have been thrown"); + } catch (PropertyMissingException e) { + // The response's part did not hold the property. + } + } + } + + /** + * A null with no reason beside it is no value with the message Unknown. + */ + @Test + public void NullWithoutReason_MessageIsUnknown() throws Exception { + try (DeviceDetectionCloudEngine engine = + buildEngine("device", "hardwarevendor")) { + DeviceDataCloud data = process( + engine, + "{ 'device': { 'hardwarevendor': null }, " + + "'javascriptProperties': [] }"); assertFalse(data.getHardwareVendor().hasValue()); + assertEquals("Unknown", + data.getHardwareVendor().getNoValueMessage()); } } diff --git a/ip-intelligence/ip-intelligence.cloud/src/main/java/fiftyone/ipintelligence/cloud/flowelements/IPIntelligenceCloudEngine.java b/ip-intelligence/ip-intelligence.cloud/src/main/java/fiftyone/ipintelligence/cloud/flowelements/IPIntelligenceCloudEngine.java index 99b84cbb3..0b0051a0b 100644 --- a/ip-intelligence/ip-intelligence.cloud/src/main/java/fiftyone/ipintelligence/cloud/flowelements/IPIntelligenceCloudEngine.java +++ b/ip-intelligence/ip-intelligence.cloud/src/main/java/fiftyone/ipintelligence/cloud/flowelements/IPIntelligenceCloudEngine.java @@ -49,6 +49,7 @@ import java.util.ArrayList; import java.util.HashSet; import java.util.List; +import java.util.Locale; import java.util.Map; import java.util.Objects; import java.util.Set; @@ -158,6 +159,12 @@ protected void processEngine(FlowData data, IPIntelligenceDataCloud aspectData) new TreeMap<>(String.CASE_INSENSITIVE_ORDER); for (AspectPropertyMetaData property : getProperties()) { + // A property the response leaves out has no entry, so + // reading it throws a PropertyMissingException. + if (ipiObj.has( + property.getName().toLowerCase(Locale.ROOT)) == false) { + continue; + } if (weightedPropertyNames.contains( property.getName().toLowerCase())) { propertyMap.put( @@ -590,6 +597,12 @@ private AspectPropertyValue getStringAspectPropertyValue( return stringValue; } + /** + * The message for a property with no value when the response gives no + * reason. + */ + private static final String UNKNOWN_NULL_REASON = "Unknown"; + /** * Get the reason for the value of a property not being present in the cloud * engine's JSON response. @@ -599,7 +612,9 @@ private AspectPropertyValue getStringAspectPropertyValue( */ private String getNoValueReason(JSONObject ipiObj, String key){ Object reason = tryToGet(ipiObj, key + "nullreason"); - return reason == null ? null : reason.toString(); + return reason == null || reason == JSONObject.NULL ? + UNKNOWN_NULL_REASON : + reason.toString(); } /** diff --git a/ip-intelligence/ip-intelligence.cloud/src/test/java/fiftyone/ipintelligence/cloud/flowelements/ProductMissingTests.java b/ip-intelligence/ip-intelligence.cloud/src/test/java/fiftyone/ipintelligence/cloud/flowelements/ProductMissingTests.java index a597f4662..1c0d68e37 100644 --- a/ip-intelligence/ip-intelligence.cloud/src/test/java/fiftyone/ipintelligence/cloud/flowelements/ProductMissingTests.java +++ b/ip-intelligence/ip-intelligence.cloud/src/test/java/fiftyone/ipintelligence/cloud/flowelements/ProductMissingTests.java @@ -99,19 +99,64 @@ public void ProductMissing_ProcessLogsAndThrowsNothing() throws Exception { /** * With the IP Intelligence product included, a response that holds no - * part for it leaves the element data without values and throws - * nothing, as the Processing section of the cloud aspect engine - * specification says. + * part for it raises nothing while processing, and reading a property + * throws PropertyMissingException, as for any property the response + * leaves out. */ @Test - public void ProductPresent_ResponseWithoutPart_NoValues() + public void ProductPresent_ResponseWithoutPart_PropertyIsMissing() throws Exception { - try (IPIntelligenceCloudEngine engine = buildEngine("ip", "registeredname")) { + try (IPIntelligenceCloudEngine engine = + buildEngine("ip", "registeredname")) { IPIntelligenceDataCloud data = process( engine, "{ 'device': { 'hardwarevendor': 'Example' }, " + "'javascriptProperties': [] }"); + try { + data.getRegisteredName(); + fail("A PropertyMissingException should have been thrown"); + } catch (PropertyMissingException e) { + // The response held nothing for the product. + } + } + } + + /** + * A property the response's part leaves out has no entry, so reading it + * throws PropertyMissingException rather than giving no value. + */ + @Test + public void ProductPresent_PropertyLeftOut_PropertyIsMissing() + throws Exception { + try (IPIntelligenceCloudEngine engine = + buildEngine("ip", "registeredname")) { + IPIntelligenceDataCloud data = process( + engine, + "{ 'ip': { 'another': 'Example' }, " + + "'javascriptProperties': [] }"); + try { + data.getRegisteredName(); + fail("A PropertyMissingException should have been thrown"); + } catch (PropertyMissingException e) { + // The response's part did not hold the property. + } + } + } + + /** + * A null with no reason beside it is no value with the message Unknown. + */ + @Test + public void NullWithoutReason_MessageIsUnknown() throws Exception { + try (IPIntelligenceCloudEngine engine = + buildEngine("ip", "registeredname")) { + IPIntelligenceDataCloud data = process( + engine, + "{ 'ip': { 'registeredname': null }, " + + "'javascriptProperties': [] }"); assertFalse(data.getRegisteredName().hasValue()); + assertEquals("Unknown", + data.getRegisteredName().getNoValueMessage()); } }