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.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;
68 import reactor.core.publisher.Mono;
69 import reactor.test.StepVerifier;
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"//
78 class ApplicationTest {
81 private ApplicationConfig applicationConfig;
87 private InfoTypes types;
90 private ConsumerController consumerController;
93 private EcsSimulatorController ecsSimulatorController;
95 private com.google.gson.Gson gson = new com.google.gson.GsonBuilder().create();
98 int localServerHttpPort;
100 static class TestApplicationConfig extends ApplicationConfig {
102 public String getEcsBaseUrl() {
103 return thisProcessUrl();
107 public String getDmaapBaseUrl() {
108 return thisProcessUrl();
112 public String getSelfUrl() {
113 return thisProcessUrl();
116 private String thisProcessUrl() {
117 final String url = "https://localhost:" + getLocalServerHttpPort();
123 * Overrides the BeanFactory.
126 static class TestBeanFactory extends BeanFactory {
130 public ServletWebServerFactory servletContainer() {
131 return new TomcatServletWebServerFactory();
136 public ApplicationConfig getApplicationConfig() {
137 TestApplicationConfig cfg = new TestApplicationConfig();
144 this.applicationConfig.setLocalServerHttpPort(this.localServerHttpPort);
149 this.consumerController.testResults.reset();
150 this.ecsSimulatorController.testResults.reset();
154 private AsyncRestClient restClient(boolean useTrustValidation) {
155 WebClientConfig config = this.applicationConfig.getWebClientConfig();
156 HttpProxyConfig httpProxyConfig = ImmutableHttpProxyConfig.builder() //
157 .httpProxyHost("") //
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();
170 AsyncRestClientFactory restClientFactory = new AsyncRestClientFactory(config);
171 return restClientFactory.createRestClientNoHttpProxy(baseUrl());
174 private AsyncRestClient restClient() {
175 return restClient(false);
178 private String baseUrl() {
179 return "https://localhost:" + this.applicationConfig.getLocalServerHttpPort();
182 private ConsumerJobInfo consumerJobInfo() {
183 return consumerJobInfo("DmaapInformationType", "EI_JOB_ID");
186 private Object jsonObject() {
187 return jsonObject("{}");
190 private Object jsonObject(String json) {
192 return JsonParser.parseString(json).getAsJsonObject();
193 } catch (Exception e) {
194 throw new NullPointerException(e.toString());
198 private ConsumerJobInfo consumerJobInfo(String typeId, String infoJobId) {
200 String targetUri = baseUrl() + ConsumerController.CONSUMER_TARGET_URL;
201 return new ConsumerJobInfo(typeId, jsonObject(), "owner", targetUri, "");
202 } catch (Exception e) {
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();
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"))) {
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);
229 String jobUrl = baseUrl() + ProducerCallbacksController.JOB_URL;
230 resp = restClient().deleteForEntity(jobUrl + "/junk").block();
231 assertThat(resp.getStatusCode()).isEqualTo(HttpStatus.OK);
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");
240 void testWholeChain() throws Exception {
241 final String JOB_ID = "ID";
243 // Register producer, Register types
244 await().untilAsserted(() -> assertThat(ecsSimulatorController.testResults.registrationInfo).isNotNull());
245 assertThat(ecsSimulatorController.testResults.registrationInfo.supportedTypeIds).hasSize(this.types.size());
248 this.ecsSimulatorController.addJob(consumerJobInfo(), JOB_ID, restClient());
249 await().untilAsserted(() -> assertThat(this.jobs.size()).isEqualTo(1));
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");
259 String jobUrl = baseUrl() + ProducerCallbacksController.JOB_URL;
260 String jobs = restClient().get(jobUrl).block();
261 assertThat(jobs).contains(JOB_ID);
264 this.ecsSimulatorController.deleteJob(JOB_ID, restClient());
265 await().untilAsserted(() -> assertThat(this.jobs.size()).isZero());
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());
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());
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));
286 void testCreateKafkaJob() {
287 await().untilAsserted(() -> assertThat(ecsSimulatorController.testResults.registrationInfo).isNotNull());
288 assertThat(ecsSimulatorController.testResults.registrationInfo.supportedTypeIds).hasSize(this.types.size());
290 final String TYPE_ID = "KafkaInformationType";
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, "");
297 this.ecsSimulatorController.addJob(jobInfo, "JOB_ID", restClient());
298 await().untilAsserted(() -> assertThat(this.jobs.size()).isEqualTo(1));
301 this.ecsSimulatorController.deleteJob("JOB_ID", restClient());
302 await().untilAsserted(() -> assertThat(this.jobs.size()).isZero());
305 private void testErrorCode(Mono<?> request, HttpStatus expStatus, String responseContains) {
306 testErrorCode(request, expStatus, responseContains, true);
309 private void testErrorCode(Mono<?> request, HttpStatus expStatus, String responseContains,
310 boolean expectApplicationProblemJsonMediaType) {
311 StepVerifier.create(request) //
312 .expectSubscription() //
314 t -> checkWebClientError(t, expStatus, responseContains, expectApplicationProblemJsonMediaType)) //
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);