1f0ea5d013429f56216969aa8241a62c93130a72
[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 @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" //
54 })
55 class ConfigurationControllerV3Test {
56     @Autowired
57     ApplicationContext context;
58
59     @Autowired
60     ApplicationConfig applicationConfig;
61
62     @Autowired
63     private Rics rics;
64
65     @Autowired
66     private TestHelper testHelper;
67
68     @TempDir
69     public static File temporaryFolder;
70     private static File configFile;
71
72     @LocalServerPort
73     private int port;
74
75     @BeforeEach
76     void init() {
77         testHelper.port = port;
78     }
79     @BeforeAll
80     static void setup() throws Exception {
81         Field f1 = RefreshConfigTask.class.getDeclaredField("configRefreshInterval");
82         f1.setAccessible(true);
83         f1.set(null, Duration.ofSeconds(1));
84     }
85
86     public static class MockApplicationConfig extends ApplicationConfig {
87         @Override
88         public String getLocalConfigurationFilePath() {
89             configFile = new File(temporaryFolder, "config.json");
90             return configFile.getAbsolutePath();
91         }
92     }
93
94     /**
95      * Overrides the BeanFactory.
96      */
97     @TestConfiguration
98     static class TestBeanFactory {
99         @Bean
100         public ApplicationConfig getApplicationConfig() {
101             return new MockApplicationConfig();
102         }
103
104         @Bean
105         public ServletWebServerFactory servletContainer() {
106             return new TomcatServletWebServerFactory();
107         }
108     }
109
110     @Test
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"));
120     }
121
122     @Test
123     void testHealthCheck() {
124         Mono<ResponseEntity<String>> responseHealthCheckMono = testHelper.restClientV3().getForEntity("/status");
125         testHelper.testSuccessResponse(responseHealthCheckMono, HttpStatus.OK, responseBody -> responseBody.contains("status"));
126     }
127 }