|
7 | 7 |
|
8 | 8 | import static org.assertj.core.api.Assertions.assertThat; |
9 | 9 | import static org.assertj.core.api.Assertions.assertThatThrownBy; |
| 10 | +import static org.mockito.Mockito.mock; |
| 11 | +import static org.mockito.Mockito.when; |
10 | 12 |
|
| 13 | +import io.opentelemetry.contrib.dynamic.policy.PolicyProvider; |
| 14 | +import io.opentelemetry.contrib.dynamic.policy.PolicyValidator; |
| 15 | +import io.opentelemetry.contrib.dynamic.policy.registry.PolicySourceConfig; |
| 16 | +import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties; |
| 17 | +import java.util.Collections; |
| 18 | +import java.util.List; |
11 | 19 | import org.junit.jupiter.api.Test; |
12 | 20 |
|
13 | 21 | class SourceKindTest { |
@@ -44,4 +52,85 @@ void fromConfigValueRejectsUnknownValue() { |
44 | 52 | .isInstanceOf(IllegalArgumentException.class) |
45 | 53 | .hasMessage("Unknown source kind: unknown"); |
46 | 54 | } |
| 55 | + |
| 56 | + @Test |
| 57 | + void createProviderReturnsNullForKindsWithoutProviderCreator() { |
| 58 | + PolicySourceConfig source = source(SourceKind.FILE, "ignored"); |
| 59 | + ConfigProperties config = opampConfig(); |
| 60 | + List<PolicyValidator> validators = Collections.emptyList(); |
| 61 | + |
| 62 | + assertThat(SourceKind.FILE.createProvider(source, config, validators)).isNull(); |
| 63 | + assertThat( |
| 64 | + SourceKind.HTTP.createProvider(source(SourceKind.HTTP, "ignored"), config, validators)) |
| 65 | + .isNull(); |
| 66 | + assertThat( |
| 67 | + SourceKind.CUSTOM.createProvider( |
| 68 | + source(SourceKind.CUSTOM, "ignored"), config, validators)) |
| 69 | + .isNull(); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + void opampCreateProviderReturnsNullWhenLocationMissing() { |
| 74 | + ConfigProperties config = opampConfig(); |
| 75 | + List<PolicyValidator> validators = Collections.emptyList(); |
| 76 | + |
| 77 | + assertThat(SourceKind.OPAMP.createProvider(source(SourceKind.OPAMP, null), config, validators)) |
| 78 | + .isNull(); |
| 79 | + assertThat(SourceKind.OPAMP.createProvider(source(SourceKind.OPAMP, " "), config, validators)) |
| 80 | + .isNull(); |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + void opampCreateProviderReturnsProviderWhenLocationPresent() { |
| 85 | + PolicyProvider provider = |
| 86 | + SourceKind.OPAMP.createProvider( |
| 87 | + source(SourceKind.OPAMP, "vendor-specific"), opampConfig(), Collections.emptyList()); |
| 88 | + |
| 89 | + assertThat(provider).isNotNull(); |
| 90 | + } |
| 91 | + |
| 92 | + @Test |
| 93 | + void opampCreateProviderReturnsNullWhenRequiredConfigMissing() { |
| 94 | + ConfigProperties config = mock(ConfigProperties.class); |
| 95 | + when(config.getString("otel.opamp.service.url")).thenReturn(null); |
| 96 | + when(config.getString("otel.service.name")).thenReturn("test-service"); |
| 97 | + when(config.getMap("otel.experimental.opamp.headers")).thenReturn(Collections.emptyMap()); |
| 98 | + when(config.getMap("otel.resource.attributes")).thenReturn(Collections.emptyMap()); |
| 99 | + |
| 100 | + PolicyProvider provider = |
| 101 | + SourceKind.OPAMP.createProvider( |
| 102 | + source(SourceKind.OPAMP, "vendor-specific"), config, Collections.emptyList()); |
| 103 | + |
| 104 | + assertThat(provider).isNull(); |
| 105 | + } |
| 106 | + |
| 107 | + @Test |
| 108 | + void createProviderRejectsNullArguments() { |
| 109 | + ConfigProperties config = opampConfig(); |
| 110 | + PolicySourceConfig source = source(SourceKind.OPAMP, "vendor-specific"); |
| 111 | + List<PolicyValidator> validators = Collections.emptyList(); |
| 112 | + |
| 113 | + assertThatThrownBy(() -> SourceKind.OPAMP.createProvider(null, config, validators)) |
| 114 | + .isInstanceOf(NullPointerException.class) |
| 115 | + .hasMessage("source cannot be null"); |
| 116 | + assertThatThrownBy(() -> SourceKind.OPAMP.createProvider(source, null, validators)) |
| 117 | + .isInstanceOf(NullPointerException.class) |
| 118 | + .hasMessage("config cannot be null"); |
| 119 | + assertThatThrownBy(() -> SourceKind.OPAMP.createProvider(source, config, null)) |
| 120 | + .isInstanceOf(NullPointerException.class) |
| 121 | + .hasMessage("validators cannot be null"); |
| 122 | + } |
| 123 | + |
| 124 | + private static PolicySourceConfig source(SourceKind kind, String location) { |
| 125 | + return new PolicySourceConfig(kind, SourceFormat.KEYVALUE, location, Collections.emptyList()); |
| 126 | + } |
| 127 | + |
| 128 | + private static ConfigProperties opampConfig() { |
| 129 | + ConfigProperties config = mock(ConfigProperties.class); |
| 130 | + when(config.getString("otel.opamp.service.url")).thenReturn("https://example.com"); |
| 131 | + when(config.getString("otel.service.name")).thenReturn("test-service"); |
| 132 | + when(config.getMap("otel.experimental.opamp.headers")).thenReturn(Collections.emptyMap()); |
| 133 | + when(config.getMap("otel.resource.attributes")).thenReturn(Collections.emptyMap()); |
| 134 | + return config; |
| 135 | + } |
47 | 136 | } |
0 commit comments