From: elinuxhenrik Date: Thu, 5 Mar 2020 08:32:27 +0000 (+0100) Subject: Adding tests for EnvironmentProcessor X-Git-Tag: 2.0.0~137^2 X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=commitdiff_plain;h=b95b0012093a0792ac97f4444bc4d9077ddd7f02;p=nonrtric.git Adding tests for EnvironmentProcessor Change-Id: I3218a6e9d976855ca11e7c2d25fe424656b245b0 Issue-ID: NONRTRIC-138 Signed-off-by: elinuxhenrik --- diff --git a/policy-agent/src/test/java/org/oransc/policyagent/tasks/EnvironmentProcessorTest.java b/policy-agent/src/test/java/org/oransc/policyagent/tasks/EnvironmentProcessorTest.java new file mode 100644 index 00000000..c22629b4 --- /dev/null +++ b/policy-agent/src/test/java/org/oransc/policyagent/tasks/EnvironmentProcessorTest.java @@ -0,0 +1,145 @@ +/*- + * ========================LICENSE_START================================= + * O-RAN-SC + * %% + * Copyright (C) 2020 Nordix Foundation + * %% + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ========================LICENSE_END=================================== + */ + +package org.oransc.policyagent.tasks; + +import static org.assertj.core.api.Assertions.assertThat; + +import ch.qos.logback.classic.Level; +import ch.qos.logback.classic.spi.ILoggingEvent; +import ch.qos.logback.core.read.ListAppender; + +import java.util.Properties; + +import org.junit.jupiter.api.Test; +import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.EnvProperties; +import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.ImmutableEnvProperties; +import org.oransc.policyagent.exceptions.EnvironmentLoaderException; +import org.oransc.policyagent.utils.LoggingUtils; +import reactor.test.StepVerifier; + +public class EnvironmentProcessorTest { + private static final String CONSUL_HOST = "CONSUL_HOST"; + private static final String CONSUL_HOST_VALUE = "consulHost"; + + private static final String CONFIG_BINDING_SERVICE = "CONFIG_BINDING_SERVICE"; + private static final String CONFIG_BINDING_SERVICE_VALUE = "configBindingService"; + + private static final String HOSTNAME = "HOSTNAME"; + private static final String HOSTNAME_VALUE = "hostname"; + + @Test + public void allPropertiesAvailableWithHostname_thenAllPropertiesAreReturnedWithGivenConsulPort() { + Properties systemEnvironment = new Properties(); + String consulPort = "8080"; + systemEnvironment.put(CONSUL_HOST, CONSUL_HOST_VALUE); + systemEnvironment.put("CONSUL_PORT", consulPort); + systemEnvironment.put(CONFIG_BINDING_SERVICE, CONFIG_BINDING_SERVICE_VALUE); + systemEnvironment.put(HOSTNAME, HOSTNAME_VALUE); + + EnvProperties expectedEnvProperties = ImmutableEnvProperties.builder() // + .consulHost(CONSUL_HOST_VALUE) // + .consulPort(Integer.valueOf(consulPort)) // + .cbsName(CONFIG_BINDING_SERVICE_VALUE) // + .appName(HOSTNAME_VALUE) // + .build(); + + StepVerifier.create(EnvironmentProcessor.readEnvironmentVariables(systemEnvironment)) + .expectNext(expectedEnvProperties).expectComplete(); + } + + @Test + public void consulHostMissing_thenExceptionReturned() { + Properties systemEnvironment = new Properties(); + + StepVerifier.create(EnvironmentProcessor.readEnvironmentVariables(systemEnvironment)) + .expectErrorMatches(throwable -> throwable instanceof EnvironmentLoaderException + && throwable.getMessage().equals("$CONSUL_HOST environment has not been defined")) + .verify(); + } + + @Test + public void withAllPropertiesExceptConsulPort_thenAllPropertiesAreReturnedWithDefaultConsulPortAndWarning() { + Properties systemEnvironment = new Properties(); + systemEnvironment.put(CONSUL_HOST, CONSUL_HOST_VALUE); + systemEnvironment.put(CONFIG_BINDING_SERVICE, CONFIG_BINDING_SERVICE_VALUE); + systemEnvironment.put(HOSTNAME, HOSTNAME_VALUE); + + String defaultConsulPort = "8500"; + EnvProperties expectedEnvProperties = ImmutableEnvProperties.builder() // + .consulHost(CONSUL_HOST_VALUE) // + .consulPort(Integer.valueOf(defaultConsulPort)) // + .cbsName(CONFIG_BINDING_SERVICE_VALUE) // + .appName(HOSTNAME_VALUE) // + .build(); + + final ListAppender logAppender = LoggingUtils.getLogListAppender(EnvironmentProcessor.class); + + StepVerifier.create(EnvironmentProcessor.readEnvironmentVariables(systemEnvironment)) + .expectNext(expectedEnvProperties).expectComplete(); + + assertThat(logAppender.list.get(0).getLevel()).isEqualTo(Level.WARN); + assertThat(logAppender.list.toString() + .contains("$CONSUL_PORT variable will be set to default port " + defaultConsulPort)).isTrue(); + } + + @Test + public void configBindingServiceMissing_thenExceptionReturned() { + Properties systemEnvironment = new Properties(); + systemEnvironment.put(CONSUL_HOST, CONSUL_HOST_VALUE); + + StepVerifier.create(EnvironmentProcessor.readEnvironmentVariables(systemEnvironment)) + .expectErrorMatches(throwable -> throwable instanceof EnvironmentLoaderException + && throwable.getMessage().equals("$CONFIG_BINDING_SERVICE environment has not been defined")) + .verify(); + } + + @Test + public void allPropertiesAvailableWithServiceName_thenAllPropertiesAreReturned() { + Properties systemEnvironment = new Properties(); + String consulPort = "8080"; + systemEnvironment.put(CONSUL_HOST, CONSUL_HOST_VALUE); + systemEnvironment.put("CONSUL_PORT", consulPort); + systemEnvironment.put(CONFIG_BINDING_SERVICE, CONFIG_BINDING_SERVICE_VALUE); + systemEnvironment.put("SERVICE_NAME", HOSTNAME_VALUE); + + EnvProperties expectedEnvProperties = ImmutableEnvProperties.builder() // + .consulHost(CONSUL_HOST_VALUE) // + .consulPort(Integer.valueOf(consulPort)) // + .cbsName(CONFIG_BINDING_SERVICE_VALUE) // + .appName(HOSTNAME_VALUE) // + .build(); + + StepVerifier.create(EnvironmentProcessor.readEnvironmentVariables(systemEnvironment)) + .expectNext(expectedEnvProperties).expectComplete(); + } + + @Test + public void serviceNameAndHostnameMissing_thenExceptionIsReturned() { + Properties systemEnvironment = new Properties(); + systemEnvironment.put(CONSUL_HOST, CONSUL_HOST_VALUE); + systemEnvironment.put(CONFIG_BINDING_SERVICE, CONFIG_BINDING_SERVICE_VALUE); + + StepVerifier.create(EnvironmentProcessor.readEnvironmentVariables(systemEnvironment)) + .expectErrorMatches(throwable -> throwable instanceof EnvironmentLoaderException && throwable.getMessage() + .equals("Neither $HOSTNAME/$SERVICE_NAME have not been defined as system environment")) + .verify(); + } +}