|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package io.opentelemetry.javaagent.extension; |
| 7 | + |
| 8 | +import static org.assertj.core.api.Assertions.assertThat; |
| 9 | +import static org.mockito.ArgumentMatchers.any; |
| 10 | +import static org.mockito.ArgumentMatchers.eq; |
| 11 | +import static org.mockito.Mockito.mock; |
| 12 | +import static org.mockito.Mockito.when; |
| 13 | + |
| 14 | +import io.opentelemetry.api.incubator.config.ConfigProvider; |
| 15 | +import io.opentelemetry.api.incubator.config.DeclarativeConfigProperties; |
| 16 | +import io.opentelemetry.sdk.autoconfigure.AutoConfiguredOpenTelemetrySdk; |
| 17 | +import io.opentelemetry.sdk.autoconfigure.internal.AutoConfigureUtil; |
| 18 | +import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties; |
| 19 | +import org.junit.jupiter.api.Test; |
| 20 | +import org.mockito.MockedStatic; |
| 21 | +import org.mockito.Mockito; |
| 22 | + |
| 23 | +@SuppressWarnings("DoNotMockAutoValue") |
| 24 | +class ConfigPropertiesUtilTest { |
| 25 | + @Test |
| 26 | + void shouldUseConfigPropertiesForAutoConfiguration() { |
| 27 | + ConfigProperties configPropertiesMock = mock(ConfigProperties.class); |
| 28 | + AutoConfiguredOpenTelemetrySdk sdkMock = mock(AutoConfiguredOpenTelemetrySdk.class); |
| 29 | + try (MockedStatic<AutoConfigureUtil> autoConfigureUtilMock = |
| 30 | + Mockito.mockStatic(AutoConfigureUtil.class)) { |
| 31 | + autoConfigureUtilMock |
| 32 | + .when(() -> AutoConfigureUtil.getConfig(sdkMock)) |
| 33 | + .thenReturn(configPropertiesMock); |
| 34 | + |
| 35 | + ConfigProperties configProperties = ConfigPropertiesUtil.resolveConfigProperties(sdkMock); |
| 36 | + |
| 37 | + assertThat(configProperties).isSameAs(configPropertiesMock); |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + @Test |
| 42 | + void shouldUseConfigProviderForDeclarativeConfiguration() { |
| 43 | + String propertyName = "testProperty"; |
| 44 | + String expectedValue = "the value"; |
| 45 | + DeclarativeConfigProperties javaNodeMock = mock(DeclarativeConfigProperties.class); |
| 46 | + when(javaNodeMock.getString(propertyName)).thenReturn(expectedValue); |
| 47 | + |
| 48 | + DeclarativeConfigProperties instrumentationConfigMock = mock(DeclarativeConfigProperties.class); |
| 49 | + when(instrumentationConfigMock.getStructured(eq("java"), any())).thenReturn(javaNodeMock); |
| 50 | + |
| 51 | + ConfigProvider configProviderMock = mock(ConfigProvider.class); |
| 52 | + when(configProviderMock.getInstrumentationConfig()).thenReturn(instrumentationConfigMock); |
| 53 | + |
| 54 | + AutoConfiguredOpenTelemetrySdk sdkMock = mock(AutoConfiguredOpenTelemetrySdk.class); |
| 55 | + |
| 56 | + try (MockedStatic<AutoConfigureUtil> autoConfigureUtilMock = |
| 57 | + Mockito.mockStatic(AutoConfigureUtil.class)) { |
| 58 | + autoConfigureUtilMock.when(() -> AutoConfigureUtil.getConfig(sdkMock)).thenReturn(null); |
| 59 | + autoConfigureUtilMock |
| 60 | + .when(() -> AutoConfigureUtil.getConfigProvider(sdkMock)) |
| 61 | + .thenReturn(configProviderMock); |
| 62 | + |
| 63 | + ConfigProperties configProperties = ConfigPropertiesUtil.resolveConfigProperties(sdkMock); |
| 64 | + |
| 65 | + assertThat(configProperties.getString(propertyName)).isEqualTo(expectedValue); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + void shouldUseConfigProviderForDeclarativeConfiguration_noInstrumentationConfig() { |
| 71 | + AutoConfiguredOpenTelemetrySdk sdkMock = mock(AutoConfiguredOpenTelemetrySdk.class); |
| 72 | + ConfigProvider configProviderMock = mock(ConfigProvider.class); |
| 73 | + when(configProviderMock.getInstrumentationConfig()).thenReturn(null); |
| 74 | + |
| 75 | + try (MockedStatic<AutoConfigureUtil> autoConfigureUtilMock = |
| 76 | + Mockito.mockStatic(AutoConfigureUtil.class)) { |
| 77 | + autoConfigureUtilMock.when(() -> AutoConfigureUtil.getConfig(sdkMock)).thenReturn(null); |
| 78 | + autoConfigureUtilMock |
| 79 | + .when(() -> AutoConfigureUtil.getConfigProvider(sdkMock)) |
| 80 | + .thenReturn(configProviderMock); |
| 81 | + |
| 82 | + ConfigProperties configProperties = ConfigPropertiesUtil.resolveConfigProperties(sdkMock); |
| 83 | + |
| 84 | + assertThat(configProperties.getString("testProperty")).isEqualTo(null); |
| 85 | + } |
| 86 | + } |
| 87 | +} |
0 commit comments