From 231bde66e77943df4dd8afa29edb62f708432e96 Mon Sep 17 00:00:00 2001 From: James Rosewell Date: Mon, 23 Nov 2026 08:53:25 +0000 Subject: [PATCH] TEST: Expect the cloud address the environment gives in engine tests The cloud request engine takes its address from code or configuration first, then the _51DEGREES_CLOUD_ENDPOINT environment variable, then https://cloud.51degrees.com/api/v4/ when both are blank. CI sets the variable to another service, so Process, Process_LicenseKey and OriginHeader in CloudRequestEngineTests failed because they expected the default address. ExpectedEndpoint works out the address a test that gives none should expect, the variable when it is set and not empty and the default otherwise, and CloudRequestEngineTestsBase builds expectedUrl from it. CloudRequestEngineBuilderTests gains three tests. An empty variable gives the default, the engine built in the real environment uses the address ExpectedEndpoint gives, and an address given in configuration is used for every request whatever the variable holds. --- .../CloudRequestEngineBuilderTests.java | 66 +++++++++++++++++++ .../CloudRequestEngineTestsBase.java | 5 +- .../flowelements/ExpectedEndpoint.java | 56 ++++++++++++++++ 3 files changed, 126 insertions(+), 1 deletion(-) create mode 100644 pipeline/pipeline.cloudrequestengine/src/test/java/fiftyone/pipeline/cloudrequestengine/flowelements/ExpectedEndpoint.java diff --git a/pipeline/pipeline.cloudrequestengine/src/test/java/fiftyone/pipeline/cloudrequestengine/flowelements/CloudRequestEngineBuilderTests.java b/pipeline/pipeline.cloudrequestengine/src/test/java/fiftyone/pipeline/cloudrequestengine/flowelements/CloudRequestEngineBuilderTests.java index 9dacf573f..e35b77bfe 100644 --- a/pipeline/pipeline.cloudrequestengine/src/test/java/fiftyone/pipeline/cloudrequestengine/flowelements/CloudRequestEngineBuilderTests.java +++ b/pipeline/pipeline.cloudrequestengine/src/test/java/fiftyone/pipeline/cloudrequestengine/flowelements/CloudRequestEngineBuilderTests.java @@ -23,18 +23,26 @@ package fiftyone.pipeline.cloudrequestengine.flowelements; import fiftyone.pipeline.cloudrequestengine.CloudRequestException; +import fiftyone.pipeline.core.configuration.ElementOptions; +import fiftyone.pipeline.core.configuration.PipelineOptions; import fiftyone.pipeline.core.exceptions.PipelineConfigurationException; +import fiftyone.pipeline.core.flowelements.Pipeline; +import fiftyone.pipeline.core.flowelements.PipelineBuilder; import fiftyone.pipeline.engines.services.HttpClientDefault; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.mockito.ArgumentCaptor; import org.slf4j.LoggerFactory; import java.io.IOException; import java.lang.reflect.Field; import java.net.MalformedURLException; +import java.net.URL; import java.util.function.Function; import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.Mockito.atLeastOnce; +import static org.mockito.Mockito.verify; public class CloudRequestEngineBuilderTests extends CloudRequestEngineTestsBase { private final String testResourceKey = "resource_key"; @@ -124,4 +132,62 @@ public void CloudEndPoint_Default_Setting() throws Exception { assertEquals( Constants.END_POINT_DEFAULT + "/", getEndpointValue(builder)); } + + /** + * A variable that is set but empty counts as not set, so the default + * is used. + */ + @Test + public void CloudEndPoint_Blank_Environment_Setting() throws Exception { + CloudRequestEngineBuilder builder = + new CloudRequestEngineBuilder(loggerFactory, httpClient); + setExpectedEnvVarValue(builder, ""); + + builder.setResourceKey(testResourceKey) + .build(); + assertEquals( + Constants.END_POINT_DEFAULT + "/", getEndpointValue(builder)); + } + + /** + * With no address given, the engine uses the address the environment + * this test runs in gives, which is what {@link ExpectedEndpoint} + * expects for the other tests. + */ + @Test + public void CloudEndPoint_Matches_Expected() throws Exception { + CloudRequestEngineBuilder builder = + new CloudRequestEngineBuilder(loggerFactory, httpClient); + + builder.setResourceKey(testResourceKey) + .build(); + assertEquals(ExpectedEndpoint.root(), getEndpointValue(builder)); + } + + /** + * An address given in configuration is used whatever the environment + * variable holds, so every request goes to that address. + */ + @Test + public void CloudEndPoint_Configuration_Setting() throws Exception { + ElementOptions element = new ElementOptions(); + element.builderName = "CloudRequestEngineBuilder"; + element.buildParameters.put("ResourceKey", testResourceKey); + element.buildParameters.put("Endpoint", testEndpoint); + PipelineOptions options = new PipelineOptions(); + options.elements.add(element); + + try (Pipeline pipeline = new PipelineBuilder(loggerFactory) + .addService(httpClient) + .buildFromConfiguration(options)) { + assertNotNull(pipeline.getElement(CloudRequestEngine.class)); + } + + ArgumentCaptor urls = ArgumentCaptor.forClass(URL.class); + verify(httpClient, atLeastOnce()).connect(urls.capture()); + for (URL url : urls.getAllValues()) { + assertTrue(url.toString().startsWith(testEndpoint), + url.toString()); + } + } } diff --git a/pipeline/pipeline.cloudrequestengine/src/test/java/fiftyone/pipeline/cloudrequestengine/flowelements/CloudRequestEngineTestsBase.java b/pipeline/pipeline.cloudrequestengine/src/test/java/fiftyone/pipeline/cloudrequestengine/flowelements/CloudRequestEngineTestsBase.java index dfbd106b4..ce69af6b9 100644 --- a/pipeline/pipeline.cloudrequestengine/src/test/java/fiftyone/pipeline/cloudrequestengine/flowelements/CloudRequestEngineTestsBase.java +++ b/pipeline/pipeline.cloudrequestengine/src/test/java/fiftyone/pipeline/cloudrequestengine/flowelements/CloudRequestEngineTestsBase.java @@ -46,7 +46,10 @@ public class CloudRequestEngineTestsBase { HttpClient httpClient; protected TestLoggerFactory loggerFactory; - protected URL expectedUrl = URI.create("https://cloud.51degrees.com/api/v4/resource_key.json").toURL(); + // The data address for the test resource key when a test gives no + // address, which follows the environment the test runs in. + protected URL expectedUrl = + URI.create(ExpectedEndpoint.root() + "resource_key.json").toURL(); protected String jsonResponse = "{'device':{'value':'1'}}"; protected String evidenceKeysResponse = "['query.User-Agent']"; protected String accessiblePropertiesResponse = diff --git a/pipeline/pipeline.cloudrequestengine/src/test/java/fiftyone/pipeline/cloudrequestengine/flowelements/ExpectedEndpoint.java b/pipeline/pipeline.cloudrequestengine/src/test/java/fiftyone/pipeline/cloudrequestengine/flowelements/ExpectedEndpoint.java new file mode 100644 index 000000000..a4ac0e68b --- /dev/null +++ b/pipeline/pipeline.cloudrequestengine/src/test/java/fiftyone/pipeline/cloudrequestengine/flowelements/ExpectedEndpoint.java @@ -0,0 +1,56 @@ +/* ********************************************************************* + * 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; + +/** + * The root address a test should expect when it gives the engine no + * address in code or configuration. That is the + * {@link Constants#CLOUD_ENDPOINT_VARIABLE} environment variable when it + * is set and not empty, otherwise {@link Constants#END_POINT_DEFAULT}, + * ending in "/" as the builder makes it. + */ +final class ExpectedEndpoint { + + private ExpectedEndpoint() { + } + + /** + * The root address for the environment this test runs in. + * @return the address, ending in "/" + */ + static String root() { + return root(System.getenv(Constants.CLOUD_ENDPOINT_VARIABLE)); + } + + /** + * The root address for a given value of the environment variable. + * @param variable the value of the variable, or null when it is unset + * @return the address, ending in "/" + */ + static String root(String variable) { + String root = variable == null || variable.isEmpty() + ? Constants.END_POINT_DEFAULT + : variable; + return root.endsWith("/") ? root : root + "/"; + } +}