|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package io.opentelemetry.instrumentation.spring.autoconfigure; |
| 7 | + |
| 8 | +import static org.assertj.core.api.Assertions.assertThat; |
| 9 | + |
| 10 | +import java.util.HashMap; |
| 11 | +import java.util.List; |
| 12 | +import java.util.Map; |
| 13 | +import org.junit.jupiter.api.Test; |
| 14 | + |
| 15 | +class EmbeddedConfigFileTest { |
| 16 | + |
| 17 | + @Test |
| 18 | + void convertFlatPropsToNested_simpleProperties() { |
| 19 | + Map<String, Object> flatProps = new HashMap<>(); |
| 20 | + flatProps.put("resource.service.name", "my-service"); |
| 21 | + flatProps.put("traces.exporter", "otlp"); |
| 22 | + |
| 23 | + Map<String, Object> result = EmbeddedConfigFile.convertFlatPropsToNested(flatProps); |
| 24 | + |
| 25 | + assertThat(result) |
| 26 | + .containsOnlyKeys("resource", "traces") |
| 27 | + .satisfies( |
| 28 | + map -> { |
| 29 | + assertThat(map.get("resource")) |
| 30 | + .isInstanceOf(Map.class) |
| 31 | + .satisfies( |
| 32 | + resource -> { |
| 33 | + @SuppressWarnings("unchecked") |
| 34 | + Map<String, Object> resourceMap = (Map<String, Object>) resource; |
| 35 | + assertThat(resourceMap) |
| 36 | + .containsOnlyKeys("service") |
| 37 | + .satisfies( |
| 38 | + m -> { |
| 39 | + @SuppressWarnings("unchecked") |
| 40 | + Map<String, Object> serviceMap = |
| 41 | + (Map<String, Object>) m.get("service"); |
| 42 | + assertThat(serviceMap).containsEntry("name", "my-service"); |
| 43 | + }); |
| 44 | + }); |
| 45 | + assertThat(map.get("traces")) |
| 46 | + .isInstanceOf(Map.class) |
| 47 | + .satisfies( |
| 48 | + traces -> { |
| 49 | + @SuppressWarnings("unchecked") |
| 50 | + Map<String, Object> tracesMap = (Map<String, Object>) traces; |
| 51 | + assertThat(tracesMap).containsEntry("exporter", "otlp"); |
| 52 | + }); |
| 53 | + }); |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + void convertFlatPropsToNested_arrayProperties() { |
| 58 | + Map<String, Object> flatProps = new HashMap<>(); |
| 59 | + flatProps.put("instrumentation.java.list[0]", "one"); |
| 60 | + flatProps.put("instrumentation.java.list[1]", "two"); |
| 61 | + flatProps.put("instrumentation.java.list[2]", "three"); |
| 62 | + |
| 63 | + Map<String, Object> result = EmbeddedConfigFile.convertFlatPropsToNested(flatProps); |
| 64 | + |
| 65 | + assertThat(result) |
| 66 | + .containsOnlyKeys("instrumentation") |
| 67 | + .satisfies( |
| 68 | + map -> { |
| 69 | + @SuppressWarnings("unchecked") |
| 70 | + Map<String, Object> instrumentation = |
| 71 | + (Map<String, Object>) map.get("instrumentation"); |
| 72 | + assertThat(instrumentation) |
| 73 | + .containsOnlyKeys("java") |
| 74 | + .satisfies( |
| 75 | + m -> { |
| 76 | + @SuppressWarnings("unchecked") |
| 77 | + Map<String, Object> javaMap = (Map<String, Object>) m.get("java"); |
| 78 | + assertThat(javaMap) |
| 79 | + .containsOnlyKeys("list") |
| 80 | + .satisfies( |
| 81 | + java -> { |
| 82 | + @SuppressWarnings("unchecked") |
| 83 | + List<Object> list = (List<Object>) java.get("list"); |
| 84 | + assertThat(list).containsExactly("one", "two", "three"); |
| 85 | + }); |
| 86 | + }); |
| 87 | + }); |
| 88 | + } |
| 89 | + |
| 90 | + @Test |
| 91 | + void convertFlatPropsToNested_mixedPropertiesAndArrays() { |
| 92 | + Map<String, Object> flatProps = new HashMap<>(); |
| 93 | + flatProps.put("resource.service.name", "test-service"); |
| 94 | + flatProps.put("resource.attributes[0]", "key1=value1"); |
| 95 | + flatProps.put("resource.attributes[1]", "key2=value2"); |
| 96 | + flatProps.put("traces.exporter", "otlp"); |
| 97 | + |
| 98 | + Map<String, Object> result = EmbeddedConfigFile.convertFlatPropsToNested(flatProps); |
| 99 | + |
| 100 | + assertThat(result) |
| 101 | + .containsOnlyKeys("resource", "traces") |
| 102 | + .satisfies( |
| 103 | + map -> { |
| 104 | + @SuppressWarnings("unchecked") |
| 105 | + Map<String, Object> resource = (Map<String, Object>) map.get("resource"); |
| 106 | + assertThat(resource) |
| 107 | + .containsKeys("service", "attributes") |
| 108 | + .satisfies( |
| 109 | + r -> { |
| 110 | + @SuppressWarnings("unchecked") |
| 111 | + Map<String, Object> service = (Map<String, Object>) r.get("service"); |
| 112 | + assertThat(service).containsEntry("name", "test-service"); |
| 113 | + |
| 114 | + @SuppressWarnings("unchecked") |
| 115 | + List<Object> attributes = (List<Object>) r.get("attributes"); |
| 116 | + assertThat(attributes).containsExactly("key1=value1", "key2=value2"); |
| 117 | + }); |
| 118 | + |
| 119 | + @SuppressWarnings("unchecked") |
| 120 | + Map<String, Object> traces = (Map<String, Object>) map.get("traces"); |
| 121 | + assertThat(traces).containsEntry("exporter", "otlp"); |
| 122 | + }); |
| 123 | + } |
| 124 | + |
| 125 | + @Test |
| 126 | + void convertFlatPropsToNested_emptyMap() { |
| 127 | + Map<String, Object> flatProps = new HashMap<>(); |
| 128 | + |
| 129 | + Map<String, Object> result = EmbeddedConfigFile.convertFlatPropsToNested(flatProps); |
| 130 | + |
| 131 | + assertThat(result).isEmpty(); |
| 132 | + } |
| 133 | + |
| 134 | + @Test |
| 135 | + void convertFlatPropsToNested_singleLevelProperty() { |
| 136 | + Map<String, Object> flatProps = new HashMap<>(); |
| 137 | + flatProps.put("enabled", "true"); |
| 138 | + |
| 139 | + Map<String, Object> result = EmbeddedConfigFile.convertFlatPropsToNested(flatProps); |
| 140 | + |
| 141 | + assertThat(result).containsEntry("enabled", "true"); |
| 142 | + } |
| 143 | + |
| 144 | + @Test |
| 145 | + void convertFlatPropsToNested_arrayWithGaps() { |
| 146 | + Map<String, Object> flatProps = new HashMap<>(); |
| 147 | + flatProps.put("list[0]", "first"); |
| 148 | + flatProps.put("list[2]", "third"); |
| 149 | + |
| 150 | + Map<String, Object> result = EmbeddedConfigFile.convertFlatPropsToNested(flatProps); |
| 151 | + |
| 152 | + assertThat(result) |
| 153 | + .containsOnlyKeys("list") |
| 154 | + .satisfies( |
| 155 | + map -> { |
| 156 | + @SuppressWarnings("unchecked") |
| 157 | + List<Object> list = (List<Object>) map.get("list"); |
| 158 | + assertThat(list).hasSize(3).containsExactly("first", null, "third"); |
| 159 | + }); |
| 160 | + } |
| 161 | + |
| 162 | + @Test |
| 163 | + void convertFlatPropsToNested_deeplyNestedProperties() { |
| 164 | + Map<String, Object> flatProps = new HashMap<>(); |
| 165 | + flatProps.put("a.b.c.d.e", "deep-value"); |
| 166 | + |
| 167 | + Map<String, Object> result = EmbeddedConfigFile.convertFlatPropsToNested(flatProps); |
| 168 | + |
| 169 | + assertThat(result).containsOnlyKeys("a"); |
| 170 | + @SuppressWarnings("unchecked") |
| 171 | + Map<String, Object> a = (Map<String, Object>) result.get("a"); |
| 172 | + assertThat(a).containsOnlyKeys("b"); |
| 173 | + @SuppressWarnings("unchecked") |
| 174 | + Map<String, Object> b = (Map<String, Object>) a.get("b"); |
| 175 | + assertThat(b).containsOnlyKeys("c"); |
| 176 | + @SuppressWarnings("unchecked") |
| 177 | + Map<String, Object> c = (Map<String, Object>) b.get("c"); |
| 178 | + assertThat(c).containsOnlyKeys("d"); |
| 179 | + @SuppressWarnings("unchecked") |
| 180 | + Map<String, Object> d = (Map<String, Object>) c.get("d"); |
| 181 | + assertThat(d).containsEntry("e", "deep-value"); |
| 182 | + } |
| 183 | + |
| 184 | + @Test |
| 185 | + void convertFlatPropsToNested_nestedArrays() { |
| 186 | + Map<String, Object> flatProps = new HashMap<>(); |
| 187 | + flatProps.put("outer[0].inner[0]", "value1"); |
| 188 | + flatProps.put("outer[0].inner[1]", "value2"); |
| 189 | + flatProps.put("outer[1].inner[0]", "value3"); |
| 190 | + |
| 191 | + Map<String, Object> result = EmbeddedConfigFile.convertFlatPropsToNested(flatProps); |
| 192 | + |
| 193 | + assertThat(result) |
| 194 | + .containsOnlyKeys("outer") |
| 195 | + .satisfies( |
| 196 | + map -> { |
| 197 | + @SuppressWarnings("unchecked") |
| 198 | + List<Object> outer = (List<Object>) map.get("outer"); |
| 199 | + assertThat(outer).hasSize(2); |
| 200 | + |
| 201 | + @SuppressWarnings("unchecked") |
| 202 | + Map<String, Object> firstElement = (Map<String, Object>) outer.get(0); |
| 203 | + @SuppressWarnings("unchecked") |
| 204 | + List<Object> firstInner = (List<Object>) firstElement.get("inner"); |
| 205 | + assertThat(firstInner).containsExactly("value1", "value2"); |
| 206 | + |
| 207 | + @SuppressWarnings("unchecked") |
| 208 | + Map<String, Object> secondElement = (Map<String, Object>) outer.get(1); |
| 209 | + @SuppressWarnings("unchecked") |
| 210 | + List<Object> secondInner = (List<Object>) secondElement.get("inner"); |
| 211 | + assertThat(secondInner).containsExactly("value3"); |
| 212 | + }); |
| 213 | + } |
| 214 | +} |
0 commit comments