Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
2b84a80
Test: only one thread at a time reads the native results of finished …
jwrosewell Nov 15, 2026
8e1a1fc
Fix: hold one lock for every read of the native results in device data
jwrosewell Nov 15, 2026
3e252ca
Test: the engine adds metadata for each match metric, as the table gi…
jwrosewell Nov 15, 2026
56b1acf
Fix: give each match metric the Metrics component, and Method a singl…
jwrosewell Nov 15, 2026
aa549c4
Test: the engine's components include Metrics
jwrosewell Nov 15, 2026
4f5ab52
Fix: add the Metrics component to the engine's components
jwrosewell Nov 15, 2026
17a3469
Test: the engine gives the ids of the profiles that hold a value of a…
jwrosewell Nov 15, 2026
a657a4f
Fix: give the profiles for a property value, and an Indexed Propertie…
jwrosewell Nov 15, 2026
ccce971
Test: every Device Detection engine populates Aspect Data
jwrosewell Nov 15, 2026
fe870d7
Test: on-premise and cloud engines return interface compatible data
jwrosewell Nov 15, 2026
5288ee6
Test: the engine reads the evidence keys again when the data is refre…
jwrosewell Nov 15, 2026
1ce6add
Test: device data reads a property from the native results only when …
jwrosewell Nov 15, 2026
d71666f
Test: an engine configured with no property names is created with none
jwrosewell Nov 15, 2026
4c15cff
Test: the published Device Detection packages hold no tests or examples
jwrosewell Nov 15, 2026
234d9ee
Fix: keep the test helpers out of the published device-detection.shar…
jwrosewell Nov 15, 2026
a15e255
Test: a read of a property the engine was created with goes by positi…
jwrosewell Nov 15, 2026
23783e5
Fix: find a property by its position, not by a native lookup of its name
jwrosewell Nov 15, 2026
7f5a917
Test: the engine reads what changes only on refresh from native code …
jwrosewell Nov 15, 2026
74985cd
Test: the cloud Device Detection engine leaves processing to the shar…
jwrosewell Nov 15, 2026
5c440dc
Fix: let the shared Cloud Aspect Engine logic process for cloud Devic…
jwrosewell Nov 15, 2026
a4f3217
Test: the hardware profile cloud engine processes through the shared …
jwrosewell Nov 15, 2026
9ff3ff4
Merge rc into the section 11 conformance work
jwrosewell Nov 16, 2026
dbc6c8d
Merge rc into the Device Detection engine work
jwrosewell Nov 16, 2026
cdd4138
Take the Device Detection test files off the deprecated FileUtils
jwrosewell Nov 16, 2026
88c03aa
Keep rc's getNamedKey and isInvalidKey, which the merge of rc gave th…
jwrosewell Nov 16, 2026
e8c2559
Keep FileUtils and KeyUtils in the published jar, deprecated, as rc h…
jwrosewell Nov 16, 2026
60d509f
Let the published jar keep the two deprecated test helpers, and nothi…
jwrosewell Nov 16, 2026
12c0e79
Merge rc into the Device Detection engine work
jwrosewell Nov 16, 2026
c1e7424
Process through the engine's public process in the empty metadata tes…
jwrosewell Nov 16, 2026
96a04e4
Say in the empty metadata tests that the cloud request ran, as the sh…
jwrosewell Nov 16, 2026
91b52ae
Let the published jar keep the folder entry of the two deprecated tes…
jwrosewell Nov 16, 2026
4bb1bbe
Test: Device Detection data reads its native results with no lock
jwrosewell Nov 19, 2026
bcac3b8
Fix: read Device Detection's native results with no lock
jwrosewell Nov 19, 2026
bfc08ce
Keep cloud Device Detection's own processing, as released
jwrosewell Nov 19, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
/* *********************************************************************
* 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.cloud.flowelements;

import fiftyone.devicedetection.cloud.data.MultiDeviceDataCloud;
import fiftyone.devicedetection.shared.DeviceData;
import fiftyone.pipeline.cloudrequestengine.data.CloudRequestData;
import fiftyone.pipeline.cloudrequestengine.flowelements.CloudRequestEngine;
import fiftyone.pipeline.cloudrequestengine.flowelements.PropertyKeyedCloudEngineBase;
import fiftyone.pipeline.core.data.AccessiblePropertyMetaData.ProductMetaData;
import fiftyone.pipeline.core.data.AccessiblePropertyMetaData.PropertyMetaData;
import fiftyone.pipeline.core.data.FlowData;
import fiftyone.pipeline.core.flowelements.Pipeline;
import fiftyone.pipeline.engines.services.MissingPropertyServiceDefault;
import org.junit.Test;
import org.slf4j.ILoggerFactory;
import org.slf4j.LoggerFactory;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

/**
* The majority of the processing SHOULD be handled by the shared Cloud Aspect
* Engine logic
* (device-detection-specification/pipeline-elements/hardware-profile-lookup-cloud.md
* line 56 at 706de1f0). The hardware profile engine gives no processing of
* its own, and the shared logic for property keyed engines turns each
* profile in its part of the response into DeviceData.
*/
public class HardwareProfileCloudEngineTests {

private static final ILoggerFactory loggerFactory =
LoggerFactory.getILoggerFactory();

private static HardwareProfileCloudEngine engine() throws Exception {
HardwareProfileCloudEngine engine =
new HardwareProfileCloudEngineBuilder(loggerFactory).build();
ProductMetaData product = new ProductMetaData();
product.dataTier = "test";
product.properties = Arrays.asList(
new PropertyMetaData("profiles", "Array", "Device", Arrays.asList(
new PropertyMetaData("hardwarevendor", "String", "Device", null),
new PropertyMetaData("hardwarename", "Array", "Device", null))));
Map<String, ProductMetaData> products = new HashMap<>();
products.put("hardware", product);
CloudRequestEngine requestEngine = mock(CloudRequestEngine.class);
when(requestEngine.getPublicProperties()).thenReturn(products);
Pipeline pipeline = mock(Pipeline.class);
when(pipeline.getElement(CloudRequestEngine.class))
.thenReturn(requestEngine);
engine.addPipeline(pipeline);
return engine;
}

private static MultiDeviceDataCloud process(
HardwareProfileCloudEngine engine,
String response) throws Exception {
CloudRequestData cloudData = mock(CloudRequestData.class);
when(cloudData.getJsonResponse()).thenReturn(response);
when(cloudData.getProcessStarted()).thenReturn(true);
FlowData flowData = mock(FlowData.class);
when(flowData.getFromElement(any(CloudRequestEngine.class)))
.thenReturn(cloudData);
MultiDeviceDataCloud data = new MultiDeviceDataCloud(
loggerFactory.getLogger(MultiDeviceDataCloud.class.getSimpleName()),
flowData,
engine,
MissingPropertyServiceDefault.getInstance());
// The Pipeline calls the protected processEngine of the shared base.
Method processEngine = null;
for (Class<?> type = engine.getClass(); processEngine == null &&
type != null; type = type.getSuperclass()) {
for (Method method : type.getDeclaredMethods()) {
if (method.getName().equals("processEngine") &&
method.getParameterCount() == 2) {
processEngine = method;
}
}
}
processEngine.setAccessible(true);
try {
processEngine.invoke(engine, flowData, data);
} catch (InvocationTargetException e) {
throw (Exception) e.getCause();
}
return data;
}

/**
* The engine gives no processing of its own.
*/
@Test
public void HardwareProfile_ProcessingIsShared() {
assertTrue(PropertyKeyedCloudEngineBase.class.isAssignableFrom(
HardwareProfileCloudEngine.class));
for (Method method :
HardwareProfileCloudEngine.class.getDeclaredMethods()) {
assertTrue("The engine processes with " + method.getName(),
method.getName().equals("processEngine") == false &&
method.getName().equals("processCloudEngine") == false);
}
}

/**
* Each profile in the engine's part of the response becomes DeviceData
* with the values the response gives.
*/
@Test
public void HardwareProfile_ProfilesAreDeviceData() throws Exception {
try (HardwareProfileCloudEngine engine = engine()) {
MultiDeviceDataCloud data = process(engine,
"{ 'hardware': { 'profiles': [" +
"{ 'hardwarevendor': 'Apple', " +
"'hardwarename': ['iPhone 12', 'iPhone 12 Pro'] }, " +
"{ 'hardwarevendor': null, " +
"'hardwarevendornullreason': 'No match', " +
"'hardwarename': [] } ] } }");
List<DeviceData> profiles = data.getProfiles();
assertEquals(2, profiles.size());
assertEquals("Apple",
profiles.get(0).getHardwareVendor().getValue());
assertEquals(Arrays.asList("iPhone 12", "iPhone 12 Pro"),
profiles.get(0).getHardwareName().getValue());
assertEquals(false,
profiles.get(1).getHardwareVendor().hasValue());
assertEquals("No match",
profiles.get(1).getHardwareVendor().getNoValueMessage());
}
}

/**
* An empty response, after an error the Cloud Request Engine has
* reported, gives no profiles and raises no error.
*/
@Test
public void HardwareProfile_EmptyResponse_NoProfiles() throws Exception {
try (HardwareProfileCloudEngine engine = engine()) {
assertTrue(process(engine, "").getProfiles().isEmpty());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ public class Constants {
public static class MatchMetrics {
/* Category */
public static final String CATEGORY = "Device Metrics";


/* Component */
public static final String COMPONENT = "Metrics";

/* Names */
public static final String MATCHED_NODES = "MatchedNodes";
public static final String DIFFERENCE = "Difference";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@
/**
* Internal implementation of the {@link DeviceDataHash} interface. This can
* only be constructed by the {@link DeviceDetectionHashEngine}.
* <p>
* Values are read from the native results with no lock. Reading a value
* writes into the results, so an instance is not safe to use from several
* threads at once, and a caller that shares one is responsible for any
* locking.
* @see <a href="https://github.com/51Degrees/specifications/blob/d79bbe3b2aef86273570742f6b660f29bfe72816/device-detection-specification/pipeline-elements/device-detection-on-premise.md#results-and-threads">Results and threads</a>
* @see <a href="https://github.com/51Degrees/specifications/blob/main/device-detection-specification/pipeline-elements/device-detection-on-premise.md#element-data">Specification</a>
*/
public class DeviceDataHashDefault
Expand Down Expand Up @@ -259,10 +265,13 @@ private AspectPropertyValue<List<String>> getUserAgentsInternal() {
@Override
protected boolean propertyIsAvailable(String propertyName) {
checkState();
// A property the engine has a position for is held by its results,
// so that needs no lookup of the name in native code.
if (indexOf(propertyName) != null) {
return resultsList.isEmpty() == false;
// A property the engine was created with has a position, and the
// read goes by it, so there is no need to look the name up in the
// results first. A property the data set lacks keeps its position
// and reads as having no value. With results from more than one
// engine, the name finds the results that hold the property.
if (resultsList.size() == 1 && indexOf(propertyName) != null) {
return true;
}
for (ResultsHashSwig results : resultsList) {
if (results.containsProperty(Swig.asBytes(propertyName))) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ public class DeviceDetectionHashEngine
*/
private volatile boolean propertyIndexesFixed;

/**
* The Metrics component, made the first time it is needed.
*/
private volatile MetricsComponentMetaData metricsComponent;

/**
* Construct a new instance of the {@link DeviceDetectionHashEngine}.
* @param logger logger instance to use for logging
Expand Down Expand Up @@ -259,11 +264,42 @@ public ProfileMetaData getProfile(int profileId) {
null : new ProfileMetaDataHash(this, profile);
}

/**
* Get the ids of the profiles that hold a value of a property, such as a
* TAC or a native model name, from the native engine. The property name
* and the value are both matched exactly, as the data file holds them,
* so a value in another case does not match. The ids are in ascending
* order and each appears once. A property named with
* {@link DeviceDetectionHashEngineBuilder#setIndexedProperties(List)} is
* found through the native index, and any other by reading every
* profile, which gives the same ids more slowly.
* @see <a href="https://github.com/51Degrees/specifications/blob/main/device-detection-specification/pipeline-elements/device-detection-on-premise.md#profiles-for-a-property-value">Specification</a>
* @param propertyName name of the property, such as TAC or NativeModel
* @param valueName the value of the property
* @return ids of the profiles holding the value, empty when none do
*/
public List<Long> getProfileIdsForPropertyValue(
String propertyName,
String valueName) {
List<Long> result = new ArrayList<>();
try (VectorUInt32Swig ids =
engine.getProfileIdsForPropertyValue(propertyName, valueName)) {
int size = ids.size();
for (int i = 0; i < size; i++) {
result.add(ids.get(i));
}
}
return result;
}

@Override
public CloseableIterable<ComponentMetaData> getComponents() {
// The Metrics component holds the match metrics, which the data file
// does not have, so it is given after the data file's components.
return new ComponentIterable(
this,
engine.getMetaData().getComponents());
engine.getMetaData().getComponents(),
getMetricsComponent());
}

@Override
Expand Down Expand Up @@ -447,6 +483,32 @@ private void setEngineMetaData() {
* @return meta data for metric properties
*/
private List<FiftyOneAspectPropertyMetaData> getMetricProperties() {
return getMetricsComponent().getPropertyList();
}

/**
* Get the Metrics component, which holds the match metric properties. It
* does not depend on the data file, so it is made once. Two threads that
* both find it missing each make one the same as the other.
* @return the Metrics component
*/
private MetricsComponentMetaData getMetricsComponent() {
MetricsComponentMetaData component = metricsComponent;
if (component == null) {
component = newMetricsComponent();
metricsComponent = component;
}
return component;
}

/**
* Make the Metrics component and the match metric properties in it, which
* the native code does not include in its metadata, as the table of match
* metric properties gives them.
* @return new Metrics component
*/
private MetricsComponentMetaData newMetricsComponent() {
MetricsComponentMetaData component = new MetricsComponentMetaData();
List<String> dataFileList = Arrays.asList(
"Lite", "Premium", "Enterprise", "TAC");
FiftyOneAspectPropertyMetaData[] metricProperties =
Expand All @@ -467,7 +529,7 @@ private List<FiftyOneAspectPropertyMetaData> getMetricProperties() {
false,
false,
Constants.MatchMetrics.MATCHED_NODES_DESCRIPTION,
null,
component,
Arrays.asList(),
new ValueMetaDataDefault("0")),
new FiftyOneAspectPropertyMetaDataDefault(
Expand All @@ -485,7 +547,7 @@ private List<FiftyOneAspectPropertyMetaData> getMetricProperties() {
false,
false,
Constants.MatchMetrics.DIFFERENCE_DESCRIPTION,
null,
component,
Arrays.asList(),
new ValueMetaDataDefault("0")),
new FiftyOneAspectPropertyMetaDataDefault(
Expand All @@ -503,7 +565,7 @@ private List<FiftyOneAspectPropertyMetaData> getMetricProperties() {
false,
false,
Constants.MatchMetrics.DRIFT_DESCRIPTION,
null,
component,
Arrays.asList(),
new ValueMetaDataDefault("0")),
new FiftyOneAspectPropertyMetaDataDefault(
Expand All @@ -521,7 +583,7 @@ private List<FiftyOneAspectPropertyMetaData> getMetricProperties() {
false,
false,
Constants.MatchMetrics.DEVICE_ID_DESCRIPTION,
null,
component,
Arrays.asList(),
new ValueMetaDataDefault("0-0-0-0")),
new FiftyOneAspectPropertyMetaDataDefault(
Expand All @@ -539,7 +601,7 @@ private List<FiftyOneAspectPropertyMetaData> getMetricProperties() {
false,
false,
Constants.MatchMetrics.USER_AGENTS_DESCRIPTION,
null,
component,
Arrays.asList(),
new ValueMetaDataDefault("n/a")),
new FiftyOneAspectPropertyMetaDataDefault(
Expand All @@ -557,7 +619,7 @@ private List<FiftyOneAspectPropertyMetaData> getMetricProperties() {
false,
false,
Constants.MatchMetrics.ITERATIONS_DESCRIPTION,
null,
component,
Arrays.asList(),
new ValueMetaDataDefault("0")),
new FiftyOneAspectPropertyMetaDataDefault(
Expand All @@ -570,20 +632,23 @@ private List<FiftyOneAspectPropertyMetaData> getMetricProperties() {
null,
(byte) 0,
true,
true,
false,
false,
false,
false,
Constants.MatchMetrics.METHOD_DESCRIPTION,
null,
component,
Arrays.asList(
new ValueMetaDataDefault("NONE"),
new ValueMetaDataDefault("PERFORMANCE"),
new ValueMetaDataDefault("COMBINED"),
new ValueMetaDataDefault( "PREDICTIVE")),
new ValueMetaDataDefault("NONE"))
};
return Arrays.asList(metricProperties);
for (FiftyOneAspectPropertyMetaData property : metricProperties) {
component.addProperty(property);
}
return component;
}

@Override
Expand Down
Loading
Loading