From 4e949f53ecb87516db61c1bd54f02ce377e1b089 Mon Sep 17 00:00:00 2001 From: James Rosewell Date: Mon, 23 Nov 2026 19:21:31 +0000 Subject: [PATCH 1/2] FIX: Say when the resource key lacks the hardware lookup in the TAC and native model examples With a resource key that does not include HardwarePlatform.Profiles, the TAC and native model examples printed their introduction and the lookup headings with no devices under them, so the key looked fine and the lookup looked empty. After the introduction each example now reads the hardware engine's properties, which come from the cloud's accessible properties for the key. When Profiles is not among them, it prints one line saying the key does not include HardwarePlatform.Profiles, with tagged links to the configuration page for the key and to the contact page, makes no lookup, prints its Find out more section and exits with code 0. When the accessible properties could not be fetched, nothing is known to be missing, so the lookups run and report the failure as before. A device is now printed as " ()", the form the other languages use, and a lookup with no devices prints one line saying so rather than nothing. The native model values are now package-private, as the TAC values already are, so the tests can name them. --- .../examples/console/NativeModelCloud.java | 61 +++++++++++++++++-- .../examples/console/TacCloud.java | 60 ++++++++++++++++-- 2 files changed, 113 insertions(+), 8 deletions(-) diff --git a/examples/device-detection/console/src/main/java/fiftyone/devicedetection/examples/console/NativeModelCloud.java b/examples/device-detection/console/src/main/java/fiftyone/devicedetection/examples/console/NativeModelCloud.java index af8ad3775..c2b585df6 100644 --- a/examples/device-detection/console/src/main/java/fiftyone/devicedetection/examples/console/NativeModelCloud.java +++ b/examples/device-detection/console/src/main/java/fiftyone/devicedetection/examples/console/NativeModelCloud.java @@ -25,13 +25,16 @@ import fiftyone.devicedetection.examples.shared.FindOutMore; import fiftyone.devicedetection.examples.shared.KeyHelper; import fiftyone.devicedetection.cloud.data.MultiDeviceDataCloud; +import fiftyone.devicedetection.cloud.flowelements.HardwareProfileCloudEngine; import fiftyone.devicedetection.cloud.flowelements.HardwareProfileCloudEngineBuilder; import fiftyone.devicedetection.shared.Constants; import fiftyone.devicedetection.shared.DeviceData; +import fiftyone.pipeline.cloudrequestengine.flowelements.CloudRequestEngine; import fiftyone.pipeline.cloudrequestengine.flowelements.CloudRequestEngineBuilder; import fiftyone.pipeline.core.data.FlowData; import fiftyone.pipeline.core.flowelements.Pipeline; import fiftyone.pipeline.core.flowelements.PipelineBuilder; +import fiftyone.pipeline.engines.data.AspectPropertyMetaData; import org.slf4j.LoggerFactory; import java.io.OutputStream; @@ -49,9 +52,19 @@ public class NativeModelCloud { private static final String EXAMPLE_TAG = "console-src-main-java-fiftyone-devicedetection-examples-console-nativemodelcloud.java"; + /** + * The hardware engine's property that holds the matching devices. + */ + private static final String PROFILES = "Profiles"; + + /** + * The tag of the links given when the resource key lacks the lookup. + */ + private static final String NO_HARDWARE_LOOKUP = "no-hardware-lookup"; + // Example values to use when looking up device details from native model names. - private static final String NATIVE_MODEL_1 = "SC-03L"; - private static final String NATIVE_MODEL_2 = "iPhone11,8"; + static final String NATIVE_MODEL_1 = "SC-03L"; + static final String NATIVE_MODEL_2 = "iPhone11,8"; public static final String NATIVE_MODEL_EXAMPLE_RESOURCE_KEY_NAME = KeyHelper.RESOURCE_KEY_VARIABLE; public static void main (String[] args) throws Exception { @@ -103,10 +116,24 @@ public static void run(String resourceKey, OutputStream os) throws Exception { // we are not closing them ourselves .setAutoCloseElements(true) .build()) { - // Pass a native model into the pipeline and list the matching devices. + if (lacksHardwareLookup(pipeline)) { + output.println("The resource key does not include " + + "HardwarePlatform.Profiles, the hardware lookup " + + "this example needs, so no devices are listed. " + + "Create a resource key that includes it at " + + FindOutMore.link( + KeyHelper.PAID_PROPERTIES_CONFIGURATION, + EXAMPLE_TAG, NO_HARDWARE_LOOKUP) + + ", or contact us about it at " + + FindOutMore.link("contact-us", EXAMPLE_TAG, + NO_HARDWARE_LOOKUP) + "."); + } else { + // Pass a native model into the pipeline and list the + // matching devices. analyseNativeModel(NATIVE_MODEL_1, pipeline, output); // Repeat for an alternative native model name. analyseNativeModel(NATIVE_MODEL_2, pipeline, output); + } } // The writer closes the stream it was given, so the section is // written before it. @@ -130,15 +157,41 @@ static void analyseNativeModel(String nativemodel, Pipeline pipeline, PrintWrite // detection, so we have access to all the same properties. output.format("Which devices are associated with the " + "native model name '%s'?%n", nativemodel); + if (result.getProfiles().isEmpty()) { + output.println("\tNo devices are associated with this " + + "native model name."); + } for (DeviceData device : result.getProfiles()) { // output the result, taking care to deal with "no value" cases - output.format("\t%s; %s; %s%n", asString(device.getHardwareVendor()), + output.format("\t%s %s (%s)%n", asString(device.getHardwareVendor()), asString(device.getHardwareName()), asString(device.getHardwareModel())); } } } + + /** + * Whether the resource key lacks the hardware lookup. The hardware + * engine's properties come from the cloud's accessible properties for + * the key, and hold Profiles only when the key includes the lookup. + * When the accessible properties could not be fetched nothing is known + * to be missing, so the lookups run and report what went wrong. + */ + static boolean lacksHardwareLookup(Pipeline pipeline) { + if (pipeline.getElement(CloudRequestEngine.class) + .getPublicProperties() == null) { + return false; + } + for (AspectPropertyMetaData property : pipeline + .getElement(HardwareProfileCloudEngine.class) + .getProperties()) { + if (property.getName().equalsIgnoreCase(PROFILES)) { + return false; + } + } + return true; + } } /*! * @example console/NativeModelCloud.java diff --git a/examples/device-detection/console/src/main/java/fiftyone/devicedetection/examples/console/TacCloud.java b/examples/device-detection/console/src/main/java/fiftyone/devicedetection/examples/console/TacCloud.java index ab1118a84..c0e253136 100644 --- a/examples/device-detection/console/src/main/java/fiftyone/devicedetection/examples/console/TacCloud.java +++ b/examples/device-detection/console/src/main/java/fiftyone/devicedetection/examples/console/TacCloud.java @@ -25,12 +25,15 @@ import fiftyone.devicedetection.examples.shared.FindOutMore; import fiftyone.devicedetection.examples.shared.KeyHelper; import fiftyone.devicedetection.cloud.data.MultiDeviceDataCloud; +import fiftyone.devicedetection.cloud.flowelements.HardwareProfileCloudEngine; import fiftyone.devicedetection.shared.Constants; import fiftyone.devicedetection.shared.DeviceData; +import fiftyone.pipeline.cloudrequestengine.flowelements.CloudRequestEngine; import fiftyone.pipeline.core.configuration.PipelineOptions; import fiftyone.pipeline.core.configuration.PipelineOptionsFactory; import fiftyone.pipeline.core.data.FlowData; import fiftyone.pipeline.core.flowelements.Pipeline; +import fiftyone.pipeline.engines.data.AspectPropertyMetaData; import fiftyone.pipeline.engines.data.AspectPropertyValue; import fiftyone.pipeline.engines.fiftyone.flowelements.FiftyOnePipelineBuilder; @@ -63,6 +66,16 @@ public class TacCloud { private static final String EXAMPLE_TAG = "console-src-main-java-fiftyone-devicedetection-examples-console-taccloud.java"; + /** + * The hardware engine's property that holds the matching devices. + */ + private static final String PROFILES = "Profiles"; + + /** + * The tag of the links given when the resource key lacks the lookup. + */ + private static final String NO_HARDWARE_LOOKUP = "no-hardware-lookup"; + // Example values to use when looking up device details from TACs. static final String TAC_1 = "35925406"; static final String TAC_2 = "86386802"; @@ -117,9 +130,23 @@ public static void run(String resourceKey, OutputStream os) throws Exception { // Create the pipeline using the service provider and the configured options. try (Pipeline pipeline = new FiftyOnePipelineBuilder() .buildFromConfiguration(pipelineOptions)) { - // Pass a TAC into the pipeline and list the matching devices. - analyseTac(TAC_1, pipeline, writer); - analyseTac(TAC_2, pipeline, writer); + if (lacksHardwareLookup(pipeline)) { + writer.println("The resource key does not include " + + "HardwarePlatform.Profiles, the hardware lookup " + + "this example needs, so no devices are listed. " + + "Create a resource key that includes it at " + + FindOutMore.link( + KeyHelper.PAID_PROPERTIES_CONFIGURATION, + EXAMPLE_TAG, NO_HARDWARE_LOOKUP) + + ", or contact us about it at " + + FindOutMore.link("contact-us", EXAMPLE_TAG, + NO_HARDWARE_LOOKUP) + "."); + } else { + // Pass a TAC into the pipeline and list the matching + // devices. + analyseTac(TAC_1, pipeline, writer); + analyseTac(TAC_2, pipeline, writer); + } } // The writer closes the stream it was given, so the section is // written before it. @@ -141,15 +168,40 @@ static void analyseTac(String tac, Pipeline pipeline, PrintWriter output) throws // implementing 'DeviceData' which is the same interface used for standard device // detection, so we have access to all the same properties. output.format("Which devices are associated with the TAC '%s'?%n", tac); + if (result.getProfiles().isEmpty()) { + output.println("\tNo devices are associated with this TAC."); + } for (DeviceData device : result.getProfiles()) { AspectPropertyValue vendor = device.getHardwareVendor(); AspectPropertyValue> name = device.getHardwareName(); AspectPropertyValue model = device.getHardwareModel(); // output the result, taking care to deal with "no value" cases - output.format("\t%s; %s; %s%n", asString(vendor), asString(name), asString(model)); + output.format("\t%s %s (%s)%n", asString(vendor), asString(name), asString(model)); + } + } + } + + /** + * Whether the resource key lacks the hardware lookup. The hardware + * engine's properties come from the cloud's accessible properties for + * the key, and hold Profiles only when the key includes the lookup. + * When the accessible properties could not be fetched nothing is known + * to be missing, so the lookups run and report what went wrong. + */ + static boolean lacksHardwareLookup(Pipeline pipeline) { + if (pipeline.getElement(CloudRequestEngine.class) + .getPublicProperties() == null) { + return false; + } + for (AspectPropertyMetaData property : pipeline + .getElement(HardwareProfileCloudEngine.class) + .getProperties()) { + if (property.getName().equalsIgnoreCase(PROFILES)) { + return false; } } + return true; } } /*! From c4eca2998e0c9974870f2accdb4b7a07fe0ad7f3 Mon Sep 17 00:00:00 2001 From: James Rosewell Date: Mon, 23 Nov 2026 19:21:43 +0000 Subject: [PATCH 2/2] TEST: Fail the TAC and native model tests when no device is listed TacCloudTest and NativeModelCloudTest only checked that the example ended with exit code 0 and printed its introduction, so a resource key without the hardware lookup passed them while the examples listed no devices. Each test now fails when the example says the key in _51DEGREES_RESOURCE_KEY_BESPOKE does not include HardwarePlatform.Profiles, naming the lookup that needs it. Otherwise it requires, for each value the example looks up, at least one device line under that value's heading whose vendor and model both have a value, and then exit code 0. The example runs in its own process, which takes _51DEGREES_CLOUD_ENDPOINT from the test's environment, so the tests can be pointed at another cloud. With no _51DEGREES_RESOURCE_KEY_BESPOKE the tests are skipped, as before. HardwareLookupWithoutProfilesTest runs both examples against a stand-in for the cloud service on this machine, which gives the accessible properties the cloud gives a key without the hardware element, the evidence keys the cloud accepts, and 404 for anything else. It requires the one line with both tagged links, no lookup heading, no lookup request and exit code 0, with no real key and no network. HardwareLookupOutputTest checks which lines count as a device. AGENTS.md says what the examples print with and without the property. --- AGENTS.md | 12 ++ .../console/HardwareLookupOutput.java | 139 +++++++++++++ .../console/HardwareLookupOutputTest.java | 65 ++++++ .../HardwareLookupWithoutProfilesTest.java | 193 ++++++++++++++++++ .../console/NativeModelCloudTest.java | 18 +- .../examples/console/TacCloudTest.java | 17 +- .../accessibleproperties-no-hardware.json | 15 ++ .../examples/console/evidencekeys.json | 142 +++++++++++++ 8 files changed, 581 insertions(+), 20 deletions(-) create mode 100644 examples/device-detection/console/src/test/java/fiftyone/devicedetection/examples/console/HardwareLookupOutput.java create mode 100644 examples/device-detection/console/src/test/java/fiftyone/devicedetection/examples/console/HardwareLookupOutputTest.java create mode 100644 examples/device-detection/console/src/test/java/fiftyone/devicedetection/examples/console/HardwareLookupWithoutProfilesTest.java create mode 100644 examples/device-detection/console/src/test/resources/fiftyone/devicedetection/examples/console/accessibleproperties-no-hardware.json create mode 100644 examples/device-detection/console/src/test/resources/fiftyone/devicedetection/examples/console/evidencekeys.json diff --git a/AGENTS.md b/AGENTS.md index 9db6644c4..4d740e998 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -199,6 +199,18 @@ key, names the variable to set, gives the page where a key is created, ends with its "Find out more" section and exits with code 0. The tests named `WithoutKey` run each one that way. +The TAC and native model examples need a key that includes +HardwarePlatform.Profiles. With one, they list each device as +` ()` under the value looked up. With a key that does +not include it, they say so in one line that links to where a key is created +and to the contact page, make no lookup, end with their "Find out more" +section and exit with code 0. The published packages up to 4.5.130 cannot +build the pipeline for such a key, so with those the examples end with that +error instead. `TacCloudTest` and `NativeModelCloudTest` fail when the key in +`_51DEGREES_RESOURCE_KEY_BESPOKE` lacks the property, and +`HardwareLookupWithoutProfilesTest` checks the message against a stand-in for +the cloud service. + ### Pitfalls - **A data file that is not there.** `cxx/data` holds a `.link` for each file diff --git a/examples/device-detection/console/src/test/java/fiftyone/devicedetection/examples/console/HardwareLookupOutput.java b/examples/device-detection/console/src/test/java/fiftyone/devicedetection/examples/console/HardwareLookupOutput.java new file mode 100644 index 000000000..b0deb4aea --- /dev/null +++ b/examples/device-detection/console/src/test/java/fiftyone/devicedetection/examples/console/HardwareLookupOutput.java @@ -0,0 +1,139 @@ +/* ********************************************************************* + * This Original Work is copyright of 51 Degrees Mobile Experts Limited. + * Copyright 2026 51 Degrees Mobile Experts Limited, Davidson House, + * Forbury Square, Reading, Berkshire, United Kingdom RG1 3EU. + * + * This Original Work is licensed under the European Union Public Licence + * (EUPL) v.1.2 and is subject to its terms as set out below. + * + * If a copy of the EUPL was not distributed with this file, You can obtain + * one at https://opensource.org/licenses/EUPL-1.2. + * + * The 'Compatible Licences' set out in the Appendix to the EUPL (as may be + * amended by the European Commission) shall be deemed incompatible for + * the purposes of the Work and the provisions of the compatibility + * clause in Article 5 of the EUPL shall not apply. + * + * If using the Work as, or as part of, a network application, by + * including the attribution notice(s) required under Article 5 of the EUPL + * in the end user terms of the application under an appropriate heading, + * such notice(s) shall fulfill the requirements of that article. + * ********************************************************************* */ + +package fiftyone.devicedetection.examples.console; + +import fiftyone.devicedetection.examples.shared.ExampleProcess; +import fiftyone.devicedetection.examples.shared.KeyHelper; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; + +/** + * Checks what the TAC and native model examples print for the values they + * look up, being a heading for each value with a line for each device under + * it, indented by a tab, in the form {@code ()}. + */ +final class HardwareLookupOutput { + + /** + * What the examples print when the resource key lacks the lookup. + */ + static final String NOT_INCLUDED = + "does not include HardwarePlatform.Profiles"; + + /** + * How the examples render a property with no value. + */ + private static final String NO_VALUE = "Unknown"; + + private HardwareLookupOutput() { + } + + /** + * Fails unless the example listed at least one device with a vendor and + * a model for each value, and then ended with exit code 0. + * @param example the example that ran + * @param result what the example wrote and how it ended + * @param lookup the lookup the example makes, such as "TAC lookup" + * @param subject what each value is, such as "the TAC" + * @param values the values the example looks up + */ + static void requireDevices(Class example, + ExampleProcess.Result result, + String lookup, + String subject, + String... values) { + String output = result.output; + if (output.contains(NOT_INCLUDED)) { + fail("The resource key in " + + KeyHelper.BESPOKE_RESOURCE_KEY_VARIABLE + + " does not include HardwarePlatform.Profiles, which the " + + lookup + " needs.\n" + output); + } + for (String value : values) { + String heading = "Which devices are associated with " + + subject + " '" + value + "'?"; + if (listsDevice(output, heading) == false) { + fail("No device with a vendor and a model was listed " + + "for " + subject + " '" + value + "'.\n" + output); + } + } + assertEquals(example.getSimpleName() + + " should end with exit code 0.\n" + output, + 0, result.exitCode); + } + + /** + * Whether a tab indented line under the heading is a device with a + * vendor and a model. + */ + private static boolean listsDevice(String output, String heading) { + String[] lines = output.split("\r?\n"); + for (int i = 0; i < lines.length; i++) { + if (lines[i].equals(heading)) { + for (int j = i + 1; + j < lines.length && lines[j].startsWith("\t"); j++) { + if (isDevice(lines[j].substring(1))) { + return true; + } + } + } + } + return false; + } + + /** + * Whether the line has the form {@code ()}, where + * the vendor is the text before the first space and the model is the + * text in the final parentheses, and neither is empty or a no value. + */ + static boolean isDevice(String line) { + if (line.endsWith(")") == false) { + return false; + } + // The parenthesis that opens the final one, counting any nested in + // the model. + int depth = 0; + int open = -1; + for (int i = line.length() - 1; i >= 0 && open < 0; i--) { + char c = line.charAt(i); + if (c == ')') { + depth++; + } else if (c == '(' && --depth == 0) { + open = i; + } + } + int space = line.indexOf(' '); + if (open < 1 || line.charAt(open - 1) != ' ' || + space < 1 || space >= open - 1) { + return false; + } + return hasValue(line.substring(0, space)) && + hasValue(line.substring(open + 1, line.length() - 1)); + } + + private static boolean hasValue(String text) { + String value = text.trim(); + return value.isEmpty() == false && value.startsWith(NO_VALUE) == false; + } +} diff --git a/examples/device-detection/console/src/test/java/fiftyone/devicedetection/examples/console/HardwareLookupOutputTest.java b/examples/device-detection/console/src/test/java/fiftyone/devicedetection/examples/console/HardwareLookupOutputTest.java new file mode 100644 index 000000000..20ce8018e --- /dev/null +++ b/examples/device-detection/console/src/test/java/fiftyone/devicedetection/examples/console/HardwareLookupOutputTest.java @@ -0,0 +1,65 @@ +/* ********************************************************************* + * This Original Work is copyright of 51 Degrees Mobile Experts Limited. + * Copyright 2026 51 Degrees Mobile Experts Limited, Davidson House, + * Forbury Square, Reading, Berkshire, United Kingdom RG1 3EU. + * + * This Original Work is licensed under the European Union Public Licence + * (EUPL) v.1.2 and is subject to its terms as set out below. + * + * If a copy of the EUPL was not distributed with this file, You can obtain + * one at https://opensource.org/licenses/EUPL-1.2. + * + * The 'Compatible Licences' set out in the Appendix to the EUPL (as may be + * amended by the European Commission) shall be deemed incompatible for + * the purposes of the Work and the provisions of the compatibility + * clause in Article 5 of the EUPL shall not apply. + * + * If using the Work as, or as part of, a network application, by + * including the attribution notice(s) required under Article 5 of the EUPL + * in the end user terms of the application under an appropriate heading, + * such notice(s) shall fulfill the requirements of that article. + * ********************************************************************* */ + +package fiftyone.devicedetection.examples.console; + +import org.junit.Test; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +/** + * Checks which lines the live tests of the TAC and native model examples + * count as a device with a vendor and a model. + */ +public class HardwareLookupOutputTest { + + @Test + public void deviceWithVendorAndModel() { + assertTrue(HardwareLookupOutput.isDevice("Apple iPhone 6 (A1586)")); + assertTrue(HardwareLookupOutput.isDevice( + "Samsung Galaxy S10 (SC-03L)")); + } + + @Test + public void modelHoldingParentheses() { + assertTrue(HardwareLookupOutput.isDevice( + "Vendor Name (Model (2020))")); + } + + @Test + public void vendorOrModelWithNoValue() { + assertFalse(HardwareLookupOutput.isDevice( + "Unknown. No value was found. iPhone 6 (A1586)")); + assertFalse(HardwareLookupOutput.isDevice( + "Apple iPhone 6 (Unknown. No value (reason))")); + assertFalse(HardwareLookupOutput.isDevice("Apple iPhone 6 ()")); + } + + @Test + public void otherForms() { + assertFalse(HardwareLookupOutput.isDevice("Apple; iPhone 6; A1586")); + assertFalse(HardwareLookupOutput.isDevice("Apple (A1586)")); + assertFalse(HardwareLookupOutput.isDevice( + "No devices are associated with this TAC.")); + } +} diff --git a/examples/device-detection/console/src/test/java/fiftyone/devicedetection/examples/console/HardwareLookupWithoutProfilesTest.java b/examples/device-detection/console/src/test/java/fiftyone/devicedetection/examples/console/HardwareLookupWithoutProfilesTest.java new file mode 100644 index 000000000..1336aad45 --- /dev/null +++ b/examples/device-detection/console/src/test/java/fiftyone/devicedetection/examples/console/HardwareLookupWithoutProfilesTest.java @@ -0,0 +1,193 @@ +/* ********************************************************************* + * This Original Work is copyright of 51 Degrees Mobile Experts Limited. + * Copyright 2026 51 Degrees Mobile Experts Limited, Davidson House, + * Forbury Square, Reading, Berkshire, United Kingdom RG1 3EU. + * + * This Original Work is licensed under the European Union Public Licence + * (EUPL) v.1.2 and is subject to its terms as set out below. + * + * If a copy of the EUPL was not distributed with this file, You can obtain + * one at https://opensource.org/licenses/EUPL-1.2. + * + * The 'Compatible Licences' set out in the Appendix to the EUPL (as may be + * amended by the European Commission) shall be deemed incompatible for + * the purposes of the Work and the provisions of the compatibility + * clause in Article 5 of the EUPL shall not apply. + * + * If using the Work as, or as part of, a network application, by + * including the attribution notice(s) required under Article 5 of the EUPL + * in the end user terms of the application under an appropriate heading, + * such notice(s) shall fulfill the requirements of that article. + * ********************************************************************* */ + +package fiftyone.devicedetection.examples.console; + +import com.sun.net.httpserver.HttpServer; +import fiftyone.devicedetection.examples.shared.ExampleProcess; +import fiftyone.devicedetection.examples.shared.FindOutMore; +import fiftyone.devicedetection.examples.shared.KeyHelper; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.net.InetAddress; +import java.net.InetSocketAddress; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.concurrent.CopyOnWriteArrayList; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +/** + * Runs the TAC and native model examples against a stand-in for the cloud + * service that this test starts on this machine, so no resource key or + * network is needed. The stand-in gives the accessible properties the cloud + * gives a resource key that does not include HardwarePlatform.Profiles, the + * evidence keys the cloud accepts, and 404 for anything else. Each example + * should say the key lacks the lookup, link to where a key is created and + * to the contact page, make no lookup and end normally. + */ +public class HardwareLookupWithoutProfilesTest { + + /** + * The variable the cloud request engine reads its endpoint from. + */ + private static final String CLOUD_ENDPOINT = "_51DEGREES_CLOUD_ENDPOINT"; + + /** + * A value the examples accept as a resource key, which the stand-in + * does not check. + */ + private static final String RESOURCE_KEY = "standInResourceKey000000"; + + private static final String TAGS = + "?utm_source=code&utm_medium=example&utm_campaign=51degrees-java"; + + private static final String CONFIGURE = + "https://51degrees.com/configure/QKyYH5XT" + TAGS; + + private static final String CONTACT_US = + "https://51degrees.com/contact-us" + TAGS; + + private static final String MESSAGE = + "The resource key does not include HardwarePlatform.Profiles"; + + private final List requests = new CopyOnWriteArrayList<>(); + + private HttpServer cloud; + + @Before + public void startCloud() throws Exception { + byte[] properties = fixture("accessibleproperties-no-hardware.json"); + byte[] evidenceKeys = fixture("evidencekeys.json"); + cloud = HttpServer.create( + new InetSocketAddress(InetAddress.getLoopbackAddress(), 0), 0); + cloud.createContext("/", exchange -> { + try (InputStream body = exchange.getRequestBody()) { + byte[] buffer = new byte[4096]; + while (body.read(buffer) >= 0) { + // Read the whole request before answering. + } + } + String path = exchange.getRequestURI().getPath(); + requests.add(path); + byte[] answer = path.endsWith("accessibleproperties") ? + properties : + path.endsWith("evidencekeys") ? evidenceKeys : null; + if (answer == null) { + exchange.sendResponseHeaders(404, -1); + exchange.close(); + return; + } + exchange.getResponseHeaders().add("Content-Type", + "application/json"); + exchange.sendResponseHeaders(200, answer.length); + try (OutputStream out = exchange.getResponseBody()) { + out.write(answer); + } + }); + cloud.start(); + } + + @After + public void stopCloud() { + cloud.stop(0); + } + + private static byte[] fixture(String name) throws IOException { + try (InputStream input = + HardwareLookupWithoutProfilesTest.class + .getResourceAsStream(name)) { + assertNotNull("The fixture " + name + " should be a resource", + input); + ByteArrayOutputStream bytes = new ByteArrayOutputStream(); + byte[] buffer = new byte[4096]; + int read; + while ((read = input.read(buffer)) >= 0) { + bytes.write(buffer, 0, read); + } + return bytes.toByteArray(); + } + } + + private void runWithoutProfiles(Class example) throws Exception { + Map environment = new HashMap<>(); + environment.put(KeyHelper.RESOURCE_KEY_VARIABLE, RESOURCE_KEY); + environment.put(CLOUD_ENDPOINT, "http://" + + cloud.getAddress().getHostString() + ":" + + cloud.getAddress().getPort() + "/api/v4/"); + ExampleProcess.Result result = + ExampleProcess.runWith(example, environment); + System.out.println(result.output); + String output = result.output; + + assertEquals(example.getSimpleName() + + " should end with exit code 0.\n" + output, + 0, result.exitCode); + String message = null; + for (String line : output.split("\r?\n")) { + assertFalse("No lookup should be listed.\n" + output, + line.startsWith("Which devices are associated")); + if (line.startsWith(MESSAGE)) { + message = line; + } + } + assertNotNull("The output should say the key does not include " + + "HardwarePlatform.Profiles.\n" + output, message); + assertTrue("The message should link to where a key is created.\n" + + message, message.contains(CONFIGURE)); + assertTrue("The message should link to the contact page.\n" + + message, message.contains(CONTACT_US)); + assertTrue("The output should end with the '" + FindOutMore.HEADING + + "' section after the message.\n" + output, + output.lastIndexOf(FindOutMore.HEADING) > + output.indexOf(MESSAGE)); + for (String request : requests) { + assertFalse("No lookup request should be made, and " + + request + " was requested.", + request.endsWith("json")); + } + assertTrue("The example should have asked the stand-in for the " + + "accessible properties, and asked for " + requests, + requests.stream().anyMatch( + request -> request.endsWith("accessibleproperties"))); + } + + @Test + public void tacCloud() throws Exception { + runWithoutProfiles(TacCloud.class); + } + + @Test + public void nativeModelCloud() throws Exception { + runWithoutProfiles(NativeModelCloud.class); + } +} diff --git a/examples/device-detection/console/src/test/java/fiftyone/devicedetection/examples/console/NativeModelCloudTest.java b/examples/device-detection/console/src/test/java/fiftyone/devicedetection/examples/console/NativeModelCloudTest.java index b30a9e9d9..1d4a0e0b2 100644 --- a/examples/device-detection/console/src/test/java/fiftyone/devicedetection/examples/console/NativeModelCloudTest.java +++ b/examples/device-detection/console/src/test/java/fiftyone/devicedetection/examples/console/NativeModelCloudTest.java @@ -26,14 +26,14 @@ import fiftyone.devicedetection.examples.shared.KeyHelper; import org.junit.Test; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; - public class NativeModelCloudTest { /** * The example runs as a customer runs it, in a process of its own - * that reads its key from _51DEGREES_RESOURCE_KEY. The test gives that - * process the key with every property. + * that reads its key from _51DEGREES_RESOURCE_KEY, and the cloud that + * _51DEGREES_CLOUD_ENDPOINT names when that is set. The test gives that + * process the key with every property, and fails unless the example + * lists a device with a vendor and a model for each native model name, + * so a key that does not include HardwarePlatform.Profiles fails it. */ @Test public void run() throws Exception { @@ -43,10 +43,8 @@ public void run() throws Exception { ExampleProcess.Result result = ExampleProcess.runWithResourceKey( NativeModelCloud.class, resourceKey); System.out.println(result.output); - assertEquals("NativeModelCloud should end with exit code 0", - 0, result.exitCode); - assertTrue("The output should hold the example's results", - result.output.contains("This example shows the details of " + - "devices associated with a given 'native model name'")); + HardwareLookupOutput.requireDevices(NativeModelCloud.class, result, + "native model lookup", "the native model name", + NativeModelCloud.NATIVE_MODEL_1, NativeModelCloud.NATIVE_MODEL_2); } } \ No newline at end of file diff --git a/examples/device-detection/console/src/test/java/fiftyone/devicedetection/examples/console/TacCloudTest.java b/examples/device-detection/console/src/test/java/fiftyone/devicedetection/examples/console/TacCloudTest.java index aacd35231..1a5bf3776 100644 --- a/examples/device-detection/console/src/test/java/fiftyone/devicedetection/examples/console/TacCloudTest.java +++ b/examples/device-detection/console/src/test/java/fiftyone/devicedetection/examples/console/TacCloudTest.java @@ -26,15 +26,15 @@ import fiftyone.devicedetection.examples.shared.KeyHelper; import org.junit.Test; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; - public class TacCloudTest { /** * The example runs as a customer runs it, in a process of its own - * that reads its key from _51DEGREES_RESOURCE_KEY. The test gives that - * process the key with every property. + * that reads its key from _51DEGREES_RESOURCE_KEY, and the cloud that + * _51DEGREES_CLOUD_ENDPOINT names when that is set. The test gives that + * process the key with every property, and fails unless the example + * lists a device with a vendor and a model for each TAC, so a key that + * does not include HardwarePlatform.Profiles fails it. */ @Test public void run() throws Exception { @@ -44,10 +44,7 @@ public void run() throws Exception { ExampleProcess.Result result = ExampleProcess.runWithResourceKey(TacCloud.class, resourceKey); System.out.println(result.output); - assertEquals("TacCloud should end with exit code 0", - 0, result.exitCode); - assertTrue("The output should hold the example's results", - result.output.contains("This example shows the details of " + - "devices associated with a given 'Type Allocation Code'")); + HardwareLookupOutput.requireDevices(TacCloud.class, result, + "TAC lookup", "the TAC", TacCloud.TAC_1, TacCloud.TAC_2); } } \ No newline at end of file diff --git a/examples/device-detection/console/src/test/resources/fiftyone/devicedetection/examples/console/accessibleproperties-no-hardware.json b/examples/device-detection/console/src/test/resources/fiftyone/devicedetection/examples/console/accessibleproperties-no-hardware.json new file mode 100644 index 000000000..12b626c2e --- /dev/null +++ b/examples/device-detection/console/src/test/resources/fiftyone/devicedetection/examples/console/accessibleproperties-no-hardware.json @@ -0,0 +1,15 @@ +{ + "Products": { + "device": { + "DataTier": "CloudV5Basic", + "Properties": [ + { + "Name": "IsMobile", + "Type": "Boolean", + "Category": "Device", + "DelayExecution": false + } + ] + } + } +} diff --git a/examples/device-detection/console/src/test/resources/fiftyone/devicedetection/examples/console/evidencekeys.json b/examples/device-detection/console/src/test/resources/fiftyone/devicedetection/examples/console/evidencekeys.json new file mode 100644 index 000000000..9d7e73847 --- /dev/null +++ b/examples/device-detection/console/src/test/resources/fiftyone/devicedetection/examples/console/evidencekeys.json @@ -0,0 +1,142 @@ +[ + "fiftyone.resource-key", + "header.referer", + "header.origin", + "fiftyone.license-key", + "fiftyone.default-cloud-product", + "fiftyone.default-v4-free", + "query.values", + "server.client-ip", + "location.latitude", + "location.longitude", + "cookie.51d_pos_latitude", + "cookie.51d_pos_longitude", + "query.51d_pos_latitude", + "query.51d_pos_longitude", + "header.Device-Stock-UA", + "query.Device-Stock-UA", + "header.X-Device-User-Agent", + "query.X-Device-User-Agent", + "header.X-OperaMini-Phone-UA", + "query.X-OperaMini-Phone-UA", + "header.X-UCBrowser-Device-UA", + "query.X-UCBrowser-Device-UA", + "header.User-Agent", + "query.User-Agent", + "header.Sec-CH-UA-Mobile", + "query.Sec-CH-UA-Mobile", + "header.Sec-CH-UA-Platform", + "query.Sec-CH-UA-Platform", + "header.Sec-CH-UA-Model", + "query.Sec-CH-UA-Model", + "header.Sec-CH-UA-Platform-Version", + "query.Sec-CH-UA-Platform-Version", + "header.Sec-CH-UA", + "query.Sec-CH-UA", + "header.Sec-CH-UA-Full-Version-List", + "query.Sec-CH-UA-Full-Version-List", + "cookie.51D_HasWebDriver", + "query.51D_HasWebDriver", + "cookie.51D_HasWebDriverJavaScript", + "query.51D_HasWebDriverJavaScript", + "cookie.51D_IsVerifiediPhone", + "query.51D_IsVerifiediPhone", + "cookie.51D_IsVerifiediPhoneJavaScript", + "query.51D_IsVerifiediPhoneJavaScript", + "cookie.51D_IsVisible", + "query.51D_IsVisible", + "cookie.51D_IsVisibleJavaScript", + "query.51D_IsVisibleJavaScript", + "cookie.51D_Javascript", + "query.51D_Javascript", + "cookie.51D_JavascriptBandwidth", + "query.51D_JavascriptBandwidth", + "cookie.51D_JavaScriptBrowserOverride", + "query.51D_JavaScriptBrowserOverride", + "cookie.51D_JavascriptGetHighEntropyValues", + "query.51D_JavascriptGetHighEntropyValues", + "cookie.51D_JavascriptHardwareProfile", + "query.51D_JavascriptHardwareProfile", + "cookie.51D_JavascriptImageOptimiser", + "query.51D_JavascriptImageOptimiser", + "cookie.51D_PixelRatio", + "query.51D_PixelRatio", + "cookie.51D_PixelRatioJavascript", + "query.51D_PixelRatioJavascript", + "cookie.51D_ScreenPixelsHeight", + "query.51D_ScreenPixelsHeight", + "cookie.51D_ScreenPixelsHeightJavaScript", + "query.51D_ScreenPixelsHeightJavaScript", + "cookie.51D_ScreenPixelsWidth", + "query.51D_ScreenPixelsWidth", + "cookie.51D_ScreenPixelsWidthJavaScript", + "query.51D_ScreenPixelsWidthJavaScript", + "cookie.51D_ThirdPartyCookiesEnabled", + "query.51D_ThirdPartyCookiesEnabled", + "cookie.51D_ThirdPartyCookiesEnabledJavaScript", + "query.51D_ThirdPartyCookiesEnabledJavaScript", + "query.51D_deviceId", + "query.51D_gethighentropyvalues", + "query.51D_structureduseragent", + "cookie.51D_gethighentropyvalues", + "cookie.51D_structureduseragent", + "query.51D_ProfileIds", + "cookie.51D_ProfileIds", + "query.tac", + "query.nativemodel", + "query.robotstxt.analytics", + "query.robotstxt.archiving", + "query.robotstxt.discovery", + "query.robotstxt.feed", + "query.robotstxt.index", + "query.robotstxt.input", + "query.robotstxt.monitor", + "query.robotstxt.n/a", + "query.robotstxt.preview", + "query.robotstxt.search", + "query.robotstxt.security", + "query.robotstxt.train", + "query.robotstxt.tdl", + "header.signature", + "header.signature-input", + "header.signature-agent", + "header.host", + "header.protocol", + "server.request-method", + "server.request-path", + "server.request-query", + "query.true-client-ip-51d", + "server.true-client-ip-51d", + "query.client-ip-51d", + "server.client-ip-51d", + "query.client-ip", + "query.translation", + "query.accept-language", + "header.accept-language", + "query.tcstring", + "header.client-ip", + "server.connection-ip", + "server.browser-ip", + "query.id.usage", + "header.id.usage", + "derived.id.usage", + "server.browser-page", + "query.id.email", + "header.id.email", + "query.id.salt", + "header.id.salt", + "query.ja4", + "header.ja4", + "server.ja4", + "query.h2", + "header.h2", + "server.h2", + "server.licensed-caller", + "query.humanconfidence", + "query.transaction", + "query.challenge", + "cloud.json-values-only", + "query.json-values-only", + "query.fod-js-object-name", + "query.fod-js-enable-cookies" +]