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