64a7a19eb390e9fb70144968d6233a4069bc1a55
[ccsdk/oran.git] /
1 /*-
2  * ========================LICENSE_START=================================
3  * ONAP : ccsdk oran
4  * ======================================================================
5  * Copyright (C) 2020 Nordix Foundation. 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.ccsdk.oran.a1policymanagementservice.tasks;
22
23 import static ch.qos.logback.classic.Level.WARN;
24 import static org.assertj.core.api.Assertions.assertThat;
25
26 import ch.qos.logback.classic.spi.ILoggingEvent;
27 import ch.qos.logback.core.read.ListAppender;
28
29 import java.util.Properties;
30
31 import org.junit.jupiter.api.Test;
32 import org.onap.ccsdk.oran.a1policymanagementservice.exceptions.ServiceException;
33 import org.onap.ccsdk.oran.a1policymanagementservice.utils.LoggingUtils;
34 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.EnvProperties;
35 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.ImmutableEnvProperties;
36 import reactor.test.StepVerifier;
37
38 class EnvironmentProcessorTest {
39     private static final String CONSUL_HOST = "CONSUL_HOST";
40     private static final String CONSUL_HOST_VALUE = "consulHost";
41
42     private static final String CONFIG_BINDING_SERVICE = "CONFIG_BINDING_SERVICE";
43     private static final String CONFIG_BINDING_SERVICE_VALUE = "configBindingService";
44
45     private static final String HOSTNAME = "HOSTNAME";
46     private static final String HOSTNAME_VALUE = "hostname";
47
48     @Test
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);
56
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) //
62                 .build();
63
64         StepVerifier.create(EnvironmentProcessor.readEnvironmentVariables(systemEnvironment))
65                 .expectNext(expectedEnvProperties).expectComplete();
66     }
67
68     @Test
69     void consulHostMissing_thenExceptionReturned() {
70         Properties systemEnvironment = new Properties();
71
72         StepVerifier.create(EnvironmentProcessor.readEnvironmentVariables(systemEnvironment))
73                 .expectErrorMatches(throwable -> throwable instanceof ServiceException
74                         && throwable.getMessage().equals("$CONSUL_HOST environment has not been defined"))
75                 .verify();
76     }
77
78     @Test
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);
84
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) //
91                 .build();
92
93         final ListAppender<ILoggingEvent> logAppender =
94                 LoggingUtils.getLogListAppender(EnvironmentProcessor.class, WARN);
95
96         StepVerifier.create(EnvironmentProcessor.readEnvironmentVariables(systemEnvironment))
97                 .expectNext(expectedEnvProperties).expectComplete();
98
99         assertThat(logAppender.list.get(0).getFormattedMessage())
100                 .isEqualTo("$CONSUL_PORT variable will be set to default port " + defaultConsulPort);
101     }
102
103     @Test
104     void configBindingServiceMissing_thenExceptionReturned() {
105         Properties systemEnvironment = new Properties();
106         systemEnvironment.put(CONSUL_HOST, CONSUL_HOST_VALUE);
107
108         StepVerifier.create(EnvironmentProcessor.readEnvironmentVariables(systemEnvironment))
109                 .expectErrorMatches(throwable -> throwable instanceof ServiceException
110                         && throwable.getMessage().equals("$CONFIG_BINDING_SERVICE environment has not been defined"))
111                 .verify();
112     }
113
114     @Test
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);
122
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) //
128                 .build();
129
130         StepVerifier.create(EnvironmentProcessor.readEnvironmentVariables(systemEnvironment))
131                 .expectNext(expectedEnvProperties).expectComplete();
132     }
133
134     @Test
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);
139
140         StepVerifier.create(EnvironmentProcessor.readEnvironmentVariables(systemEnvironment))
141                 .expectErrorMatches(throwable -> throwable instanceof ServiceException && throwable.getMessage()
142                         .equals("Neither $HOSTNAME/$SERVICE_NAME have not been defined as system environment"))
143                 .verify();
144     }
145 }