|
| 1 | +/* |
| 2 | + * Copyright Java Operator SDK Authors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package io.javaoperatorsdk.operator.config.loader.provider; |
| 17 | + |
| 18 | +import java.io.IOException; |
| 19 | +import java.io.UncheckedIOException; |
| 20 | +import java.nio.file.Files; |
| 21 | +import java.nio.file.Path; |
| 22 | +import java.time.Duration; |
| 23 | +import java.util.Map; |
| 24 | +import java.util.concurrent.atomic.AtomicInteger; |
| 25 | + |
| 26 | +import org.junit.jupiter.api.Test; |
| 27 | +import org.junit.jupiter.api.io.TempDir; |
| 28 | + |
| 29 | +import static org.assertj.core.api.Assertions.assertThat; |
| 30 | +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; |
| 31 | +import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; |
| 32 | + |
| 33 | +class YamlConfigProviderTest { |
| 34 | + |
| 35 | + // -- Map constructor -------------------------------------------------------- |
| 36 | + |
| 37 | + @Test |
| 38 | + void returnsEmptyWhenKeyAbsent() { |
| 39 | + var provider = new YamlConfigProvider(Map.of()); |
| 40 | + assertThat(provider.getValue("josdk.no.such.key", String.class)).isEmpty(); |
| 41 | + } |
| 42 | + |
| 43 | + @Test |
| 44 | + void returnsEmptyForNullKey() { |
| 45 | + var provider = new YamlConfigProvider(Map.of("josdk", Map.of("test", "value"))); |
| 46 | + assertThat(provider.getValue(null, String.class)).isEmpty(); |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + void readsTopLevelString() { |
| 51 | + var provider = new YamlConfigProvider(Map.of("key", "hello")); |
| 52 | + assertThat(provider.getValue("key", String.class)).hasValue("hello"); |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + void readsNestedString() { |
| 57 | + var provider = |
| 58 | + new YamlConfigProvider(Map.of("josdk", Map.of("test", Map.of("string", "hello")))); |
| 59 | + assertThat(provider.getValue("josdk.test.string", String.class)).hasValue("hello"); |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + void readsBoolean() { |
| 64 | + var provider = new YamlConfigProvider(Map.of("josdk", Map.of("test", Map.of("bool", "true")))); |
| 65 | + assertThat(provider.getValue("josdk.test.bool", Boolean.class)).hasValue(true); |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + void readsInteger() { |
| 70 | + var provider = new YamlConfigProvider(Map.of("josdk", Map.of("test", Map.of("integer", 42)))); |
| 71 | + assertThat(provider.getValue("josdk.test.integer", Integer.class)).hasValue(42); |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + void readsLong() { |
| 76 | + var provider = |
| 77 | + new YamlConfigProvider(Map.of("josdk", Map.of("test", Map.of("long", 123456789L)))); |
| 78 | + assertThat(provider.getValue("josdk.test.long", Long.class)).hasValue(123456789L); |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + void readsDouble() { |
| 83 | + var provider = |
| 84 | + new YamlConfigProvider(Map.of("josdk", Map.of("test", Map.of("double", "3.14")))); |
| 85 | + assertThat(provider.getValue("josdk.test.double", Double.class)).hasValue(3.14); |
| 86 | + } |
| 87 | + |
| 88 | + @Test |
| 89 | + void readsDuration() { |
| 90 | + var provider = |
| 91 | + new YamlConfigProvider(Map.of("josdk", Map.of("test", Map.of("duration", "PT30S")))); |
| 92 | + assertThat(provider.getValue("josdk.test.duration", Duration.class)) |
| 93 | + .hasValue(Duration.ofSeconds(30)); |
| 94 | + } |
| 95 | + |
| 96 | + @Test |
| 97 | + void returnsEmptyWhenIntermediateSegmentMissing() { |
| 98 | + var provider = new YamlConfigProvider(Map.of("josdk", Map.of("other", "value"))); |
| 99 | + assertThat(provider.getValue("josdk.test.key", String.class)).isEmpty(); |
| 100 | + } |
| 101 | + |
| 102 | + @Test |
| 103 | + void returnsEmptyWhenIntermediateSegmentIsLeaf() { |
| 104 | + // "josdk.test" is a leaf – trying to drill further should return empty |
| 105 | + var provider = new YamlConfigProvider(Map.of("josdk", Map.of("test", "leaf"))); |
| 106 | + assertThat(provider.getValue("josdk.test.key", String.class)).isEmpty(); |
| 107 | + } |
| 108 | + |
| 109 | + @Test |
| 110 | + void throwsForUnsupportedType() { |
| 111 | + var provider = |
| 112 | + new YamlConfigProvider(Map.of("josdk", Map.of("test", Map.of("unsupported", "value")))); |
| 113 | + assertThatIllegalArgumentException() |
| 114 | + .isThrownBy(() -> provider.getValue("josdk.test.unsupported", AtomicInteger.class)) |
| 115 | + .withMessageContaining("Unsupported config type"); |
| 116 | + } |
| 117 | + |
| 118 | + // -- Path constructor ------------------------------------------------------- |
| 119 | + |
| 120 | + @Test |
| 121 | + void loadsFromFile(@TempDir Path dir) throws IOException { |
| 122 | + Path file = dir.resolve("test.yaml"); |
| 123 | + Files.writeString( |
| 124 | + file, |
| 125 | + """ |
| 126 | + josdk: |
| 127 | + test: |
| 128 | + string: from-file |
| 129 | + integer: 7 |
| 130 | + """); |
| 131 | + |
| 132 | + var provider = new YamlConfigProvider(file); |
| 133 | + assertThat(provider.getValue("josdk.test.string", String.class)).hasValue("from-file"); |
| 134 | + assertThat(provider.getValue("josdk.test.integer", Integer.class)).hasValue(7); |
| 135 | + } |
| 136 | + |
| 137 | + @Test |
| 138 | + void throwsUncheckedIOExceptionForMissingFile(@TempDir Path dir) { |
| 139 | + Path missing = dir.resolve("does-not-exist.yaml"); |
| 140 | + assertThatExceptionOfType(UncheckedIOException.class) |
| 141 | + .isThrownBy(() -> new YamlConfigProvider(missing)) |
| 142 | + .withMessageContaining("does-not-exist.yaml"); |
| 143 | + } |
| 144 | +} |
0 commit comments