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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=====================================
21 package org.onap.dcaegen2.services.sdk.rest.services.cbs.client.api;
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;
32 import java.net.URISyntaxException;
33 import java.nio.file.Paths;
35 import static org.assertj.core.api.Assertions.assertThat;
36 import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
39 * @author <a href="mailto:piotr.jaszczyk@nokia.com">Piotr Jaszczyk</a>
40 * @since February 2019
42 class CbsClientConfigurationTest {
44 private static final String ENV_DCAE_CA_CERTPATH = "DCAE_CA_CERTPATH";
45 private static final String ENV_CONFIG_BINDING_SERVICE = "CONFIG_BINDING_SERVICE";
46 private static final String ENV_CONFIG_BINDING_SERVICE_SERVICE_PORT = "CONFIG_BINDING_SERVICE_SERVICE_PORT";
47 private static final String ENV_HOSTNAME = "HOSTNAME";
48 private static final String ENV_CONSUL_HOST = "CONSUL_HOST";
49 private static final String ENV_CBS_CLIENT_CONFIG_PATH = "CBS_CLIENT_CONFIG_PATH";
50 private static final String ENV_CBS_CLIENT_POLICY_PATH = "CBS_CLIENT_POLICY_PATH";
53 public final EnvironmentVariables envs = new EnvironmentVariables();
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);
62 void fromEnvironment_shouldReturnConfigurationForConnectionWithoutTls_when_DCAE_CA_CERTPATH_isEmpty() {
64 createBasicValidEnvsConfiguration();
65 envs.set(ENV_DCAE_CA_CERTPATH, "");
68 CbsClientConfiguration configuration = CbsClientConfiguration.fromEnvironment();
71 assertThat(configuration.trustStoreKeys()).isEqualTo(null);
72 assertThat(configuration.protocol()).isEqualTo("http");
76 void fromEnvironment_shouldReturnConfigurationForConnectionOverTls_when_DCAE_CA_CERTPATH_isSet() throws URISyntaxException {
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");
84 CbsClientConfiguration configuration = CbsClientConfiguration.fromEnvironment();
87 assertThat(configuration.trustStoreKeys()).isNotNull();
88 assertThat(configuration.protocol()).isEqualTo("https");
92 void fromEnvironment_shouldReturnConfigurationWithCorrectConfigPath_when_CBS_CLIENT_CONFIG_PATH_isSet() {
94 createBasicValidEnvsConfiguration();
95 envs.set(ENV_CBS_CLIENT_CONFIG_PATH, "/new/config/path/application.yaml");
98 CbsClientConfiguration configuration = CbsClientConfiguration.fromEnvironment();
101 assertThat(configuration).isNotNull();
102 assertThat(configuration.configMapFilePath()).isEqualTo("/new/config/path/application.yaml");
106 void fromEnvironment_shouldReturnConfigurationWithCorrectPolicyPath_when_CBS_CLIENT_POLICY_PATH_isSet() {
108 createBasicValidEnvsConfiguration();
109 envs.set(ENV_CBS_CLIENT_POLICY_PATH, "/new/config/path/policy.json");
112 CbsClientConfiguration configuration = CbsClientConfiguration.fromEnvironment();
115 assertThat(configuration).isNotNull();
116 assertThat(configuration.policySyncFilePath()).isEqualTo("/new/config/path/policy.json");
120 void fromEnvironment_shouldReturnConfigurationWithDefaultPolicyAndConfigPaths_whenEnvsNotSet() {
122 createBasicValidEnvsConfiguration();
125 CbsClientConfiguration configuration = CbsClientConfiguration.fromEnvironment();
128 assertThat(configuration).isNotNull();
129 assertThat(configuration.configMapFilePath()).isEqualTo("/app-config/application_config.yaml");
130 assertThat(configuration.policySyncFilePath()).isEqualTo("/etc/policies/policies.json");
134 void fromEnvironment_shouldReturn_CbsClientConfigurationException_When_DCAE_CA_CERTPATH_is_Null() {
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");
143 CbsClientConfiguration configuration = CbsClientConfiguration.fromEnvironment();
146 assertThat(configuration.trustStoreKeys()).isNull();
147 assertThat(configuration.protocol()).isEqualTo("http");
151 void fromEnvironment_shouldReturn_CbsClientConfigurationException_WhenAllEnvVariablesAreMissing() {
152 assertThatExceptionOfType(CbsClientConfigurationException.class)
153 .isThrownBy(CbsClientConfiguration::fromEnvironment);
157 void fromEnvironment_shouldReturn_CbsClientConfigurationException_When_DCAE_CA_CERTPATH_isWrong() {
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");
165 assertThatExceptionOfType(CbsClientConfigurationException.class)
166 .isThrownBy(CbsClientConfiguration::fromEnvironment)
167 .withMessageContaining("Required files do not exist in /home directory");
171 void fromEnvironment_shouldReturn_CbsClientConfigurationException_When_HOSTNAME_isMissing() throws URISyntaxException {
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");
179 assertThatExceptionOfType(CbsClientConfigurationException.class)
180 .isThrownBy(CbsClientConfiguration::fromEnvironment)
181 .withMessageContaining("Cannot read HOSTNAME from environment.");
185 void fromEnvironment_shouldReturn_CbsClientConfigurationException_When_CONFIG_BINDING_SERVICE_SERVICE_PORT_isEmpty() {
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");
194 assertThatExceptionOfType(CbsClientConfigurationException.class)
195 .isThrownBy(CbsClientConfiguration::fromEnvironment)
196 .withMessageContaining("Cannot read CONFIG_BINDING_SERVICE_SERVICE_PORT from environment.");
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");
206 private String preparePathToCertFile() throws URISyntaxException {
207 return Paths.get(Passwords.class.getResource("/test-certs/cacert.pem").toURI()) + "";