2 * ========================LICENSE_START=================================
3 * Copyright (C) 2020-2023 Nordix Foundation. All rights reserved.
4 * ======================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 * ========================LICENSE_END===================================
19 package org.onap.ccsdk.oran.a1policymanagementservice.controllers.v2;
21 import static org.assertj.core.api.Assertions.assertThat;
22 import static org.awaitility.Awaitility.await;
23 import static org.hamcrest.CoreMatchers.equalTo;
24 import static org.junit.jupiter.api.Assertions.assertTrue;
27 import java.lang.reflect.Field;
28 import java.time.Duration;
30 import org.apache.commons.io.FileUtils;
31 import org.junit.jupiter.api.BeforeAll;
32 import org.junit.jupiter.api.DisplayName;
33 import org.junit.jupiter.api.Test;
34 import org.junit.jupiter.api.io.TempDir;
35 import org.onap.ccsdk.oran.a1policymanagementservice.clients.AsyncRestClient;
36 import org.onap.ccsdk.oran.a1policymanagementservice.clients.AsyncRestClientFactory;
37 import org.onap.ccsdk.oran.a1policymanagementservice.clients.SecurityContext;
38 import org.onap.ccsdk.oran.a1policymanagementservice.configuration.ApplicationConfig;
39 import org.onap.ccsdk.oran.a1policymanagementservice.configuration.WebClientConfig;
40 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Rics;
41 import org.onap.ccsdk.oran.a1policymanagementservice.tasks.RefreshConfigTask;
42 import org.springframework.beans.factory.annotation.Autowired;
43 import org.springframework.boot.test.context.SpringBootTest;
44 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
45 import org.springframework.boot.test.context.TestConfiguration;
46 import org.springframework.boot.test.web.server.LocalServerPort;
47 import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
48 import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
49 import org.springframework.context.ApplicationContext;
50 import org.springframework.context.annotation.Bean;
51 import org.springframework.http.HttpStatus;
52 import org.springframework.http.MediaType;
53 import org.springframework.test.context.TestPropertySource;
54 import org.springframework.web.reactive.function.client.WebClientResponseException;
56 import reactor.core.publisher.Mono;
57 import reactor.test.StepVerifier;
59 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
60 @TestPropertySource(properties = { //
61 "server.ssl.key-store=./config/keystore.jks", //
62 "app.webclient.trust-store=./config/truststore.jks", //
63 "app.vardata-directory=./target", //
64 "app.config-file-schema-path=/application_configuration_schema.json" //
66 class ConfigurationControllerTest {
68 ApplicationContext context;
71 ApplicationConfig applicationConfig;
77 public static File temporaryFolder;
78 private static File configFile;
81 static void setup() throws Exception {
82 Field f1 = RefreshConfigTask.class.getDeclaredField("configRefreshInterval");
83 f1.setAccessible(true);
84 f1.set(null, Duration.ofSeconds(1));
87 public static class MockApplicationConfig extends ApplicationConfig {
89 public String getLocalConfigurationFilePath() {
90 configFile = new File(temporaryFolder, "config.json");
91 return configFile.getAbsolutePath();
96 * Overrides the BeanFactory.
99 static class TestBeanFactory {
101 public ApplicationConfig getApplicationConfig() {
102 return new MockApplicationConfig();
106 public ServletWebServerFactory servletContainer() {
107 return new TomcatServletWebServerFactory();
115 @DisplayName("put Valid Configuration With New Ric should Update Repository")
116 void putValidConfigurationWithNewRic_shouldUpdateRepository() throws Exception {
117 String url = "a1-policy/v2/configuration";
119 String resp = restClient().put(url, configAsString()).block();
121 assertThat(resp).isEmpty();
122 await().until(rics::size, equalTo(2));
125 resp = restClient().get(url).block();
126 assertThat(resp).contains("config");
130 @DisplayName("get No File Exists")
131 void getNoFileExists() {
132 String url = "a1-policy/v2/configuration";
133 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND, "File does not exist");
136 private String configAsString() throws Exception {
138 new File(getClass().getClassLoader().getResource("test_application_configuration.json").getFile());
139 return FileUtils.readFileToString(configFile, "UTF-8");
143 @DisplayName("put Invalid Configuration should Return Error 400")
144 void putInvalidConfiguration_shouldReturnError400() throws Exception {
145 String url = "a1-policy/v2/configuration";
147 // Valid JSON but invalid configuration.
148 testErrorCode(restClient().put(url, "{\"error\":\"error\"}"), HttpStatus.BAD_REQUEST, "");
151 private void testErrorCode(Mono<?> request, HttpStatus expStatus, String responseContains) {
152 StepVerifier.create(request) //
153 .expectSubscription() //
154 .expectErrorMatches(t -> checkWebClientError(t, expStatus, responseContains)) //
158 private boolean checkWebClientError(Throwable throwable, HttpStatus expStatus, String responseContains) {
159 assertTrue(throwable instanceof WebClientResponseException);
160 WebClientResponseException responseException = (WebClientResponseException) throwable;
161 assertThat(responseException.getStatusCode()).isEqualTo(expStatus);
162 assertThat(responseException.getResponseBodyAsString()).contains(responseContains);
163 assertThat(responseException.getHeaders().getContentType()).isEqualTo(MediaType.APPLICATION_PROBLEM_JSON);
167 private AsyncRestClient restClient() {
168 WebClientConfig config = this.applicationConfig.getWebClientConfig();
169 config = WebClientConfig.builder() //
170 .keyStoreType(config.getKeyStoreType()) //
171 .keyStorePassword(config.getKeyStorePassword()) //
172 .keyStore(config.getKeyStore()) //
173 .keyPassword(config.getKeyPassword()) //
174 .isTrustStoreUsed(false) //
175 .trustStore(config.getTrustStore()) //
176 .trustStorePassword(config.getTrustStorePassword()) //
177 .httpProxyConfig(config.getHttpProxyConfig()) //
180 AsyncRestClientFactory f = new AsyncRestClientFactory(config, new SecurityContext(""));
181 return f.createRestClientNoHttpProxy("https://localhost:" + port);