diff --git a/examples/device-detection/web/human-confidence/README.md b/examples/device-detection/web/human-confidence/README.md index 1fbb23ee7..7d4d1b431 100644 --- a/examples/device-detection/web/human-confidence/README.md +++ b/examples/device-detection/web/human-confidence/README.md @@ -111,7 +111,11 @@ passive result as the page does. The production cloud does not serve cloud, with two names that only CI sets. `CLOUD_STAGING_ENDPOINT` names the cloud, the API base including `/api/v4/`, and `RESOURCE_KEY_BESPOKE_STAGING` holds a Bespoke key that cloud resolves. With that key the answer must have -`HumanConfidenceSource` set to `Passive` and a plain level. With a key whose +`HumanConfidenceSource` set to `Passive` and a plain level. The test first +asks the cloud which properties the key includes, and fails, naming the +variable and the property, when `HumanConfidence` is not one of them. It +then asks the cloud for `HumanConfidenceSource` with the key, and fails, +naming the variable, when the answer is not `Passive`. With a key whose products don't offer the source, in `_51DEGREES_RESOURCE_KEY_BASIC`, the cloud must answer the source as null, with the reason, and the example's answer must have no source. diff --git a/examples/device-detection/web/human-confidence/src/test/java/fiftyone/devicedetection/examples/web/HumanConfidenceLiveTest.java b/examples/device-detection/web/human-confidence/src/test/java/fiftyone/devicedetection/examples/web/HumanConfidenceLiveTest.java index 83c1be02c..8aafee0dc 100644 --- a/examples/device-detection/web/human-confidence/src/test/java/fiftyone/devicedetection/examples/web/HumanConfidenceLiveTest.java +++ b/examples/device-detection/web/human-confidence/src/test/java/fiftyone/devicedetection/examples/web/HumanConfidenceLiveTest.java @@ -22,6 +22,8 @@ package fiftyone.devicedetection.examples.web; +import org.json.JSONArray; +import org.json.JSONException; import org.json.JSONObject; import org.junit.After; import org.junit.Test; @@ -34,12 +36,17 @@ import java.net.URL; import java.net.URLEncoder; import java.nio.charset.StandardCharsets; +import java.util.ArrayList; import java.util.Arrays; +import java.util.HashSet; import java.util.List; +import java.util.Locale; +import java.util.Set; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; import static org.junit.Assume.assumeTrue; /** @@ -57,7 +64,12 @@ * test checks, as a person running it would set it in * _51DEGREES_RESOURCE_KEY. A test is skipped when its key or * CLOUD_STAGING_ENDPOINT isn't set, naming the variable, and fails when the - * answer lacks what it checks. Keys are never printed. + * answer lacks what it checks. The Bespoke test first fails, naming the + * variable and the property, when the service's list of the key's + * properties doesn't include HumanConfidence, then when the service, asked + * for HumanConfidenceSource with the key, doesn't answer it as Passive. The + * list isn't asked about the source, as a key can be served the source when + * a request names it without the list holding it. Keys are never printed. */ public class HumanConfidenceLiveTest { @@ -102,17 +114,26 @@ public void stop() { public void bespokeKeysPassiveAnswerCarriesTheSourcePassive() throws IOException { String key = key(BESPOKE_KEY); + requireProperties(BESPOKE_KEY, key, "HumanConfidence"); JSONObject served = servedSource(key); + assertEquals("The staging cloud didn't answer HumanConfidenceSource " + + "as " + PASSIVE + " for the key in " + BESPOKE_KEY + + ": " + redact(served.toString(), key) + + ". Set the variable to a key whose products " + + "include it.", + PASSIVE, served.optString(SOURCE, null)); JSONObject answer = passive(key); String text = redact(answer.toString(), key); assertFalse("The example's passive call failed: " + text, answer.has("error")); - assertEquals("This key's answer carries the source " + PASSIVE - + ": " + text + ". The service says: " + assertEquals("The answer for the key in " + BESPOKE_KEY + + " carries the source " + PASSIVE + ": " + text + + ". The service says: " + redact(served.toString(), key), PASSIVE, answer.optString("source", null)); - assertTrue("A passive level is one of the plain levels: " + text, + assertTrue("The passive level for the key in " + BESPOKE_KEY + + " is one of the plain levels: " + text, LEVELS.contains(answer.optString("humanConfidence", null))); } @@ -137,6 +158,46 @@ public void basicKeysPassiveAnswerCarriesNoSource() throws IOException { answer.has("source")); } + @Test + public void missingPropertiesNamesOnlyThePropertiesNoProductLists() { + JSONObject accessible = new JSONObject( + "{\"Products\":{" + + "\"device\":{\"Properties\":[{\"Name\":\"IsMobile\"}]}," + + "\"derived\":{\"Properties\":[" + + "{\"Name\":\"HumanConfidence\"}]}}}"); + + assertEquals(Arrays.asList("HumanConfidenceSource"), + missingProperties(accessible, + "HumanConfidence", "HumanConfidenceSource")); + assertEquals(Arrays.asList("HumanConfidence"), + missingProperties(new JSONObject("{}"), "HumanConfidence")); + } + + @Test + public void missingPropertiesIgnoresTheCaseOfTheNames() { + JSONObject accessible = new JSONObject( + "{\"Products\":{\"derived\":{\"Properties\":[" + + "{\"Name\":\"humanconfidence\"}]}}}"); + + assertEquals(new ArrayList(), + missingProperties(accessible, "HumanConfidence")); + } + + @Test + public void missingMessageNamesEveryMissingPropertyAndTheVariable() { + assertEquals("The key in " + BESPOKE_KEY + " does not include the " + + "HumanConfidence property. Set the variable to a " + + "key that includes it.", + missingMessage(BESPOKE_KEY, + Arrays.asList("HumanConfidence"))); + assertEquals("The key in " + BESPOKE_KEY + " does not include the " + + "HumanConfidence and HumanConfidenceSource " + + "properties. Set the variable to a key that " + + "includes them.", + missingMessage(BESPOKE_KEY, Arrays.asList( + "HumanConfidence", "HumanConfidenceSource"))); + } + /** The key in the variable name, or a skip that names it. */ private static String key(String name) { String key = System.getenv(name); @@ -146,6 +207,87 @@ private static String key(String name) { return key.trim(); } + /** + * Fails, naming the variable and every property it lacks, when the + * service's list of the properties the key can read lacks any of them. + */ + private static void requireProperties(String name, String key, + String... properties) throws IOException { + HttpURLConnection connection = (HttpURLConnection) + new URL(api() + "accessibleproperties?resource=" + key) + .openConnection(); + connection.setConnectTimeout(TIMEOUT_MS); + connection.setReadTimeout(TIMEOUT_MS); + int status = connection.getResponseCode(); + String body = read(status >= 400 + ? connection.getErrorStream() : connection.getInputStream()); + assertEquals("The service didn't list the properties of the key in " + + name + ": " + redact(body, key), 200, status); + JSONObject accessible; + try { + accessible = new JSONObject(body); + } catch (JSONException e) { + fail("The service's property list for the key in " + name + + " isn't JSON: " + redact(body, key)); + return; + } + List missing = missingProperties(accessible, properties); + if (missing.isEmpty() == false) { + fail(missingMessage(name, missing)); + } + } + + /** + * The failure for a key whose list lacks the properties, naming the + * variable and each property. + */ + static String missingMessage(String name, List missing) { + boolean one = missing.size() == 1; + StringBuilder names = new StringBuilder(); + for (int i = 0; i < missing.size(); i++) { + if (i > 0) { + names.append(i == missing.size() - 1 + ? (missing.size() > 2 ? ", and " : " and ") : ", "); + } + names.append(missing.get(i)); + } + return "The key in " + name + " does not include the " + names + + (one ? " property" : " properties") + + ". Set the variable to a key that includes " + + (one ? "it." : "them."); + } + + /** + * The properties, of those given, that no product in the service's list + * of accessible properties names, ignoring case. + */ + static List missingProperties(JSONObject accessible, + String... properties) { + Set names = new HashSet<>(); + JSONObject products = accessible.optJSONObject("Products"); + if (products != null) { + for (String product : products.keySet()) { + JSONObject entry = products.optJSONObject(product); + JSONArray list = entry == null + ? null : entry.optJSONArray("Properties"); + for (int i = 0; list != null && i < list.length(); i++) { + JSONObject property = list.optJSONObject(i); + if (property != null) { + names.add(property.optString("Name") + .toLowerCase(Locale.ROOT)); + } + } + } + } + List missing = new ArrayList<>(); + for (String property : properties) { + if (names.contains(property.toLowerCase(Locale.ROOT)) == false) { + missing.add(property); + } + } + return missing; + } + /** Starts the example with the key and asks it for /passive. */ private JSONObject passive(String key) throws IOException { example = new HumanConfidence(api(), key, null);