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.JsonParser;
28 import java.util.HashMap;
31 import org.apache.kafka.clients.producer.ProducerConfig;
32 import org.apache.kafka.clients.producer.ProducerRecord;
33 import org.apache.kafka.common.serialization.IntegerSerializer;
34 import org.apache.kafka.common.serialization.StringSerializer;
35 import org.junit.jupiter.api.AfterEach;
36 import org.junit.jupiter.api.Test;
37 import org.junit.jupiter.api.extension.ExtendWith;
38 import org.oran.dmaapadapter.clients.AsyncRestClient;
39 import org.oran.dmaapadapter.clients.AsyncRestClientFactory;
40 import org.oran.dmaapadapter.configuration.ApplicationConfig;
41 import org.oran.dmaapadapter.configuration.ImmutableHttpProxyConfig;
42 import org.oran.dmaapadapter.configuration.ImmutableWebClientConfig;
43 import org.oran.dmaapadapter.configuration.WebClientConfig;
44 import org.oran.dmaapadapter.configuration.WebClientConfig.HttpProxyConfig;
45 import org.oran.dmaapadapter.r1.ConsumerJobInfo;
46 import org.oran.dmaapadapter.repository.InfoType;
47 import org.oran.dmaapadapter.repository.InfoTypes;
48 import org.oran.dmaapadapter.repository.Job;
49 import org.oran.dmaapadapter.repository.Jobs;
50 import org.slf4j.Logger;
51 import org.slf4j.LoggerFactory;
52 import org.springframework.beans.factory.annotation.Autowired;
53 import org.springframework.boot.test.context.SpringBootTest;
54 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
55 import org.springframework.boot.test.context.TestConfiguration;
56 import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
57 import org.springframework.boot.web.server.LocalServerPort;
58 import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
59 import org.springframework.context.annotation.Bean;
60 import org.springframework.test.context.TestPropertySource;
61 import org.springframework.test.context.junit.jupiter.SpringExtension;
63 import reactor.core.publisher.Flux;
64 import reactor.kafka.sender.KafkaSender;
65 import reactor.kafka.sender.SenderOptions;
66 import reactor.kafka.sender.SenderRecord;
68 @SuppressWarnings("java:S3577") // Rename class
69 @ExtendWith(SpringExtension.class)
70 @SpringBootTest(webEnvironment = WebEnvironment.DEFINED_PORT)
71 @TestPropertySource(properties = { //
72 "server.ssl.key-store=./config/keystore.jks", //
73 "app.webclient.trust-store=./config/truststore.jks", //
74 "app.configuration-filepath=./src/test/resources/test_application_configuration_kafka.json"//
76 class IntegrationWithKafka {
79 private ApplicationConfig applicationConfig;
85 private InfoTypes types;
88 private ConsumerController consumerController;
91 private EcsSimulatorController ecsSimulatorController;
93 private com.google.gson.Gson gson = new com.google.gson.GsonBuilder().create();
95 private static final Logger logger = LoggerFactory.getLogger(IntegrationWithKafka.class);
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.consumerController.testResults.reset();
145 this.ecsSimulatorController.testResults.reset();
149 private AsyncRestClient restClient(boolean useTrustValidation) {
150 WebClientConfig config = this.applicationConfig.getWebClientConfig();
151 HttpProxyConfig httpProxyConfig = ImmutableHttpProxyConfig.builder() //
152 .httpProxyHost("") //
155 config = ImmutableWebClientConfig.builder() //
156 .keyStoreType(config.keyStoreType()) //
157 .keyStorePassword(config.keyStorePassword()) //
158 .keyStore(config.keyStore()) //
159 .keyPassword(config.keyPassword()) //
160 .isTrustStoreUsed(useTrustValidation) //
161 .trustStore(config.trustStore()) //
162 .trustStorePassword(config.trustStorePassword()) //
163 .httpProxyConfig(httpProxyConfig).build();
165 AsyncRestClientFactory restClientFactory = new AsyncRestClientFactory(config);
166 return restClientFactory.createRestClientNoHttpProxy(baseUrl());
169 private AsyncRestClient restClient() {
170 return restClient(false);
173 private String baseUrl() {
174 return "https://localhost:" + this.applicationConfig.getLocalServerHttpPort();
177 private Object jobParametersAsJsonObject(String filter, int maxTimeMiliseconds, int maxSize) {
178 Job.Parameters param = new Job.Parameters(filter,
179 new Job.Parameters.BufferTimeout(maxSize, maxTimeMiliseconds));
180 String str = gson.toJson(param);
181 return jsonObject(str);
184 private Object jsonObject(String json) {
186 return JsonParser.parseString(json).getAsJsonObject();
187 } catch (Exception e) {
188 throw new NullPointerException(e.toString());
192 private ConsumerJobInfo consumerJobInfo(String filter, int maxTimeMiliseconds, int maxSize) {
194 InfoType type = this.types.getAll().iterator().next();
195 String typeId = type.getId();
196 String targetUri = baseUrl() + ConsumerController.CONSUMER_TARGET_URL;
197 return new ConsumerJobInfo(typeId, jobParametersAsJsonObject(filter, maxTimeMiliseconds, maxSize), "owner",
199 } catch (Exception e) {
204 private SenderOptions<Integer, String> senderOptions() {
205 String bootstrapServers = this.applicationConfig.getKafkaBootStrapServers();
207 Map<String, Object> props = new HashMap<>();
208 props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers);
209 props.put(ProducerConfig.CLIENT_ID_CONFIG, "sample-producer");
210 props.put(ProducerConfig.ACKS_CONFIG, "all");
211 props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, IntegerSerializer.class);
212 props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
213 return SenderOptions.create(props);
216 private SenderRecord<Integer, String, Integer> senderRecord(String data, int i) {
217 final InfoType infoType = this.types.getAll().iterator().next();
218 return SenderRecord.create(new ProducerRecord<>(infoType.getKafkaInputTopic(), i, data + i), i);
222 void kafkaIntegrationTest() throws InterruptedException {
223 final String JOB_ID1 = "ID1";
224 final String JOB_ID2 = "ID2";
226 // Register producer, Register types
227 await().untilAsserted(() -> assertThat(ecsSimulatorController.testResults.registrationInfo).isNotNull());
228 assertThat(ecsSimulatorController.testResults.registrationInfo.supportedTypeIds).hasSize(1);
231 this.ecsSimulatorController.addJob(consumerJobInfo(".*", 10, 1000), JOB_ID1, restClient());
232 this.ecsSimulatorController.addJob(consumerJobInfo(".*Message_1.*", 0, 0), JOB_ID2, restClient());
233 await().untilAsserted(() -> assertThat(this.jobs.size()).isEqualTo(2));
235 final KafkaSender<Integer, String> sender = KafkaSender.create(senderOptions());
237 var dataToSend = Flux.range(1, 3).map(i -> senderRecord("Message_", i)); // Message_1, Message_2 etc.
239 sender.send(dataToSend) //
240 .doOnError(e -> logger.error("Send failed", e)) //
241 .doOnNext(senderResult -> logger.debug("Sent {}", senderResult)) //
242 .doOnError(t -> logger.error("Error {}", t)) //
245 ConsumerController.TestResults consumer = this.consumerController.testResults;
246 await().untilAsserted(() -> assertThat(consumer.receivedBodies.size()).isEqualTo(2));
247 assertThat(consumer.receivedBodies.get(0)).isEqualTo("Message_1");
248 assertThat(consumer.receivedBodies.get(1)).isEqualTo("[Message_1, Message_2, Message_3]");
251 this.ecsSimulatorController.deleteJob(JOB_ID1, restClient());
252 this.ecsSimulatorController.deleteJob(JOB_ID2, restClient());
254 await().untilAsserted(() -> assertThat(this.jobs.size()).isZero());