Reverting to springboot version 2.5.8
[nonrtric.git] / dmaap-adaptor-java / src / test / java / org / oran / dmaapadapter / ApplicationTest.java
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2021 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.dmaapadapter;
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
27 import com.google.gson.JsonParser;
28
29 import java.io.FileOutputStream;
30 import java.io.IOException;
31 import java.io.PrintStream;
32 import java.nio.file.Files;
33 import java.nio.file.Paths;
34
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.Test;
39 import org.junit.jupiter.api.extension.ExtendWith;
40 import org.oran.dmaapadapter.clients.AsyncRestClient;
41 import org.oran.dmaapadapter.clients.AsyncRestClientFactory;
42 import org.oran.dmaapadapter.configuration.ApplicationConfig;
43 import org.oran.dmaapadapter.configuration.ImmutableHttpProxyConfig;
44 import org.oran.dmaapadapter.configuration.ImmutableWebClientConfig;
45 import org.oran.dmaapadapter.configuration.WebClientConfig;
46 import org.oran.dmaapadapter.configuration.WebClientConfig.HttpProxyConfig;
47 import org.oran.dmaapadapter.controllers.ProducerCallbacksController;
48 import org.oran.dmaapadapter.r1.ConsumerJobInfo;
49 import org.oran.dmaapadapter.r1.ProducerJobInfo;
50 import org.oran.dmaapadapter.repository.InfoTypes;
51 import org.oran.dmaapadapter.repository.Job;
52 import org.oran.dmaapadapter.repository.Jobs;
53 import org.oran.dmaapadapter.tasks.KafkaJobDataConsumer;
54 import org.oran.dmaapadapter.tasks.KafkaTopicConsumers;
55 import org.oran.dmaapadapter.tasks.ProducerRegstrationTask;
56 import org.springframework.beans.factory.annotation.Autowired;
57 import org.springframework.boot.test.context.SpringBootTest;
58 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
59 import org.springframework.boot.test.context.TestConfiguration;
60 import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
61 import org.springframework.boot.web.server.LocalServerPort;
62 import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
63 import org.springframework.context.annotation.Bean;
64 import org.springframework.http.HttpStatus;
65 import org.springframework.http.MediaType;
66 import org.springframework.http.ResponseEntity;
67 import org.springframework.test.context.TestPropertySource;
68 import org.springframework.test.context.junit.jupiter.SpringExtension;
69 import org.springframework.web.reactive.function.client.WebClientResponseException;
70
71 import reactor.core.publisher.Flux;
72 import reactor.core.publisher.Mono;
73 import reactor.test.StepVerifier;
74
75 @ExtendWith(SpringExtension.class)
76 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
77 @TestPropertySource(properties = { //
78         "server.ssl.key-store=./config/keystore.jks", //
79         "app.webclient.trust-store=./config/truststore.jks", //
80         "app.configuration-filepath=./src/test/resources/test_application_configuration.json"//
81 })
82 class ApplicationTest {
83
84     @Autowired
85     private ApplicationConfig applicationConfig;
86
87     @Autowired
88     private Jobs jobs;
89
90     @Autowired
91     private InfoTypes types;
92
93     @Autowired
94     private ConsumerController consumerController;
95
96     @Autowired
97     private IcsSimulatorController icsSimulatorController;
98
99     @Autowired
100     KafkaTopicConsumers kafkaTopicConsumers;
101
102     @Autowired
103     ProducerRegstrationTask producerRegistrationTask;
104
105     private com.google.gson.Gson gson = new com.google.gson.GsonBuilder().create();
106
107     @LocalServerPort
108     int localServerHttpPort;
109
110     static class TestApplicationConfig extends ApplicationConfig {
111         @Override
112         public String getIcsBaseUrl() {
113             return thisProcessUrl();
114         }
115
116         @Override
117         public String getDmaapBaseUrl() {
118             return thisProcessUrl();
119         }
120
121         @Override
122         public String getSelfUrl() {
123             return thisProcessUrl();
124         }
125
126         private String thisProcessUrl() {
127             final String url = "https://localhost:" + getLocalServerHttpPort();
128             return url;
129         }
130     }
131
132     /**
133      * Overrides the BeanFactory.
134      */
135     @TestConfiguration
136     static class TestBeanFactory extends BeanFactory {
137
138         @Override
139         @Bean
140         public ServletWebServerFactory servletContainer() {
141             return new TomcatServletWebServerFactory();
142         }
143
144         @Override
145         @Bean
146         public ApplicationConfig getApplicationConfig() {
147             TestApplicationConfig cfg = new TestApplicationConfig();
148             return cfg;
149         }
150     }
151
152     @BeforeEach
153     void setPort() {
154         this.applicationConfig.setLocalServerHttpPort(this.localServerHttpPort);
155     }
156
157     @AfterEach
158     void reset() {
159         this.consumerController.testResults.reset();
160         this.icsSimulatorController.testResults.reset();
161         this.jobs.clear();
162     }
163
164     private AsyncRestClient restClient(boolean useTrustValidation) {
165         WebClientConfig config = this.applicationConfig.getWebClientConfig();
166         HttpProxyConfig httpProxyConfig = ImmutableHttpProxyConfig.builder() //
167                 .httpProxyHost("") //
168                 .httpProxyPort(0) //
169                 .build();
170         config = ImmutableWebClientConfig.builder() //
171                 .keyStoreType(config.keyStoreType()) //
172                 .keyStorePassword(config.keyStorePassword()) //
173                 .keyStore(config.keyStore()) //
174                 .keyPassword(config.keyPassword()) //
175                 .isTrustStoreUsed(useTrustValidation) //
176                 .trustStore(config.trustStore()) //
177                 .trustStorePassword(config.trustStorePassword()) //
178                 .httpProxyConfig(httpProxyConfig).build();
179
180         AsyncRestClientFactory restClientFactory = new AsyncRestClientFactory(config);
181         return restClientFactory.createRestClientNoHttpProxy(baseUrl());
182     }
183
184     private AsyncRestClient restClient() {
185         return restClient(false);
186     }
187
188     private String baseUrl() {
189         return "https://localhost:" + this.applicationConfig.getLocalServerHttpPort();
190     }
191
192     private ConsumerJobInfo consumerJobInfo() {
193         return consumerJobInfo("DmaapInformationType", "EI_JOB_ID");
194     }
195
196     private Object jsonObject() {
197         return jsonObject("{}");
198     }
199
200     private Object jsonObject(String json) {
201         try {
202             return JsonParser.parseString(json).getAsJsonObject();
203         } catch (Exception e) {
204             throw new NullPointerException(e.toString());
205         }
206     }
207
208     private ConsumerJobInfo consumerJobInfo(String typeId, String infoJobId) {
209         try {
210             String targetUri = baseUrl() + ConsumerController.CONSUMER_TARGET_URL;
211             return new ConsumerJobInfo(typeId, jsonObject(), "owner", targetUri, "");
212         } catch (Exception e) {
213             return null;
214         }
215     }
216
217     @Test
218     void generateApiDoc() throws IOException {
219         String url = "https://localhost:" + applicationConfig.getLocalServerHttpPort() + "/v3/api-docs";
220         ResponseEntity<String> resp = restClient().getForEntity(url).block();
221         assertThat(resp.getStatusCode()).isEqualTo(HttpStatus.OK);
222         JSONObject jsonObj = new JSONObject(resp.getBody());
223         assertThat(jsonObj.remove("servers")).isNotNull();
224
225         String indented = (jsonObj).toString(4);
226         String docDir = "api/";
227         Files.createDirectories(Paths.get(docDir));
228         try (PrintStream out = new PrintStream(new FileOutputStream(docDir + "api.json"))) {
229             out.print(indented);
230         }
231     }
232
233     @Test
234     void testResponseCodes() throws Exception {
235         String supervisionUrl = baseUrl() + ProducerCallbacksController.SUPERVISION_URL;
236         ResponseEntity<String> resp = restClient().getForEntity(supervisionUrl).block();
237         assertThat(resp.getStatusCode()).isEqualTo(HttpStatus.OK);
238
239         String jobUrl = baseUrl() + ProducerCallbacksController.JOB_URL;
240         resp = restClient().deleteForEntity(jobUrl + "/junk").block();
241         assertThat(resp.getStatusCode()).isEqualTo(HttpStatus.OK);
242
243         ProducerJobInfo info = new ProducerJobInfo(null, "id", "typeId", "targetUri", "owner", "lastUpdated");
244         String body = gson.toJson(info);
245         testErrorCode(restClient().post(jobUrl, body, MediaType.APPLICATION_JSON), HttpStatus.NOT_FOUND,
246                 "Could not find type");
247     }
248
249     @Test
250     void testReceiveAndPostDataFromKafka() {
251         final String JOB_ID = "ID";
252         final String TYPE_ID = "KafkaInformationType";
253         await().untilAsserted(() -> assertThat(icsSimulatorController.testResults.registrationInfo).isNotNull());
254         assertThat(icsSimulatorController.testResults.registrationInfo.supportedTypeIds).hasSize(this.types.size());
255
256         // Create a job
257         Job.Parameters param = new Job.Parameters("", new Job.BufferTimeout(123, 456), 1);
258         String targetUri = baseUrl() + ConsumerController.CONSUMER_TARGET_URL;
259         ConsumerJobInfo kafkaJobInfo =
260                 new ConsumerJobInfo(TYPE_ID, jsonObject(gson.toJson(param)), "owner", targetUri, "");
261
262         this.icsSimulatorController.addJob(kafkaJobInfo, JOB_ID, restClient());
263         await().untilAsserted(() -> assertThat(this.jobs.size()).isEqualTo(1));
264
265         KafkaJobDataConsumer kafkaConsumer = this.kafkaTopicConsumers.getConsumers().get(TYPE_ID, JOB_ID);
266
267         // Handle received data from Kafka, check that it has been posted to the
268         // consumer
269         kafkaConsumer.start(Flux.just("data"));
270
271         ConsumerController.TestResults consumer = this.consumerController.testResults;
272         await().untilAsserted(() -> assertThat(consumer.receivedBodies.size()).isEqualTo(1));
273         assertThat(consumer.receivedBodies.get(0)).isEqualTo("[\"data\"]");
274
275         // Test send an exception
276         kafkaConsumer.start(Flux.error(new NullPointerException()));
277
278         // Test regular restart of stopped
279         kafkaConsumer.stop();
280         this.kafkaTopicConsumers.restartNonRunningTopics();
281         await().untilAsserted(() -> assertThat(kafkaConsumer.isRunning()).isTrue());
282
283         // Delete the job
284         this.icsSimulatorController.deleteJob(JOB_ID, restClient());
285         await().untilAsserted(() -> assertThat(this.jobs.size()).isZero());
286     }
287
288     @Test
289     void testReceiveAndPostDataFromDmaap() throws Exception {
290         final String JOB_ID = "ID";
291
292         // Register producer, Register types
293         await().untilAsserted(() -> assertThat(icsSimulatorController.testResults.registrationInfo).isNotNull());
294         assertThat(icsSimulatorController.testResults.registrationInfo.supportedTypeIds).hasSize(this.types.size());
295         assertThat(producerRegistrationTask.isRegisteredInIcs()).isTrue();
296         producerRegistrationTask.supervisionTask().block();
297
298         // Create a job
299         this.icsSimulatorController.addJob(consumerJobInfo(), JOB_ID, restClient());
300         await().untilAsserted(() -> assertThat(this.jobs.size()).isEqualTo(1));
301
302         // Return two messages from DMAAP and verify that these are sent to the owner of
303         // the job (consumer)
304         DmaapSimulatorController.dmaapResponses.add("DmaapResponse1");
305         DmaapSimulatorController.dmaapResponses.add("DmaapResponse2");
306         ConsumerController.TestResults consumer = this.consumerController.testResults;
307         await().untilAsserted(() -> assertThat(consumer.receivedBodies.size()).isEqualTo(2));
308         assertThat(consumer.receivedBodies.get(0)).isEqualTo("DmaapResponse1");
309
310         String jobUrl = baseUrl() + ProducerCallbacksController.JOB_URL;
311         String jobs = restClient().get(jobUrl).block();
312         assertThat(jobs).contains(JOB_ID);
313
314         // Delete the job
315         this.icsSimulatorController.deleteJob(JOB_ID, restClient());
316         await().untilAsserted(() -> assertThat(this.jobs.size()).isZero());
317     }
318
319     @Test
320     void testReRegister() throws Exception {
321         // Wait foir register types and producer
322         await().untilAsserted(() -> assertThat(icsSimulatorController.testResults.registrationInfo).isNotNull());
323         assertThat(icsSimulatorController.testResults.registrationInfo.supportedTypeIds).hasSize(this.types.size());
324
325         // Clear the registration, should trigger a re-register
326         icsSimulatorController.testResults.reset();
327         await().untilAsserted(() -> assertThat(icsSimulatorController.testResults.registrationInfo).isNotNull());
328         assertThat(icsSimulatorController.testResults.registrationInfo.supportedTypeIds).hasSize(this.types.size());
329
330         // Just clear the registerred types, should trigger a re-register
331         icsSimulatorController.testResults.types.clear();
332         await().untilAsserted(
333                 () -> assertThat(icsSimulatorController.testResults.registrationInfo.supportedTypeIds).hasSize(2));
334     }
335
336     private void testErrorCode(Mono<?> request, HttpStatus expStatus, String responseContains) {
337         testErrorCode(request, expStatus, responseContains, true);
338     }
339
340     private void testErrorCode(Mono<?> request, HttpStatus expStatus, String responseContains,
341             boolean expectApplicationProblemJsonMediaType) {
342         StepVerifier.create(request) //
343                 .expectSubscription() //
344                 .expectErrorMatches(
345                         t -> checkWebClientError(t, expStatus, responseContains, expectApplicationProblemJsonMediaType)) //
346                 .verify();
347     }
348
349     private boolean checkWebClientError(Throwable throwable, HttpStatus expStatus, String responseContains,
350             boolean expectApplicationProblemJsonMediaType) {
351         assertTrue(throwable instanceof WebClientResponseException);
352         WebClientResponseException responseException = (WebClientResponseException) throwable;
353         assertThat(responseException.getStatusCode()).isEqualTo(expStatus);
354         assertThat(responseException.getResponseBodyAsString()).contains(responseContains);
355         if (expectApplicationProblemJsonMediaType) {
356             assertThat(responseException.getHeaders().getContentType()).isEqualTo(MediaType.APPLICATION_PROBLEM_JSON);
357         }
358         return true;
359     }
360 }