544600438a2ead6bee392ff984c1806622fd24d7
[nonrtric.git] / dmaap-adaptor-java / src / test / java / org / oran / dmaapadapter / IntegrationWithEcs.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.InfoType;
42 import org.oran.dmaapadapter.repository.InfoTypes;
43 import org.oran.dmaapadapter.repository.Jobs;
44 import org.oran.dmaapadapter.tasks.ProducerRegstrationTask;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
47 import org.springframework.beans.factory.annotation.Autowired;
48 import org.springframework.boot.test.context.SpringBootTest;
49 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
50 import org.springframework.boot.test.context.TestConfiguration;
51 import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
52 import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
53 import org.springframework.context.annotation.Bean;
54 import org.springframework.test.context.TestPropertySource;
55 import org.springframework.test.context.junit.jupiter.SpringExtension;
56
57 @ExtendWith(SpringExtension.class)
58 @SpringBootTest(webEnvironment = WebEnvironment.DEFINED_PORT)
59 @TestPropertySource(properties = { //
60         "server.ssl.key-store=./config/keystore.jks", //
61         "app.webclient.trust-store=./config/truststore.jks", //
62         "app.vardata-directory=./target", //
63         "app.configuration-filepath=./src/test/resources/test_application_configuration.json", //
64         "app.ecs-base-url=https://localhost:8434" //
65 })
66 class IntegrationWithEcs {
67     private static final Logger logger = LoggerFactory.getLogger(ApplicationTest.class);
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 getEcsBaseUrl() {
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         this.jobs.clear();
133         this.types.clear();
134     }
135
136     private AsyncRestClient restClient(boolean useTrustValidation) {
137         WebClientConfig config = this.applicationConfig.getWebClientConfig();
138         HttpProxyConfig httpProxyConfig = ImmutableHttpProxyConfig.builder() //
139                 .httpProxyHost("") //
140                 .httpProxyPort(0) //
141                 .build();
142         config = ImmutableWebClientConfig.builder() //
143                 .keyStoreType(config.keyStoreType()) //
144                 .keyStorePassword(config.keyStorePassword()) //
145                 .keyStore(config.keyStore()) //
146                 .keyPassword(config.keyPassword()) //
147                 .isTrustStoreUsed(useTrustValidation) //
148                 .trustStore(config.trustStore()) //
149                 .trustStorePassword(config.trustStorePassword()) //
150                 .httpProxyConfig(httpProxyConfig).build();
151
152         AsyncRestClientFactory restClientFactory = new AsyncRestClientFactory(config);
153         return restClientFactory.createRestClientNoHttpProxy(selfBaseUrl());
154     }
155
156     private AsyncRestClient restClient() {
157         return restClient(false);
158     }
159
160     private String selfBaseUrl() {
161         return "https://localhost:" + this.applicationConfig.getLocalServerHttpPort();
162     }
163
164     private String ecsBaseUrl() {
165         return applicationConfig.getEcsBaseUrl();
166     }
167
168     private void createInformationJobInEcs() {
169         String url = ecsBaseUrl() + "/data-consumer/v1/info-jobs/jobId";
170         String body = gson.toJson(consumerJobInfo());
171         try {
172             // Delete the job if it already exists
173             restClient().delete(url).block();
174         } catch (Exception e) {
175         }
176         restClient().putForEntity(url, body).block();
177     }
178
179     private ConsumerJobInfo consumerJobInfo() {
180         InfoType type = this.types.getAll().iterator().next();
181         return consumerJobInfo(type.getId(), "EI_JOB_ID");
182     }
183
184     private Object jsonObject() {
185         return jsonObject("{}");
186     }
187
188     private Object jsonObject(String json) {
189         try {
190             return JsonParser.parseString(json).getAsJsonObject();
191         } catch (Exception e) {
192             throw new NullPointerException(e.toString());
193         }
194     }
195
196     private ConsumerJobInfo consumerJobInfo(String typeId, String infoJobId) {
197         try {
198             String targetUri = selfBaseUrl() + ConsumerController.CONSUMER_TARGET_URL;
199             return new ConsumerJobInfo(typeId, jsonObject(), "owner", targetUri, "");
200         } catch (Exception e) {
201             return null;
202         }
203     }
204
205     @Test
206     void testWholeChain() throws Exception {
207         await().untilAsserted(() -> assertThat(producerRegstrationTask.isRegisteredInEcs()).isTrue());
208
209         createInformationJobInEcs();
210
211         await().untilAsserted(() -> assertThat(this.jobs.size()).isEqualTo(1));
212
213         DmaapSimulatorController.dmaapResponses.add("DmaapResponse1");
214         DmaapSimulatorController.dmaapResponses.add("DmaapResponse2");
215
216         ConsumerController.TestResults results = this.consumerController.testResults;
217         await().untilAsserted(() -> assertThat(results.receivedBodies.size()).isEqualTo(2));
218         assertThat(results.receivedBodies.get(0)).isEqualTo("DmaapResponse1");
219
220         synchronized (this) {
221             // logger.warn("**************** Keeping server alive! " +
222             // this.applicationConfig.getLocalServerHttpPort());
223             // this.wait();
224         }
225     }
226
227 }