2 * ========================LICENSE_START=================================
5 * Copyright (C) 2021 Nordix Foundation
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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===================================
21 package org.oran.dmaapadapter;
23 import static org.assertj.core.api.Assertions.assertThat;
24 import static org.awaitility.Awaitility.await;
26 import com.google.gson.Gson;
27 import com.google.gson.GsonBuilder;
28 import com.google.gson.JsonParser;
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;
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.configuration-filepath=./src/test/resources/test_application_configuration.json", //
63 "app.ecs-base-url=https://localhost:8434" //
65 class IntegrationWithEcs {
66 private static final Logger logger = LoggerFactory.getLogger(ApplicationTest.class);
69 private ApplicationConfig applicationConfig;
72 private ProducerRegstrationTask producerRegstrationTask;
78 private InfoTypes types;
81 private ConsumerController consumerController;
83 private static Gson gson = new GsonBuilder().create();
85 static class TestApplicationConfig extends ApplicationConfig {
88 public String getEcsBaseUrl() {
89 return "https://localhost:8434";
93 public String getDmaapBaseUrl() {
94 return thisProcessUrl();
98 public String getSelfUrl() {
99 return thisProcessUrl();
102 private String thisProcessUrl() {
103 final String url = "https://localhost:" + getLocalServerHttpPort();
109 * Overrides the BeanFactory.
112 static class TestBeanFactory extends BeanFactory {
116 public ServletWebServerFactory servletContainer() {
117 return new TomcatServletWebServerFactory();
122 public ApplicationConfig getApplicationConfig() {
123 TestApplicationConfig cfg = new TestApplicationConfig();
130 this.consumerController.testResults.reset();
135 private AsyncRestClient restClient(boolean useTrustValidation) {
136 WebClientConfig config = this.applicationConfig.getWebClientConfig();
137 HttpProxyConfig httpProxyConfig = ImmutableHttpProxyConfig.builder() //
138 .httpProxyHost("") //
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();
151 AsyncRestClientFactory restClientFactory = new AsyncRestClientFactory(config);
152 return restClientFactory.createRestClientNoHttpProxy(selfBaseUrl());
155 private AsyncRestClient restClient() {
156 return restClient(false);
159 private String selfBaseUrl() {
160 return "https://localhost:" + this.applicationConfig.getLocalServerHttpPort();
163 private String ecsBaseUrl() {
164 return applicationConfig.getEcsBaseUrl();
167 private void createInformationJobInEcs() {
168 String url = ecsBaseUrl() + "/data-consumer/v1/info-jobs/jobId";
169 String body = gson.toJson(consumerJobInfo());
171 // Delete the job if it already exists
172 restClient().delete(url).block();
173 } catch (Exception e) {
175 restClient().putForEntity(url, body).block();
178 private ConsumerJobInfo consumerJobInfo() {
179 InfoType type = this.types.getAll().iterator().next();
180 return consumerJobInfo(type.getId(), "EI_JOB_ID");
183 private Object jsonObject() {
184 return jsonObject("{}");
187 private Object jsonObject(String json) {
189 return JsonParser.parseString(json).getAsJsonObject();
190 } catch (Exception e) {
191 throw new NullPointerException(e.toString());
195 private ConsumerJobInfo consumerJobInfo(String typeId, String infoJobId) {
197 String targetUri = selfBaseUrl() + ConsumerController.CONSUMER_TARGET_URL;
198 return new ConsumerJobInfo(typeId, jsonObject(), "owner", targetUri, "");
199 } catch (Exception e) {
205 void testWholeChain() throws Exception {
206 await().untilAsserted(() -> assertThat(producerRegstrationTask.isRegisteredInEcs()).isTrue());
208 createInformationJobInEcs();
210 await().untilAsserted(() -> assertThat(this.jobs.size()).isEqualTo(1));
212 DmaapSimulatorController.dmaapResponses.add("DmaapResponse1");
213 DmaapSimulatorController.dmaapResponses.add("DmaapResponse2");
215 ConsumerController.TestResults results = this.consumerController.testResults;
216 await().untilAsserted(() -> assertThat(results.receivedBodies.size()).isEqualTo(2));
217 assertThat(results.receivedBodies.get(0)).isEqualTo("DmaapResponse1");
219 synchronized (this) {
220 // logger.warn("**************** Keeping server alive! " +
221 // this.applicationConfig.getLocalServerHttpPort());