|
16 | 16 | package io.javaoperatorsdk.operator.config.loader.provider; |
17 | 17 |
|
18 | 18 | import java.io.IOException; |
| 19 | +import java.io.UncheckedIOException; |
19 | 20 | import java.nio.file.Files; |
20 | 21 | import java.nio.file.Path; |
21 | 22 | import java.time.Duration; |
|
26 | 27 | import org.junit.jupiter.api.io.TempDir; |
27 | 28 |
|
28 | 29 | import static org.assertj.core.api.Assertions.assertThat; |
| 30 | +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; |
29 | 31 | import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; |
30 | 32 |
|
31 | 33 | class YamlConfigProviderTest { |
@@ -132,10 +134,60 @@ void loadsFromFile(@TempDir Path dir) throws IOException { |
132 | 134 | assertThat(provider.getValue("josdk.test.integer", Integer.class)).hasValue(7); |
133 | 135 | } |
134 | 136 |
|
| 137 | + @Test |
| 138 | + void emptyFile(@TempDir Path dir) throws IOException { |
| 139 | + Path file = dir.resolve("empty.yaml"); |
| 140 | + Files.writeString(file, ""); |
| 141 | + |
| 142 | + var provider = new YamlConfigProvider(file); |
| 143 | + assertThat(provider.getValue("any.key", String.class)).isEmpty(); |
| 144 | + } |
| 145 | + |
| 146 | + @Test |
| 147 | + void fileWithCommentOnly(@TempDir Path dir) throws IOException { |
| 148 | + Path file = dir.resolve("empty.yaml"); |
| 149 | + Files.writeString( |
| 150 | + file, |
| 151 | + """ |
| 152 | + # sample comment |
| 153 | + """); |
| 154 | + var provider = new YamlConfigProvider(file); |
| 155 | + assertThat(provider.getValue("any.key", String.class)).isEmpty(); |
| 156 | + } |
| 157 | + |
135 | 158 | @Test |
136 | 159 | void returnsEmptyForNonExistingFile(@TempDir Path dir) { |
137 | 160 | Path missing = dir.resolve("does-not-exist.yaml"); |
138 | 161 | var provider = new YamlConfigProvider(missing); |
139 | 162 | assertThat(provider.getValue("any.key", String.class)).isEmpty(); |
140 | 163 | } |
| 164 | + |
| 165 | + @Test |
| 166 | + void throwsForMalformedYaml(@TempDir Path dir) throws IOException { |
| 167 | + Path file = dir.resolve("bad.yaml"); |
| 168 | + // YAML list where a map is expected |
| 169 | + Files.writeString( |
| 170 | + file, |
| 171 | + """ |
| 172 | + - item1 |
| 173 | + - item2 |
| 174 | + """); |
| 175 | + assertThatExceptionOfType(UncheckedIOException.class) |
| 176 | + .isThrownBy(() -> new YamlConfigProvider(file)); |
| 177 | + } |
| 178 | + |
| 179 | + @Test |
| 180 | + void commentWithProperDocument(@TempDir Path dir) throws IOException { |
| 181 | + Path file = dir.resolve("filewithcomment.yaml"); |
| 182 | + // YAML comment followed by a proper mapping document |
| 183 | + Files.writeString( |
| 184 | + file, |
| 185 | + """ |
| 186 | + # comment |
| 187 | + key: |
| 188 | + subkey: val |
| 189 | + """); |
| 190 | + var provider = new YamlConfigProvider(file); |
| 191 | + assertThat(provider.getValue("key.subkey", String.class)).contains("val"); |
| 192 | + } |
141 | 193 | } |
0 commit comments