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.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;
71 import reactor.core.publisher.Flux;
72 import reactor.core.publisher.Mono;
73 import reactor.test.StepVerifier;
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"//
82 class ApplicationTest {
85 private ApplicationConfig applicationConfig;
91 private InfoTypes types;
94 private ConsumerController consumerController;
97 private IcsSimulatorController icsSimulatorController;
100 KafkaTopicConsumers kafkaTopicConsumers;
103 ProducerRegstrationTask producerRegistrationTask;
105 private com.google.gson.Gson gson = new com.google.gson.GsonBuilder().create();
108 int localServerHttpPort;
110 static class TestApplicationConfig extends ApplicationConfig {
112 public String getIcsBaseUrl() {
113 return thisProcessUrl();
117 public String getDmaapBaseUrl() {
118 return thisProcessUrl();
122 public String getSelfUrl() {
123 return thisProcessUrl();
126 private String thisProcessUrl() {
127 final String url = "https://localhost:" + getLocalServerHttpPort();
133 * Overrides the BeanFactory.
136 static class TestBeanFactory extends BeanFactory {
140 public ServletWebServerFactory servletContainer() {
141 return new TomcatServletWebServerFactory();
146 public ApplicationConfig getApplicationConfig() {
147 TestApplicationConfig cfg = new TestApplicationConfig();
154 this.applicationConfig.setLocalServerHttpPort(this.localServerHttpPort);
159 this.consumerController.testResults.reset();
160 this.icsSimulatorController.testResults.reset();
164 private AsyncRestClient restClient(boolean useTrustValidation) {
165 WebClientConfig config = this.applicationConfig.getWebClientConfig();
166 HttpProxyConfig httpProxyConfig = ImmutableHttpProxyConfig.builder() //
167 .httpProxyHost("") //
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();
180 AsyncRestClientFactory restClientFactory = new AsyncRestClientFactory(config);
181 return restClientFactory.createRestClientNoHttpProxy(baseUrl());
184 private AsyncRestClient restClient() {
185 return restClient(false);
188 private String baseUrl() {
189 return "https://localhost:" + this.applicationConfig.getLocalServerHttpPort();
192 private ConsumerJobInfo consumerJobInfo() {
193 return consumerJobInfo("DmaapInformationType", "EI_JOB_ID");
196 private Object jsonObject() {
197 return jsonObject("{}");
200 private Object jsonObject(String json) {
202 return JsonParser.parseString(json).getAsJsonObject();
203 } catch (Exception e) {
204 throw new NullPointerException(e.toString());
208 private ConsumerJobInfo consumerJobInfo(String typeId, String infoJobId) {
210 String targetUri = baseUrl() + ConsumerController.CONSUMER_TARGET_URL;
211 return new ConsumerJobInfo(typeId, jsonObject(), "owner", targetUri, "");
212 } catch (Exception e) {
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();
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"))) {
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);
239 String jobUrl = baseUrl() + ProducerCallbacksController.JOB_URL;
240 resp = restClient().deleteForEntity(jobUrl + "/junk").block();
241 assertThat(resp.getStatusCode()).isEqualTo(HttpStatus.OK);
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");
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());
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, "");
262 this.icsSimulatorController.addJob(kafkaJobInfo, JOB_ID, restClient());
263 await().untilAsserted(() -> assertThat(this.jobs.size()).isEqualTo(1));
265 KafkaJobDataConsumer kafkaConsumer = this.kafkaTopicConsumers.getConsumers().get(TYPE_ID, JOB_ID);
267 // Handle received data from Kafka, check that it has been posted to the
269 kafkaConsumer.start(Flux.just("data"));
271 ConsumerController.TestResults consumer = this.consumerController.testResults;
272 await().untilAsserted(() -> assertThat(consumer.receivedBodies.size()).isEqualTo(1));
273 assertThat(consumer.receivedBodies.get(0)).isEqualTo("[\"data\"]");
275 // Test send an exception
276 kafkaConsumer.start(Flux.error(new NullPointerException()));
278 // Test regular restart of stopped
279 kafkaConsumer.stop();
280 this.kafkaTopicConsumers.restartNonRunningTopics();
281 await().untilAsserted(() -> assertThat(kafkaConsumer.isRunning()).isTrue());
284 this.icsSimulatorController.deleteJob(JOB_ID, restClient());
285 await().untilAsserted(() -> assertThat(this.jobs.size()).isZero());
289 void testReceiveAndPostDataFromDmaap() throws Exception {
290 final String JOB_ID = "ID";
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();
299 this.icsSimulatorController.addJob(consumerJobInfo(), JOB_ID, restClient());
300 await().untilAsserted(() -> assertThat(this.jobs.size()).isEqualTo(1));
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");
310 String jobUrl = baseUrl() + ProducerCallbacksController.JOB_URL;
311 String jobs = restClient().get(jobUrl).block();
312 assertThat(jobs).contains(JOB_ID);
315 this.icsSimulatorController.deleteJob(JOB_ID, restClient());
316 await().untilAsserted(() -> assertThat(this.jobs.size()).isZero());
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());
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());
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));
336 private void testErrorCode(Mono<?> request, HttpStatus expStatus, String responseContains) {
337 testErrorCode(request, expStatus, responseContains, true);
340 private void testErrorCode(Mono<?> request, HttpStatus expStatus, String responseContains,
341 boolean expectApplicationProblemJsonMediaType) {
342 StepVerifier.create(request) //
343 .expectSubscription() //
345 t -> checkWebClientError(t, expStatus, responseContains, expectApplicationProblemJsonMediaType)) //
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);