7a683daca20b938681f797d40b729585ddd456dc
[dcaegen2/services/sdk.git] /
1 /*
2  * ============LICENSE_START====================================
3  * DCAEGEN2-SERVICES-SDK
4  * =========================================================
5  * Copyright (C) 2019-2021 Nokia. All rights reserved.
6  * Copyright (C) 2022 AT&T Intellectual Property. All rights reserved.
7  * =========================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *       http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=====================================
20  */
21
22 package org.onap.dcaegen2.services.sdk.rest.services.cbs.client.api;
23
24
25 import org.junit.Rule;
26 import org.junit.contrib.java.lang.system.EnvironmentVariables;
27 import org.junit.jupiter.api.BeforeEach;
28 import org.junit.jupiter.api.Test;
29 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.api.exceptions.CbsClientConfigurationException;
30 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.CbsClientConfiguration;
31 import org.onap.dcaegen2.services.sdk.security.ssl.Passwords;
32
33 import java.net.URISyntaxException;
34 import java.nio.file.Paths;
35
36 import static org.assertj.core.api.Assertions.assertThat;
37 import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
38
39 /**
40  * @author <a href="mailto:piotr.jaszczyk@nokia.com">Piotr Jaszczyk</a>
41  * @since February 2019
42  */
43 class CbsClientConfigurationTest {
44
45     private static final String ENV_APPNAME = "HOSTNAME";
46     private static final String ENV_CBS_CLIENT_CONFIG_PATH = "CBS_CLIENT_CONFIG_PATH";
47     private static final String ENV_CBS_CLIENT_POLICY_PATH = "CBS_CLIENT_POLICY_PATH";
48
49     @Rule
50     public final EnvironmentVariables envs = new EnvironmentVariables();
51
52     @BeforeEach
53     void setUp(){
54         envs.clear(ENV_APPNAME, ENV_CBS_CLIENT_CONFIG_PATH, ENV_CBS_CLIENT_POLICY_PATH);
55     }
56
57     @Test
58     void fromEnvironment_shouldReturnConfigurationWithCorrectConfigPath_when_CBS_CLIENT_CONFIG_PATH_isSet() {
59         // given
60         createBasicValidEnvsConfiguration();
61         envs.set(ENV_CBS_CLIENT_CONFIG_PATH, "/new/config/path/application.yaml");
62
63         // when
64         CbsClientConfiguration configuration = CbsClientConfiguration.fromEnvironment();
65
66         // then
67         assertThat(configuration).isNotNull();
68         assertThat(configuration.configMapFilePath()).isEqualTo("/new/config/path/application.yaml");
69     }
70
71     @Test
72     void fromEnvironment_shouldReturnConfigurationWithCorrectPolicyPath_when_CBS_CLIENT_POLICY_PATH_isSet() {
73         // given
74         createBasicValidEnvsConfiguration();
75         envs.set(ENV_CBS_CLIENT_POLICY_PATH, "/new/config/path/policy.json");
76
77         // when
78         CbsClientConfiguration configuration = CbsClientConfiguration.fromEnvironment();
79
80         // then
81         assertThat(configuration).isNotNull();
82         assertThat(configuration.policySyncFilePath()).isEqualTo("/new/config/path/policy.json");
83     }
84
85     @Test
86     void fromEnvironment_shouldReturnConfigurationWithDefaultPolicyAndConfigPaths_whenEnvsNotSet() {
87         // given
88         createBasicValidEnvsConfiguration();
89
90         // when
91         CbsClientConfiguration configuration = CbsClientConfiguration.fromEnvironment();
92
93         // then
94         assertThat(configuration).isNotNull();
95         assertThat(configuration.configMapFilePath()).isEqualTo("/app-config/application_config.yaml");
96         assertThat(configuration.policySyncFilePath()).isEqualTo("/etc/policies/policies.json");
97     }
98
99
100     @Test
101     void fromEnvironment_shouldReturn_CbsClientConfigurationException_WhenAllEnvVariablesAreMissing() {
102         assertThatExceptionOfType(CbsClientConfigurationException.class)
103                 .isThrownBy(CbsClientConfiguration::fromEnvironment);
104     }
105
106     private void createBasicValidEnvsConfiguration() {
107         envs.set(ENV_APPNAME, "dcae-prh");
108     }
109 }