2e57adaaa48b0a4c146660ab5f12a89763995db8
[dcaegen2/services/sdk.git] /
1 /*
2  * ============LICENSE_START====================================
3  * DCAEGEN2-SERVICES-SDK
4  * =========================================================
5  * Copyright (C) 2019-2021 Nokia. All rights reserved.
6  * =========================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *       http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=====================================
19  */
20
21 package org.onap.dcaegen2.services.sdk.rest.services.cbs.client.api;
22
23
24 import org.junit.Rule;
25 import org.junit.contrib.java.lang.system.EnvironmentVariables;
26 import org.junit.jupiter.api.BeforeEach;
27 import org.junit.jupiter.api.Test;
28 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.api.exceptions.CbsClientConfigurationException;
29 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.CbsClientConfiguration;
30 import org.onap.dcaegen2.services.sdk.security.ssl.Passwords;
31
32 import java.net.URISyntaxException;
33 import java.nio.file.Paths;
34
35 import static org.assertj.core.api.Assertions.assertThat;
36 import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
37
38 /**
39  * @author <a href="mailto:piotr.jaszczyk@nokia.com">Piotr Jaszczyk</a>
40  * @since February 2019
41  */
42 class CbsClientConfigurationTest {
43
44     public static final String ENV_DCAE_CA_CERTPATH = "DCAE_CA_CERTPATH";
45     public static final String ENV_CONFIG_BINDING_SERVICE = "CONFIG_BINDING_SERVICE";
46     public static final String ENV_CONFIG_BINDING_SERVICE_SERVICE_PORT = "CONFIG_BINDING_SERVICE_SERVICE_PORT";
47     public static final String ENV_HOSTNAME = "HOSTNAME";
48     public static final String ENV_CONSUL_HOST = "CONSUL_HOST";
49     public static final String ENV_CBS_CLIENT_CONFIG_PATH = "CBS_CLIENT_CONFIG_PATH";
50     public static final String ENV_CBS_CLIENT_POLICY_PATH = "CBS_CLIENT_POLICY_PATH";
51
52     @Rule
53     public final EnvironmentVariables envs = new EnvironmentVariables();
54
55     @BeforeEach
56     void setUp(){
57         envs.clear(ENV_DCAE_CA_CERTPATH, ENV_CONFIG_BINDING_SERVICE, ENV_CONFIG_BINDING_SERVICE_SERVICE_PORT,
58             ENV_HOSTNAME, ENV_CONSUL_HOST, ENV_CBS_CLIENT_CONFIG_PATH, ENV_CBS_CLIENT_POLICY_PATH);
59     }
60
61     @Test
62     void fromEnvironment_shouldReturnConfigurationForConnectionWithoutTls_when_DCAE_CA_CERTPATH_isEmpty() {
63         // given
64         createBasicValidEnvsConfiguration();
65         envs.set(ENV_DCAE_CA_CERTPATH, "");
66
67         // when
68         CbsClientConfiguration configuration = CbsClientConfiguration.fromEnvironment();
69
70         // then
71         assertThat(configuration.trustStoreKeys()).isEqualTo(null);
72         assertThat(configuration.protocol()).isEqualTo("http");
73     }
74
75     @Test
76     void fromEnvironment_shouldReturnConfigurationForConnectionOverTls_when_DCAE_CA_CERTPATH_isSet() throws URISyntaxException {
77         // given
78         envs.set(ENV_DCAE_CA_CERTPATH, preparePathToCertFile());
79         envs.set(ENV_CONFIG_BINDING_SERVICE, "config-binding-service");
80         envs.set(ENV_HOSTNAME, "dcae-prh");
81         envs.set(ENV_CONSUL_HOST, "consul-server.onap");
82
83         // when
84         CbsClientConfiguration configuration = CbsClientConfiguration.fromEnvironment();
85
86         // then
87         assertThat(configuration.trustStoreKeys()).isNotNull();
88         assertThat(configuration.protocol()).isEqualTo("https");
89     }
90
91     @Test
92     void fromEnvironment_shouldReturnConfigurationWithCorrectConfigPath_when_CBS_CLIENT_CONFIG_PATH_isSet() {
93         // given
94         createBasicValidEnvsConfiguration();
95         envs.set(ENV_CBS_CLIENT_CONFIG_PATH, "/new/config/path/application.yaml");
96
97         // when
98         CbsClientConfiguration configuration = CbsClientConfiguration.fromEnvironment();
99
100         // then
101         assertThat(configuration).isNotNull();
102         assertThat(configuration.configMapFilePath()).isEqualTo("/new/config/path/application.yaml");
103     }
104
105     @Test
106     void fromEnvironment_shouldReturnConfigurationWithCorrectPolicyPath_when_CBS_CLIENT_POLICY_PATH_isSet() {
107         // given
108         createBasicValidEnvsConfiguration();
109         envs.set(ENV_CBS_CLIENT_POLICY_PATH, "/new/config/path/policy.json");
110
111         // when
112         CbsClientConfiguration configuration = CbsClientConfiguration.fromEnvironment();
113
114         // then
115         assertThat(configuration).isNotNull();
116         assertThat(configuration.policySyncFilePath()).isEqualTo("/new/config/path/policy.json");
117     }
118
119     @Test
120     void fromEnvironment_shouldReturnConfigurationWithDefaultPolicyAndConfigPaths_whenEnvsNotSet() {
121         // given
122         createBasicValidEnvsConfiguration();
123
124         // when
125         CbsClientConfiguration configuration = CbsClientConfiguration.fromEnvironment();
126
127         // then
128         assertThat(configuration).isNotNull();
129         assertThat(configuration.configMapFilePath()).isEqualTo("/app-config/application_config.yaml");
130         assertThat(configuration.policySyncFilePath()).isEqualTo("/etc/policies/policies.json");
131     }
132
133     @Test
134     void fromEnvironment_shouldReturn_CbsClientConfigurationException_When_DCAE_CA_CERTPATH_is_Null() {
135         // given
136         envs.set(ENV_DCAE_CA_CERTPATH, null);
137         envs.set(ENV_CONFIG_BINDING_SERVICE_SERVICE_PORT, "9090");
138         envs.set(ENV_CONFIG_BINDING_SERVICE, "config-binding-service");
139         envs.set(ENV_HOSTNAME, "dcae-prh");
140         envs.set(ENV_CONSUL_HOST, "consul-server.onap");
141
142         // when
143         CbsClientConfiguration configuration = CbsClientConfiguration.fromEnvironment();
144
145         // then
146         assertThat(configuration.trustStoreKeys()).isNull();
147         assertThat(configuration.protocol()).isEqualTo("http");
148     }
149
150     @Test
151     void fromEnvironment_shouldReturn_CbsClientConfigurationException_WhenAllEnvVariablesAreMissing() {
152         assertThatExceptionOfType(CbsClientConfigurationException.class)
153                 .isThrownBy(CbsClientConfiguration::fromEnvironment);
154     }
155
156     @Test
157     void fromEnvironment_shouldReturn_CbsClientConfigurationException_When_DCAE_CA_CERTPATH_isWrong() {
158         // given
159         envs.set(ENV_DCAE_CA_CERTPATH, "/home/cacert.pem");
160         envs.set(ENV_HOSTNAME, "dcae-prh");
161         envs.set(ENV_CONFIG_BINDING_SERVICE, "config-binding-service");
162         envs.set(ENV_CONSUL_HOST, "consul-server.onap");
163
164         // then
165         assertThatExceptionOfType(CbsClientConfigurationException.class)
166                 .isThrownBy(CbsClientConfiguration::fromEnvironment)
167                 .withMessageContaining("Required files do not exist in /home directory");
168     }
169
170     @Test
171     void fromEnvironment_shouldReturn_CbsClientConfigurationException_When_HOSTNAME_isMissing() throws URISyntaxException {
172         // given
173         envs.set(ENV_HOSTNAME, "");
174         envs.set(ENV_DCAE_CA_CERTPATH, preparePathToCertFile());
175         envs.set(ENV_CONFIG_BINDING_SERVICE, "config-binding-service");
176         envs.set(ENV_CONSUL_HOST, "consul-server.onap");
177
178         // then
179         assertThatExceptionOfType(CbsClientConfigurationException.class)
180                 .isThrownBy(CbsClientConfiguration::fromEnvironment)
181                 .withMessageContaining("Cannot read HOSTNAME from environment.");
182     }
183
184     @Test
185     void fromEnvironment_shouldReturn_CbsClientConfigurationException_When_CONFIG_BINDING_SERVICE_SERVICE_PORT_isEmpty() {
186         // given
187         envs.set(ENV_CONFIG_BINDING_SERVICE_SERVICE_PORT, "");
188         envs.set(ENV_DCAE_CA_CERTPATH, "");
189         envs.set(ENV_HOSTNAME, "dcae-prh");
190         envs.set(ENV_CONFIG_BINDING_SERVICE, "config-binding-service");
191         envs.set(ENV_CONSUL_HOST, "consul-server.onap");
192
193         // then
194         assertThatExceptionOfType(CbsClientConfigurationException.class)
195                 .isThrownBy(CbsClientConfiguration::fromEnvironment)
196                 .withMessageContaining("Cannot read CONFIG_BINDING_SERVICE_SERVICE_PORT from environment.");
197     }
198
199     private void createBasicValidEnvsConfiguration() {
200         envs.set(ENV_CONFIG_BINDING_SERVICE, "config-binding-service");
201         envs.set(ENV_CONFIG_BINDING_SERVICE_SERVICE_PORT, "10000");
202         envs.set(ENV_HOSTNAME, "dcae-prh");
203         envs.set(ENV_CONSUL_HOST, "consul-server.onap");
204     }
205
206     private String preparePathToCertFile() throws URISyntaxException {
207         return Paths.get(Passwords.class.getResource("/test-certs/cacert.pem").toURI()) + "";
208     }
209 }