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