d0df0b6c9de6f288c7bcbe7d697284a59ae67ee5
[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("CONFIG_BINDING_SERVICE_PORT_10443_TCP_PORT", "10443");
69         envs.set("HOSTNAME", "dcae-prh");
70         envs.set("CONSUL_HOST", "consul-server.onap");
71
72         // when
73         CbsClientConfiguration configuration = CbsClientConfiguration.fromEnvironment();
74
75         // then
76         assertThat(configuration.trustStoreKeys()).isNotNull();
77         assertThat(configuration.protocol()).isEqualTo("https");
78     }
79
80     @Test
81     void fromEnvironment_shouldReturn_CbsClientConfigurationException_When_DCAE_CA_CERTPATH_is_Null() {
82         // given
83         envs.set("DCAE_CA_CERTPATH", null);
84         envs.set("CONFIG_BINDING_SERVICE_SERVICE_PORT", "9090");
85         envs.set("CONFIG_BINDING_SERVICE", "config-binding-service");
86         envs.set("CONFIG_BINDING_SERVICE_PORT_10443_TCP_PORT", "10443");
87         envs.set("HOSTNAME", "dcae-prh");
88         envs.set("CONSUL_HOST", "consul-server.onap");
89
90         // when
91         CbsClientConfiguration configuration = CbsClientConfiguration.fromEnvironment();
92
93         // then
94         assertThat(configuration.trustStoreKeys()).isNull();
95         assertThat(configuration.protocol()).isEqualTo("http");
96     }
97
98     @Test
99     void fromEnvironment_shouldReturn_CbsClientConfigurationException_WhenAllEnvVariablesAreMissing() {
100         assertThatExceptionOfType(CbsClientConfigurationException.class)
101                 .isThrownBy(CbsClientConfiguration::fromEnvironment);
102     }
103
104     @Test
105     void fromEnvironment_shouldReturn_CbsClientConfigurationException_When_DCAE_CA_CERTPATH_isWrong() {
106         // given
107         envs.set("DCAE_CA_CERTPATH", "/home/cacert.pem");
108         envs.set("HOSTNAME", "dcae-prh");
109         envs.set("CONFIG_BINDING_SERVICE", "config-binding-service");
110         envs.set("CONFIG_BINDING_SERVICE_PORT_10443_TCP_PORT", "10443");
111         envs.set("CONSUL_HOST", "consul-server.onap");
112
113         // then
114         assertThatExceptionOfType(CbsClientConfigurationException.class)
115                 .isThrownBy(CbsClientConfiguration::fromEnvironment)
116                 .withMessageContaining("Required files do not exist in /home directory");
117     }
118
119     @Test
120     void fromEnvironment_shouldReturn_CbsClientConfigurationException_When_HOSTNAME_isMissing() throws URISyntaxException {
121         // given
122         envs.set("HOSTNAME", "");
123         envs.set("DCAE_CA_CERTPATH", preparePathToCertFile());
124         envs.set("CONFIG_BINDING_SERVICE", "config-binding-service");
125         envs.set("CONFIG_BINDING_SERVICE_PORT_10443_TCP_PORT", "10443");
126         envs.set("CONSUL_HOST", "consul-server.onap");
127
128         // then
129         assertThatExceptionOfType(CbsClientConfigurationException.class)
130                 .isThrownBy(CbsClientConfiguration::fromEnvironment)
131                 .withMessageContaining("Cannot read HOSTNAME from environment.");
132     }
133
134     @Test
135     void fromEnvironment_shouldReturn_CbsClientConfigurationException_When_CONFIG_BINDING_SERVICE_SERVICE_PORT_isEmpty() {
136         // given
137         envs.set("CONFIG_BINDING_SERVICE_SERVICE_PORT", "");
138         envs.set("DCAE_CA_CERTPATH", "");
139         envs.set("HOSTNAME", "dcae-prh");
140         envs.set("CONFIG_BINDING_SERVICE", "config-binding-service");
141         envs.set("CONSUL_HOST", "consul-server.onap");
142
143         // then
144         assertThatExceptionOfType(CbsClientConfigurationException.class)
145                 .isThrownBy(CbsClientConfiguration::fromEnvironment)
146                 .withMessageContaining("Cannot read CONFIG_BINDING_SERVICE_SERVICE_PORT from environment.");
147     }
148
149     private String preparePathToCertFile() throws URISyntaxException {
150         return Paths.get(Passwords.class.getResource("/test-certs/cacert.pem").toURI()) + "";
151     }
152 }