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 @TestMethodOrder(MethodOrderer.MethodName.class)
49 @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
50 @TestPropertySource(properties = { //
51 "server.ssl.key-store=./config/keystore.jks", //
52 "app.webclient.trust-store=./config/truststore.jks", //
53 "app.vardata-directory=./target", //
54 "app.config-file-schema-path=/application_configuration_schema.json" //
56 class ConfigurationControllerTestV3 {
58 ApplicationContext context;
61 ApplicationConfig applicationConfig;
67 private TestHelper testHelper;
70 public static File temporaryFolder;
71 private static File configFile;
78 testHelper.port = port;
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();
112 void testPutConfiguration() throws Exception {
113 Mono<ResponseEntity<String>> responseEntityMono = testHelper.restClientV3().putForEntity("/configuration",
114 testHelper.configAsString());
115 testHelper.testSuccessResponse(responseEntityMono, HttpStatus.OK, Objects::isNull);
116 //put Valid Configuration With New Ric should Update Repository. So, will wait until the ric size is 2
117 await().until(rics::size, equalTo(2));
118 //test Get Configuration
119 Mono<ResponseEntity<String>> responseGetConfigMono = testHelper.restClientV3().getForEntity("/configuration");
120 testHelper.testSuccessResponse(responseGetConfigMono, HttpStatus.OK, responseBody -> responseBody.contains("config"));
124 public void testHealthCheck() {
125 Mono<ResponseEntity<String>> responseHealthCheckMono = testHelper.restClientV3().getForEntity("/status");
126 testHelper.testSuccessResponse(responseHealthCheckMono, HttpStatus.OK, responseBody -> responseBody.contains("status"));