f6a8d5d1dbb03959e9c14563a13cc2e458a4ec39
[nonrtric/plt/ranpm.git] / pmproducer / src / test / java / org / oran / pmproducer / IntegrationWithIcs.java
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2023 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.pmproducer;
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
29 import org.junit.jupiter.api.AfterEach;
30 import org.junit.jupiter.api.Test;
31 import org.oran.pmproducer.clients.AsyncRestClient;
32 import org.oran.pmproducer.clients.AsyncRestClientFactory;
33 import org.oran.pmproducer.configuration.ApplicationConfig;
34 import org.oran.pmproducer.configuration.WebClientConfig;
35 import org.oran.pmproducer.configuration.WebClientConfig.HttpProxyConfig;
36 import org.oran.pmproducer.filter.PmReportFilter;
37 import org.oran.pmproducer.oauth2.SecurityContext;
38 import org.oran.pmproducer.r1.ConsumerJobInfo;
39 import org.oran.pmproducer.repository.Jobs;
40 import org.oran.pmproducer.tasks.ProducerRegstrationTask;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
43 import org.springframework.beans.factory.annotation.Autowired;
44 import org.springframework.boot.test.context.SpringBootTest;
45 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
46 import org.springframework.boot.test.context.TestConfiguration;
47 import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
48 import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
49 import org.springframework.context.annotation.Bean;
50 import org.springframework.test.context.TestPropertySource;
51
52 @SuppressWarnings("java:S3577") // Rename class
53 @SpringBootTest(webEnvironment = WebEnvironment.DEFINED_PORT)
54 @TestPropertySource(properties = { //
55         "server.ssl.key-store=./config/keystore.jks", //
56         "app.webclient.trust-store=./config/truststore.jks", //
57         "app.configuration-filepath=./src/test/resources/test_application_configuration.json", //
58         "app.ics-base-url=https://localhost:8434" //
59 })
60 /**
61  * Tests that the interwork with ICS works.
62  * ICS must be running.
63  */
64 class IntegrationWithIcs {
65
66     private static final String JOB_ID = "JOB_ID";
67     private static final Logger logger = LoggerFactory.getLogger(Application.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 SecurityContext securityContext;
80
81     private static Gson gson = new GsonBuilder().disableHtmlEscaping().create();
82
83     static class TestApplicationConfig extends ApplicationConfig {
84
85         @Override
86         public String getIcsBaseUrl() {
87             return "https://localhost:8434";
88         }
89
90         @Override
91         public String getSelfUrl() {
92             return thisProcessUrl();
93         }
94
95         private String thisProcessUrl() {
96             final String url = "https://localhost:" + getLocalServerHttpPort();
97             return url;
98         }
99     }
100
101     /**
102      * Overrides the BeanFactory.
103      */
104     @TestConfiguration
105     static class TestBeanFactory extends BeanFactory {
106
107         @Override
108         @Bean
109         public ServletWebServerFactory servletContainer() {
110             return new TomcatServletWebServerFactory();
111         }
112
113         @Override
114         @Bean
115         public ApplicationConfig getApplicationConfig() {
116             TestApplicationConfig cfg = new TestApplicationConfig();
117             return cfg;
118         }
119     }
120
121     @AfterEach
122     void reset() {
123         assertThat(this.jobs.size()).isZero();
124     }
125
126     private AsyncRestClient restClient(boolean useTrustValidation) {
127         WebClientConfig config = this.applicationConfig.getWebClientConfig();
128         HttpProxyConfig httpProxyConfig = HttpProxyConfig.builder() //
129                 .httpProxyHost("") //
130                 .httpProxyPort(0) //
131                 .build();
132         config = WebClientConfig.builder() //
133                 .keyStoreType(config.getKeyStoreType()) //
134                 .keyStorePassword(config.getKeyStorePassword()) //
135                 .keyStore(config.getKeyStore()) //
136                 .keyPassword(config.getKeyPassword()) //
137                 .isTrustStoreUsed(useTrustValidation) //
138                 .trustStore(config.getTrustStore()) //
139                 .trustStorePassword(config.getTrustStorePassword()) //
140                 .httpProxyConfig(httpProxyConfig).build();
141
142         AsyncRestClientFactory restClientFactory = new AsyncRestClientFactory(config, securityContext);
143         return restClientFactory.createRestClientNoHttpProxy(selfBaseUrl());
144     }
145
146     private AsyncRestClient restClient() {
147         return restClient(false);
148     }
149
150     private String selfBaseUrl() {
151         return "https://localhost:" + this.applicationConfig.getLocalServerHttpPort();
152     }
153
154     private String icsBaseUrl() {
155         return applicationConfig.getIcsBaseUrl();
156     }
157
158     private String jobUrl(String jobId) {
159         return icsBaseUrl() + "/data-consumer/v1/info-jobs/" + jobId + "?typeCheck=true";
160     }
161
162     private void deleteInformationJobInIcs(String jobId) {
163         try {
164             restClient().delete(jobUrl(jobId)).block();
165         } catch (Exception e) {
166             logger.warn("Couldnot delete job: {}  reason: {}", jobId, e.getMessage());
167         }
168     }
169
170     private void createInformationJobInIcs(String jobId, ConsumerJobInfo jobInfo) {
171         String body = gson.toJson(jobInfo);
172         restClient().putForEntity(jobUrl(jobId), body).block();
173         logger.info("Created job {}, {}", jobId, body);
174     }
175
176     @Test
177     void testCreateJob() throws Exception {
178         await().untilAsserted(() -> assertThat(producerRegstrationTask.isRegisteredInIcs()).isTrue());
179         final String TYPE_ID = "PmDataOverKafka";
180
181         PmReportFilter.FilterData filterData = new PmReportFilter.FilterData();
182
183         ConsumerJobInfo jobInfo = IntegrationWithKafka
184                 .consumerJobInfoKafka(this.applicationConfig.getKafkaBootStrapServers(), TYPE_ID, filterData);
185
186         createInformationJobInIcs(JOB_ID, jobInfo);
187         await().untilAsserted(() -> assertThat(this.jobs.size()).isEqualTo(1));
188
189         deleteInformationJobInIcs(JOB_ID);
190         await().untilAsserted(() -> assertThat(this.jobs.size()).isZero());
191     }
192 }