From b5a560b903d25ea14c1f0db6cf5b87f8e98e7b1c Mon Sep 17 00:00:00 2001 From: James Rosewell Date: Mon, 23 Nov 2026 21:54:06 +0000 Subject: [PATCH 1/3] TEST: Run each Human Confidence live test with its own tier key HumanConfidenceLiveTest had one test that read _51DEGREES_RESOURCE_KEY and took the source it expected from a system property, and the workflow ran it once for each key. It skipped when the cloud service did not list HumanConfidenceSource for the key. There are now two tests. The Bespoke test reads _51DEGREES_RESOURCE_KEY_BESPOKE and expects the source Passive and a level of High, Medium or Low. The Basic test reads _51DEGREES_RESOURCE_KEY_BASIC and expects no source. Each starts the example with the key it checks. A test skips only when its own key is not set, and fails when the answer lacks what it checks. cloud-human-confidence.yml runs Maven once with both keys under their own names. It fails when a key is empty, when no test ran, when Maven fails or when a test was skipped. New Pester tests in ci/workflow-keys.Tests.ps1 cover that step. --- .github/workflows/cloud-human-confidence.yml | 79 +++++------ ci/README.md | 5 +- ci/workflow-keys.Tests.ps1 | 124 ++++++++++++++++++ .../web/human-confidence/README.md | 19 +-- .../examples/web/HumanConfidenceLiveTest.java | 106 ++++++++------- 5 files changed, 221 insertions(+), 112 deletions(-) diff --git a/.github/workflows/cloud-human-confidence.yml b/.github/workflows/cloud-human-confidence.yml index 68aac8eb9..cad16cfe9 100644 --- a/.github/workflows/cloud-human-confidence.yml +++ b/.github/workflows/cloud-human-confidence.yml @@ -1,19 +1,18 @@ name: Cloud Human Confidence Tests -# Runs the Human Confidence web example's live test +# Runs the Human Confidence web example's live tests # (examples/device-detection/web/human-confidence, HumanConfidenceLiveTest) -# once for each resource key below. Each pass sets the single -# _51DEGREES_RESOURCE_KEY the example reads, and tells the test with -# -DhumanConfidenceSource what that key's answer carries. The Bespoke key's -# products offer HumanConfidenceSource, so its answer carries Passive. The -# Basic key's products don't, so its answer carries no source. +# once. Each test reads its own resource key under the key's own name. The +# Bespoke key's products offer HumanConfidenceSource, so its answer carries +# Passive and a level. The Basic key's products don't, so its answer carries +# no source. # # The resource keys are organization secrets, and _51DEGREES_CLOUD_ENDPOINT, # the service the example calls, is an organization variable. A new key needs -# a line here as well as the organization secret. +# a line here and in the test, as well as the organization secret. # -# A pass fails when its key is empty or when the test is skipped. A cloud -# test that passes without calling the cloud reports success having proved +# The run fails when a key is empty or when a test is skipped. A cloud test +# that passes without calling the cloud reports success having proved # nothing, and that is worse than a red job. on: @@ -42,7 +41,7 @@ jobs: distribution: temurin java-version: '17' cache: maven - - name: Human Confidence test for each resource key + - name: Human Confidence tests shell: pwsh working-directory: examples/device-detection env: @@ -50,44 +49,30 @@ jobs: _51DEGREES_RESOURCE_KEY_BASIC: ${{ secrets._51DEGREES_RESOURCE_KEY_BASIC }} _51DEGREES_CLOUD_ENDPOINT: ${{ vars._51DEGREES_CLOUD_ENDPOINT }} run: | - # Each key, with the source its answer carries. - $passes = [ordered]@{ - _51DEGREES_RESOURCE_KEY_BESPOKE = 'Passive' - _51DEGREES_RESOURCE_KEY_BASIC = 'none' + # Each test skips without its key, so a missing key fails here, + # naming the organization secret. + $missing = @( + '_51DEGREES_RESOURCE_KEY_BESPOKE' + '_51DEGREES_RESOURCE_KEY_BASIC' + ) | Where-Object { -not [Environment]::GetEnvironmentVariable($_) } + if ($missing) { + throw ("The organization secret $($missing -join ', ') is " + + 'empty or missing, so the live tests cannot run with it.') } $report = 'web/human-confidence/target/surefire-reports/' + 'TEST-fiftyone.devicedetection.examples.web.HumanConfidenceLiveTest.xml' - $failed = $false - foreach ($name in $passes.Keys) { - Write-Host "::group::Live test with $name" - $key = [Environment]::GetEnvironmentVariable($name) - if (-not $key) { - Write-Host ("::error::The organization secret $name is " + - 'empty or missing, so the live test cannot run with it.') - $failed = $true - Write-Host '::endgroup::' - continue - } - $env:_51DEGREES_RESOURCE_KEY = $key - Remove-Item $report -ErrorAction SilentlyContinue - mvn -B --no-transfer-progress -pl web/human-confidence -am test ` - '-Dtest=HumanConfidenceLiveTest' ` - '-Dsurefire.failIfNoSpecifiedTests=false' ` - "-DhumanConfidenceSource=$($passes[$name])" - if ($LASTEXITCODE -ne 0) { - $failed = $true - } elseif (-not (Test-Path $report)) { - Write-Host "::error::The live test with $name did not run." - $failed = $true - } else { - $suite = ([xml](Get-Content -Raw $report)).testsuite - if ([int]$suite.tests -eq 0 -or [int]$suite.skipped -gt 0) { - Write-Host ("::error::The live test with $name was " + - "skipped, so it proved nothing. " + - "$($suite.testcase.skipped.message)") - $failed = $true - } - } - Write-Host '::endgroup::' + Remove-Item $report -ErrorAction SilentlyContinue + mvn -B --no-transfer-progress -pl web/human-confidence -am test ` + '-Dtest=HumanConfidenceLiveTest' ` + '-Dsurefire.failIfNoSpecifiedTests=false' + if ($LASTEXITCODE -ne 0) { + throw 'The live tests failed.' + } + if (-not (Test-Path $report)) { + throw 'The live tests did not run.' + } + $suite = ([xml](Get-Content -Raw $report)).testsuite + if ([int]$suite.tests -eq 0 -or [int]$suite.skipped -gt 0) { + throw ('A live test was skipped, so it proved nothing. ' + + "$($suite.testcase.skipped.message)") } - if ($failed) { exit 1 } diff --git a/ci/README.md b/ci/README.md index eb4c7d94b..03a01dacd 100644 --- a/ci/README.md +++ b/ci/README.md @@ -144,8 +144,9 @@ organization secrets: - `_51DEGREES_RESOURCE_KEY_BASIC`: resource key whose products don't offer `HumanConfidenceSource`. [cloud-human-confidence.yml](../.github/workflows/cloud-human-confidence.yml) - runs the Human Confidence example's live test with it, expecting no - source, and with `_51DEGREES_RESOURCE_KEY_BESPOKE`, expecting `Passive`. + runs the Human Confidence example's live tests, one of which reads it and + expects no source, while the other reads `_51DEGREES_RESOURCE_KEY_BESPOKE` + and expects `Passive`. - `_51DEGREES_CLOUD_ENDPOINT`: an organization variable naming the cloud service the cloud tests and examples call, the API base including `/api/v4/`. They call the public service when it is not set. diff --git a/ci/workflow-keys.Tests.ps1 b/ci/workflow-keys.Tests.ps1 index 238962c85..b3ea07098 100644 --- a/ci/workflow-keys.Tests.ps1 +++ b/ci/workflow-keys.Tests.ps1 @@ -164,4 +164,128 @@ Describe 'the workflows' { } | Should -Not -BeNullOrEmpty } } + + # cloud-human-confidence.yml runs the Human Confidence live tests once. + # Each test reads its own key under its own name, so the step sets no + # name of its own. The step's script runs here with mvn replaced by a + # function that records what it is handed and writes the report Maven + # would write. + Context 'cloud-human-confidence.yml' { + + BeforeAll { + $workflow = Join-Path $PSScriptRoot '../.github/workflows/cloud-human-confidence.yml' + $script:Lines = @(Get-Content $workflow) + $start = $script:Lines | Select-String -Pattern '^(\s*)run: \|\s*$' | + Select-Object -First 1 + $indent = $start.Matches[0].Groups[1].Value.Length + $body = foreach ($line in $script:Lines[$start.LineNumber..($script:Lines.Count - 1)]) { + if ($line.Trim() -and $line -notmatch "^\s{$($indent + 1),}") { break } + if ($line.Length -gt $indent + 2) { $line.Substring($indent + 2) } else { '' } + } + $script:Step = [scriptblock]::Create($body -join "`n") + $script:Report = 'web/human-confidence/target/surefire-reports/' + + 'TEST-fiftyone.devicedetection.examples.web.HumanConfidenceLiveTest.xml' + + # Writes the report of a run of the given number of tests, of + # which the given number were skipped. + function script:Write-Report([int]$Tests, [int]$Skipped) { + New-Item -ItemType Directory -Force (Split-Path $script:Report) | Out-Null + $cases = (1..$Tests | ForEach-Object { + if ($_ -le $Skipped) { + "" + } else { + "" + } + }) -join '' + Set-Content $script:Report ("$cases") + } + } + + BeforeEach { + $script:Saved = @(Get-ChildItem env: | + Where-Object { $_.Name -like '_51DEGREES_*' }) + $script:Saved | ForEach-Object { Remove-Item "Env:$($_.Name)" } + $env:_51DEGREES_RESOURCE_KEY_BESPOKE = 'resource-bespoke' + $env:_51DEGREES_RESOURCE_KEY_BASIC = 'resource-basic' + Push-Location TestDrive: + } + + AfterEach { + Pop-Location + Remove-Item TestDrive:/web -Recurse -Force -ErrorAction SilentlyContinue + Get-ChildItem env: | Where-Object { $_.Name -like '_51DEGREES_*' } | + ForEach-Object { Remove-Item "Env:$($_.Name)" } + $script:Saved | ForEach-Object { Set-Item "Env:$($_.Name)" $_.Value } + } + + It 'runs the tests once, with each key under its own name and no other' { + $runs = [System.Collections.Generic.List[object]]::new() + function mvn { + $names = @(Get-ChildItem env: | + Where-Object { $_.Name -like '_51DEGREES_*' } | + ForEach-Object { "$($_.Name)=$($_.Value)" } | Sort-Object) + $runs.Add(@{ Names = $names; Arguments = "$args" }) + Write-Report -Tests 2 -Skipped 0 + $global:LASTEXITCODE = 0 + } + { & $script:Step 6> $null } | Should -Not -Throw + + $runs.Count | Should -Be 1 + $runs[0].Names | Should -Be @( + '_51DEGREES_RESOURCE_KEY_BASIC=resource-basic' + '_51DEGREES_RESOURCE_KEY_BESPOKE=resource-bespoke') + $runs[0].Arguments | Should -BeLike '*-Dtest=HumanConfidenceLiveTest*' + $runs[0].Arguments | Should -Not -BeLike '*humanConfidenceSource*' + } + + # Each test needs its own key, so a missing one means a test that + # would skip, and the job would still pass having proved nothing. + It 'fails naming when it is empty' -ForEach @( + @{ Name = '_51DEGREES_RESOURCE_KEY_BESPOKE' } + @{ Name = '_51DEGREES_RESOURCE_KEY_BASIC' } + ) { + $runs = [System.Collections.Generic.List[object]]::new() + function mvn { + $runs.Add('run') + $global:LASTEXITCODE = 0 + } + Remove-Item "Env:$Name" + + { & $script:Step 6> $null } | Should -Throw "*$Name*" + $runs | Should -BeNullOrEmpty + } + + It 'fails when a test was skipped' { + function mvn { + Write-Report -Tests 2 -Skipped 1 + $global:LASTEXITCODE = 0 + } + + { & $script:Step 6> $null } | Should -Throw '*skipped*' + } + + It 'fails when the tests did not run' { + function mvn { + $global:LASTEXITCODE = 0 + } + + { & $script:Step 6> $null } | Should -Throw '*did not run*' + } + + It 'fails when Maven fails' { + function mvn { + Write-Report -Tests 2 -Skipped 0 + $global:LASTEXITCODE = 1 + } + + { & $script:Step 6> $null } | Should -Throw + } + + It 'takes the cloud endpoint from the variable of the same name' { + $script:Lines | Where-Object { + $_ -match '^\s*_51DEGREES_CLOUD_ENDPOINT:\s*\$\{\{\s*vars\._51DEGREES_CLOUD_ENDPOINT\s*\}\}\s*$' + } | Should -Not -BeNullOrEmpty + } + } } diff --git a/examples/device-detection/web/human-confidence/README.md b/examples/device-detection/web/human-confidence/README.md index 1de0b961d..787e6e0b2 100644 --- a/examples/device-detection/web/human-confidence/README.md +++ b/examples/device-detection/web/human-confidence/README.md @@ -105,22 +105,23 @@ tests, or by itself from `examples/device-detection`. ../../mvnw -pl web/human-confidence -am test ``` -`HumanConfidenceLiveTest` starts the server with the key in -`_51DEGREES_RESOURCE_KEY` and the cloud service that +`HumanConfidenceLiveTest` starts the server against the cloud service that `_51DEGREES_CLOUD_ENDPOINT` names, and asks it for the passive result as the -page does. By default the answer must have `HumanConfidenceSource` set to -`Passive` and a plain level. For a key whose products don't offer the source, -give `-DhumanConfidenceSource=none`, and the answer must have no source. +page does. It has two tests, and each gives the server the key it checks. +With the key in `_51DEGREES_RESOURCE_KEY_BESPOKE` the answer must have +`HumanConfidenceSource` set to `Passive` and a plain level. With the key in +`_51DEGREES_RESOURCE_KEY_BASIC`, whose products don't offer the source, the +answer must have no source. ``` ../../mvnw -pl web/human-confidence -am test -Dtest=HumanConfidenceLiveTest -Dsurefire.failIfNoSpecifiedTests=false ``` -The test is skipped when `_51DEGREES_RESOURCE_KEY` is not set, and when the -service doesn't serve `HumanConfidenceSource`. The `Active` source needs the -client script to run in a browser, so the test doesn't check it. +Each test is skipped when its key is not set, and fails when the answer +lacks what it checks. The `Active` source needs the client script to run in +a browser, so the tests don't check it. [cloud-human-confidence.yml](../../../../.github/workflows/cloud-human-confidence.yml) -runs it once for each of the CI resource keys. +runs them with the CI resource keys. ## What a run costs diff --git a/examples/device-detection/web/human-confidence/src/test/java/fiftyone/devicedetection/examples/web/HumanConfidenceLiveTest.java b/examples/device-detection/web/human-confidence/src/test/java/fiftyone/devicedetection/examples/web/HumanConfidenceLiveTest.java index 5eeec7fe8..5338be291 100644 --- a/examples/device-detection/web/human-confidence/src/test/java/fiftyone/devicedetection/examples/web/HumanConfidenceLiveTest.java +++ b/examples/device-detection/web/human-confidence/src/test/java/fiftyone/devicedetection/examples/web/HumanConfidenceLiveTest.java @@ -43,27 +43,29 @@ import static org.junit.Assume.assumeTrue; /** - * Runs the example against the live cloud service, with the Resource Key in - * _51DEGREES_RESOURCE_KEY, and asks its /passive path as the page does. The - * service is the one _51DEGREES_CLOUD_ENDPOINT names, or production. + * Runs the example against the live cloud service and asks its /passive path + * as the page does. The service is the one _51DEGREES_CLOUD_ENDPOINT names, + * or production. *

- * The system property humanConfidenceSource says what the key's answer - * carries. Passive, the default, is for a key whose products offer - * HumanConfidenceSource, and none is for a key whose products don't. The - * test is skipped when the key isn't set, and when the service doesn't - * serve HumanConfidenceSource at all. Keys are never printed. + * Each test reads its own key. The key in _51DEGREES_RESOURCE_KEY_BESPOKE has + * products that offer HumanConfidenceSource, so its answer carries the source + * Passive and a level. The key in _51DEGREES_RESOURCE_KEY_BASIC has products + * that don't, so its answer carries no source. The example is given the key + * the test checks, as a person running it would set it in + * _51DEGREES_RESOURCE_KEY. A test is skipped when its key isn't set, naming + * the variable, and fails when the answer lacks what it checks. Keys are + * never printed. */ public class HumanConfidenceLiveTest { - private static final String RESOURCE_KEY = "_51DEGREES_RESOURCE_KEY"; + /** The key whose products offer HumanConfidenceSource. */ + private static final String BESPOKE_KEY = + "_51DEGREES_RESOURCE_KEY_BESPOKE"; - private static final String ENDPOINT = "_51DEGREES_CLOUD_ENDPOINT"; - - /** The system property naming the source the key's answer carries. */ - private static final String EXPECTED_SOURCE = "humanConfidenceSource"; + /** The key whose products don't offer HumanConfidenceSource. */ + private static final String BASIC_KEY = "_51DEGREES_RESOURCE_KEY_BASIC"; - /** The expected source for a key whose products don't offer it. */ - private static final String NONE = "none"; + private static final String ENDPOINT = "_51DEGREES_CLOUD_ENDPOINT"; private static final String PASSIVE = "Passive"; @@ -86,52 +88,53 @@ public void stop() { } @Test - public void passiveAnswerCarriesTheKeysSource() throws IOException { - String expected = expectedSource(); - String key = key(); + public void bespokeKeysPassiveAnswerCarriesTheSourcePassive() + throws IOException { + String key = key(BESPOKE_KEY); JSONObject served = servedSource(key); - example = new HumanConfidence(api(), key, null); - int port = example.start(0); - JSONObject answer = passive(port); + JSONObject answer = passive(key); String text = redact(answer.toString(), key); assertFalse("The example's passive call failed: " + text, answer.has("error")); - if (NONE.equals(expected)) { - assertFalse("This key's answer carries no source: " + text - + ". The service says: " - + redact(served.toString(), key), - answer.has("source")); - } else { - assertEquals("This key's answer carries the source " + PASSIVE - + ": " + text + ". The service says: " - + redact(served.toString(), key), - PASSIVE, answer.optString("source", null)); - assertTrue("A passive level is one of the plain levels: " - + text, - LEVELS.contains(answer.optString( - "humanConfidence", null))); - } + assertEquals("This key's answer carries the source " + PASSIVE + + ": " + text + ". The service says: " + + redact(served.toString(), key), + PASSIVE, answer.optString("source", null)); + assertTrue("A passive level is one of the plain levels: " + text, + LEVELS.contains(answer.optString("humanConfidence", null))); } - /** Passive or none, from the system property, or a failure. */ - private static String expectedSource() { - String expected = System.getProperty(EXPECTED_SOURCE, PASSIVE).trim(); - assertTrue(EXPECTED_SOURCE + " is " + PASSIVE + " or " + NONE - + ", not " + expected, - PASSIVE.equals(expected) || NONE.equals(expected)); - return expected; + @Test + public void basicKeysPassiveAnswerCarriesNoSource() throws IOException { + String key = key(BASIC_KEY); + JSONObject served = servedSource(key); + JSONObject answer = passive(key); + String text = redact(answer.toString(), key); + + assertFalse("The example's passive call failed: " + text, + answer.has("error")); + assertFalse("This key's answer carries no source: " + text + + ". The service says: " + + redact(served.toString(), key), + answer.has("source")); } - /** The key in _51DEGREES_RESOURCE_KEY, or a skip that names it. */ - private static String key() { - String key = System.getenv(RESOURCE_KEY); - assumeTrue(RESOURCE_KEY + " is not set, so this live cloud test is " + /** The key in the variable name, or a skip that names it. */ + private static String key(String name) { + String key = System.getenv(name); + assumeTrue(name + " is not set, so this live cloud test is " + "skipped. " + ENDPOINT + " names the service.", key != null && key.trim().isEmpty() == false); return key.trim(); } + /** Starts the example with the key and asks it for /passive. */ + private JSONObject passive(String key) throws IOException { + example = new HumanConfidence(api(), key, null); + return passive(example.start(0)); + } + /** The API base the example calls, ending in one slash. */ private static String api() { String endpoint = System.getenv(ENDPOINT); @@ -143,9 +146,8 @@ private static String api() { /** * Asks the service for HumanConfidenceSource with the key, and returns - * the source's value and null reason from the answer. Skips the test - * when the answer has neither, as the service doesn't serve the - * property, so no key can get it there. + * the source's value and null reason from the answer, for a failure to + * quote. */ private static JSONObject servedSource(String key) throws IOException { String form = "user-agent=" + encode(USER_AGENT) @@ -177,10 +179,6 @@ private static JSONObject servedSource(String key) throws IOException { } } } - assumeTrue("The service " + ENDPOINT + " names, or production when " - + "it isn't set, doesn't serve HumanConfidenceSource, " - + "so this test is skipped.", - served.length() > 0); return served; } From 8e2cb1cbce1999266587c4ce9fa901782ba4dac5 Mon Sep 17 00:00:00 2001 From: Mykhailo Dubish Date: Tue, 24 Nov 2026 02:44:58 +0000 Subject: [PATCH 2/3] TEST: Check the Bespoke key includes the Human Confidence properties first The Bespoke live test now asks the staging cloud which properties the key in RESOURCE_KEY_BESPOKE_STAGING includes before it calls the example. If HumanConfidence or HumanConfidenceSource is not listed, the test fails and names the variable and the property, so it is clear which key to change. The source and level messages name the variable as well. A test that needs no network checks how the list of properties is read. --- .../web/human-confidence/README.md | 5 +- .../examples/web/HumanConfidenceLiveTest.java | 88 ++++++++++++++++++- 2 files changed, 88 insertions(+), 5 deletions(-) diff --git a/examples/device-detection/web/human-confidence/README.md b/examples/device-detection/web/human-confidence/README.md index 1fbb23ee7..19a447ed0 100644 --- a/examples/device-detection/web/human-confidence/README.md +++ b/examples/device-detection/web/human-confidence/README.md @@ -111,7 +111,10 @@ passive result as the page does. The production cloud does not serve cloud, with two names that only CI sets. `CLOUD_STAGING_ENDPOINT` names the cloud, the API base including `/api/v4/`, and `RESOURCE_KEY_BESPOKE_STAGING` holds a Bespoke key that cloud resolves. With that key the answer must have -`HumanConfidenceSource` set to `Passive` and a plain level. With a key whose +`HumanConfidenceSource` set to `Passive` and a plain level. The test first +asks the cloud which properties the key includes, and fails, naming the +variable and the property, when `HumanConfidence` or `HumanConfidenceSource` +is not one of them. With a key whose products don't offer the source, in `_51DEGREES_RESOURCE_KEY_BASIC`, the cloud must answer the source as null, with the reason, and the example's answer must have no source. diff --git a/examples/device-detection/web/human-confidence/src/test/java/fiftyone/devicedetection/examples/web/HumanConfidenceLiveTest.java b/examples/device-detection/web/human-confidence/src/test/java/fiftyone/devicedetection/examples/web/HumanConfidenceLiveTest.java index 83c1be02c..3344e8c1c 100644 --- a/examples/device-detection/web/human-confidence/src/test/java/fiftyone/devicedetection/examples/web/HumanConfidenceLiveTest.java +++ b/examples/device-detection/web/human-confidence/src/test/java/fiftyone/devicedetection/examples/web/HumanConfidenceLiveTest.java @@ -22,6 +22,7 @@ package fiftyone.devicedetection.examples.web; +import org.json.JSONArray; import org.json.JSONObject; import org.junit.After; import org.junit.Test; @@ -34,12 +35,16 @@ import java.net.URL; import java.net.URLEncoder; import java.nio.charset.StandardCharsets; +import java.util.ArrayList; import java.util.Arrays; +import java.util.HashSet; import java.util.List; +import java.util.Set; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; import static org.junit.Assume.assumeTrue; /** @@ -57,7 +62,9 @@ * test checks, as a person running it would set it in * _51DEGREES_RESOURCE_KEY. A test is skipped when its key or * CLOUD_STAGING_ENDPOINT isn't set, naming the variable, and fails when the - * answer lacks what it checks. Keys are never printed. + * answer lacks what it checks. The Bespoke test first fails, naming the + * variable and the property, when the key doesn't include HumanConfidence or + * HumanConfidenceSource. Keys are never printed. */ public class HumanConfidenceLiveTest { @@ -102,17 +109,21 @@ public void stop() { public void bespokeKeysPassiveAnswerCarriesTheSourcePassive() throws IOException { String key = key(BESPOKE_KEY); + requireProperties(BESPOKE_KEY, key, + "HumanConfidence", "HumanConfidenceSource"); JSONObject served = servedSource(key); JSONObject answer = passive(key); String text = redact(answer.toString(), key); assertFalse("The example's passive call failed: " + text, answer.has("error")); - assertEquals("This key's answer carries the source " + PASSIVE - + ": " + text + ". The service says: " + assertEquals("The answer for the key in " + BESPOKE_KEY + + " carries the source " + PASSIVE + ": " + text + + ". The service says: " + redact(served.toString(), key), PASSIVE, answer.optString("source", null)); - assertTrue("A passive level is one of the plain levels: " + text, + assertTrue("The passive level for the key in " + BESPOKE_KEY + + " is one of the plain levels: " + text, LEVELS.contains(answer.optString("humanConfidence", null))); } @@ -137,6 +148,21 @@ public void basicKeysPassiveAnswerCarriesNoSource() throws IOException { answer.has("source")); } + @Test + public void missingPropertiesNamesOnlyThePropertiesNoProductLists() { + JSONObject accessible = new JSONObject( + "{\"Products\":{" + + "\"device\":{\"Properties\":[{\"Name\":\"IsMobile\"}]}," + + "\"derived\":{\"Properties\":[" + + "{\"Name\":\"HumanConfidence\"}]}}}"); + + assertEquals(Arrays.asList("HumanConfidenceSource"), + missingProperties(accessible, + "HumanConfidence", "HumanConfidenceSource")); + assertEquals(Arrays.asList("HumanConfidence"), + missingProperties(new JSONObject("{}"), "HumanConfidence")); + } + /** The key in the variable name, or a skip that names it. */ private static String key(String name) { String key = System.getenv(name); @@ -146,6 +172,60 @@ private static String key(String name) { return key.trim(); } + /** + * Fails, naming the variable and the property, when the service's list + * of the properties the key can read lacks one of them. + */ + private static void requireProperties(String name, String key, + String... properties) throws IOException { + HttpURLConnection connection = (HttpURLConnection) + new URL(api() + "accessibleproperties?resource=" + key) + .openConnection(); + connection.setConnectTimeout(TIMEOUT_MS); + connection.setReadTimeout(TIMEOUT_MS); + int status = connection.getResponseCode(); + String body = read(status >= 400 + ? connection.getErrorStream() : connection.getInputStream()); + assertEquals("The service didn't list the properties of the key in " + + name + ": " + redact(body, key), 200, status); + for (String property : missingProperties( + new JSONObject(body), properties)) { + fail("The key in " + name + " does not include the " + property + + " property. Set the variable to a key that includes " + + "it."); + } + } + + /** + * The properties, of those given, that no product in the service's list + * of accessible properties names. + */ + static List missingProperties(JSONObject accessible, + String... properties) { + Set names = new HashSet<>(); + JSONObject products = accessible.optJSONObject("Products"); + if (products != null) { + for (String product : products.keySet()) { + JSONObject entry = products.optJSONObject(product); + JSONArray list = entry == null + ? null : entry.optJSONArray("Properties"); + for (int i = 0; list != null && i < list.length(); i++) { + JSONObject property = list.optJSONObject(i); + if (property != null) { + names.add(property.optString("Name")); + } + } + } + } + List missing = new ArrayList<>(); + for (String property : properties) { + if (names.contains(property) == false) { + missing.add(property); + } + } + return missing; + } + /** Starts the example with the key and asks it for /passive. */ private JSONObject passive(String key) throws IOException { example = new HumanConfidence(api(), key, null); From a6380f639a20361a65e951957acf2475e8694921 Mon Sep 17 00:00:00 2001 From: Mykhailo Dubish Date: Tue, 24 Nov 2026 05:55:26 +0000 Subject: [PATCH 3/3] TEST: Check the Bespoke key's source from the service's answer The Bespoke test asked the list of accessible properties for both HumanConfidence and HumanConfidenceSource. The staging cloud serves the source to a request that names it without listing it there, so the test failed for a key the cloud does serve. The list is now asked for HumanConfidence only, as in .NET and PHP. The test then asks the service for HumanConfidenceSource with the key and fails, naming the variable, when the answer is not Passive. The list check also names every missing property in one failure, matches names ignoring case, and fails naming the variable when the list is not JSON. The README and the class comment say the same. --- .../web/human-confidence/README.md | 5 +- .../examples/web/HumanConfidenceLiveTest.java | 90 ++++++++++++++++--- 2 files changed, 79 insertions(+), 16 deletions(-) diff --git a/examples/device-detection/web/human-confidence/README.md b/examples/device-detection/web/human-confidence/README.md index 19a447ed0..7d4d1b431 100644 --- a/examples/device-detection/web/human-confidence/README.md +++ b/examples/device-detection/web/human-confidence/README.md @@ -113,8 +113,9 @@ cloud, the API base including `/api/v4/`, and `RESOURCE_KEY_BESPOKE_STAGING` holds a Bespoke key that cloud resolves. With that key the answer must have `HumanConfidenceSource` set to `Passive` and a plain level. The test first asks the cloud which properties the key includes, and fails, naming the -variable and the property, when `HumanConfidence` or `HumanConfidenceSource` -is not one of them. With a key whose +variable and the property, when `HumanConfidence` is not one of them. It +then asks the cloud for `HumanConfidenceSource` with the key, and fails, +naming the variable, when the answer is not `Passive`. With a key whose products don't offer the source, in `_51DEGREES_RESOURCE_KEY_BASIC`, the cloud must answer the source as null, with the reason, and the example's answer must have no source. diff --git a/examples/device-detection/web/human-confidence/src/test/java/fiftyone/devicedetection/examples/web/HumanConfidenceLiveTest.java b/examples/device-detection/web/human-confidence/src/test/java/fiftyone/devicedetection/examples/web/HumanConfidenceLiveTest.java index 3344e8c1c..8aafee0dc 100644 --- a/examples/device-detection/web/human-confidence/src/test/java/fiftyone/devicedetection/examples/web/HumanConfidenceLiveTest.java +++ b/examples/device-detection/web/human-confidence/src/test/java/fiftyone/devicedetection/examples/web/HumanConfidenceLiveTest.java @@ -23,6 +23,7 @@ package fiftyone.devicedetection.examples.web; import org.json.JSONArray; +import org.json.JSONException; import org.json.JSONObject; import org.junit.After; import org.junit.Test; @@ -39,6 +40,7 @@ import java.util.Arrays; import java.util.HashSet; import java.util.List; +import java.util.Locale; import java.util.Set; import static org.junit.Assert.assertEquals; @@ -63,8 +65,11 @@ * _51DEGREES_RESOURCE_KEY. A test is skipped when its key or * CLOUD_STAGING_ENDPOINT isn't set, naming the variable, and fails when the * answer lacks what it checks. The Bespoke test first fails, naming the - * variable and the property, when the key doesn't include HumanConfidence or - * HumanConfidenceSource. Keys are never printed. + * variable and the property, when the service's list of the key's + * properties doesn't include HumanConfidence, then when the service, asked + * for HumanConfidenceSource with the key, doesn't answer it as Passive. The + * list isn't asked about the source, as a key can be served the source when + * a request names it without the list holding it. Keys are never printed. */ public class HumanConfidenceLiveTest { @@ -109,9 +114,14 @@ public void stop() { public void bespokeKeysPassiveAnswerCarriesTheSourcePassive() throws IOException { String key = key(BESPOKE_KEY); - requireProperties(BESPOKE_KEY, key, - "HumanConfidence", "HumanConfidenceSource"); + requireProperties(BESPOKE_KEY, key, "HumanConfidence"); JSONObject served = servedSource(key); + assertEquals("The staging cloud didn't answer HumanConfidenceSource " + + "as " + PASSIVE + " for the key in " + BESPOKE_KEY + + ": " + redact(served.toString(), key) + + ". Set the variable to a key whose products " + + "include it.", + PASSIVE, served.optString(SOURCE, null)); JSONObject answer = passive(key); String text = redact(answer.toString(), key); @@ -163,6 +173,31 @@ public void missingPropertiesNamesOnlyThePropertiesNoProductLists() { missingProperties(new JSONObject("{}"), "HumanConfidence")); } + @Test + public void missingPropertiesIgnoresTheCaseOfTheNames() { + JSONObject accessible = new JSONObject( + "{\"Products\":{\"derived\":{\"Properties\":[" + + "{\"Name\":\"humanconfidence\"}]}}}"); + + assertEquals(new ArrayList(), + missingProperties(accessible, "HumanConfidence")); + } + + @Test + public void missingMessageNamesEveryMissingPropertyAndTheVariable() { + assertEquals("The key in " + BESPOKE_KEY + " does not include the " + + "HumanConfidence property. Set the variable to a " + + "key that includes it.", + missingMessage(BESPOKE_KEY, + Arrays.asList("HumanConfidence"))); + assertEquals("The key in " + BESPOKE_KEY + " does not include the " + + "HumanConfidence and HumanConfidenceSource " + + "properties. Set the variable to a key that " + + "includes them.", + missingMessage(BESPOKE_KEY, Arrays.asList( + "HumanConfidence", "HumanConfidenceSource"))); + } + /** The key in the variable name, or a skip that names it. */ private static String key(String name) { String key = System.getenv(name); @@ -173,8 +208,8 @@ private static String key(String name) { } /** - * Fails, naming the variable and the property, when the service's list - * of the properties the key can read lacks one of them. + * Fails, naming the variable and every property it lacks, when the + * service's list of the properties the key can read lacks any of them. */ private static void requireProperties(String name, String key, String... properties) throws IOException { @@ -188,17 +223,43 @@ private static void requireProperties(String name, String key, ? connection.getErrorStream() : connection.getInputStream()); assertEquals("The service didn't list the properties of the key in " + name + ": " + redact(body, key), 200, status); - for (String property : missingProperties( - new JSONObject(body), properties)) { - fail("The key in " + name + " does not include the " + property - + " property. Set the variable to a key that includes " - + "it."); + JSONObject accessible; + try { + accessible = new JSONObject(body); + } catch (JSONException e) { + fail("The service's property list for the key in " + name + + " isn't JSON: " + redact(body, key)); + return; + } + List missing = missingProperties(accessible, properties); + if (missing.isEmpty() == false) { + fail(missingMessage(name, missing)); + } + } + + /** + * The failure for a key whose list lacks the properties, naming the + * variable and each property. + */ + static String missingMessage(String name, List missing) { + boolean one = missing.size() == 1; + StringBuilder names = new StringBuilder(); + for (int i = 0; i < missing.size(); i++) { + if (i > 0) { + names.append(i == missing.size() - 1 + ? (missing.size() > 2 ? ", and " : " and ") : ", "); + } + names.append(missing.get(i)); } + return "The key in " + name + " does not include the " + names + + (one ? " property" : " properties") + + ". Set the variable to a key that includes " + + (one ? "it." : "them."); } /** * The properties, of those given, that no product in the service's list - * of accessible properties names. + * of accessible properties names, ignoring case. */ static List missingProperties(JSONObject accessible, String... properties) { @@ -212,14 +273,15 @@ static List missingProperties(JSONObject accessible, for (int i = 0; list != null && i < list.length(); i++) { JSONObject property = list.optJSONObject(i); if (property != null) { - names.add(property.optString("Name")); + names.add(property.optString("Name") + .toLowerCase(Locale.ROOT)); } } } } List missing = new ArrayList<>(); for (String property : properties) { - if (names.contains(property) == false) { + if (names.contains(property.toLowerCase(Locale.ROOT)) == false) { missing.add(property); } }