|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package io.opentelemetry.javaagent.tooling.config; |
| 7 | + |
| 8 | +import static org.assertj.core.api.Assertions.assertThat; |
| 9 | +import static org.assertj.core.api.Assertions.assertThatThrownBy; |
| 10 | +import static org.assertj.core.api.Assertions.entry; |
| 11 | + |
| 12 | +import io.opentelemetry.instrumentation.api.config.Config; |
| 13 | +import io.opentelemetry.sdk.autoconfigure.ConfigProperties; |
| 14 | +import io.opentelemetry.sdk.autoconfigure.ConfigurationException; |
| 15 | +import java.time.Duration; |
| 16 | +import java.util.Collections; |
| 17 | +import java.util.HashMap; |
| 18 | +import java.util.Map; |
| 19 | +import org.junit.jupiter.api.Disabled; |
| 20 | +import org.junit.jupiter.api.Test; |
| 21 | + |
| 22 | +// Copied from |
| 23 | +// https://github.com/open-telemetry/opentelemetry-java/blob/main/sdk-extensions/autoconfigure/src/test/java/io/opentelemetry/sdk/autoconfigure/ConfigPropertiesTest.java |
| 24 | +class ConfigPropertiesAdapterTest { |
| 25 | + |
| 26 | + @Test |
| 27 | + void allValid() { |
| 28 | + Map<String, String> properties = new HashMap<>(); |
| 29 | + properties.put("string", "str"); |
| 30 | + properties.put("int", "10"); |
| 31 | + properties.put("long", "20"); |
| 32 | + properties.put("double", "5.4"); |
| 33 | + properties.put("list", "cat,dog,bear"); |
| 34 | + properties.put("map", "cat=meow,dog=bark,bear=growl"); |
| 35 | + properties.put("duration", "1s"); |
| 36 | + |
| 37 | + ConfigProperties config = createConfig(properties); |
| 38 | + assertThat(config.getString("string")).isEqualTo("str"); |
| 39 | + assertThat(config.getInt("int")).isEqualTo(10); |
| 40 | + assertThat(config.getLong("long")).isEqualTo(20); |
| 41 | + assertThat(config.getDouble("double")).isEqualTo(5.4); |
| 42 | + assertThat(config.getCommaSeparatedValues("list")).containsExactly("cat", "dog", "bear"); |
| 43 | + assertThat(config.getCommaSeparatedMap("map")) |
| 44 | + .containsExactly(entry("cat", "meow"), entry("dog", "bark"), entry("bear", "growl")); |
| 45 | + assertThat(config.getDuration("duration")).isEqualTo(Duration.ofSeconds(1)); |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + void allMissing() { |
| 50 | + ConfigProperties config = createConfig(Collections.emptyMap()); |
| 51 | + assertThat(config.getString("string")).isNull(); |
| 52 | + assertThat(config.getInt("int")).isNull(); |
| 53 | + assertThat(config.getLong("long")).isNull(); |
| 54 | + assertThat(config.getDouble("double")).isNull(); |
| 55 | + assertThat(config.getCommaSeparatedValues("list")).isEmpty(); |
| 56 | + assertThat(config.getCommaSeparatedMap("map")).isEmpty(); |
| 57 | + assertThat(config.getDuration("duration")).isNull(); |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + void allEmpty() { |
| 62 | + Map<String, String> properties = new HashMap<>(); |
| 63 | + properties.put("string", ""); |
| 64 | + properties.put("int", ""); |
| 65 | + properties.put("long", ""); |
| 66 | + properties.put("double", ""); |
| 67 | + properties.put("list", ""); |
| 68 | + properties.put("map", ""); |
| 69 | + properties.put("duration", ""); |
| 70 | + |
| 71 | + ConfigProperties config = createConfig(properties); |
| 72 | + assertThat(config.getString("string")).isEmpty(); |
| 73 | + assertThat(config.getInt("int")).isNull(); |
| 74 | + assertThat(config.getLong("long")).isNull(); |
| 75 | + assertThat(config.getDouble("double")).isNull(); |
| 76 | + assertThat(config.getCommaSeparatedValues("list")).isEmpty(); |
| 77 | + assertThat(config.getCommaSeparatedMap("map")).isEmpty(); |
| 78 | + assertThat(config.getDuration("duration")).isNull(); |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + @Disabled("Currently invalid values are not handled the same as the SDK") |
| 83 | + void invalidInt() { |
| 84 | + assertThatThrownBy(() -> createConfig(Collections.singletonMap("int", "bar")).getInt("int")) |
| 85 | + .isInstanceOf(ConfigurationException.class) |
| 86 | + .hasMessage("Invalid value for property int=bar. Must be a integer."); |
| 87 | + assertThatThrownBy( |
| 88 | + () -> createConfig(Collections.singletonMap("int", "999999999999999")).getInt("int")) |
| 89 | + .isInstanceOf(ConfigurationException.class) |
| 90 | + .hasMessage("Invalid value for property int=999999999999999. Must be a integer."); |
| 91 | + } |
| 92 | + |
| 93 | + @Test |
| 94 | + @Disabled("Currently invalid values are not handled the same as the SDK") |
| 95 | + void invalidLong() { |
| 96 | + assertThatThrownBy(() -> createConfig(Collections.singletonMap("long", "bar")).getLong("long")) |
| 97 | + .isInstanceOf(ConfigurationException.class) |
| 98 | + .hasMessage("Invalid value for property long=bar. Must be a long."); |
| 99 | + assertThatThrownBy( |
| 100 | + () -> |
| 101 | + createConfig(Collections.singletonMap("long", "99223372036854775807")) |
| 102 | + .getLong("long")) |
| 103 | + .isInstanceOf(ConfigurationException.class) |
| 104 | + .hasMessage("Invalid value for property long=99223372036854775807. Must be a long."); |
| 105 | + } |
| 106 | + |
| 107 | + @Test |
| 108 | + @Disabled("Currently invalid values are not handled the same as the SDK") |
| 109 | + void invalidDouble() { |
| 110 | + assertThatThrownBy( |
| 111 | + () -> createConfig(Collections.singletonMap("double", "bar")).getDouble("double")) |
| 112 | + .isInstanceOf(ConfigurationException.class) |
| 113 | + .hasMessage("Invalid value for property double=bar. Must be a double."); |
| 114 | + assertThatThrownBy( |
| 115 | + () -> createConfig(Collections.singletonMap("double", "1.0.1")).getDouble("double")) |
| 116 | + .isInstanceOf(ConfigurationException.class) |
| 117 | + .hasMessage("Invalid value for property double=1.0.1. Must be a double."); |
| 118 | + } |
| 119 | + |
| 120 | + @Test |
| 121 | + void uncleanList() { |
| 122 | + assertThat( |
| 123 | + createConfig(Collections.singletonMap("list", " a ,b,c , d,, ,")) |
| 124 | + .getCommaSeparatedValues("list")) |
| 125 | + .containsExactly("a", "b", "c", "d"); |
| 126 | + } |
| 127 | + |
| 128 | + @Test |
| 129 | + void uncleanMap() { |
| 130 | + assertThat( |
| 131 | + createConfig(Collections.singletonMap("map", " a=1 ,b=2,c = 3 , d= 4,, ,")) |
| 132 | + .getCommaSeparatedMap("map")) |
| 133 | + .containsExactly(entry("a", "1"), entry("b", "2"), entry("c", "3"), entry("d", "4")); |
| 134 | + } |
| 135 | + |
| 136 | + @Test |
| 137 | + @Disabled("Currently invalid values are not handled the same as the SDK") |
| 138 | + void invalidMap() { |
| 139 | + assertThatThrownBy( |
| 140 | + () -> |
| 141 | + createConfig(Collections.singletonMap("map", "a=1,b=")).getCommaSeparatedMap("map")) |
| 142 | + .isInstanceOf(ConfigurationException.class) |
| 143 | + .hasMessage("Invalid map property: map=a=1,b="); |
| 144 | + assertThatThrownBy( |
| 145 | + () -> |
| 146 | + createConfig(Collections.singletonMap("map", "a=1,b")).getCommaSeparatedMap("map")) |
| 147 | + .isInstanceOf(ConfigurationException.class) |
| 148 | + .hasMessage("Invalid map property: map=a=1,b"); |
| 149 | + assertThatThrownBy( |
| 150 | + () -> |
| 151 | + createConfig(Collections.singletonMap("map", "a=1,=b")).getCommaSeparatedMap("map")) |
| 152 | + .isInstanceOf(ConfigurationException.class) |
| 153 | + .hasMessage("Invalid map property: map=a=1,=b"); |
| 154 | + } |
| 155 | + |
| 156 | + @Test |
| 157 | + @Disabled("Currently invalid values are not handled the same as the SDK") |
| 158 | + void invalidDuration() { |
| 159 | + assertThatThrownBy( |
| 160 | + () -> |
| 161 | + createConfig(Collections.singletonMap("duration", "1a1ms")).getDuration("duration")) |
| 162 | + .isInstanceOf(ConfigurationException.class) |
| 163 | + .hasMessage("Invalid duration property duration=1a1ms. Expected number, found: 1a1"); |
| 164 | + assertThatThrownBy( |
| 165 | + () -> createConfig(Collections.singletonMap("duration", "9mm")).getDuration("duration")) |
| 166 | + .isInstanceOf(ConfigurationException.class) |
| 167 | + .hasMessage("Invalid duration property duration=9mm. Invalid duration string, found: mm"); |
| 168 | + } |
| 169 | + |
| 170 | + @Test |
| 171 | + void durationUnitParsing() { |
| 172 | + assertThat(createConfig(Collections.singletonMap("duration", "1")).getDuration("duration")) |
| 173 | + .isEqualTo(Duration.ofMillis(1)); |
| 174 | + assertThat(createConfig(Collections.singletonMap("duration", "2ms")).getDuration("duration")) |
| 175 | + .isEqualTo(Duration.ofMillis(2)); |
| 176 | + assertThat(createConfig(Collections.singletonMap("duration", "3s")).getDuration("duration")) |
| 177 | + .isEqualTo(Duration.ofSeconds(3)); |
| 178 | + assertThat(createConfig(Collections.singletonMap("duration", "4m")).getDuration("duration")) |
| 179 | + .isEqualTo(Duration.ofMinutes(4)); |
| 180 | + assertThat(createConfig(Collections.singletonMap("duration", "5h")).getDuration("duration")) |
| 181 | + .isEqualTo(Duration.ofHours(5)); |
| 182 | + assertThat(createConfig(Collections.singletonMap("duration", "6d")).getDuration("duration")) |
| 183 | + .isEqualTo(Duration.ofDays(6)); |
| 184 | + // Check Space handling |
| 185 | + assertThat(createConfig(Collections.singletonMap("duration", "7 ms")).getDuration("duration")) |
| 186 | + .isEqualTo(Duration.ofMillis(7)); |
| 187 | + assertThat(createConfig(Collections.singletonMap("duration", "8 ms")).getDuration("duration")) |
| 188 | + .isEqualTo(Duration.ofMillis(8)); |
| 189 | + } |
| 190 | + |
| 191 | + private static ConfigProperties createConfig(Map<String, String> properties) { |
| 192 | + return new ConfigPropertiesAdapter(Config.newBuilder().readProperties(properties).build()); |
| 193 | + } |
| 194 | +} |
0 commit comments