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;
26 import com.google.gson.Gson;
27 import com.google.gson.GsonBuilder;
28 import com.google.gson.JsonParser;
30 import org.junit.jupiter.api.AfterEach;
31 import org.junit.jupiter.api.Test;
32 import org.junit.jupiter.api.extension.ExtendWith;
33 import org.oran.dmaapadapter.clients.AsyncRestClient;
34 import org.oran.dmaapadapter.clients.AsyncRestClientFactory;
35 import org.oran.dmaapadapter.configuration.ApplicationConfig;
36 import org.oran.dmaapadapter.configuration.ImmutableHttpProxyConfig;
37 import org.oran.dmaapadapter.configuration.ImmutableWebClientConfig;
38 import org.oran.dmaapadapter.configuration.WebClientConfig;
39 import org.oran.dmaapadapter.configuration.WebClientConfig.HttpProxyConfig;
40 import org.oran.dmaapadapter.r1.ConsumerJobInfo;
41 import org.oran.dmaapadapter.repository.InfoTypes;
42 import org.oran.dmaapadapter.repository.Job;
43 import org.oran.dmaapadapter.repository.Jobs;
44 import org.oran.dmaapadapter.tasks.ProducerRegstrationTask;
45 import org.springframework.beans.factory.annotation.Autowired;
46 import org.springframework.boot.test.context.SpringBootTest;
47 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
48 import org.springframework.boot.test.context.TestConfiguration;
49 import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
50 import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
51 import org.springframework.context.annotation.Bean;
52 import org.springframework.test.context.TestPropertySource;
53 import org.springframework.test.context.junit.jupiter.SpringExtension;
55 @SuppressWarnings("java:S3577") // Rename class
56 @ExtendWith(SpringExtension.class)
57 @SpringBootTest(webEnvironment = WebEnvironment.DEFINED_PORT)
58 @TestPropertySource(properties = { //
59 "server.ssl.key-store=./config/keystore.jks", //
60 "app.webclient.trust-store=./config/truststore.jks", //
61 "app.configuration-filepath=./src/test/resources/test_application_configuration.json", //
62 "app.ics-base-url=https://localhost:8434" //
64 class IntegrationWithIcs {
66 private static final String DMAAP_JOB_ID = "DMAAP_JOB_ID";
67 private static final String DMAAP_TYPE_ID = "DmaapInformationType";
70 private ApplicationConfig applicationConfig;
73 private ProducerRegstrationTask producerRegstrationTask;
79 private InfoTypes types;
82 private ConsumerController consumerController;
84 private static Gson gson = new GsonBuilder().create();
86 static class TestApplicationConfig extends ApplicationConfig {
89 public String getIcsBaseUrl() {
90 return "https://localhost:8434";
94 public String getDmaapBaseUrl() {
95 return thisProcessUrl();
99 public String getSelfUrl() {
100 return thisProcessUrl();
103 private String thisProcessUrl() {
104 final String url = "https://localhost:" + getLocalServerHttpPort();
110 * Overrides the BeanFactory.
113 static class TestBeanFactory extends BeanFactory {
117 public ServletWebServerFactory servletContainer() {
118 return new TomcatServletWebServerFactory();
123 public ApplicationConfig getApplicationConfig() {
124 TestApplicationConfig cfg = new TestApplicationConfig();
131 this.consumerController.testResults.reset();
132 assertThat(this.jobs.size()).isZero();
135 private AsyncRestClient restClient(boolean useTrustValidation) {
136 WebClientConfig config = this.applicationConfig.getWebClientConfig();
137 HttpProxyConfig httpProxyConfig = ImmutableHttpProxyConfig.builder() //
138 .httpProxyHost("") //
141 config = ImmutableWebClientConfig.builder() //
142 .keyStoreType(config.keyStoreType()) //
143 .keyStorePassword(config.keyStorePassword()) //
144 .keyStore(config.keyStore()) //
145 .keyPassword(config.keyPassword()) //
146 .isTrustStoreUsed(useTrustValidation) //
147 .trustStore(config.trustStore()) //
148 .trustStorePassword(config.trustStorePassword()) //
149 .httpProxyConfig(httpProxyConfig).build();
151 AsyncRestClientFactory restClientFactory = new AsyncRestClientFactory(config);
152 return restClientFactory.createRestClientNoHttpProxy(selfBaseUrl());
155 private AsyncRestClient restClient() {
156 return restClient(false);
159 private String selfBaseUrl() {
160 return "https://localhost:" + this.applicationConfig.getLocalServerHttpPort();
163 private String icsBaseUrl() {
164 return applicationConfig.getIcsBaseUrl();
167 private String jobUrl(String jobId) {
168 return icsBaseUrl() + "/data-consumer/v1/info-jobs/" + jobId + "?typeCheck=true";
171 private void createInformationJobInIcs(String typeId, String jobId, String filter) {
172 String body = gson.toJson(consumerJobInfo(typeId, filter));
174 // Delete the job if it already exists
175 deleteInformationJobInIcs(jobId);
176 } catch (Exception e) {
178 restClient().putForEntity(jobUrl(jobId), body).block();
181 private void deleteInformationJobInIcs(String jobId) {
182 restClient().delete(jobUrl(jobId)).block();
185 private ConsumerJobInfo consumerJobInfo(String typeId, String filter) {
186 return consumerJobInfo(typeId, DMAAP_JOB_ID, filter);
189 private Object jsonObject(String json) {
191 return JsonParser.parseString(json).getAsJsonObject();
192 } catch (Exception e) {
193 throw new NullPointerException(e.toString());
197 private String quote(String str) {
198 return "\"" + str + "\"";
201 private String consumerUri() {
202 return selfBaseUrl() + ConsumerController.CONSUMER_TARGET_URL;
205 private ConsumerJobInfo consumerJobInfo(String typeId, String infoJobId, String filter) {
208 String jsonStr = "{ \"filter\" :" + quote(filter) + "}";
209 return new ConsumerJobInfo(typeId, jsonObject(jsonStr), "owner", consumerUri(), "");
210 } catch (Exception e) {
216 void testCreateKafkaJob() {
217 await().untilAsserted(() -> assertThat(producerRegstrationTask.isRegisteredInIcs()).isTrue());
218 final String TYPE_ID = "KafkaInformationType";
220 Job.Parameters param = new Job.Parameters("filter", new Job.BufferTimeout(123, 456), 1);
222 ConsumerJobInfo jobInfo =
223 new ConsumerJobInfo(TYPE_ID, jsonObject(gson.toJson(param)), "owner", consumerUri(), "");
224 String body = gson.toJson(jobInfo);
226 restClient().putForEntity(jobUrl("KAFKA_JOB_ID"), body).block();
228 await().untilAsserted(() -> assertThat(this.jobs.size()).isEqualTo(1));
230 deleteInformationJobInIcs("KAFKA_JOB_ID");
231 await().untilAsserted(() -> assertThat(this.jobs.size()).isZero());
235 void testWholeChain() throws Exception {
236 await().untilAsserted(() -> assertThat(producerRegstrationTask.isRegisteredInIcs()).isTrue());
238 createInformationJobInIcs(DMAAP_TYPE_ID, DMAAP_JOB_ID, ".*DmaapResponse.*");
240 await().untilAsserted(() -> assertThat(this.jobs.size()).isEqualTo(1));
242 DmaapSimulatorController.dmaapResponses.add("DmaapResponse1");
243 DmaapSimulatorController.dmaapResponses.add("DmaapResponse2");
244 DmaapSimulatorController.dmaapResponses.add("Junk");
246 ConsumerController.TestResults results = this.consumerController.testResults;
247 await().untilAsserted(() -> assertThat(results.receivedBodies.size()).isEqualTo(2));
248 assertThat(results.receivedBodies.get(0)).isEqualTo("DmaapResponse1");
250 deleteInformationJobInIcs(DMAAP_JOB_ID);
252 await().untilAsserted(() -> assertThat(this.jobs.size()).isZero());