NONRTRIC - Implement DMaaP mediator producer service in Java
[nonrtric.git] / dmaap-adaptor-java / src / test / java / org / oran / dmaapadapter / IntegrationWithKafka.java
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2021 Nordix Foundation
6  * %%
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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===================================
19  */
20
21 package org.oran.dmaapadapter;
22
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;
26
27 import com.google.gson.JsonParser;
28
29 import java.time.Duration;
30 import java.util.HashMap;
31 import java.util.Map;
32
33 import org.apache.kafka.clients.producer.ProducerConfig;
34 import org.apache.kafka.clients.producer.ProducerRecord;
35 import org.apache.kafka.common.serialization.IntegerSerializer;
36 import org.apache.kafka.common.serialization.StringSerializer;
37 import org.junit.jupiter.api.AfterEach;
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.r1.ConsumerJobInfo;
48 import org.oran.dmaapadapter.repository.InfoType;
49 import org.oran.dmaapadapter.repository.InfoTypes;
50 import org.oran.dmaapadapter.repository.Job;
51 import org.oran.dmaapadapter.repository.Jobs;
52 import org.oran.dmaapadapter.tasks.KafkaJobDataConsumer;
53 import org.oran.dmaapadapter.tasks.KafkaTopicConsumers;
54 import org.slf4j.Logger;
55 import org.slf4j.LoggerFactory;
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.test.context.TestPropertySource;
65 import org.springframework.test.context.junit.jupiter.SpringExtension;
66
67 import reactor.core.publisher.Flux;
68 import reactor.kafka.sender.KafkaSender;
69 import reactor.kafka.sender.SenderOptions;
70 import reactor.kafka.sender.SenderRecord;
71
72 @SuppressWarnings("java:S3577") // Rename class
73 @ExtendWith(SpringExtension.class)
74 @SpringBootTest(webEnvironment = WebEnvironment.DEFINED_PORT)
75 @TestPropertySource(properties = { //
76         "server.ssl.key-store=./config/keystore.jks", //
77         "app.webclient.trust-store=./config/truststore.jks", //
78         "app.configuration-filepath=./src/test/resources/test_application_configuration_kafka.json"//
79 })
80 class IntegrationWithKafka {
81
82     @Autowired
83     private ApplicationConfig applicationConfig;
84
85     @Autowired
86     private Jobs jobs;
87
88     @Autowired
89     private InfoTypes types;
90
91     @Autowired
92     private ConsumerController consumerController;
93
94     @Autowired
95     private EcsSimulatorController ecsSimulatorController;
96
97     @Autowired
98     private KafkaTopicConsumers kafkaTopicConsumers;
99
100     private com.google.gson.Gson gson = new com.google.gson.GsonBuilder().create();
101
102     private static final Logger logger = LoggerFactory.getLogger(IntegrationWithKafka.class);
103
104     @LocalServerPort
105     int localServerHttpPort;
106
107     static class TestApplicationConfig extends ApplicationConfig {
108         @Override
109         public String getEcsBaseUrl() {
110             return thisProcessUrl();
111         }
112
113         @Override
114         public String getDmaapBaseUrl() {
115             return thisProcessUrl();
116         }
117
118         @Override
119         public String getSelfUrl() {
120             return thisProcessUrl();
121         }
122
123         private String thisProcessUrl() {
124             final String url = "https://localhost:" + getLocalServerHttpPort();
125             return url;
126         }
127     }
128
129     /**
130      * Overrides the BeanFactory.
131      */
132     @TestConfiguration
133     static class TestBeanFactory extends BeanFactory {
134
135         @Override
136         @Bean
137         public ServletWebServerFactory servletContainer() {
138             return new TomcatServletWebServerFactory();
139         }
140
141         @Override
142         @Bean
143         public ApplicationConfig getApplicationConfig() {
144             TestApplicationConfig cfg = new TestApplicationConfig();
145             return cfg;
146         }
147     }
148
149     @AfterEach
150     void reset() {
151         this.consumerController.testResults.reset();
152         this.ecsSimulatorController.testResults.reset();
153         this.jobs.clear();
154     }
155
156     private AsyncRestClient restClient(boolean useTrustValidation) {
157         WebClientConfig config = this.applicationConfig.getWebClientConfig();
158         HttpProxyConfig httpProxyConfig = ImmutableHttpProxyConfig.builder() //
159                 .httpProxyHost("") //
160                 .httpProxyPort(0) //
161                 .build();
162         config = ImmutableWebClientConfig.builder() //
163                 .keyStoreType(config.keyStoreType()) //
164                 .keyStorePassword(config.keyStorePassword()) //
165                 .keyStore(config.keyStore()) //
166                 .keyPassword(config.keyPassword()) //
167                 .isTrustStoreUsed(useTrustValidation) //
168                 .trustStore(config.trustStore()) //
169                 .trustStorePassword(config.trustStorePassword()) //
170                 .httpProxyConfig(httpProxyConfig).build();
171
172         AsyncRestClientFactory restClientFactory = new AsyncRestClientFactory(config);
173         return restClientFactory.createRestClientNoHttpProxy(baseUrl());
174     }
175
176     private AsyncRestClient restClient() {
177         return restClient(false);
178     }
179
180     private String baseUrl() {
181         return "https://localhost:" + this.applicationConfig.getLocalServerHttpPort();
182     }
183
184     private Object jobParametersAsJsonObject(String filter, long maxTimeMiliseconds, int maxSize, int maxConcurrency) {
185         Job.Parameters param =
186                 new Job.Parameters(filter, new Job.BufferTimeout(maxSize, maxTimeMiliseconds), maxConcurrency);
187         String str = gson.toJson(param);
188         return jsonObject(str);
189     }
190
191     private Object jsonObject(String json) {
192         try {
193             return JsonParser.parseString(json).getAsJsonObject();
194         } catch (Exception e) {
195             throw new NullPointerException(e.toString());
196         }
197     }
198
199     private ConsumerJobInfo consumerJobInfo(String filter, Duration maxTime, int maxSize, int maxConcurrency) {
200         try {
201             InfoType type = this.types.getAll().iterator().next();
202             String typeId = type.getId();
203             String targetUri = baseUrl() + ConsumerController.CONSUMER_TARGET_URL;
204             return new ConsumerJobInfo(typeId,
205                     jobParametersAsJsonObject(filter, maxTime.toMillis(), maxSize, maxConcurrency), "owner", targetUri,
206                     "");
207         } catch (Exception e) {
208             return null;
209         }
210     }
211
212     private SenderOptions<Integer, String> senderOptions() {
213         String bootstrapServers = this.applicationConfig.getKafkaBootStrapServers();
214
215         Map<String, Object> props = new HashMap<>();
216         props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers);
217         props.put(ProducerConfig.CLIENT_ID_CONFIG, "sample-producer");
218         props.put(ProducerConfig.ACKS_CONFIG, "all");
219         props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, IntegerSerializer.class);
220         props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
221         return SenderOptions.create(props);
222     }
223
224     private SenderRecord<Integer, String, Integer> senderRecord(String data, int i) {
225         final InfoType infoType = this.types.getAll().iterator().next();
226         return SenderRecord.create(new ProducerRecord<>(infoType.getKafkaInputTopic(), i, data + i), i);
227     }
228
229     private void sendDataToStream(Flux<SenderRecord<Integer, String, Integer>> dataToSend) {
230         final KafkaSender<Integer, String> sender = KafkaSender.create(senderOptions());
231
232         sender.send(dataToSend) //
233                 .doOnError(e -> logger.error("Send failed", e)) //
234                 .blockLast();
235
236     }
237
238     private void verifiedReceivedByConsumer(String... strings) {
239         ConsumerController.TestResults consumer = this.consumerController.testResults;
240         await().untilAsserted(() -> assertThat(consumer.receivedBodies.size()).isEqualTo(strings.length));
241         for (String s : strings) {
242             assertTrue(consumer.hasReceived(s));
243         }
244     }
245
246     @Test
247     void kafkaIntegrationTest() throws InterruptedException {
248         final String JOB_ID1 = "ID1";
249         final String JOB_ID2 = "ID2";
250
251         // Register producer, Register types
252         await().untilAsserted(() -> assertThat(ecsSimulatorController.testResults.registrationInfo).isNotNull());
253         assertThat(ecsSimulatorController.testResults.registrationInfo.supportedTypeIds).hasSize(1);
254
255         // Create two jobs. One buffering and one with a filter
256         this.ecsSimulatorController.addJob(consumerJobInfo(null, Duration.ofMillis(400), 1000, 20), JOB_ID1,
257                 restClient());
258         this.ecsSimulatorController.addJob(consumerJobInfo("^Message_1$", Duration.ZERO, 0, 1), JOB_ID2, restClient());
259
260         await().untilAsserted(() -> assertThat(this.jobs.size()).isEqualTo(2));
261
262         var dataToSend = Flux.range(1, 3).map(i -> senderRecord("Message_", i)); // Message_1, Message_2 etc.
263         sendDataToStream(dataToSend);
264
265         verifiedReceivedByConsumer("Message_1", "[Message_1, Message_2, Message_3]");
266
267         // Delete the jobs
268         this.ecsSimulatorController.deleteJob(JOB_ID1, restClient());
269         this.ecsSimulatorController.deleteJob(JOB_ID2, restClient());
270
271         await().untilAsserted(() -> assertThat(this.jobs.size()).isZero());
272         await().untilAsserted(() -> assertThat(this.kafkaTopicConsumers.getActiveSubscriptions()).isEmpty());
273     }
274
275     @Test
276     void kafkaIOverflow() throws InterruptedException {
277         // This does not work. After an overflow, the kafka stream does not seem to work
278         //
279         final String JOB_ID1 = "ID1";
280         final String JOB_ID2 = "ID2";
281
282         // Register producer, Register types
283         await().untilAsserted(() -> assertThat(ecsSimulatorController.testResults.registrationInfo).isNotNull());
284         assertThat(ecsSimulatorController.testResults.registrationInfo.supportedTypeIds).hasSize(1);
285
286         // Create two jobs.
287         this.ecsSimulatorController.addJob(consumerJobInfo(null, Duration.ZERO, 0, 1), JOB_ID1, restClient());
288         this.ecsSimulatorController.addJob(consumerJobInfo(null, Duration.ZERO, 0, 1), JOB_ID2, restClient());
289
290         await().untilAsserted(() -> assertThat(this.jobs.size()).isEqualTo(2));
291
292         var dataToSend = Flux.range(1, 1000000).map(i -> senderRecord("Message_", i)); // Message_1, Message_2 etc.
293         sendDataToStream(dataToSend); // this will overflow
294
295         KafkaJobDataConsumer consumer = kafkaTopicConsumers.getActiveSubscriptions().values().iterator().next();
296         await().untilAsserted(() -> assertThat(consumer.isRunning()).isFalse());
297         this.consumerController.testResults.reset();
298
299         kafkaTopicConsumers.restartNonRunningTasks();
300
301         dataToSend = Flux.range(1, 3).map(i -> senderRecord("Message__", i)); // Message_1
302         sendDataToStream(dataToSend);
303
304         verifiedReceivedByConsumer("Message__1", "Message__1");
305     }
306
307 }