Creating PM-producer
[nonrtric/plt/ranpm.git] / pmproducer / src / test / java / org / oran / pmproducer / 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.pmproducer;
22
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;
32
33 import com.google.gson.JsonParser;
34
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;
45
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.clients.SecurityContext;
57 import org.oran.pmproducer.configuration.ApplicationConfig;
58 import org.oran.pmproducer.configuration.WebClientConfig;
59 import org.oran.pmproducer.configuration.WebClientConfig.HttpProxyConfig;
60 import org.oran.pmproducer.controllers.ProducerCallbacksController;
61 import org.oran.pmproducer.datastore.DataStore;
62 import org.oran.pmproducer.datastore.DataStore.Bucket;
63 import org.oran.pmproducer.filter.FilteredData;
64 import org.oran.pmproducer.filter.PmReport;
65 import org.oran.pmproducer.filter.PmReportFilter;
66 import org.oran.pmproducer.filter.PmReportFilter.FilterData;
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;
97
98 import reactor.core.publisher.Flux;
99 import reactor.core.publisher.Mono;
100 import reactor.test.StepVerifier;
101
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=" //
111 })
112 class ApplicationTest {
113
114     @Autowired
115     private ApplicationConfig applicationConfig;
116
117     @Autowired
118     private Jobs jobs;
119
120     @Autowired
121     private InfoTypes types;
122
123     @Autowired
124     private IcsSimulatorController icsSimulatorController;
125
126     @Autowired
127     TopicListeners topicListeners;
128
129     @Autowired
130     ProducerRegstrationTask producerRegistrationTask;
131
132     @Autowired
133     private SecurityContext securityContext;
134
135     private com.google.gson.Gson gson = new com.google.gson.GsonBuilder().disableHtmlEscaping().create();
136
137     @LocalServerPort
138     int localServerHttpPort;
139
140     private final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
141
142     static class TestApplicationConfig extends ApplicationConfig {
143
144         @Override
145         public String getIcsBaseUrl() {
146             return thisProcessUrl();
147         }
148
149         @Override
150         public String getDmaapBaseUrl() {
151             return thisProcessUrl();
152         }
153
154         @Override
155         public String getSelfUrl() {
156             return thisProcessUrl();
157         }
158
159         private String thisProcessUrl() {
160             final String url = "https://localhost:" + getLocalServerHttpPort();
161             return url;
162         }
163     }
164
165     /**
166      * Overrides the BeanFactory.
167      */
168     @TestConfiguration
169     static class TestBeanFactory extends BeanFactory {
170
171         @Override
172         @Bean
173         public ServletWebServerFactory servletContainer() {
174             return new TomcatServletWebServerFactory();
175         }
176
177         // @Override
178         @Bean
179         public ApplicationConfig getApplicationConfig() {
180             TestApplicationConfig cfg = new TestApplicationConfig();
181             return cfg;
182         }
183     }
184
185     @BeforeEach
186     public void init() {
187         this.applicationConfig.setLocalServerHttpPort(this.localServerHttpPort);
188         assertThat(this.jobs.size()).isZero();
189
190         DataStore fileStore = this.dataStore();
191         fileStore.create(DataStore.Bucket.FILES).block();
192         fileStore.create(DataStore.Bucket.LOCKS).block();
193     }
194
195     private DataStore dataStore() {
196         return DataStore.create(this.applicationConfig);
197     }
198
199     @AfterEach
200     void reset() {
201
202         for (Job job : this.jobs.getAll()) {
203             this.icsSimulatorController.deleteJob(job.getId(), restClient());
204         }
205         await().untilAsserted(() -> assertThat(this.jobs.size()).isZero());
206
207         this.icsSimulatorController.testResults.reset();
208
209         DataStore fileStore = DataStore.create(applicationConfig);
210         fileStore.deleteBucket(Bucket.FILES);
211         fileStore.deleteBucket(Bucket.LOCKS);
212
213     }
214
215     private AsyncRestClient restClient(boolean useTrustValidation) {
216         WebClientConfig config = this.applicationConfig.getWebClientConfig();
217         HttpProxyConfig httpProxyConfig = HttpProxyConfig.builder() //
218                 .httpProxyHost("") //
219                 .httpProxyPort(0) //
220                 .build();
221         config = WebClientConfig.builder() //
222                 .keyStoreType(config.getKeyStoreType()) //
223                 .keyStorePassword(config.getKeyStorePassword()) //
224                 .keyStore(config.getKeyStore()) //
225                 .keyPassword(config.getKeyPassword()) //
226                 .isTrustStoreUsed(useTrustValidation) //
227                 .trustStore(config.getTrustStore()) //
228                 .trustStorePassword(config.getTrustStorePassword()) //
229                 .httpProxyConfig(httpProxyConfig).build();
230
231         AsyncRestClientFactory restClientFactory = new AsyncRestClientFactory(config, securityContext);
232         return restClientFactory.createRestClientNoHttpProxy(baseUrl());
233     }
234
235     private AsyncRestClient restClient() {
236         return restClient(false);
237     }
238
239     private String baseUrl() {
240         return "https://localhost:" + this.applicationConfig.getLocalServerHttpPort();
241     }
242
243     private String quote(String str) {
244         final String q = "\"";
245         return q + str.replace(q, "\\\"") + q;
246     }
247
248     private Object toJson(String json) {
249         try {
250             return JsonParser.parseString(json).getAsJsonObject();
251         } catch (Exception e) {
252             throw new NullPointerException(e.toString());
253         }
254     }
255
256     private ConsumerJobInfo consumerJobInfo(String typeId, String infoJobId, Object filter) {
257         try {
258             return new ConsumerJobInfo(typeId, filter, "owner", "");
259         } catch (Exception e) {
260             return null;
261         }
262     }
263
264     private void waitForRegistration() {
265         // Register producer, Register types
266         await().untilAsserted(() -> assertThat(icsSimulatorController.testResults.registrationInfo).isNotNull());
267         producerRegistrationTask.supervisionTask().block();
268
269         assertThat(icsSimulatorController.testResults.registrationInfo.supportedTypeIds).hasSize(this.types.size());
270         assertThat(producerRegistrationTask.isRegisteredInIcs()).isTrue();
271         assertThat(icsSimulatorController.testResults.types).hasSize(this.types.size());
272     }
273
274     @Test
275     void generateApiDoc() throws IOException {
276         String url = "https://localhost:" + applicationConfig.getLocalServerHttpPort() + "/v3/api-docs";
277         ResponseEntity<String> resp = restClient().getForEntity(url).block();
278         assertThat(resp.getStatusCode()).isEqualTo(HttpStatus.OK);
279         JSONObject jsonObj = new JSONObject(resp.getBody());
280         assertThat(jsonObj.remove("servers")).isNotNull();
281
282         String indented = (jsonObj).toString(4);
283         String docDir = "api/";
284         Files.createDirectories(Paths.get(docDir));
285         try (PrintStream out = new PrintStream(new FileOutputStream(docDir + "api.json"))) {
286             out.print(indented);
287         }
288     }
289
290     @Test
291     void testTrustValidation() throws IOException {
292         String url = "https://localhost:" + applicationConfig.getLocalServerHttpPort() + "/v3/api-docs";
293         ResponseEntity<String> resp = restClient(true).getForEntity(url).block();
294         assertThat(resp.getStatusCode()).isEqualTo(HttpStatus.OK);
295     }
296
297     @Test
298     void testResponseCodes() throws Exception {
299         String supervisionUrl = baseUrl() + ProducerCallbacksController.SUPERVISION_URL;
300         ResponseEntity<String> resp = restClient().getForEntity(supervisionUrl).block();
301         assertThat(resp.getStatusCode()).isEqualTo(HttpStatus.OK);
302
303         String jobUrl = baseUrl() + ProducerCallbacksController.JOB_URL;
304         resp = restClient().deleteForEntity(jobUrl + "/junk").block();
305         assertThat(resp.getStatusCode()).isEqualTo(HttpStatus.OK);
306
307         ProducerJobInfo info = new ProducerJobInfo(null, "id", "typeId", "owner", "lastUpdated");
308         String body = gson.toJson(info);
309         testErrorCode(restClient().post(jobUrl, body, MediaType.APPLICATION_JSON), HttpStatus.NOT_FOUND,
310                 "Could not find type");
311     }
312
313     @Test
314     void testFiltering() {
315         String path = "./src/test/resources/pm_report.json.gz";
316         DataStore fs = DataStore.create(this.applicationConfig);
317         fs.copyFileTo(Path.of(path), "pm_report.json.gz");
318
319         InfoType infoType = this.types.getAll().iterator().next();
320         TopicListener listener = spy(new TopicListener(this.applicationConfig, infoType));
321         NewFileEvent event = NewFileEvent.builder().filename("pm_report.json.gz").build();
322         ConsumerRecord<byte[], byte[]> cr = new ConsumerRecord<>("", 0, 0, new byte[0], gson.toJson(event).getBytes());
323         when(listener.receiveFromKafka(any())).thenReturn(Flux.just(cr));
324
325         KafkaDeliveryInfo deliveryInfo = KafkaDeliveryInfo.builder().topic("topic").bootStrapServers("").build();
326         JobGroup jobGroup = new JobGroup(infoType, deliveryInfo);
327         jobGroup.add(new Job("id", infoType, "owner", "lastUpdated",
328                 Parameters.builder().filter(new FilterData()).build(), this.applicationConfig));
329         JobDataDistributor distributor = spy(new JobDataDistributor(jobGroup, this.applicationConfig));
330
331         doReturn(Mono.just("")).when(distributor).sendToClient(any());
332
333         distributor.start(listener.getFlux());
334
335         {
336             ArgumentCaptor<FilteredData> captor = ArgumentCaptor.forClass(FilteredData.class);
337             verify(distributor).sendToClient(captor.capture());
338             FilteredData data = captor.getValue();
339             PmReport report = PmReportFilter.parse(new String(data.value));
340             assertThat(report.event.getCommonEventHeader().getSourceName()).isEqualTo("O-DU-1122");
341         }
342     }
343
344     @Test
345     void testFilteringHistoricalData() {
346         DataStore fileStore = DataStore.create(this.applicationConfig);
347         fileStore.copyFileTo(Path.of("./src/test/resources/pm_report.json"),
348                 "O-DU-1122/A20000626.2315+0200-2330+0200_HTTPS-6-73.json").block();
349
350         InfoType infoType = this.types.getAll().iterator().next();
351         TopicListener listener = spy(new TopicListener(this.applicationConfig, infoType));
352         Flux<ConsumerRecord<byte[], byte[]>> flux = Flux.just("") //
353                 .delayElements(Duration.ofSeconds(10)).flatMap(s -> Flux.empty());
354         when(listener.receiveFromKafka(any())).thenReturn(flux);
355
356         PmReportFilter.FilterData filterData = new PmReportFilter.FilterData();
357         filterData.getSourceNames().add("O-DU-1122");
358         filterData.setPmRopStartTime("1999-12-27T10:50:44.000-08:00");
359         filterData.setPmRopEndTime(OffsetDateTime.now().toString());
360         KafkaDeliveryInfo deliveryInfo = KafkaDeliveryInfo.builder().topic("topic").bootStrapServers("").build();
361         JobGroup jobGroup = new JobGroup(infoType, deliveryInfo);
362         Parameters params = Parameters.builder().filter(filterData).build();
363         Job job = new Job("id", infoType, "owner", "lastUpdated", params, this.applicationConfig);
364         jobGroup.add(job);
365         JobDataDistributor distributor = spy(new JobDataDistributor(jobGroup, this.applicationConfig));
366
367         doReturn(Mono.just("")).when(distributor).sendToClient(any());
368
369         distributor.start(listener.getFlux());
370
371         {
372             ArgumentCaptor<FilteredData> captor = ArgumentCaptor.forClass(FilteredData.class);
373             verify(distributor, times(2)).sendToClient(captor.capture());
374             List<FilteredData> data = captor.getAllValues();
375             assertThat(data).hasSize(2);
376             PmReport report = PmReportFilter.parse(new String(data.get(0).value));
377             assertThat(report.event.getCommonEventHeader().getSourceName()).isEqualTo("O-DU-1122");
378             assertThat(new String(data.get(1).value)).isEqualTo("{}");
379         }
380     }
381
382     @Test
383     void testCreateJob() throws Exception {
384         // Create a job
385         final String JOB_ID = "ID";
386
387         // Register producer, Register types
388         waitForRegistration();
389
390         assertThat(this.topicListeners.getTopicListeners()).hasSize(1);
391
392         // Create a job with a PM filter
393         PmReportFilter.FilterData filterData = new PmReportFilter.FilterData();
394
395         filterData.addMeasTypes("UtranCell", "succImmediateAssignProcs");
396         filterData.getMeasObjInstIds().add("UtranCell=Gbg-997");
397         filterData.getSourceNames().add("O-DU-1122");
398         filterData.getMeasuredEntityDns().add("ManagedElement=RNC-Gbg-1");
399         Job.Parameters param = Job.Parameters.builder() //
400                 .filter(filterData).deliveryInfo(KafkaDeliveryInfo.builder().bootStrapServers("").topic("").build()) //
401                 .build();
402
403         String paramJson = gson.toJson(param);
404         System.out.println(paramJson);
405         ConsumerJobInfo jobInfo = consumerJobInfo("PmDataOverKafka", "EI_PM_JOB_ID", toJson(paramJson));
406
407         this.icsSimulatorController.addJob(jobInfo, JOB_ID, restClient());
408         await().untilAsserted(() -> assertThat(this.jobs.size()).isEqualTo(1));
409
410         assertThat(this.topicListeners.getDataDistributors().keySet()).hasSize(1);
411     }
412
413     @Test
414     void testPmFilteringKafka() throws Exception {
415         // Test that the schema for kafka and pm filtering is OK.
416
417         // Create a job
418         final String JOB_ID = "ID";
419
420         // Register producer, Register types
421         waitForRegistration();
422
423         // Create a job with a PM filter
424         PmReportFilter.FilterData filterData = new PmReportFilter.FilterData();
425         filterData.addMeasTypes("ManagedElement", "succImmediateAssignProcs");
426         Job.Parameters param = Job.Parameters.builder() //
427                 .filter(filterData).deliveryInfo(KafkaDeliveryInfo.builder().bootStrapServers("").topic("").build()) //
428                 .build();
429
430         String paramJson = gson.toJson(param);
431
432         ConsumerJobInfo jobInfo = consumerJobInfo("PmDataOverKafka", "EI_PM_JOB_ID", toJson(paramJson));
433         this.icsSimulatorController.addJob(jobInfo, JOB_ID, restClient());
434         await().untilAsserted(() -> assertThat(this.jobs.size()).isEqualTo(1));
435     }
436
437     @Test
438     void testReRegister() throws Exception {
439         // Wait foir register types and producer
440         waitForRegistration();
441
442         // Clear the registration, should trigger a re-register
443         icsSimulatorController.testResults.reset();
444         producerRegistrationTask.supervisionTask().block();
445         await().untilAsserted(() -> assertThat(icsSimulatorController.testResults.registrationInfo).isNotNull());
446         assertThat(icsSimulatorController.testResults.registrationInfo.supportedTypeIds).hasSize(this.types.size());
447
448         // Just clear the registerred types, should trigger a re-register
449         icsSimulatorController.testResults.types.clear();
450         await().untilAsserted(() -> assertThat(icsSimulatorController.testResults.registrationInfo.supportedTypeIds)
451                 .hasSize(this.types.size()));
452     }
453
454     @Test
455     @SuppressWarnings("squid:S2925") // "Thread.sleep" should not be used in tests.
456     void testZZActuator() throws Exception {
457         // The test must be run last, hence the "ZZ" in the name. All succeeding tests
458         // will fail.
459         AsyncRestClient client = restClient();
460         client.post("/actuator/loggers/org.oran.pmproducer", "{\"configuredLevel\":\"trace\"}").block();
461         String resp = client.get("/actuator/loggers/org.oran.pmproducer").block();
462         assertThat(resp).contains("TRACE");
463         client.post("/actuator/loggers/org.springframework.boot.actuate", "{\"configuredLevel\":\"trace\"}").block();
464         // This will stop the web server and all coming tests will fail.
465         client.post("/actuator/shutdown", "").block();
466         Thread.sleep(1000);
467
468         String url = "https://localhost:" + applicationConfig.getLocalServerHttpPort() + "/v3/api-docs";
469         StepVerifier.create(restClient().get(url)) // Any call
470                 .expectSubscription() //
471                 .expectErrorMatches(t -> t instanceof WebClientRequestException) //
472                 .verify();
473     }
474
475     public static void testErrorCode(Mono<?> request, HttpStatus expStatus, String responseContains) {
476         testErrorCode(request, expStatus, responseContains, true);
477     }
478
479     public static void testErrorCode(Mono<?> request, HttpStatus expStatus, String responseContains,
480             boolean expectApplicationProblemJsonMediaType) {
481         StepVerifier.create(request) //
482                 .expectSubscription() //
483                 .expectErrorMatches(
484                         t -> checkWebClientError(t, expStatus, responseContains, expectApplicationProblemJsonMediaType)) //
485                 .verify();
486     }
487
488     private static boolean checkWebClientError(Throwable throwable, HttpStatus expStatus, String responseContains,
489             boolean expectApplicationProblemJsonMediaType) {
490         assertTrue(throwable instanceof WebClientResponseException);
491         WebClientResponseException responseException = (WebClientResponseException) throwable;
492         assertThat(responseException.getStatusCode()).isEqualTo(expStatus);
493         assertThat(responseException.getResponseBodyAsString()).contains(responseContains);
494         if (expectApplicationProblemJsonMediaType) {
495             assertThat(responseException.getHeaders().getContentType()).isEqualTo(MediaType.APPLICATION_PROBLEM_JSON);
496         }
497         return true;
498     }
499 }