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     @Autowired
66     private ApplicationConfig applicationConfig;
67
68     @Autowired
69     private ProducerRegstrationTask producerRegstrationTask;
70
71     @Autowired
72     private Jobs jobs;
73
74     @Autowired
75     private InfoTypes types;
76
77     @Autowired
78     private ConsumerController consumerController;
79
80     private static Gson gson = new GsonBuilder().create();
81
82     static class TestApplicationConfig extends ApplicationConfig {
83
84         @Override
85         public String getEcsBaseUrl() {
86             return "https://localhost:8434";
87         }
88
89         @Override
90         public String getDmaapBaseUrl() {
91             return thisProcessUrl();
92         }
93
94         @Override
95         public String getSelfUrl() {
96             return thisProcessUrl();
97         }
98
99         private String thisProcessUrl() {
100             final String url = "https://localhost:" + getLocalServerHttpPort();
101             return url;
102         }
103     }
104
105     /**
106      * Overrides the BeanFactory.
107      */
108     @TestConfiguration
109     static class TestBeanFactory extends BeanFactory {
110
111         @Override
112         @Bean
113         public ServletWebServerFactory servletContainer() {
114             return new TomcatServletWebServerFactory();
115         }
116
117         @Override
118         @Bean
119         public ApplicationConfig getApplicationConfig() {
120             TestApplicationConfig cfg = new TestApplicationConfig();
121             return cfg;
122         }
123     }
124
125     @AfterEach
126     void reset() {
127         this.consumerController.testResults.reset();
128         this.jobs.clear();
129         this.types.clear();
130     }
131
132     private AsyncRestClient restClient(boolean useTrustValidation) {
133         WebClientConfig config = this.applicationConfig.getWebClientConfig();
134         HttpProxyConfig httpProxyConfig = ImmutableHttpProxyConfig.builder() //
135                 .httpProxyHost("") //
136                 .httpProxyPort(0) //
137                 .build();
138         config = ImmutableWebClientConfig.builder() //
139                 .keyStoreType(config.keyStoreType()) //
140                 .keyStorePassword(config.keyStorePassword()) //
141                 .keyStore(config.keyStore()) //
142                 .keyPassword(config.keyPassword()) //
143                 .isTrustStoreUsed(useTrustValidation) //
144                 .trustStore(config.trustStore()) //
145                 .trustStorePassword(config.trustStorePassword()) //
146                 .httpProxyConfig(httpProxyConfig).build();
147
148         AsyncRestClientFactory restClientFactory = new AsyncRestClientFactory(config);
149         return restClientFactory.createRestClientNoHttpProxy(selfBaseUrl());
150     }
151
152     private AsyncRestClient restClient() {
153         return restClient(false);
154     }
155
156     private String selfBaseUrl() {
157         return "https://localhost:" + this.applicationConfig.getLocalServerHttpPort();
158     }
159
160     private String ecsBaseUrl() {
161         return applicationConfig.getEcsBaseUrl();
162     }
163
164     private void createInformationJobInEcs() {
165         String url = ecsBaseUrl() + "/data-consumer/v1/info-jobs/jobId";
166         String body = gson.toJson(consumerJobInfo());
167         try {
168             // Delete the job if it already exists
169             restClient().delete(url).block();
170         } catch (Exception e) {
171         }
172         restClient().putForEntity(url, body).block();
173     }
174
175     private ConsumerJobInfo consumerJobInfo() {
176         InfoType type = this.types.getAll().iterator().next();
177         return consumerJobInfo(type.getId(), "EI_JOB_ID");
178     }
179
180     private Object jsonObject() {
181         return jsonObject("{}");
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 typeId, String infoJobId) {
193         try {
194             String targetUri = selfBaseUrl() + ConsumerController.CONSUMER_TARGET_URL;
195             return new ConsumerJobInfo(typeId, jsonObject(), "owner", targetUri, "");
196         } catch (Exception e) {
197             return null;
198         }
199     }
200
201     @Test
202     void testWholeChain() throws Exception {
203         await().untilAsserted(() -> assertThat(producerRegstrationTask.isRegisteredInEcs()).isTrue());
204
205         createInformationJobInEcs();
206
207         await().untilAsserted(() -> assertThat(this.jobs.size()).isEqualTo(1));
208
209         DmaapSimulatorController.dmaapResponses.add("DmaapResponse1");
210         DmaapSimulatorController.dmaapResponses.add("DmaapResponse2");
211
212         ConsumerController.TestResults results = this.consumerController.testResults;
213         await().untilAsserted(() -> assertThat(results.receivedBodies.size()).isEqualTo(2));
214         assertThat(results.receivedBodies.get(0)).isEqualTo("DmaapResponse1");
215
216         synchronized (this) {
217             // logger.warn("**************** Keeping server alive! " +
218             // this.applicationConfig.getLocalServerHttpPort());
219             // this.wait();
220         }
221     }
222
223 }