|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package io.opentelemetry.api.incubator.config; |
| 7 | + |
| 8 | +import static org.assertj.core.api.Assertions.assertThat; |
| 9 | + |
| 10 | +import io.opentelemetry.common.ComponentLoader; |
| 11 | +import java.util.Arrays; |
| 12 | +import java.util.Collections; |
| 13 | +import java.util.LinkedHashMap; |
| 14 | +import java.util.List; |
| 15 | +import java.util.Map; |
| 16 | +import org.junit.jupiter.api.Test; |
| 17 | + |
| 18 | +class MapBackedDeclarativeConfigPropertiesTest { |
| 19 | + |
| 20 | + private static final ComponentLoader COMPONENT_LOADER = |
| 21 | + ComponentLoader.forClassLoader( |
| 22 | + MapBackedDeclarativeConfigPropertiesTest.class.getClassLoader()); |
| 23 | + |
| 24 | + @Test |
| 25 | + void getString() { |
| 26 | + DeclarativeConfigProperties config = fromMap(mapOf("key", "value", "notString", 42)); |
| 27 | + |
| 28 | + assertThat(config.getString("key")).isEqualTo("value"); |
| 29 | + assertThat(config.getString("missing")).isNull(); |
| 30 | + assertThat(config.getString("notString")).isNull(); |
| 31 | + } |
| 32 | + |
| 33 | + @Test |
| 34 | + void getBoolean() { |
| 35 | + DeclarativeConfigProperties config = fromMap(mapOf("key", true, "notBoolean", "true")); |
| 36 | + |
| 37 | + assertThat(config.getBoolean("key")).isTrue(); |
| 38 | + assertThat(config.getBoolean("missing")).isNull(); |
| 39 | + assertThat(config.getBoolean("notBoolean")).isNull(); |
| 40 | + } |
| 41 | + |
| 42 | + @Test |
| 43 | + void getInt() { |
| 44 | + DeclarativeConfigProperties config = fromMap(mapOf("intVal", 42, "longVal", 100L, "str", "x")); |
| 45 | + |
| 46 | + assertThat(config.getInt("intVal")).isEqualTo(42); |
| 47 | + assertThat(config.getInt("longVal")).isEqualTo(100); |
| 48 | + assertThat(config.getInt("missing")).isNull(); |
| 49 | + assertThat(config.getInt("str")).isNull(); |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + void getLong() { |
| 54 | + DeclarativeConfigProperties config = fromMap(mapOf("longVal", 100L, "intVal", 42, "str", "x")); |
| 55 | + |
| 56 | + assertThat(config.getLong("longVal")).isEqualTo(100L); |
| 57 | + assertThat(config.getLong("intVal")).isEqualTo(42L); |
| 58 | + assertThat(config.getLong("missing")).isNull(); |
| 59 | + assertThat(config.getLong("str")).isNull(); |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + void getDouble() { |
| 64 | + DeclarativeConfigProperties config = |
| 65 | + fromMap(mapOf("doubleVal", 3.14, "intVal", 42, "str", "x")); |
| 66 | + |
| 67 | + assertThat(config.getDouble("doubleVal")).isEqualTo(3.14); |
| 68 | + assertThat(config.getDouble("intVal")).isEqualTo(42.0); |
| 69 | + assertThat(config.getDouble("missing")).isNull(); |
| 70 | + assertThat(config.getDouble("str")).isNull(); |
| 71 | + } |
| 72 | + |
| 73 | + @Test |
| 74 | + void getScalarList() { |
| 75 | + DeclarativeConfigProperties config = |
| 76 | + fromMap(mapOf("strings", Arrays.asList("a", "b"), "mixed", Arrays.asList("a", 1))); |
| 77 | + |
| 78 | + assertThat(config.getScalarList("strings", String.class)).containsExactly("a", "b"); |
| 79 | + assertThat(config.getScalarList("mixed", String.class)).isNull(); |
| 80 | + assertThat(config.getScalarList("missing", String.class)).isNull(); |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + void getScalarList_nonListReturnsNull() { |
| 85 | + DeclarativeConfigProperties config = fromMap(mapOf("notList", "value")); |
| 86 | + |
| 87 | + assertThat(config.getScalarList("notList", String.class)).isNull(); |
| 88 | + } |
| 89 | + |
| 90 | + @Test |
| 91 | + void getStructured() { |
| 92 | + Map<String, Object> child = mapOf("nested", "value"); |
| 93 | + DeclarativeConfigProperties config = fromMap(mapOf("child", child, "notMap", "scalar")); |
| 94 | + |
| 95 | + DeclarativeConfigProperties structured = config.getStructured("child"); |
| 96 | + assertThat(structured).isNotNull(); |
| 97 | + assertThat(structured.getString("nested")).isEqualTo("value"); |
| 98 | + assertThat(config.getStructured("missing")).isNull(); |
| 99 | + assertThat(config.getStructured("notMap")).isNull(); |
| 100 | + } |
| 101 | + |
| 102 | + @Test |
| 103 | + void getStructuredList() { |
| 104 | + List<Map<String, Object>> items = |
| 105 | + Arrays.asList(mapOf("name", "first"), mapOf("name", "second")); |
| 106 | + DeclarativeConfigProperties config = |
| 107 | + fromMap(mapOf("items", items, "badItems", Arrays.asList("notAMap"))); |
| 108 | + |
| 109 | + List<DeclarativeConfigProperties> result = config.getStructuredList("items"); |
| 110 | + assertThat(result).hasSize(2); |
| 111 | + assertThat(result.get(0).getString("name")).isEqualTo("first"); |
| 112 | + assertThat(result.get(1).getString("name")).isEqualTo("second"); |
| 113 | + assertThat(config.getStructuredList("missing")).isNull(); |
| 114 | + assertThat(config.getStructuredList("badItems")).isNull(); |
| 115 | + } |
| 116 | + |
| 117 | + @Test |
| 118 | + void getStructuredList_nonListReturnsNull() { |
| 119 | + DeclarativeConfigProperties config = fromMap(mapOf("notList", "value")); |
| 120 | + |
| 121 | + assertThat(config.getStructuredList("notList")).isNull(); |
| 122 | + } |
| 123 | + |
| 124 | + @Test |
| 125 | + void getPropertyKeys() { |
| 126 | + DeclarativeConfigProperties config = fromMap(mapOf("a", 1, "b", 2)); |
| 127 | + |
| 128 | + assertThat(config.getPropertyKeys()).containsExactlyInAnyOrder("a", "b"); |
| 129 | + } |
| 130 | + |
| 131 | + @Test |
| 132 | + void getPropertyKeys_empty() { |
| 133 | + DeclarativeConfigProperties config = fromMap(Collections.emptyMap()); |
| 134 | + |
| 135 | + assertThat(config.getPropertyKeys()).isEmpty(); |
| 136 | + } |
| 137 | + |
| 138 | + @Test |
| 139 | + void getComponentLoader() { |
| 140 | + DeclarativeConfigProperties config = fromMap(Collections.emptyMap()); |
| 141 | + |
| 142 | + assertThat(config.getComponentLoader()).isSameAs(COMPONENT_LOADER); |
| 143 | + } |
| 144 | + |
| 145 | + @Test |
| 146 | + void get_defaultMethod() { |
| 147 | + Map<String, Object> child = mapOf("nested", "value"); |
| 148 | + DeclarativeConfigProperties config = fromMap(mapOf("child", child)); |
| 149 | + |
| 150 | + assertThat(config.get("child").getString("nested")).isEqualTo("value"); |
| 151 | + assertThat(config.get("missing").getPropertyKeys()).isEmpty(); |
| 152 | + } |
| 153 | + |
| 154 | + private static DeclarativeConfigProperties fromMap(Map<String, Object> map) { |
| 155 | + return DeclarativeConfigProperties.fromMap(map, COMPONENT_LOADER); |
| 156 | + } |
| 157 | + |
| 158 | + private static Map<String, Object> mapOf(Object... entries) { |
| 159 | + Map<String, Object> result = new LinkedHashMap<>(); |
| 160 | + for (int i = 0; i < entries.length; i += 2) { |
| 161 | + result.put((String) entries[i], entries[i + 1]); |
| 162 | + } |
| 163 | + return result; |
| 164 | + } |
| 165 | +} |
0 commit comments