2 * ========================LICENSE_START=================================
5 * Copyright (C) 2023 Nordix Foundation
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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===================================
21 package org.oran.pmlog;
23 import static org.assertj.core.api.Assertions.assertThat;
24 import static org.mockito.ArgumentMatchers.any;
25 import static org.mockito.Mockito.spy;
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;
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;
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" //
71 class ApplicationTest {
74 private ApplicationConfig applicationConfig;
77 SecurityContext securityContext;
79 private com.google.gson.Gson gson = new com.google.gson.GsonBuilder().disableHtmlEscaping().create();
84 private final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
86 static class TestApplicationConfig extends ApplicationConfig {
88 String thisProcessUrl() {
89 final String url = "https://localhost:" + getLocalServerHttpPort();
95 * Overrides the BeanFactory.
98 static class TestBeanFactory extends BeanFactory {
100 static final TestApplicationConfig applicationConfig = new TestApplicationConfig();
101 static KafkaTopicListener kafkaListener;
102 static InfluxStore influxStore;
106 public ServletWebServerFactory servletContainer() {
107 return new TomcatServletWebServerFactory();
112 public ApplicationConfig getApplicationConfig() {
113 return applicationConfig;
118 public KafkaTopicListener getKafkaTopicListener(@Autowired ApplicationConfig appConfig) {
119 kafkaListener = new KafkaTopicListener(applicationConfig);
120 return kafkaListener;
125 public InfluxStore getInfluxStore(@Autowired ApplicationConfig appConfig,
126 @Autowired KafkaTopicListener listener) {
127 influxStore = spy(new InfluxStore(applicationConfig));
134 public void init() {}
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);
147 JSONObject jsonObj = new JSONObject(resp.getBody());
148 assertThat(jsonObj.remove("servers")).isNotNull();
150 String indented = jsonObj.toString(4);
151 try (PrintStream out = new PrintStream(new FileOutputStream("api/pmlog-api.json"))) {
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());
162 Mockito.doNothing().when(TestBeanFactory.influxStore).store(any(), any());
163 TestBeanFactory.influxStore.start(Flux.just(data));
165 Mockito.verify(TestBeanFactory.influxStore, Mockito.times(1)).store(any(), any());
168 private AsyncRestClient restClient() {
169 return restClient(false);
172 private String baseUrl() {
173 return "https://localhost:" + this.port;
176 private AsyncRestClient restClient(boolean useTrustValidation) {
177 WebClientConfig config = this.applicationConfig.getWebClientConfig();
178 HttpProxyConfig httpProxyConfig = HttpProxyConfig.builder() //
179 .httpProxyHost("") //
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();
192 AsyncRestClientFactory restClientFactory = new AsyncRestClientFactory(config, securityContext);
193 return restClientFactory.createRestClientNoHttpProxy(baseUrl());