Skip to content

Commit b213f45

Browse files
Fix Literal Quotes in Env Vars
Fixes: #503
1 parent 77fff4f commit b213f45

File tree

2 files changed

+66
-7
lines changed

2 files changed

+66
-7
lines changed

src/main/java/de/medizininformatikinitiative/torch/config/TorchProperties.java

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,26 @@ public record TorchProperties(
2525
@NotBlank(message = "DSE mapping tree file path is required") String dseMappingTreeFile,
2626
boolean useCql
2727
) {
28+
private static final String EMPTY_QUOTES = "\"\"";
29+
30+
public static boolean isNotSet(String variable) {
31+
return variable == null || variable.isBlank() || EMPTY_QUOTES.equals(variable);
32+
}
33+
2834
public TorchProperties {
2935
if (!useCql) {
3036
if (flare == null) {
3137
throw new IllegalArgumentException("When useCql is false, flare.url must be a non-empty string");
3238
}
33-
if (flare.url() == null || flare.url().isBlank()) {
39+
if (isNotSet(flare.url())) {
3440
throw new IllegalArgumentException("When useCql is false, flare.url must be a non-empty string");
3541
}
3642
}
3743

3844
if (fhir == null) {
3945
throw new IllegalArgumentException("FHIR URL must not be null or empty");
4046
}
41-
if (fhir.url() == null || fhir.url().isBlank()) {
47+
if (isNotSet(fhir.url())) {
4248
throw new IllegalArgumentException("FHIR URL must not be null or empty");
4349
}
4450
}
@@ -74,8 +80,8 @@ public record Fhir(
7480
String password) {
7581

7682
public Fhir {
77-
if (user == null) user = "";
78-
if (password == null) password = "";
83+
if (isNotSet(user)) user = "";
84+
if (isNotSet(password)) password = "";
7985
if (oauth == null) {
8086
oauth = new Oauth(new Oauth.Issuer(""), new Oauth.Client("", ""));
8187
}
@@ -96,14 +102,14 @@ public record Oauth(@Valid Issuer issuer, @Valid Client client) {
96102

97103
public record Issuer(String uri) {
98104
public Issuer {
99-
if (uri == null) uri = "";
105+
if (isNotSet(uri)) uri = "";
100106
}
101107
}
102108

103109
public record Client(String id, String secret) {
104110
public Client {
105-
if (id == null) id = "";
106-
if (secret == null) secret = "";
111+
if (isNotSet(id)) id = "";
112+
if (isNotSet(secret)) secret = "";
107113
}
108114
}
109115
}

src/test/java/de/medizininformatikinitiative/torch/config/TorchPropertiesTest.java

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,59 @@ public class TorchPropertiesTest {
2323
"password"
2424
);
2525

26+
@Nested
27+
class TestEmptyFields {
28+
29+
@Test
30+
void testIsNotSet_withNull() {
31+
String property = null;
32+
33+
assertThat(TorchProperties.isNotSet(property)).isTrue();
34+
}
35+
36+
@Test
37+
void testIsNotSet_withBlankString() {
38+
String property = "";
39+
40+
assertThat(TorchProperties.isNotSet(property)).isTrue();
41+
}
42+
43+
@Test
44+
void testIsNotSet_withLiteralQuotes() {
45+
String property = "\"\"";
46+
47+
assertThat(TorchProperties.isNotSet(property)).isTrue();
48+
}
49+
50+
@Test
51+
void testIsNotSet_withCharSequence() {
52+
String property = "test";
53+
54+
assertThat(TorchProperties.isNotSet(property)).isFalse();
55+
}
56+
57+
@Test
58+
void testFhirProperties() {
59+
String clientSecret = "test-111243";
60+
TorchProperties.Fhir.Oauth.Issuer issuer = new TorchProperties.Fhir.Oauth.Issuer("");
61+
TorchProperties.Fhir.Oauth.Client client = new TorchProperties.Fhir.Oauth.Client(null, clientSecret);
62+
TorchProperties.Fhir fhirProperties = new TorchProperties.Fhir(
63+
"some-url",
64+
new TorchProperties.Max(1),
65+
new TorchProperties.Fhir.Page(1),
66+
new TorchProperties.Fhir.Oauth(issuer, client),
67+
new TorchProperties.Fhir.Disable(false),
68+
"\"\"",
69+
null);
70+
71+
assertThat(issuer.uri()).isBlank();
72+
assertThat(client.id()).isBlank();
73+
assertThat(client.secret()).isEqualTo(clientSecret);
74+
assertThat(fhirProperties.user()).isBlank();
75+
assertThat(fhirProperties.password()).isBlank();
76+
}
77+
}
78+
2679
@Nested
2780
class FlareValidation {
2881

0 commit comments

Comments
 (0)