Reverting to springboot version 2.5.8
[nonrtric.git] / dmaap-adaptor-java / src / test / java / org / oran / dmaapadapter / IntegrationWithIcs.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.Gson;
27 import com.google.gson.GsonBuilder;
28 import com.google.gson.JsonParser;
29
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;
54
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" //
63 })
64 class IntegrationWithIcs {
65
66     private static final String DMAAP_JOB_ID = "DMAAP_JOB_ID";
67     private static final String DMAAP_TYPE_ID = "DmaapInformationType";
68
69     @Autowired
70     private ApplicationConfig applicationConfig;
71
72     @Autowired
73     private ProducerRegstrationTask producerRegstrationTask;
74
75     @Autowired
76     private Jobs jobs;
77
78     @Autowired
79     private InfoTypes types;
80
81     @Autowired
82     private ConsumerController consumerController;
83
84     private static Gson gson = new GsonBuilder().create();
85
86     static class TestApplicationConfig extends ApplicationConfig {
87
88         @Override
89         public String getIcsBaseUrl() {
90             return "https://localhost:8434";
91         }
92
93         @Override
94         public String getDmaapBaseUrl() {
95             return thisProcessUrl();
96         }
97
98         @Override
99         public String getSelfUrl() {
100             return thisProcessUrl();
101         }
102
103         private String thisProcessUrl() {
104             final String url = "https://localhost:" + getLocalServerHttpPort();
105             return url;
106         }
107     }
108
109     /**
110      * Overrides the BeanFactory.
111      */
112     @TestConfiguration
113     static class TestBeanFactory extends BeanFactory {
114
115         @Override
116         @Bean
117         public ServletWebServerFactory servletContainer() {
118             return new TomcatServletWebServerFactory();
119         }
120
121         @Override
122         @Bean
123         public ApplicationConfig getApplicationConfig() {
124             TestApplicationConfig cfg = new TestApplicationConfig();
125             return cfg;
126         }
127     }
128
129     @AfterEach
130     void reset() {
131         this.consumerController.testResults.reset();
132         assertThat(this.jobs.size()).isZero();
133     }
134
135     private AsyncRestClient restClient(boolean useTrustValidation) {
136         WebClientConfig config = this.applicationConfig.getWebClientConfig();
137         HttpProxyConfig httpProxyConfig = ImmutableHttpProxyConfig.builder() //
138                 .httpProxyHost("") //
139                 .httpProxyPort(0) //
140                 .build();
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();
150
151         AsyncRestClientFactory restClientFactory = new AsyncRestClientFactory(config);
152         return restClientFactory.createRestClientNoHttpProxy(selfBaseUrl());
153     }
154
155     private AsyncRestClient restClient() {
156         return restClient(false);
157     }
158
159     private String selfBaseUrl() {
160         return "https://localhost:" + this.applicationConfig.getLocalServerHttpPort();
161     }
162
163     private String icsBaseUrl() {
164         return applicationConfig.getIcsBaseUrl();
165     }
166
167     private String jobUrl(String jobId) {
168         return icsBaseUrl() + "/data-consumer/v1/info-jobs/" + jobId + "?typeCheck=true";
169     }
170
171     private void createInformationJobInIcs(String typeId, String jobId, String filter) {
172         String body = gson.toJson(consumerJobInfo(typeId, filter));
173         try {
174             // Delete the job if it already exists
175             deleteInformationJobInIcs(jobId);
176         } catch (Exception e) {
177         }
178         restClient().putForEntity(jobUrl(jobId), body).block();
179     }
180
181     private void deleteInformationJobInIcs(String jobId) {
182         restClient().delete(jobUrl(jobId)).block();
183     }
184
185     private ConsumerJobInfo consumerJobInfo(String typeId, String filter) {
186         return consumerJobInfo(typeId, DMAAP_JOB_ID, filter);
187     }
188
189     private Object jsonObject(String json) {
190         try {
191             return JsonParser.parseString(json).getAsJsonObject();
192         } catch (Exception e) {
193             throw new NullPointerException(e.toString());
194         }
195     }
196
197     private String quote(String str) {
198         return "\"" + str + "\"";
199     }
200
201     private String consumerUri() {
202         return selfBaseUrl() + ConsumerController.CONSUMER_TARGET_URL;
203     }
204
205     private ConsumerJobInfo consumerJobInfo(String typeId, String infoJobId, String filter) {
206         try {
207
208             String jsonStr = "{ \"filter\" :" + quote(filter) + "}";
209             return new ConsumerJobInfo(typeId, jsonObject(jsonStr), "owner", consumerUri(), "");
210         } catch (Exception e) {
211             return null;
212         }
213     }
214
215     @Test
216     void testCreateKafkaJob() {
217         await().untilAsserted(() -> assertThat(producerRegstrationTask.isRegisteredInIcs()).isTrue());
218         final String TYPE_ID = "KafkaInformationType";
219
220         Job.Parameters param = new Job.Parameters("filter", new Job.BufferTimeout(123, 456), 1);
221
222         ConsumerJobInfo jobInfo =
223                 new ConsumerJobInfo(TYPE_ID, jsonObject(gson.toJson(param)), "owner", consumerUri(), "");
224         String body = gson.toJson(jobInfo);
225
226         restClient().putForEntity(jobUrl("KAFKA_JOB_ID"), body).block();
227
228         await().untilAsserted(() -> assertThat(this.jobs.size()).isEqualTo(1));
229
230         deleteInformationJobInIcs("KAFKA_JOB_ID");
231         await().untilAsserted(() -> assertThat(this.jobs.size()).isZero());
232     }
233
234     @Test
235     void testWholeChain() throws Exception {
236         await().untilAsserted(() -> assertThat(producerRegstrationTask.isRegisteredInIcs()).isTrue());
237
238         createInformationJobInIcs(DMAAP_TYPE_ID, DMAAP_JOB_ID, ".*DmaapResponse.*");
239
240         await().untilAsserted(() -> assertThat(this.jobs.size()).isEqualTo(1));
241
242         DmaapSimulatorController.dmaapResponses.add("DmaapResponse1");
243         DmaapSimulatorController.dmaapResponses.add("DmaapResponse2");
244         DmaapSimulatorController.dmaapResponses.add("Junk");
245
246         ConsumerController.TestResults results = this.consumerController.testResults;
247         await().untilAsserted(() -> assertThat(results.receivedBodies.size()).isEqualTo(2));
248         assertThat(results.receivedBodies.get(0)).isEqualTo("DmaapResponse1");
249
250         deleteInformationJobInIcs(DMAAP_JOB_ID);
251
252         await().untilAsserted(() -> assertThat(this.jobs.size()).isZero());
253
254     }
255
256 }