2 * ========================LICENSE_START=================================
5 * Copyright (C) 2021 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.dmaapadapter;
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;
27 import com.google.gson.JsonParser;
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;
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;
70 import reactor.core.publisher.Flux;
71 import reactor.core.publisher.Mono;
72 import reactor.test.StepVerifier;
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"//
81 class ApplicationTest {
84 private ApplicationConfig applicationConfig;
90 private InfoTypes types;
93 private ConsumerController consumerController;
96 private IcsSimulatorController icsSimulatorController;
99 KafkaTopicConsumers kafkaTopicConsumers;
101 private com.google.gson.Gson gson = new com.google.gson.GsonBuilder().create();
104 int localServerHttpPort;
106 static class TestApplicationConfig extends ApplicationConfig {
108 public String getIcsBaseUrl() {
109 return thisProcessUrl();
113 public String getDmaapBaseUrl() {
114 return thisProcessUrl();
118 public String getSelfUrl() {
119 return thisProcessUrl();
122 private String thisProcessUrl() {
123 final String url = "https://localhost:" + getLocalServerHttpPort();
129 * Overrides the BeanFactory.
132 static class TestBeanFactory extends BeanFactory {
136 public ServletWebServerFactory servletContainer() {
137 return new TomcatServletWebServerFactory();
142 public ApplicationConfig getApplicationConfig() {
143 TestApplicationConfig cfg = new TestApplicationConfig();
150 this.applicationConfig.setLocalServerHttpPort(this.localServerHttpPort);
155 this.consumerController.testResults.reset();
156 this.icsSimulatorController.testResults.reset();
160 private AsyncRestClient restClient(boolean useTrustValidation) {
161 WebClientConfig config = this.applicationConfig.getWebClientConfig();
162 HttpProxyConfig httpProxyConfig = ImmutableHttpProxyConfig.builder() //
163 .httpProxyHost("") //
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();
176 AsyncRestClientFactory restClientFactory = new AsyncRestClientFactory(config);
177 return restClientFactory.createRestClientNoHttpProxy(baseUrl());
180 private AsyncRestClient restClient() {
181 return restClient(false);
184 private String baseUrl() {
185 return "https://localhost:" + this.applicationConfig.getLocalServerHttpPort();
188 private ConsumerJobInfo consumerJobInfo() {
189 return consumerJobInfo("DmaapInformationType", "EI_JOB_ID");
192 private Object jsonObject() {
193 return jsonObject("{}");
196 private Object jsonObject(String json) {
198 return JsonParser.parseString(json).getAsJsonObject();
199 } catch (Exception e) {
200 throw new NullPointerException(e.toString());
204 private ConsumerJobInfo consumerJobInfo(String typeId, String infoJobId) {
206 String targetUri = baseUrl() + ConsumerController.CONSUMER_TARGET_URL;
207 return new ConsumerJobInfo(typeId, jsonObject(), "owner", targetUri, "");
208 } catch (Exception e) {
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();
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"))) {
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);
235 String jobUrl = baseUrl() + ProducerCallbacksController.JOB_URL;
236 resp = restClient().deleteForEntity(jobUrl + "/junk").block();
237 assertThat(resp.getStatusCode()).isEqualTo(HttpStatus.OK);
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");
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());
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, "");
258 this.icsSimulatorController.addJob(kafkaJobInfo, JOB_ID, restClient());
259 await().untilAsserted(() -> assertThat(this.jobs.size()).isEqualTo(1));
261 KafkaJobDataConsumer kafkaConsumer = this.kafkaTopicConsumers.getConsumers().get(TYPE_ID, JOB_ID);
263 // Handle received data from Kafka, check that it has been posted to the
265 kafkaConsumer.start(Flux.just("data"));
267 ConsumerController.TestResults consumer = this.consumerController.testResults;
268 await().untilAsserted(() -> assertThat(consumer.receivedBodies.size()).isEqualTo(1));
269 assertThat(consumer.receivedBodies.get(0)).isEqualTo("[\"data\"]");
271 // Test send an exception
272 kafkaConsumer.start(Flux.error(new NullPointerException()));
274 // Test regular restart of stopped
275 kafkaConsumer.stop();
276 this.kafkaTopicConsumers.restartNonRunningTopics();
277 await().untilAsserted(() -> assertThat(kafkaConsumer.isRunning()).isTrue());
280 this.icsSimulatorController.deleteJob(JOB_ID, restClient());
281 await().untilAsserted(() -> assertThat(this.jobs.size()).isZero());
285 void testReceiveAndPostDataFromDmaap() throws Exception {
286 final String JOB_ID = "ID";
288 // Register producer, Register types
289 await().untilAsserted(() -> assertThat(icsSimulatorController.testResults.registrationInfo).isNotNull());
290 assertThat(icsSimulatorController.testResults.registrationInfo.supportedTypeIds).hasSize(this.types.size());
293 this.icsSimulatorController.addJob(consumerJobInfo(), JOB_ID, restClient());
294 await().untilAsserted(() -> assertThat(this.jobs.size()).isEqualTo(1));
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");
304 String jobUrl = baseUrl() + ProducerCallbacksController.JOB_URL;
305 String jobs = restClient().get(jobUrl).block();
306 assertThat(jobs).contains(JOB_ID);
309 this.icsSimulatorController.deleteJob(JOB_ID, restClient());
310 await().untilAsserted(() -> assertThat(this.jobs.size()).isZero());
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());
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());
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));
330 private void testErrorCode(Mono<?> request, HttpStatus expStatus, String responseContains) {
331 testErrorCode(request, expStatus, responseContains, true);
334 private void testErrorCode(Mono<?> request, HttpStatus expStatus, String responseContains,
335 boolean expectApplicationProblemJsonMediaType) {
336 StepVerifier.create(request) //
337 .expectSubscription() //
339 t -> checkWebClientError(t, expStatus, responseContains, expectApplicationProblemJsonMediaType)) //
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);