2 * ========================LICENSE_START=================================
5 * Copyright (C) 2020 Nordix Foundation
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.oransc.policyagent.tasks;
23 import static org.assertj.core.api.Assertions.assertThat;
25 import ch.qos.logback.classic.Level;
26 import ch.qos.logback.classic.spi.ILoggingEvent;
27 import ch.qos.logback.core.read.ListAppender;
29 import java.util.Properties;
31 import org.junit.jupiter.api.Test;
32 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.EnvProperties;
33 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.ImmutableEnvProperties;
34 import org.oransc.policyagent.exceptions.EnvironmentLoaderException;
35 import org.oransc.policyagent.utils.LoggingUtils;
36 import reactor.test.StepVerifier;
38 class EnvironmentProcessorTest {
39 private static final String CONSUL_HOST = "CONSUL_HOST";
40 private static final String CONSUL_HOST_VALUE = "consulHost";
42 private static final String CONFIG_BINDING_SERVICE = "CONFIG_BINDING_SERVICE";
43 private static final String CONFIG_BINDING_SERVICE_VALUE = "configBindingService";
45 private static final String HOSTNAME = "HOSTNAME";
46 private static final String HOSTNAME_VALUE = "hostname";
49 void allPropertiesAvailableWithHostname_thenAllPropertiesAreReturnedWithGivenConsulPort() {
50 Properties systemEnvironment = new Properties();
51 String consulPort = "8080";
52 systemEnvironment.put(CONSUL_HOST, CONSUL_HOST_VALUE);
53 systemEnvironment.put("CONSUL_PORT", consulPort);
54 systemEnvironment.put(CONFIG_BINDING_SERVICE, CONFIG_BINDING_SERVICE_VALUE);
55 systemEnvironment.put(HOSTNAME, HOSTNAME_VALUE);
57 EnvProperties expectedEnvProperties = ImmutableEnvProperties.builder() //
58 .consulHost(CONSUL_HOST_VALUE) //
59 .consulPort(Integer.valueOf(consulPort)) //
60 .cbsName(CONFIG_BINDING_SERVICE_VALUE) //
61 .appName(HOSTNAME_VALUE) //
64 StepVerifier.create(EnvironmentProcessor.readEnvironmentVariables(systemEnvironment))
65 .expectNext(expectedEnvProperties).expectComplete();
69 void consulHostMissing_thenExceptionReturned() {
70 Properties systemEnvironment = new Properties();
72 StepVerifier.create(EnvironmentProcessor.readEnvironmentVariables(systemEnvironment))
73 .expectErrorMatches(throwable -> throwable instanceof EnvironmentLoaderException
74 && throwable.getMessage().equals("$CONSUL_HOST environment has not been defined"))
79 void withAllPropertiesExceptConsulPort_thenAllPropertiesAreReturnedWithDefaultConsulPortAndWarning() {
80 Properties systemEnvironment = new Properties();
81 systemEnvironment.put(CONSUL_HOST, CONSUL_HOST_VALUE);
82 systemEnvironment.put(CONFIG_BINDING_SERVICE, CONFIG_BINDING_SERVICE_VALUE);
83 systemEnvironment.put(HOSTNAME, HOSTNAME_VALUE);
85 String defaultConsulPort = "8500";
86 EnvProperties expectedEnvProperties = ImmutableEnvProperties.builder() //
87 .consulHost(CONSUL_HOST_VALUE) //
88 .consulPort(Integer.valueOf(defaultConsulPort)) //
89 .cbsName(CONFIG_BINDING_SERVICE_VALUE) //
90 .appName(HOSTNAME_VALUE) //
93 final ListAppender<ILoggingEvent> logAppender = LoggingUtils.getLogListAppender(EnvironmentProcessor.class);
95 StepVerifier.create(EnvironmentProcessor.readEnvironmentVariables(systemEnvironment))
96 .expectNext(expectedEnvProperties).expectComplete();
98 assertThat(logAppender.list.get(0).getLevel()).isEqualTo(Level.WARN);
99 assertThat(logAppender.list.toString()
100 .contains("$CONSUL_PORT variable will be set to default port " + defaultConsulPort)).isTrue();
104 void configBindingServiceMissing_thenExceptionReturned() {
105 Properties systemEnvironment = new Properties();
106 systemEnvironment.put(CONSUL_HOST, CONSUL_HOST_VALUE);
108 StepVerifier.create(EnvironmentProcessor.readEnvironmentVariables(systemEnvironment))
109 .expectErrorMatches(throwable -> throwable instanceof EnvironmentLoaderException
110 && throwable.getMessage().equals("$CONFIG_BINDING_SERVICE environment has not been defined"))
115 void allPropertiesAvailableWithServiceName_thenAllPropertiesAreReturned() {
116 Properties systemEnvironment = new Properties();
117 String consulPort = "8080";
118 systemEnvironment.put(CONSUL_HOST, CONSUL_HOST_VALUE);
119 systemEnvironment.put("CONSUL_PORT", consulPort);
120 systemEnvironment.put(CONFIG_BINDING_SERVICE, CONFIG_BINDING_SERVICE_VALUE);
121 systemEnvironment.put("SERVICE_NAME", HOSTNAME_VALUE);
123 EnvProperties expectedEnvProperties = ImmutableEnvProperties.builder() //
124 .consulHost(CONSUL_HOST_VALUE) //
125 .consulPort(Integer.valueOf(consulPort)) //
126 .cbsName(CONFIG_BINDING_SERVICE_VALUE) //
127 .appName(HOSTNAME_VALUE) //
130 StepVerifier.create(EnvironmentProcessor.readEnvironmentVariables(systemEnvironment))
131 .expectNext(expectedEnvProperties).expectComplete();
135 void serviceNameAndHostnameMissing_thenExceptionIsReturned() {
136 Properties systemEnvironment = new Properties();
137 systemEnvironment.put(CONSUL_HOST, CONSUL_HOST_VALUE);
138 systemEnvironment.put(CONFIG_BINDING_SERVICE, CONFIG_BINDING_SERVICE_VALUE);
140 StepVerifier.create(EnvironmentProcessor.readEnvironmentVariables(systemEnvironment))
141 .expectErrorMatches(throwable -> throwable instanceof EnvironmentLoaderException && throwable.getMessage()
142 .equals("Neither $HOSTNAME/$SERVICE_NAME have not been defined as system environment"))