107c2d327671cb93867a4f5e76d0dfa5f82f8c43
[dcaegen2/services/sdk.git] /
1 /*
2  * ============LICENSE_START====================================
3  * DCAEGEN2-SERVICES-SDK
4  * =========================================================
5  * Copyright (C) 2019 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.Test;
27 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.api.exceptions.CbsClientConfigurationException;
28 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.CbsClientConfiguration;
29 import org.onap.dcaegen2.services.sdk.security.ssl.Passwords;
30
31 import java.net.URISyntaxException;
32 import java.nio.file.Paths;
33
34 import static org.assertj.core.api.Assertions.assertThat;
35 import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
36
37 /**
38  * @author <a href="mailto:piotr.jaszczyk@nokia.com">Piotr Jaszczyk</a>
39  * @since February 2019
40  */
41 class CbsClientConfigurationTest {
42
43     @Rule
44     public final EnvironmentVariables envs = new EnvironmentVariables();
45
46     @Test
47     void fromEnvironment_shouldReturnConfigurationForConnectionWithoutTls_when_DCAE_CA_CERTPATH_isEmpty() {
48         // given
49         envs.set("DCAE_CA_CERTPATH", "");
50         envs.set("CONFIG_BINDING_SERVICE", "config-binding-service");
51         envs.set("CONFIG_BINDING_SERVICE_SERVICE_PORT", "10000");
52         envs.set("HOSTNAME", "dcae-prh");
53         envs.set("CONSUL_HOST", "consul-server.onap");
54
55         // when
56         CbsClientConfiguration configuration = CbsClientConfiguration.fromEnvironment();
57
58         // then
59         assertThat(configuration.trustStoreKeys()).isEqualTo(null);
60         assertThat(configuration.protocol()).isEqualTo("http");
61     }
62
63     @Test
64     void fromEnvironment_shouldReturnConfigurationForConnectionOverTls_when_DCAE_CA_CERTPATH_isSet() throws URISyntaxException {
65         // given
66         envs.set("DCAE_CA_CERTPATH", preparePathToCertFile());
67         envs.set("CONFIG_BINDING_SERVICE", "config-binding-service");
68         envs.set("HOSTNAME", "dcae-prh");
69         envs.set("CONSUL_HOST", "consul-server.onap");
70
71         // when
72         CbsClientConfiguration configuration = CbsClientConfiguration.fromEnvironment();
73
74         // then
75         assertThat(configuration.trustStoreKeys()).isNotNull();
76         assertThat(configuration.protocol()).isEqualTo("https");
77     }
78
79     @Test
80     void fromEnvironment_shouldReturn_CbsClientConfigurationException_When_DCAE_CA_CERTPATH_is_Null() {
81         // given
82         envs.set("DCAE_CA_CERTPATH", null);
83         envs.set("CONFIG_BINDING_SERVICE_SERVICE_PORT", "9090");
84         envs.set("CONFIG_BINDING_SERVICE", "config-binding-service");
85         envs.set("HOSTNAME", "dcae-prh");
86         envs.set("CONSUL_HOST", "consul-server.onap");
87
88         // when
89         CbsClientConfiguration configuration = CbsClientConfiguration.fromEnvironment();
90
91         // then
92         assertThat(configuration.trustStoreKeys()).isNull();
93         assertThat(configuration.protocol()).isEqualTo("http");
94     }
95
96     @Test
97     void fromEnvironment_shouldReturn_CbsClientConfigurationException_WhenAllEnvVariablesAreMissing() {
98         assertThatExceptionOfType(CbsClientConfigurationException.class)
99                 .isThrownBy(CbsClientConfiguration::fromEnvironment);
100     }
101
102     @Test
103     void fromEnvironment_shouldReturn_CbsClientConfigurationException_When_DCAE_CA_CERTPATH_isWrong() {
104         // given
105         envs.set("DCAE_CA_CERTPATH", "/home/cacert.pem");
106         envs.set("HOSTNAME", "dcae-prh");
107         envs.set("CONFIG_BINDING_SERVICE", "config-binding-service");
108         envs.set("CONSUL_HOST", "consul-server.onap");
109
110         // then
111         assertThatExceptionOfType(CbsClientConfigurationException.class)
112                 .isThrownBy(CbsClientConfiguration::fromEnvironment)
113                 .withMessageContaining("Required files do not exist in /home directory");
114     }
115
116     @Test
117     void fromEnvironment_shouldReturn_CbsClientConfigurationException_When_HOSTNAME_isMissing() throws URISyntaxException {
118         // given
119         envs.set("HOSTNAME", "");
120         envs.set("DCAE_CA_CERTPATH", preparePathToCertFile());
121         envs.set("CONFIG_BINDING_SERVICE", "config-binding-service");
122         envs.set("CONSUL_HOST", "consul-server.onap");
123
124         // then
125         assertThatExceptionOfType(CbsClientConfigurationException.class)
126                 .isThrownBy(CbsClientConfiguration::fromEnvironment)
127                 .withMessageContaining("Cannot read HOSTNAME from environment.");
128     }
129
130     @Test
131     void fromEnvironment_shouldReturn_CbsClientConfigurationException_When_CONFIG_BINDING_SERVICE_SERVICE_PORT_isEmpty() {
132         // given
133         envs.set("CONFIG_BINDING_SERVICE_SERVICE_PORT", "");
134         envs.set("DCAE_CA_CERTPATH", "");
135         envs.set("HOSTNAME", "dcae-prh");
136         envs.set("CONFIG_BINDING_SERVICE", "config-binding-service");
137         envs.set("CONSUL_HOST", "consul-server.onap");
138
139         // then
140         assertThatExceptionOfType(CbsClientConfigurationException.class)
141                 .isThrownBy(CbsClientConfiguration::fromEnvironment)
142                 .withMessageContaining("Cannot read CONFIG_BINDING_SERVICE_SERVICE_PORT from environment.");
143     }
144
145     private String preparePathToCertFile() throws URISyntaxException {
146         return Paths.get(Passwords.class.getResource("/test-certs/cacert.pem").toURI()) + "";
147     }
148 }