7c04de2062bb6f5540b8b5dd38e593d0bed723ed
[ccsdk/oran.git] /
1 /*-
2  * ========================LICENSE_START=================================
3  * ONAP : ccsdk oran
4  * ======================================================================
5  * Copyright (C) 2024 OpenInfra Foundation Europe. All rights reserved.
6  * ======================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ========================LICENSE_END===================================
19  */
20 package org.onap.ccsdk.oran.a1policymanagementservice.configuration;
21
22 import static org.junit.jupiter.api.Assertions.assertFalse;
23 import static org.junit.jupiter.api.Assertions.assertNotNull;
24 import static org.junit.jupiter.api.Assertions.assertThrows;
25 import static org.junit.jupiter.api.Assertions.assertTrue;
26 import static org.mockito.Mockito.mock;
27 import static org.mockito.Mockito.when;
28
29 import io.micrometer.observation.Observation;
30 import io.micrometer.observation.ObservationRegistry;
31 import io.opentelemetry.api.OpenTelemetry;
32 import io.opentelemetry.exporter.otlp.http.trace.OtlpHttpSpanExporter;
33 import io.opentelemetry.sdk.autoconfigure.AutoConfiguredOpenTelemetrySdk;
34 import jakarta.servlet.http.HttpServletRequest;
35 import java.util.Objects;
36 import org.junit.jupiter.api.Test;
37 import org.springframework.beans.BeansException;
38 import org.springframework.beans.factory.annotation.Autowired;
39 import org.springframework.boot.test.autoconfigure.actuate.observability.AutoConfigureObservability;
40 import org.springframework.boot.test.context.SpringBootTest;
41 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
42 import org.springframework.context.ApplicationContext;
43 import org.springframework.context.annotation.Bean;
44 import org.springframework.http.server.observation.ServerRequestObservationContext;
45 import org.springframework.test.context.TestPropertySource;
46 import org.springframework.util.AntPathMatcher;
47
48 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
49 @TestPropertySource(properties = { //
50     "server.ssl.key-store=./config/keystore.jks",
51     "app.webclient.trust-store=./config/truststore.jks",
52     "app.webclient.trust-store-used=true",
53     "app.vardata-directory=/tmp/pmstest",
54     "app.filepath=",
55     "app.s3.bucket=",
56     "spring.application.name=a1-pms",
57     "management.tracing.enabled=true",
58     "management.tracing.exporter.protocol=grpc",
59     "management.tracing.sampler.jaeger_remote.endpoint=http://127.0.0.1:14250",
60     "management.tracing.propagator.type=W3C"
61 })
62 @AutoConfigureObservability
63 class OtelConfigTest {
64
65     @Autowired private ApplicationContext context;
66
67     @Autowired OtelConfig otelConfig;
68
69     @Autowired ObservationRegistry observationRegistry;
70
71     @Bean
72     OpenTelemetry openTelemetry() {
73         return AutoConfiguredOpenTelemetrySdk.initialize().getOpenTelemetrySdk();
74     }
75
76     @Test
77     void otlpExporterGrpc() {
78         assertNotNull(otelConfig);
79         assertNotNull(otelConfig.otlpExporterGrpc());
80     }
81
82     @Test
83     void otlpExporterHttpNotActive() {
84         assertNotNull(otelConfig);
85         assertThrows(BeansException.class, () -> context.getBean(OtlpHttpSpanExporter.class));
86     }
87
88     @Test
89     void jaegerRemoteSampler() {
90         assertNotNull(otelConfig);
91         assertNotNull(otelConfig.jaegerRemoteSampler());
92     }
93
94     @Test
95     void skipActuatorEndpointsFromObservation() {
96         assertNotNull(otelConfig);
97         var actuatorCustomizer = otelConfig.skipActuatorEndpointsFromObservation();
98         assertNotNull(actuatorCustomizer);
99         Observation.Scope otelScope = Observation.Scope.NOOP;
100         observationRegistry.setCurrentObservationScope(otelScope);
101         Objects.requireNonNull(observationRegistry.getCurrentObservation()).start();
102     }
103
104     @Test
105     void observationPredicate() {
106         var antPathMatcher = new AntPathMatcher("/");
107         ServerRequestObservationContext serverRequestObservationContext =
108             mock(ServerRequestObservationContext.class);
109         HttpServletRequest httpServletRequest = mock(HttpServletRequest.class);
110         when(httpServletRequest.getRequestURI()).thenReturn("/actuator/health");
111         when(serverRequestObservationContext.getCarrier()).thenReturn(httpServletRequest);
112         boolean result =
113             OtelConfig.observationPredicate(antPathMatcher)
114                 .test("anything", serverRequestObservationContext);
115         assertFalse(result);
116         when(httpServletRequest.getRequestURI()).thenReturn("/api/v1/anything");
117         result =
118             OtelConfig.observationPredicate(antPathMatcher)
119                 .test("anything", serverRequestObservationContext);
120         assertTrue(result);
121     }
122 }