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
26 import com.google.gson.JsonParser;
27
28 import java.util.HashMap;
29 import java.util.Map;
30
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;
62
63 import reactor.core.publisher.Flux;
64 import reactor.kafka.sender.KafkaSender;
65 import reactor.kafka.sender.SenderOptions;
66 import reactor.kafka.sender.SenderRecord;
67
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"//
75 })
76 class IntegrationWithKafka {
77
78     @Autowired
79     private ApplicationConfig applicationConfig;
80
81     @Autowired
82     private Jobs jobs;
83
84     @Autowired
85     private InfoTypes types;
86
87     @Autowired
88     private ConsumerController consumerController;
89
90     @Autowired
91     private EcsSimulatorController ecsSimulatorController;
92
93     private com.google.gson.Gson gson = new com.google.gson.GsonBuilder().create();
94
95     private static final Logger logger = LoggerFactory.getLogger(IntegrationWithKafka.class);
96
97     @LocalServerPort
98     int localServerHttpPort;
99
100     static class TestApplicationConfig extends ApplicationConfig {
101         @Override
102         public String getEcsBaseUrl() {
103             return thisProcessUrl();
104         }
105
106         @Override
107         public String getDmaapBaseUrl() {
108             return thisProcessUrl();
109         }
110
111         @Override
112         public String getSelfUrl() {
113             return thisProcessUrl();
114         }
115
116         private String thisProcessUrl() {
117             final String url = "https://localhost:" + getLocalServerHttpPort();
118             return url;
119         }
120     }
121
122     /**
123      * Overrides the BeanFactory.
124      */
125     @TestConfiguration
126     static class TestBeanFactory extends BeanFactory {
127
128         @Override
129         @Bean
130         public ServletWebServerFactory servletContainer() {
131             return new TomcatServletWebServerFactory();
132         }
133
134         @Override
135         @Bean
136         public ApplicationConfig getApplicationConfig() {
137             TestApplicationConfig cfg = new TestApplicationConfig();
138             return cfg;
139         }
140     }
141
142     @AfterEach
143     void reset() {
144         this.consumerController.testResults.reset();
145         this.ecsSimulatorController.testResults.reset();
146         this.jobs.clear();
147     }
148
149     private AsyncRestClient restClient(boolean useTrustValidation) {
150         WebClientConfig config = this.applicationConfig.getWebClientConfig();
151         HttpProxyConfig httpProxyConfig = ImmutableHttpProxyConfig.builder() //
152                 .httpProxyHost("") //
153                 .httpProxyPort(0) //
154                 .build();
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();
164
165         AsyncRestClientFactory restClientFactory = new AsyncRestClientFactory(config);
166         return restClientFactory.createRestClientNoHttpProxy(baseUrl());
167     }
168
169     private AsyncRestClient restClient() {
170         return restClient(false);
171     }
172
173     private String baseUrl() {
174         return "https://localhost:" + this.applicationConfig.getLocalServerHttpPort();
175     }
176
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);
182     }
183
184     private Object jsonObject(String json) {
185         try {
186             return JsonParser.parseString(json).getAsJsonObject();
187         } catch (Exception e) {
188             throw new NullPointerException(e.toString());
189         }
190     }
191
192     private ConsumerJobInfo consumerJobInfo(String filter, int maxTimeMiliseconds, int maxSize) {
193         try {
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",
198                     targetUri, "");
199         } catch (Exception e) {
200             return null;
201         }
202     }
203
204     private SenderOptions<Integer, String> senderOptions() {
205         String bootstrapServers = this.applicationConfig.getKafkaBootStrapServers();
206
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);
214     }
215
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);
219     }
220
221     @Test
222     void kafkaIntegrationTest() throws InterruptedException {
223         final String JOB_ID1 = "ID1";
224         final String JOB_ID2 = "ID2";
225
226         // Register producer, Register types
227         await().untilAsserted(() -> assertThat(ecsSimulatorController.testResults.registrationInfo).isNotNull());
228         assertThat(ecsSimulatorController.testResults.registrationInfo.supportedTypeIds).hasSize(1);
229
230         // Create a job
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));
234
235         final KafkaSender<Integer, String> sender = KafkaSender.create(senderOptions());
236
237         var dataToSend = Flux.range(1, 3).map(i -> senderRecord("Message_", i)); // Message_1, Message_2 etc.
238
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)) //
243                 .blockLast();
244
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]");
249
250         // Delete the job
251         this.ecsSimulatorController.deleteJob(JOB_ID1, restClient());
252         this.ecsSimulatorController.deleteJob(JOB_ID2, restClient());
253
254         await().untilAsserted(() -> assertThat(this.jobs.size()).isZero());
255     }
256
257 }