Skip to content
Original file line number Diff line number Diff line change
@@ -0,0 +1,215 @@
/* *********************************************************************
* 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.hash.engine.onpremise.flowelements;

import fiftyone.devicedetection.hash.engine.onpremise.TestsBase;
import fiftyone.devicedetection.shared.DeviceData;
import fiftyone.pipeline.core.data.FlowData;
import fiftyone.pipeline.engines.Constants;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

/**
* Refreshing the engine's data, with the free Lite data file. Refreshes
* from several threads at once leave a working engine, a refresh from data
* that cannot be used fails and leaves the data the engine had, and
* processing while the data is refreshed gives the same values.
*/
public class DataRefreshTests extends TestsBase {

private static final int THREADS = 4;

private static final int REFRESHES = 5;

private static final long WAIT_SECONDS = 300;

private static final List<String> USER_AGENTS = Arrays.asList(
"Mozilla/5.0 (iPhone; CPU iPhone OS 17_0 like Mac OS X) " +
"AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.0 " +
"Mobile/15E148 Safari/604.1",
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 " +
"(KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36");

private ExecutorService executor;

@Before
public void init() throws Exception {
testInitialize(Constants.PerformanceProfiles.MaxPerformance);
executor = Executors.newFixedThreadPool(THREADS);
}

@After
public void cleanup() {
executor.shutdownNow();
testCleanup();
}

/**
* Several threads refresh the engine from its data file at once, and
* the engine then gives the values it gave before.
*/
@Test
public void Refresh_FromSeveralThreads_EngineStillWorks()
throws Exception {
List<Boolean> expected = isMobile();
DeviceDetectionHashEngine engine = getWrapper().getEngine();
String identifier = engine.getDataFileMetaData().getIdentifier();
CountDownLatch start = new CountDownLatch(1);
List<Future<Integer>> refreshers = new ArrayList<>();
for (int t = 0; t < THREADS; t++) {
refreshers.add(executor.submit(() -> {
start.await();
for (int i = 0; i < REFRESHES; i++) {
engine.refreshData(identifier);
}
return REFRESHES;
}));
}
start.countDown();
for (Future<Integer> refresher : refreshers) {
assertEquals(Integer.valueOf(REFRESHES),
refresher.get(WAIT_SECONDS, TimeUnit.SECONDS));
}
assertEquals(expected, isMobile());
}

/**
* A refresh from data that is not a data file fails with an error, and
* the engine goes on using the data it had.
*/
@Test
public void Refresh_DataNotUsable_FailsAndKeepsTheData() throws Exception {
List<Boolean> expected = isMobile();
DeviceDetectionHashEngine engine = getWrapper().getEngine();
String published = String.valueOf(
engine.getDataFileMetaData().getDataPublishedDateTime());
byte[] notData = "This is not a data file.".getBytes(
StandardCharsets.UTF_8);

boolean failed = false;
try {
engine.refreshData(
engine.getDataFileMetaData().getIdentifier(),
notData);
}
catch (RuntimeException e) {
failed = true;
}

assertTrue("A refresh from data that cannot be used did not fail.",
failed);
assertEquals(expected, isMobile());
assertEquals(published, String.valueOf(
engine.getDataFileMetaData().getDataPublishedDateTime()));
}

/**
* Threads process while another refreshes the engine from its data
* file, and every process gives the values the engine gave before.
*/
@Test
public void Process_WhileRefreshing_GivesTheSameValues()
throws Exception {
List<Boolean> expected = isMobile();
DeviceDetectionHashEngine engine = getWrapper().getEngine();
String identifier = engine.getDataFileMetaData().getIdentifier();
AtomicBoolean refreshing = new AtomicBoolean(true);
CountDownLatch start = new CountDownLatch(1);
List<Future<Integer>> processors = new ArrayList<>();
for (int t = 0; t < THREADS - 1; t++) {
processors.add(executor.submit(process(expected, refreshing, start)));
}
Future<Integer> refresher = executor.submit(() -> {
start.await();
try {
for (int i = 0; i < REFRESHES; i++) {
engine.refreshData(identifier);
}
}
finally {
refreshing.set(false);
}
return REFRESHES;
});
start.countDown();
assertEquals(Integer.valueOf(REFRESHES),
refresher.get(WAIT_SECONDS, TimeUnit.SECONDS));
for (Future<Integer> processor : processors) {
assertTrue("A thread processed nothing during the refreshes.",
processor.get(WAIT_SECONDS, TimeUnit.SECONDS) > 0);
}
}

private Callable<Integer> process(
List<Boolean> expected,
AtomicBoolean refreshing,
CountDownLatch start) {
return () -> {
start.await();
int processed = 0;
do {
List<Boolean> values = isMobile();
if (expected.equals(values) == false) {
fail("Processing during a refresh gave " + values +
" where " + expected + " was given before.");
}
processed++;
} while (refreshing.get());
return processed;
};
}

/**
* Whether each of the User-Agents is a mobile device.
*/
private List<Boolean> isMobile() throws Exception {
List<Boolean> values = new ArrayList<>();
for (String userAgent : USER_AGENTS) {
try (FlowData data =
getWrapper().getPipeline().createFlowData()) {
data.addEvidence("header.user-agent", userAgent);
data.process();
values.add(
data.get(DeviceData.class).getIsMobile().getValue());
}
}
return values;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,23 @@ public void addProfile(TestData profile) {
}
}

/**
* A property keyed cloud engine that declares no Evidence of its own
* takes the base class's default, which accepts no Evidence.
*/
@Test
public void propertyKeyedDefault_AcceptsNoEvidence() {
EvidenceKeyFilter filter = new TestKeyedInstance().getEvidenceKeyFilter();
for (String key : Arrays.asList(
"header.user-agent",
"query.client-ip",
"server.client-ip",
"cookie.51D_example")) {
assertFalse(filter.include(key),
"The default Evidence filter accepted '" + key + "'.");
}
}

private class TestKeyedInstance
extends PropertyKeyedCloudEngineBase<TestProfiles, TestData> {
public TestKeyedInstance() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/* *********************************************************************
* 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 static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

/**
* Every Flow Element declares the Evidence it accepts, because the base
* Flow Element leaves getEvidenceKeyFilter abstract, so the compiler makes
* each concrete element give one.
*/
public class AcceptedEvidenceDeclaredTests {

@Test
public void FlowElement_DeclaresTheEvidenceFilter() throws Exception {
Method method = FlowElement.class.getMethod("getEvidenceKeyFilter");
assertEquals(EvidenceKeyFilter.class, method.getReturnType());
assertTrue("The interface gives a default, so an element need not " +
"declare the Evidence it accepts.",
method.isDefault() == false);
}

@Test
public void FlowElementBase_LeavesTheEvidenceFilterAbstract()
throws Exception {
Method method = FlowElementBase.class.getDeclaredMethod(
"getEvidenceKeyFilter");
assertTrue("FlowElementBase gives a default, so an element need " +
"not declare the Evidence it accepts.",
Modifier.isAbstract(method.getModifiers()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,9 @@ enum AutoUpdateStatus {
// Refreshing the engine with the new data caused an error to occur.
AUTO_UPDATE_REFRESH_FAILED,
// An operation could not be completed within the expected time.
AUTO_UPDATE_TIMEOUT
AUTO_UPDATE_TIMEOUT,
// The data file checking for updates has no temporary file path.
AUTO_UPDATE_TEMP_PATH_NOT_SET
}

class DataUpdateCompleteArgs {
Expand Down
Loading
Loading