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("CONFIG_BINDING_SERVICE_PORT_10443_TCP_PORT", "10443");
69 envs.set("HOSTNAME", "dcae-prh");
70 envs.set("CONSUL_HOST", "consul-server.onap");
73 CbsClientConfiguration configuration = CbsClientConfiguration.fromEnvironment();
76 assertThat(configuration.trustStoreKeys()).isNotNull();
77 assertThat(configuration.protocol()).isEqualTo("https");
81 void fromEnvironment_shouldReturn_CbsClientConfigurationException_When_DCAE_CA_CERTPATH_is_Null() {
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");
91 CbsClientConfiguration configuration = CbsClientConfiguration.fromEnvironment();
94 assertThat(configuration.trustStoreKeys()).isNull();
95 assertThat(configuration.protocol()).isEqualTo("http");
99 void fromEnvironment_shouldReturn_CbsClientConfigurationException_WhenAllEnvVariablesAreMissing() {
100 assertThatExceptionOfType(CbsClientConfigurationException.class)
101 .isThrownBy(CbsClientConfiguration::fromEnvironment);
105 void fromEnvironment_shouldReturn_CbsClientConfigurationException_When_DCAE_CA_CERTPATH_isWrong() {
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");
114 assertThatExceptionOfType(CbsClientConfigurationException.class)
115 .isThrownBy(CbsClientConfiguration::fromEnvironment)
116 .withMessageContaining("Required files do not exist in /home directory");
120 void fromEnvironment_shouldReturn_CbsClientConfigurationException_When_HOSTNAME_isMissing() throws URISyntaxException {
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");
129 assertThatExceptionOfType(CbsClientConfigurationException.class)
130 .isThrownBy(CbsClientConfiguration::fromEnvironment)
131 .withMessageContaining("Cannot read HOSTNAME from environment.");
135 void fromEnvironment_shouldReturn_CbsClientConfigurationException_When_CONFIG_BINDING_SERVICE_SERVICE_PORT_isEmpty() {
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");
144 assertThatExceptionOfType(CbsClientConfigurationException.class)
145 .isThrownBy(CbsClientConfiguration::fromEnvironment)
146 .withMessageContaining("Cannot read CONFIG_BINDING_SERVICE_SERVICE_PORT from environment.");
149 private String preparePathToCertFile() throws URISyntaxException {
150 return Paths.get(Passwords.class.getResource("/test-certs/cacert.pem").toURI()) + "";