|
| 1 | +package com.marketdata.sdk.internal; |
| 2 | + |
| 3 | +import static org.assertj.core.api.Assertions.assertThat; |
| 4 | + |
| 5 | +import java.io.IOException; |
| 6 | +import java.lang.reflect.Constructor; |
| 7 | +import java.nio.file.Files; |
| 8 | +import java.nio.file.Path; |
| 9 | +import java.util.Map; |
| 10 | +import org.junit.jupiter.api.Test; |
| 11 | +import org.junit.jupiter.api.io.TempDir; |
| 12 | + |
| 13 | +class ConfigurationTest { |
| 14 | + |
| 15 | + /** |
| 16 | + * Reflection bridge to {@code Configuration}'s private constructor. Tests need to inject custom |
| 17 | + * environment maps; production code cannot — that's the entire point of keeping the constructor |
| 18 | + * private. Encapsulating the reflection here keeps individual tests clean. |
| 19 | + */ |
| 20 | + private static Configuration newConfig( |
| 21 | + Map<String, String> systemEnv, Map<String, String> dotEnv) { |
| 22 | + try { |
| 23 | + Constructor<Configuration> ctor = |
| 24 | + Configuration.class.getDeclaredConstructor(Map.class, Map.class); |
| 25 | + ctor.setAccessible(true); |
| 26 | + return ctor.newInstance(systemEnv, dotEnv); |
| 27 | + } catch (ReflectiveOperationException e) { |
| 28 | + throw new IllegalStateException( |
| 29 | + "Could not construct Configuration via reflection — has the private ctor signature" |
| 30 | + + " changed?", |
| 31 | + e); |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + @Test |
| 36 | + void explicitWinsOverEverything() { |
| 37 | + Configuration config = |
| 38 | + newConfig( |
| 39 | + Map.of("MARKETDATA_TOKEN", "from-env"), Map.of("MARKETDATA_TOKEN", "from-dotenv")); |
| 40 | + |
| 41 | + assertThat(config.resolve("explicit-value", "MARKETDATA_TOKEN")).isEqualTo("explicit-value"); |
| 42 | + } |
| 43 | + |
| 44 | + @Test |
| 45 | + void envVarWinsOverDotEnv() { |
| 46 | + Configuration config = |
| 47 | + newConfig( |
| 48 | + Map.of("MARKETDATA_TOKEN", "from-env"), Map.of("MARKETDATA_TOKEN", "from-dotenv")); |
| 49 | + |
| 50 | + assertThat(config.resolve(null, "MARKETDATA_TOKEN")).isEqualTo("from-env"); |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + void fallsBackToDotEnvWhenEnvVarMissing() { |
| 55 | + Configuration config = newConfig(Map.of(), Map.of("MARKETDATA_TOKEN", "from-dotenv")); |
| 56 | + |
| 57 | + assertThat(config.resolve(null, "MARKETDATA_TOKEN")).isEqualTo("from-dotenv"); |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + void blankExplicitDoesNotCount() { |
| 62 | + Configuration config = newConfig(Map.of("MARKETDATA_TOKEN", "from-env"), Map.of()); |
| 63 | + |
| 64 | + assertThat(config.resolve(" ", "MARKETDATA_TOKEN")).isEqualTo("from-env"); |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + void blankEnvVarFallsThroughToDotEnv() { |
| 69 | + Configuration config = |
| 70 | + newConfig(Map.of("MARKETDATA_TOKEN", " "), Map.of("MARKETDATA_TOKEN", "from-dotenv")); |
| 71 | + |
| 72 | + assertThat(config.resolve(null, "MARKETDATA_TOKEN")).isEqualTo("from-dotenv"); |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + void resolveReturnsNullWhenAllSourcesEmpty() { |
| 77 | + Configuration config = newConfig(Map.of(), Map.of()); |
| 78 | + |
| 79 | + assertThat(config.resolve(null, "MARKETDATA_TOKEN")).isNull(); |
| 80 | + } |
| 81 | + |
| 82 | + @Test |
| 83 | + void resolveOrDefaultReturnsDefaultWhenAllEmpty() { |
| 84 | + Configuration config = newConfig(Map.of(), Map.of()); |
| 85 | + |
| 86 | + assertThat(config.resolveOrDefault(null, "MARKETDATA_BASE_URL", "https://default")) |
| 87 | + .isEqualTo("https://default"); |
| 88 | + } |
| 89 | + |
| 90 | + @Test |
| 91 | + void resolveOrDefaultPrefersResolvedValue() { |
| 92 | + Configuration config = newConfig(Map.of("MARKETDATA_BASE_URL", "https://explicit"), Map.of()); |
| 93 | + |
| 94 | + assertThat(config.resolveOrDefault(null, "MARKETDATA_BASE_URL", "https://default")) |
| 95 | + .isEqualTo("https://explicit"); |
| 96 | + } |
| 97 | + |
| 98 | + // ---------- .env file parsing ---------- |
| 99 | + |
| 100 | + @Test |
| 101 | + void readsAndParsesDotEnvFile(@TempDir Path tmp) throws IOException { |
| 102 | + Path dotenv = tmp.resolve(".env"); |
| 103 | + Files.writeString( |
| 104 | + dotenv, |
| 105 | + """ |
| 106 | + # comment line — should be ignored |
| 107 | + MARKETDATA_TOKEN=plain-token |
| 108 | + MARKETDATA_BASE_URL="https://staging.example.com" |
| 109 | + QUOTED_SINGLE='single-quoted' |
| 110 | + EMPTY_VALUE= |
| 111 | +
|
| 112 | + # blank line above |
| 113 | + BAD_LINE_NO_EQUALS |
| 114 | + =BAD_LINE_NO_KEY |
| 115 | + """); |
| 116 | + |
| 117 | + Map<String, String> parsed = Configuration.readDotEnvFile(dotenv); |
| 118 | + |
| 119 | + assertThat(parsed) |
| 120 | + .containsEntry("MARKETDATA_TOKEN", "plain-token") |
| 121 | + .containsEntry("MARKETDATA_BASE_URL", "https://staging.example.com") |
| 122 | + .containsEntry("QUOTED_SINGLE", "single-quoted") |
| 123 | + .containsEntry("EMPTY_VALUE", "") |
| 124 | + .doesNotContainKey("# comment line — should be ignored") |
| 125 | + .doesNotContainKey("BAD_LINE_NO_EQUALS"); |
| 126 | + } |
| 127 | + |
| 128 | + @Test |
| 129 | + void missingDotEnvReturnsEmpty(@TempDir Path tmp) { |
| 130 | + assertThat(Configuration.readDotEnvFile(tmp.resolve(".env"))).isEmpty(); |
| 131 | + } |
| 132 | + |
| 133 | + @Test |
| 134 | + void dotEnvParsingIntegratesWithCascade(@TempDir Path tmp) throws IOException { |
| 135 | + Path dotenv = tmp.resolve(".env"); |
| 136 | + Files.writeString(dotenv, "MARKETDATA_TOKEN=from-real-dotenv\n"); |
| 137 | + |
| 138 | + Configuration config = newConfig(Map.of(), Configuration.readDotEnvFile(dotenv)); |
| 139 | + |
| 140 | + assertThat(config.resolve(null, "MARKETDATA_TOKEN")).isEqualTo("from-real-dotenv"); |
| 141 | + } |
| 142 | +} |
0 commit comments