376ce76e08f1d2d84ffa3d8638e80a9e7dedd0a9
[ccsdk/oran.git] /
1 /*-
2  * ========================LICENSE_START=================================
3  * Copyright (C) 2020 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.v2;
20
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;
25
26 import java.io.File;
27 import java.lang.reflect.Field;
28 import java.time.Duration;
29
30 import org.apache.commons.io.FileUtils;
31 import org.junit.jupiter.api.BeforeAll;
32 import org.junit.jupiter.api.Test;
33 import org.junit.jupiter.api.extension.ExtendWith;
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.configuration.ApplicationConfig;
38 import org.onap.ccsdk.oran.a1policymanagementservice.configuration.ImmutableWebClientConfig;
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.web.server.LocalServerPort;
47 import org.springframework.context.ApplicationContext;
48 import org.springframework.context.annotation.Bean;
49 import org.springframework.http.HttpStatus;
50 import org.springframework.http.MediaType;
51 import org.springframework.test.context.TestPropertySource;
52 import org.springframework.test.context.junit.jupiter.SpringExtension;
53 import org.springframework.web.reactive.function.client.WebClientResponseException;
54
55 import reactor.core.publisher.Mono;
56 import reactor.test.StepVerifier;
57
58 @ExtendWith(SpringExtension.class)
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 class ConfigurationControllerTest {
64     @Autowired
65     ApplicationContext context;
66
67     @Autowired
68     ApplicationConfig applicationConfig;
69
70     @Autowired
71     private Rics rics;
72
73     @TempDir
74     public static File temporaryFolder;
75     private static File configFile;
76
77     @BeforeAll
78     private static void setup() throws Exception {
79         Field f1 = RefreshConfigTask.class.getDeclaredField("configRefreshInterval");
80         f1.setAccessible(true);
81         f1.set(null, Duration.ofSeconds(1));
82     }
83
84     public static class MockApplicationConfig extends ApplicationConfig {
85         @Override
86         public String getLocalConfigurationFilePath() {
87             configFile = new File(temporaryFolder, "config.json");
88             return configFile.getAbsolutePath();
89         }
90     }
91
92     /**
93      * Overrides the BeanFactory.
94      */
95     @TestConfiguration
96     static class TestBeanFactory {
97         @Bean
98         public ApplicationConfig getApplicationConfig() {
99             return new MockApplicationConfig();
100         }
101     }
102
103     @LocalServerPort
104     private int port;
105
106     @Test
107     void putValidConfigurationWithNewRic_shouldUpdateRepository() throws Exception {
108         String url = "/v2/configuration";
109
110         String resp = restClient().put(url, configAsString()).block();
111
112         assertThat(resp).isEmpty();
113         await().until(rics::size, equalTo(2));
114
115         // GET config
116         resp = restClient().get(url).block();
117         assertThat(resp).contains("config");
118     }
119
120     @Test
121     void getNoFileExists() {
122         String url = "/v2/configuration";
123         testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND, "File does not exist");
124     }
125
126     private String configAsString() throws Exception {
127         File configFile = new File(getClass().getClassLoader()
128                 .getResource("test_application_configuration_with_dmaap_config.json").getFile());
129         return FileUtils.readFileToString(configFile, "UTF-8");
130     }
131
132     @Test
133     void putInvalidConfiguration_shouldReturnError400() throws Exception {
134         String url = "/v2/configuration";
135
136         // Valid JSON but invalid configuration.
137         testErrorCode(restClient().put(url, "{\"error\":\"error\"}"), HttpStatus.BAD_REQUEST, "Faulty configuration");
138     }
139
140     private void testErrorCode(Mono<?> request, HttpStatus expStatus, String responseContains) {
141         StepVerifier.create(request) //
142                 .expectSubscription() //
143                 .expectErrorMatches(t -> checkWebClientError(t, expStatus, responseContains)) //
144                 .verify();
145     }
146
147     private boolean checkWebClientError(Throwable throwable, HttpStatus expStatus, String responseContains) {
148         assertTrue(throwable instanceof WebClientResponseException);
149         WebClientResponseException responseException = (WebClientResponseException) throwable;
150         assertThat(responseException.getStatusCode()).isEqualTo(expStatus);
151         assertThat(responseException.getResponseBodyAsString()).contains(responseContains);
152         assertThat(responseException.getHeaders().getContentType()).isEqualTo(MediaType.APPLICATION_PROBLEM_JSON);
153         return true;
154     }
155
156     private AsyncRestClient restClient() {
157         WebClientConfig config = this.applicationConfig.getWebClientConfig();
158         config = ImmutableWebClientConfig.builder() //
159                 .keyStoreType(config.keyStoreType()) //
160                 .keyStorePassword(config.keyStorePassword()) //
161                 .keyStore(config.keyStore()) //
162                 .keyPassword(config.keyPassword()) //
163                 .isTrustStoreUsed(true) //
164                 .trustStore(config.trustStore()) //
165                 .trustStorePassword(config.trustStorePassword()) //
166                 .httpProxyConfig(config.httpProxyConfig()) //
167                 .build();
168
169         AsyncRestClientFactory f = new AsyncRestClientFactory(config);
170         return f.createRestClient("https://localhost:" + port);
171
172     }
173 }