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;
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;
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" //
69 class ApplicationTest {
72 private ApplicationConfig applicationConfig;
75 SecurityContext securityContext;
78 private IcsSimulatorController icsSimulatorController;
81 private ConsumerRegstrationTask consumerRegstrationTask;
83 private com.google.gson.Gson gson = new com.google.gson.GsonBuilder().disableHtmlEscaping().create();
85 private final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
87 static class TestApplicationConfig extends ApplicationConfig {
90 public String getIcsBaseUrl() {
91 return thisProcessUrl();
94 String thisProcessUrl() {
95 final String url = "https://localhost:" + getLocalServerHttpsPort();
101 * Overrides the BeanFactory.
104 static class TestBeanFactory extends BeanFactory {
106 static final TestApplicationConfig applicationConfig = new TestApplicationConfig();
107 static KafkaTopicListener kafkaListener;
108 static InfluxStore influxStore;
112 public ServletWebServerFactory servletContainer() {
113 return new TomcatServletWebServerFactory();
118 public ApplicationConfig getApplicationConfig() {
119 return applicationConfig;
124 public KafkaTopicListener getKafkaTopicListener(@Autowired ApplicationConfig appConfig) {
125 kafkaListener = new KafkaTopicListener(applicationConfig);
126 return kafkaListener;
131 public InfluxStore getInfluxStore(@Autowired ApplicationConfig appConfig,
132 @Autowired KafkaTopicListener listener) {
133 influxStore = spy(new InfluxStore(applicationConfig));
140 public void init() {}
144 this.icsSimulatorController.testResults.reset();
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);
153 JSONObject jsonObj = new JSONObject(resp.getBody());
154 assertThat(jsonObj.remove("servers")).isNotNull();
156 String indented = jsonObj.toString(4);
157 try (PrintStream out = new PrintStream(new FileOutputStream("api/pmlog-api.json"))) {
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());
168 Mockito.doNothing().when(TestBeanFactory.influxStore).store(any(), any());
169 TestBeanFactory.influxStore.start(Flux.just(data));
171 Mockito.verify(TestBeanFactory.influxStore, Mockito.times(1)).store(any(), any());
174 private AsyncRestClient restClient() {
175 return restClient(false);
178 private String baseUrl() {
179 return "https://localhost:" + this.applicationConfig.getLocalServerHttpsPort();
182 private AsyncRestClient restClient(boolean useTrustValidation) {
183 WebClientConfig config = this.applicationConfig.getWebClientConfig();
184 HttpProxyConfig httpProxyConfig = HttpProxyConfig.builder() //
185 .httpProxyHost("") //
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();
198 AsyncRestClientFactory restClientFactory = new AsyncRestClientFactory(config, securityContext);
199 return restClientFactory.createRestClientNoHttpProxy(baseUrl());