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.pmproducer;
23 import static org.assertj.core.api.Assertions.assertThat;
24 import static org.awaitility.Awaitility.await;
25 import static org.junit.jupiter.api.Assertions.assertTrue;
26 import static org.mockito.ArgumentMatchers.any;
27 import static org.mockito.Mockito.doReturn;
28 import static org.mockito.Mockito.spy;
29 import static org.mockito.Mockito.times;
30 import static org.mockito.Mockito.verify;
31 import static org.mockito.Mockito.when;
33 import com.google.gson.JsonParser;
35 import java.io.FileOutputStream;
36 import java.io.IOException;
37 import java.io.PrintStream;
38 import java.lang.invoke.MethodHandles;
39 import java.nio.file.Files;
40 import java.nio.file.Path;
41 import java.nio.file.Paths;
42 import java.time.Duration;
43 import java.time.OffsetDateTime;
44 import java.util.List;
46 import org.apache.kafka.clients.consumer.ConsumerRecord;
47 import org.json.JSONObject;
48 import org.junit.jupiter.api.AfterEach;
49 import org.junit.jupiter.api.BeforeEach;
50 import org.junit.jupiter.api.MethodOrderer;
51 import org.junit.jupiter.api.Test;
52 import org.junit.jupiter.api.TestMethodOrder;
53 import org.mockito.ArgumentCaptor;
54 import org.oran.pmproducer.clients.AsyncRestClient;
55 import org.oran.pmproducer.clients.AsyncRestClientFactory;
56 import org.oran.pmproducer.configuration.ApplicationConfig;
57 import org.oran.pmproducer.configuration.WebClientConfig;
58 import org.oran.pmproducer.configuration.WebClientConfig.HttpProxyConfig;
59 import org.oran.pmproducer.controllers.ProducerCallbacksController;
60 import org.oran.pmproducer.datastore.DataStore;
61 import org.oran.pmproducer.datastore.DataStore.Bucket;
62 import org.oran.pmproducer.filter.FilteredData;
63 import org.oran.pmproducer.filter.PmReport;
64 import org.oran.pmproducer.filter.PmReportFilter;
65 import org.oran.pmproducer.filter.PmReportFilter.FilterData;
66 import org.oran.pmproducer.oauth2.SecurityContext;
67 import org.oran.pmproducer.r1.ConsumerJobInfo;
68 import org.oran.pmproducer.r1.ProducerJobInfo;
69 import org.oran.pmproducer.repository.InfoType;
70 import org.oran.pmproducer.repository.InfoTypes;
71 import org.oran.pmproducer.repository.Job;
72 import org.oran.pmproducer.repository.Job.Parameters;
73 import org.oran.pmproducer.repository.Job.Parameters.KafkaDeliveryInfo;
74 import org.oran.pmproducer.repository.Jobs;
75 import org.oran.pmproducer.repository.Jobs.JobGroup;
76 import org.oran.pmproducer.tasks.JobDataDistributor;
77 import org.oran.pmproducer.tasks.NewFileEvent;
78 import org.oran.pmproducer.tasks.ProducerRegstrationTask;
79 import org.oran.pmproducer.tasks.TopicListener;
80 import org.oran.pmproducer.tasks.TopicListeners;
81 import org.slf4j.Logger;
82 import org.slf4j.LoggerFactory;
83 import org.springframework.beans.factory.annotation.Autowired;
84 import org.springframework.boot.test.context.SpringBootTest;
85 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
86 import org.springframework.boot.test.context.TestConfiguration;
87 import org.springframework.boot.test.web.server.LocalServerPort;
88 import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
89 import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
90 import org.springframework.context.annotation.Bean;
91 import org.springframework.http.HttpStatus;
92 import org.springframework.http.MediaType;
93 import org.springframework.http.ResponseEntity;
94 import org.springframework.test.context.TestPropertySource;
95 import org.springframework.web.reactive.function.client.WebClientRequestException;
96 import org.springframework.web.reactive.function.client.WebClientResponseException;
98 import reactor.core.publisher.Flux;
99 import reactor.core.publisher.Mono;
100 import reactor.test.StepVerifier;
102 @TestMethodOrder(MethodOrderer.MethodName.class)
103 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
104 @TestPropertySource(properties = { //
105 "server.ssl.key-store=./config/keystore.jks", //
106 "app.webclient.trust-store=./config/truststore.jks", //
107 "app.webclient.trust-store-used=true", //
108 "app.configuration-filepath=./src/test/resources/test_application_configuration.json", //
109 "app.pm-files-path=/tmp/dmaapadaptor", //
110 "app.s3.endpointOverride=" //
112 class ApplicationTest {
115 private ApplicationConfig applicationConfig;
121 private InfoTypes types;
124 private IcsSimulatorController icsSimulatorController;
127 TopicListeners topicListeners;
130 ProducerRegstrationTask producerRegistrationTask;
133 private SecurityContext securityContext;
135 private com.google.gson.Gson gson = new com.google.gson.GsonBuilder().disableHtmlEscaping().create();
138 int localServerHttpPort;
140 private final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
142 static class TestApplicationConfig extends ApplicationConfig {
145 public String getIcsBaseUrl() {
146 return thisProcessUrl();
150 public String getSelfUrl() {
151 return thisProcessUrl();
154 private String thisProcessUrl() {
155 final String url = "https://localhost:" + getLocalServerHttpPort();
161 * Overrides the BeanFactory.
164 static class TestBeanFactory extends BeanFactory {
168 public ServletWebServerFactory servletContainer() {
169 return new TomcatServletWebServerFactory();
174 public ApplicationConfig getApplicationConfig() {
175 TestApplicationConfig cfg = new TestApplicationConfig();
182 this.applicationConfig.setLocalServerHttpPort(this.localServerHttpPort);
183 assertThat(this.jobs.size()).isZero();
185 DataStore fileStore = this.dataStore();
186 fileStore.create(DataStore.Bucket.FILES).block();
187 fileStore.create(DataStore.Bucket.LOCKS).block();
190 private DataStore dataStore() {
191 return DataStore.create(this.applicationConfig);
197 for (Job job : this.jobs.getAll()) {
198 this.icsSimulatorController.deleteJob(job.getId(), restClient());
200 await().untilAsserted(() -> assertThat(this.jobs.size()).isZero());
202 this.icsSimulatorController.testResults.reset();
204 DataStore fileStore = DataStore.create(applicationConfig);
205 fileStore.deleteBucket(Bucket.FILES);
206 fileStore.deleteBucket(Bucket.LOCKS);
210 private AsyncRestClient restClient(boolean useTrustValidation) {
211 WebClientConfig config = this.applicationConfig.getWebClientConfig();
212 HttpProxyConfig httpProxyConfig = HttpProxyConfig.builder() //
213 .httpProxyHost("") //
216 config = WebClientConfig.builder() //
217 .keyStoreType(config.getKeyStoreType()) //
218 .keyStorePassword(config.getKeyStorePassword()) //
219 .keyStore(config.getKeyStore()) //
220 .keyPassword(config.getKeyPassword()) //
221 .isTrustStoreUsed(useTrustValidation) //
222 .trustStore(config.getTrustStore()) //
223 .trustStorePassword(config.getTrustStorePassword()) //
224 .httpProxyConfig(httpProxyConfig).build();
226 AsyncRestClientFactory restClientFactory = new AsyncRestClientFactory(config, securityContext);
227 return restClientFactory.createRestClientNoHttpProxy(baseUrl());
230 private AsyncRestClient restClient() {
231 return restClient(false);
234 private String baseUrl() {
235 return "https://localhost:" + this.applicationConfig.getLocalServerHttpPort();
238 private Object toJson(String json) {
240 return JsonParser.parseString(json).getAsJsonObject();
241 } catch (Exception e) {
242 throw new NullPointerException(e.toString());
246 private ConsumerJobInfo consumerJobInfo(String typeId, String infoJobId, Object filter) {
248 return new ConsumerJobInfo(typeId, filter, "owner", "");
249 } catch (Exception e) {
254 private void waitForRegistration() {
255 producerRegistrationTask.registerTypesAndProducer().block();
256 // Register producer, Register types
257 await().untilAsserted(() -> assertThat(icsSimulatorController.testResults.registrationInfo).isNotNull());
258 producerRegistrationTask.registerTypesAndProducer().block();
260 assertThat(icsSimulatorController.testResults.registrationInfo.supportedTypeIds).hasSize(this.types.size());
261 assertThat(producerRegistrationTask.isRegisteredInIcs()).isTrue();
262 assertThat(icsSimulatorController.testResults.types).hasSize(this.types.size());
266 void generateApiDoc() throws IOException {
267 String url = "https://localhost:" + applicationConfig.getLocalServerHttpPort() + "/v3/api-docs";
268 ResponseEntity<String> resp = restClient().getForEntity(url).block();
269 assertThat(resp.getStatusCode()).isEqualTo(HttpStatus.OK);
270 JSONObject jsonObj = new JSONObject(resp.getBody());
271 assertThat(jsonObj.remove("servers")).isNotNull();
273 String indented = (jsonObj).toString(4);
274 String docDir = "api/";
275 Files.createDirectories(Paths.get(docDir));
276 try (PrintStream out = new PrintStream(new FileOutputStream(docDir + "api.json"))) {
282 void testTrustValidation() throws IOException {
283 String url = "https://localhost:" + applicationConfig.getLocalServerHttpPort() + "/v3/api-docs";
284 ResponseEntity<String> resp = restClient(true).getForEntity(url).block();
285 assertThat(resp.getStatusCode()).isEqualTo(HttpStatus.OK);
289 void testResponseCodes() throws Exception {
290 String supervisionUrl = baseUrl() + ProducerCallbacksController.SUPERVISION_URL;
291 ResponseEntity<String> resp = restClient().getForEntity(supervisionUrl).block();
292 assertThat(resp.getStatusCode()).isEqualTo(HttpStatus.OK);
294 String jobUrl = baseUrl() + ProducerCallbacksController.JOB_URL;
295 resp = restClient().deleteForEntity(jobUrl + "/junk").block();
296 assertThat(resp.getStatusCode()).isEqualTo(HttpStatus.OK);
298 ProducerJobInfo info = new ProducerJobInfo(null, "id", "typeId", "owner", "lastUpdated");
299 String body = gson.toJson(info);
300 testErrorCode(restClient().post(jobUrl, body, MediaType.APPLICATION_JSON), HttpStatus.NOT_FOUND,
301 "Could not find type");
305 void testFiltering() {
306 String path = "./src/test/resources/pm_report.json.gz";
307 DataStore fs = DataStore.create(this.applicationConfig);
308 fs.copyFileTo(Path.of(path), "pm_report.json.gz");
310 InfoType infoType = this.types.getAll().iterator().next();
311 TopicListener listener = spy(new TopicListener(this.applicationConfig, infoType));
312 NewFileEvent event = NewFileEvent.builder().filename("pm_report.json.gz").build();
313 ConsumerRecord<byte[], byte[]> cr = new ConsumerRecord<>("", 0, 0, new byte[0], gson.toJson(event).getBytes());
314 when(listener.receiveFromKafka(any())).thenReturn(Flux.just(cr));
316 KafkaDeliveryInfo deliveryInfo = KafkaDeliveryInfo.builder().topic("topic").bootStrapServers("").build();
317 JobGroup jobGroup = new JobGroup(infoType, deliveryInfo);
318 jobGroup.add(new Job("id", infoType, "owner", "lastUpdated",
319 Parameters.builder().filter(new FilterData()).build(), this.applicationConfig));
320 JobDataDistributor distributor = spy(new JobDataDistributor(jobGroup, this.applicationConfig));
322 doReturn(Mono.just("")).when(distributor).sendToClient(any());
324 distributor.start(listener.getFlux());
327 ArgumentCaptor<FilteredData> captor = ArgumentCaptor.forClass(FilteredData.class);
328 verify(distributor).sendToClient(captor.capture());
329 FilteredData data = captor.getValue();
330 PmReport report = PmReportFilter.parse(new String(data.value));
331 assertThat(report.event.getCommonEventHeader().getSourceName()).isEqualTo("O-DU-1122");
336 void testFilteringHistoricalData() {
337 DataStore fileStore = DataStore.create(this.applicationConfig);
338 fileStore.copyFileTo(Path.of("./src/test/resources/pm_report.json"),
339 "O-DU-1122/A20000626.2315+0200-2330+0200_HTTPS-6-73.json").block();
341 InfoType infoType = this.types.getAll().iterator().next();
342 TopicListener listener = spy(new TopicListener(this.applicationConfig, infoType));
343 Flux<ConsumerRecord<byte[], byte[]>> flux = Flux.just("") //
344 .delayElements(Duration.ofSeconds(10)).flatMap(s -> Flux.empty());
345 when(listener.receiveFromKafka(any())).thenReturn(flux);
347 PmReportFilter.FilterData filterData = new PmReportFilter.FilterData();
348 filterData.getSourceNames().add("O-DU-1122");
349 filterData.setPmRopStartTime("1999-12-27T10:50:44.000-08:00");
350 filterData.setPmRopEndTime(OffsetDateTime.now().toString());
351 KafkaDeliveryInfo deliveryInfo = KafkaDeliveryInfo.builder().topic("topic").bootStrapServers("").build();
352 JobGroup jobGroup = new JobGroup(infoType, deliveryInfo);
353 Parameters params = Parameters.builder().filter(filterData).build();
354 Job job = new Job("id", infoType, "owner", "lastUpdated", params, this.applicationConfig);
356 JobDataDistributor distributor = spy(new JobDataDistributor(jobGroup, this.applicationConfig));
358 doReturn(Mono.just("")).when(distributor).sendToClient(any());
360 distributor.start(listener.getFlux());
363 ArgumentCaptor<FilteredData> captor = ArgumentCaptor.forClass(FilteredData.class);
364 verify(distributor, times(2)).sendToClient(captor.capture());
365 List<FilteredData> data = captor.getAllValues();
366 assertThat(data).hasSize(2);
367 PmReport report = PmReportFilter.parse(new String(data.get(0).value));
368 assertThat(report.event.getCommonEventHeader().getSourceName()).isEqualTo("O-DU-1122");
369 assertThat(new String(data.get(1).value)).isEqualTo("{}");
374 void testCreateJob() throws Exception {
376 final String JOB_ID = "ID";
378 // Register producer, Register types
379 waitForRegistration();
381 assertThat(this.topicListeners.getTopicListeners()).hasSize(1);
383 // Create a job with a PM filter
384 PmReportFilter.FilterData filterData = new PmReportFilter.FilterData();
386 filterData.addMeasTypes("UtranCell", "succImmediateAssignProcs");
387 filterData.getMeasObjInstIds().add("UtranCell=Gbg-997");
388 filterData.getSourceNames().add("O-DU-1122");
389 filterData.getMeasuredEntityDns().add("ManagedElement=RNC-Gbg-1");
390 Job.Parameters param = Job.Parameters.builder() //
391 .filter(filterData).deliveryInfo(KafkaDeliveryInfo.builder().bootStrapServers("").topic("").build()) //
394 String paramJson = gson.toJson(param);
395 System.out.println(paramJson);
396 ConsumerJobInfo jobInfo = consumerJobInfo("PmDataOverKafka", "EI_PM_JOB_ID", toJson(paramJson));
398 this.icsSimulatorController.addJob(jobInfo, JOB_ID, restClient());
399 await().untilAsserted(() -> assertThat(this.jobs.size()).isEqualTo(1));
401 assertThat(this.topicListeners.getDataDistributors().keySet()).hasSize(1);
405 void testPmFilteringKafka() throws Exception {
406 // Test that the schema for kafka and pm filtering is OK.
409 final String JOB_ID = "ID";
411 // Register producer, Register types
412 waitForRegistration();
414 // Create a job with a PM filter
415 PmReportFilter.FilterData filterData = new PmReportFilter.FilterData();
416 filterData.addMeasTypes("ManagedElement", "succImmediateAssignProcs");
417 Job.Parameters param = Job.Parameters.builder() //
418 .filter(filterData).deliveryInfo(KafkaDeliveryInfo.builder().bootStrapServers("").topic("").build()) //
421 String paramJson = gson.toJson(param);
423 ConsumerJobInfo jobInfo = consumerJobInfo("PmDataOverKafka", "EI_PM_JOB_ID", toJson(paramJson));
424 this.icsSimulatorController.addJob(jobInfo, JOB_ID, restClient());
425 await().untilAsserted(() -> assertThat(this.jobs.size()).isEqualTo(1));
429 @SuppressWarnings("squid:S2925") // "Thread.sleep" should not be used in tests.
430 void testZZActuator() throws Exception {
431 // The test must be run last, hence the "ZZ" in the name. All succeeding tests
433 AsyncRestClient client = restClient();
434 client.post("/actuator/loggers/org.oran.pmproducer", "{\"configuredLevel\":\"trace\"}").block();
435 String resp = client.get("/actuator/loggers/org.oran.pmproducer").block();
436 assertThat(resp).contains("TRACE");
437 client.post("/actuator/loggers/org.springframework.boot.actuate", "{\"configuredLevel\":\"trace\"}").block();
438 // This will stop the web server and all coming tests will fail.
439 client.post("/actuator/shutdown", "").block();
442 String url = "https://localhost:" + applicationConfig.getLocalServerHttpPort() + "/v3/api-docs";
443 StepVerifier.create(restClient().get(url)) // Any call
444 .expectSubscription() //
445 .expectErrorMatches(t -> t instanceof WebClientRequestException) //
449 public static void testErrorCode(Mono<?> request, HttpStatus expStatus, String responseContains) {
450 testErrorCode(request, expStatus, responseContains, true);
453 public static void testErrorCode(Mono<?> request, HttpStatus expStatus, String responseContains,
454 boolean expectApplicationProblemJsonMediaType) {
455 StepVerifier.create(request) //
456 .expectSubscription() //
458 t -> checkWebClientError(t, expStatus, responseContains, expectApplicationProblemJsonMediaType)) //
462 private static boolean checkWebClientError(Throwable throwable, HttpStatus expStatus, String responseContains,
463 boolean expectApplicationProblemJsonMediaType) {
464 assertTrue(throwable instanceof WebClientResponseException);
465 WebClientResponseException responseException = (WebClientResponseException) throwable;
466 assertThat(responseException.getStatusCode()).isEqualTo(expStatus);
467 assertThat(responseException.getResponseBodyAsString()).contains(responseContains);
468 if (expectApplicationProblemJsonMediaType) {
469 assertThat(responseException.getHeaders().getContentType()).isEqualTo(MediaType.APPLICATION_PROBLEM_JSON);