diff --git a/.github/workflows/cloud-human-confidence.yml b/.github/workflows/cloud-human-confidence.yml index 68aac8eb9..ca67eff15 100644 --- a/.github/workflows/cloud-human-confidence.yml +++ b/.github/workflows/cloud-human-confidence.yml @@ -1,19 +1,21 @@ 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, against the staging cloud, because the production cloud does not +# serve HumanConfidenceSource yet. 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 the cloud answers its source as null, with the reason. # -# 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. +# CLOUD_STAGING_ENDPOINT, the staging cloud's API base, is an organization +# variable that only CI uses. RESOURCE_KEY_BESPOKE_STAGING is a Bespoke key +# the staging cloud resolves, and _51DEGREES_RESOURCE_KEY_BASIC resolves on +# both clouds. Both keys are organization secrets. A new key needs 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 name 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,52 +44,40 @@ 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: - _51DEGREES_RESOURCE_KEY_BESPOKE: ${{ secrets._51DEGREES_RESOURCE_KEY_BESPOKE }} + RESOURCE_KEY_BESPOKE_STAGING: ${{ secrets.RESOURCE_KEY_BESPOKE_STAGING }} _51DEGREES_RESOURCE_KEY_BASIC: ${{ secrets._51DEGREES_RESOURCE_KEY_BASIC }} - _51DEGREES_CLOUD_ENDPOINT: ${{ vars._51DEGREES_CLOUD_ENDPOINT }} + CLOUD_STAGING_ENDPOINT: ${{ vars.CLOUD_STAGING_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 or the endpoint, so a missing one + # fails here, naming every organization secret or variable missing. + $missing = @( + 'RESOURCE_KEY_BESPOKE_STAGING' + '_51DEGREES_RESOURCE_KEY_BASIC' + 'CLOUD_STAGING_ENDPOINT' + ) | Where-Object { -not [Environment]::GetEnvironmentVariable($_) } + if ($missing) { + throw ("The organization secret or variable " + + "$($missing -join ', ') is empty or missing, so the live " + + 'tests cannot run.') } $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/AGENTS.md b/AGENTS.md index 7f70f2de3..43183df91 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -270,11 +270,13 @@ and for a list of properties that fails once or every time. Every name 51Degrees defines for an environment variable or a Java system property starts `_51DEGREES_`, each name holds one thing, and no name is -filled from another. A name the platform or a build tool defines keeps the -name its owner gave it. +filled from another. A name only CI uses, which no customer sets, has no +prefix and says what it holds. A name the platform or a build tool defines +keeps the name its owner gave it. [`ci/check-environment-variable-names.ps1`](ci/check-environment-variable-names.ps1) -reads every tracked file and fails on a name read or set that is neither. It -holds the short list of platform and tool names. Run it, and its tests in +reads every tracked file and fails on a name read or set that is none of +these. It holds the short lists of CI names and of platform and tool +names. Run it, and its tests in [`ci/check-environment-variable-names.Tests.ps1`](ci/check-environment-variable-names.Tests.ps1), after a change that reads or sets a name. diff --git a/README.md b/README.md index eec12597e..628d03b57 100644 --- a/README.md +++ b/README.md @@ -107,9 +107,10 @@ on-premise tests need the free data files, which the script in Intelligence data file, see `ip-intelligence/README.md`. Every environment variable name that the packages, tests, examples and CI -scripts define starts `_51DEGREES_`, which +scripts define starts `_51DEGREES_`, apart from the few that only CI uses, +which [`ci/check-environment-variable-names.ps1`](ci/check-environment-variable-names.ps1) -checks. +lists and checks. Tests find data files by searching from the root of this repository, so run Maven from here rather than from inside a package folder. diff --git a/ci/README.md b/ci/README.md index eb4c7d94b..575067f78 100644 --- a/ci/README.md +++ b/ci/README.md @@ -115,9 +115,10 @@ Required: secret that only CI uses, so it has no `_51DEGREES_` prefix. The workflows pass the secrets to common-ci with `secrets: inherit`, and -common-ci exports each secret and each `_51DEGREES_` organization variable -under its own name before the scripts here run. The tests read only these -names. +common-ci exports each `_51DEGREES_` secret and variable, and the CI secrets +on its own list, under its own name before the scripts here run. The tests +read these names, apart from the Human Confidence live tests below, which +read two names that common-ci does not export. To run the on-premise tests: @@ -144,12 +145,24 @@ 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 test of a key outside Bespoke + with it, on the staging cloud, expecting the cloud to answer the source as + null and the example's answer to carry none. - `_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. +The production cloud does not serve `HumanConfidenceSource` yet, so the +Human Confidence example's live tests run against the staging cloud, with +two names that only CI uses and so have no `_51DEGREES_` prefix: + +- `CLOUD_STAGING_ENDPOINT`: an organization variable naming the staging + cloud, the API base including `/api/v4/`. +- `RESOURCE_KEY_BESPOKE_STAGING`: an organization secret holding a Bespoke + resource key the staging cloud resolves. + [cloud-human-confidence.yml](../.github/workflows/cloud-human-confidence.yml) + runs the live test of the Bespoke key with it, expecting `Passive`. + A cloud test with no key is skipped, naming the variable it reads. To publish to Maven Central (51Degrees only), these organization secrets, diff --git a/ci/check-environment-variable-names.Tests.ps1 b/ci/check-environment-variable-names.Tests.ps1 index 236699918..bea25aa45 100644 --- a/ci/check-environment-variable-names.Tests.ps1 +++ b/ci/check-environment-variable-names.Tests.ps1 @@ -37,6 +37,13 @@ Describe 'Test-AllowedName' { Test-AllowedName $Name | Should -BeTrue } + It 'allows the CI name ' -ForEach @( + @{ Name = 'CLOUD_STAGING_ENDPOINT' } + @{ Name = 'RESOURCE_KEY_BESPOKE_STAGING' } + ) { + Test-AllowedName $Name | Should -BeTrue + } + It 'allows the platform name ' -ForEach @( @{ Name = 'PATH' } @{ Name = 'JAVA_HOME' } @@ -55,6 +62,8 @@ Describe 'Test-AllowedName' { It 'refuses ' -ForEach @( @{ Name = 'SAMPLE_NAME' } + @{ Name = 'cloud_staging_endpoint' } + @{ Name = 'RESOURCE_KEY_BESPOKE' } @{ Name = 'SampleName' } @{ Name = 'sample.name' } @{ Name = '_51DEGREES_' } diff --git a/ci/check-environment-variable-names.ps1 b/ci/check-environment-variable-names.ps1 index ea668b1a6..62f821fb7 100644 --- a/ci/check-environment-variable-names.ps1 +++ b/ci/check-environment-variable-names.ps1 @@ -5,8 +5,9 @@ # on: # # - any name that starts with the digit, wherever it is written, and -# - any name read or set that neither starts _51DEGREES_ nor is one the -# platform or a build tool defines, which are listed below. +# - any name read or set that neither starts _51DEGREES_, nor is one of the +# names only CI uses, nor is one the platform or a build tool defines, +# which are listed below. # # A name counts as read or set when it is a string given to getenv or to # System.getProperty, setProperty or clearProperty, a name on PowerShell's @@ -61,13 +62,22 @@ $script:PlatformNames = @( 'CHROMEWEBDRIVER' ) +# The names only CI uses. No customer sets them, so they have no +# _51DEGREES_ prefix, and each name says what it holds. +$script:CiNames = @( + # The staging cloud and a Bespoke key it resolves, for the tests of what + # only the staging cloud serves. + 'CLOUD_STAGING_ENDPOINT', 'RESOURCE_KEY_BESPOKE_STAGING' +) + # The system properties the JDK defines start with one of these. $script:PlatformPropertyPattern = [regex]::new('^(?:java|os|user|sun|file|line)\.[A-Za-z0-9_.]+$') -# Whether a name read or set is one 51Degrees defines under its prefix, or -# one the platform or a build tool defines. +# Whether a name read or set is one 51Degrees defines under its prefix, one +# only CI uses, or one the platform or a build tool defines. function Test-AllowedName([string]$Name) { return $script:OwnNamePattern.IsMatch($Name) -or + $script:CiNames -ccontains $Name -or $script:PlatformNames -ccontains $Name -or $script:PlatformPropertyPattern.IsMatch($Name) } @@ -259,8 +269,9 @@ if ($digits.Count -gt 0 -or $names.Count -gt 0) { throw ("$($digits.Count) names start with a digit, which a POSIX shell " + 'cannot set, so each must start with an underscore, and ' + "$($names.Count) names are read or set that are not _51DEGREES_ " + - 'names or names the platform or a build tool defines.') + 'names, names only CI uses, or names the platform or a build tool ' + + 'defines.') } -Write-Output ('Every name read or set is a _51DEGREES_ name or one the ' + - 'platform or a build tool defines.') +Write-Output ('Every name read or set is a _51DEGREES_ name, one only CI ' + + 'uses, or one the platform or a build tool defines.') diff --git a/ci/workflow-keys.Tests.ps1 b/ci/workflow-keys.Tests.ps1 index 238962c85..dde11ae75 100644 --- a/ci/workflow-keys.Tests.ps1 +++ b/ci/workflow-keys.Tests.ps1 @@ -23,14 +23,17 @@ Describe 'the workflows' { } | ForEach-Object Text | Should -BeNullOrEmpty } - # The resource keys and the license keys are organization secrets, so - # each comes from the secret of its own name. + # The resource keys and the license keys are organization secrets, and + # the staging cloud's address an organization variable, so each comes + # from the secret or variable of its own name. It 'fill from the organization of the same name' -ForEach @( @{ Name = '_51DEGREES_RESOURCE_KEY_51DID'; Kind = 'secret'; Context = 'secrets' } @{ Name = '_51DEGREES_RESOURCE_KEY_BESPOKE'; Kind = 'secret'; Context = 'secrets' } @{ Name = '_51DEGREES_RESOURCE_KEY_BASIC'; Kind = 'secret'; Context = 'secrets' } @{ Name = '_51DEGREES_LICENSE_KEY_51DID'; Kind = 'secret'; Context = 'secrets' } @{ Name = '_51DEGREES_LICENSE_KEY_BESPOKE'; Kind = 'secret'; Context = 'secrets' } + @{ Name = 'RESOURCE_KEY_BESPOKE_STAGING'; Kind = 'secret'; Context = 'secrets' } + @{ Name = 'CLOUD_STAGING_ENDPOINT'; Kind = 'variable'; Context = 'vars' } ) { $workflows = Join-Path $PSScriptRoot '../.github/workflows' $lines = @(Get-ChildItem $workflows -Filter *.yml | @@ -164,4 +167,142 @@ Describe 'the workflows' { } | Should -Not -BeNullOrEmpty } } + + # cloud-human-confidence.yml runs the Human Confidence live tests once, + # against the staging cloud. Each test reads its own key and the endpoint + # under their own names, 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' + # The names the step and the tests read. + $script:Read = '^(_51DEGREES_.*|CLOUD_STAGING_ENDPOINT|RESOURCE_KEY_BESPOKE_STAGING)$' + + # 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 -match $script:Read }) + $script:Saved | ForEach-Object { Remove-Item "Env:$($_.Name)" } + $env:RESOURCE_KEY_BESPOKE_STAGING = 'resource-bespoke-staging' + $env:_51DEGREES_RESOURCE_KEY_BASIC = 'resource-basic' + $env:CLOUD_STAGING_ENDPOINT = 'endpoint-staging' + Push-Location TestDrive: + } + + AfterEach { + Pop-Location + Remove-Item TestDrive:/web -Recurse -Force -ErrorAction SilentlyContinue + Get-ChildItem env: | Where-Object { $_.Name -match $script:Read } | + ForEach-Object { Remove-Item "Env:$($_.Name)" } + $script:Saved | ForEach-Object { Set-Item "Env:$($_.Name)" $_.Value } + } + + It 'runs the tests once, with each name under its own name and no other' { + $runs = [System.Collections.Generic.List[object]]::new() + function mvn { + $names = @(Get-ChildItem env: | + Where-Object { $_.Name -match $script:Read } | + ForEach-Object { "$($_.Name)=$($_.Value)" }) + $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 | Sort-Object) | Should -Be @(@( + 'CLOUD_STAGING_ENDPOINT=endpoint-staging' + 'RESOURCE_KEY_BESPOKE_STAGING=resource-bespoke-staging' + '_51DEGREES_RESOURCE_KEY_BASIC=resource-basic') | Sort-Object) + $runs[0].Arguments | Should -BeLike '*-Dtest=HumanConfidenceLiveTest*' + $runs[0].Arguments | Should -Not -BeLike '*humanConfidenceSource*' + } + + # Each test needs its own key and the endpoint, 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 = 'RESOURCE_KEY_BESPOKE_STAGING' } + @{ Name = '_51DEGREES_RESOURCE_KEY_BASIC' } + @{ Name = 'CLOUD_STAGING_ENDPOINT' } + ) { + $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 'names every missing name at once' { + function mvn { $global:LASTEXITCODE = 0 } + Remove-Item Env:RESOURCE_KEY_BESPOKE_STAGING, Env:CLOUD_STAGING_ENDPOINT + + { & $script:Step 6> $null } | + Should -Throw '*RESOURCE_KEY_BESPOKE_STAGING, CLOUD_STAGING_ENDPOINT*' + } + + 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 staging cloud from the variable of the same name' { + $script:Lines | Where-Object { + $_ -match '^\s*CLOUD_STAGING_ENDPOINT:\s*\$\{\{\s*vars\.CLOUD_STAGING_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..1fbb23ee7 100644 --- a/examples/device-detection/web/human-confidence/README.md +++ b/examples/device-detection/web/human-confidence/README.md @@ -105,22 +105,26 @@ 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 -`_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. +`HumanConfidenceLiveTest` starts the server with a key and asks it for the +passive result as the page does. The production cloud does not serve +`HumanConfidenceSource` yet, so CI runs these tests against the staging +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 +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. ``` ../../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 or `CLOUD_STAGING_ENDPOINT` is not set. +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 both, and fails if either is skipped. ## 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..83c1be02c 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,40 @@ 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 staging cloud service that + * CLOUD_STAGING_ENDPOINT names, and asks its /passive path as the page does. + * The production cloud does not serve HumanConfidenceSource yet, so the + * tests ask the staging cloud. *

- * 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 Bespoke key in + * RESOURCE_KEY_BESPOKE_STAGING, which the staging cloud resolves, 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 the cloud answers the source as null, with the reason, and + * the example's 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 or + * CLOUD_STAGING_ENDPOINT 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"; + /** + * A Bespoke key, whose products offer HumanConfidenceSource, that the + * staging cloud resolves. Only CI sets it. + */ + private static final String BESPOKE_KEY = "RESOURCE_KEY_BESPOKE_STAGING"; + + /** The key whose products don't offer HumanConfidenceSource. */ + private static final String BASIC_KEY = "_51DEGREES_RESOURCE_KEY_BASIC"; - private static final String ENDPOINT = "_51DEGREES_CLOUD_ENDPOINT"; + /** The staging cloud's API base. Only CI sets it. */ + private static final String ENDPOINT = "CLOUD_STAGING_ENDPOINT"; - /** The system property naming the source the key's answer carries. */ - private static final String EXPECTED_SOURCE = "humanConfidenceSource"; + private static final String SOURCE = "humanconfidencesource"; - /** The expected source for a key whose products don't offer it. */ - private static final String NONE = "none"; + private static final String SOURCE_REASON = + "humanconfidencesourcenullreason"; private static final String PASSIVE = "Passive"; @@ -86,66 +99,75 @@ 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")); + // A cloud that serves the source answers it for this key as null, + // with the reason. A cloud that does not serve it leaves it out. + String said = redact(served.toString(), key); + assertTrue("The service answers this key's source as null: " + said, + served.has(SOURCE) && served.isNull(SOURCE)); + assertTrue("The service says why the source is null: " + said, + served.opt(SOURCE_REASON) instanceof String); + assertFalse("This key's answer carries no source: " + text + + ". The service says: " + said, + 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 " - + "skipped. " + ENDPOINT + " names the service.", + /** 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.", key != null && key.trim().isEmpty() == false); return key.trim(); } - /** The API base the example calls, ending in one slash. */ + /** 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, or a skip that + * names the variable. There is no default address. + */ private static String api() { String endpoint = System.getenv(ENDPOINT); - if (endpoint == null || endpoint.trim().isEmpty()) { - endpoint = HumanConfidence.DEFAULT_ENDPOINT; - } + assumeTrue(ENDPOINT + " is not set, so this live cloud test is " + + "skipped.", + endpoint != null && endpoint.trim().isEmpty() == false); return endpoint.trim().replaceAll("/+$", "") + "/"; } /** * 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, as the service + * gave them. */ private static JSONObject servedSource(String key) throws IOException { String form = "user-agent=" + encode(USER_AGENT) @@ -170,17 +192,12 @@ private static JSONObject servedSource(String key) throws IOException { JSONObject served = new JSONObject(); if (derived != null) { for (String name : new String[] { - "humanconfidencesource", - "humanconfidencesourcenullreason" }) { + SOURCE, SOURCE_REASON }) { if (derived.has(name)) { served.put(name, derived.get(name)); } } } - 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; }