diff --git a/src/main/java/de/medizininformatikinitiative/torch/config/TorchProperties.java b/src/main/java/de/medizininformatikinitiative/torch/config/TorchProperties.java index ebc92721..ed6a5b08 100644 --- a/src/main/java/de/medizininformatikinitiative/torch/config/TorchProperties.java +++ b/src/main/java/de/medizininformatikinitiative/torch/config/TorchProperties.java @@ -25,12 +25,18 @@ public record TorchProperties( @NotBlank(message = "DSE mapping tree file path is required") String dseMappingTreeFile, boolean useCql ) { + private static final String EMPTY_QUOTES = "\"\""; + + public static boolean isNotSet(String variable) { + return variable == null || variable.isBlank() || EMPTY_QUOTES.equals(variable); + } + public TorchProperties { if (!useCql) { if (flare == null) { throw new IllegalArgumentException("When useCql is false, flare.url must be a non-empty string"); } - if (flare.url() == null || flare.url().isBlank()) { + if (isNotSet(flare.url())) { throw new IllegalArgumentException("When useCql is false, flare.url must be a non-empty string"); } } @@ -38,7 +44,7 @@ public record TorchProperties( if (fhir == null) { throw new IllegalArgumentException("FHIR URL must not be null or empty"); } - if (fhir.url() == null || fhir.url().isBlank()) { + if (isNotSet(fhir.url())) { throw new IllegalArgumentException("FHIR URL must not be null or empty"); } } @@ -74,8 +80,8 @@ public record Fhir( String password) { public Fhir { - if (user == null) user = ""; - if (password == null) password = ""; + if (isNotSet(user)) user = ""; + if (isNotSet(password)) password = ""; if (oauth == null) { oauth = new Oauth(new Oauth.Issuer(""), new Oauth.Client("", "")); } @@ -96,14 +102,14 @@ public record Oauth(@Valid Issuer issuer, @Valid Client client) { public record Issuer(String uri) { public Issuer { - if (uri == null) uri = ""; + if (isNotSet(uri)) uri = ""; } } public record Client(String id, String secret) { public Client { - if (id == null) id = ""; - if (secret == null) secret = ""; + if (isNotSet(id)) id = ""; + if (isNotSet(secret)) secret = ""; } } } diff --git a/src/test/java/de/medizininformatikinitiative/torch/config/TorchPropertiesTest.java b/src/test/java/de/medizininformatikinitiative/torch/config/TorchPropertiesTest.java index e272e1e6..df5771e7 100644 --- a/src/test/java/de/medizininformatikinitiative/torch/config/TorchPropertiesTest.java +++ b/src/test/java/de/medizininformatikinitiative/torch/config/TorchPropertiesTest.java @@ -23,6 +23,59 @@ public class TorchPropertiesTest { "password" ); + @Nested + class TestEmptyFields { + + @Test + void testIsNotSet_withNull() { + String property = null; + + assertThat(TorchProperties.isNotSet(property)).isTrue(); + } + + @Test + void testIsNotSet_withBlankString() { + String property = ""; + + assertThat(TorchProperties.isNotSet(property)).isTrue(); + } + + @Test + void testIsNotSet_withLiteralQuotes() { + String property = "\"\""; + + assertThat(TorchProperties.isNotSet(property)).isTrue(); + } + + @Test + void testIsNotSet_withCharSequence() { + String property = "test"; + + assertThat(TorchProperties.isNotSet(property)).isFalse(); + } + + @Test + void testFhirProperties() { + String clientSecret = "test-111243"; + TorchProperties.Fhir.Oauth.Issuer issuer = new TorchProperties.Fhir.Oauth.Issuer(""); + TorchProperties.Fhir.Oauth.Client client = new TorchProperties.Fhir.Oauth.Client(null, clientSecret); + TorchProperties.Fhir fhirProperties = new TorchProperties.Fhir( + "some-url", + new TorchProperties.Max(1), + new TorchProperties.Fhir.Page(1), + new TorchProperties.Fhir.Oauth(issuer, client), + new TorchProperties.Fhir.Disable(false), + "\"\"", + null); + + assertThat(issuer.uri()).isBlank(); + assertThat(client.id()).isBlank(); + assertThat(client.secret()).isEqualTo(clientSecret); + assertThat(fhirProperties.user()).isBlank(); + assertThat(fhirProperties.password()).isBlank(); + } + } + @Nested class FlareValidation {