summaryrefslogtreecommitdiffstats
path: root/src/test
diff options
context:
space:
mode:
Diffstat (limited to 'src/test')
-rw-r--r--src/test/java/org/onap/dcae/ApplicationSettingsTest.java28
-rw-r--r--src/test/java/org/onap/dcae/TestingUtilities.java94
-rw-r--r--src/test/java/org/onap/dcae/WiremockBasedTest.java68
-rw-r--r--src/test/java/org/onap/dcae/controller/ConfigCBSSourceTest.java151
-rw-r--r--src/test/java/org/onap/dcae/controller/ConfigFilesFacadeTest.java139
-rw-r--r--src/test/java/org/onap/dcae/controller/ConfigLoaderIntegrationE2ETest.java92
-rw-r--r--src/test/java/org/onap/dcae/controller/ConfigParsingTest.java72
-rw-r--r--src/test/java/org/onap/dcae/controller/EnvPropertiesReaderTest.java68
-rw-r--r--src/test/java/org/onap/dcae/vestest/TestFetchConfig.java81
-rw-r--r--src/test/java/org/onap/dcae/vestest/TestLoadDynamicConfig.java76
10 files changed, 708 insertions, 161 deletions
diff --git a/src/test/java/org/onap/dcae/ApplicationSettingsTest.java b/src/test/java/org/onap/dcae/ApplicationSettingsTest.java
index b483bcb6..2ac42080 100644
--- a/src/test/java/org/onap/dcae/ApplicationSettingsTest.java
+++ b/src/test/java/org/onap/dcae/ApplicationSettingsTest.java
@@ -149,6 +149,26 @@ public class ApplicationSettingsTest {
}
@Test
+ public void shouldReturnConfigurationUpdateInterval() throws IOException {
+ // when
+ int updateFrequency = fromTemporaryConfiguration("collector.dynamic.config.update.frequency=10")
+ .configurationUpdateFrequency();
+
+ // then
+ assertEquals(10, updateFrequency);
+ }
+
+ @Test
+ public void shouldReturnDefaultConfigurationUpdateInterval() throws IOException {
+ // when
+ int updateFrequency = fromTemporaryConfiguration()
+ .configurationUpdateFrequency();
+
+ // then
+ assertEquals(5, updateFrequency);
+ }
+
+ @Test
public void shouldReturnLocationOfThePasswordFile() throws IOException {
// when
String passwordFileLocation = fromTemporaryConfiguration("collector.keystore.passwordfile=/somewhere/password").keystorePasswordFileLocation();
@@ -207,7 +227,7 @@ public class ApplicationSettingsTest {
@Test
public void shouldReturnDMAAPConfigFileLocation() throws IOException {
// when
- String dmaapConfigFileLocation = fromTemporaryConfiguration("collector.dmaapfile=/somewhere/dmaapFile").cambriaConfigurationFileLocation();
+ String dmaapConfigFileLocation = fromTemporaryConfiguration("collector.dmaapfile=/somewhere/dmaapFile").dMaaPConfigurationFileLocation();
// then
assertEquals(sanitizePath("/somewhere/dmaapFile"), dmaapConfigFileLocation);
@@ -216,7 +236,7 @@ public class ApplicationSettingsTest {
@Test
public void shouldReturnDefaultDMAAPConfigFileLocation() throws IOException {
// when
- String dmaapConfigFileLocation = fromTemporaryConfiguration().cambriaConfigurationFileLocation();
+ String dmaapConfigFileLocation = fromTemporaryConfiguration().dMaaPConfigurationFileLocation();
// then
assertEquals(sanitizePath("etc/DmaapConfig.json"), dmaapConfigFileLocation);
@@ -390,7 +410,7 @@ public class ApplicationSettingsTest {
public void shouldReturnCambriaConfigurationFileLocation() throws IOException {
// when
String cambriaConfigurationFileLocation = fromTemporaryConfiguration("collector.dmaapfile=/somewhere/dmaapConfig")
- .cambriaConfigurationFileLocation();
+ .dMaaPConfigurationFileLocation();
// then
assertEquals(sanitizePath("/somewhere/dmaapConfig"), cambriaConfigurationFileLocation);
@@ -400,7 +420,7 @@ public class ApplicationSettingsTest {
public void shouldReturnDefaultCambriaConfigurationFileLocation() throws IOException {
// when
String cambriaConfigurationFileLocation = fromTemporaryConfiguration()
- .cambriaConfigurationFileLocation();
+ .dMaaPConfigurationFileLocation();
// then
assertEquals(sanitizePath("etc/DmaapConfig.json"), cambriaConfigurationFileLocation);
diff --git a/src/test/java/org/onap/dcae/TestingUtilities.java b/src/test/java/org/onap/dcae/TestingUtilities.java
new file mode 100644
index 00000000..0bbb6cc3
--- /dev/null
+++ b/src/test/java/org/onap/dcae/TestingUtilities.java
@@ -0,0 +1,94 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * org.onap.dcaegen2.collectors.ves
+ * ================================================================================
+ * Copyright (C) 2018 Nokia. All rights reserved.
+ * ================================================================================
+ * 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.onap.dcae;
+
+import static java.nio.file.Files.readAllBytes;
+import static org.assertj.core.api.Assertions.assertThat;
+
+import com.google.gson.JsonObject;
+import com.google.gson.JsonParser;
+import io.vavr.control.Try;
+import java.io.File;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import org.assertj.core.api.AbstractThrowableAssert;
+import org.assertj.core.api.Java6Assertions;
+import org.json.JSONObject;
+
+/**
+ * @author Pawel Szalapski (pawel.szalapski@nokia.com)
+ */
+public final class TestingUtilities {
+
+ private TestingUtilities() {
+ // utility class, no objects allowed
+ }
+
+ public static void assertJSONObjectsEqual(JSONObject o1, JSONObject o2) {
+ assertThat(o1.toString()).isEqualTo(o2.toString());
+ }
+
+ public static JSONObject readJSONFromFile(Path path) {
+ return rethrow(() -> new JSONObject(readFile(path)));
+ }
+
+ public static String readFile(Path path) {
+ return rethrow(() -> new String(readAllBytes(path)));
+ }
+
+ public static Path createTemporaryFile(String content) {
+ return rethrow(() -> {
+ File temp = File.createTempFile("ves-collector-tests-created-this-file", ".tmp");
+ temp.deleteOnExit();
+ Path filePath = Paths.get(temp.toString());
+ Files.write(filePath, content.getBytes());
+ return filePath;
+ });
+ }
+
+ /**
+ * Exception in test case usually means there is something wrong, it should never be catched, but rather thrown to
+ * be handled by JUnit framework.
+ */
+ private static <T> T rethrow(CheckedSupplier<T> supplier) {
+ try {
+ return supplier.get();
+ } catch (Exception e) {
+ throw new RuntimeException();
+ }
+ }
+
+ @FunctionalInterface
+ interface CheckedSupplier<T> {
+
+ T get() throws Exception;
+ }
+
+
+ public static void assertFailureHasInfo(Try any, String... msgPart) {
+ Java6Assertions.assertThat(any.isFailure()).isTrue();
+ AbstractThrowableAssert<?, ? extends Throwable> o = Java6Assertions.assertThat(any.getCause())
+ .hasCauseInstanceOf(Exception.class);
+ for (String s : msgPart) {
+ o.hasStackTraceContaining(s);
+ }
+ }
+}
diff --git a/src/test/java/org/onap/dcae/WiremockBasedTest.java b/src/test/java/org/onap/dcae/WiremockBasedTest.java
new file mode 100644
index 00000000..ae851259
--- /dev/null
+++ b/src/test/java/org/onap/dcae/WiremockBasedTest.java
@@ -0,0 +1,68 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * org.onap.dcaegen2.collectors.ves
+ * ================================================================================
+ * Copyright (C) 2018 Nokia. All rights reserved.
+ * ================================================================================
+ * 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.onap.dcae;
+
+import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
+import static com.github.tomakehurst.wiremock.client.WireMock.get;
+import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
+import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
+import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig;
+import static io.vavr.API.Map;
+
+import com.github.tomakehurst.wiremock.junit.WireMockRule;
+import io.vavr.collection.Map;
+import org.junit.Rule;
+
+/**
+ * @author Pawel Szalapski (pawel.szalapski@nokia.com)
+ */
+public class WiremockBasedTest {
+
+ @Rule
+ public WireMockRule wireMockRule = new WireMockRule(
+ wireMockConfig().dynamicPort().dynamicHttpsPort().keystorePath(null));
+
+ protected void stubConsulToReturnLocalAddressOfCBS() {
+ stubFor(get(urlEqualTo("/v1/catalog/service/CBSName"))
+ .willReturn(aResponse().withBody(validLocalCBSConf())));
+ }
+
+ protected void stubCBSToReturnAppConfig(String sampleConfigForVES) {
+ stubFor(get(urlEqualTo("/service_component/VESCollector"))
+ .willReturn(aResponse().withBody(sampleConfigForVES)));
+ }
+
+ protected Map<String, String> wiremockBasedEnvProps() {
+ return Map(
+ "CONSUL_HOST", "http://localhost",
+ "CONSUL_PORT", "" + wireMockRule.port(),
+ "HOSTNAME", "VESCollector",
+ "CONFIG_BINDING_SERVICE", "CBSName"
+ );
+ }
+
+ protected String validLocalCBSConf() {
+ return ""
+ + "[{ "
+ + "\"ServiceAddress\": \"http://localhost\","
+ + "\"ServicePort\":" + wireMockRule.port()
+ + "}]";
+ }
+}
diff --git a/src/test/java/org/onap/dcae/controller/ConfigCBSSourceTest.java b/src/test/java/org/onap/dcae/controller/ConfigCBSSourceTest.java
new file mode 100644
index 00000000..336788a9
--- /dev/null
+++ b/src/test/java/org/onap/dcae/controller/ConfigCBSSourceTest.java
@@ -0,0 +1,151 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * org.onap.dcaegen2.collectors.ves
+ * ================================================================================
+ * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2018 Nokia. All rights reserved.
+ * ================================================================================
+ * 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.onap.dcae.controller;
+
+import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
+import static com.github.tomakehurst.wiremock.client.WireMock.get;
+import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
+import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
+import static org.assertj.core.api.Java6Assertions.assertThat;
+import static org.onap.dcae.TestingUtilities.assertFailureHasInfo;
+import static org.onap.dcae.controller.ConfigSource.getAppConfig;
+
+import io.vavr.control.Try;
+import org.json.JSONObject;
+import org.junit.Test;
+import org.onap.dcae.WiremockBasedTest;
+
+public class ConfigCBSSourceTest extends WiremockBasedTest {
+
+ @Test
+ public void shouldReturnValidAppConfiguration() {
+ // given
+ String sampleConfigForVES = "{\"collector.port\": 8080}";
+
+ stubConsulToReturnLocalAddressOfCBS();
+ stubCBSToReturnAppConfig(sampleConfigForVES);
+
+ // when
+ Try<JSONObject> actual = tryToGetConfig();
+
+ // then
+ assertThat(actual.get().toString()).isEqualTo(new JSONObject(sampleConfigForVES).toString());
+ }
+
+ @Test
+ public void shouldReturnFailureOnFailureToCommunicateWithConsul() {
+ // given
+ stubFor(get(urlEqualTo("/v1/catalog/service/CBSName"))
+ .willReturn(aResponse().withStatus(400)));
+
+ // when
+ Try<JSONObject> actual = tryToGetConfig();
+
+ // then
+ assertFailureHasInfo(actual, "HTTP", "Consul", "400",
+ "http://localhost:" + wireMockRule.port() + "/v1/catalog/service/CBSName");
+ }
+
+ @Test
+ public void shouldReturnFailureOnBadJsonFromConsul() {
+ // given
+ stubFor(get(urlEqualTo("/v1/catalog/service/CBSName"))
+ .willReturn(aResponse().withStatus(200).withBody("[{")));
+
+ // when
+ Try<JSONObject> actual = tryToGetConfig();
+
+ // then
+ assertFailureHasInfo(actual, "JSON", "array", "[{");
+ }
+
+ @Test
+ public void shouldReturnFailureOnInvalidCatalogFormat() {
+ // given
+ String notAListCatalog = ""
+ + "{"
+ + "\"ServiceAddress\":\"http://localhost\","
+ + "\"ServicePort\":" + wireMockRule.port()
+ + "}";
+
+ stubFor(get(urlEqualTo("/v1/catalog/service/CBSName"))
+ .willReturn(aResponse().withStatus(200).withBody(notAListCatalog)));
+
+ // when
+ Try<JSONObject> actual = tryToGetConfig();
+
+ // then
+ assertFailureHasInfo(actual, "JSON", "array", notAListCatalog);
+ }
+
+
+ @Test
+ public void shouldReturnFailureIfConfigIsMissingRequiredProperties() {
+ // given
+ String actualConf = "{\"ServicePort\":" + wireMockRule.port() + "}";
+ String asCatalog = "[" + actualConf + "]";
+
+ stubFor(get(urlEqualTo("/v1/catalog/service/CBSName"))
+ .willReturn(aResponse().withStatus(200).withBody(asCatalog)));
+
+ // when
+ Try<JSONObject> actual = tryToGetConfig();
+
+ // then
+ assertFailureHasInfo(actual, "ServiceAddress", "ServicePort", "missing", actualConf);
+ }
+
+
+ @Test
+ public void shouldReturnFailureOnFailureToCommunicateWithCBS() {
+ // given
+ stubFor(get(urlEqualTo("/v1/catalog/service/CBSName"))
+ .willReturn(aResponse().withStatus(200).withBody(validLocalCBSConf())));
+ stubFor(get(urlEqualTo("/service_component/VESCollector"))
+ .willReturn(aResponse().withStatus(400)));
+
+ // when
+ Try<JSONObject> actual = tryToGetConfig();
+
+ // then
+ assertFailureHasInfo(actual, "HTTP", "CBS", "400",
+ "http://localhost:" + wireMockRule.port() + "/service_component/VESCollector");
+ }
+
+ @Test
+ public void shouldReturnFailureIfAppIsInvalidJsonDocument() {
+ // given
+ String invalidAppConf = "[$";
+ stubConsulToReturnLocalAddressOfCBS();
+ stubCBSToReturnAppConfig(invalidAppConf);
+
+ // when
+ Try<JSONObject> actual = tryToGetConfig();
+
+ // then
+ assertFailureHasInfo(actual, "JSON", "document", invalidAppConf);
+ }
+
+ private Try<JSONObject> tryToGetConfig() {
+ return getAppConfig(new EnvProps("http://localhost", wireMockRule.port(), "CBSName", "VESCollector"));
+ }
+}
+
diff --git a/src/test/java/org/onap/dcae/controller/ConfigFilesFacadeTest.java b/src/test/java/org/onap/dcae/controller/ConfigFilesFacadeTest.java
new file mode 100644
index 00000000..474a77c5
--- /dev/null
+++ b/src/test/java/org/onap/dcae/controller/ConfigFilesFacadeTest.java
@@ -0,0 +1,139 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * org.onap.dcaegen2.collectors.ves
+ * ================================================================================
+ * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2018 Nokia. All rights reserved.
+ * ================================================================================
+ * 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.onap.dcae.controller;
+
+import static io.vavr.API.Map;
+import static io.vavr.API.Some;
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.onap.dcae.TestingUtilities.assertFailureHasInfo;
+import static org.onap.dcae.TestingUtilities.assertJSONObjectsEqual;
+import static org.onap.dcae.TestingUtilities.createTemporaryFile;
+import static org.onap.dcae.TestingUtilities.readFile;
+import static org.onap.dcae.TestingUtilities.readJSONFromFile;
+
+import io.vavr.collection.Map;
+import io.vavr.control.Try;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import org.json.JSONObject;
+import org.junit.Test;
+
+public class ConfigFilesFacadeTest {
+
+ private static final Path NON_EXISTENT = Paths.get("/non-existent");
+ private static final ConfigFilesFacade TO_NON_EXISTENT_POINTING_FACADE = new ConfigFilesFacade(NON_EXISTENT,
+ NON_EXISTENT);
+
+ @Test
+ public void shouldReadPropertyFile() {
+ // given
+ Path temporaryFile = createTemporaryFile("some.property=10");
+
+ // when
+ ConfigFilesFacade configFilesFacade = new ConfigFilesFacade(temporaryFile, temporaryFile);
+
+ Try<Map<String, String>> propertiesConfigurations = configFilesFacade.readCollectorProperties();
+
+ // then
+ assertThat(propertiesConfigurations.isSuccess()).isTrue();
+ assertThat(propertiesConfigurations.get().containsKey("some.property")).isTrue();
+ assertThat(propertiesConfigurations.get().get("some.property")).isEqualTo(Some("10"));
+ }
+
+
+ @Test
+ public void shouldReadDMaaPFile() {
+ // given
+ Path temporaryFile = createTemporaryFile("{}");
+
+ // when
+ ConfigFilesFacade configFilesFacade = new ConfigFilesFacade(temporaryFile, temporaryFile);
+
+ Try<JSONObject> dMaaPConfiguration = configFilesFacade.readDMaaPConfiguration();
+
+ // then
+ assertThat(dMaaPConfiguration.isSuccess()).isTrue();
+ assertThat(dMaaPConfiguration.get().toString()).isEqualTo("{}");
+ }
+
+ @Test
+ public void shouldWriteDMaaPConf() {
+ // given
+ Path temporaryFile = createTemporaryFile("{}");
+ JSONObject desiredConf = new JSONObject("{\"key\": 1}");
+
+ // when
+ ConfigFilesFacade configFilesFacade = new ConfigFilesFacade(temporaryFile, temporaryFile);
+
+ Try<Void> propertiesConfigurations = configFilesFacade.writeDMaaPConfiguration(desiredConf);
+
+ // then
+ assertThat(propertiesConfigurations.isSuccess()).isTrue();
+ assertJSONObjectsEqual(readJSONFromFile(temporaryFile), desiredConf);
+ }
+
+
+ @Test
+ public void shouldWriteProperties() {
+ // given
+ Path temporaryFile = createTemporaryFile("{}");
+
+ // when
+ ConfigFilesFacade configFilesFacade = new ConfigFilesFacade(temporaryFile, temporaryFile);
+ Try<Void> propertiesConfigurations = configFilesFacade.writeProperties(Map("prop1", "hi"));
+
+ // then
+ assertThat(propertiesConfigurations.isSuccess()).isTrue();
+ assertThat(readFile(temporaryFile).trim()).isEqualTo("prop1 = hi");
+ }
+
+ @Test
+ public void shouldContainPropertiesPathInCaseOfFailures() {
+ Try<Map<String, String>> result = TO_NON_EXISTENT_POINTING_FACADE.readCollectorProperties();
+ assertThat(result.isFailure()).isTrue();
+ assertFailureHasInfo(result, NON_EXISTENT.toString());
+ }
+
+ @Test
+ public void shouldContainDMaaPPathPathInCaseOfFailures() {
+ Try<JSONObject> result = TO_NON_EXISTENT_POINTING_FACADE.readDMaaPConfiguration();
+ assertThat(result.isFailure()).isTrue();
+ assertFailureHasInfo(result, NON_EXISTENT.toString());
+ }
+
+ @Test
+ public void shouldContainPropertiesPathPathInCaseOfFailuresOnWrite() {
+ // given
+ Try<Void> result = TO_NON_EXISTENT_POINTING_FACADE.writeProperties(Map("any", "any"));
+ assertThat(result.isFailure()).isTrue();
+ assertFailureHasInfo(result, NON_EXISTENT.toString());
+ }
+
+ @Test
+ public void shouldContainDMaaPPathPathInCaseOfFailuresOnWrite() {
+ // given
+ Try<Void> result = TO_NON_EXISTENT_POINTING_FACADE.writeDMaaPConfiguration(new JSONObject());
+ assertThat(result.isFailure()).isTrue();
+ assertFailureHasInfo(result, NON_EXISTENT.toString());
+ }
+
+}
+
diff --git a/src/test/java/org/onap/dcae/controller/ConfigLoaderIntegrationE2ETest.java b/src/test/java/org/onap/dcae/controller/ConfigLoaderIntegrationE2ETest.java
new file mode 100644
index 00000000..09dca676
--- /dev/null
+++ b/src/test/java/org/onap/dcae/controller/ConfigLoaderIntegrationE2ETest.java
@@ -0,0 +1,92 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * org.onap.dcaegen2.collectors.ves
+ * ================================================================================
+ * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2018 Nokia. All rights reserved.
+ * ================================================================================
+ * 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.onap.dcae.controller;
+
+import static io.vavr.API.Map;
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.verifyZeroInteractions;
+import static org.mockito.Mockito.when;
+import static org.onap.dcae.TestingUtilities.createTemporaryFile;
+import static org.onap.dcae.TestingUtilities.readFile;
+import static org.onap.dcae.TestingUtilities.readJSONFromFile;
+import static org.onap.dcae.commonFunction.event.publishing.VavrUtils.f;
+
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import org.json.JSONObject;
+import org.junit.Test;
+import org.onap.dcae.WiremockBasedTest;
+import org.onap.dcae.commonFunction.event.publishing.DMaaPConfigurationParser;
+import org.onap.dcae.commonFunction.event.publishing.EventPublisher;
+
+public class ConfigLoaderIntegrationE2ETest extends WiremockBasedTest {
+
+ @Test
+ public void testSuccessfulE2EFlow() {
+ // given
+ Path dMaaPConfigFile = createTemporaryFile("{}");
+ Path collectorPropertiesFile = createTemporaryFile("");
+ Path dMaaPConfigSource = Paths.get("src/test/resources/testParseDMaaPCredentialsGen2.json");
+ JSONObject dMaaPConf = readJSONFromFile(dMaaPConfigSource);
+ stubConsulToReturnLocalAddressOfCBS();
+ stubCBSToReturnAppConfig(f("{\"collector.port\": 8080, \"streams_publishes\": %s}}", dMaaPConf));
+
+ EventPublisher eventPublisherMock = mock(EventPublisher.class);
+ ConfigFilesFacade configFilesFacade = new ConfigFilesFacade(dMaaPConfigFile, collectorPropertiesFile);
+
+ // when
+ ConfigLoader configLoader = new ConfigLoader(eventPublisherMock::reconfigure, configFilesFacade, ConfigSource::getAppConfig, () -> wiremockBasedEnvProps());
+ configLoader.updateConfig();
+
+ // then
+ assertThat(readJSONFromFile(dMaaPConfigSource).toString()).isEqualTo(dMaaPConf.toString());
+ assertThat(readFile(collectorPropertiesFile).trim()).isEqualTo("collector.port = 8080");
+ verify(eventPublisherMock, times(1)).reconfigure(
+ DMaaPConfigurationParser.parseToDomainMapping(dMaaPConf).get()
+ );
+ }
+
+ @Test
+ public void shouldNotReconfigureNotOverwriteIfConfigurationHasNotChanged() {
+ // given
+ Path dMaaPConfigFile = createTemporaryFile("{}");
+ Path collectorPropertiesFile = createTemporaryFile("");
+ JSONObject dMaaPConf = readJSONFromFile(Paths.get("src/test/resources/testParseDMaaPCredentialsGen2.json"));
+ stubConsulToReturnLocalAddressOfCBS();
+ stubCBSToReturnAppConfig(f("{\"collector.port\": 8080, \"streams_publishes\": %s}}", dMaaPConf));
+ EventPublisher eventPublisherMock = mock(EventPublisher.class);
+ ConfigFilesFacade configFilesFacade = new ConfigFilesFacade(dMaaPConfigFile, collectorPropertiesFile);
+ configFilesFacade.writeProperties(Map("collector.port", "8080"));
+ configFilesFacade.writeDMaaPConfiguration(dMaaPConf);
+
+ // when
+ ConfigLoader configLoader = new ConfigLoader(eventPublisherMock::reconfigure, configFilesFacade, ConfigSource::getAppConfig, () -> wiremockBasedEnvProps());
+ configLoader.updateConfig();
+
+ // then
+ verifyZeroInteractions(eventPublisherMock);
+ }
+
+} \ No newline at end of file
diff --git a/src/test/java/org/onap/dcae/controller/ConfigParsingTest.java b/src/test/java/org/onap/dcae/controller/ConfigParsingTest.java
new file mode 100644
index 00000000..a00a3d3b
--- /dev/null
+++ b/src/test/java/org/onap/dcae/controller/ConfigParsingTest.java
@@ -0,0 +1,72 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * org.onap.dcaegen2.collectors.ves
+ * ================================================================================
+ * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2018 Nokia. All rights reserved.
+ * ================================================================================
+ * 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.onap.dcae.controller;
+
+
+import static io.vavr.API.Map;
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.onap.dcae.TestingUtilities.assertJSONObjectsEqual;
+import static org.onap.dcae.TestingUtilities.readJSONFromFile;
+
+import io.vavr.collection.Map;
+import io.vavr.control.Option;
+import java.nio.file.Paths;
+import org.json.JSONObject;
+import org.junit.Test;
+
+public class ConfigParsingTest {
+
+ @Test
+ public void shouldReturnDMaaPConfig() {
+ JSONObject dMaaPConf = readJSONFromFile(Paths.get("src/test/resources/testParseDMaaPCredentialsGen2.json"));
+ JSONObject root = new JSONObject();
+ root.put("key1", "someProperty");
+ root.put("key2", "someProperty");
+ root.put("streams_publishes", dMaaPConf);
+
+ Option<JSONObject> dMaaPConfig = ConfigParsing.getDMaaPConfig(root);
+
+ assertThat(dMaaPConfig.isEmpty()).isFalse();
+ assertJSONObjectsEqual(dMaaPConfig.get(), dMaaPConf);
+ }
+
+ @Test
+ public void shouldReturnEmptyIfDMaaPConfigIsInvalid() {
+ JSONObject root = new JSONObject();
+ root.put("streams_publishes", 1);
+
+ Option<JSONObject> dMaaPConfig = ConfigParsing.getDMaaPConfig(root);
+
+ assertThat(dMaaPConfig.isEmpty()).isTrue();
+ }
+
+ @Test
+ public void getProperties() {
+ JSONObject dMaaPConf = readJSONFromFile(Paths.get("src/test/resources/testParseDMaaPCredentialsGen2.json"));
+ JSONObject root = new JSONObject();
+ root.put("key1", "someProperty");
+ root.put("key2", "someProperty");
+ root.put("streams_publishes", dMaaPConf);
+
+ Map<String, String> properties = ConfigParsing.getProperties(root);
+ assertThat(properties).isEqualTo(Map("key1", "someProperty", "key2", "someProperty"));
+ }
+} \ No newline at end of file
diff --git a/src/test/java/org/onap/dcae/controller/EnvPropertiesReaderTest.java b/src/test/java/org/onap/dcae/controller/EnvPropertiesReaderTest.java
new file mode 100644
index 00000000..581f6eae
--- /dev/null
+++ b/src/test/java/org/onap/dcae/controller/EnvPropertiesReaderTest.java
@@ -0,0 +1,68 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * org.onap.dcaegen2.collectors.ves
+ * ================================================================================
+ * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2018 Nokia. All rights reserved.
+ * ================================================================================
+ * 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.onap.dcae.controller;
+
+import static io.vavr.API.Map;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+import static org.onap.dcae.controller.EnvPropertiesReader.readEnvProps;
+
+import io.vavr.collection.Map;
+import org.junit.Test;
+
+
+public class EnvPropertiesReaderTest {
+
+ @Test
+ public void shouldReturnEmptyOnMissingConsulHost() {
+ Map<String, String> envs = Map(
+ "CONFIG_BINDING_SERVICE", "doesNotMatter",
+ "HOSTNAME", "doesNotMatter");
+ assertTrue(readEnvProps(envs).isEmpty());
+ }
+
+ @Test
+ public void shouldReturnEmptyOnMissingCBSName() {
+ Map<String, String> envs = Map(
+ "CONSUL_HOST", "doesNotMatter",
+ "HOSTNAME", "doesNotMatter");
+ assertTrue(readEnvProps(envs).isEmpty());
+ }
+
+ @Test
+ public void shouldReturnEmptyOnMissingVESAppName() {
+ Map<String, String> envs = Map(
+ "CONSUL_HOST", "doesNotMatter",
+ "CONFIG_BINDING_SERVICE", "doesNotMatter");
+ assertTrue(readEnvProps(envs).isEmpty());
+ }
+
+ @Test
+ public void shouldReturnSomeOfAllProperties() {
+ Map<String, String> envs = Map(
+ "CONSUL_HOST", "doesNotMatter",
+ "HOSTNAME", "doesNotMatter",
+ "CONFIG_BINDING_SERVICE", "doesNotMatter");
+ assertFalse(readEnvProps(envs).isEmpty());
+ }
+
+}
+
diff --git a/src/test/java/org/onap/dcae/vestest/TestFetchConfig.java b/src/test/java/org/onap/dcae/vestest/TestFetchConfig.java
deleted file mode 100644
index 0b6b5027..00000000
--- a/src/test/java/org/onap/dcae/vestest/TestFetchConfig.java
+++ /dev/null
@@ -1,81 +0,0 @@
-/*-
- * ============LICENSE_START=======================================================
- * PROJECT
- * ================================================================================
- * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved.
- * ================================================================================
- * 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.onap.dcae.vestest;
-
-import static org.junit.Assert.assertTrue;
-import static org.onap.dcae.vestest.TestingUtilities.createTemporaryFile;
-
-import com.google.gson.JsonObject;
-import java.nio.file.Path;
-import org.junit.Before;
-import org.junit.Test;
-import org.onap.dcae.controller.FetchDynamicConfig;
-import org.onap.dcae.controller.LoadDynamicConfig;
-
-
-public class TestFetchConfig {
-
- private Path temporaryFile;
-
- @Before
- public void setUp() {
- temporaryFile = createTemporaryFile();
- }
-
- @Test
- public void shouldWriteFileAndAttachDMaaPStreamsPropertiesFromConfiguration() {
- // given
- FetchDynamicConfig loadDynamicConfig = new FetchDynamicConfig();
- FetchDynamicConfig.configFile = temporaryFile.toString();
- String sampleConfiguration = LoadDynamicConfig.readFile("src/test/resources/controller-config_singleline_ip.json");
-
- // when
- loadDynamicConfig.writefile(sampleConfiguration);
-
- // then
- JsonObject actuallyWrittenJSONContent = TestingUtilities.readJSONFromFile(temporaryFile);
- assertTrue(actuallyWrittenJSONContent.has("streams_publishes"));
- }
-
- @Test
- public void shouldThrowNoErrorsWhileParsingConsulResponse() {
- // given
- FetchDynamicConfig.retString = "[{\"ID\":\"81bc2a17-8cfa-3f6f-30a9-a545a9b6ac2f\",\"Node\":\"zldcrdm5bdcc2dokr00\",\"Address\":\"135.25.108.161\",\"Datacenter\":\"zldcrdm5bdcc2\",\"TaggedAddresses\":{\"lan\":\"135.25.108.161\",\"wan\":\"135.25.108.161\"},\"NodeMeta\":{\"fqdn\":\"zldcrdm5bdcc2dokr00.2f3fb3.rdm5b.tci.att.com\"},\"ServiceID\":\"20299a144716:config_binding_service:10000\",\"ServiceName\":\"config_binding_service\",\"ServiceTags\":[],\"ServiceAddress\":\"135.25.108.161\",\"ServicePort\":10000,\"ServiceEnableTagOverride\":false,\"CreateIndex\":9153156,\"ModifyIndex\":9153156}]";
-
- // then
- FetchDynamicConfig.getCBS();
- }
-
-
- @Test
- public void shouldReturnTrueOnConfigurationChange() {
- // given
- FetchDynamicConfig.configFile = "src/test/resources/controller-config_singleline_ip.json";
- FetchDynamicConfig.retCBSString = "{\"header.authflag\": \"1\", \"collector.schema.file\": \"{\\\"v1\\\": \\\"./etc/CommonEventFormat_27.2.json\\\", \\\"v2\\\": \\\"./etc/CommonEventFormat_27.2.json\\\", \\\"v3\\\": \\\"./etc/CommonEventFormat_27.2.json\\\", \\\"v4\\\": \\\"./etc/CommonEventFormat_27.2.json\\\", \\\"v5\\\": \\\"./etc/CommonEventFormat_28.4.json\\\"}\", \"collector.keystore.passwordfile\": \"/opt/app/dcae-certificate/.password\", \"tomcat.maxthreads\": \"200\", \"collector.dmaap.streamid\": \"fault=ves-fault|syslog=ves-syslog|heartbeat=ves-heartbeat|measurementsForVfScaling=ves-measurement|mobileFlow=ves-mobileflow|other=ves-other|stateChange=ves-statechange|thresholdCrossingAlert=ves-thresholdCrossingAlert|voiceQuality=ves-voicequality|sipSignaling=ves-sipsignaling\", \"streams_subscribes\": {}, \"collector.inputQueue.maxPending\": \"8096\", \"collector.keystore.alias\": \"dynamically generated\", \"streams_publishes\": {\"ves-mobileflow\": {\"type\": \"message_router\", \"dmaap_info\": {\"client_id\": \"1517590629043\", \"client_role\": \"com.att.secCollector.member\", \"location\": \"rdm5bdcc2\", \"topic_url\": \"https://DMAAPHOST:3905/events/com.att.dcae.dmaap.FTL.24256-SEC-MOBILEFLOW-OUTPUT-v1\"}, \"aaf_username\": \"userid@namespace\", \"aaf_password\": \"authpwd\"}, \"ves-measurement\": {\"type\": \"message_router\", \"dmaap_info\": {\"client_id\": \"1517590433916\", \"client_role\": \"com.att.secCollector.member\", \"location\": \"rdm5bdcc2\", \"topic_url\": \"https://DMAAPHOST:3905/events/com.att.dcae.dmaap.FTL.24256-ENC-MEASUREMENT-OUTPUT-v1\"}, \"aaf_username\": \"userid@namespace\", \"aaf_password\": \"authpwd\"}, \"ves-voicequality\": {\"type\": \"message_router\", \"dmaap_info\": {\"client_id\": \"1517590778397\", \"client_role\": \"com.att.secCollector.member\", \"location\": \"rdm5bdcc2\", \"topic_url\": \"https://DMAAPHOST:3905/events/com.att.dcae.dmaap.FTL.24256-VES-VOICEQUALITY-OUTPUT-v1\"}, \"aaf_username\": \"userid@namespace\", \"aaf_password\": \"authpwd\"}, \"ves-thresholdCrossingAlert\": {\"type\": \"message_router\", \"dmaap_info\": {\"client_id\": \"1517590728150\", \"client_role\": \"com.att.secCollector.member\", \"location\": \"rdm5bdcc2\", \"topic_url\": \"https://DMAAPHOST:3905/events/com.att.dcae.dmaap.FTL.24256-SEC-TCA-OUTPUT-v1\"}, \"aaf_username\": \"userid@namespace\", \"aaf_password\": \"authpwd\"}, \"ves-fault\": {\"type\": \"message_router\", \"dmaap_info\": {\"client_id\": \"1517590384670\", \"client_role\": \"com.att.secCollector.member\", \"location\": \"rdm5bdcc2\", \"topic_url\": \"https://DMAAPHOST:3905/events/com.att.dcae.dmaap.FTL.24256-SEC-FAULT-OUTPUT-v1\"}, \"aaf_username\": \"userid@namespace\", \"aaf_password\": \"authpwd\"}, \"ves-heartbeat\": {\"type\": \"message_router\", \"dmaap_info\": {\"client_id\": \"1517590530041\", \"client_role\": \"com.att.secCollector.member\", \"location\": \"rdm5bdcc2\", \"topic_url\": \"https://DMAAPHOST:3905/events/com.att.dcae.dmaap.FTL.24256-SEC-HEARTBEAT-OUTPUT-v1\"}, \"aaf_username\": \"userid@namespace\", \"aaf_password\": \"authpwd\"}, \"ves-sipsignaling\": {\"type\": \"message_router\", \"dmaap_info\": {\"client_id\": \"1517590828736\", \"client_role\": \"com.att.secCollector.member\", \"location\": \"rdm5bdcc2\", \"topic_url\": \"https://DMAAPHOST:3905/events/com.att.dcae.dmaap.FTL.24256-VES-SIPSIGNALING-OUTPUT-v1\"}, \"aaf_username\": \"userid@namespace\", \"aaf_password\": \"authpwd\"}, \"ves-syslog\": {\"type\": \"message_router\", \"dmaap_info\": {\"client_id\": \"1517590482019\", \"client_role\": \"com.att.secCollector.member\", \"location\": \"rdm5bdcc2\", \"topic_url\": \"https://DMAAPHOST:3905/events/com.att.dcae.dmaap.FTL.24256-SEC-SYSLOG-OUTPUT-v1\"}, \"aaf_username\": \"userid@namespace\", \"aaf_password\": \"authpwd\"}, \"ves-other\": {\"type\": \"message_router\", \"dmaap_info\": {\"client_id\": \"1517590581045\", \"client_role\": \"com.att.secCollector.member\", \"location\": \"rdm5bdcc2\", \"topic_url\": \"https://DMAAPHOST:3905/events/com.att.dcae.dmaap.FTL.24256-SEC-OTHER-OUTPUT-v1\"}, \"aaf_username\": \"userid@namespace\", \"aaf_password\": \"authpwd\"}, \"ves-statechange\": {\"type\": \"message_router\", \"dmaap_info\": {\"client_id\": \"1517590677649\", \"client_role\": \"com.att.secCollector.member\", \"location\": \"rdm5bdcc2\", \"topic_url\": \"https://DMAAPHOST:3905/events/com.att.dcae.dmaap.FTL.24256-SEC-STATECHANGE-OUTPUT-v1\"}, \"aaf_username\": \"userid@namespace\", \"aaf_password\": \"authpwd\"}}, \"collector.schema.checkflag\": \"1\", \"services_calls\": {}, \"event.transform.flag\": \"1\", \"collector.keystore.file.location\": \"/opt/app/dcae-certificate/keystore.jks\", \"header.authlist\": \"sample1,c2FtcGxlMQ==|userid1,base64encodepwd1|userid2,base64encodepwd2\", \"collector.service.secure.port\": \"8443\", \"collector.service.port\": \"-1\"}";
-
- // when
- boolean didConfigsChange = FetchDynamicConfig.verifyConfigChange();
-
- // then
- assertTrue(didConfigsChange);
- }
-
-}
-
diff --git a/src/test/java/org/onap/dcae/vestest/TestLoadDynamicConfig.java b/src/test/java/org/onap/dcae/vestest/TestLoadDynamicConfig.java
deleted file mode 100644
index 03a074d7..00000000
--- a/src/test/java/org/onap/dcae/vestest/TestLoadDynamicConfig.java
+++ /dev/null
@@ -1,76 +0,0 @@
-/*-
- * ============LICENSE_START=======================================================
- * PROJECT
- * ================================================================================
- * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved.
- * ================================================================================
- * 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.onap.dcae.vestest;
-
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertTrue;
-import static org.onap.dcae.vestest.TestingUtilities.createTemporaryFile;
-
-import com.github.fge.jackson.JsonLoader;
-import com.google.gson.JsonObject;
-import java.io.IOException;
-import java.nio.file.Files;
-import java.nio.file.Path;
-import org.json.JSONObject;
-import org.junit.Before;
-import org.junit.Test;
-import org.onap.dcae.controller.LoadDynamicConfig;
-
-public class TestLoadDynamicConfig {
-
- private Path temporaryFile;
-
- @Before
- public void setUp() {
- temporaryFile = createTemporaryFile();
- }
-
- @Test
- public void shouldReadFileContent() throws IOException {
- // given
- String expectedJSON = "{ \"field\" : 1 }";
- Files.write(temporaryFile, expectedJSON.getBytes());
-
- // when
- String readFileContent = LoadDynamicConfig.readFile(temporaryFile.toString());
-
- // then
- assertEquals(JsonLoader.fromString(expectedJSON), JsonLoader.fromString(readFileContent));
- }
-
- @Test
- public void shouldWriteFileAndAttachDMaaPRelatedPropertiesFromConfiguration() {
- // given
- LoadDynamicConfig loadDynamicConfig = new LoadDynamicConfig();
- loadDynamicConfig.propFile = "src/test/resources/test_collector_ip_op.properties";
- loadDynamicConfig.configFile = "src/test/resources/controller-config_dmaap_ip.json";
- loadDynamicConfig.dMaaPOutputFile = temporaryFile.toString();
- String sampleConfiguration = LoadDynamicConfig.readFile(loadDynamicConfig.configFile);
-
- // when
- loadDynamicConfig.writeconfig(new JSONObject(sampleConfiguration));
-
- // then
- JsonObject actuallyWrittenJSONContent = TestingUtilities.readJSONFromFile(temporaryFile);
- assertTrue(actuallyWrittenJSONContent.has("ves-fault-secondary"));
- }
-
-}
-