diff --git a/cxx b/cxx
index 7992af701..178371ac8 160000
--- a/cxx
+++ b/cxx
@@ -1 +1 @@
-Subproject commit 7992af7010b7654c115510938a2f7626204bf936
+Subproject commit 178371ac8d5dec71e625fcdcb833d1dcd1ce3480
diff --git a/device-detection/device-detection.hash.engine.on-premise/src/main/java/fiftyone/devicedetection/hash/engine/onpremise/flowelements/DeviceDetectionHashEngineBuilder.java b/device-detection/device-detection.hash.engine.on-premise/src/main/java/fiftyone/devicedetection/hash/engine/onpremise/flowelements/DeviceDetectionHashEngineBuilder.java
index ae78f9d0a..0e6f98760 100644
--- a/device-detection/device-detection.hash.engine.on-premise/src/main/java/fiftyone/devicedetection/hash/engine/onpremise/flowelements/DeviceDetectionHashEngineBuilder.java
+++ b/device-detection/device-detection.hash.engine.on-premise/src/main/java/fiftyone/devicedetection/hash/engine/onpremise/flowelements/DeviceDetectionHashEngineBuilder.java
@@ -156,6 +156,7 @@ public DeviceDetectionHashEngineBuilder setPerformanceProfile(
}
}
+ @DefaultValue("Balanced")
@Override
public DeviceDetectionHashEngineBuilder setPerformanceProfile(
PerformanceProfiles profile) {
@@ -327,6 +328,7 @@ public DeviceDetectionHashEngineBuilder setIndexedProperties(
return setIndexedProperties(list);
}
+ @DefaultValue("No results cache, and none can be set for this engine")
@Override
public DeviceDetectionHashEngineBuilder setCache(
CacheConfiguration cacheConfiguration) {
diff --git a/device-detection/device-detection.shared/src/main/java/fiftyone/devicedetection/shared/DeviceDataBaseOnPremise.java b/device-detection/device-detection.shared/src/main/java/fiftyone/devicedetection/shared/DeviceDataBaseOnPremise.java
index 68f4a8dbe..412418eb0 100644
--- a/device-detection/device-detection.shared/src/main/java/fiftyone/devicedetection/shared/DeviceDataBaseOnPremise.java
+++ b/device-detection/device-detection.shared/src/main/java/fiftyone/devicedetection/shared/DeviceDataBaseOnPremise.java
@@ -43,6 +43,12 @@
/**
* Base class used for all 51Degrees on-premise device detection results classes.
+ *
+ * An instance reads its values from native results, and reading a value
+ * writes into them, so an instance is not safe to use from several threads
+ * at once. The caller is responsible for thread-safe access, for example by
+ * wrapping its calls in a lock of its own. See the
+ * thread safety specification .
*/
public abstract class DeviceDataBaseOnPremise extends DeviceDataBase
implements ProvidesValuesAsString {
diff --git a/device-detection/device-detection/src/main/java/fiftyone/devicedetection/DeviceDetectionOnPremisePipelineBuilder.java b/device-detection/device-detection/src/main/java/fiftyone/devicedetection/DeviceDetectionOnPremisePipelineBuilder.java
index 435c29172..f38644676 100644
--- a/device-detection/device-detection/src/main/java/fiftyone/devicedetection/DeviceDetectionOnPremisePipelineBuilder.java
+++ b/device-detection/device-detection/src/main/java/fiftyone/devicedetection/DeviceDetectionOnPremisePipelineBuilder.java
@@ -24,6 +24,7 @@
import fiftyone.devicedetection.hash.engine.onpremise.flowelements.DeviceDetectionHashEngine;
import fiftyone.devicedetection.hash.engine.onpremise.flowelements.DeviceDetectionHashEngineBuilder;
+import fiftyone.pipeline.annotations.DefaultValue;
import fiftyone.pipeline.core.exceptions.PipelineConfigurationException;
import fiftyone.pipeline.core.flowelements.FlowElement;
import fiftyone.pipeline.core.flowelements.Pipeline;
@@ -139,6 +140,8 @@ DeviceDetectionOnPremisePipelineBuilder setEngineData(byte[] data) {
* @param enabled True to enable usage sharing. False to disable.
* @return This builder instance.
*/
+ @DefaultValue(booleanValue = fiftyone.pipeline.engines.fiftyone
+ .flowelements.Constants.SHARE_USAGE_DEFAULT_ENABLED)
public DeviceDetectionOnPremisePipelineBuilder setShareUsage(
boolean enabled) {
shareUsageEnabled = enabled;
@@ -153,6 +156,7 @@ public DeviceDetectionOnPremisePipelineBuilder setShareUsage(
* @param enabled True to enable auto update. False to disable.
* @return This builder instance.
*/
+ @DefaultValue(booleanValue = Constants.DEFAULT_AUTOUPDATE_ENABLED)
public DeviceDetectionOnPremisePipelineBuilder setAutoUpdate(
boolean enabled) {
autoUpdateEnabled = enabled;
@@ -167,6 +171,7 @@ public DeviceDetectionOnPremisePipelineBuilder setAutoUpdate(
* @param enabled True to enable file system watcher. False to disable.
* @return This builder instance.
*/
+ @DefaultValue(booleanValue = Constants.DEFAULT_WATCHER_ENABLED)
public DeviceDetectionOnPremisePipelineBuilder setDataFileSystemWatcher(
boolean enabled) {
dataFileSystemWatcher = enabled;
@@ -178,6 +183,7 @@ public DeviceDetectionOnPremisePipelineBuilder setDataFileSystemWatcher(
* @param dataUpdateService an instance of a dataUpdateService
* @return This builder instance.
*/
+ @DefaultValue("The DataUpdateService given to the constructor")
public DeviceDetectionOnPremisePipelineBuilder setDataUpdateService(
DataUpdateService dataUpdateService) {
this.dataUpdateService = dataUpdateService;
@@ -186,7 +192,7 @@ public DeviceDetectionOnPremisePipelineBuilder setDataUpdateService(
/**
* Enable/Disable update on startup.
- * Defaults to enabled.
+ * Defaults to disabled.
* If enabled, the auto update system will be used to check for
* an update before the device detection engine is created.
* If an update is available, it will be downloaded and applied
@@ -195,6 +201,7 @@ public DeviceDetectionOnPremisePipelineBuilder setDataUpdateService(
* @param enabled True to enable update on startup. False to disable.
* @return This builder instance.
*/
+ @DefaultValue(booleanValue = Constants.DEFAULT_UPDATE_ON_STARTUP)
public DeviceDetectionOnPremisePipelineBuilder setDataUpdateOnStartup(
boolean enabled) {
dataUpdateOnStartup = enabled;
@@ -208,6 +215,7 @@ public DeviceDetectionOnPremisePipelineBuilder setDataUpdateOnStartup(
* @param pollingIntervalSeconds The number of seconds between checks.
* @return This builder instance.
*/
+ @DefaultValue(intValue = Constants.DATA_UPDATE_POLLING_DEFAULT)
public DeviceDetectionOnPremisePipelineBuilder setUpdatePollingInterval(
int pollingIntervalSeconds) {
@@ -221,6 +229,7 @@ public DeviceDetectionOnPremisePipelineBuilder setUpdatePollingInterval(
* @param pollingIntervalMillis The number of milliseconds between checks.
* @return This builder instance.
*/
+ @DefaultValue(intValue = Constants.DATA_UPDATE_POLLING_DEFAULT * 1000)
public DeviceDetectionOnPremisePipelineBuilder setUpdatePollingIntervalMillis(
long pollingIntervalMillis) {
updatePollingInterval = pollingIntervalMillis;
@@ -235,6 +244,7 @@ public DeviceDetectionOnPremisePipelineBuilder setUpdatePollingIntervalMillis(
* polling interval in seconds.
* @return This builder instance.
*/
+ @DefaultValue(intValue = Constants.DATA_UPDATE_RANDOMISATION_DEFAULT)
public DeviceDetectionOnPremisePipelineBuilder setUpdateRandomisationMax(
int randomisationMaxSeconds) {
updateRandomisationMax = (long)randomisationMaxSeconds * 1000;
@@ -249,6 +259,7 @@ public DeviceDetectionOnPremisePipelineBuilder setUpdateRandomisationMax(
* polling interval in milliseconds.
* @return This builder instance.
*/
+ @DefaultValue(intValue = Constants.DATA_UPDATE_RANDOMISATION_DEFAULT * 1000)
public DeviceDetectionOnPremisePipelineBuilder setUpdateRandomisationMaxMillis(
long randomisationMaxMillis) {
updateRandomisationMax = randomisationMaxMillis;
@@ -262,6 +273,7 @@ public DeviceDetectionOnPremisePipelineBuilder setUpdateRandomisationMaxMillis(
* @param key The license key.
* @return This builder instance.
*/
+ @DefaultValue("No license key")
public DeviceDetectionOnPremisePipelineBuilder setDataUpdateLicenseKey(
String key) {
dataUpdateLicenseKey = key;
@@ -274,6 +286,7 @@ public DeviceDetectionOnPremisePipelineBuilder setDataUpdateLicenseKey(
* @param profile The performance profile to use.
* @return This builder instance.
*/
+ @DefaultValue("Balanced")
public DeviceDetectionOnPremisePipelineBuilder setPerformanceProfile(
Constants.PerformanceProfiles profile) {
performanceProfile = profile;
@@ -287,6 +300,7 @@ public DeviceDetectionOnPremisePipelineBuilder setPerformanceProfile(
* @param concurrency expected concurrent accesses
* @return this builder
*/
+ @DefaultValue("The result of Runtime#getRuntime().getAvailableProcessors()")
public DeviceDetectionOnPremisePipelineBuilder setConcurrency(int concurrency) {
this.concurrency = concurrency;
return this;
@@ -301,6 +315,7 @@ public DeviceDetectionOnPremisePipelineBuilder setConcurrency(int concurrency) {
* @param difference to allow
* @return this builder
*/
+ @DefaultValue(intValue = 0)
public DeviceDetectionOnPremisePipelineBuilder setDifference(int difference) {
this.difference = difference;
return this;
@@ -317,6 +332,7 @@ public DeviceDetectionOnPremisePipelineBuilder setDifference(int difference) {
* considered valid
* @return this builder
*/
+ @DefaultValue(booleanValue = false)
public DeviceDetectionOnPremisePipelineBuilder setAllowUnmatched(boolean allow) {
this.allowUnmatched = allow;
return this;
@@ -329,6 +345,7 @@ public DeviceDetectionOnPremisePipelineBuilder setAllowUnmatched(boolean allow)
* @param drift to set
* @return this builder
*/
+ @DefaultValue(intValue = 0)
public DeviceDetectionOnPremisePipelineBuilder setDrift(int drift) {
this.drift = drift;
return this;
@@ -346,6 +363,7 @@ public DeviceDetectionOnPremisePipelineBuilder setDrift(int drift) {
* @param use true if the performance graph should be used
* @return this builder
*/
+ @DefaultValue(booleanValue = false)
public DeviceDetectionOnPremisePipelineBuilder setUsePerformanceGraph(boolean use) {
this.usePerformanceGraph = use;
return this;
@@ -362,6 +380,7 @@ public DeviceDetectionOnPremisePipelineBuilder setUsePerformanceGraph(boolean us
* @param use true if the predictive graph should be used
* @return this builder
*/
+ @DefaultValue(booleanValue = true)
public DeviceDetectionOnPremisePipelineBuilder setUsePredictiveGraph(boolean use) {
this.usePredictiveGraph = use;
return this;
@@ -373,6 +392,7 @@ public DeviceDetectionOnPremisePipelineBuilder setUsePredictiveGraph(boolean use
* @param property the property that we want the engine to populate
* @return this builder
*/
+ @DefaultValue("All properties")
public DeviceDetectionOnPremisePipelineBuilder setProperty(String property) {
this.properties.add(property);
return this;
@@ -387,6 +407,7 @@ public DeviceDetectionOnPremisePipelineBuilder setProperty(String property) {
* @param url the URL to check for a new data file
* @return this builder
*/
+ @DefaultValue("51Degrees default, set by the engine's builder")
public DeviceDetectionOnPremisePipelineBuilder setDataUpdateUrl(String url) {
dataUpdateUrl = url;
return this;
@@ -403,6 +424,7 @@ public DeviceDetectionOnPremisePipelineBuilder setDataUpdateUrl(String url) {
* False otherwise
* @return this builder
*/
+ @DefaultValue(booleanValue = Constants.DEFAULT_VERIFY_MD5)
public DeviceDetectionOnPremisePipelineBuilder setDataUpdateVerifyMd5(Boolean verify) {
dataUpdateVerifyMd5 = verify;
return this;
@@ -416,6 +438,7 @@ public DeviceDetectionOnPremisePipelineBuilder setDataUpdateVerifyMd5(Boolean ve
* @param formatter the formatter to use
* @return this builder
*/
+ @DefaultValue("51Degrees default, set by the engine's builder")
public DeviceDetectionOnPremisePipelineBuilder setDataUpdateUrlFormatter(DataUpdateUrlFormatter formatter) {
dataUpdateUrlFormatter = formatter;
return this;
diff --git a/device-detection/device-detection/src/test/java/fiftyone/devicedetection/BuilderDefaultValueTests.java b/device-detection/device-detection/src/test/java/fiftyone/devicedetection/BuilderDefaultValueTests.java
new file mode 100644
index 000000000..3d04cdbfb
--- /dev/null
+++ b/device-detection/device-detection/src/test/java/fiftyone/devicedetection/BuilderDefaultValueTests.java
@@ -0,0 +1,70 @@
+/* *********************************************************************
+ * 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;
+
+import fiftyone.devicedetection.hash.engine.onpremise.flowelements.DeviceDetectionHashEngineBuilder;
+import fiftyone.pipeline.annotations.DefaultValue;
+import org.junit.Test;
+
+import java.lang.reflect.Method;
+import java.lang.reflect.Modifier;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+import static org.junit.Assert.assertEquals;
+
+/**
+ * Every configuration setter of the Device Detection on-premise builders
+ * gives its default value beside it, with {@link DefaultValue}. See
+ * reference-implementation-notes.md line 79 :
+ * "In all cases, there SHOULD be some mechanism to specify the default
+ * values alongside configuration property setters."
+ */
+public class BuilderDefaultValueTests {
+
+ static List missingDefaults(Class>... builders) {
+ List missing = new ArrayList<>();
+ for (Class> builder : builders) {
+ for (Method method : builder.getDeclaredMethods()) {
+ if (Modifier.isPublic(method.getModifiers()) &&
+ method.getName().startsWith("set") &&
+ method.isSynthetic() == false &&
+ method.isBridge() == false &&
+ method.getAnnotation(DefaultValue.class) == null) {
+ missing.add(builder.getSimpleName() + "#" +
+ method.getName() +
+ Arrays.toString(method.getParameterTypes()));
+ }
+ }
+ }
+ return missing;
+ }
+
+ @Test
+ public void builderSetters_GiveTheirDefaults() {
+ assertEquals(new ArrayList(), missingDefaults(
+ DeviceDetectionOnPremisePipelineBuilder.class,
+ DeviceDetectionHashEngineBuilder.class));
+ }
+}
diff --git a/device-detection/device-detection/src/test/java/fiftyone/devicedetection/CloudOnPremiseSubstitutionTests.java b/device-detection/device-detection/src/test/java/fiftyone/devicedetection/CloudOnPremiseSubstitutionTests.java
new file mode 100644
index 000000000..e2c3b95f3
--- /dev/null
+++ b/device-detection/device-detection/src/test/java/fiftyone/devicedetection/CloudOnPremiseSubstitutionTests.java
@@ -0,0 +1,101 @@
+/* *********************************************************************
+ * 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;
+
+import fiftyone.devicedetection.cloud.flowelements.DeviceDetectionCloudEngine;
+import fiftyone.devicedetection.hash.engine.onpremise.flowelements.DeviceDetectionHashEngine;
+import fiftyone.devicedetection.shared.DeviceData;
+import fiftyone.pipeline.core.flowelements.FlowElement;
+import org.junit.Test;
+
+import java.lang.reflect.ParameterizedType;
+import java.lang.reflect.Type;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+import static org.mockito.Mockito.CALLS_REAL_METHODS;
+import static org.mockito.Mockito.mock;
+
+/**
+ * The cloud and on-premise Device Detection engines put their results in
+ * Flow Data under the same key and as the same interface, so a consuming
+ * application reads them in the same way whichever engine is in the
+ * Pipeline. See
+ * README.md line 95 :
+ * "Where both Cloud and On-Premise variants of an Engine are available,
+ * they MUST arrange that the Properties and values produced are compatible
+ * with each other so that the Engines can be substituted in the Pipeline
+ * without alteration to the consuming application."
+ *
+ * Comparing the values each engine gives needs a resource key and the
+ * native engine with a data file, so it is left to the example tests.
+ */
+public class CloudOnPremiseSubstitutionTests {
+
+ /**
+ * The engines are created without running a constructor, as only the
+ * data key, which is a constant, is read from them.
+ */
+ @Test
+ public void elementDataKey_IsTheSame() {
+ FlowElement, ?> cloud =
+ mock(DeviceDetectionCloudEngine.class, CALLS_REAL_METHODS);
+ FlowElement, ?> onPremise =
+ mock(DeviceDetectionHashEngine.class, CALLS_REAL_METHODS);
+ assertEquals(
+ onPremise.getElementDataKey(),
+ cloud.getElementDataKey());
+ }
+
+ /**
+ * Each engine's Element Data is a {@link DeviceData}, which the
+ * consuming application asks Flow Data for.
+ */
+ @Test
+ public void elementData_IsDeviceData() {
+ assertTrue(DeviceData.class.isAssignableFrom(
+ elementDataType(DeviceDetectionCloudEngine.class)));
+ assertTrue(DeviceData.class.isAssignableFrom(
+ elementDataType(DeviceDetectionHashEngine.class)));
+ }
+
+ /**
+ * The first type argument of the nearest generic superclass, which is
+ * the engine's Element Data type.
+ */
+ private static Class> elementDataType(Class> engine) {
+ for (Class> c = engine; c != null; c = c.getSuperclass()) {
+ Type type = c.getGenericSuperclass();
+ if (type instanceof ParameterizedType) {
+ Type first =
+ ((ParameterizedType) type).getActualTypeArguments()[0];
+ if (first instanceof Class) {
+ return (Class>) first;
+ }
+ }
+ }
+ assertNotNull("No Element Data type found for " + engine, null);
+ return null;
+ }
+}
diff --git a/ip-intelligence/ip-intelligence.engine.on-premise/src/main/java/fiftyone/ipintelligence/engine/onpremise/flowelements/IPIntelligenceOnPremiseEngineBuilder.java b/ip-intelligence/ip-intelligence.engine.on-premise/src/main/java/fiftyone/ipintelligence/engine/onpremise/flowelements/IPIntelligenceOnPremiseEngineBuilder.java
index cf0b071fe..cd7a9010e 100644
--- a/ip-intelligence/ip-intelligence.engine.on-premise/src/main/java/fiftyone/ipintelligence/engine/onpremise/flowelements/IPIntelligenceOnPremiseEngineBuilder.java
+++ b/ip-intelligence/ip-intelligence.engine.on-premise/src/main/java/fiftyone/ipintelligence/engine/onpremise/flowelements/IPIntelligenceOnPremiseEngineBuilder.java
@@ -159,6 +159,7 @@ public IPIntelligenceOnPremiseEngineBuilder setPerformanceProfile(
}
}
+ @DefaultValue("Balanced")
@Override
public IPIntelligenceOnPremiseEngineBuilder setPerformanceProfile(
PerformanceProfiles profile) {
@@ -201,6 +202,7 @@ public IPIntelligenceOnPremiseEngineBuilder setConcurrency(int concurrency) {
return this;
}
+ @DefaultValue("No results cache, and none can be set for this engine")
@Override
public IPIntelligenceOnPremiseEngineBuilder setCache(
CacheConfiguration cacheConfiguration) {
diff --git a/ip-intelligence/ip-intelligence.engine.on-premise/src/test/java/fiftyone/ipintelligence/engine/onpremise/flowelements/AngleRoundingTests.java b/ip-intelligence/ip-intelligence.engine.on-premise/src/test/java/fiftyone/ipintelligence/engine/onpremise/flowelements/AngleRoundingTests.java
new file mode 100644
index 000000000..3d26cf1ae
--- /dev/null
+++ b/ip-intelligence/ip-intelligence.engine.on-premise/src/test/java/fiftyone/ipintelligence/engine/onpremise/flowelements/AngleRoundingTests.java
@@ -0,0 +1,188 @@
+/* *********************************************************************
+ * 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.ipintelligence.engine.onpremise.flowelements;
+
+import fiftyone.ipintelligence.shared.IPIntelligenceData;
+import fiftyone.ipintelligence.shared.IPIntelligenceDataBaseOnPremise;
+import fiftyone.ipintelligence.shared.testhelpers.FileUtils;
+import fiftyone.pipeline.core.data.FlowData;
+import fiftyone.pipeline.core.data.IWeightedValue;
+import fiftyone.pipeline.core.flowelements.Pipeline;
+import fiftyone.pipeline.core.flowelements.PipelineBuilder;
+import fiftyone.pipeline.engines.Constants;
+import fiftyone.pipeline.engines.data.AspectPropertyValue;
+import org.junit.Test;
+import org.slf4j.ILoggerFactory;
+import org.slf4j.LoggerFactory;
+
+import java.io.File;
+import java.util.Arrays;
+import java.util.List;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assume.assumeTrue;
+
+/**
+ * Latitude and Longitude are stored as Declination and Azimuth, so every
+ * form they are given in is rounded to 3 decimal places. See
+ * value-types.md line 99 :
+ * "A value converted from Azimuth or Declination MUST be given to the
+ * caller rounded to 3 decimal places, whether it is given as a number, as a
+ * string or as a point in a WKT shape."
+ *
+ * The free Lite file has no Latitude or Longitude, so these tests need a
+ * paid data file and are skipped without one.
+ */
+public class AngleRoundingTests {
+
+ private static final ILoggerFactory loggerFactory =
+ LoggerFactory.getILoggerFactory();
+
+ private static final List ANGLES =
+ Arrays.asList("Latitude", "Longitude");
+
+ /**
+ * Addresses in different parts of the world, so that the values are
+ * not all whole numbers.
+ */
+ private static final List ADDRESSES = Arrays.asList(
+ "185.28.167.77",
+ "116.154.188.222",
+ "45.236.48.61",
+ "2001:4860:4860::8888");
+
+ private interface Check {
+ void accept(IPIntelligenceData data) throws Exception;
+ }
+
+ private static void forEachAddress(Check check) throws Exception {
+ File file = FileUtils.getHashFile();
+ assumeTrue("No IP Intelligence data file was found.", file != null);
+ try (IPIntelligenceOnPremiseEngine engine =
+ new IPIntelligenceOnPremiseEngineBuilder(loggerFactory, null)
+ .setPerformanceProfile(
+ Constants.PerformanceProfiles.MaxPerformance)
+ .setAutoUpdate(false)
+ .setDataFileSystemWatcher(false)
+ .build(file.toString(), false);
+ Pipeline pipeline = new PipelineBuilder(loggerFactory)
+ .addFlowElement(engine)
+ .build()) {
+ for (String angle : ANGLES) {
+ assumeTrue(
+ "The data file has no " + angle + ".",
+ engine.getProperties().stream().anyMatch(
+ p -> p.getName().equalsIgnoreCase(angle)));
+ }
+ for (String address : ADDRESSES) {
+ try (FlowData flowData = pipeline.createFlowData()) {
+ flowData.addEvidence("query.client-ip", address);
+ flowData.process();
+ check.accept(flowData.get(IPIntelligenceData.class));
+ }
+ }
+ }
+ }
+
+ private static void assertRounded(String angle, double value) {
+ double rounded = Math.round(value * 1000d) / 1000d;
+ assertEquals(
+ angle + " " + value + " is not rounded to 3 decimal places.",
+ rounded,
+ value,
+ 0d);
+ }
+
+ private static void assertRounded(String angle, float value) {
+ float rounded = (float) (Math.round(value * 1000d) / 1000d);
+ assertEquals(
+ angle + " " + value + " is not rounded to 3 decimal places.",
+ rounded,
+ value,
+ 0f);
+ }
+
+ /**
+ * A weighted list of doubles gives each value rounded.
+ */
+ @Test
+ public void weightedDoubles_AreRounded() throws Exception {
+ forEachAddress(data -> {
+ for (String angle : ANGLES) {
+ for (Object value : weighted(data, angle, Double.class)) {
+ assertRounded(angle, (double) (Double) value);
+ }
+ }
+ });
+ }
+
+ /**
+ * The values of a weighted list, read through the public getValues.
+ */
+ private static List weighted(
+ IPIntelligenceData data, String angle, Class> type) {
+ AspectPropertyValue> values =
+ ((IPIntelligenceDataBaseOnPremise) data).getValues(
+ angle,
+ new Class>[] { List.class, IWeightedValue.class, type });
+ List result = new java.util.ArrayList<>();
+ if (values != null && values.hasValue()) {
+ for (Object value : values.getValue()) {
+ result.add(((IWeightedValue>) value).getValue());
+ }
+ }
+ return result;
+ }
+
+ /**
+ * A weighted list of floats gives each value rounded.
+ */
+ @Test
+ public void weightedFloats_AreRounded() throws Exception {
+ forEachAddress(data -> {
+ for (String angle : ANGLES) {
+ for (Object value : weighted(data, angle, Float.class)) {
+ assertRounded(angle, (float) (Float) value);
+ }
+ }
+ });
+ }
+
+ /**
+ * The single number is rounded. The text the native results give is
+ * not reachable through a public accessor, so it is not checked here.
+ */
+ @Test
+ public void number_IsRounded() throws Exception {
+ forEachAddress(data -> {
+ AspectPropertyValue latitude = data.getLatitude();
+ if (latitude.hasValue()) {
+ assertRounded("Latitude", latitude.getValue());
+ }
+ AspectPropertyValue longitude = data.getLongitude();
+ if (longitude.hasValue()) {
+ assertRounded("Longitude", longitude.getValue());
+ }
+ });
+ }
+}
diff --git a/ip-intelligence/ip-intelligence.shared/src/main/java/fiftyone/ipintelligence/shared/IPIntelligenceDataBaseOnPremise.java b/ip-intelligence/ip-intelligence.shared/src/main/java/fiftyone/ipintelligence/shared/IPIntelligenceDataBaseOnPremise.java
index 6a781ae87..fb5dd54a8 100644
--- a/ip-intelligence/ip-intelligence.shared/src/main/java/fiftyone/ipintelligence/shared/IPIntelligenceDataBaseOnPremise.java
+++ b/ip-intelligence/ip-intelligence.shared/src/main/java/fiftyone/ipintelligence/shared/IPIntelligenceDataBaseOnPremise.java
@@ -49,6 +49,12 @@
/**
* Base class used for all 51Degrees on-premise IP Intelligence results classes.
+ *
+ * An instance reads its values from native results, and reading a value
+ * writes into them, so an instance is not safe to use from several threads
+ * at once. The caller is responsible for thread-safe access, for example by
+ * wrapping its calls in a lock of its own. See the
+ * thread safety specification .
*/
public abstract class IPIntelligenceDataBaseOnPremise extends IPIntelligenceDataBase
implements ProvidesValuesAsString {
diff --git a/ip-intelligence/ip-intelligence.translation/src/main/java/fiftyone/ipintelligence/translation/flowelements/CountryCodeTranslationEngineBuilder.java b/ip-intelligence/ip-intelligence.translation/src/main/java/fiftyone/ipintelligence/translation/flowelements/CountryCodeTranslationEngineBuilder.java
index 0a23fe9aa..affeec009 100644
--- a/ip-intelligence/ip-intelligence.translation/src/main/java/fiftyone/ipintelligence/translation/flowelements/CountryCodeTranslationEngineBuilder.java
+++ b/ip-intelligence/ip-intelligence.translation/src/main/java/fiftyone/ipintelligence/translation/flowelements/CountryCodeTranslationEngineBuilder.java
@@ -24,6 +24,7 @@
import fiftyone.ipintelligence.translation.Constants;
import fiftyone.ipintelligence.translation.data.CountryCodeTranslationData;
+import fiftyone.pipeline.annotations.DefaultValue;
import fiftyone.pipeline.annotations.ElementBuilder;
import org.slf4j.ILoggerFactory;
import org.slf4j.LoggerFactory;
@@ -61,6 +62,7 @@ public CountryCodeTranslationEngineBuilder(ILoggerFactory loggerFactory) {
* @param sourceElementDataKey the IP Intelligence element data key
* @return this builder
*/
+ @DefaultValue(Constants.IP_INTELLIGENCE_KEY)
public CountryCodeTranslationEngineBuilder setSourceElementDataKey(
String sourceElementDataKey) {
this.sourceElementDataKey = sourceElementDataKey;
diff --git a/ip-intelligence/ip-intelligence.translation/src/test/java/fiftyone/ipintelligence/translation/BuilderDefaultValueTests.java b/ip-intelligence/ip-intelligence.translation/src/test/java/fiftyone/ipintelligence/translation/BuilderDefaultValueTests.java
new file mode 100644
index 000000000..508924ca5
--- /dev/null
+++ b/ip-intelligence/ip-intelligence.translation/src/test/java/fiftyone/ipintelligence/translation/BuilderDefaultValueTests.java
@@ -0,0 +1,63 @@
+/* *********************************************************************
+ * 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.ipintelligence.translation;
+
+import fiftyone.ipintelligence.translation.flowelements.CountryCodeTranslationEngineBuilder;
+import fiftyone.pipeline.annotations.DefaultValue;
+import org.junit.Test;
+
+import java.lang.reflect.Method;
+import java.lang.reflect.Modifier;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+import static org.junit.Assert.assertEquals;
+
+/**
+ * Every configuration setter of the country code translation engine
+ * builder gives its default value beside it, with {@link DefaultValue}.
+ * See
+ * reference-implementation-notes.md line 79 :
+ * "In all cases, there SHOULD be some mechanism to specify the default
+ * values alongside configuration property setters."
+ */
+public class BuilderDefaultValueTests {
+
+ @Test
+ public void builderSetters_GiveTheirDefaults() {
+ List missing = new ArrayList<>();
+ for (Method method :
+ CountryCodeTranslationEngineBuilder.class.getDeclaredMethods()) {
+ if (Modifier.isPublic(method.getModifiers()) &&
+ method.getName().startsWith("set") &&
+ method.isSynthetic() == false &&
+ method.isBridge() == false &&
+ method.getAnnotation(DefaultValue.class) == null) {
+ missing.add(method.getName() +
+ Arrays.toString(method.getParameterTypes()));
+ }
+ }
+ assertEquals(new ArrayList(), missing);
+ }
+}
diff --git a/ip-intelligence/ip-intelligence/src/main/java/fiftyone/ipintelligence/IPIntelligenceOnPremisePipelineBuilder.java b/ip-intelligence/ip-intelligence/src/main/java/fiftyone/ipintelligence/IPIntelligenceOnPremisePipelineBuilder.java
index 498ec66ed..52f455e39 100644
--- a/ip-intelligence/ip-intelligence/src/main/java/fiftyone/ipintelligence/IPIntelligenceOnPremisePipelineBuilder.java
+++ b/ip-intelligence/ip-intelligence/src/main/java/fiftyone/ipintelligence/IPIntelligenceOnPremisePipelineBuilder.java
@@ -24,6 +24,7 @@
import fiftyone.ipintelligence.engine.onpremise.flowelements.IPIntelligenceOnPremiseEngine;
import fiftyone.ipintelligence.engine.onpremise.flowelements.IPIntelligenceOnPremiseEngineBuilder;
+import fiftyone.pipeline.annotations.DefaultValue;
import fiftyone.pipeline.core.exceptions.PipelineConfigurationException;
import fiftyone.pipeline.core.flowelements.FlowElement;
import fiftyone.pipeline.core.flowelements.Pipeline;
@@ -115,6 +116,8 @@ IPIntelligenceOnPremisePipelineBuilder setFilename(
* @param enabled True to enable usage sharing. False to disable.
* @return This builder instance.
*/
+ @DefaultValue(booleanValue = fiftyone.pipeline.engines.fiftyone
+ .flowelements.Constants.SHARE_USAGE_DEFAULT_ENABLED)
public IPIntelligenceOnPremisePipelineBuilder setShareUsage(
boolean enabled) {
shareUsageEnabled = enabled;
@@ -129,6 +132,7 @@ public IPIntelligenceOnPremisePipelineBuilder setShareUsage(
* @param enabled True to enable auto update. False to disable.
* @return This builder instance.
*/
+ @DefaultValue(booleanValue = Constants.DEFAULT_AUTOUPDATE_ENABLED)
public IPIntelligenceOnPremisePipelineBuilder setAutoUpdate(
boolean enabled) {
autoUpdateEnabled = enabled;
@@ -143,6 +147,7 @@ public IPIntelligenceOnPremisePipelineBuilder setAutoUpdate(
* @param enabled True to enable file system watcher. False to disable.
* @return This builder instance.
*/
+ @DefaultValue(booleanValue = Constants.DEFAULT_WATCHER_ENABLED)
public IPIntelligenceOnPremisePipelineBuilder setDataFileSystemWatcher(
boolean enabled) {
dataFileSystemWatcher = enabled;
@@ -154,6 +159,7 @@ public IPIntelligenceOnPremisePipelineBuilder setDataFileSystemWatcher(
* @param dataUpdateService an instance of a dataUpdateService
* @return This builder instance.
*/
+ @DefaultValue("The DataUpdateService given to the constructor")
public IPIntelligenceOnPremisePipelineBuilder setDataUpdateService(
DataUpdateService dataUpdateService) {
this.dataUpdateService = dataUpdateService;
@@ -162,7 +168,7 @@ public IPIntelligenceOnPremisePipelineBuilder setDataUpdateService(
/**
* Enable/Disable update on startup.
- * Defaults to enabled.
+ * Defaults to disabled.
* If enabled, the auto update system will be used to check for
* an update before the IP Intelligence engine is created.
* If an update is available, it will be downloaded and applied
@@ -171,6 +177,7 @@ public IPIntelligenceOnPremisePipelineBuilder setDataUpdateService(
* @param enabled True to enable update on startup. False to disable.
* @return This builder instance.
*/
+ @DefaultValue(booleanValue = Constants.DEFAULT_UPDATE_ON_STARTUP)
public IPIntelligenceOnPremisePipelineBuilder setDataUpdateOnStartup(
boolean enabled) {
dataUpdateOnStartup = enabled;
@@ -184,6 +191,7 @@ public IPIntelligenceOnPremisePipelineBuilder setDataUpdateOnStartup(
* @param pollingIntervalSeconds The number of seconds between checks.
* @return This builder instance.
*/
+ @DefaultValue(intValue = Constants.DATA_UPDATE_POLLING_DEFAULT)
public IPIntelligenceOnPremisePipelineBuilder setUpdatePollingInterval(
int pollingIntervalSeconds) {
@@ -197,6 +205,7 @@ public IPIntelligenceOnPremisePipelineBuilder setUpdatePollingInterval(
* @param pollingIntervalMillis The number of milliseconds between checks.
* @return This builder instance.
*/
+ @DefaultValue(intValue = Constants.DATA_UPDATE_POLLING_DEFAULT * 1000)
public IPIntelligenceOnPremisePipelineBuilder setUpdatePollingIntervalMillis(
long pollingIntervalMillis) {
updatePollingInterval = pollingIntervalMillis;
@@ -211,6 +220,7 @@ public IPIntelligenceOnPremisePipelineBuilder setUpdatePollingIntervalMillis(
* polling interval in seconds.
* @return This builder instance.
*/
+ @DefaultValue(intValue = Constants.DATA_UPDATE_RANDOMISATION_DEFAULT)
public IPIntelligenceOnPremisePipelineBuilder setUpdateRandomisationMax(
int randomisationMaxSeconds) {
updateRandomisationMax = (long)randomisationMaxSeconds * 1000;
@@ -225,6 +235,7 @@ public IPIntelligenceOnPremisePipelineBuilder setUpdateRandomisationMax(
* polling interval in milliseconds.
* @return This builder instance.
*/
+ @DefaultValue(intValue = Constants.DATA_UPDATE_RANDOMISATION_DEFAULT * 1000)
public IPIntelligenceOnPremisePipelineBuilder setUpdateRandomisationMaxMillis(
long randomisationMaxMillis) {
updateRandomisationMax = randomisationMaxMillis;
@@ -238,6 +249,7 @@ public IPIntelligenceOnPremisePipelineBuilder setUpdateRandomisationMaxMillis(
* @param key The license key.
* @return This builder instance.
*/
+ @DefaultValue("No license key")
public IPIntelligenceOnPremisePipelineBuilder setDataUpdateLicenseKey(
String key) {
dataUpdateLicenseKey = key;
@@ -251,6 +263,7 @@ public IPIntelligenceOnPremisePipelineBuilder setDataUpdateLicenseKey(
* @param profile The performance profile to use.
* @return This builder instance.
*/
+ @DefaultValue("Balanced")
public IPIntelligenceOnPremisePipelineBuilder setPerformanceProfile(
Constants.PerformanceProfiles profile) {
performanceProfile = profile;
@@ -264,6 +277,7 @@ public IPIntelligenceOnPremisePipelineBuilder setPerformanceProfile(
* @param concurrency expected concurrent accesses
* @return this builder
*/
+ @DefaultValue("The result of Runtime#getRuntime().getAvailableProcessors()")
public IPIntelligenceOnPremisePipelineBuilder setConcurrency(int concurrency) {
this.concurrency = concurrency;
return this;
@@ -275,6 +289,7 @@ public IPIntelligenceOnPremisePipelineBuilder setConcurrency(int concurrency) {
* @param property the property that we want the engine to populate
* @return this builder
*/
+ @DefaultValue("All properties")
public IPIntelligenceOnPremisePipelineBuilder setProperty(String property) {
this.properties.add(property);
return this;
@@ -289,6 +304,7 @@ public IPIntelligenceOnPremisePipelineBuilder setProperty(String property) {
* @param url the URL to check for a new data file
* @return this builder
*/
+ @DefaultValue("51Degrees default, set by the engine's builder")
public IPIntelligenceOnPremisePipelineBuilder setDataUpdateUrl(String url) {
dataUpdateUrl = url;
return this;
@@ -305,6 +321,7 @@ public IPIntelligenceOnPremisePipelineBuilder setDataUpdateUrl(String url) {
* False otherwise
* @return this builder
*/
+ @DefaultValue(booleanValue = Constants.DEFAULT_VERIFY_MD5)
public IPIntelligenceOnPremisePipelineBuilder setDataUpdateVerifyMd5(Boolean verify) {
dataUpdateVerifyMd5 = verify;
return this;
@@ -318,6 +335,7 @@ public IPIntelligenceOnPremisePipelineBuilder setDataUpdateVerifyMd5(Boolean ver
* @param formatter the formatter to use
* @return this builder
*/
+ @DefaultValue("51Degrees default, set by the engine's builder")
public IPIntelligenceOnPremisePipelineBuilder setDataUpdateUrlFormatter(DataUpdateUrlFormatter formatter) {
dataUpdateUrlFormatter = formatter;
return this;
diff --git a/ip-intelligence/ip-intelligence/src/test/java/fiftyone/ipintelligence/BuilderDefaultValueTests.java b/ip-intelligence/ip-intelligence/src/test/java/fiftyone/ipintelligence/BuilderDefaultValueTests.java
new file mode 100644
index 000000000..96ee86770
--- /dev/null
+++ b/ip-intelligence/ip-intelligence/src/test/java/fiftyone/ipintelligence/BuilderDefaultValueTests.java
@@ -0,0 +1,70 @@
+/* *********************************************************************
+ * 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.ipintelligence;
+
+import fiftyone.ipintelligence.engine.onpremise.flowelements.IPIntelligenceOnPremiseEngineBuilder;
+import fiftyone.pipeline.annotations.DefaultValue;
+import org.junit.Test;
+
+import java.lang.reflect.Method;
+import java.lang.reflect.Modifier;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+import static org.junit.Assert.assertEquals;
+
+/**
+ * Every configuration setter of the IP Intelligence on-premise builders
+ * gives its default value beside it, with {@link DefaultValue}. See
+ * reference-implementation-notes.md line 79 :
+ * "In all cases, there SHOULD be some mechanism to specify the default
+ * values alongside configuration property setters."
+ */
+public class BuilderDefaultValueTests {
+
+ static List missingDefaults(Class>... builders) {
+ List missing = new ArrayList<>();
+ for (Class> builder : builders) {
+ for (Method method : builder.getDeclaredMethods()) {
+ if (Modifier.isPublic(method.getModifiers()) &&
+ method.getName().startsWith("set") &&
+ method.isSynthetic() == false &&
+ method.isBridge() == false &&
+ method.getAnnotation(DefaultValue.class) == null) {
+ missing.add(builder.getSimpleName() + "#" +
+ method.getName() +
+ Arrays.toString(method.getParameterTypes()));
+ }
+ }
+ }
+ return missing;
+ }
+
+ @Test
+ public void builderSetters_GiveTheirDefaults() {
+ assertEquals(new ArrayList(), missingDefaults(
+ IPIntelligenceOnPremisePipelineBuilder.class,
+ IPIntelligenceOnPremiseEngineBuilder.class));
+ }
+}
diff --git a/ip-intelligence/ip-intelligence/src/test/java/fiftyone/ipintelligence/CloudOnPremiseSubstitutionTests.java b/ip-intelligence/ip-intelligence/src/test/java/fiftyone/ipintelligence/CloudOnPremiseSubstitutionTests.java
new file mode 100644
index 000000000..352ebf27f
--- /dev/null
+++ b/ip-intelligence/ip-intelligence/src/test/java/fiftyone/ipintelligence/CloudOnPremiseSubstitutionTests.java
@@ -0,0 +1,86 @@
+/* *********************************************************************
+ * 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.ipintelligence;
+
+import fiftyone.ipintelligence.cloud.flowelements.IPIntelligenceCloudEngine;
+import fiftyone.ipintelligence.engine.onpremise.flowelements.IPIntelligenceOnPremiseEngine;
+import fiftyone.ipintelligence.shared.IPIntelligenceData;
+import fiftyone.pipeline.core.flowelements.FlowElement;
+import org.junit.Test;
+
+import java.lang.reflect.ParameterizedType;
+import java.lang.reflect.Type;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+import static org.mockito.Mockito.CALLS_REAL_METHODS;
+import static org.mockito.Mockito.mock;
+
+/**
+ * The cloud and on-premise IP Intelligence engines put their results in
+ * Flow Data under the same key and as the same interface, so a consuming
+ * application reads them in the same way whichever engine is in the
+ * Pipeline. See
+ * README.md line 95 :
+ * "Where both Cloud and On-Premise variants of an Engine are available,
+ * they MUST arrange that the Properties and values produced are compatible
+ * with each other so that the Engines can be substituted in the Pipeline
+ * without alteration to the consuming application."
+ */
+public class CloudOnPremiseSubstitutionTests {
+
+ @Test
+ public void elementDataKey_IsTheSame() {
+ FlowElement, ?> cloud =
+ mock(IPIntelligenceCloudEngine.class, CALLS_REAL_METHODS);
+ FlowElement, ?> onPremise =
+ mock(IPIntelligenceOnPremiseEngine.class, CALLS_REAL_METHODS);
+ assertEquals(
+ onPremise.getElementDataKey(),
+ cloud.getElementDataKey());
+ }
+
+ @Test
+ public void elementData_IsIPIntelligenceData() {
+ assertTrue(IPIntelligenceData.class.isAssignableFrom(
+ elementDataType(IPIntelligenceCloudEngine.class)));
+ assertTrue(IPIntelligenceData.class.isAssignableFrom(
+ elementDataType(IPIntelligenceOnPremiseEngine.class)));
+ }
+
+ private static Class> elementDataType(Class> engine) {
+ for (Class> c = engine; c != null; c = c.getSuperclass()) {
+ Type type = c.getGenericSuperclass();
+ if (type instanceof ParameterizedType) {
+ Type first =
+ ((ParameterizedType) type).getActualTypeArguments()[0];
+ if (first instanceof Class) {
+ return (Class>) first;
+ }
+ }
+ }
+ assertNotNull("No Element Data type found for " + engine, null);
+ return null;
+ }
+}
diff --git a/pipeline/pipeline.cloudrequestengine/src/main/java/fiftyone/pipeline/cloudrequestengine/flowelements/CloudRequestDataInternal.java b/pipeline/pipeline.cloudrequestengine/src/main/java/fiftyone/pipeline/cloudrequestengine/flowelements/CloudRequestDataInternal.java
index b3145b2b8..92a5c9760 100644
--- a/pipeline/pipeline.cloudrequestengine/src/main/java/fiftyone/pipeline/cloudrequestengine/flowelements/CloudRequestDataInternal.java
+++ b/pipeline/pipeline.cloudrequestengine/src/main/java/fiftyone/pipeline/cloudrequestengine/flowelements/CloudRequestDataInternal.java
@@ -39,6 +39,17 @@ public String getJsonResponse() {
return null;
}
+ /**
+ * The JSON response as it is stored now, without waiting for lazy
+ * loading processing to finish, so the engine can check it from inside
+ * that processing.
+ * @return the JSON response, or null if none has been stored
+ */
+ String getJsonResponseWithoutWaiting() {
+ Object response = getWithoutWaiting(JSON_RESPONSE_KEY);
+ return response == null ? null : response.toString();
+ }
+
public CloudRequestDataInternal(
Logger logger,
diff --git a/pipeline/pipeline.cloudrequestengine/src/main/java/fiftyone/pipeline/cloudrequestengine/flowelements/CloudRequestEngineBuilder.java b/pipeline/pipeline.cloudrequestengine/src/main/java/fiftyone/pipeline/cloudrequestengine/flowelements/CloudRequestEngineBuilder.java
index b7235cac3..b40f4db91 100644
--- a/pipeline/pipeline.cloudrequestengine/src/main/java/fiftyone/pipeline/cloudrequestengine/flowelements/CloudRequestEngineBuilder.java
+++ b/pipeline/pipeline.cloudrequestengine/src/main/java/fiftyone/pipeline/cloudrequestengine/flowelements/CloudRequestEngineBuilder.java
@@ -138,6 +138,7 @@ protected CloudRequestEngine newEngine(List properties) throws Exception
* @param state the copy, or null to discover when building
* @return this builder
*/
+ @DefaultValue("None - discovered when the engine is built")
public CloudRequestEngineBuilder setDiscoveryState(
CloudDiscoveryState state) {
this.discoveryState = state;
@@ -245,7 +246,7 @@ public CloudRequestEngineBuilder setEndpoint(String uri) {
* @param uri data endpoint
* @return this builder
*/
- @DefaultValue(Constants.END_POINT_DEFAULT + "{resourcekey}.json")
+ @DefaultValue(Constants.END_POINT_DEFAULT + "/{resourcekey}.json")
public CloudRequestEngineBuilder setDataEndpoint(String uri) {
dataEndpoint = uri;
return this;
@@ -259,7 +260,7 @@ public CloudRequestEngineBuilder setDataEndpoint(String uri) {
* @param uri properties endpoint
* @return this builder
*/
- @DefaultValue(Constants.END_POINT_DEFAULT + "accessibleproperties")
+ @DefaultValue(Constants.END_POINT_DEFAULT + "/accessibleproperties")
public CloudRequestEngineBuilder setPropertiesEndpoint(String uri) {
propertiesEndpoint = uri;
return this;
@@ -273,7 +274,7 @@ public CloudRequestEngineBuilder setPropertiesEndpoint(String uri) {
* @param uri evidence keys endpoint
* @return this builder
*/
- @DefaultValue(Constants.END_POINT_DEFAULT + "evidencekeys")
+ @DefaultValue(Constants.END_POINT_DEFAULT + "/evidencekeys")
public CloudRequestEngineBuilder setEvidenceKeysEndpoint(String uri) {
evidenceKeysEndpoint = uri;
return this;
diff --git a/pipeline/pipeline.cloudrequestengine/src/main/java/fiftyone/pipeline/cloudrequestengine/flowelements/CloudRequestEngineDefault.java b/pipeline/pipeline.cloudrequestengine/src/main/java/fiftyone/pipeline/cloudrequestengine/flowelements/CloudRequestEngineDefault.java
index 8cca7a9d8..79e5479c2 100644
--- a/pipeline/pipeline.cloudrequestengine/src/main/java/fiftyone/pipeline/cloudrequestengine/flowelements/CloudRequestEngineDefault.java
+++ b/pipeline/pipeline.cloudrequestengine/src/main/java/fiftyone/pipeline/cloudrequestengine/flowelements/CloudRequestEngineDefault.java
@@ -457,6 +457,46 @@ private void completeDiscovery() throws IOException {
}
}
+ /**
+ * A response whose JSON has a top-level member named fodid, in any case,
+ * holds a 51Did created for the one request, so it is neither stored in
+ * nor served from the cache, and the request goes to the cloud service
+ * instead. See the
+ * caching specification :
+ * "A cache MUST NOT store a result that holds such a value, and MUST NOT
+ * serve one that it finds." A response that cannot be read as a JSON
+ * object is not cached either, as it cannot be checked.
+ *
+ * The response is read without waiting for lazy loading, so the check
+ * never makes process wait for the cloud service. With lazy loading it
+ * is made by the processing task once the response is stored.
+ * @param data the result to check
+ * @return false if the response has a top-level fodid member
+ */
+ @Override
+ protected boolean isCacheable(CloudRequestData data) {
+ String response =
+ ((CloudRequestDataInternal)data).getJsonResponseWithoutWaiting();
+ if (response == null) {
+ return false;
+ }
+ try {
+ for (String key : new JSONObject(response).keySet()) {
+ if (FODID.equalsIgnoreCase(key)) {
+ return false;
+ }
+ }
+ return true;
+ } catch (JSONException e) {
+ return false;
+ }
+ }
+
+ /**
+ * The top-level member of a cloud response that holds a 51Did.
+ */
+ private static final String FODID = "fodid";
+
@Override
protected void processEngine(FlowData data, CloudRequestData aspectData) throws IOException {
((CloudRequestDataInternal)aspectData).setProcessStarted(true);
diff --git a/pipeline/pipeline.cloudrequestengine/src/test/java/fiftyone/pipeline/cloudrequestengine/flowelements/BuilderDefaultValueTests.java b/pipeline/pipeline.cloudrequestengine/src/test/java/fiftyone/pipeline/cloudrequestengine/flowelements/BuilderDefaultValueTests.java
new file mode 100644
index 000000000..bfde8d476
--- /dev/null
+++ b/pipeline/pipeline.cloudrequestengine/src/test/java/fiftyone/pipeline/cloudrequestengine/flowelements/BuilderDefaultValueTests.java
@@ -0,0 +1,68 @@
+/* *********************************************************************
+ * 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.pipeline.cloudrequestengine.flowelements;
+
+import fiftyone.pipeline.annotations.DefaultValue;
+import org.junit.jupiter.api.Test;
+
+import java.lang.reflect.Method;
+import java.lang.reflect.Modifier;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+/**
+ * Every configuration setter of the Cloud Request Engine builder gives its
+ * default value beside it, with {@link DefaultValue}. See
+ * reference-implementation-notes.md line 79 :
+ * "In all cases, there SHOULD be some mechanism to specify the default
+ * values alongside configuration property setters."
+ */
+public class BuilderDefaultValueTests {
+
+ static List missingDefaults(Class>... builders) {
+ List missing = new ArrayList<>();
+ for (Class> builder : builders) {
+ for (Method method : builder.getDeclaredMethods()) {
+ if (Modifier.isPublic(method.getModifiers()) &&
+ method.getName().startsWith("set") &&
+ method.isSynthetic() == false &&
+ method.isBridge() == false &&
+ method.getAnnotation(DefaultValue.class) == null) {
+ missing.add(builder.getSimpleName() + "#" +
+ method.getName() +
+ Arrays.toString(method.getParameterTypes()));
+ }
+ }
+ }
+ return missing;
+ }
+
+ @Test
+ public void cloudRequestEngineBuilderSetters_GiveTheirDefaults() {
+ assertEquals(new ArrayList(),
+ missingDefaults(CloudRequestEngineBuilder.class));
+ }
+}
diff --git a/pipeline/pipeline.cloudrequestengine/src/test/java/fiftyone/pipeline/cloudrequestengine/flowelements/CloudRequestEngineCacheKeyCaseTests.java b/pipeline/pipeline.cloudrequestengine/src/test/java/fiftyone/pipeline/cloudrequestengine/flowelements/CloudRequestEngineCacheKeyCaseTests.java
new file mode 100644
index 000000000..2f001f8a0
--- /dev/null
+++ b/pipeline/pipeline.cloudrequestengine/src/test/java/fiftyone/pipeline/cloudrequestengine/flowelements/CloudRequestEngineCacheKeyCaseTests.java
@@ -0,0 +1,121 @@
+/* *********************************************************************
+ * 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.pipeline.cloudrequestengine.flowelements;
+
+import fiftyone.pipeline.cloudrequestengine.data.CloudRequestData;
+import fiftyone.pipeline.core.data.FlowData;
+import fiftyone.pipeline.core.flowelements.Pipeline;
+import fiftyone.pipeline.core.flowelements.PipelineBuilder;
+import org.json.JSONArray;
+import org.junit.jupiter.api.Test;
+
+import java.net.MalformedURLException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.anyMap;
+import static org.mockito.ArgumentMatchers.argThat;
+import static org.mockito.Mockito.doAnswer;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+
+/**
+ * An engine with a cache treats two requests whose Evidence differs only
+ * in the case of a key as the same request, and two whose Evidence differs
+ * only in the case of a value as different ones. See the Generation of keys
+ * section of
+ * caching.md :
+ * "Comparison of Evidence keys MUST be case-insensitive."
+ */
+public class CloudRequestEngineCacheKeyCaseTests
+ extends CloudRequestEngineTestsBase {
+
+ public CloudRequestEngineCacheKeyCaseTests() throws MalformedURLException {
+ super();
+ }
+
+ /**
+ * Process the two pieces of Evidence in turn with one engine that has a
+ * cache, and count the requests that reach the cloud service.
+ */
+ private int requestsFor(String[] first, String[] second) throws Exception {
+ evidenceKeysResponse =
+ new JSONArray(Arrays.asList("query.User-Agent")).toString();
+ configureMockedClient();
+ final List answers = new ArrayList<>(Arrays.asList(
+ "{'device':{'value':'1'}}",
+ "{'device':{'value':'2'}}"));
+ doAnswer(invocation -> {
+ java.net.HttpURLConnection connection = invocation.getArgument(0);
+ String path = connection.getURL().getPath();
+ if (path.endsWith("properties")) {
+ return accessiblePropertiesResponse;
+ }
+ if (path.endsWith("evidencekeys")) {
+ return evidenceKeysResponse;
+ }
+ return answers.remove(0);
+ }).when(httpClient).postData(any(), anyMap(), (byte[]) any());
+
+ CloudRequestEngine engine =
+ new CloudRequestEngineBuilder(loggerFactory, httpClient)
+ .setResourceKey("resourcekey")
+ .setCacheSize(10)
+ .build();
+ try (Pipeline pipeline = new PipelineBuilder(loggerFactory)
+ .addFlowElement(engine).build()) {
+ for (String[] evidence : new String[][] { first, second }) {
+ try (FlowData data = pipeline.createFlowData()) {
+ data.addEvidence(evidence[0], evidence[1]);
+ data.process();
+ data.get(CloudRequestData.class).getJsonResponse();
+ }
+ }
+ }
+ return 2 - answers.size();
+ }
+
+ @Test
+ public void keyInAnotherCase_IsServedFromTheCache() throws Exception {
+ assertEquals(1, requestsFor(
+ new String[] { "query.User-Agent", "iPhone" },
+ new String[] { "query.user-agent", "iPhone" }),
+ "A key in another case was not taken as the same Evidence.");
+ verify(httpClient, times(1)).postData(
+ argThat(c -> c.getURL().getPath().endsWith("properties") == false &&
+ c.getURL().getPath().endsWith("evidencekeys") == false),
+ any(),
+ any());
+ }
+
+ @Test
+ public void valueInAnotherCase_IsNotServedFromTheCache() throws Exception {
+ assertEquals(2, requestsFor(
+ new String[] { "query.User-Agent", "iPhone" },
+ new String[] { "query.User-Agent", "IPHONE" }),
+ "A value in another case was taken as the same Evidence.");
+ }
+}
diff --git a/pipeline/pipeline.cloudrequestengine/src/test/java/fiftyone/pipeline/cloudrequestengine/flowelements/CloudRequestEngineFodIdCacheTests.java b/pipeline/pipeline.cloudrequestengine/src/test/java/fiftyone/pipeline/cloudrequestengine/flowelements/CloudRequestEngineFodIdCacheTests.java
new file mode 100644
index 000000000..8d17244a5
--- /dev/null
+++ b/pipeline/pipeline.cloudrequestengine/src/test/java/fiftyone/pipeline/cloudrequestengine/flowelements/CloudRequestEngineFodIdCacheTests.java
@@ -0,0 +1,176 @@
+/* *********************************************************************
+ * 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.pipeline.cloudrequestengine.flowelements;
+
+import fiftyone.pipeline.cloudrequestengine.data.CloudRequestData;
+import fiftyone.pipeline.core.data.FlowData;
+import fiftyone.pipeline.core.flowelements.Pipeline;
+import fiftyone.pipeline.core.flowelements.PipelineBuilder;
+import fiftyone.pipeline.engines.caching.FlowCache;
+import org.json.JSONArray;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.ValueSource;
+
+import java.net.MalformedURLException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+import static org.junit.jupiter.api.Assertions.*;
+import static org.mockito.ArgumentMatchers.*;
+import static org.mockito.Mockito.*;
+
+/**
+ * A response that holds a value created for the one request that asked for
+ * it, such as a 51Did, is neither stored in nor served from the cache.
+ * See the Values created for one request section of
+ * caching.md :
+ * "A cache MUST NOT store a result that holds such a value, and MUST NOT
+ * serve one that it finds."
+ */
+public class CloudRequestEngineFodIdCacheTests
+ extends CloudRequestEngineTestsBase {
+
+ public CloudRequestEngineFodIdCacheTests() throws MalformedURLException {
+ super();
+ }
+
+ /**
+ * Process the same Evidence twice with an engine that has a cache, the
+ * service answering each request with the next response in the list.
+ * @param responses the responses, one a request
+ * @return the JSON each request's Flow Data holds, in order
+ */
+ private List processTwice(String... responses) throws Exception {
+ evidenceKeysResponse =
+ new JSONArray(Arrays.asList("query.User-Agent")).toString();
+ configureMockedClient();
+ final List remaining = new ArrayList<>(Arrays.asList(responses));
+ doAnswer(invocation -> {
+ java.net.HttpURLConnection connection = invocation.getArgument(0);
+ String path = connection.getURL().getPath();
+ if (path.endsWith("properties")) {
+ return accessiblePropertiesResponse;
+ }
+ if (path.endsWith("evidencekeys")) {
+ return evidenceKeysResponse;
+ }
+ return remaining.remove(0);
+ }).when(httpClient).postData(any(), anyMap(), (byte[]) any());
+
+ CloudRequestEngine engine =
+ new CloudRequestEngineBuilder(loggerFactory, httpClient)
+ .setResourceKey("resourcekey")
+ .setCacheSize(10)
+ .build();
+ List answers = new ArrayList<>();
+ try (Pipeline pipeline = new PipelineBuilder(loggerFactory)
+ .addFlowElement(engine).build()) {
+ for (int i = 0; i < 2; i++) {
+ try (FlowData data = pipeline.createFlowData()) {
+ data.addEvidence("query.User-Agent", "iPhone");
+ data.process();
+ answers.add(data.get(CloudRequestData.class)
+ .getJsonResponse());
+ }
+ }
+ }
+ return answers;
+ }
+
+ /**
+ * A response whose JSON has a top-level member named fodid, in any case,
+ * is not stored, so the second identical request goes to the cloud
+ * service and gets its own 51Did.
+ */
+ @ParameterizedTest
+ @ValueSource(strings = { "fodid", "FodId", "FODID" })
+ public void process_FodIdResponseIsNotStored(String name) throws Exception {
+ String first = "{'device':{'value':'1'},'" + name + "':'first'}";
+ String second = "{'device':{'value':'1'},'" + name + "':'second'}";
+
+ List answers = processTwice(first, second);
+
+ assertEquals(Arrays.asList(first, second), answers,
+ "A response with a 51Did was served from the cache.");
+ verify(httpClient, times(2)).postData(
+ argThat(c -> c.getURL().getPath().endsWith("properties") == false),
+ any(),
+ any());
+ }
+
+ /**
+ * Every other response is cached as before, including one whose fodid
+ * member is not at the top level.
+ */
+ @Test
+ public void process_ResponseWithoutTopLevelFodIdIsCached()
+ throws Exception {
+ String first = "{'device':{'value':'1','fodid':'nested'}}";
+
+ List answers = processTwice(first);
+
+ assertEquals(Arrays.asList(first, first), answers);
+ verify(httpClient, times(1)).postData(
+ argThat(c -> c.getURL().getPath().endsWith("properties") == false),
+ any(),
+ any());
+ }
+
+ /**
+ * A result with a 51Did that the cache already holds, such as one an
+ * earlier version stored, is not served. The request goes to the cloud
+ * service instead.
+ */
+ @Test
+ public void process_FodIdResultFoundInTheCacheIsNotServed()
+ throws Exception {
+ evidenceKeysResponse =
+ new JSONArray(Arrays.asList("query.User-Agent")).toString();
+ configureMockedClient();
+ CloudRequestEngine engine =
+ new CloudRequestEngineBuilder(loggerFactory, httpClient)
+ .setResourceKey("resourcekey")
+ .build();
+ FlowCache cache = mock(FlowCache.class);
+ engine.setCache(cache);
+
+ try (Pipeline pipeline = new PipelineBuilder(loggerFactory)
+ .addFlowElement(engine).build();
+ FlowData earlier = pipeline.createFlowData();
+ FlowData data = pipeline.createFlowData()) {
+ CloudRequestData cached = new CloudRequestData(
+ loggerFactory.getLogger("test"), earlier, engine);
+ cached.setJsonResponse(
+ "{'device':{'value':'1'},'fodid':'stale'}");
+ when(cache.get(any())).thenReturn(cached);
+ data.addEvidence("query.User-Agent", "iPhone");
+ data.process();
+ assertEquals(jsonResponse,
+ data.get(CloudRequestData.class).getJsonResponse(),
+ "A result with a 51Did found in the cache was served.");
+ verify(cache, never()).put(any(), same(cached));
+ }
+ }
+}
diff --git a/pipeline/pipeline.cloudrequestengine/src/test/java/fiftyone/pipeline/cloudrequestengine/flowelements/CloudRequestEngineLazyCacheTests.java b/pipeline/pipeline.cloudrequestengine/src/test/java/fiftyone/pipeline/cloudrequestengine/flowelements/CloudRequestEngineLazyCacheTests.java
new file mode 100644
index 000000000..2d96b05a2
--- /dev/null
+++ b/pipeline/pipeline.cloudrequestengine/src/test/java/fiftyone/pipeline/cloudrequestengine/flowelements/CloudRequestEngineLazyCacheTests.java
@@ -0,0 +1,291 @@
+/* *********************************************************************
+ * 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.pipeline.cloudrequestengine.flowelements;
+
+import fiftyone.caching.Cache;
+import fiftyone.caching.CacheBuilder;
+import fiftyone.caching.LruPutCache;
+import fiftyone.caching.PutCache;
+import fiftyone.pipeline.cloudrequestengine.data.CloudRequestData;
+import fiftyone.pipeline.core.data.FlowData;
+import fiftyone.pipeline.core.flowelements.Pipeline;
+import fiftyone.pipeline.core.flowelements.PipelineBuilder;
+import fiftyone.pipeline.engines.configuration.CacheConfiguration;
+import fiftyone.pipeline.engines.configuration.LazyLoadingConfiguration;
+import org.json.JSONArray;
+import org.junit.jupiter.api.Test;
+
+import java.net.HttpURLConnection;
+import java.net.MalformedURLException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.concurrent.atomic.AtomicReference;
+
+import static org.junit.jupiter.api.Assertions.*;
+import static org.mockito.ArgumentMatchers.*;
+import static org.mockito.Mockito.*;
+
+/**
+ * A lazy loading Cloud Request Engine with a cache returns from process
+ * without waiting for the cloud service, and still never serves a response
+ * with a 51Did from the cache. Lazy loading means "the Engine returns from process
+ * at once and the work is done on another thread", which the check of
+ * caching.md
+ * ("A cache MUST NOT store a result that holds such a value, and MUST NOT
+ * serve one that it finds.") must not undo.
+ *
+ * No result depends on timing: the cloud service's answer is held back until
+ * the test releases it, and the test reads the processing thread's stack to
+ * see whether it is waiting for that answer.
+ */
+public class CloudRequestEngineLazyCacheTests
+ extends CloudRequestEngineTestsBase {
+
+ /**
+ * Long enough that a process which waits for the held back answer is
+ * still waiting when the test looks.
+ */
+ private static final int LAZY_TIMEOUT_MILLIS = 600000;
+
+ public CloudRequestEngineLazyCacheTests() throws MalformedURLException {
+ super();
+ }
+
+ /**
+ * Mock the cloud service so that each data request takes the next
+ * response in the list, and waits for the gate first.
+ */
+ private void mockService(
+ final CountDownLatch called,
+ final CountDownLatch gate,
+ String... responses) throws Exception {
+ evidenceKeysResponse =
+ new JSONArray(Arrays.asList("query.User-Agent")).toString();
+ configureMockedClient();
+ final List remaining = new ArrayList<>(Arrays.asList(responses));
+ doAnswer(invocation -> {
+ HttpURLConnection connection = invocation.getArgument(0);
+ String path = connection.getURL().getPath();
+ if (path.endsWith("properties")) {
+ return accessiblePropertiesResponse;
+ }
+ if (path.endsWith("evidencekeys")) {
+ return evidenceKeysResponse;
+ }
+ called.countDown();
+ gate.await();
+ return remaining.remove(0);
+ }).when(httpClient).postData(any(), anyMap(), (byte[]) any());
+ }
+
+ private CloudRequestEngine buildLazyCachedEngine() throws Exception {
+ return new CloudRequestEngineBuilder(loggerFactory, httpClient)
+ .setResourceKey("resourcekey")
+ .setCacheSize(10)
+ .setLazyLoading(new LazyLoadingConfiguration(LAZY_TIMEOUT_MILLIS))
+ .build();
+ }
+
+ private static boolean waitingForProcessing(Thread thread) {
+ for (StackTraceElement frame : thread.getStackTrace()) {
+ if (frame.getMethodName().equals("waitOnAllProcessFutures") ||
+ frame.getClassName().endsWith("FutureTask") &&
+ frame.getMethodName().equals("get")) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ /**
+ * process returns while the cloud service has not answered. Before the
+ * fix the cache check read the response, which waited for it.
+ */
+ @Test
+ public void process_LazyCachedEngineDoesNotWaitForTheService()
+ throws Exception {
+ CountDownLatch called = new CountDownLatch(1);
+ CountDownLatch gate = new CountDownLatch(1);
+ mockService(called, gate, jsonResponse);
+ CloudRequestEngine engine = buildLazyCachedEngine();
+
+ try (Pipeline pipeline = new PipelineBuilder(loggerFactory)
+ .addFlowElement(engine).build();
+ FlowData data = pipeline.createFlowData()) {
+ data.addEvidence("query.User-Agent", "iPhone");
+ final AtomicBoolean returned = new AtomicBoolean(false);
+ final AtomicReference failure = new AtomicReference<>();
+ Thread processing = new Thread(() -> {
+ try {
+ data.process();
+ returned.set(true);
+ } catch (Throwable t) {
+ failure.set(t);
+ }
+ });
+ processing.start();
+ called.await();
+ // Look until process has returned, or is seen waiting for the
+ // answer that is held back.
+ boolean waited = false;
+ while (returned.get() == false && failure.get() == null &&
+ processing.isAlive()) {
+ if (waitingForProcessing(processing)) {
+ waited = true;
+ break;
+ }
+ Thread.yield();
+ }
+ gate.countDown();
+ processing.join();
+ assertNull(failure.get());
+ assertFalse(waited,
+ "process waited for the cloud service's answer.");
+ assertTrue(returned.get());
+ assertEquals(jsonResponse,
+ data.get(CloudRequestData.class).getJsonResponse());
+ }
+ }
+
+ /**
+ * With lazy loading, a response without a 51Did is still cached, so the
+ * second identical request is served from the cache.
+ */
+ @Test
+ public void process_LazyResponseWithoutFodIdIsCached() throws Exception {
+ CountDownLatch gate = new CountDownLatch(0);
+ String first = "{'device':{'value':'1'}}";
+ mockService(new CountDownLatch(1), gate, first);
+ CloudRequestEngine engine = buildLazyCachedEngine();
+
+ List answers = processTwice(engine);
+
+ assertEquals(Arrays.asList(first, first), answers);
+ verify(httpClient, times(1)).postData(
+ argThat(c -> c.getURL().getPath().endsWith("properties") == false &&
+ c.getURL().getPath().endsWith("evidencekeys") == false),
+ any(),
+ any());
+ }
+
+ /**
+ * With lazy loading, a response with a 51Did is not served from the
+ * cache, so the second identical request goes to the cloud service and
+ * gets its own. The result is stored before it is processed, so the
+ * check before serving is the one that keeps it out.
+ */
+ @Test
+ public void process_LazyFodIdResponseIsNotServed() throws Exception {
+ CountDownLatch gate = new CountDownLatch(0);
+ String first = "{'device':{'value':'1'},'fodid':'first'}";
+ String second = "{'device':{'value':'1'},'fodid':'second'}";
+ mockService(new CountDownLatch(2), gate, first, second);
+ CloudRequestEngine engine = buildLazyCachedEngine();
+
+ List answers = processTwice(engine);
+
+ assertEquals(Arrays.asList(first, second), answers,
+ "A response with a 51Did was served from the cache.");
+ }
+
+ /**
+ * With lazy loading, the result is offered to the cache on the thread
+ * that called process, so the processing task never reads the Flow Data
+ * to make the cache key once process has returned.
+ */
+ @Test
+ public void process_LazyResultIsStoredOnTheProcessingThread()
+ throws Exception {
+ mockService(new CountDownLatch(1), new CountDownLatch(0),
+ jsonResponse);
+ final List putThreads =
+ Collections.synchronizedList(new ArrayList<>());
+ CacheBuilder recording = new CacheBuilder() {
+ @Override
+ public Cache build(Cache c, int cacheSize) {
+ final PutCache inner =
+ new LruPutCache.Builder().build(c, cacheSize);
+ return new PutCache() {
+ @Override
+ public void put(K key, V value) {
+ putThreads.add(Thread.currentThread());
+ inner.put(key, value);
+ }
+
+ @Override
+ public V get(K key) {
+ return inner.get(key);
+ }
+
+ @Override
+ public void close() throws java.io.IOException {
+ inner.close();
+ }
+ };
+ }
+ };
+ CloudRequestEngine engine =
+ new CloudRequestEngineBuilder(loggerFactory, httpClient)
+ .setResourceKey("resourcekey")
+ .setCache(new CacheConfiguration(recording, 10))
+ .setLazyLoading(
+ new LazyLoadingConfiguration(LAZY_TIMEOUT_MILLIS))
+ .build();
+
+ try (Pipeline pipeline = new PipelineBuilder(loggerFactory)
+ .addFlowElement(engine).build();
+ FlowData data = pipeline.createFlowData()) {
+ data.addEvidence("query.User-Agent", "iPhone");
+ data.process();
+ // Waits for the processing task to finish.
+ assertEquals(jsonResponse,
+ data.get(CloudRequestData.class).getJsonResponse());
+ }
+
+ assertEquals(1, putThreads.size(),
+ "The result was not offered to the cache once.");
+ assertSame(Thread.currentThread(), putThreads.get(0),
+ "The processing task read the Flow Data to cache the result.");
+ }
+
+ private List processTwice(CloudRequestEngine engine)
+ throws Exception {
+ List answers = new ArrayList<>();
+ try (Pipeline pipeline = new PipelineBuilder(loggerFactory)
+ .addFlowElement(engine).build()) {
+ for (int i = 0; i < 2; i++) {
+ try (FlowData data = pipeline.createFlowData()) {
+ data.addEvidence("query.User-Agent", "iPhone");
+ data.process();
+ answers.add(data.get(CloudRequestData.class)
+ .getJsonResponse());
+ }
+ }
+ }
+ return answers;
+ }
+}
diff --git a/pipeline/pipeline.core/src/main/java/fiftyone/pipeline/core/data/FlowData.java b/pipeline/pipeline.core/src/main/java/fiftyone/pipeline/core/data/FlowData.java
index 6b60abfa9..0ad485465 100644
--- a/pipeline/pipeline.core/src/main/java/fiftyone/pipeline/core/data/FlowData.java
+++ b/pipeline/pipeline.core/src/main/java/fiftyone/pipeline/core/data/FlowData.java
@@ -134,29 +134,37 @@ public interface FlowData extends AutoCloseable {
* Get the {@link ElementData} for the requested aspect.
* @param key the aspect to get
* @param type type of the aspect
- * @return aspect's {@link ElementData} or null if not present
+ * @return aspect's {@link ElementData}
* @throws IllegalStateException if this FlowData has not been processed
+ * @throws IllegalArgumentException if the key is null
+ * @throws java.util.NoSuchElementException if no element added data
+ * under the key
*/
T get(TypedKey key)
- throws IllegalStateException;
+ throws IllegalStateException, IllegalArgumentException,
+ java.util.NoSuchElementException;
/**
* Get the {@link ElementData} for the requested data key.
* @param key to the aspect to get
- * @return aspect's {@link ElementData} or null if not present
+ * @return aspect's {@link ElementData}
* @throws IllegalStateException if this FlowData has not been processed
+ * @throws java.util.NoSuchElementException if no element added data
+ * under the key
*/
- ElementData get(String key) throws IllegalStateException;
+ ElementData get(String key)
+ throws IllegalStateException, java.util.NoSuchElementException;
/**
* Get the {@link ElementData} for the requested {@link ElementData}
- * implementation. If multiple instances of the type exist then an exception
- * is thrown.
+ * implementation.
* @param type the type of {@link ElementData} to return
* @param the type of {@link ElementData} to return
- * @return aspect's {@link ElementData} or null if the type is not present
+ * @return aspect's {@link ElementData}
+ * @throws PipelineDataException if no data of the type is present, or
+ * more than one instance is
*/
- T get(Class type);
+ T get(Class type) throws PipelineDataException;
/**
* Get the ElementData for the requested {@link FlowElement}. This method
@@ -167,9 +175,10 @@ T get(TypedKey key)
* @param {@link ElementData} stored by the element
* @return element data stored by the element
* @throws IllegalStateException if this FlowData has not been processed
+ * @throws IllegalArgumentException if the element is null
*/
T getFromElement(FlowElement element)
- throws IllegalStateException;
+ throws IllegalStateException, IllegalArgumentException;
/**
* Get the specified property as the specified type.
@@ -299,12 +308,16 @@ T getOrAdd(
Iterable elementDataAsIterable();
/**
- * Get all element data values that match the specified predicate.
+ * Get all element data values that match the specified predicate. The
+ * properties of an element that added no data, and properties with no
+ * value, are left out.
* @param matcher if a property passed to this matcher returns true then it
* will be included in the results
* @return all the element data values that match the matcher
+ * @throws IllegalStateException if this FlowData has not been processed
*/
- Map getWhere(PropertyMatcher matcher);
+ Map getWhere(PropertyMatcher matcher)
+ throws IllegalStateException;
/**
* Generate a {@link DataKey} that contains the evidence data from this
diff --git a/pipeline/pipeline.core/src/main/java/fiftyone/pipeline/core/flowelements/FlowDataDefault.java b/pipeline/pipeline.core/src/main/java/fiftyone/pipeline/core/flowelements/FlowDataDefault.java
index 604e11a30..e4edd3a92 100644
--- a/pipeline/pipeline.core/src/main/java/fiftyone/pipeline/core/flowelements/FlowDataDefault.java
+++ b/pipeline/pipeline.core/src/main/java/fiftyone/pipeline/core/flowelements/FlowDataDefault.java
@@ -362,8 +362,21 @@ public Iterable elementDataAsIterable() {
@Override
@SuppressWarnings("rawtypes")
public Map getWhere(PropertyMatcher matcher) {
+ if (processed.get() == false) {
+ throw new IllegalStateException(
+ "This instance has not yet been processed.");
+ }
Map map = new HashMap<>();
for (FlowElement element : pipeline.getFlowElements()) {
+ // An element that did not run, for example because an earlier
+ // one stopped processing, has no data, and a property may have
+ // no value. Neither is the caller's mistake, so both are left
+ // out rather than thrown. See
+ // https://github.com/51Degrees/specifications/blob/706de1f0cb3edd560eb7fd7ef3bb19b93a4883dc/pipeline-specification/features/exception-handling.md#L57
+ if (data.containsKey(element.getElementDataKey()) == false) {
+ continue;
+ }
+ ElementData elementData = get(element.getElementDataKey());
// We know the property has extends ElementPropertyMetaData as it
// is a constraint of the FlowElement interface, so don't check this
// cast
@@ -371,10 +384,14 @@ public Map getWhere(PropertyMatcher matcher) {
ElementPropertyMetaData property =
(ElementPropertyMetaData)propertyObject;
if (property.isAvailable() && matcher.isMatch(property)) {
- map.put(
- element.getElementDataKey() + "." + property.getName(),
- get(element.getElementDataKey())
- .get(property.getName().toLowerCase(Locale.ROOT)).toString());
+ Object value = elementData.get(
+ property.getName().toLowerCase(Locale.ROOT));
+ if (value != null) {
+ map.put(
+ element.getElementDataKey() + "." +
+ property.getName(),
+ value.toString());
+ }
}
}
}
diff --git a/pipeline/pipeline.core/src/main/java/fiftyone/pipeline/core/flowelements/Pipeline.java b/pipeline/pipeline.core/src/main/java/fiftyone/pipeline/core/flowelements/Pipeline.java
index 098f54fee..f36061939 100644
--- a/pipeline/pipeline.core/src/main/java/fiftyone/pipeline/core/flowelements/Pipeline.java
+++ b/pipeline/pipeline.core/src/main/java/fiftyone/pipeline/core/flowelements/Pipeline.java
@@ -61,6 +61,24 @@ public interface Pipeline extends AutoCloseable {
*/
boolean isConcurrent();
+ /**
+ * True if exceptions that happen while processing are recorded in
+ * {@link FlowData#getErrors()} and logged, rather than thrown once
+ * processing is complete. A wrapper of the Pipeline, such as the web
+ * integration, can read it to decide whether to suppress and log an
+ * exception of its own. See the
+ * exception handling specification .
+ *
+ * The default gives the builder's default, so that an implementation
+ * written before this method was added still compiles. Override it to
+ * give the real setting.
+ * @return true if exceptions during processing are suppressed
+ */
+ default boolean isSuppressProcessExceptions() {
+ return fiftyone.pipeline.core.Constants
+ .DEFAULT_SUPPRESS_PROCESS_EXCEPTIONS;
+ }
+
/**
* Indicates whether the Pipeline has been closed using the {@link #close()}
* method, either explicitly or as a result of a 'try with resource'.
diff --git a/pipeline/pipeline.core/src/main/java/fiftyone/pipeline/core/flowelements/PipelineBuilderBase.java b/pipeline/pipeline.core/src/main/java/fiftyone/pipeline/core/flowelements/PipelineBuilderBase.java
index 2145aae2a..8df57efad 100644
--- a/pipeline/pipeline.core/src/main/java/fiftyone/pipeline/core/flowelements/PipelineBuilderBase.java
+++ b/pipeline/pipeline.core/src/main/java/fiftyone/pipeline/core/flowelements/PipelineBuilderBase.java
@@ -23,6 +23,7 @@
package fiftyone.pipeline.core.flowelements;
import fiftyone.pipeline.annotations.AlternateName;
+import fiftyone.pipeline.annotations.DefaultValue;
import fiftyone.pipeline.core.exceptions.PipelineConfigurationException;
import fiftyone.pipeline.core.Constants;
import fiftyone.pipeline.core.configuration.PipelineOptions;
@@ -244,6 +245,7 @@ protected void addServicesToPipeline(Pipeline pipeline) {
* @return this builder
*/
@SuppressWarnings("unchecked")
+ @DefaultValue(booleanValue = Constants.DEFAULT_AUTO_CLOSE_ELEMENTS)
@AlternateName("SetAutoDisposeElements")
public T setAutoCloseElements(boolean autoClose) {
this.autoCloseElements = autoClose;
@@ -260,6 +262,7 @@ public T setAutoCloseElements(boolean autoClose) {
* @return this builder
*/
@SuppressWarnings("unchecked")
+ @DefaultValue(booleanValue = Constants.DEFAULT_SUPPRESS_PROCESS_EXCEPTIONS)
public T setSuppressProcessException(boolean suppress) {
this.suppressProcessExceptions = suppress;
return (T) this;
diff --git a/pipeline/pipeline.core/src/main/java/fiftyone/pipeline/core/flowelements/PipelineDefault.java b/pipeline/pipeline.core/src/main/java/fiftyone/pipeline/core/flowelements/PipelineDefault.java
index 302b51d94..93a8d4030 100644
--- a/pipeline/pipeline.core/src/main/java/fiftyone/pipeline/core/flowelements/PipelineDefault.java
+++ b/pipeline/pipeline.core/src/main/java/fiftyone/pipeline/core/flowelements/PipelineDefault.java
@@ -150,7 +150,11 @@ class PipelineDefault implements PipelineInternal {
boolean suppressProcessExceptions) {
this.logger = logger;
this.flowDataFactory = flowDataFactory;
- this.flowElements = flowElements;
+ // A copy, so that the builder adding to its own list later cannot
+ // change this Pipeline, which is immutable. See
+ // https://github.com/51Degrees/specifications/blob/706de1f0cb3edd560eb7fd7ef3bb19b93a4883dc/pipeline-specification/conceptual-overview.md#L118
+ this.flowElements = Collections.unmodifiableList(
+ new ArrayList<>(flowElements));
this.services = new ArrayList<>();
this.autoDisposeElements = autoDisposeElements;
this.suppressProcessExceptions = suppressProcessExceptions;
@@ -212,6 +216,11 @@ public boolean isConcurrent() {
return isConcurrent;
}
+ @Override
+ public boolean isSuppressProcessExceptions() {
+ return suppressProcessExceptions;
+ }
+
@Override
public boolean isClosed() {
return isClosed;
diff --git a/pipeline/pipeline.core/src/test/java/fiftyone/pipeline/core/flowelements/BuilderDefaultValueTests.java b/pipeline/pipeline.core/src/test/java/fiftyone/pipeline/core/flowelements/BuilderDefaultValueTests.java
new file mode 100644
index 000000000..f9159208e
--- /dev/null
+++ b/pipeline/pipeline.core/src/test/java/fiftyone/pipeline/core/flowelements/BuilderDefaultValueTests.java
@@ -0,0 +1,73 @@
+/* *********************************************************************
+ * 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.pipeline.core.flowelements;
+
+import fiftyone.pipeline.annotations.DefaultValue;
+import org.junit.Test;
+
+import java.lang.reflect.Method;
+import java.lang.reflect.Modifier;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+import static org.junit.Assert.assertEquals;
+
+/**
+ * Every configuration setter of the Pipeline builder gives its default
+ * value beside it, with {@link DefaultValue}. See
+ * reference-implementation-notes.md line 79 :
+ * "In all cases, there SHOULD be some mechanism to specify the default
+ * values alongside configuration property setters."
+ */
+public class BuilderDefaultValueTests {
+
+ /**
+ * The public setters declared by the builders that do not carry a
+ * {@link DefaultValue}.
+ */
+ static List missingDefaults(Class>... builders) {
+ List missing = new ArrayList<>();
+ for (Class> builder : builders) {
+ for (Method method : builder.getDeclaredMethods()) {
+ if (Modifier.isPublic(method.getModifiers()) &&
+ method.getName().startsWith("set") &&
+ method.isSynthetic() == false &&
+ method.isBridge() == false &&
+ method.getAnnotation(DefaultValue.class) == null) {
+ missing.add(builder.getSimpleName() + "#" +
+ method.getName() +
+ Arrays.toString(method.getParameterTypes()));
+ }
+ }
+ }
+ return missing;
+ }
+
+ @Test
+ public void pipelineBuilderSetters_GiveTheirDefaults() {
+ assertEquals(new ArrayList(), missingDefaults(
+ PipelineBuilderBase.class,
+ PipelineBuilder.class));
+ }
+}
diff --git a/pipeline/pipeline.core/src/test/java/fiftyone/pipeline/core/flowelements/CacheKeyTests.java b/pipeline/pipeline.core/src/test/java/fiftyone/pipeline/core/flowelements/CacheKeyTests.java
new file mode 100644
index 000000000..469dda135
--- /dev/null
+++ b/pipeline/pipeline.core/src/test/java/fiftyone/pipeline/core/flowelements/CacheKeyTests.java
@@ -0,0 +1,109 @@
+/* *********************************************************************
+ * 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.pipeline.core.flowelements;
+
+import fiftyone.pipeline.core.data.DataKey;
+import fiftyone.pipeline.core.data.EvidenceKeyFilter;
+import fiftyone.pipeline.core.data.FlowData;
+import fiftyone.pipeline.core.testclasses.flowelements.MultiplyByFiveElement;
+import org.junit.Test;
+import org.slf4j.ILoggerFactory;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.Map;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotEquals;
+import static org.mockito.Mockito.mock;
+
+/**
+ * Cache keys are built from the Evidence that the element advertises, with
+ * values compared with their case. See the Generation of keys section of
+ * caching.md .
+ */
+public class CacheKeyTests {
+
+ private final ILoggerFactory loggerFactory =
+ LoggerFactory.getILoggerFactory();
+
+ private final MultiplyByFiveElement element =
+ new MultiplyByFiveElement(mock(Logger.class));
+
+ private DataKey keyFor(Map evidence) throws Exception {
+ try (Pipeline pipeline = new PipelineBuilder(loggerFactory)
+ .addFlowElement(element)
+ .build();
+ FlowData flowData = pipeline.createFlowData()) {
+ flowData.addEvidence(evidence);
+ EvidenceKeyFilter filter = element.getEvidenceKeyFilter();
+ return flowData.generateKey(filter);
+ }
+ }
+
+ private static Map evidence(Object... pairs) {
+ Map map = new java.util.LinkedHashMap<>();
+ for (int i = 0; i < pairs.length; i += 2) {
+ map.put((String) pairs[i], pairs[i + 1]);
+ }
+ return map;
+ }
+
+ /**
+ * caching.md line 59 at 706de1f0: "All Flow Elements MUST advertize the
+ * Evidence keys that they make use of." The key is built from those keys
+ * alone, so Evidence the element does not use does not change it, and
+ * Evidence it uses does.
+ */
+ @Test
+ public void key_IsBuiltFromTheAdvertisedEvidenceOnly() throws Exception {
+ String used = element.evidenceKeys.get(0);
+ DataKey plain = keyFor(evidence(used, 2));
+ DataKey withOther = keyFor(evidence(used, 2, "query.other", "x"));
+ DataKey otherValue = keyFor(evidence(used, 3));
+
+ assertEquals(
+ "Evidence the element does not use changed the key.",
+ plain, withOther);
+ assertEquals(plain.hashCode(), withOther.hashCode());
+ assertNotEquals(
+ "Evidence the element uses did not change the key.",
+ plain, otherValue);
+ }
+
+ /**
+ * caching.md line 82 at 706de1f0: "Comparison of Evidence values MUST
+ * be case-sensitive."
+ */
+ @Test
+ public void values_AreComparedWithCase() throws Exception {
+ String used = element.evidenceKeys.get(0);
+ assertNotEquals(
+ "Two values that differ only in case gave the same key.",
+ keyFor(evidence(used, "abc")),
+ keyFor(evidence(used, "ABC")));
+ assertEquals(
+ keyFor(evidence(used, "abc")),
+ keyFor(evidence(used, "abc")));
+ }
+}
diff --git a/pipeline/pipeline.core/src/test/java/fiftyone/pipeline/core/flowelements/FlowDataAccessorErrorTests.java b/pipeline/pipeline.core/src/test/java/fiftyone/pipeline/core/flowelements/FlowDataAccessorErrorTests.java
new file mode 100644
index 000000000..0da3dcc19
--- /dev/null
+++ b/pipeline/pipeline.core/src/test/java/fiftyone/pipeline/core/flowelements/FlowDataAccessorErrorTests.java
@@ -0,0 +1,140 @@
+/* *********************************************************************
+ * 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.pipeline.core.flowelements;
+
+import fiftyone.pipeline.core.data.ElementData;
+import fiftyone.pipeline.core.data.ElementPropertyMetaData;
+import fiftyone.pipeline.core.data.ElementPropertyMetaDataDefault;
+import fiftyone.pipeline.core.data.FlowData;
+import org.junit.Test;
+import org.slf4j.ILoggerFactory;
+import org.slf4j.Logger;
+
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.anyString;
+import static org.mockito.Mockito.doAnswer;
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+/**
+ * FlowData's accessors throw only for something the caller did wrong. Data
+ * that is simply not there, because an element did not run or gave no
+ * value, is reported as absent rather than as an error. See
+ * exception-handling.md line 57 :
+ * "The general guidance is that these functions and accessors SHOULD only
+ * throw errors/exceptions if the failure is likely due to something that
+ * the API user has done wrong."
+ */
+public class FlowDataAccessorErrorTests {
+
+ private final ILoggerFactory loggerFactory = name -> mock(Logger.class);
+
+ /**
+ * An element with one available property, "value". When it runs it
+ * adds Element Data holding the given value; when writes is false it
+ * adds nothing, as an element that did not run.
+ */
+ @SuppressWarnings({ "unchecked", "rawtypes" })
+ private static FlowElement, ?> element(boolean writes, Object value)
+ throws Exception {
+ FlowElement element = mock(FlowElement.class);
+ ElementPropertyMetaData property = new ElementPropertyMetaDataDefault(
+ "value", element, "test", String.class, true);
+ doReturn("props").when(element).getElementDataKey();
+ doReturn(Collections.singletonList(property))
+ .when(element).getProperties();
+ when(element.hasLoadedProperties()).thenReturn(true);
+ doAnswer(invocation -> {
+ if (writes) {
+ FlowData flowData = invocation.getArgument(0);
+ ElementData data = mock(ElementData.class);
+ when(data.get(anyString())).thenReturn(value);
+ flowData.getOrAdd("props", d -> data);
+ }
+ return null;
+ }).when(element).process(any(FlowData.class));
+ return element;
+ }
+
+ private Map where(FlowElement, ?> element)
+ throws Exception {
+ try (Pipeline pipeline = new PipelineBuilder(loggerFactory)
+ .addFlowElement(element)
+ .build();
+ FlowData flowData = pipeline.createFlowData()) {
+ flowData.process();
+ return flowData.getWhere(p -> true);
+ }
+ }
+
+ @Test
+ public void getWhere_ElementThatDidNotRun_LeavesItsPropertiesOut()
+ throws Exception {
+ assertTrue(where(element(false, null)).isEmpty());
+ }
+
+ @Test
+ public void getWhere_PropertyWithoutAValue_LeavesItOut()
+ throws Exception {
+ assertTrue(where(element(true, null)).isEmpty());
+ }
+
+ @Test
+ public void getWhere_PropertyWithAValue_GivesIt() throws Exception {
+ assertEquals(Collections.singletonMap("props.value", "x"),
+ where(element(true, "x")));
+ }
+
+ @Test
+ public void absentData_IsNullOrNoValue_NotAnError() throws Exception {
+ try (Pipeline pipeline = new PipelineBuilder(loggerFactory)
+ .addFlowElement(element(false, null))
+ .build();
+ FlowData flowData = pipeline.createFlowData()) {
+ flowData.addEvidence("query.number", "not a number");
+ flowData.process();
+ // get by key or by type throws for data that no element added,
+ // which FlowDataTests and the TypedKeyMap tests expect, as
+ // asking for it is taken as the caller's mistake. Evidence that
+ // is missing or of another type is not.
+ assertFalse(flowData.tryGetEvidence("query.number", Integer.class)
+ .hasValue());
+ assertFalse(flowData.tryGetEvidence("query.missing", String.class)
+ .hasValue());
+ }
+ }
+
+ /**
+ * Keeps the List import, which the element's properties use.
+ */
+ @SuppressWarnings("unused")
+ private static List> unused;
+}
diff --git a/pipeline/pipeline.core/src/test/java/fiftyone/pipeline/core/flowelements/FlowDataCleanupTests.java b/pipeline/pipeline.core/src/test/java/fiftyone/pipeline/core/flowelements/FlowDataCleanupTests.java
new file mode 100644
index 000000000..fc2b13670
--- /dev/null
+++ b/pipeline/pipeline.core/src/test/java/fiftyone/pipeline/core/flowelements/FlowDataCleanupTests.java
@@ -0,0 +1,68 @@
+/* *********************************************************************
+ * 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.pipeline.core.flowelements;
+
+import fiftyone.pipeline.core.data.ElementData;
+import fiftyone.pipeline.core.data.FlowData;
+import fiftyone.pipeline.core.testclasses.flowelements.MultiplyByFiveElement;
+import org.junit.Test;
+import org.slf4j.ILoggerFactory;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.verifyNoInteractions;
+import static org.mockito.Mockito.withSettings;
+
+/**
+ * Closing Flow Data closes the Element Data that needs cleanup and does
+ * nothing at all to the rest, which a cache may still hold. See
+ * resource-cleanup.md line 84 :
+ * "Element Data instances that do not require cleanup MUST be left alone
+ * in order for result caching to work correctly."
+ */
+public class FlowDataCleanupTests {
+
+ private final ILoggerFactory loggerFactory =
+ LoggerFactory.getILoggerFactory();
+
+ @Test
+ public void close_LeavesElementDataWithoutCleanupAlone() throws Exception {
+ ElementData plain = mock(ElementData.class);
+ ElementData closeable = mock(ElementData.class,
+ withSettings().extraInterfaces(AutoCloseable.class));
+
+ try (Pipeline pipeline = new PipelineBuilder(loggerFactory)
+ .addFlowElement(new MultiplyByFiveElement(mock(Logger.class)))
+ .build();
+ FlowData flowData = pipeline.createFlowData()) {
+ flowData.getOrAdd("plain", data -> plain);
+ flowData.getOrAdd("closeable", data -> closeable);
+ }
+
+ verifyNoInteractions(plain);
+ verify((AutoCloseable) closeable, times(1)).close();
+ }
+}
diff --git a/pipeline/pipeline.core/src/test/java/fiftyone/pipeline/core/flowelements/FlowDataConcurrentWriteTests.java b/pipeline/pipeline.core/src/test/java/fiftyone/pipeline/core/flowelements/FlowDataConcurrentWriteTests.java
new file mode 100644
index 000000000..8d635ce15
--- /dev/null
+++ b/pipeline/pipeline.core/src/test/java/fiftyone/pipeline/core/flowelements/FlowDataConcurrentWriteTests.java
@@ -0,0 +1,236 @@
+/* *********************************************************************
+ * 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.pipeline.core.flowelements;
+
+import fiftyone.pipeline.core.data.ElementData;
+import fiftyone.pipeline.core.data.FlowData;
+import fiftyone.pipeline.core.data.FlowError;
+import fiftyone.pipeline.core.testclasses.flowelements.MultiplyByFiveElement;
+import fiftyone.pipeline.core.testclasses.flowelements.MultiplyByTenElement;
+import org.junit.Test;
+import org.slf4j.ILoggerFactory;
+import org.slf4j.Logger;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicInteger;
+import java.util.concurrent.atomic.AtomicReference;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertSame;
+import static org.junit.Assert.assertTrue;
+import static org.mockito.Mockito.mock;
+
+/**
+ * Flow Data of a Pipeline with parallel elements takes Element Data and
+ * errors from several threads, with no lock to add Element Data and errors
+ * kept whole. See
+ * thread-safety.md lines 88 to 115 :
+ * a Flow Element adds its Element Data with one compare and swap, readers
+ * take no lock, and errors that members add while a group runs are added
+ * under a lock.
+ *
+ * The first writer is held inside its write until the test lets it go,
+ * and the second is checked from what it has done, so nothing depends on
+ * how long anything takes. The deadline only stops a broken run from
+ * hanging.
+ */
+public class FlowDataConcurrentWriteTests {
+
+ private static final long DEADLINE_MILLIS = 60_000;
+
+ /**
+ * Loggers that write nothing, as the errors test logs thousands.
+ */
+ private final ILoggerFactory loggerFactory = name -> mock(Logger.class);
+
+ private Pipeline pipeline(boolean parallel) throws Exception {
+ PipelineBuilder builder = new PipelineBuilder(loggerFactory);
+ if (parallel) {
+ builder.addFlowElementsParallel(new FlowElement, ?>[] {
+ new MultiplyByFiveElement(mock(Logger.class)),
+ new MultiplyByTenElement(mock(Logger.class)) });
+ } else {
+ builder.addFlowElement(
+ new MultiplyByFiveElement(mock(Logger.class)));
+ }
+ return builder.build();
+ }
+
+ /**
+ * Two threads that add Element Data under the same key at once do not
+ * wait for each other, and both get the one instance that was kept.
+ */
+ @Test
+ public void getOrAdd_SameKeyFromTwoThreads_NeitherWaits()
+ throws Exception {
+ try (Pipeline pipeline = pipeline(true);
+ FlowData flowData = pipeline.createFlowData()) {
+ assertTrue(pipeline.isConcurrent());
+ AtomicInteger created = new AtomicInteger();
+ CountDownLatch bothCreating = new CountDownLatch(2);
+ CountDownLatch release = new CountDownLatch(1);
+ AtomicReference failure = new AtomicReference<>();
+ List results = new ArrayList<>();
+
+ FlowElement.DataFactory factory = data -> {
+ created.incrementAndGet();
+ bothCreating.countDown();
+ try {
+ release.await(DEADLINE_MILLIS, TimeUnit.MILLISECONDS);
+ } catch (InterruptedException e) {
+ throw new RuntimeException(e);
+ }
+ return mock(ElementData.class);
+ };
+ Runnable add = () -> {
+ try {
+ ElementData result = flowData.getOrAdd("shared", factory);
+ synchronized (results) {
+ results.add(result);
+ }
+ } catch (Throwable t) {
+ failure.compareAndSet(null, t);
+ }
+ };
+
+ Thread first = new Thread(add);
+ Thread second = new Thread(add);
+ first.start();
+ second.start();
+ // Both writers reach the factory while neither has finished, so
+ // neither waited for the other.
+ boolean neitherWaited = bothCreating.await(
+ DEADLINE_MILLIS, TimeUnit.MILLISECONDS);
+ release.countDown();
+ first.join(DEADLINE_MILLIS);
+ second.join(DEADLINE_MILLIS);
+
+ assertNull("A write failed: " + failure.get(), failure.get());
+ assertTrue("One writer waited for the other.", neitherWaited);
+ assertEquals(2, created.get());
+ assertEquals(2, results.size());
+ assertSame(results.get(0), results.get(1));
+ }
+ }
+
+ /**
+ * Errors added from several threads at once are all kept.
+ */
+ @Test
+ public void addError_FromSeveralThreads_KeepsEveryError()
+ throws Exception {
+ final int threads = 8;
+ final int perThread = 1000;
+ try (Pipeline pipeline = pipeline(true);
+ FlowData flowData = pipeline.createFlowData()) {
+ CountDownLatch start = new CountDownLatch(1);
+ FlowElement, ?> element = mock(FlowElement.class);
+ AtomicReference failure = new AtomicReference<>();
+ List workers = new ArrayList<>();
+ for (int t = 0; t < threads; t++) {
+ Thread worker = new Thread(() -> {
+ try {
+ start.await();
+ } catch (InterruptedException e) {
+ return;
+ }
+ try {
+ for (int i = 0; i < perThread; i++) {
+ flowData.addError(new FlowError.Default(
+ new Exception("error"), element));
+ }
+ } catch (Throwable e) {
+ failure.compareAndSet(null, e);
+ }
+ });
+ workers.add(worker);
+ worker.start();
+ }
+ start.countDown();
+ for (Thread worker : workers) {
+ worker.join(DEADLINE_MILLIS);
+ assertFalse(worker.isAlive());
+ }
+ assertNull("A write failed: " + failure.get(), failure.get());
+ assertEquals(threads * perThread, flowData.getErrors().size());
+ }
+ }
+
+ /**
+ * Without parallel elements, adding Element Data takes no lock, so a
+ * second writer for another key is not held up by the first.
+ */
+ @Test
+ public void getOrAdd_WithoutParallelElements_TakesNoLock()
+ throws Exception {
+ try (Pipeline pipeline = pipeline(false);
+ FlowData flowData = pipeline.createFlowData()) {
+ assertFalse(pipeline.isConcurrent());
+ CountDownLatch firstCreating = new CountDownLatch(1);
+ CountDownLatch release = new CountDownLatch(1);
+ AtomicReference failure = new AtomicReference<>();
+
+ Thread first = new Thread(() -> {
+ try {
+ flowData.getOrAdd("first", data -> {
+ firstCreating.countDown();
+ try {
+ release.await(
+ DEADLINE_MILLIS, TimeUnit.MILLISECONDS);
+ } catch (InterruptedException e) {
+ throw new RuntimeException(e);
+ }
+ return mock(ElementData.class);
+ });
+ } catch (Throwable t) {
+ failure.compareAndSet(null, t);
+ }
+ });
+ first.start();
+ assertTrue(firstCreating.await(
+ DEADLINE_MILLIS, TimeUnit.MILLISECONDS));
+ Thread second = new Thread(() -> {
+ try {
+ flowData.getOrAdd("second",
+ data -> mock(ElementData.class));
+ } catch (Throwable t) {
+ failure.compareAndSet(null, t);
+ }
+ });
+ second.start();
+ second.join(DEADLINE_MILLIS);
+ boolean secondFinished = second.isAlive() == false;
+ release.countDown();
+ first.join(DEADLINE_MILLIS);
+
+ assertNull("A write failed: " + failure.get(), failure.get());
+ assertTrue("The second writer waited for the first.",
+ secondFinished);
+ }
+ }
+}
diff --git a/pipeline/pipeline.core/src/test/java/fiftyone/pipeline/core/flowelements/FlowDataDeclaredExceptionsTests.java b/pipeline/pipeline.core/src/test/java/fiftyone/pipeline/core/flowelements/FlowDataDeclaredExceptionsTests.java
new file mode 100644
index 000000000..3f165ac37
--- /dev/null
+++ b/pipeline/pipeline.core/src/test/java/fiftyone/pipeline/core/flowelements/FlowDataDeclaredExceptionsTests.java
@@ -0,0 +1,164 @@
+/* *********************************************************************
+ * 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.pipeline.core.flowelements;
+
+import fiftyone.pipeline.core.data.ElementData;
+import fiftyone.pipeline.core.data.FlowData;
+import fiftyone.pipeline.core.typed.TypedKey;
+import fiftyone.pipeline.core.exceptions.PipelineDataException;
+import fiftyone.pipeline.core.testclasses.flowelements.MultiplyByFiveElement;
+import org.junit.Test;
+import org.slf4j.ILoggerFactory;
+import org.slf4j.Logger;
+
+import java.lang.reflect.Method;
+import java.util.Arrays;
+import java.util.List;
+
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+import static org.mockito.Mockito.mock;
+
+/**
+ * The FlowData interface declares, in the throws clause that IDEs and
+ * reflection read, the exceptions its methods throw for a mistake by the
+ * caller, and the implementation throws one of those. See
+ * exception-handling.md line 17 :
+ * "In all cases, standard language features SHOULD be used to let IDEs and
+ * users know what exceptions/errors a particular function can throw."
+ */
+public class FlowDataDeclaredExceptionsTests {
+
+ private final ILoggerFactory loggerFactory = name -> mock(Logger.class);
+
+ private interface Call {
+ void run(FlowData flowData) throws Exception;
+ }
+
+ private static List> declared(String name, Class>... types)
+ throws Exception {
+ Method method = FlowData.class.getMethod(name, types);
+ return Arrays.asList(method.getExceptionTypes());
+ }
+
+ private static void assertDeclares(
+ List> declared, Class> type, String method) {
+ assertTrue(method + " does not declare " + type.getSimpleName() +
+ ", only " + declared + ".",
+ declared.contains(type));
+ }
+
+ /**
+ * Call the method on Flow Data that has not been processed, and check
+ * the exception is one the interface declares.
+ */
+ private void assertThrowsDeclared(
+ List> declared, String method, boolean process, Call call)
+ throws Exception {
+ MultiplyByFiveElement element =
+ new MultiplyByFiveElement(mock(Logger.class));
+ try (Pipeline pipeline = new PipelineBuilder(loggerFactory)
+ .addFlowElement(element)
+ .build();
+ FlowData flowData = pipeline.createFlowData()) {
+ if (process) {
+ flowData.addEvidence(element.evidenceKeys.get(0), 2);
+ flowData.process();
+ }
+ try {
+ call.run(flowData);
+ fail(method + " did not throw.");
+ } catch (Exception e) {
+ boolean isDeclared = false;
+ for (Class> type : declared) {
+ isDeclared |= type.isInstance(e);
+ }
+ assertTrue(method + " threw " + e.getClass().getName() +
+ ", which it does not declare, only " + declared + ".",
+ isDeclared);
+ }
+ }
+ }
+
+ @Test
+ public void process_DeclaresAndThrowsIllegalState() throws Exception {
+ List> declared = declared("process");
+ assertDeclares(declared, IllegalStateException.class, "process");
+ assertThrowsDeclared(declared, "process", true, FlowData::process);
+ }
+
+ @Test
+ @SuppressWarnings("unchecked")
+ public void getByTypedKey_DeclaresAndThrowsIllegalState()
+ throws Exception {
+ List> declared = declared("get", TypedKey.class);
+ assertDeclares(declared, IllegalStateException.class, "get(TypedKey)");
+ assertDeclares(declared, IllegalArgumentException.class,
+ "get(TypedKey)");
+ assertThrowsDeclared(declared, "get(TypedKey)", false,
+ d -> d.get(mock(TypedKey.class)));
+ }
+
+ @Test
+ public void getByName_DeclaresAndThrowsIllegalState() throws Exception {
+ List> declared = declared("get", String.class);
+ assertDeclares(declared, IllegalStateException.class, "get(String)");
+ assertThrowsDeclared(declared, "get(String)", false,
+ d -> d.get("muliplyByFive"));
+ }
+
+ @Test
+ public void getFromElement_DeclaresAndThrowsIllegalState()
+ throws Exception {
+ List> declared =
+ declared("getFromElement", FlowElement.class);
+ assertDeclares(declared, IllegalStateException.class,
+ "getFromElement");
+ assertDeclares(declared, IllegalArgumentException.class,
+ "getFromElement");
+ assertThrowsDeclared(declared, "getFromElement", true,
+ d -> d.getFromElement(null));
+ }
+
+ @Test
+ public void getAs_DeclaresAndThrowsPipelineDataException()
+ throws Exception {
+ List> declared = declared("getAs", String.class, Class.class);
+ assertDeclares(declared, PipelineDataException.class, "getAs");
+ assertDeclares(declared, ClassCastException.class, "getAs");
+ assertThrowsDeclared(declared, "getAs", false,
+ d -> d.getAs("result", Integer.class));
+ for (String name : new String[] {
+ "getAsString", "getAsBool", "getAsInt", "getAsLong",
+ "getAsFloat", "getAsDouble" }) {
+ assertDeclares(declared(name, String.class),
+ PipelineDataException.class, name);
+ }
+ }
+
+ /**
+ * Keeps the ElementData import, which documents what get returns.
+ */
+ @SuppressWarnings("unused")
+ private static ElementData unused;
+}
diff --git a/pipeline/pipeline.core/src/test/java/fiftyone/pipeline/core/flowelements/FlowElementContractTests.java b/pipeline/pipeline.core/src/test/java/fiftyone/pipeline/core/flowelements/FlowElementContractTests.java
new file mode 100644
index 000000000..dafb7e33f
--- /dev/null
+++ b/pipeline/pipeline.core/src/test/java/fiftyone/pipeline/core/flowelements/FlowElementContractTests.java
@@ -0,0 +1,73 @@
+/* *********************************************************************
+ * 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.pipeline.core.flowelements;
+
+import fiftyone.pipeline.core.data.EvidenceKeyFilter;
+import org.junit.Test;
+
+import java.lang.reflect.Method;
+import java.lang.reflect.Modifier;
+import java.util.List;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+/**
+ * Every Flow Element has to give the Evidence keys it uses, the Properties
+ * it populates and its Data Key, because the FlowElement interface declares
+ * all three without a default, so no element compiles without them. See
+ * conceptual-overview.md line 73 :
+ * "Each Element MUST:" advertize the Evidence keys that it can make use of,
+ * publicize details of the Properties it will populate, and expose the
+ * string Data Key that is used to identify its Element Data within Flow
+ * Data.
+ */
+public class FlowElementContractTests {
+
+ private static void assertRequired(
+ String name, Class> returnType) throws Exception {
+ Method method = FlowElement.class.getMethod(name);
+ assertTrue(name + " is not public.",
+ Modifier.isPublic(method.getModifiers()));
+ assertTrue(name + " is not abstract, so an element need not give it.",
+ Modifier.isAbstract(method.getModifiers()));
+ assertFalse(name + " has a default.", method.isDefault());
+ assertEquals(returnType, method.getReturnType());
+ }
+
+ @Test
+ public void evidenceKeys_AreRequired() throws Exception {
+ assertRequired("getEvidenceKeyFilter", EvidenceKeyFilter.class);
+ }
+
+ @Test
+ public void properties_AreRequired() throws Exception {
+ assertRequired("getProperties", List.class);
+ }
+
+ @Test
+ public void dataKey_IsRequired() throws Exception {
+ assertRequired("getElementDataKey", String.class);
+ }
+}
diff --git a/pipeline/pipeline.core/src/test/java/fiftyone/pipeline/core/flowelements/PipelineAutoCloseTests.java b/pipeline/pipeline.core/src/test/java/fiftyone/pipeline/core/flowelements/PipelineAutoCloseTests.java
new file mode 100644
index 000000000..737a2a5da
--- /dev/null
+++ b/pipeline/pipeline.core/src/test/java/fiftyone/pipeline/core/flowelements/PipelineAutoCloseTests.java
@@ -0,0 +1,88 @@
+/* *********************************************************************
+ * 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.pipeline.core.flowelements;
+
+import fiftyone.pipeline.core.data.FlowData;
+import fiftyone.pipeline.core.testclasses.flowelements.MultiplyByFiveElement;
+import org.junit.Test;
+import org.slf4j.ILoggerFactory;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+import static org.mockito.Mockito.mock;
+
+/**
+ * Closing a Pipeline closes its elements only when it is set to, so that
+ * an element shared with another Pipeline can be kept open. See
+ * resource-cleanup.md line 28 :
+ * "However, if the multi Pipeline Elements feature is implemented then
+ * there MUST be an option to disable this behavior."
+ */
+public class PipelineAutoCloseTests {
+
+ private final ILoggerFactory loggerFactory =
+ LoggerFactory.getILoggerFactory();
+
+ @Test
+ public void autoCloseOff_SharedElementStaysOpen() throws Exception {
+ MultiplyByFiveElement shared =
+ new MultiplyByFiveElement(mock(Logger.class));
+ try (Pipeline first = new PipelineBuilder(loggerFactory)
+ .setAutoCloseElements(false)
+ .addFlowElement(shared)
+ .build();
+ Pipeline second = new PipelineBuilder(loggerFactory)
+ .setAutoCloseElements(false)
+ .addFlowElement(shared)
+ .build()) {
+ // Closing again at the end of the block does nothing.
+ first.close();
+
+ assertFalse("Closing one Pipeline closed the shared element.",
+ shared.isClosed());
+ try (FlowData flowData = second.createFlowData()) {
+ flowData.addEvidence(shared.evidenceKeys.get(0), 2);
+ flowData.process();
+ assertEquals(10, flowData.getFromElement(shared).getResult());
+ }
+ } finally {
+ shared.close();
+ }
+ }
+
+ @Test
+ public void autoCloseOn_ClosesTheElement() throws Exception {
+ MultiplyByFiveElement element =
+ new MultiplyByFiveElement(mock(Logger.class));
+ try (Pipeline pipeline = new PipelineBuilder(loggerFactory)
+ .setAutoCloseElements(true)
+ .addFlowElement(element)
+ .build()) {
+ assertFalse(element.isClosed());
+ }
+ assertTrue(element.isClosed());
+ }
+}
diff --git a/pipeline/pipeline.core/src/test/java/fiftyone/pipeline/core/flowelements/PipelineImmutableTests.java b/pipeline/pipeline.core/src/test/java/fiftyone/pipeline/core/flowelements/PipelineImmutableTests.java
new file mode 100644
index 000000000..a63e24b46
--- /dev/null
+++ b/pipeline/pipeline.core/src/test/java/fiftyone/pipeline/core/flowelements/PipelineImmutableTests.java
@@ -0,0 +1,109 @@
+/* *********************************************************************
+ * 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.pipeline.core.flowelements;
+
+import fiftyone.pipeline.core.data.FlowData;
+import fiftyone.pipeline.core.testclasses.flowelements.MultiplyByFiveElement;
+import fiftyone.pipeline.core.testclasses.flowelements.MultiplyByTenElement;
+import org.junit.Test;
+import org.slf4j.ILoggerFactory;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.Collections;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertSame;
+import static org.junit.Assert.fail;
+import static org.mockito.Mockito.mock;
+
+/**
+ * Once built, a Pipeline's elements cannot be changed. See
+ * conceptual-overview.md line 118 :
+ * "A Pipeline instance MUST be immutable. In other words, once created, it
+ * cannot be changed by adding new elements, removing old ones, etc."
+ */
+public class PipelineImmutableTests {
+
+ private final ILoggerFactory loggerFactory =
+ LoggerFactory.getILoggerFactory();
+
+ /**
+ * The list of elements a Pipeline gives cannot be changed.
+ */
+ @Test
+ public void getFlowElements_CannotBeChanged() throws Exception {
+ MultiplyByFiveElement five = new MultiplyByFiveElement(
+ mock(Logger.class));
+ try (Pipeline pipeline = new PipelineBuilder(loggerFactory)
+ .addFlowElement(five)
+ .build()) {
+ try {
+ pipeline.getFlowElements().add(
+ new MultiplyByTenElement(mock(Logger.class)));
+ fail("An element was added to a built Pipeline.");
+ } catch (UnsupportedOperationException e) {
+ // Expected.
+ }
+ try {
+ pipeline.getFlowElements().clear();
+ fail("The elements of a built Pipeline were removed.");
+ } catch (UnsupportedOperationException e) {
+ // Expected.
+ }
+ assertEquals(Collections.singletonList(five),
+ pipeline.getFlowElements());
+ }
+ }
+
+ /**
+ * An element added to the builder after it has built a Pipeline is not
+ * part of that Pipeline, and does not run when it processes.
+ */
+ @Test
+ public void builderReused_DoesNotChangeABuiltPipeline() throws Exception {
+ MultiplyByFiveElement five = new MultiplyByFiveElement(
+ mock(Logger.class));
+ MultiplyByTenElement ten = new MultiplyByTenElement(
+ mock(Logger.class));
+ PipelineBuilder builder = new PipelineBuilder(loggerFactory)
+ .addFlowElement(five);
+ try (Pipeline pipeline = builder.build()) {
+ builder.addFlowElement(ten);
+
+ assertEquals(1, pipeline.getFlowElements().size());
+ assertSame(five, pipeline.getFlowElements().get(0));
+ assertEquals(null, pipeline.getElement(MultiplyByTenElement.class));
+ try (FlowData flowData = pipeline.createFlowData()) {
+ flowData.addEvidence(five.evidenceKeys.get(0), 2);
+ flowData.process();
+ assertEquals(10, flowData.getFromElement(five).getResult());
+ assertFalse(
+ "An element added to the builder later ran.",
+ flowData.getDataKeys().stream().anyMatch(
+ k -> k.equalsIgnoreCase(ten.getElementDataKey())));
+ }
+ }
+ }
+}
diff --git a/pipeline/pipeline.core/src/test/java/fiftyone/pipeline/core/flowelements/PipelineSuppressProcessExceptionsTests.java b/pipeline/pipeline.core/src/test/java/fiftyone/pipeline/core/flowelements/PipelineSuppressProcessExceptionsTests.java
new file mode 100644
index 000000000..756802f18
--- /dev/null
+++ b/pipeline/pipeline.core/src/test/java/fiftyone/pipeline/core/flowelements/PipelineSuppressProcessExceptionsTests.java
@@ -0,0 +1,77 @@
+/* *********************************************************************
+ * 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.pipeline.core.flowelements;
+
+import org.junit.Test;
+import org.slf4j.ILoggerFactory;
+import org.slf4j.LoggerFactory;
+
+import java.lang.reflect.Method;
+import java.lang.reflect.Modifier;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+/**
+ * The suppress process exceptions setting can be read through the public
+ * Pipeline interface, so that a wrapper such as the web integration can
+ * decide from it. See
+ * exception-handling.md :
+ * "This property MUST be exposed as a public property as part of the
+ * `Pipeline` public interface as `IsSuppressProcessExceptions`".
+ */
+public class PipelineSuppressProcessExceptionsTests {
+
+ private final ILoggerFactory loggerFactory =
+ LoggerFactory.getILoggerFactory();
+
+ /**
+ * The Pipeline interface declares a public isSuppressProcessExceptions
+ * that returns a boolean.
+ */
+ @Test
+ public void pipelineInterface_DeclaresIsSuppressProcessExceptions()
+ throws Exception {
+ Method method = Pipeline.class.getMethod("isSuppressProcessExceptions");
+ assertEquals(boolean.class, method.getReturnType());
+ assertTrue(Modifier.isPublic(method.getModifiers()));
+ }
+
+ /**
+ * The value read through the interface is the one the builder set, for
+ * both settings.
+ */
+ @Test
+ public void isSuppressProcessExceptions_GivesTheBuilderSetting()
+ throws Exception {
+ for (boolean suppress : new boolean[] { true, false }) {
+ try (Pipeline pipeline = new PipelineBuilder(loggerFactory)
+ .setSuppressProcessException(suppress)
+ .build()) {
+ Method method =
+ Pipeline.class.getMethod("isSuppressProcessExceptions");
+ assertEquals(suppress, method.invoke(pipeline));
+ }
+ }
+ }
+}
diff --git a/pipeline/pipeline.engines.fiftyone/src/main/java/fiftyone/pipeline/engines/fiftyone/flowelements/FiftyOnePipelineBuilder.java b/pipeline/pipeline.engines.fiftyone/src/main/java/fiftyone/pipeline/engines/fiftyone/flowelements/FiftyOnePipelineBuilder.java
index 83322e115..a111b1723 100644
--- a/pipeline/pipeline.engines.fiftyone/src/main/java/fiftyone/pipeline/engines/fiftyone/flowelements/FiftyOnePipelineBuilder.java
+++ b/pipeline/pipeline.engines.fiftyone/src/main/java/fiftyone/pipeline/engines/fiftyone/flowelements/FiftyOnePipelineBuilder.java
@@ -22,6 +22,7 @@
package fiftyone.pipeline.engines.fiftyone.flowelements;
+import fiftyone.pipeline.annotations.DefaultValue;
import fiftyone.pipeline.core.flowelements.FlowElement;
import fiftyone.pipeline.core.flowelements.PipelineBuilder;
import fiftyone.pipeline.engines.services.HttpClientDefault;
@@ -57,6 +58,7 @@ public FiftyOnePipelineBuilder(ILoggerFactory loggerFactory) {
* @param enabled true to enable usage sharing, false to disable
* @return this builder
*/
+ @DefaultValue(booleanValue = Constants.SHARE_USAGE_DEFAULT_ENABLED)
public FiftyOnePipelineBuilder setShareUsage(boolean enabled) {
this.shareUsageEnabled = enabled;
return this;
diff --git a/pipeline/pipeline.engines.fiftyone/src/main/java/fiftyone/pipeline/engines/fiftyone/flowelements/ShareUsageBuilderBase.java b/pipeline/pipeline.engines.fiftyone/src/main/java/fiftyone/pipeline/engines/fiftyone/flowelements/ShareUsageBuilderBase.java
index 6865a52fd..39861edda 100644
--- a/pipeline/pipeline.engines.fiftyone/src/main/java/fiftyone/pipeline/engines/fiftyone/flowelements/ShareUsageBuilderBase.java
+++ b/pipeline/pipeline.engines.fiftyone/src/main/java/fiftyone/pipeline/engines/fiftyone/flowelements/ShareUsageBuilderBase.java
@@ -358,6 +358,7 @@ public int getMaximumQueueSize() {
* @return this builder
*/
@Deprecated
+ @DefaultValue(intValue = Constants.SHARE_USAGE_DEFAULT_ADD_TIMEOUT)
public ShareUsageBuilderBase setAddTimeout(int milliseconds) {
addTimeout = milliseconds;
return this;
@@ -388,6 +389,7 @@ public ShareUsageBuilderBase setAddTimeoutMillis(int milliseconds) {
* @return this builder
*/
@Deprecated
+ @DefaultValue(intValue = Constants.SHARE_USAGE_DEFAULT_TAKE_TIMEOUT)
public ShareUsageBuilderBase setTakeTimeout(int milliseconds) {
takeTimeout = milliseconds;
return this;
diff --git a/pipeline/pipeline.engines.fiftyone/src/test/java/fiftyone/pipeline/engines/fiftyone/flowelements/BuilderDefaultValueTests.java b/pipeline/pipeline.engines.fiftyone/src/test/java/fiftyone/pipeline/engines/fiftyone/flowelements/BuilderDefaultValueTests.java
new file mode 100644
index 000000000..8dab0cd98
--- /dev/null
+++ b/pipeline/pipeline.engines.fiftyone/src/test/java/fiftyone/pipeline/engines/fiftyone/flowelements/BuilderDefaultValueTests.java
@@ -0,0 +1,70 @@
+/* *********************************************************************
+ * 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.pipeline.engines.fiftyone.flowelements;
+
+import fiftyone.pipeline.annotations.DefaultValue;
+import org.junit.jupiter.api.Test;
+
+import java.lang.reflect.Method;
+import java.lang.reflect.Modifier;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+/**
+ * Every configuration setter of the usage sharing and 51Degrees Pipeline
+ * builders gives its default value beside it, with {@link DefaultValue}.
+ * See
+ * reference-implementation-notes.md line 79 :
+ * "In all cases, there SHOULD be some mechanism to specify the default
+ * values alongside configuration property setters."
+ */
+public class BuilderDefaultValueTests {
+
+ static List missingDefaults(Class>... builders) {
+ List missing = new ArrayList<>();
+ for (Class> builder : builders) {
+ for (Method method : builder.getDeclaredMethods()) {
+ if (Modifier.isPublic(method.getModifiers()) &&
+ method.getName().startsWith("set") &&
+ method.isSynthetic() == false &&
+ method.isBridge() == false &&
+ method.getAnnotation(DefaultValue.class) == null) {
+ missing.add(builder.getSimpleName() + "#" +
+ method.getName() +
+ Arrays.toString(method.getParameterTypes()));
+ }
+ }
+ }
+ return missing;
+ }
+
+ @Test
+ public void builderSetters_GiveTheirDefaults() {
+ assertEquals(new ArrayList(), missingDefaults(
+ ShareUsageBuilderBase.class,
+ FiftyOnePipelineBuilder.class));
+ }
+}
diff --git a/pipeline/pipeline.engines/src/main/java/fiftyone/pipeline/engines/configuration/DataFileConfigurationBuilderBase.java b/pipeline/pipeline.engines/src/main/java/fiftyone/pipeline/engines/configuration/DataFileConfigurationBuilderBase.java
index aeb9a1b29..9d7cb5818 100644
--- a/pipeline/pipeline.engines/src/main/java/fiftyone/pipeline/engines/configuration/DataFileConfigurationBuilderBase.java
+++ b/pipeline/pipeline.engines/src/main/java/fiftyone/pipeline/engines/configuration/DataFileConfigurationBuilderBase.java
@@ -22,6 +22,8 @@
package fiftyone.pipeline.engines.configuration;
+import fiftyone.pipeline.annotations.DefaultValue;
+import fiftyone.pipeline.engines.Constants;
import fiftyone.pipeline.engines.data.DataUpdateUrlFormatter;
import fiftyone.pipeline.engines.services.DataUpdateService;
@@ -71,6 +73,7 @@ public DataFileConfigurationBuilderBase() {
* @param identifier the identifier to use
* @return this builder instance
*/
+ @DefaultValue("Default")
@SuppressWarnings("unchecked")
public TBuilder setDataFileIdentifier(String identifier) {
this.identifier = identifier;
@@ -83,6 +86,7 @@ public TBuilder setDataFileIdentifier(String identifier) {
* @param url the URL to check for a new data file.
* @return this builder instance.
*/
+ @DefaultValue("51Degrees default, set by the engine's builder")
@SuppressWarnings("unchecked")
public TBuilder setDataUpdateUrl(String url) {
dataUpdateUrlOverride = url;
@@ -95,6 +99,7 @@ public TBuilder setDataUpdateUrl(String url) {
* @param formatter the formatter to use
* @return this builder instance
*/
+ @DefaultValue("51Degrees default, set by the engine's builder")
@SuppressWarnings("unchecked")
public TBuilder setDataUpdateUrlFormatter( DataUpdateUrlFormatter formatter) {
dataUpdateUrlFormatter = formatter;
@@ -110,6 +115,7 @@ public TBuilder setDataUpdateUrlFormatter( DataUpdateUrlFormatter formatter) {
* False otherwise
* @return this builder instance
*/
+ @DefaultValue(booleanValue = Constants.DEFAULT_VERIFY_MD5)
@SuppressWarnings("unchecked")
public TBuilder setDataUpdateVerifyMd5(boolean verify) {
dataUpdateVerifyMd5 = verify;
@@ -123,6 +129,7 @@ public TBuilder setDataUpdateVerifyMd5(boolean verify) {
* be decompressed. False otherwise
* @return this builder instance
*/
+ @DefaultValue(booleanValue = Constants.DEFAULT_DECOMPRESS)
@SuppressWarnings("unchecked")
public TBuilder setDataUpdateDecompress(boolean decompress) {
dataUpdateDecompress = decompress;
@@ -137,6 +144,7 @@ public TBuilder setDataUpdateDecompress(boolean decompress) {
* called on {@link DataUpdateService}
* @return this builder instance
*/
+ @DefaultValue(booleanValue = Constants.DEFAULT_AUTOUPDATE_ENABLED)
@SuppressWarnings("unchecked")
public TBuilder setAutoUpdate(boolean enabled) {
autoUpdateEnabled = enabled;
@@ -155,6 +163,7 @@ public TBuilder setAutoUpdate(boolean enabled) {
* @param enabled the cache configuration to use
* @return this builder instance
*/
+ @DefaultValue(booleanValue = Constants.DEFAULT_WATCHER_ENABLED)
@SuppressWarnings("unchecked")
public TBuilder setDataFileSystemWatcher(boolean enabled) {
dataFileSystemWatcherEnabled = enabled;
@@ -175,6 +184,7 @@ public TBuilder setDataFileSystemWatcher(boolean enabled) {
* @param pollingIntervalSeconds the number of seconds between checks
* @return this builder instance
*/
+ @DefaultValue(intValue = Constants.DATA_UPDATE_POLLING_DEFAULT)
@SuppressWarnings("unchecked")
public TBuilder setUpdatePollingInterval(int pollingIntervalSeconds) {
updatePollingIntervalSeconds = pollingIntervalSeconds;
@@ -194,6 +204,7 @@ public TBuilder setUpdatePollingInterval(int pollingIntervalSeconds) {
* @param pollingIntervalMillis the time between checks
* @return this builder instance
*/
+ @DefaultValue(intValue = Constants.DATA_UPDATE_POLLING_DEFAULT * 1000)
@SuppressWarnings("unchecked")
public TBuilder setUpdatePollingInterval(long pollingIntervalMillis) {
if (pollingIntervalMillis / 1000 > Integer.MAX_VALUE) {
@@ -212,6 +223,7 @@ public TBuilder setUpdatePollingInterval(long pollingIntervalMillis) {
* polling interval
* @return this builder instance
*/
+ @DefaultValue(intValue = Constants.DATA_UPDATE_RANDOMISATION_DEFAULT * 1000)
@SuppressWarnings("unchecked")
public TBuilder setUpdateRandomisationMax(long maximumDeviationMillis){
if (maximumDeviationMillis / 1000 > Integer.MAX_VALUE) {
@@ -228,6 +240,7 @@ public TBuilder setUpdateRandomisationMax(long maximumDeviationMillis){
* @param enabled whether to use the If-Modified-Since header
* @return this builder instance
*/
+ @DefaultValue(booleanValue = Constants.DEFAULT_VERIFY_IF_MODIFIED_SINCE)
@SuppressWarnings("unchecked")
public TBuilder setVerifyIfModifiedSince(boolean enabled) {
dataUpdateVerifyModifiedSince = enabled;
@@ -241,6 +254,7 @@ public TBuilder setVerifyIfModifiedSince(boolean enabled) {
* this file
* @return this builder
* */
+ @DefaultValue("No license keys")
@SuppressWarnings("unchecked")
public TBuilder setDataUpdateLicenseKey(String key) {
if (key == null) {
@@ -265,6 +279,7 @@ public TBuilder setDataUpdateLicenseKey(String key) {
* latest copy of the file
* @return this builder
*/
+ @DefaultValue(booleanValue = Constants.DEFAULT_UPDATE_ON_STARTUP)
@SuppressWarnings("unchecked")
public TBuilder setUpdateOnStartup(boolean enabled) {
updateOnStartup = enabled;
@@ -276,6 +291,7 @@ public TBuilder setUpdateOnStartup(boolean enabled) {
* @param keys 51Degrees license keys
* @return this builder
* */
+ @DefaultValue("No license keys")
@SuppressWarnings("unchecked")
public TBuilder setDataUpdateLicenseKeys(String[] keys) {
Collections.addAll(licenseKeys, keys);
diff --git a/pipeline/pipeline.engines/src/main/java/fiftyone/pipeline/engines/data/AspectDataBase.java b/pipeline/pipeline.engines/src/main/java/fiftyone/pipeline/engines/data/AspectDataBase.java
index 88ced1b32..d82f4fbd8 100644
--- a/pipeline/pipeline.engines/src/main/java/fiftyone/pipeline/engines/data/AspectDataBase.java
+++ b/pipeline/pipeline.engines/src/main/java/fiftyone/pipeline/engines/data/AspectDataBase.java
@@ -213,6 +213,18 @@ public Map asKeyMap() {
return super.asKeyMap();
}
+ /**
+ * Get the value stored for the key as it is now, without waiting for any
+ * lazy loading processing to finish, so it can be read from inside that
+ * processing. The value is null when nothing has been stored for the key,
+ * including when processing has not stored it yet.
+ * @param key the key to read
+ * @return the value stored for the key, or null
+ */
+ protected Object getWithoutWaiting(String key) {
+ return super.asKeyMap().get(key);
+ }
+
/**
* Gets the value stored using the specified key with full checks
* against the {@link MissingPropertyService}.
diff --git a/pipeline/pipeline.engines/src/main/java/fiftyone/pipeline/engines/flowelements/AspectEngineBase.java b/pipeline/pipeline.engines/src/main/java/fiftyone/pipeline/engines/flowelements/AspectEngineBase.java
index 89a545be1..b8664fd37 100644
--- a/pipeline/pipeline.engines/src/main/java/fiftyone/pipeline/engines/flowelements/AspectEngineBase.java
+++ b/pipeline/pipeline.engines/src/main/java/fiftyone/pipeline/engines/flowelements/AspectEngineBase.java
@@ -135,6 +135,27 @@ protected final void processInternal(FlowData flowData) throws Exception {
processWithCache(flowData);
}
+ /**
+ * Whether a result may be stored in the cache, and served when it is
+ * found there. A result that holds a value created for the one request
+ * that asked for it, such as a 51Did, must be neither. See the
+ * caching specification :
+ * "A cache MUST NOT store a result that holds such a value, and MUST NOT
+ * serve one that it finds."
+ *
+ * It is asked about a processed result before it is stored, and about a
+ * result found in the cache before it is served. With the deprecated
+ * lazy loading option a result is stored before it has been processed,
+ * so only the check before serving applies, and an override must answer
+ * without waiting for the processing to finish.
+ * @param data the result to check
+ * @return true, unless an engine overrides this to keep a result out of
+ * the cache
+ */
+ protected boolean isCacheable(TData data) {
+ return true;
+ }
+
/**
* Private method that checks if the result is already in the cache or not.
* If it is then the result is added to 'data', if not then
@@ -151,7 +172,8 @@ private void processWithCache(final FlowData flowData) throws Exception {
Object cacheResultsObject = cache.get(flowData);
if (cacheResultsObject != null &&
getTypedDataKey().getType().isAssignableFrom(
- cacheResultsObject.getClass())) {
+ cacheResultsObject.getClass()) &&
+ isCacheable((TData)cacheResultsObject)) {
// This is checked.
//noinspection unchecked
cacheResult = (TData)cacheResultsObject;
@@ -183,15 +205,18 @@ public Void call() throws Exception {
}
}
);
+ // The cache key is made from the Flow Data's Evidence, so
+ // the result is stored here, on the processing thread, and
+ // not by the task. It is not processed yet, so the check
+ // before serving keeps out a result that may not be cached.
+ if (cache != null) {
+ cache.put(flowData, aspectData);
+ }
}
else {
// If not lazy loading, just start processing.
processEngine(flowData, aspectData);
- }
- // If there is a cache then add the result
- // of processing to the cache.
- if (cache != null) {
- cache.put(flowData, flowData.getFromElement(this));
+ putInCache(flowData, aspectData);
}
} else {
// We have a result from the cache so add it
@@ -200,6 +225,18 @@ public Void call() throws Exception {
}
}
+ /**
+ * Add a processed result to the cache, if there is one and the result
+ * may be cached.
+ * @param flowData the {@link FlowData} whose Evidence makes the key
+ * @param aspectData the processed result
+ */
+ private void putInCache(FlowData flowData, TData aspectData) {
+ if (cache != null && isCacheable(aspectData)) {
+ cache.put(flowData, aspectData);
+ }
+ }
+
@Override
protected void managedResourcesCleanup() {
if (cache != null) {
diff --git a/pipeline/pipeline.engines/src/main/java/fiftyone/pipeline/engines/flowelements/AspectEngineBuilderBase.java b/pipeline/pipeline.engines/src/main/java/fiftyone/pipeline/engines/flowelements/AspectEngineBuilderBase.java
index cd13fe07f..44e1ce312 100644
--- a/pipeline/pipeline.engines/src/main/java/fiftyone/pipeline/engines/flowelements/AspectEngineBuilderBase.java
+++ b/pipeline/pipeline.engines/src/main/java/fiftyone/pipeline/engines/flowelements/AspectEngineBuilderBase.java
@@ -95,6 +95,7 @@ protected List getProperties() {
* @param set The properties that we want the engine to populate
* @return this builder
*/
+ @DefaultValue("All properties")
@SuppressWarnings("unchecked")
public TBuilder setProperties(Set set) {
for (AspectPropertyMetaData property : set) {
@@ -124,6 +125,7 @@ public TBuilder setProperty(String s) {
* @param properties The properties that we want the engine to populate
* @return this builder
*/
+ @DefaultValue("All properties")
@SuppressWarnings("unchecked")
public TBuilder setProperties(List properties) {
for (String property : properties) {
@@ -159,6 +161,7 @@ public TBuilder setProperties(String properties) {
* @param aspectProperty the property that we want the engine to populate
* @return this builder
*/
+ @DefaultValue("All properties")
@SuppressWarnings("unchecked")
public TBuilder setProperty(AspectPropertyMetaData aspectProperty) {
tryAddProperty(aspectProperty.getName());
@@ -170,6 +173,7 @@ public TBuilder setProperty(AspectPropertyMetaData aspectProperty) {
* @param configuration the configuration to use
* @return this builder
*/
+ @DefaultValue("No lazy loading")
@SuppressWarnings("unchecked")
public TBuilder setLazyLoading(LazyLoadingConfiguration configuration) {
this.lazyLoadingConfig = configuration;
@@ -182,6 +186,7 @@ public TBuilder setLazyLoading(LazyLoadingConfiguration configuration) {
* @param cacheConfiguration the cache configuration to use
* @return this builder
*/
+ @DefaultValue("No results cache")
@SuppressWarnings("unchecked")
public TBuilder setCache(CacheConfiguration cacheConfiguration) {
this.cacheConfig = cacheConfiguration;
diff --git a/pipeline/pipeline.engines/src/main/java/fiftyone/pipeline/engines/flowelements/CloudPipelineBuilderBase.java b/pipeline/pipeline.engines/src/main/java/fiftyone/pipeline/engines/flowelements/CloudPipelineBuilderBase.java
index ffdf0e361..234f0099c 100644
--- a/pipeline/pipeline.engines/src/main/java/fiftyone/pipeline/engines/flowelements/CloudPipelineBuilderBase.java
+++ b/pipeline/pipeline.engines/src/main/java/fiftyone/pipeline/engines/flowelements/CloudPipelineBuilderBase.java
@@ -22,6 +22,7 @@
package fiftyone.pipeline.engines.flowelements;
+import fiftyone.pipeline.annotations.DefaultValue;
import org.slf4j.ILoggerFactory;
import java.util.ArrayList;
@@ -70,6 +71,8 @@ public CloudPipelineBuilderBase(ILoggerFactory loggerFactory) {
* @param url endpoint URL
* @return this builder
*/
+ @DefaultValue("https://cloud.51degrees.com/api/v4/, the Cloud Request " +
+ "Engine's default")
@SuppressWarnings("unchecked")
public TBuilder setEndPoint(String url) {
if (url.endsWith("/") == false) {
@@ -84,6 +87,7 @@ public TBuilder setEndPoint(String url) {
* @param url data URL
* @return this builder
*/
+ @DefaultValue("The endpoint followed by {resourcekey}.json")
@SuppressWarnings("unchecked")
public TBuilder setDataEndpoint(String url) {
this.dataEndpoint = url;
@@ -96,6 +100,7 @@ public TBuilder setDataEndpoint(String url) {
* @param propertiesEndpoint properties URL
* @return this builder
*/
+ @DefaultValue("The endpoint followed by accessibleproperties")
@SuppressWarnings("unchecked")
public TBuilder setPropertiesEndpoint(String propertiesEndpoint) {
this.propertiesEndpoint = propertiesEndpoint;
@@ -108,6 +113,7 @@ public TBuilder setPropertiesEndpoint(String propertiesEndpoint) {
* @param evidenceKeysEndpoint evidence keys URL
* @return this builder
*/
+ @DefaultValue("The endpoint followed by evidencekeys")
@SuppressWarnings("unchecked")
public TBuilder setEvidenceKeysEndpoint(String evidenceKeysEndpoint) {
this.evidenceKeysEndpoint = evidenceKeysEndpoint;
@@ -119,6 +125,8 @@ public TBuilder setEvidenceKeysEndpoint(String evidenceKeysEndpoint) {
* @param key resource key
* @return this builder
*/
+ @DefaultValue("No default - a resource key or a license key must be " +
+ "supplied")
@SuppressWarnings("unchecked")
public TBuilder setResourceKey(String key) {
this.resourceKey = key;
@@ -134,6 +142,7 @@ public TBuilder setResourceKey(String key) {
* @param key license key
* @return this builder
*/
+ @DefaultValue("No default")
@SuppressWarnings("unchecked")
public TBuilder setLicenseKey(String key) {
this.licenseKey = key;
@@ -148,6 +157,7 @@ public TBuilder setLicenseKey(String key) {
* @param properties the fully qualified property names
* @return this builder
*/
+ @DefaultValue("None - required with a license key alone")
@SuppressWarnings("unchecked")
public TBuilder setRequestedProperties(List properties) {
if (properties == null) {
@@ -164,6 +174,7 @@ public TBuilder setRequestedProperties(List properties) {
* @param properties comma separated fully qualified property names
* @return this builder
*/
+ @DefaultValue("None - required with a license key alone")
public TBuilder setRequestedProperties(String properties) {
if (properties == null) {
throw new IllegalArgumentException(
@@ -183,6 +194,7 @@ public TBuilder setRequestedProperties(String properties) {
* @param cloudRequestOrigin the value to set the origin header to
* @return this builder
*/
+ @DefaultValue("None")
@SuppressWarnings("unchecked")
public TBuilder setCloudRequestOrigin(String cloudRequestOrigin) {
this.cloudRequestOrigin = cloudRequestOrigin;
diff --git a/pipeline/pipeline.engines/src/main/java/fiftyone/pipeline/engines/flowelements/OnPremiseAspectEngineBuilderBase.java b/pipeline/pipeline.engines/src/main/java/fiftyone/pipeline/engines/flowelements/OnPremiseAspectEngineBuilderBase.java
index 879f55f44..f743cd6a6 100644
--- a/pipeline/pipeline.engines/src/main/java/fiftyone/pipeline/engines/flowelements/OnPremiseAspectEngineBuilderBase.java
+++ b/pipeline/pipeline.engines/src/main/java/fiftyone/pipeline/engines/flowelements/OnPremiseAspectEngineBuilderBase.java
@@ -174,6 +174,8 @@ public TBuilder setTempDirPath(String dirPath) {
* @param profile the performance profile for the engine to use
* @return this engine builder instance
*/
+ @DefaultValue("Set by each engine's builder, which gives it on its own " +
+ "setPerformanceProfile")
public abstract TBuilder setPerformanceProfile(Constants.PerformanceProfiles profile);
@Override
diff --git a/pipeline/pipeline.engines/src/main/java/fiftyone/pipeline/engines/flowelements/SingleFileAspectEngineBuilderBase.java b/pipeline/pipeline.engines/src/main/java/fiftyone/pipeline/engines/flowelements/SingleFileAspectEngineBuilderBase.java
index 84df5a4f9..d389e1600 100644
--- a/pipeline/pipeline.engines/src/main/java/fiftyone/pipeline/engines/flowelements/SingleFileAspectEngineBuilderBase.java
+++ b/pipeline/pipeline.engines/src/main/java/fiftyone/pipeline/engines/flowelements/SingleFileAspectEngineBuilderBase.java
@@ -242,6 +242,7 @@ public TBuilder setDataFileSystemWatcher(boolean enabled) {
* @return this builder
*/
@Deprecated
+ @DefaultValue(intValue = Constants.DATA_UPDATE_POLLING_DEFAULT)
@SuppressWarnings("unchecked")
public TBuilder setUpdatePollingInterval(int pollingIntervalSeconds) {
dataFileBuilder.setUpdatePollingInterval(pollingIntervalSeconds);
@@ -277,6 +278,7 @@ public TBuilder setUpdatePollingIntervalSeconds(int pollingIntervalSeconds) {
* @return this builder
*/
@Deprecated
+ @DefaultValue(intValue = Constants.DATA_UPDATE_POLLING_DEFAULT * 1000)
@SuppressWarnings("unchecked")
public TBuilder setUpdatePollingInterval(long pollingIntervalMillis) {
dataFileBuilder.setUpdatePollingInterval(pollingIntervalMillis);
@@ -308,6 +310,7 @@ public TBuilder setUpdatePollingIntervalMillis(long pollingIntervalMillis) {
* @return this builder
*/
@Deprecated
+ @DefaultValue(intValue = Constants.DATA_UPDATE_RANDOMISATION_DEFAULT * 1000)
@SuppressWarnings("unchecked")
public TBuilder setUpdateRandomisationMax(long maximumDeviationMillis) {
dataFileBuilder.setUpdateRandomisationMax(maximumDeviationMillis);
@@ -324,7 +327,7 @@ public TBuilder setUpdateRandomisationMax(long maximumDeviationMillis) {
* polling interval
* @return this builder
*/
- @DefaultValue(intValue = Constants.DATA_UPDATE_RANDOMISATION_DEFAULT)
+ @DefaultValue(intValue = Constants.DATA_UPDATE_RANDOMISATION_DEFAULT * 1000)
@SuppressWarnings("unchecked")
public TBuilder setUpdateRandomisationMaxMillis(long maximumDeviationMillis) {
dataFileBuilder.setUpdateRandomisationMax(maximumDeviationMillis);
diff --git a/pipeline/pipeline.engines/src/main/java/fiftyone/pipeline/engines/services/DataUpdateServiceDefault.java b/pipeline/pipeline.engines/src/main/java/fiftyone/pipeline/engines/services/DataUpdateServiceDefault.java
index 5a1647c0c..60b31e242 100644
--- a/pipeline/pipeline.engines/src/main/java/fiftyone/pipeline/engines/services/DataUpdateServiceDefault.java
+++ b/pipeline/pipeline.engines/src/main/java/fiftyone/pipeline/engines/services/DataUpdateServiceDefault.java
@@ -227,6 +227,35 @@ public AutoUpdateStatus checkForUpdate(OnPremiseAspectEngine extends AspectDat
* @return {@link AutoUpdateStatus#AUTO_UPDATE_SUCCESS} if the data file was
* successfully updated
*/
+ /**
+ * Wrap a task that runs in the background so that an error it did not
+ * expect is logged and kept inside the task. Nothing else would see it:
+ * the executor keeps it in a future that nobody reads. See
+ * exception-handling.md line 83 :
+ * "When errors do occur, they will be logged and MUST NOT cause the
+ * process to crash." An error of the virtual machine itself, such as
+ * running out of memory, is logged and then thrown on.
+ * @param what what the task does, for the log
+ * @param task the task
+ * @return the wrapped task
+ */
+ private Runnable logErrors(final String what, final Runnable task) {
+ return new Runnable() {
+ @Override
+ public void run() {
+ try {
+ task.run();
+ } catch (Throwable t) {
+ logger.error("Unexpected error in the background " +
+ what + ".", t);
+ if (t instanceof VirtualMachineError) {
+ throw (VirtualMachineError) t;
+ }
+ }
+ }
+ };
+ }
+
private AutoUpdateStatus checkForUpdate(Object state, boolean manualUpdate) {
AspectEngineDataFile dataFile =
state instanceof AspectEngineDataFileDefault ?
@@ -341,12 +370,12 @@ private AutoUpdateStatus runCheck(Object state, boolean manualUpdate) {
if (dataFile.getFuture() != null) {
dataFile.getFuture().cancel(true);
dataFile.setFuture(futureFactory.schedule(
- new Runnable() {
+ logErrors("update check", new Runnable() {
@Override
public void run() {
checkForUpdateOnTimer(dataFile);
}
- },
+ }),
getIntervalMillis(dataFile.getConfiguration())));
}
}
@@ -603,7 +632,7 @@ public void registerDataFile(final AspectEngineDataFile dataFile) {
delayMillis);
try {
ScheduledFuture> future = futureFactory.schedule(
- new Runnable() {
+ logErrors("update check", new Runnable() {
@Override
public void run() {
logger.debug("running checkForUpdate {}",
@@ -611,7 +640,7 @@ public void run() {
checkForUpdateOnTimer(aspectDataFile);
logger.debug("Returning from checkForUpdate");
}
- },
+ }),
delayMillis);
logger.debug("Update check scheduled for {} millis",
future.getDelay(TimeUnit.MILLISECONDS));
@@ -663,7 +692,7 @@ public void run() {
aspectDataFile.getConfiguration().getPollingIntervalSeconds() * 1000L;
logger.debug("Setting poll future {} millis", pollingInterval);
aspectDataFile.setPollFuture(futureFactory.scheduleRepeating(
- new Runnable() {
+ logErrors("file system watch", new Runnable() {
@Override
public void run() {
logger.debug("Watcher is running");
@@ -686,7 +715,7 @@ public void run() {
// make sure to reset the watch key or we are not called again
watchKey.reset();
}
- },
+ }),
pollingInterval));
logger.debug("Poll check scheduled for {} mins",
aspectDataFile.getPollFuture().getDelay(TimeUnit.MINUTES));
diff --git a/pipeline/pipeline.engines/src/test/java/fiftyone/pipeline/engines/flowelements/BuilderDefaultValueTests.java b/pipeline/pipeline.engines/src/test/java/fiftyone/pipeline/engines/flowelements/BuilderDefaultValueTests.java
new file mode 100644
index 000000000..b29c14985
--- /dev/null
+++ b/pipeline/pipeline.engines/src/test/java/fiftyone/pipeline/engines/flowelements/BuilderDefaultValueTests.java
@@ -0,0 +1,73 @@
+/* *********************************************************************
+ * 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.pipeline.engines.flowelements;
+
+import fiftyone.pipeline.annotations.DefaultValue;
+import fiftyone.pipeline.engines.configuration.DataFileConfigurationBuilderBase;
+import org.junit.Test;
+
+import java.lang.reflect.Method;
+import java.lang.reflect.Modifier;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+import static org.junit.Assert.assertEquals;
+
+/**
+ * Every configuration setter of the engine builders gives its default
+ * value beside it, with {@link DefaultValue}. See
+ * reference-implementation-notes.md line 79 :
+ * "In all cases, there SHOULD be some mechanism to specify the default
+ * values alongside configuration property setters."
+ */
+public class BuilderDefaultValueTests {
+
+ static List missingDefaults(Class>... builders) {
+ List missing = new ArrayList<>();
+ for (Class> builder : builders) {
+ for (Method method : builder.getDeclaredMethods()) {
+ if (Modifier.isPublic(method.getModifiers()) &&
+ method.getName().startsWith("set") &&
+ method.isSynthetic() == false &&
+ method.isBridge() == false &&
+ method.getAnnotation(DefaultValue.class) == null) {
+ missing.add(builder.getSimpleName() + "#" +
+ method.getName() +
+ Arrays.toString(method.getParameterTypes()));
+ }
+ }
+ }
+ return missing;
+ }
+
+ @Test
+ public void engineBuilderSetters_GiveTheirDefaults() {
+ assertEquals(new ArrayList(), missingDefaults(
+ AspectEngineBuilderBase.class,
+ OnPremiseAspectEngineBuilderBase.class,
+ SingleFileAspectEngineBuilderBase.class,
+ CloudPipelineBuilderBase.class,
+ DataFileConfigurationBuilderBase.class));
+ }
+}
diff --git a/pipeline/pipeline.engines/src/test/java/fiftyone/pipeline/engines/services/DataUpdateServiceBackgroundErrorTests.java b/pipeline/pipeline.engines/src/test/java/fiftyone/pipeline/engines/services/DataUpdateServiceBackgroundErrorTests.java
new file mode 100644
index 000000000..93863e58a
--- /dev/null
+++ b/pipeline/pipeline.engines/src/test/java/fiftyone/pipeline/engines/services/DataUpdateServiceBackgroundErrorTests.java
@@ -0,0 +1,148 @@
+/* *********************************************************************
+ * 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.pipeline.engines.services;
+
+import ch.qos.logback.classic.Level;
+import fiftyone.common.testhelpers.TestLogger;
+import fiftyone.common.wrappers.io.FileWrapperFactory;
+import fiftyone.pipeline.engines.configuration.DataFileConfigurationDefault;
+import fiftyone.pipeline.engines.data.AspectData;
+import fiftyone.pipeline.engines.data.AspectEngineDataFileDefault;
+import fiftyone.pipeline.engines.data.AspectPropertyMetaData;
+import fiftyone.pipeline.engines.flowelements.OnPremiseAspectEngine;
+import fiftyone.pipeline.engines.services.update.FutureFactory;
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.ArgumentCaptor;
+import org.slf4j.LoggerFactory;
+
+import java.util.concurrent.ScheduledFuture;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.fail;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.anyLong;
+import static org.mockito.ArgumentMatchers.anyString;
+import static org.mockito.Mockito.atLeastOnce;
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+/**
+ * An unexpected error in the background update check is logged and kept
+ * inside the task, so it can neither end the thread silently nor reach the
+ * process. See
+ * exception-handling.md line 83 :
+ * "When errors do occur, they will be logged and MUST NOT cause the
+ * process to crash."
+ *
+ * The scheduled task is captured and run on the test's thread, so nothing
+ * depends on timing.
+ */
+public class DataUpdateServiceBackgroundErrorTests {
+
+ private TestLogger logger;
+ private FutureFactory futureFactory;
+ private FileWrapperFactory fileWrapperFactory;
+ private DataUpdateServiceDefault dataUpdate;
+
+ @Before
+ public void init() {
+ ch.qos.logback.classic.Logger real =
+ (ch.qos.logback.classic.Logger) LoggerFactory.getLogger(
+ getClass());
+ real.setLevel(Level.INFO);
+ logger = new TestLogger("test", real);
+ futureFactory = mock(FutureFactory.class);
+ doReturn(mock(ScheduledFuture.class)).when(futureFactory)
+ .schedule(any(Runnable.class), anyLong());
+ doReturn(mock(ScheduledFuture.class)).when(futureFactory)
+ .scheduleRepeating(any(Runnable.class), anyLong());
+ fileWrapperFactory = mock(FileWrapperFactory.class);
+ dataUpdate = new DataUpdateServiceDefault(
+ logger,
+ mock(HttpClient.class),
+ fileWrapperFactory,
+ futureFactory);
+ }
+
+ @SuppressWarnings("unchecked")
+ private AspectEngineDataFileDefault registerFile() {
+ DataFileConfigurationDefault config =
+ new DataFileConfigurationDefault();
+ config.setAutomaticUpdatesEnabled(true);
+ config.setFileSystemWatcherEnabled(false);
+ config.setDataFilePath("data.dat");
+ AspectEngineDataFileDefault file = new AspectEngineDataFileDefault();
+ file.setEngine(mock(OnPremiseAspectEngine.class));
+ file.setConfiguration(config);
+ file.setTempDataFilePath("temp.dat");
+ dataUpdate.registerDataFile(file);
+ return file;
+ }
+
+ private Runnable scheduledCheck() {
+ ArgumentCaptor task = ArgumentCaptor.forClass(Runnable.class);
+ verify(futureFactory, atLeastOnce()).schedule(task.capture(), anyLong());
+ return task.getValue();
+ }
+
+ @Test
+ public void scheduledCheck_UnexpectedError_IsLoggedAndKeptInTheTask() {
+ registerFile();
+ Runnable check = scheduledCheck();
+ when(fileWrapperFactory.getLastModified(anyString()))
+ .thenThrow(new IllegalStateException("disk went away"));
+ int errorsBefore = logger.errorsLogged.size();
+
+ try {
+ check.run();
+ } catch (RuntimeException e) {
+ fail("The error left the background task: " + e);
+ }
+
+ assertFalse("The error was not logged.",
+ logger.errorsLogged.size() == errorsBefore);
+ }
+
+ @Test
+ public void scheduledCheck_ErrorInACallback_IsLoggedAndKeptInTheTask() {
+ dataUpdate.onUpdateComplete((sender, args) -> {
+ throw new IllegalStateException("callback failed");
+ });
+ registerFile();
+ Runnable check = scheduledCheck();
+ when(fileWrapperFactory.getLastModified(anyString())).thenReturn(0L);
+ int errorsBefore = logger.errorsLogged.size();
+
+ try {
+ check.run();
+ } catch (RuntimeException e) {
+ fail("The error left the background task: " + e);
+ }
+
+ assertFalse("The error was not logged.",
+ logger.errorsLogged.size() == errorsBefore);
+ }
+}
diff --git a/pipeline/pipeline.jsonbuilder/src/test/java/fiftyone/pipeline/jsonbuilder/JsonBuilderMultiPipelineTests.java b/pipeline/pipeline.jsonbuilder/src/test/java/fiftyone/pipeline/jsonbuilder/JsonBuilderMultiPipelineTests.java
new file mode 100644
index 000000000..0b16dc2a6
--- /dev/null
+++ b/pipeline/pipeline.jsonbuilder/src/test/java/fiftyone/pipeline/jsonbuilder/JsonBuilderMultiPipelineTests.java
@@ -0,0 +1,150 @@
+/* *********************************************************************
+ * 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.pipeline.jsonbuilder;
+
+import fiftyone.pipeline.core.data.ElementData;
+import fiftyone.pipeline.core.data.ElementPropertyMetaData;
+import fiftyone.pipeline.core.data.ElementPropertyMetaDataDefault;
+import fiftyone.pipeline.core.data.FlowData;
+import fiftyone.pipeline.core.data.FlowError;
+import fiftyone.pipeline.core.data.PropertyMatcher;
+import fiftyone.pipeline.core.data.TryGetResult;
+import fiftyone.pipeline.core.data.types.JavaScript;
+import fiftyone.pipeline.core.flowelements.FlowElement;
+import fiftyone.pipeline.core.flowelements.Pipeline;
+import fiftyone.pipeline.jsonbuilder.data.JsonBuilderData;
+import fiftyone.pipeline.jsonbuilder.flowelements.JsonBuilderElement;
+import fiftyone.pipeline.jsonbuilder.flowelements.JsonBuilderElementBuilder;
+import org.json.JSONObject;
+import org.junit.jupiter.api.Test;
+import org.mockito.stubbing.Answer;
+import org.slf4j.LoggerFactory;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.anyString;
+import static org.mockito.ArgumentMatchers.eq;
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+/**
+ * One JSON Builder in two Pipelines keeps the configuration it reads from
+ * each Pipeline apart, and uses the right one for each. See
+ * multi-pipeline-elements.md line 17 :
+ * "If this feature is enabled, then all Flow Elements that maintain any
+ * such state information MUST ensure that they have the means to keep
+ * track of isolated instances of this state for each Pipeline to which
+ * they are added and use the correct state information when processing."
+ */
+public class JsonBuilderMultiPipelineTests {
+
+ /**
+ * A Pipeline whose "test" element has a JavaScript property, delayed or
+ * not.
+ */
+ private static Pipeline pipeline(boolean delayExecution) {
+ FlowElement, ?> element = mock(FlowElement.class);
+ doReturn("test").when(element).getElementDataKey();
+ ElementPropertyMetaData property = new ElementPropertyMetaDataDefault(
+ "jsproperty",
+ element,
+ "jsproperty",
+ JavaScript.class,
+ true,
+ null,
+ delayExecution,
+ null);
+ Map> metaData =
+ new HashMap<>();
+ metaData.put("test",
+ Collections.singletonMap("jsproperty", property));
+ Pipeline pipeline = mock(Pipeline.class);
+ when(pipeline.getElementAvailableProperties()).thenReturn(metaData);
+ return pipeline;
+ }
+
+ /**
+ * Process Flow Data of the Pipeline with the element and return the
+ * JSON of the "test" element.
+ */
+ @SuppressWarnings({ "unchecked", "rawtypes" })
+ private static JSONObject process(
+ JsonBuilderElement element,
+ Pipeline pipeline) throws Exception {
+ ElementData data = mock(ElementData.class);
+ Map values = new HashMap<>();
+ values.put("jsproperty", "var x = 1;");
+ doReturn(values).when(data).asKeyMap();
+ Map elementData = new HashMap<>();
+ elementData.put("test", data);
+
+ FlowData flowData = mock(FlowData.class);
+ when(flowData.elementDataAsMap()).thenReturn(elementData);
+ TryGetResult session = new TryGetResult<>();
+ session.setValue("session");
+ TryGetResult sequence = new TryGetResult<>();
+ sequence.setValue(1);
+ when(flowData.tryGetEvidence(eq("query.session-id"), any(Class.class)))
+ .thenReturn(session);
+ when(flowData.tryGetEvidence(eq("query.sequence"), any(Class.class)))
+ .thenReturn(sequence);
+ when(flowData.getWhere(any(PropertyMatcher.class)))
+ .thenReturn(new HashMap<>());
+ when(flowData.getPipeline()).thenReturn(pipeline);
+ when(flowData.getErrors()).thenReturn(new ArrayList());
+ final JsonBuilderData[] result = { null };
+ when(flowData.getOrAdd(anyString(), any(FlowElement.DataFactory.class)))
+ .thenAnswer((Answer) invocation -> {
+ FlowElement.DataFactory factory = invocation.getArgument(1);
+ result[0] = (JsonBuilderData) factory.create(flowData);
+ return result[0];
+ });
+
+ element.process(flowData);
+ return new JSONObject(result[0].getJson()).getJSONObject("test");
+ }
+
+ @Test
+ public void onePipelinesConfiguration_IsNotUsedForAnother()
+ throws Exception {
+ JsonBuilderElement element = new JsonBuilderElementBuilder(
+ LoggerFactory.getILoggerFactory()).build();
+ Pipeline delayed = pipeline(true);
+ Pipeline notDelayed = pipeline(false);
+
+ assertTrue(process(element, delayed).has("jspropertydelayexecution"),
+ "The first Pipeline's property is delayed.");
+ assertFalse(
+ process(element, notDelayed).has("jspropertydelayexecution"),
+ "The second Pipeline's configuration came from the first.");
+ assertTrue(process(element, delayed).has("jspropertydelayexecution"),
+ "The first Pipeline's configuration came from the second.");
+ }
+}
diff --git a/pipeline/pipeline.translation/src/main/java/fiftyone/pipeline/translation/flowelements/TranslationEngineBuilder.java b/pipeline/pipeline.translation/src/main/java/fiftyone/pipeline/translation/flowelements/TranslationEngineBuilder.java
index 3c460191d..39e157c75 100644
--- a/pipeline/pipeline.translation/src/main/java/fiftyone/pipeline/translation/flowelements/TranslationEngineBuilder.java
+++ b/pipeline/pipeline.translation/src/main/java/fiftyone/pipeline/translation/flowelements/TranslationEngineBuilder.java
@@ -22,6 +22,7 @@
package fiftyone.pipeline.translation.flowelements;
+import fiftyone.pipeline.annotations.DefaultValue;
import fiftyone.pipeline.annotations.ElementBuilder;
import fiftyone.pipeline.translation.data.MissingTranslationBehavior;
import fiftyone.pipeline.translation.data.TranslationData;
@@ -80,6 +81,7 @@ public TranslationEngineBuilder(ILoggerFactory loggerFactory) {
* @param sourceElementDataKey the source element data key
* @return this builder
*/
+ @DefaultValue("No default - the source element data key must be supplied")
public TranslationEngineBuilder setSourceElementDataKey(
String sourceElementDataKey) {
this.sourceElementDataKey = sourceElementDataKey;
@@ -93,6 +95,7 @@ public TranslationEngineBuilder setSourceElementDataKey(
* @param language the language to translate to
* @return this builder
*/
+ @DefaultValue("None - the language is resolved from the evidence")
public TranslationEngineBuilder setFixedLanguage(String language) {
this.fixedLanguage = language;
return this;
@@ -103,6 +106,7 @@ public TranslationEngineBuilder setFixedLanguage(String language) {
* @param behavior the missing-translation behaviour
* @return this builder
*/
+ @DefaultValue("ORIGINAL")
public TranslationEngineBuilder setMissingTranslationBehavior(
MissingTranslationBehavior behavior) {
this.behavior = behavior;
diff --git a/pipeline/pipeline.translation/src/test/java/fiftyone/pipeline/translation/flowelements/BuilderDefaultValueTests.java b/pipeline/pipeline.translation/src/test/java/fiftyone/pipeline/translation/flowelements/BuilderDefaultValueTests.java
new file mode 100644
index 000000000..4d28b2448
--- /dev/null
+++ b/pipeline/pipeline.translation/src/test/java/fiftyone/pipeline/translation/flowelements/BuilderDefaultValueTests.java
@@ -0,0 +1,68 @@
+/* *********************************************************************
+ * 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.pipeline.translation.flowelements;
+
+import fiftyone.pipeline.annotations.DefaultValue;
+import org.junit.jupiter.api.Test;
+
+import java.lang.reflect.Method;
+import java.lang.reflect.Modifier;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+/**
+ * Every configuration setter of the translation engine builder gives its
+ * default value beside it, with {@link DefaultValue}. See
+ * reference-implementation-notes.md line 79 :
+ * "In all cases, there SHOULD be some mechanism to specify the default
+ * values alongside configuration property setters."
+ */
+public class BuilderDefaultValueTests {
+
+ static List missingDefaults(Class>... builders) {
+ List missing = new ArrayList<>();
+ for (Class> builder : builders) {
+ for (Method method : builder.getDeclaredMethods()) {
+ if (Modifier.isPublic(method.getModifiers()) &&
+ method.getName().startsWith("set") &&
+ method.isSynthetic() == false &&
+ method.isBridge() == false &&
+ method.getAnnotation(DefaultValue.class) == null) {
+ missing.add(builder.getSimpleName() + "#" +
+ method.getName() +
+ Arrays.toString(method.getParameterTypes()));
+ }
+ }
+ }
+ return missing;
+ }
+
+ @Test
+ public void translationEngineBuilderSetters_GiveTheirDefaults() {
+ assertEquals(new ArrayList(),
+ missingDefaults(TranslationEngineBuilder.class));
+ }
+}
diff --git a/pipeline/pipeline.web.packages/pipeline.web/src/main/java/fiftyone/pipeline/web/PipelineFilter.java b/pipeline/pipeline.web.packages/pipeline.web/src/main/java/fiftyone/pipeline/web/PipelineFilter.java
index f68e9646c..f1ef653d4 100644
--- a/pipeline/pipeline.web.packages/pipeline.web/src/main/java/fiftyone/pipeline/web/PipelineFilter.java
+++ b/pipeline/pipeline.web.packages/pipeline.web/src/main/java/fiftyone/pipeline/web/PipelineFilter.java
@@ -63,6 +63,9 @@
* @see Specification
*/
public class PipelineFilter implements Filter {
+ private static final org.slf4j.Logger logger =
+ LoggerFactory.getLogger(PipelineFilter.class);
+
private Pipeline pipeline;
private WebRequestEvidenceServiceCore evidenceService;
@@ -200,42 +203,125 @@ public void doFilter(
ServletResponse response,
FilterChain chain) throws IOException, ServletException {
+ HttpServletRequest httpRequest = (HttpServletRequest) request;
+ HttpServletResponse httpResponse = (HttpServletResponse) response;
+ boolean suppress = isSuppressProcessExceptions();
+
+ boolean served;
+ try {
+ served = processRequest(httpRequest, httpResponse);
+ } catch (RuntimeException e) {
+ // The Pipeline's setting decides whether an error in the web
+ // integration stops the request. When exceptions are suppressed
+ // it is logged and the request goes on without the results, so
+ // the API does not take down the website. See
+ // https://github.com/51Degrees/specifications/blob/706de1f0cb3edd560eb7fd7ef3bb19b93a4883dc/pipeline-specification/features/exception-handling.md#L73
+ closeFlowData(httpRequest, suppress, e);
+ if (suppress == false) {
+ throw e;
+ }
+ logger.error("The 51Degrees Pipeline could not process the " +
+ "request, which goes on without its results.", e);
+ served = false;
+ } catch (IOException e) {
+ // Writing the JavaScript or JSON failed, for example because the
+ // client went away. The request ends here, so the FlowData is
+ // closed before the failure is thrown.
+ closeFlowData(httpRequest, suppress, e);
+ throw e;
+ }
+ if (served) {
+ closeFlowData(httpRequest, suppress, null);
+ return;
+ }
+
+ try {
+ chain.doFilter(request, response);//sends request to next resource
+ } finally {
+ // The rest of the filters in the chain have now been called, or
+ // one of them failed. Either way it is time to dispose of the
+ // FlowData instance.
+ closeFlowData(httpRequest, suppress, null);
+ }
+ }
+
+ /**
+ * Whether the Pipeline suppresses process exceptions, which also
+ * decides whether an error in this filter stops the request.
+ * @return true if exceptions are suppressed and logged
+ */
+ private boolean isSuppressProcessExceptions() {
+ return pipeline != null && pipeline.isSuppressProcessExceptions();
+ }
+
+ /**
+ * Process the request and set the response headers, and serve the
+ * 51Degrees JavaScript or JSON if that is what is asked for.
+ * @param request the request
+ * @param response the response
+ * @return true if the response has been served, so the request does not
+ * go on down the filter chain
+ * @throws IOException if the JavaScript or JSON could not be written
+ */
+ private boolean processRequest(
+ HttpServletRequest request,
+ HttpServletResponse response) throws IOException {
// A request for the 51Degrees JavaScript or JSON whose If-None-Match
// matches the ETag of its Evidence is answered without processing,
// because that Evidence alone decides the content.
- if (jsService.serveNotModified(
- (HttpServletRequest) request,
- (HttpServletResponse) response,
- resultService)) {
- return;
+ if (jsService.serveNotModified(request, response, resultService)) {
+ return true;
}
// Populate the request properties and store against the
// HttpContext.
- resultService.process((HttpServletRequest)request);
+ resultService.process(request);
+
+ // Get FlowData from the request.
+ FlowData flowData = flowDataProviderCore.getFlowData(request);
+
+ // Set UACH response headers.
+ uachServiceCore.setResponseHeaders(flowData, response);
+ // If 51Degrees JavaScript or JSON is being requested then serve it.
+ // Otherwise the request continues down the filter chain.
+ return jsService.serveJS(request, response) ||
+ jsService.serveJson(request, response);
+ }
+
+ /**
+ * Close the request's FlowData, if processing put one there.
+ * @param request the request
+ * @param suppress true if a failure to close is logged rather than
+ * thrown
+ * @param failure the failure already being handled, which a failure to
+ * close is added to, or null
+ * @throws ServletException if the FlowData could not be closed and
+ * exceptions are not suppressed
+ */
+ private void closeFlowData(
+ HttpServletRequest request,
+ boolean suppress,
+ Exception failure) throws ServletException {
+ FlowData flowData;
try {
- // Get FlowData from the request.
- FlowData flowData = flowDataProviderCore.getFlowData((HttpServletRequest)request);
-
- // Set UACH response headers.
- uachServiceCore.setResponseHeaders(
- flowData, (HttpServletResponse)response);
-
- // If 51Degrees JavaScript or JSON is being requested then serve it.
- // Otherwise continue down the filter Pipeline.
- if (jsService.serveJS((HttpServletRequest)request, (HttpServletResponse) response) == false &&
- jsService.serveJson((HttpServletRequest)request, (HttpServletResponse) response) == false) {
- chain.doFilter(request, response);//sends request to next resource
- }
- } finally {
- // The rest of the filters in the chain have now been called, or
- // one of them failed. Either way it is time to dispose of the
- // FlowData instance.
- try {
- flowDataProviderCore.getFlowData((HttpServletRequest) request).close();
- } catch (Exception e) {
- throw new ServletException("FlowData could not be disposed of.", e);
+ flowData = flowDataProviderCore.getFlowData(request);
+ } catch (RuntimeException e) {
+ flowData = null;
+ }
+ if (flowData == null) {
+ return;
+ }
+ try {
+ flowData.close();
+ } catch (Exception e) {
+ if (failure != null) {
+ failure.addSuppressed(e);
+ } else if (suppress) {
+ logger.error("FlowData could not be disposed of.", e);
+ } else {
+ throw new ServletException(
+ "FlowData could not be disposed of.", e);
}
}
}
diff --git a/pipeline/pipeline.web.packages/pipeline.web/src/test/java/fiftyone/pipeline/web/PipelineFilterErrorTests.java b/pipeline/pipeline.web.packages/pipeline.web/src/test/java/fiftyone/pipeline/web/PipelineFilterErrorTests.java
new file mode 100644
index 000000000..19bbb29cd
--- /dev/null
+++ b/pipeline/pipeline.web.packages/pipeline.web/src/test/java/fiftyone/pipeline/web/PipelineFilterErrorTests.java
@@ -0,0 +1,228 @@
+/* *********************************************************************
+ * 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.pipeline.web;
+
+import fiftyone.pipeline.core.data.FlowData;
+import fiftyone.pipeline.core.flowelements.Pipeline;
+import fiftyone.pipeline.web.services.FiftyOneJSServiceCore;
+import fiftyone.pipeline.web.services.FlowDataProviderCore;
+import fiftyone.pipeline.web.services.PipelineResultServiceCore;
+import fiftyone.pipeline.web.services.UACHServiceCore;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.stubbing.Answer;
+
+import javax.servlet.FilterChain;
+import javax.servlet.FilterConfig;
+import javax.servlet.ServletContext;
+import javax.servlet.ServletException;
+import javax.servlet.ServletRequest;
+import javax.servlet.ServletResponse;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.io.File;
+import java.io.IOException;
+import java.lang.reflect.Field;
+
+import static fiftyone.pipeline.web.Constants.HTTPCONTEXT_FLOWDATA;
+import static org.junit.Assert.assertSame;
+import static org.junit.Assert.fail;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.anyString;
+import static org.mockito.ArgumentMatchers.eq;
+import static org.mockito.Mockito.doThrow;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+/**
+ * An error in the web integration does not stop the customer's request
+ * when the Pipeline suppresses process exceptions: it is logged and the
+ * request goes on down the filter chain. When the Pipeline does not
+ * suppress them, the error is thrown as before. See
+ * exception-handling.md line 73 :
+ * "The implementor MUST ensure that exceptions/errors that happen in these
+ * areas are handled correctly in order to avoid the API taking down the
+ * customer's production website." and line 42, which gives
+ * IsSuppressProcessExceptions to wrappers "to make a decision on whether the
+ * exceptions should be suppressed and logged during processing."
+ */
+public class PipelineFilterErrorTests {
+
+ private PipelineFilter filter;
+ private HttpServletRequest request;
+ private HttpServletResponse response;
+ private FilterChain chain;
+ private Object storedFlowData;
+
+ @Before
+ public void init() throws Exception {
+ FilterConfig config = mock(FilterConfig.class);
+ when(config.getInitParameter(anyString())).thenAnswer(
+ (Answer) invocation ->
+ "config-file".equals(invocation.getArgument(0)) ?
+ "filename" : null);
+ ServletContext context = mock(ServletContext.class);
+ File file = new File(getClass().getClassLoader()
+ .getResource("config.xml").getFile());
+ when(context.getRealPath(anyString()))
+ .thenReturn(file.getAbsolutePath());
+ when(config.getServletContext()).thenReturn(context);
+
+ request = mock(HttpServletRequest.class);
+ response = mock(HttpServletResponse.class);
+ chain = mock(FilterChain.class);
+ when(request.getScheme()).thenReturn("");
+ when(request.getLocalAddr()).thenReturn("");
+ when(request.getRequestURL()).thenReturn(new StringBuffer(""));
+ when(request.getRequestURI()).thenReturn("");
+ when(request.getAttribute(eq(HTTPCONTEXT_FLOWDATA)))
+ .thenAnswer(invocation -> storedFlowData);
+ org.mockito.Mockito.doAnswer(invocation -> {
+ storedFlowData = invocation.getArgument(1);
+ return null;
+ }).when(request).setAttribute(eq(HTTPCONTEXT_FLOWDATA), any());
+
+ filter = new PipelineFilter();
+ filter.init(config);
+ }
+
+ @After
+ public void cleanUp() {
+ if (filter != null) {
+ filter.destroy();
+ }
+ }
+
+ private void set(String name, Object value) throws Exception {
+ Field field = PipelineFilter.class.getDeclaredField(name);
+ field.setAccessible(true);
+ field.set(filter, value);
+ }
+
+ private Object get(String name) throws Exception {
+ Field field = PipelineFilter.class.getDeclaredField(name);
+ field.setAccessible(true);
+ return field.get(filter);
+ }
+
+ /**
+ * Give the filter a Pipeline that reports the setting, keeping the
+ * real one to close.
+ */
+ private void suppress(boolean value) throws Exception {
+ Pipeline real = (Pipeline) get("pipeline");
+ Pipeline reporting = mock(Pipeline.class,
+ org.mockito.AdditionalAnswers.delegatesTo(real));
+ when(reporting.isSuppressProcessExceptions()).thenReturn(value);
+ set("pipeline", reporting);
+ }
+
+ private void failProcessing(RuntimeException failure) throws Exception {
+ PipelineResultServiceCore failing =
+ mock(PipelineResultServiceCore.class);
+ doThrow(failure).when(failing).process(any(HttpServletRequest.class));
+ set("resultService", failing);
+ }
+
+ @Test
+ public void processingFails_Suppressed_RequestGoesOn() throws Exception {
+ suppress(true);
+ failProcessing(new IllegalStateException("processing failed"));
+
+ filter.doFilter(request, response, chain);
+
+ verify(chain, times(1)).doFilter(
+ any(ServletRequest.class), any(ServletResponse.class));
+ }
+
+ @Test
+ public void headersFail_Suppressed_RequestGoesOn() throws Exception {
+ suppress(true);
+ UACHServiceCore failing = mock(UACHServiceCore.class);
+ doThrow(new IllegalStateException("headers failed")).when(failing)
+ .setResponseHeaders(any(FlowData.class),
+ any(HttpServletResponse.class));
+ set("uachServiceCore", failing);
+
+ filter.doFilter(request, response, chain);
+
+ verify(chain, times(1)).doFilter(
+ any(ServletRequest.class), any(ServletResponse.class));
+ }
+
+ @Test
+ public void processingFails_NotSuppressed_IsThrown() throws Exception {
+ suppress(false);
+ IllegalStateException failure =
+ new IllegalStateException("processing failed");
+ failProcessing(failure);
+
+ try {
+ filter.doFilter(request, response, chain);
+ fail("The failure was not thrown.");
+ } catch (IllegalStateException e) {
+ assertSame(failure, e);
+ } catch (ServletException e) {
+ fail("The failure was wrapped: " + e);
+ }
+ }
+
+ /**
+ * Serving the 51Degrees JavaScript fails with an IOException, for
+ * example when the client has gone away. The failure is thrown, and the
+ * Flow Data is still closed, so its Element Data and native results are
+ * released.
+ */
+ @Test
+ public void servingJavaScriptFails_IOException_FlowDataClosed()
+ throws Exception {
+ suppress(true);
+ set("resultService", mock(PipelineResultServiceCore.class));
+ set("uachServiceCore", mock(UACHServiceCore.class));
+ FlowData flowData = mock(FlowData.class);
+ FlowDataProviderCore provider = mock(FlowDataProviderCore.class);
+ when(provider.getFlowData(any(HttpServletRequest.class)))
+ .thenReturn(flowData);
+ set("flowDataProviderCore", provider);
+ FiftyOneJSServiceCore js = mock(FiftyOneJSServiceCore.class);
+ IOException failure = new IOException("client went away");
+ when(js.serveJS(any(HttpServletRequest.class),
+ any(HttpServletResponse.class))).thenThrow(failure);
+ set("jsService", js);
+
+ try {
+ filter.doFilter(request, response, chain);
+ fail("The failure was not thrown.");
+ } catch (IOException e) {
+ assertSame(failure, e);
+ }
+
+ verify(flowData, times(1)).close();
+ verify(chain, never()).doFilter(
+ any(ServletRequest.class), any(ServletResponse.class));
+ }
+}