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
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.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;
31 import java.net.URISyntaxException;
32 import java.nio.file.Paths;
34 import static org.assertj.core.api.Assertions.assertThat;
35 import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
38 * @author <a href="mailto:piotr.jaszczyk@nokia.com">Piotr Jaszczyk</a>
39 * @since February 2019
41 class CbsClientConfigurationTest {
44 public final EnvironmentVariables envs = new EnvironmentVariables();
47 void fromEnvironment_shouldReturnConfigurationForConnectionWithoutTls_when_DCAE_CA_CERTPATH_isEmpty() {
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");
56 CbsClientConfiguration configuration = CbsClientConfiguration.fromEnvironment();
59 assertThat(configuration.trustStoreKeys()).isEqualTo(null);
60 assertThat(configuration.protocol()).isEqualTo("http");
64 void fromEnvironment_shouldReturnConfigurationForConnectionOverTls_when_DCAE_CA_CERTPATH_isSet() throws URISyntaxException {
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");
72 CbsClientConfiguration configuration = CbsClientConfiguration.fromEnvironment();
75 assertThat(configuration.trustStoreKeys()).isNotNull();
76 assertThat(configuration.protocol()).isEqualTo("https");
80 void fromEnvironment_shouldReturn_CbsClientConfigurationException_When_DCAE_CA_CERTPATH_is_Null() {
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");
89 CbsClientConfiguration configuration = CbsClientConfiguration.fromEnvironment();
92 assertThat(configuration.trustStoreKeys()).isNull();
93 assertThat(configuration.protocol()).isEqualTo("http");
97 void fromEnvironment_shouldReturn_CbsClientConfigurationException_WhenAllEnvVariablesAreMissing() {
98 assertThatExceptionOfType(CbsClientConfigurationException.class)
99 .isThrownBy(CbsClientConfiguration::fromEnvironment);
103 void fromEnvironment_shouldReturn_CbsClientConfigurationException_When_DCAE_CA_CERTPATH_isWrong() {
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");
111 assertThatExceptionOfType(CbsClientConfigurationException.class)
112 .isThrownBy(CbsClientConfiguration::fromEnvironment)
113 .withMessageContaining("Required files do not exist in /home directory");
117 void fromEnvironment_shouldReturn_CbsClientConfigurationException_When_HOSTNAME_isMissing() throws URISyntaxException {
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");
125 assertThatExceptionOfType(CbsClientConfigurationException.class)
126 .isThrownBy(CbsClientConfiguration::fromEnvironment)
127 .withMessageContaining("Cannot read HOSTNAME from environment.");
131 void fromEnvironment_shouldReturn_CbsClientConfigurationException_When_CONFIG_BINDING_SERVICE_SERVICE_PORT_isEmpty() {
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");
140 assertThatExceptionOfType(CbsClientConfigurationException.class)
141 .isThrownBy(CbsClientConfiguration::fromEnvironment)
142 .withMessageContaining("Cannot read CONFIG_BINDING_SERVICE_SERVICE_PORT from environment.");
145 private String preparePathToCertFile() throws URISyntaxException {
146 return Paths.get(Passwords.class.getResource("/test-certs/cacert.pem").toURI()) + "";