Creating PM-producer
[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.clients.SecurityContext;
34 import org.oran.pmproducer.configuration.ApplicationConfig;
35 import org.oran.pmproducer.configuration.WebClientConfig;
36 import org.oran.pmproducer.configuration.WebClientConfig.HttpProxyConfig;
37 import org.oran.pmproducer.filter.PmReportFilter;
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 class IntegrationWithIcs {
61
62     private static final String JOB_ID = "JOB_ID";
63     private static final Logger logger = LoggerFactory.getLogger(Application.class);
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 SecurityContext securityContext;
76
77     private static Gson gson = new GsonBuilder().disableHtmlEscaping().create();
78
79     static class TestApplicationConfig extends ApplicationConfig {
80
81         @Override
82         public String getIcsBaseUrl() {
83             return "https://localhost:8434";
84         }
85
86         @Override
87         public String getDmaapBaseUrl() {
88             return thisProcessUrl();
89         }
90
91         @Override
92         public String getSelfUrl() {
93             return thisProcessUrl();
94         }
95
96         private String thisProcessUrl() {
97             final String url = "https://localhost:" + getLocalServerHttpPort();
98             return url;
99         }
100     }
101
102     /**
103      * Overrides the BeanFactory.
104      */
105     @TestConfiguration
106     static class TestBeanFactory extends BeanFactory {
107
108         @Override
109         @Bean
110         public ServletWebServerFactory servletContainer() {
111             return new TomcatServletWebServerFactory();
112         }
113
114         @Override
115         @Bean
116         public ApplicationConfig getApplicationConfig() {
117             TestApplicationConfig cfg = new TestApplicationConfig();
118             return cfg;
119         }
120     }
121
122     @AfterEach
123     void reset() {
124         assertThat(this.jobs.size()).isZero();
125     }
126
127     private AsyncRestClient restClient(boolean useTrustValidation) {
128         WebClientConfig config = this.applicationConfig.getWebClientConfig();
129         HttpProxyConfig httpProxyConfig = HttpProxyConfig.builder() //
130                 .httpProxyHost("") //
131                 .httpProxyPort(0) //
132                 .build();
133         config = WebClientConfig.builder() //
134                 .keyStoreType(config.getKeyStoreType()) //
135                 .keyStorePassword(config.getKeyStorePassword()) //
136                 .keyStore(config.getKeyStore()) //
137                 .keyPassword(config.getKeyPassword()) //
138                 .isTrustStoreUsed(useTrustValidation) //
139                 .trustStore(config.getTrustStore()) //
140                 .trustStorePassword(config.getTrustStorePassword()) //
141                 .httpProxyConfig(httpProxyConfig).build();
142
143         AsyncRestClientFactory restClientFactory = new AsyncRestClientFactory(config, securityContext);
144         return restClientFactory.createRestClientNoHttpProxy(selfBaseUrl());
145     }
146
147     private AsyncRestClient restClient() {
148         return restClient(false);
149     }
150
151     private String selfBaseUrl() {
152         return "https://localhost:" + this.applicationConfig.getLocalServerHttpPort();
153     }
154
155     private String icsBaseUrl() {
156         return applicationConfig.getIcsBaseUrl();
157     }
158
159     private String jobUrl(String jobId) {
160         return icsBaseUrl() + "/data-consumer/v1/info-jobs/" + jobId + "?typeCheck=true";
161     }
162
163     private void deleteInformationJobInIcs(String jobId) {
164         try {
165             restClient().delete(jobUrl(jobId)).block();
166         } catch (Exception e) {
167             logger.warn("Couldnot delete job: {}  reason: {}", jobId, e.getMessage());
168         }
169     }
170
171     private void createInformationJobInIcs(String jobId, ConsumerJobInfo jobInfo) {
172         String body = gson.toJson(jobInfo);
173         restClient().putForEntity(jobUrl(jobId), body).block();
174         logger.info("Created job {}, {}", jobId, body);
175     }
176
177     @Test
178     void testCreateJob() throws Exception {
179         await().untilAsserted(() -> assertThat(producerRegstrationTask.isRegisteredInIcs()).isTrue());
180         final String TYPE_ID = "PmDataOverKafka";
181
182         PmReportFilter.FilterData filterData = new PmReportFilter.FilterData();
183
184         ConsumerJobInfo jobInfo = IntegrationWithKafka
185                 .consumerJobInfoKafka(this.applicationConfig.getKafkaBootStrapServers(), TYPE_ID, filterData);
186
187         createInformationJobInIcs(JOB_ID, jobInfo);
188         await().untilAsserted(() -> assertThat(this.jobs.size()).isEqualTo(1));
189
190         deleteInformationJobInIcs(JOB_ID);
191         await().untilAsserted(() -> assertThat(this.jobs.size()).isZero());
192     }
193 }