summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul-Christian Volkmer2026-09-07 09:23:53 +0200
committerGitHub2026-09-07 07:23:53 +0000
commita0b85ed6a990e8cf533be4ace3b1fabb5ad14d6f (patch)
treea2446afa7d62bf3157b542d138e68c484873d345
parent26edfd897f0a65d252ebf74f1b4f33cba0742643 (diff)
feat: fail early on invalid gICS HTTP(S) URI (#316)
-rw-r--r--src/main/java/dev/dnpm/etl/processor/consent/GicsConsentService.java33
-rw-r--r--src/main/java/dev/dnpm/etl/processor/consent/GicsGetBroadConsentService.java2
-rw-r--r--src/test/java/dev/dnpm/etl/processor/consent/GicsConsentServiceTest.java27
3 files changed, 46 insertions, 16 deletions
diff --git a/src/main/java/dev/dnpm/etl/processor/consent/GicsConsentService.java b/src/main/java/dev/dnpm/etl/processor/consent/GicsConsentService.java
index 20fa3c4..35fc3be 100644
--- a/src/main/java/dev/dnpm/etl/processor/consent/GicsConsentService.java
+++ b/src/main/java/dev/dnpm/etl/processor/consent/GicsConsentService.java
@@ -25,6 +25,7 @@ import dev.dnpm.etl.processor.config.GIcsConfigProperties;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Date;
+import java.util.Objects;
import kotlin.random.Random;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
@@ -59,6 +60,7 @@ public class GicsConsentService extends AbstractConsentService {
private final RetryTemplate retryTemplate;
private final RestTemplate restTemplate;
private final GIcsConfigProperties gIcsConfigProperties;
+ private final URI gIcsUri;
public GicsConsentService(
GIcsConfigProperties gIcsConfigProperties,
@@ -70,6 +72,20 @@ public class GicsConsentService extends AbstractConsentService {
this.retryTemplate = retryTemplate;
this.restTemplate = restTemplate;
this.gIcsConfigProperties = gIcsConfigProperties;
+
+ Objects.requireNonNull(
+ this.gIcsConfigProperties.getUri(), "gICS URI must be a valid URI in configuration");
+ try {
+ final var uri = URI.create(this.gIcsConfigProperties.getUri());
+ if (null == uri.getScheme()
+ || !(uri.getScheme().equals("http") || uri.getScheme().equals("https"))) {
+ throw new IllegalArgumentException("gICS URI must be a valid URI in configuration");
+ }
+ this.gIcsUri = uri;
+ } catch (IllegalArgumentException e) {
+ throw new IllegalArgumentException("gICS URI must be a valid URI in configuration", e);
+ }
+
log.info("GicsConsentService initialized...");
}
@@ -127,20 +143,7 @@ public class GicsConsentService extends AbstractConsentService {
}
private URI endpointUri(String endpoint) throws URISyntaxException {
- if (null == this.gIcsConfigProperties.getUri()) {
- throw new URISyntaxException("null", "URI must not be null");
- }
- var gPasUrl1 = this.gIcsConfigProperties.getUri();
- if (this.gIcsConfigProperties.getUri().lastIndexOf("/")
- == this.gIcsConfigProperties.getUri().length() - 1) {
- gPasUrl1 =
- this.gIcsConfigProperties
- .getUri()
- .substring(0, this.gIcsConfigProperties.getUri().length() - 1);
- }
- var urlBuilder = new URIBuilder(new URI(gPasUrl1)).appendPath(endpoint);
-
- return urlBuilder.build();
+ return new URIBuilder(this.gIcsUri).appendPath(endpoint).build();
}
private HttpHeaders headersWithHttpBasicAuth() {
@@ -194,7 +197,7 @@ public class GicsConsentService extends AbstractConsentService {
log.error(msg);
return null;
} catch (URISyntaxException e) {
- var msg = String.format("Invalid URI for consents status request: '%s", e.getMessage());
+ var msg = String.format("Invalid URI for consents status request: '%s'", e.getMessage());
log.error(msg);
return null;
}
diff --git a/src/main/java/dev/dnpm/etl/processor/consent/GicsGetBroadConsentService.java b/src/main/java/dev/dnpm/etl/processor/consent/GicsGetBroadConsentService.java
index 3b75e7c..fdf0a65 100644
--- a/src/main/java/dev/dnpm/etl/processor/consent/GicsGetBroadConsentService.java
+++ b/src/main/java/dev/dnpm/etl/processor/consent/GicsGetBroadConsentService.java
@@ -156,7 +156,7 @@ public class GicsGetBroadConsentService extends AbstractConsentService {
log.error(msg);
return null;
} catch (URISyntaxException e) {
- var msg = String.format("Invalid URI for consents status request: '%s", e.getMessage());
+ var msg = String.format("Invalid URI for consents status request: '%s'", e.getMessage());
log.error(msg);
return null;
}
diff --git a/src/test/java/dev/dnpm/etl/processor/consent/GicsConsentServiceTest.java b/src/test/java/dev/dnpm/etl/processor/consent/GicsConsentServiceTest.java
index 1f9e698..90c0734 100644
--- a/src/test/java/dev/dnpm/etl/processor/consent/GicsConsentServiceTest.java
+++ b/src/test/java/dev/dnpm/etl/processor/consent/GicsConsentServiceTest.java
@@ -22,6 +22,7 @@ package dev.dnpm.etl.processor.consent;
import static dev.dnpm.etl.processor.consent.GicsConsentService.IS_CONSENTED_ENDPOINT;
import static org.assertj.core.api.Assertions.assertThat;
+import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.springframework.test.web.client.match.MockRestRequestMatchers.requestTo;
import static org.springframework.test.web.client.response.MockRestResponseCreators.withServerError;
import static org.springframework.test.web.client.response.MockRestResponseCreators.withSuccess;
@@ -44,7 +45,10 @@ import org.hl7.fhir.r4.model.OperationOutcome.IssueType;
import org.hl7.fhir.r4.model.OperationOutcome.OperationOutcomeIssueComponent;
import org.hl7.fhir.r4.model.Parameters.ParametersParameterComponent;
import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.ValueSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.restclient.test.autoconfigure.RestClientTest;
import org.springframework.http.MediaType;
@@ -257,4 +261,27 @@ class GicsConsentServiceTest {
assertThat(fhirJsonParser.encodeToString(actual)).isEqualTo(miiConsent);
}
+
+ @Nested
+ static class WithoutValidConfig {
+
+ @ParameterizedTest
+ @ValueSource(strings = {"", "mailto:test@example.com", "ftp://localhost"})
+ void shouldNotUseGicsConsentServiceWithoutGicsUri(String uri) throws Exception {
+ final var testConfigProperties =
+ new GIcsConfigProperties(uri, null, null, "", "", null, "", "", "", "", "", "");
+
+ final var exception =
+ assertThrows(
+ IllegalArgumentException.class,
+ () ->
+ new GicsConsentService(
+ testConfigProperties,
+ RetryTemplate.builder().maxAttempts(1).build(),
+ new RestTemplate(),
+ new AppFhirConfig()));
+
+ assertThat(exception.getMessage()).isEqualTo("gICS URI must be a valid URI in configuration");
+ }
+ }
}