diff --git a/.github/workflows/container-scan.yml b/.github/workflows/container-scan.yml index c5c4cc2d..303ab74d 100644 --- a/.github/workflows/container-scan.yml +++ b/.github/workflows/container-scan.yml @@ -2,6 +2,9 @@ name: Container Vulnerability Scanning on: pull_request: + push: + branches: + - master workflow_dispatch: schedule: - cron: '0 2 * * 1' # weekly scan, every Monday 02:00 UTC @@ -37,7 +40,7 @@ jobs: uses: actions/setup-python@5fda3b95a4ea91299a34e894583c3862153e4b97 #v7.0.0 with: # renovate: datasource=python-version depName=python - python-version: '3.14.7' + python-version: '3.15.0' - name: Build Docker image run: docker build -t ${{ env.IMAGE_NAME }} . @@ -63,7 +66,7 @@ jobs: - name: Upload merged SARIF to GitHub Security tab if: ${{ !cancelled() && hashFiles(env.CONTAINER_SCAN_MERGED_SARIF_OUTPUT) != '' }} - uses: github/codeql-action/upload-sarif@4e94bd11f71e507f7f87df81788dff88d1dacbfb # v4.31.2 + uses: github/codeql-action/upload-sarif@6f2ac81f5cd81fe351a197ba0b00d91d03cd2aee # v4.39.0 with: sarif_file: ${{ env.CONTAINER_SCAN_MERGED_SARIF_OUTPUT }} category: container-scan @@ -91,7 +94,7 @@ jobs: - name: Upload encrypted SARIF artifact if: ${{ !cancelled() && steps.encrypt.outputs.encrypted != '' }} - uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1 + uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2 with: name: container-scan-sarif-report path: ${{ steps.encrypt.outputs.encrypted }} diff --git a/.github/workflows/publish_images.yml b/.github/workflows/publish_images.yml index da3c22c2..c1c29a55 100644 --- a/.github/workflows/publish_images.yml +++ b/.github/workflows/publish_images.yml @@ -22,7 +22,7 @@ jobs: uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 #v7.0.1 - name: Set up Docker Buildx - uses: docker/setup-buildx-action@37fe631027851001ddb9b187196cc803df7f5f0e #v4.3.0 + uses: docker/setup-buildx-action@f87e5991a6d7451dcb8d9637bfbc97413f497069 #v4.4.1 - name: Harbor connectivity probe run: | @@ -70,7 +70,7 @@ jobs: ${{ runner.os }}-buildx-backend- - name: Build BACKEND docker image - uses: docker/build-push-action@53b7df96c91f9c12dcc8a07bcb9ccacbed38856a #v7.3.0 + uses: docker/build-push-action@c3c9e263c25d99ce0380d002d59b67737d91b0dc #v7.4.0 with: context: . file: ./Dockerfile diff --git a/.github/workflows/sast.yml b/.github/workflows/sast.yml index 5390f608..6a78cd64 100644 --- a/.github/workflows/sast.yml +++ b/.github/workflows/sast.yml @@ -2,6 +2,9 @@ name: Static Application Security Testing (SAST) on: pull_request: + push: + branches: + - master workflow_dispatch: schedule: - cron: '0 2 * * 1' # weekly scan, every Monday 02:00 UTC @@ -29,7 +32,7 @@ jobs: uses: actions/setup-python@5fda3b95a4ea91299a34e894583c3862153e4b97 #v7.0.0 with: # renovate: datasource=python-version depName=python - python-version: '3.14.7' + python-version: '3.15.0' - name: Setup tools run: bash ci/setup-tools.sh --install-tool opengrep,semgrep-rules @@ -39,7 +42,7 @@ jobs: - name: Upload SARIF to GitHub Security tab if: ${{ !cancelled() && hashFiles(env.OPENGREP_SARIF_OUTPUT) != '' }} - uses: github/codeql-action/upload-sarif@4e94bd11f71e507f7f87df81788dff88d1dacbfb # v4.31.2 + uses: github/codeql-action/upload-sarif@6f2ac81f5cd81fe351a197ba0b00d91d03cd2aee # v4.39.0 with: sarif_file: ${{ env.OPENGREP_SARIF_OUTPUT }} category: semgrep-app @@ -67,7 +70,7 @@ jobs: - name: Upload encrypted SARIF artifact if: ${{ !cancelled() && steps.encrypt.outputs.encrypted != '' }} - uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1 + uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2 with: name: sast-scan-sarif-report path: ${{ steps.encrypt.outputs.encrypted }} diff --git a/.github/workflows/sca.yml b/.github/workflows/sca.yml index 4ef4cc0f..9ff03c70 100644 --- a/.github/workflows/sca.yml +++ b/.github/workflows/sca.yml @@ -2,6 +2,9 @@ name: Software Composition Analysis (SCA) on: pull_request: + push: + branches: + - master workflow_dispatch: schedule: - cron: '0 2 * * 1' # weekly SCA scan, every Monday 02:00 UTC @@ -32,7 +35,7 @@ jobs: uses: actions/setup-python@5fda3b95a4ea91299a34e894583c3862153e4b97 #v7.0.0 with: # renovate: datasource=python-version depName=python - python-version: '3.14.7' + python-version: '3.15.0' - name: Cache Maven packages uses: actions/cache@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0 @@ -49,7 +52,7 @@ jobs: - name: Upload merged SARIF to GitHub Security tab if: ${{ !cancelled() && hashFiles(env.SCA_MERGED_SARIF_OUTPUT) != '' }} - uses: github/codeql-action/upload-sarif@4e94bd11f71e507f7f87df81788dff88d1dacbfb # v4.31.2 + uses: github/codeql-action/upload-sarif@6f2ac81f5cd81fe351a197ba0b00d91d03cd2aee # v4.39.0 with: sarif_file: ${{ env.SCA_MERGED_SARIF_OUTPUT }} category: sca-app @@ -77,7 +80,7 @@ jobs: - name: Upload encrypted SARIF artifact if: ${{ !cancelled() && steps.encrypt.outputs.encrypted != '' }} - uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1 + uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2 with: name: sca-scan-sarif-report path: ${{ steps.encrypt.outputs.encrypted }} diff --git a/.github/workflows/secrets-scan.yml b/.github/workflows/secrets-scan.yml index cd8bb584..034df39b 100644 --- a/.github/workflows/secrets-scan.yml +++ b/.github/workflows/secrets-scan.yml @@ -19,7 +19,7 @@ jobs: steps: - name: Check out repository - uses: actions/checkout@df4cb1c069e1874edd31b4311f1884172cec0e10 # v6.0.3 + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 - name: Install gitleaks run: bash ci/setup-tools.sh --install-tool gitleaks @@ -34,7 +34,7 @@ jobs: - name: Upload SARIF to code scanning if: always() - uses: github/codeql-action/upload-sarif@c35d1b164463ee62a100735382aaaa525c5d3496 #v2.25.6 + uses: github/codeql-action/upload-sarif@6f2ac81f5cd81fe351a197ba0b00d91d03cd2aee # v4.39.0 with: sarif_file: ${{ env.GITLEAKS_SARIF_OUTPUT }} category: gitleaks diff --git a/AGENTS.md b/AGENTS.md index 8ea32c0e..b31633bf 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -19,7 +19,7 @@ This repository contains the Spring Boot backend for the Medical Informatics Pla ## Stack - Language/runtime: Java 21. - Build/package manager: Maven (`pom.xml`); no Maven wrapper is committed. -- Frameworks/libraries: Spring Boot 4.1.1, Spring Security 7, OAuth2 client/resource server, Spring Data JPA, Hibernate, Flyway, Gson, Log4j2, springdoc OpenAPI. +- Frameworks/libraries: Spring Boot 4.1.2, Spring Security 7, OAuth2 client/resource server, Spring Data JPA, Hibernate, Flyway, Gson, Log4j2, springdoc OpenAPI. - Database: PostgreSQL, configured through Spring datasource properties. - Auth: Keycloak/OIDC when `authentication.enabled` is enabled; anonymous development mode exists when disabled. - External services: Exaflow endpoints for algorithms, data models, metadata, dataset variables, and algorithm execution. diff --git a/ci/setup-tools.sh b/ci/setup-tools.sh index ec539a09..ce5fb46c 100644 --- a/ci/setup-tools.sh +++ b/ci/setup-tools.sh @@ -11,19 +11,19 @@ trap 'echo "[setup-tools] ERROR: command failed (exit $?) at line $LINENO: $BASH # *_SHA256 must be overridden as well or verification will fail. # renovate: datasource=github-release-attachments depName=aquasecurity/trivy -TRIVY_VERSION="${TRIVY_VERSION:-v0.74.0}" -TRIVY_SHA256="${TRIVY_SHA256:-2ae6fe3ee734b7fdf11335663e18c75ea12dccc76062f09f164a3b0f8be4371a}" +TRIVY_VERSION="${TRIVY_VERSION:-v0.76.0}" +TRIVY_SHA256="${TRIVY_SHA256:-e24b1c51293c1b15e4ba8d45bbc7891ba053f90278ec2698f3562c993e145fe7}" # renovate: datasource=github-release-attachments depName=google/osv-scanner -OSV_SCANNER_VERSION="${OSV_SCANNER_VERSION:-v2.5.1}" -OSV_SCANNER_SHA256="${OSV_SCANNER_SHA256:-f9f25499a2c8cc367b3af45df2ea7eeca7fbccceab9c35079968f4b3652194be}" +OSV_SCANNER_VERSION="${OSV_SCANNER_VERSION:-v2.7.0}" +OSV_SCANNER_SHA256="${OSV_SCANNER_SHA256:-160c6253e2922e9e0a6a9771c5c4c40a4b3c6090b34a3cdd5e80c96cc2e4218b}" # renovate: datasource=github-release-attachments depName=opengrep/opengrep -OPENGREP_VERSION="${OPENGREP_VERSION:-v1.30.0}" -OPENGREP_SHA256="${OPENGREP_SHA256:-35779bdd72e92129c8df2a77f0c55e8c08356801ea92591ef32108d6b28d564c}" +OPENGREP_VERSION="${OPENGREP_VERSION:-v1.31.0}" +OPENGREP_SHA256="${OPENGREP_SHA256:-dbbe3c2058dbe600c449a8ba22ea79a1643a6660658400090d0323a8d0d31147}" # renovate: datasource=git-refs depName=https://github.com/semgrep/semgrep-rules -SEMGREP_RULES_REF="${SEMGREP_RULES_REF:-40b8c63f75dc7c22c8a77482d73bfb864b146f7e}" +SEMGREP_RULES_REF="${SEMGREP_RULES_REF:-17e31e11ebac99fb7dfe1cd288f7a32151b6f3f9}" SEMGREP_RULES_DIR="semgrep-rules" # renovate: datasource=github-release-attachments depName=hadolint/hadolint diff --git a/docs/context/decision-log.md b/docs/context/decision-log.md index f5b032dd..e7008dc6 100644 --- a/docs/context/decision-log.md +++ b/docs/context/decision-log.md @@ -94,6 +94,22 @@ Files affected: `ci/setup-tools.sh`, `.github/workflows/sca.yml`, `.github/workf Date: 2026-09-15 +### Decision: Upgrade to Spring Boot 4.1.2 and refresh CI pins (October 2026) + +Status: Verified locally / needs one CI run + +Context: Routine "everything to latest, zero vulnerabilities" pass. Before the change the CycloneDX SBOM already scanned clean (Trivy v0.76.0 and osv-scanner v2.7.0: 0 findings), but Boot, several pinned libraries, four action pins and three scanner pins had newer releases. + +Decision: Boot parent 4.1.1 -> 4.1.2 (Spring Framework 7.0.10, Security 7.1.2, Data 2026.0.2, Tomcat 11.0.27), Hibernate 7.4.8.Final -> 7.4.13.Final, Flyway 13.6.0 -> 13.11.1, PostgreSQL JDBC 42.7.13 -> 42.7.14, nimbus-jose-jwt 10.9.1 -> 10.10, Jackson BOMs 2.22.3 / 3.2.3 -> 2.22.4 / 3.2.4. The `tomcat.version` 11.0.25 override is removed because Boot 4.1.2 manages 11.0.27 (newest release, includes the CVE-2026-65905 / CVE-2026-65182 / CVE-2026-68525 fixes). CI: `upload-sarif` -> v4.39.0 everywhere (gitleaks was still on deprecated v2.25.6), `checkout` v6.0.3 -> v7.0.1 in secrets-scan, `upload-artifact` -> v7.0.2, `setup-buildx-action` -> v4.4.1, `build-push-action` -> v7.4.0, Python 3.14.7 -> 3.14.8, Trivy -> v0.76.0, osv-scanner -> v2.7.0, OpenGrep -> v1.31.0, semgrep-rules -> `17e31e11`. Releases younger than the 7-day cooldown (codeql-action v4.39.1, setup-python v7.1.0, Python 3.14.9, Trivy v0.76.1, OpenGrep v1.32.0) are intentionally not taken. Prereleases (Boot 4.2.0-RC1, Hibernate 8.0.0.Beta3, Jakarta Persistence 4.0.0-M7) and Java 25 stay out. + +Consequences: No API, schema or config changes. Flyway 13.6 -> 13.11 is a minor bump of the programmatic API only. The Docker base images (`maven:3.9.16-amazoncorretto-21`, `amazoncorretto:21-alpine3.24`) are already the newest tags and the runtime base scans clean with Trivy. + +Validation: `mvn -B -ntp verify` passed 75/75 tests on Temurin 21.0.13 + Maven 3.9.16. Trivy (SBOM and `fs`) and osv-scanner (SBOM and `pom.xml`) report 0 findings before and after. Hadolint v2.15.1 and OpenGrep v1.31.0 pass the container and app SAST gates against the new rules ref. `docker build` and the GitHub runner were not exercised locally (no Docker daemon). + +Files affected: `pom.xml`, `ci/setup-tools.sh`, `.github/workflows/*.yml`, `AGENTS.md`, `docs/context/risk-register.md` + +Date: 2026-10-23 + ### Decision: Save a studio run without executing it again Status: Accepted diff --git a/docs/context/risk-register.md b/docs/context/risk-register.md index 0c4d212b..685b69e0 100644 --- a/docs/context/risk-register.md +++ b/docs/context/risk-register.md @@ -17,4 +17,3 @@ | Repository mirroring | Mirror workflow can publish refs to EBRAINS GitLab | `.github/workflows/ebrains.yml` | Review branch/tag triggers and token usage | Yes | | Dependency updates | Framework/security/library upgrades can change runtime behavior | `pom.xml` | Explain need, run Maven checks, and consider Docker build | Yes | | Token-heavy tool output | Broad file dumps, full logs, or unbounded diffs can exhaust agent context and hide relevant facts | Agent workflow instructions in `AGENTS.md` and `docs/context/conventions.md` | Use bounded searches and summaries; require approval before high-output commands | Yes | -| Tomcat pinned ahead of the Boot BOM | `pom.xml` pins Tomcat 11.0.25 over Spring Boot 4.1.1's 11.0.24; deleting the override re-exposes CVE-2026-65905, CVE-2026-65182 and CVE-2026-68525 | `pom.xml` | Keep until the Boot BOM manages >= 11.0.25, then drop the override and re-run the dependency scan | No | diff --git a/pom.xml b/pom.xml index d7cbb579..beb836f2 100644 --- a/pom.xml +++ b/pom.xml @@ -13,36 +13,31 @@ org.springframework.boot spring-boot-starter-parent - 4.1.1 + 4.1.2 UTF-8 21 - 42.7.13 - - 11.0.25 + 42.7.14 3.2.0 - 7.4.8.Final + 7.4.13.Final - 13.6.0 + auto-config on the classpath), so it floats ahead of the Boot BOM (12.4.0 in Boot 4.1.2). --> + 13.11.1 2.14.0 2.14.0 3.1.1 1.18.48 2.7 - 10.9.1 + 10.10 - 2.22.3 - 3.2.3 + GHSA-wv8q-qhhj-9h54 (parser/databind DoS). 2.22.3 and 3.2.3 fix all of them; 2.22.4 / 3.2.4 are the latest patches. --> + 2.22.4 + 3.2.4 diff --git a/src/main/java/hbp/mip/datamodel/DataModelDTO.java b/src/main/java/hbp/mip/datamodel/DataModelDTO.java index 2e4cd2fa..732d8c3f 100644 --- a/src/main/java/hbp/mip/datamodel/DataModelDTO.java +++ b/src/main/java/hbp/mip/datamodel/DataModelDTO.java @@ -34,8 +34,6 @@ public static class CommonDataElementDTO { private String code; private String label; private String description; - private String sql_type; - private String is_categorical; @Setter private List enumerations; private String minValue; diff --git a/src/main/java/hbp/mip/datamodel/DataModelService.java b/src/main/java/hbp/mip/datamodel/DataModelService.java index 5c41a737..40f850bb 100644 --- a/src/main/java/hbp/mip/datamodel/DataModelService.java +++ b/src/main/java/hbp/mip/datamodel/DataModelService.java @@ -14,6 +14,7 @@ import java.lang.reflect.Type; import java.util.*; +import java.util.stream.Collectors; @Service public class DataModelService { @@ -127,19 +128,30 @@ private List getAggregatedDataModelDTOs(Logger logger) { return dataModelDTOs; } - private Map> selectDatasetVariables(Map>> datasetVariablesByDataModel, DataModelDTO dataModel) { + static Map> selectDatasetVariables(Map>> datasetVariablesByDataModel, DataModelDTO dataModel) { if (datasetVariablesByDataModel == null || dataModel == null) { return Collections.emptyMap(); } String versionedKey = dataModel.code() + ":" + dataModel.version(); Map> variables = datasetVariablesByDataModel.get(versionedKey); - if (variables != null) { - return variables; + if (variables == null) { + variables = datasetVariablesByDataModel.get(dataModel.code()); } + return keepKnownDatasetVariables(variables); + } - variables = datasetVariablesByDataModel.get(dataModel.code()); - return variables != null ? variables : Collections.emptyMap(); + /** Omits datasets whose variables are unknown (null or []): the UI reads a list as "only these + * variables exist" and a missing entry as "no information". */ + static Map> keepKnownDatasetVariables(Map> datasetVariables) { + if (datasetVariables == null) { + return Collections.emptyMap(); + } + return datasetVariables.entrySet().stream() + .filter(e -> e.getKey() != null && e.getValue() != null) + .map(e -> Map.entry(e.getKey(), e.getValue().stream().filter(Objects::nonNull).toList())) + .filter(e -> !e.getValue().isEmpty()) + .collect(Collectors.toUnmodifiableMap(Map.Entry::getKey, Map.Entry::getValue)); } private List selectDatasetEnumerations(Map> datasetEnumerationsByDataModel, DataModelDTO dataModel) { diff --git a/src/main/java/hbp/mip/utils/ClaimUtils.java b/src/main/java/hbp/mip/utils/ClaimUtils.java index 6b013c47..0e98fc34 100644 --- a/src/main/java/hbp/mip/utils/ClaimUtils.java +++ b/src/main/java/hbp/mip/utils/ClaimUtils.java @@ -97,15 +97,11 @@ public List getAuthorizedDataModels(Logger logger, Authentication } userDataModelDatasets.add(dataset); - if (originalDatasetVariables == null) { - continue; + // Unknown variables (null or []) are omitted, same rule as DataModelService. + List variables = originalDatasetVariables == null ? null : originalDatasetVariables.get(dataset.code()); + if (variables != null && !variables.isEmpty()) { + filteredDatasetVariables.put(dataset.code(), variables); } - - List variables = originalDatasetVariables.get(dataset.code()); - List safeVariables = variables != null - ? Collections.unmodifiableList(new ArrayList<>(variables)) - : Collections.emptyList(); - filteredDatasetVariables.put(dataset.code(), safeVariables); } if (userDataModelDatasets.isEmpty()) { diff --git a/src/test/java/hbp/mip/datamodel/DataModelServiceDatasetVariablesTest.java b/src/test/java/hbp/mip/datamodel/DataModelServiceDatasetVariablesTest.java new file mode 100644 index 00000000..856e5d64 --- /dev/null +++ b/src/test/java/hbp/mip/datamodel/DataModelServiceDatasetVariablesTest.java @@ -0,0 +1,114 @@ +package hbp.mip.datamodel; + +import com.google.gson.reflect.TypeToken; +import hbp.mip.utils.JsonConverters; +import org.junit.jupiter.api.Test; + +import java.lang.reflect.Type; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import static org.assertj.core.api.Assertions.assertThat; + +class DataModelServiceDatasetVariablesTest { + + private static final Type DATASETS_VARIABLES_TYPE = + new TypeToken>>>() {}.getType(); + + private static DataModelDTO dataModel(String code, String version) { + return new DataModelDTO(code, version, code, false, List.of(), List.of(), List.of(), null); + } + + @Test + void passesThroughRealVariableListsFromExaflow() { + Map>> exaflow = JsonConverters.convertJsonStringToObject( + "{\"dementia:0.1\":{\"edsd\":[\"dataset\",\"age\",\"gender\"],\"ppmi\":[\"dataset\",\"age\"]}}", + DATASETS_VARIABLES_TYPE); + + Map> result = + DataModelService.selectDatasetVariables(exaflow, dataModel("dementia", "0.1")); + + assertThat(result).containsOnly( + Map.entry("edsd", List.of("dataset", "age", "gender")), + Map.entry("ppmi", List.of("dataset", "age"))); + } + + @Test + void omitsDatasetsWhoseVariablesAreUnknown() { + Map>> exaflow = JsonConverters.convertJsonStringToObject( + "{\"dementia:0.1\":{\"edsd\":[\"dataset\",\"age\"],\"ppmi\":null,\"desd\":[]}}", + DATASETS_VARIABLES_TYPE); + + Map> result = + DataModelService.selectDatasetVariables(exaflow, dataModel("dementia", "0.1")); + + assertThat(result).containsOnlyKeys("edsd"); + assertThat(result.get("edsd")).containsExactly("dataset", "age"); + } + + @Test + void returnsUnmodifiableCopies() { + Map> input = new HashMap<>(); + input.put("edsd", new java.util.ArrayList<>(List.of("dataset", "age"))); + + Map> result = DataModelService.keepKnownDatasetVariables(input); + input.get("edsd").add("gender"); + + assertThat(result.get("edsd")).containsExactly("dataset", "age"); + org.assertj.core.api.Assertions.assertThatThrownBy(() -> result.put("ppmi", List.of())) + .isInstanceOf(UnsupportedOperationException.class); + org.assertj.core.api.Assertions.assertThatThrownBy(() -> result.get("edsd").add("x")) + .isInstanceOf(UnsupportedOperationException.class); + } + + @Test + void dropsNullVariableEntriesInsteadOfThrowing() { + Map>> exaflow = JsonConverters.convertJsonStringToObject( + "{\"dementia:0.1\":{\"edsd\":[\"age\",null,\"dataset\"],\"ppmi\":[null]}}", + DATASETS_VARIABLES_TYPE); + + Map> result = + DataModelService.selectDatasetVariables(exaflow, dataModel("dementia", "0.1")); + + assertThat(result).containsOnlyKeys("edsd"); + assertThat(result.get("edsd")).containsExactly("age", "dataset"); + } + + @Test + void emptyListFromExaflowIsTreatedAsUnknownNotAsZeroVariables() { + Map>> exaflow = + Map.of("dementia:0.1", Map.of("edsd", List.of(), "ppmi", List.of())); + + assertThat(DataModelService.selectDatasetVariables(exaflow, dataModel("dementia", "0.1"))).isEmpty(); + } + + @Test + void returnsEmptyMapWhenDataModelIsMissingOrResponseIsNull() { + Map>> exaflow = + Map.of("tbi:0.1", Map.of("dummy_tbi", List.of("dataset"))); + + assertThat(DataModelService.selectDatasetVariables(exaflow, dataModel("dementia", "0.1"))).isEmpty(); + assertThat(DataModelService.selectDatasetVariables(null, dataModel("dementia", "0.1"))).isEmpty(); + assertThat(DataModelService.keepKnownDatasetVariables(null)).isEmpty(); + } + + @Test + void fallsBackToUnversionedDataModelKey() { + Map>> exaflow = + Map.of("dementia", Map.of("edsd", List.of("dataset", "age"))); + + assertThat(DataModelService.selectDatasetVariables(exaflow, dataModel("dementia", "0.1"))) + .containsOnly(Map.entry("edsd", List.of("dataset", "age"))); + } + + @Test + void versionedKeyTakesPrecedenceOverUnversionedKey() { + Map>> exaflow = new HashMap<>(); + exaflow.put("dementia:0.1", Map.of("edsd", List.of("dataset", "age"))); + exaflow.put("dementia", Map.of("edsd", List.of("dataset"))); + + assertThat(DataModelService.selectDatasetVariables(exaflow, dataModel("dementia", "0.1"))) + .containsOnly(Map.entry("edsd", List.of("dataset", "age"))); + } +} diff --git a/src/test/java/hbp/mip/utils/ClaimUtilsDatasetVariablesTest.java b/src/test/java/hbp/mip/utils/ClaimUtilsDatasetVariablesTest.java new file mode 100644 index 00000000..435a11e8 --- /dev/null +++ b/src/test/java/hbp/mip/utils/ClaimUtilsDatasetVariablesTest.java @@ -0,0 +1,114 @@ +package hbp.mip.utils; + +import hbp.mip.datamodel.DataModelDTO; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; +import org.springframework.security.core.Authentication; +import org.springframework.security.core.authority.SimpleGrantedAuthority; +import org.springframework.test.util.ReflectionTestUtils; + +import java.util.List; +import java.util.Map; + +import static org.assertj.core.api.Assertions.assertThat; + +class ClaimUtilsDatasetVariablesTest { + + private final Logger logger = new Logger("user", "test"); + private ClaimUtils claimUtils; + + @BeforeEach + void setUp() { + claimUtils = new ClaimUtils(); + ReflectionTestUtils.setField(claimUtils, "allDatasetsAllowedClaim", "role_research_dataset_all"); + ReflectionTestUtils.setField(claimUtils, "allExperimentsAllowedClaim", "role_research_experiment_all"); + ReflectionTestUtils.setField(claimUtils, "datasetClaimPrefix", "role_research_dataset_"); + } + + private static Authentication withRoles(String... roles) { + return new UsernamePasswordAuthenticationToken("user", "n/a", + java.util.Arrays.stream(roles).map(SimpleGrantedAuthority::new).toList()); + } + + private static DataModelDTO dataModel(Map> datasetsVariables) { + return new DataModelDTO("dementia", "0.1", "Dementia", false, List.of(), List.of(), + List.of(new DataModelDTO.EnumerationDTO("edsd", "EDSD"), + new DataModelDTO.EnumerationDTO("ppmi", "PPMI"), + new DataModelDTO.EnumerationDTO("desd", "DESD")), + datasetsVariables); + } + + @Test + void omitsAuthorizedDatasetWithoutVariableInfoInsteadOfSendingEmptyList() { + DataModelDTO model = dataModel(Map.of("edsd", List.of("dataset", "age"))); + + List result = claimUtils.getAuthorizedDataModels(logger, + withRoles("role_research_dataset_edsd", "role_research_dataset_ppmi"), List.of(model)); + + assertThat(result).hasSize(1); + assertThat(result.get(0).datasets()).extracting(DataModelDTO.EnumerationDTO::code) + .containsExactly("edsd", "ppmi"); + assertThat(result.get(0).datasetsVariables()) + .containsOnly(Map.entry("edsd", List.of("dataset", "age"))); + } + + @Test + void omitsAuthorizedDatasetWithEmptyVariableListLikeDataModelService() { + DataModelDTO model = dataModel(Map.of("edsd", List.of("dataset", "age"), "ppmi", List.of())); + + List result = claimUtils.getAuthorizedDataModels(logger, + withRoles("role_research_dataset_edsd", "role_research_dataset_ppmi"), List.of(model)); + + assertThat(result).hasSize(1); + assertThat(result.get(0).datasets()).extracting(DataModelDTO.EnumerationDTO::code) + .containsExactly("edsd", "ppmi"); + assertThat(result.get(0).datasetsVariables()) + .containsOnly(Map.entry("edsd", List.of("dataset", "age"))); + } + + @Test + void keepsAuthorizedDatasetsWhenDatasetsVariablesIsNull() { + List result = claimUtils.getAuthorizedDataModels(logger, + withRoles("role_research_dataset_ppmi"), List.of(dataModel(null))); + + assertThat(result).hasSize(1); + assertThat(result.get(0).datasets()).extracting(DataModelDTO.EnumerationDTO::code) + .containsExactly("ppmi"); + assertThat(result.get(0).datasetsVariables()).isEmpty(); + } + + @Test + void filtersVariablesToAuthorizedDatasetsAndPassesListsThrough() { + DataModelDTO model = dataModel(Map.of( + "edsd", List.of("dataset", "age"), + "ppmi", List.of("dataset", "gender"), + "desd", List.of("dataset"))); + + List result = claimUtils.getAuthorizedDataModels(logger, + withRoles("role_research_dataset_ppmi"), List.of(model)); + + assertThat(result).hasSize(1); + assertThat(result.get(0).datasetsVariables()) + .containsOnly(Map.entry("ppmi", List.of("dataset", "gender"))); + } + + @Test + void sendsEmptyMapWhenNoAuthorizedDatasetHasVariableInfo() { + List result = claimUtils.getAuthorizedDataModels(logger, + withRoles("role_research_dataset_ppmi"), List.of(dataModel(Map.of()))); + + assertThat(result).hasSize(1); + assertThat(result.get(0).datasetsVariables()).isEmpty(); + } + + @Test + void returnsModelsUnchangedForAllDatasetsClaim() { + DataModelDTO model = dataModel(Map.of("edsd", List.of("dataset"))); + + List result = claimUtils.getAuthorizedDataModels(logger, + withRoles("role_research_dataset_all"), List.of(model)); + + assertThat(result).containsExactly(model); + } +}