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.v3;
21 import org.junit.jupiter.api.*;
22 import org.junit.jupiter.api.io.TempDir;
23 import org.onap.ccsdk.oran.a1policymanagementservice.configuration.ApplicationConfig;
24 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Rics;
25 import org.onap.ccsdk.oran.a1policymanagementservice.tasks.RefreshConfigTask;
26 import org.onap.ccsdk.oran.a1policymanagementservice.utils.v3.TestHelper;
27 import org.springframework.beans.factory.annotation.Autowired;
28 import org.springframework.boot.test.context.SpringBootTest;
29 import org.springframework.boot.test.context.TestConfiguration;
30 import org.springframework.boot.test.web.server.LocalServerPort;
31 import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
32 import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
33 import org.springframework.context.ApplicationContext;
34 import org.springframework.context.annotation.Bean;
35 import org.springframework.http.HttpStatus;
36 import org.springframework.http.ResponseEntity;
37 import org.springframework.test.context.TestPropertySource;
38 import reactor.core.publisher.Mono;
41 import java.lang.reflect.Field;
42 import java.time.Duration;
43 import java.util.Objects;
45 import static org.awaitility.Awaitility.await;
46 import static org.hamcrest.CoreMatchers.equalTo;
48 @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
49 @TestPropertySource(properties = { //
50 "server.ssl.key-store=./config/keystore.jks", //
51 "app.webclient.trust-store=./config/truststore.jks", //
52 "app.vardata-directory=./target", //
53 "app.config-file-schema-path=/application_configuration_schema.json" //
55 class ConfigurationControllerV3Test {
57 ApplicationContext context;
60 ApplicationConfig applicationConfig;
66 private TestHelper testHelper;
69 public static File temporaryFolder;
70 private static File configFile;
77 testHelper.port = port;
80 static void setup() throws Exception {
81 Field f1 = RefreshConfigTask.class.getDeclaredField("configRefreshInterval");
82 f1.setAccessible(true);
83 f1.set(null, Duration.ofSeconds(1));
86 public static class MockApplicationConfig extends ApplicationConfig {
88 public String getLocalConfigurationFilePath() {
89 configFile = new File(temporaryFolder, "config.json");
90 return configFile.getAbsolutePath();
95 * Overrides the BeanFactory.
98 static class TestBeanFactory {
100 public ApplicationConfig getApplicationConfig() {
101 return new MockApplicationConfig();
105 public ServletWebServerFactory servletContainer() {
106 return new TomcatServletWebServerFactory();
111 void testPutConfiguration() throws Exception {
112 Mono<ResponseEntity<String>> responseEntityMono = testHelper.restClientV3().putForEntity("/configuration",
113 testHelper.configAsString());
114 testHelper.testSuccessResponse(responseEntityMono, HttpStatus.OK, Objects::isNull);
115 //put Valid Configuration With New Ric should Update Repository. So, will wait until the ric size is 2
116 await().until(rics::size, equalTo(2));
117 //test Get Configuration
118 Mono<ResponseEntity<String>> responseGetConfigMono = testHelper.restClientV3().getForEntity("/configuration");
119 testHelper.testSuccessResponse(responseGetConfigMono, HttpStatus.OK, responseBody -> responseBody.contains("config"));
123 void testHealthCheck() {
124 Mono<ResponseEntity<String>> responseHealthCheckMono = testHelper.restClientV3().getForEntity("/status");
125 testHelper.testSuccessResponse(responseHealthCheckMono, HttpStatus.OK, responseBody -> responseBody.contains("status"));