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