Merge "NONRTRIC - Implement DMaaP mediator producer service in Java"
[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.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 @ExtendWith(SpringExtension.class)
56 @SpringBootTest(webEnvironment = WebEnvironment.DEFINED_PORT)
57 @TestPropertySource(properties = { //
58         "server.ssl.key-store=./config/keystore.jks", //
59         "app.webclient.trust-store=./config/truststore.jks", //
60         "app.configuration-filepath=./src/test/resources/test_application_configuration.json", //
61         "app.ecs-base-url=https://localhost:8434" //
62 })
63 class IntegrationWithEcs {
64
65     private static final String EI_JOB_ID = "EI_JOB_ID";
66
67     @Autowired
68     private ApplicationConfig applicationConfig;
69
70     @Autowired
71     private ProducerRegstrationTask producerRegstrationTask;
72
73     @Autowired
74     private Jobs jobs;
75
76     @Autowired
77     private InfoTypes types;
78
79     @Autowired
80     private ConsumerController consumerController;
81
82     private static Gson gson = new GsonBuilder().create();
83
84     static class TestApplicationConfig extends ApplicationConfig {
85
86         @Override
87         public String getEcsBaseUrl() {
88             return "https://localhost:8434";
89         }
90
91         @Override
92         public String getDmaapBaseUrl() {
93             return thisProcessUrl();
94         }
95
96         @Override
97         public String getSelfUrl() {
98             return thisProcessUrl();
99         }
100
101         private String thisProcessUrl() {
102             final String url = "https://localhost:" + getLocalServerHttpPort();
103             return url;
104         }
105     }
106
107     /**
108      * Overrides the BeanFactory.
109      */
110     @TestConfiguration
111     static class TestBeanFactory extends BeanFactory {
112
113         @Override
114         @Bean
115         public ServletWebServerFactory servletContainer() {
116             return new TomcatServletWebServerFactory();
117         }
118
119         @Override
120         @Bean
121         public ApplicationConfig getApplicationConfig() {
122             TestApplicationConfig cfg = new TestApplicationConfig();
123             return cfg;
124         }
125     }
126
127     @AfterEach
128     void reset() {
129         this.consumerController.testResults.reset();
130         this.jobs.clear();
131         this.types.clear();
132     }
133
134     private AsyncRestClient restClient(boolean useTrustValidation) {
135         WebClientConfig config = this.applicationConfig.getWebClientConfig();
136         HttpProxyConfig httpProxyConfig = ImmutableHttpProxyConfig.builder() //
137                 .httpProxyHost("") //
138                 .httpProxyPort(0) //
139                 .build();
140         config = ImmutableWebClientConfig.builder() //
141                 .keyStoreType(config.keyStoreType()) //
142                 .keyStorePassword(config.keyStorePassword()) //
143                 .keyStore(config.keyStore()) //
144                 .keyPassword(config.keyPassword()) //
145                 .isTrustStoreUsed(useTrustValidation) //
146                 .trustStore(config.trustStore()) //
147                 .trustStorePassword(config.trustStorePassword()) //
148                 .httpProxyConfig(httpProxyConfig).build();
149
150         AsyncRestClientFactory restClientFactory = new AsyncRestClientFactory(config);
151         return restClientFactory.createRestClientNoHttpProxy(selfBaseUrl());
152     }
153
154     private AsyncRestClient restClient() {
155         return restClient(false);
156     }
157
158     private String selfBaseUrl() {
159         return "https://localhost:" + this.applicationConfig.getLocalServerHttpPort();
160     }
161
162     private String ecsBaseUrl() {
163         return applicationConfig.getEcsBaseUrl();
164     }
165
166     private String jobUrl(String jobId) {
167         return ecsBaseUrl() + "/data-consumer/v1/info-jobs/" + jobId;
168     }
169
170     private void createInformationJobInEcs(String jobId) {
171         String body = gson.toJson(consumerJobInfo());
172         try {
173             // Delete the job if it already exists
174             deleteInformationJobInEcs(jobId);
175         } catch (Exception e) {
176         }
177         restClient().putForEntity(jobUrl(jobId), body).block();
178     }
179
180     private void deleteInformationJobInEcs(String jobId) {
181         restClient().delete(jobUrl(jobId)).block();
182     }
183
184     private ConsumerJobInfo consumerJobInfo() {
185         InfoType type = this.types.getAll().iterator().next();
186         return consumerJobInfo(type.getId(), EI_JOB_ID);
187     }
188
189     private Object jsonObject() {
190         return jsonObject("{}");
191     }
192
193     private Object jsonObject(String json) {
194         try {
195             return JsonParser.parseString(json).getAsJsonObject();
196         } catch (Exception e) {
197             throw new NullPointerException(e.toString());
198         }
199     }
200
201     private ConsumerJobInfo consumerJobInfo(String typeId, String infoJobId) {
202         try {
203             String targetUri = selfBaseUrl() + ConsumerController.CONSUMER_TARGET_URL;
204             return new ConsumerJobInfo(typeId, jsonObject(), "owner", targetUri, "");
205         } catch (Exception e) {
206             return null;
207         }
208     }
209
210     @Test
211     void testWholeChain() throws Exception {
212         await().untilAsserted(() -> assertThat(producerRegstrationTask.isRegisteredInEcs()).isTrue());
213
214         createInformationJobInEcs(EI_JOB_ID);
215
216         await().untilAsserted(() -> assertThat(this.jobs.size()).isEqualTo(1));
217
218         DmaapSimulatorController.dmaapResponses.add("DmaapResponse1");
219         DmaapSimulatorController.dmaapResponses.add("DmaapResponse2");
220
221         ConsumerController.TestResults results = this.consumerController.testResults;
222         await().untilAsserted(() -> assertThat(results.receivedBodies.size()).isEqualTo(2));
223         assertThat(results.receivedBodies.get(0)).isEqualTo("DmaapResponse1");
224
225         deleteInformationJobInEcs(EI_JOB_ID);
226
227         await().untilAsserted(() -> assertThat(this.jobs.size()).isZero());
228
229         synchronized (this) {
230             // logger.warn("**************** Keeping server alive! " +
231             // this.applicationConfig.getLocalServerHttpPort());
232             // this.wait();
233         }
234     }
235
236 }