c634ecc576e37da3ba21bf26dea940a7d6d53a3b
[ccsdk/oran.git] /
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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===================================
17  */
18
19 package org.onap.ccsdk.oran.a1policymanagementservice.controllers.v3;
20
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;
39
40 import java.io.File;
41 import java.lang.reflect.Field;
42 import java.time.Duration;
43 import java.util.Objects;
44
45 import static org.awaitility.Awaitility.await;
46 import static org.hamcrest.CoreMatchers.equalTo;
47
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" //
55 })
56 class ConfigurationControllerTestV3 {
57     @Autowired
58     ApplicationContext context;
59
60     @Autowired
61     ApplicationConfig applicationConfig;
62
63     @Autowired
64     private Rics rics;
65
66     @Autowired
67     private TestHelper testHelper;
68
69     @TempDir
70     public static File temporaryFolder;
71     private static File configFile;
72
73     @LocalServerPort
74     private int port;
75
76     @BeforeEach
77     void init() {
78         testHelper.port = port;
79     }
80     @BeforeAll
81     static void setup() throws Exception {
82         Field f1 = RefreshConfigTask.class.getDeclaredField("configRefreshInterval");
83         f1.setAccessible(true);
84         f1.set(null, Duration.ofSeconds(1));
85     }
86
87     public static class MockApplicationConfig extends ApplicationConfig {
88         @Override
89         public String getLocalConfigurationFilePath() {
90             configFile = new File(temporaryFolder, "config.json");
91             return configFile.getAbsolutePath();
92         }
93     }
94
95     /**
96      * Overrides the BeanFactory.
97      */
98     @TestConfiguration
99     static class TestBeanFactory {
100         @Bean
101         public ApplicationConfig getApplicationConfig() {
102             return new MockApplicationConfig();
103         }
104
105         @Bean
106         public ServletWebServerFactory servletContainer() {
107             return new TomcatServletWebServerFactory();
108         }
109     }
110
111     @Test
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"));
121     }
122
123     @Test
124     public void testHealthCheck() {
125         Mono<ResponseEntity<String>> responseHealthCheckMono = testHelper.restClientV3().getForEntity("/status");
126         testHelper.testSuccessResponse(responseHealthCheckMono, HttpStatus.OK, responseBody -> responseBody.contains("status"));
127     }
128 }