Storage of PM Data
[nonrtric.git] / pmlog / src / test / java / org / oran / pmlog / ApplicationTest.java
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2023 Nordix Foundation
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
21 package org.oran.pmlog;
22
23 import static org.assertj.core.api.Assertions.assertThat;
24 import static org.mockito.ArgumentMatchers.any;
25 import static org.mockito.Mockito.spy;
26
27 import java.io.FileNotFoundException;
28 import java.io.FileOutputStream;
29 import java.io.IOException;
30 import java.io.PrintStream;
31 import java.lang.invoke.MethodHandles;
32 import java.nio.charset.Charset;
33 import java.nio.file.Files;
34 import java.nio.file.Path;
35
36 import org.json.JSONObject;
37 import org.junit.jupiter.api.AfterEach;
38 import org.junit.jupiter.api.BeforeEach;
39 import org.junit.jupiter.api.MethodOrderer;
40 import org.junit.jupiter.api.Test;
41 import org.junit.jupiter.api.TestMethodOrder;
42 import org.mockito.Mockito;
43 import org.oran.pmlog.clients.AsyncRestClient;
44 import org.oran.pmlog.clients.AsyncRestClientFactory;
45 import org.oran.pmlog.clients.SecurityContext;
46 import org.oran.pmlog.configuration.ApplicationConfig;
47 import org.oran.pmlog.configuration.WebClientConfig;
48 import org.oran.pmlog.configuration.WebClientConfig.HttpProxyConfig;
49 import org.slf4j.Logger;
50 import org.slf4j.LoggerFactory;
51 import org.springframework.beans.factory.annotation.Autowired;
52 import org.springframework.boot.test.context.SpringBootTest;
53 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
54 import org.springframework.boot.test.context.TestConfiguration;
55 import org.springframework.boot.test.web.server.LocalServerPort;
56 import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
57 import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
58 import org.springframework.context.annotation.Bean;
59 import org.springframework.http.HttpStatus;
60 import org.springframework.http.ResponseEntity;
61 import org.springframework.test.context.TestPropertySource;
62 import reactor.core.publisher.Flux;
63
64 @TestMethodOrder(MethodOrderer.MethodName.class)
65 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
66 @TestPropertySource(properties = { //
67         "server.ssl.key-store=./config/keystore.jks", //
68         "app.webclient.trust-store=./config/truststore.jks", //
69         "app.webclient.trust-store-used=true" //
70 })
71 class ApplicationTest {
72
73     @Autowired
74     private ApplicationConfig applicationConfig;
75
76     @Autowired
77     SecurityContext securityContext;
78
79     private com.google.gson.Gson gson = new com.google.gson.GsonBuilder().disableHtmlEscaping().create();
80
81     @LocalServerPort
82     int port;
83
84     private final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
85
86     static class TestApplicationConfig extends ApplicationConfig {
87
88         String thisProcessUrl() {
89             final String url = "https://localhost:" + getLocalServerHttpPort();
90             return url;
91         }
92     }
93
94     /**
95      * Overrides the BeanFactory.
96      */
97     @TestConfiguration
98     static class TestBeanFactory extends BeanFactory {
99
100         static final TestApplicationConfig applicationConfig = new TestApplicationConfig();
101         static KafkaTopicListener kafkaListener;
102         static InfluxStore influxStore;
103
104         @Override
105         @Bean
106         public ServletWebServerFactory servletContainer() {
107             return new TomcatServletWebServerFactory();
108         }
109
110         @Override
111         @Bean
112         public ApplicationConfig getApplicationConfig() {
113             return applicationConfig;
114         }
115
116         @Override
117         @Bean
118         public KafkaTopicListener getKafkaTopicListener(@Autowired ApplicationConfig appConfig) {
119             kafkaListener = new KafkaTopicListener(applicationConfig);
120             return kafkaListener;
121         }
122
123         @Override
124         @Bean
125         public InfluxStore getInfluxStore(@Autowired ApplicationConfig appConfig,
126                 @Autowired KafkaTopicListener listener) {
127             influxStore = spy(new InfluxStore(applicationConfig));
128             return influxStore;
129         }
130
131     }
132
133     @BeforeEach
134     public void init() {}
135
136     @AfterEach
137     void reset() {
138
139     }
140
141     @Test
142     void generateApiDoc() throws FileNotFoundException {
143         String url = "/v3/api-docs";
144         ResponseEntity<String> resp = restClient().getForEntity(url).block();
145         assertThat(resp.getStatusCode()).isEqualTo(HttpStatus.OK);
146
147         JSONObject jsonObj = new JSONObject(resp.getBody());
148         assertThat(jsonObj.remove("servers")).isNotNull();
149
150         String indented = jsonObj.toString(4);
151         try (PrintStream out = new PrintStream(new FileOutputStream("api/pmlog-api.json"))) {
152             out.print(indented);
153         }
154     }
155
156     @Test
157     void testPmReport() throws IOException {
158         String path = "./src/test/resources/pm_report.json";
159         String str = Files.readString(Path.of(path), Charset.defaultCharset());
160         DataFromKafkaTopic data = new DataFromKafkaTopic(null, null, str.getBytes());
161
162         Mockito.doNothing().when(TestBeanFactory.influxStore).store(any(), any());
163         TestBeanFactory.influxStore.start(Flux.just(data));
164
165         Mockito.verify(TestBeanFactory.influxStore, Mockito.times(1)).store(any(), any());
166     }
167
168     private AsyncRestClient restClient() {
169         return restClient(false);
170     }
171
172     private String baseUrl() {
173         return "https://localhost:" + this.port;
174     }
175
176     private AsyncRestClient restClient(boolean useTrustValidation) {
177         WebClientConfig config = this.applicationConfig.getWebClientConfig();
178         HttpProxyConfig httpProxyConfig = HttpProxyConfig.builder() //
179                 .httpProxyHost("") //
180                 .httpProxyPort(0) //
181                 .build();
182         config = WebClientConfig.builder() //
183                 .keyStoreType(config.getKeyStoreType()) //
184                 .keyStorePassword(config.getKeyStorePassword()) //
185                 .keyStore(config.getKeyStore()) //
186                 .keyPassword(config.getKeyPassword()) //
187                 .isTrustStoreUsed(useTrustValidation) //
188                 .trustStore(config.getTrustStore()) //
189                 .trustStorePassword(config.getTrustStorePassword()) //
190                 .httpProxyConfig(httpProxyConfig).build();
191
192         AsyncRestClientFactory restClientFactory = new AsyncRestClientFactory(config, securityContext);
193         return restClientFactory.createRestClientNoHttpProxy(baseUrl());
194     }
195
196 }