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