From 50592c30fed4908bbf860f9018cc5f94f8bd2303 Mon Sep 17 00:00:00 2001 From: Pawel Date: Thu, 30 Jul 2020 16:30:39 +0200 Subject: Config fetch for VESCollector through DCAE-SDK (CBS Client) Issue-ID: DCAEGEN2-2212 Signed-off-by: Pawel Change-Id: I25072b340b5c9f2f538d39e5befb1331804b7bba Signed-off-by: Pawel --- src/test/java/org/onap/dcae/TLSTestBase.java | 10 +- src/test/java/org/onap/dcae/TestingUtilities.java | 27 +- src/test/java/org/onap/dcae/WiremockBasedTest.java | 70 ----- .../java/org/onap/dcae/common/EventSenderTest.java | 6 +- .../common/publishing/DMaaPEventPublisherTest.java | 4 +- .../dcae/configuration/ConfigFilesFacadeTest.java | 137 ++++++++++ .../configuration/ConfigLoaderFactoryTest.java | 43 +++ .../onap/dcae/configuration/ConfigLoaderTest.java | 294 +++++++++++++++++++++ .../onap/dcae/configuration/ConfigParsingTest.java | 73 +++++ .../cbs/CbsClientConfigurationResolverTest.java | 47 ++++ .../configuration/cbs/CbsClientFactoryTest.java | 36 +++ .../configuration/cbs/CbsConfigResolverTest.java | 74 ++++++ .../onap/dcae/controller/ConfigCBSSourceTest.java | 153 ----------- .../dcae/controller/ConfigFilesFacadeTest.java | 139 ---------- .../controller/ConfigLoaderIntegrationE2ETest.java | 93 ------- .../onap/dcae/controller/ConfigParsingTest.java | 73 ----- .../dcae/controller/EnvPropertiesReaderTest.java | 69 ----- .../org/onap/dcae/controller/EnvPropsTest.java | 39 --- .../onap/dcae/restapi/VesRestControllerTest.java | 6 +- 19 files changed, 723 insertions(+), 670 deletions(-) delete mode 100644 src/test/java/org/onap/dcae/WiremockBasedTest.java create mode 100644 src/test/java/org/onap/dcae/configuration/ConfigFilesFacadeTest.java create mode 100644 src/test/java/org/onap/dcae/configuration/ConfigLoaderFactoryTest.java create mode 100644 src/test/java/org/onap/dcae/configuration/ConfigLoaderTest.java create mode 100644 src/test/java/org/onap/dcae/configuration/ConfigParsingTest.java create mode 100644 src/test/java/org/onap/dcae/configuration/cbs/CbsClientConfigurationResolverTest.java create mode 100644 src/test/java/org/onap/dcae/configuration/cbs/CbsClientFactoryTest.java create mode 100644 src/test/java/org/onap/dcae/configuration/cbs/CbsConfigResolverTest.java delete mode 100644 src/test/java/org/onap/dcae/controller/ConfigCBSSourceTest.java delete mode 100644 src/test/java/org/onap/dcae/controller/ConfigFilesFacadeTest.java delete mode 100644 src/test/java/org/onap/dcae/controller/ConfigLoaderIntegrationE2ETest.java delete mode 100644 src/test/java/org/onap/dcae/controller/ConfigParsingTest.java delete mode 100644 src/test/java/org/onap/dcae/controller/EnvPropertiesReaderTest.java delete mode 100644 src/test/java/org/onap/dcae/controller/EnvPropsTest.java (limited to 'src/test/java/org/onap/dcae') diff --git a/src/test/java/org/onap/dcae/TLSTestBase.java b/src/test/java/org/onap/dcae/TLSTestBase.java index 1eb5728e..1026e759 100644 --- a/src/test/java/org/onap/dcae/TLSTestBase.java +++ b/src/test/java/org/onap/dcae/TLSTestBase.java @@ -2,9 +2,8 @@ * ============LICENSE_START======================================================= * org.onap.dcaegen2.collectors.ves * ================================================================================ - * Copyright (C) 2018 Nokia. All rights reserved. + * Copyright (C) 2018-2020 Nokia. All rights reserved. * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved. - * Copyright (C) 2019 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. @@ -136,13 +135,6 @@ public class TLSTestBase { return createHttpsRestTemplate().getForEntity(createHttpsURL("/"), String.class); } - - public ResponseEntity makeHttpsRequestWithBasicAuth(final String username, final String password) { - return addBasicAuth(createHttpsRestTemplate(), username, password) - .getForEntity(createHttpsURL("/"), String.class); - - } - public ResponseEntity makeHttpsRequestWithClientCert() { return createHttpsRestTemplateWithKeyStore().getForEntity(createHttpsURL("/"), String.class); } diff --git a/src/test/java/org/onap/dcae/TestingUtilities.java b/src/test/java/org/onap/dcae/TestingUtilities.java index 4c0d5382..8fb89154 100644 --- a/src/test/java/org/onap/dcae/TestingUtilities.java +++ b/src/test/java/org/onap/dcae/TestingUtilities.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * org.onap.dcaegen2.collectors.ves * ================================================================================ - * Copyright (C) 2018 Nokia. All rights reserved. + * Copyright (C) 2018,2020 Nokia. All rights reserved. * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); @@ -20,23 +20,7 @@ */ package org.onap.dcae; -import static java.nio.file.Files.readAllBytes; -import static org.assertj.core.api.Assertions.assertThat; - import io.vavr.control.Try; - -import java.io.File; -import java.io.FileInputStream; -import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.Paths; -import java.security.KeyStore; -import java.security.KeyStoreException; -import java.security.NoSuchAlgorithmException; -import java.security.UnrecoverableKeyException; -import java.security.cert.CertificateException; - import org.apache.http.client.HttpClient; import org.apache.http.conn.ssl.SSLConnectionSocketFactory; import org.apache.http.impl.client.HttpClients; @@ -48,6 +32,15 @@ import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; import org.springframework.web.client.RestTemplate; import javax.net.ssl.SSLContext; +import java.io.File; +import java.io.FileInputStream; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.security.KeyStore; + +import static java.nio.file.Files.readAllBytes; +import static org.assertj.core.api.Assertions.assertThat; /** * @author Pawel Szalapski (pawel.szalapski@nokia.com) diff --git a/src/test/java/org/onap/dcae/WiremockBasedTest.java b/src/test/java/org/onap/dcae/WiremockBasedTest.java deleted file mode 100644 index 3b4bdc39..00000000 --- a/src/test/java/org/onap/dcae/WiremockBasedTest.java +++ /dev/null @@ -1,70 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * org.onap.dcaegen2.collectors.ves - * ================================================================================ - * Copyright (C) 2018 Nokia. All rights reserved. - * 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; - -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 wiremockBasedEnvProps() { - return Map( - "CONSUL_HOST", "localhost", - "CONSUL_PORT", "" + wireMockRule.port(), - "HOSTNAME", "VESCollector", - "CONFIG_BINDING_SERVICE", "CBSName" - ); - } - - protected String validLocalCBSConf() { - return "" - + "[{ " - + "\"ServiceAddress\": \"localhost\"," - + "\"ServicePort\":" + wireMockRule.port() - + "}]"; - } -} diff --git a/src/test/java/org/onap/dcae/common/EventSenderTest.java b/src/test/java/org/onap/dcae/common/EventSenderTest.java index e20fa993..454cfb52 100644 --- a/src/test/java/org/onap/dcae/common/EventSenderTest.java +++ b/src/test/java/org/onap/dcae/common/EventSenderTest.java @@ -3,7 +3,7 @@ * PROJECT * ================================================================================ * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. - * Copyright (C) 2020 Nokia. All rights reserved.s + * Copyright (C) 2018,2020 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. @@ -28,7 +28,7 @@ import org.mockito.Mock; import org.mockito.junit.MockitoJUnitRunner; import org.onap.dcae.common.model.StndDefinedNamespaceParameterNotDefinedException; import org.onap.dcae.common.model.VesEvent; -import org.onap.dcae.common.publishing.EventPublisher; +import org.onap.dcae.common.publishing.DMaaPEventPublisher; import java.io.IOException; import java.util.List; @@ -43,7 +43,7 @@ import static org.mockito.Mockito.verify; public class EventSenderTest { @Mock - private EventPublisher eventPublisher; + private DMaaPEventPublisher eventPublisher; @Test diff --git a/src/test/java/org/onap/dcae/common/publishing/DMaaPEventPublisherTest.java b/src/test/java/org/onap/dcae/common/publishing/DMaaPEventPublisherTest.java index 45cdf282..99505bfd 100644 --- a/src/test/java/org/onap/dcae/common/publishing/DMaaPEventPublisherTest.java +++ b/src/test/java/org/onap/dcae/common/publishing/DMaaPEventPublisherTest.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * org.onap.dcaegen2.collectors.ves * ================================================================================ - * Copyright (C) 2018 Nokia. All rights reserved. + * Copyright (C) 2018,2020 Nokia. All rights reserved. * Copyright (C) 2020 AT&T. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); @@ -47,7 +47,7 @@ public class DMaaPEventPublisherTest { cambriaPublisher = mock(CambriaBatchingPublisher.class); DMaaPPublishersCache = mock(DMaaPPublishersCache.class); when(DMaaPPublishersCache.getPublisher(anyString())).thenReturn(Option(cambriaPublisher)); - eventPublisher = new DMaaPEventPublisher(DMaaPPublishersCache, mock(Logger.class)); + eventPublisher = new DMaaPEventPublisher(DMaaPPublishersCache); } @Test diff --git a/src/test/java/org/onap/dcae/configuration/ConfigFilesFacadeTest.java b/src/test/java/org/onap/dcae/configuration/ConfigFilesFacadeTest.java new file mode 100644 index 00000000..8849369a --- /dev/null +++ b/src/test/java/org/onap/dcae/configuration/ConfigFilesFacadeTest.java @@ -0,0 +1,137 @@ +/*- + * ============LICENSE_START======================================================= + * org.onap.dcaegen2.collectors.ves + * ================================================================================ + * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2018,2020 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.configuration; + +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> 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 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 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 propertiesConfigurations = configFilesFacade.writeProperties(Map("prop1", "hi")); + + // then + assertThat(propertiesConfigurations.isSuccess()).isTrue(); + assertThat(readFile(temporaryFile).trim()).isEqualTo("prop1 = hi"); + } + + @Test + public void shouldContainPropertiesPathInCaseOfFailures() { + Try> result = TO_NON_EXISTENT_POINTING_FACADE.readCollectorProperties(); + assertThat(result.isFailure()).isTrue(); + assertFailureHasInfo(result, NON_EXISTENT.toString()); + } + + @Test + public void shouldContainDMaaPPathPathInCaseOfFailures() { + Try result = TO_NON_EXISTENT_POINTING_FACADE.readDMaaPConfiguration(); + assertThat(result.isFailure()).isTrue(); + assertFailureHasInfo(result, NON_EXISTENT.toString()); + } + + @Test + public void shouldContainPropertiesPathPathInCaseOfFailuresOnWrite() { + // given + Try 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 result = TO_NON_EXISTENT_POINTING_FACADE.writeDMaaPConfiguration(new JSONObject()); + assertThat(result.isFailure()).isTrue(); + assertFailureHasInfo(result, NON_EXISTENT.toString()); + } +} \ No newline at end of file diff --git a/src/test/java/org/onap/dcae/configuration/ConfigLoaderFactoryTest.java b/src/test/java/org/onap/dcae/configuration/ConfigLoaderFactoryTest.java new file mode 100644 index 00000000..e9421909 --- /dev/null +++ b/src/test/java/org/onap/dcae/configuration/ConfigLoaderFactoryTest.java @@ -0,0 +1,43 @@ +/*- + * ============LICENSE_START======================================================= + * org.onap.dcaegen2.collectors.ves + * ================================================================================ + * Copyright (C) 2020 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.configuration; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.nio.file.Path; +import org.junit.Test; + +public class ConfigLoaderFactoryTest { + + @Test + public void createsCbsConfigLoaderSuccessfully() { + // given + Path testPropertiesPath = Path.of("src/test/resources/testcollector.properties"); + Path testDmaapConfigPath = Path.of("src/test/resources/testParseDMaaPCredentialsGen2.json"); + + // when + ConfigLoader configLoader = ConfigLoaderFactory.create( + testPropertiesPath, + testDmaapConfigPath); + + // then + assertThat(configLoader).isNotNull(); + } +} \ No newline at end of file diff --git a/src/test/java/org/onap/dcae/configuration/ConfigLoaderTest.java b/src/test/java/org/onap/dcae/configuration/ConfigLoaderTest.java new file mode 100644 index 00000000..46d864a0 --- /dev/null +++ b/src/test/java/org/onap/dcae/configuration/ConfigLoaderTest.java @@ -0,0 +1,294 @@ +/*- + * ============LICENSE_START======================================================= + * org.onap.dcaegen2.collectors.ves + * ================================================================================ + * Copyright (C) 2020 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.configuration; + +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.argThat; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; +import static org.onap.dcae.TestingUtilities.readJSONFromFile; +import static org.onap.dcae.common.publishing.VavrUtils.f; + +import io.vavr.collection.HashMap; +import io.vavr.collection.Map; +import io.vavr.control.Try; +import java.nio.file.Paths; +import io.vavr.control.Option; +import org.json.JSONObject; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.MockitoJUnitRunner; +import org.onap.dcae.configuration.cbs.CbsConfigResolver; + +@RunWith(MockitoJUnitRunner.Silent.class) +public class ConfigLoaderTest { + + private static final String COLLECTOR_PORT = "collector.port"; + private static final String PORT_8080 = "8080"; + private static final String PORT_8081 = "8081"; + private static final String COLLECTOR_KEYSTORE_FILE_LOCATION = "collector.keystore.file.location"; + private static final String SOME_PATH = "some/path"; + private static final String COLLECTOR_SCHEMA_FILE = "collector.schema.file"; + private static final String SOME_SCHEMA = "some schema"; + + @Mock + private CbsConfigResolver cbsConfigResolverMock; + + @Mock + private ConfigFilesFacade configFilesFacadeMock; + + @InjectMocks + private ConfigLoader configLoader; + + @Mock + private Runnable applicationRestarter; + + + @Before + public void setup() { + when(configFilesFacadeMock.readCollectorProperties()).thenReturn(Try.of(HashMap::empty)); + when(configFilesFacadeMock.readDMaaPConfiguration()).thenReturn(Try.of(JSONObject::new)); + } + + @Test + public void shouldCallConfigSourceForData() { + // given + HashMap properties = HashMap.of(COLLECTOR_PORT, PORT_8080); + mockVesInitialProperties(properties); + mockVesConfigInCbs(properties); + + // when + configLoader.updateConfig(); + + // then + verify(cbsConfigResolverMock).getAppConfig(); + } + + @Test + public void shouldNotUpdatePropertiesWhenSameKeySetAndSameValues() { + // given + HashMap properties = HashMap.of(COLLECTOR_PORT, PORT_8080); + mockVesInitialProperties(properties); + mockVesConfigInCbs(properties); + + // when + configLoader.updateConfig(); + + // then + verify(configFilesFacadeMock, never()).writeProperties(any()); + verify(applicationRestarter, never()).run(); + } + + @Test + public void shouldUpdatePropertiesWhenSameKeySetButDifferentValues() { + // given + HashMap initialProperties = HashMap.of(COLLECTOR_PORT, PORT_8080); + HashMap cbsProperties = HashMap.of(COLLECTOR_PORT, PORT_8081); + mockVesInitialProperties(initialProperties); + mockVesConfigInCbs(cbsProperties); + + // when + configLoader.updateConfig(); + + // then + verify(configFilesFacadeMock, times(1)).writeProperties(cbsProperties); + verify(applicationRestarter, times(1)).run(); + } + + @Test + public void shouldUpdatePropertiesWhenVesKeysAreSubsetOfCbsKeysAndSubsetHasSameValues() { + // given + HashMap initialProperties = HashMap.of( + COLLECTOR_PORT, PORT_8080); + HashMap cbsProperties = HashMap.of( + COLLECTOR_PORT, PORT_8080, + COLLECTOR_KEYSTORE_FILE_LOCATION, SOME_PATH); + mockVesInitialProperties(initialProperties); + mockVesConfigInCbs(cbsProperties); + + // when + configLoader.updateConfig(); + + // then + verify(configFilesFacadeMock, times(1)).writeProperties(cbsProperties); + verify(applicationRestarter, times(1)).run(); + } + + @Test + public void shouldUpdatePropertiesWhenVesKeysAreSubsetOfCbsKeysAndSubsetHasDifferentValues() { + HashMap initialProperties = HashMap.of( + COLLECTOR_PORT, PORT_8080); + HashMap cbsProperties = HashMap.of( + COLLECTOR_PORT, PORT_8081, + COLLECTOR_KEYSTORE_FILE_LOCATION, SOME_PATH); + mockVesInitialProperties(initialProperties); + mockVesConfigInCbs(cbsProperties); + + // when + configLoader.updateConfig(); + + // then + verify(configFilesFacadeMock, times(1)).writeProperties(cbsProperties); + verify(applicationRestarter, times(1)).run(); + } + + @Test + public void shouldNotUpdatePropertiesWhenCbsKeysAreSubsetOfVesKeysAndSubsetHasSameValues() { + HashMap initialProperties = HashMap.of( + COLLECTOR_PORT, PORT_8080, + COLLECTOR_KEYSTORE_FILE_LOCATION, SOME_PATH); + HashMap cbsProperties = HashMap.of( + COLLECTOR_PORT, PORT_8080); + mockVesInitialProperties(initialProperties); + mockVesConfigInCbs(cbsProperties); + + // when + configLoader.updateConfig(); + + // then + verify(configFilesFacadeMock, never()).writeProperties(any()); + verify(applicationRestarter, never()).run(); + } + + @Test + public void shouldUpdatePropertiesWhenCbsKeysAreSubsetOfVesKeysAndSubsetHasDifferentValues() { + HashMap initialProperties = HashMap.of( + COLLECTOR_PORT, PORT_8080, + COLLECTOR_KEYSTORE_FILE_LOCATION, SOME_PATH); + HashMap cbsProperties = HashMap.of( + COLLECTOR_PORT, PORT_8081); + mockVesInitialProperties(initialProperties); + mockVesConfigInCbs(cbsProperties); + + // when + configLoader.updateConfig(); + + // then + verify(configFilesFacadeMock, times(1)).writeProperties(cbsProperties); + verify(applicationRestarter, times(1)).run(); + } + + @Test + public void shouldUpdatePropertiesWhenVesAndCbsKeySetsIntersectAndIntersectingKeysHaveSameValues() { + HashMap initialProperties = HashMap.of( + COLLECTOR_PORT, PORT_8080, + COLLECTOR_KEYSTORE_FILE_LOCATION, SOME_PATH); + HashMap cbsProperties = HashMap.of( + COLLECTOR_PORT, PORT_8080, + COLLECTOR_SCHEMA_FILE, SOME_SCHEMA + ); + mockVesInitialProperties(initialProperties); + mockVesConfigInCbs(cbsProperties); + + // when + configLoader.updateConfig(); + + // then + verify(configFilesFacadeMock, times(1)).writeProperties(cbsProperties); + verify(applicationRestarter, times(1)).run(); + } + + @Test + public void shouldUpdatePropertiesWhenVesAndCbsKeySetsIntersectAndIntersectingKeysHaveDifferentValues() { + HashMap initialProperties = HashMap.of( + COLLECTOR_PORT, PORT_8080, + COLLECTOR_KEYSTORE_FILE_LOCATION, SOME_PATH); + HashMap cbsProperties = HashMap.of( + COLLECTOR_PORT, PORT_8081, + COLLECTOR_SCHEMA_FILE, SOME_SCHEMA + ); + mockVesInitialProperties(initialProperties); + mockVesConfigInCbs(cbsProperties); + + // when + configLoader.updateConfig(); + + // then + verify(configFilesFacadeMock, times(1)).writeProperties(cbsProperties); + verify(applicationRestarter, times(1)).run(); + } + + @Test + public void shouldUpdateDmaapConfigWhenConfigurationChanged() { + // given + JSONObject emptyDmaapConfig = new JSONObject(); + JSONObject dmaapConfig = loadSampleDmaapConfig(); + mockVesInitialDmaapConfig(emptyDmaapConfig); + mockVesDmaapConfigInCbs(dmaapConfig); + + // when + configLoader.updateConfig(); + + // then + verify(configFilesFacadeMock).writeDMaaPConfiguration(argThat(dmaapConfig::similar)); + verify(applicationRestarter, times(1)).run(); + } + + @Test + public void shouldNotUpdateDmaapConfigWhenConfigurationNotChanged() { + // given + JSONObject dmaapConf = loadSampleDmaapConfig(); + mockVesInitialDmaapConfig(dmaapConf); + mockVesDmaapConfigInCbs(dmaapConf); + + // when + configLoader.updateConfig(); + + // then + verify(configFilesFacadeMock, never()).writeDMaaPConfiguration(any()); + verify(applicationRestarter, never()).run(); + } + + private void mockVesInitialDmaapConfig(JSONObject dmaapConf) { + when(configFilesFacadeMock.readDMaaPConfiguration()).thenReturn(Try.of(() -> dmaapConf)); + } + + private void mockVesDmaapConfigInCbs(JSONObject dmaapConf) { + JSONObject jsonObject = new JSONObject(f("{\"streams_publishes\": %s}}", dmaapConf)); + when(cbsConfigResolverMock.getAppConfig()).thenReturn(Option.of(jsonObject)); + } + + private void mockVesConfigInCbs(HashMap properties) { + when(cbsConfigResolverMock.getAppConfig()).thenReturn(Option.of(prepareConfigurationJson(properties))); + } + + private void mockVesInitialProperties(HashMap properties) { + when(configFilesFacadeMock.readCollectorProperties()).thenReturn(Try.of(() -> properties)); + } + + + private JSONObject loadSampleDmaapConfig() { + return readJSONFromFile(Paths.get("src/test/resources/testParseDMaaPCredentialsGen2.json")); + } + + private JSONObject prepareConfigurationJson(Map properties) { + String template = "{%s, \"streams_publishes\": {}}"; + String customProperties = properties + .map(property -> "\"" + property._1 + "\": \"" + property._2 + "\"") + .mkString(", "); + String jsonBody = f(template, customProperties); + return new JSONObject(jsonBody); + } +} \ No newline at end of file diff --git a/src/test/java/org/onap/dcae/configuration/ConfigParsingTest.java b/src/test/java/org/onap/dcae/configuration/ConfigParsingTest.java new file mode 100644 index 00000000..248d3c5a --- /dev/null +++ b/src/test/java/org/onap/dcae/configuration/ConfigParsingTest.java @@ -0,0 +1,73 @@ +/*- + * ============LICENSE_START======================================================= + * org.onap.dcaegen2.collectors.ves + * ================================================================================ + * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2018,2020 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.configuration; + +import io.vavr.collection.Map; +import io.vavr.control.Option; +import org.json.JSONObject; +import org.junit.Test; + +import java.nio.file.Paths; + +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; + +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 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 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 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/configuration/cbs/CbsClientConfigurationResolverTest.java b/src/test/java/org/onap/dcae/configuration/cbs/CbsClientConfigurationResolverTest.java new file mode 100644 index 00000000..57824595 --- /dev/null +++ b/src/test/java/org/onap/dcae/configuration/cbs/CbsClientConfigurationResolverTest.java @@ -0,0 +1,47 @@ +/*- + * ============LICENSE_START======================================================= + * org.onap.dcaegen2.collectors.ves + * ================================================================================ + * Copyright (C) 2020 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.configuration.cbs; + +import static org.assertj.core.api.Assertions.assertThat; + +import org.junit.Test; +import org.junit.jupiter.api.condition.DisabledIfEnvironmentVariable; +import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.CbsClientConfiguration; + +public class CbsClientConfigurationResolverTest { + + private static final String DEFAULT_PROTOCOL = "http"; + private static final String DEFAULT_HOSTNAME = "config-binding-service"; + private static final int DEFAULT_PORT = 10000; + private static final String DEFAULT_APP_NAME = "dcae-ves-collector"; + + @Test + @DisabledIfEnvironmentVariable(named = "CONFIG_BINDING_SERVICE", matches = ".+") + public void shouldLoadDefaultConfigWhenEnvNotPresent() { + // when + CbsClientConfiguration configuration = new CbsClientConfigurationResolver().resolveCbsClientConfiguration(); + + // then + assertThat(configuration.protocol()).isEqualTo(DEFAULT_PROTOCOL); + assertThat(configuration.hostname()).isEqualTo(DEFAULT_HOSTNAME); + assertThat(configuration.port()).isEqualTo(DEFAULT_PORT); + assertThat(configuration.appName()).isEqualTo(DEFAULT_APP_NAME); + } +} \ No newline at end of file diff --git a/src/test/java/org/onap/dcae/configuration/cbs/CbsClientFactoryTest.java b/src/test/java/org/onap/dcae/configuration/cbs/CbsClientFactoryTest.java new file mode 100644 index 00000000..4b17f68d --- /dev/null +++ b/src/test/java/org/onap/dcae/configuration/cbs/CbsClientFactoryTest.java @@ -0,0 +1,36 @@ +/*- + * ============LICENSE_START======================================================= + * org.onap.dcaegen2.collectors.ves + * ================================================================================ + * Copyright (C) 2020 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.configuration.cbs; + +import static org.assertj.core.api.Assertions.assertThat; + +import org.junit.Test; + +public class CbsClientFactoryTest { + + @Test + public void createsClientSuccessfully() { + // when + CbsConfigResolver cbsConfigResolver = new CbsConfigResolverFactory().create(); + + // then + assertThat(cbsConfigResolver).isNotNull(); + } +} \ No newline at end of file diff --git a/src/test/java/org/onap/dcae/configuration/cbs/CbsConfigResolverTest.java b/src/test/java/org/onap/dcae/configuration/cbs/CbsConfigResolverTest.java new file mode 100644 index 00000000..b413a64d --- /dev/null +++ b/src/test/java/org/onap/dcae/configuration/cbs/CbsConfigResolverTest.java @@ -0,0 +1,74 @@ +/*- + * ============LICENSE_START======================================================= + * org.onap.dcaegen2.collectors.ves + * ================================================================================ + * Copyright (C) 2020 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.configuration.cbs; + +import com.github.tomakehurst.wiremock.junit.WireMockRule; +import org.json.JSONObject; +import org.junit.Rule; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.junit.MockitoJUnitRunner; +import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.CbsClientConfiguration; +import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.ImmutableCbsClientConfiguration; + +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 org.assertj.core.api.Assertions.assertThat; + +@RunWith(MockitoJUnitRunner.Silent.class) +public class CbsConfigResolverTest { + + private static final String VES_CONFIG = "{\"collector.port\": 8081}"; + private static final String HOSTNAME = "localhost"; + private static final String PROTOCOL = "http"; + private static final String APP_NAME = "VESCollector"; + + @Rule + public final WireMockRule wireMockRule = new WireMockRule( + wireMockConfig().dynamicPort().dynamicPort()); + + @Test + public void shouldFetchConfigurationFromCBS() { + // given + final int PORT = wireMockRule.port(); + stubCBSToReturnAppConfig(); + + // when + CbsClientConfiguration cbsClientConfiguration = ImmutableCbsClientConfiguration.builder() + .protocol(PROTOCOL) + .hostname(HOSTNAME) + .port(PORT) + .appName(APP_NAME) + .build(); + JSONObject appConfig = new CbsConfigResolver(cbsClientConfiguration).getAppConfig().get(); + + // then + assertThat(appConfig).isNotNull(); + assertThat(appConfig.toString()).isEqualTo(new JSONObject(VES_CONFIG).toString()); + } + + private void stubCBSToReturnAppConfig() { + stubFor(get(urlEqualTo("/service_component/VESCollector")) + .willReturn(aResponse().withBody(CbsConfigResolverTest.VES_CONFIG))); + } +} \ No newline at end of file diff --git a/src/test/java/org/onap/dcae/controller/ConfigCBSSourceTest.java b/src/test/java/org/onap/dcae/controller/ConfigCBSSourceTest.java deleted file mode 100644 index bfec3d97..00000000 --- a/src/test/java/org/onap/dcae/controller/ConfigCBSSourceTest.java +++ /dev/null @@ -1,153 +0,0 @@ -/*- - * ============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 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 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 actual = tryToGetConfig(); - - // then - assertFailureHasInfo(actual, "JSON", "array", "[{"); - } - - @Test - public void shouldReturnFailureOnInvalidCatalogFormat() { - // given - String notAListCatalog = "" - + "{" - + "\"ServiceAddress\":\"localhost\"," - + "\"ServicePort\":" + wireMockRule.port() - + "}"; - - stubFor(get(urlEqualTo("/v1/catalog/service/CBSName")) - .willReturn(aResponse().withStatus(200).withBody(notAListCatalog))); - - // when - Try 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 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 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 actual = tryToGetConfig(); - - // then - assertFailureHasInfo(actual, "JSON", "document", invalidAppConf); - } - - private Try tryToGetConfig() { - return getAppConfig(new EnvProps("http", "localhost", wireMockRule.port(), "http", "CBSName", "VESCollector")); - } -} - diff --git a/src/test/java/org/onap/dcae/controller/ConfigFilesFacadeTest.java b/src/test/java/org/onap/dcae/controller/ConfigFilesFacadeTest.java deleted file mode 100644 index 474a77c5..00000000 --- a/src/test/java/org/onap/dcae/controller/ConfigFilesFacadeTest.java +++ /dev/null @@ -1,139 +0,0 @@ -/*- - * ============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> 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 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 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 propertiesConfigurations = configFilesFacade.writeProperties(Map("prop1", "hi")); - - // then - assertThat(propertiesConfigurations.isSuccess()).isTrue(); - assertThat(readFile(temporaryFile).trim()).isEqualTo("prop1 = hi"); - } - - @Test - public void shouldContainPropertiesPathInCaseOfFailures() { - Try> result = TO_NON_EXISTENT_POINTING_FACADE.readCollectorProperties(); - assertThat(result.isFailure()).isTrue(); - assertFailureHasInfo(result, NON_EXISTENT.toString()); - } - - @Test - public void shouldContainDMaaPPathPathInCaseOfFailures() { - Try result = TO_NON_EXISTENT_POINTING_FACADE.readDMaaPConfiguration(); - assertThat(result.isFailure()).isTrue(); - assertFailureHasInfo(result, NON_EXISTENT.toString()); - } - - @Test - public void shouldContainPropertiesPathPathInCaseOfFailuresOnWrite() { - // given - Try 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 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 deleted file mode 100644 index 90c8a9c8..00000000 --- a/src/test/java/org/onap/dcae/controller/ConfigLoaderIntegrationE2ETest.java +++ /dev/null @@ -1,93 +0,0 @@ -/*- - * ============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.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.common.publishing.VavrUtils.f; - -import java.nio.file.Path; -import java.nio.file.Paths; -import org.json.JSONObject; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.Mock; -import org.mockito.junit.MockitoJUnitRunner; -import org.onap.dcae.ApplicationSettings; -import org.onap.dcae.WiremockBasedTest; -import org.onap.dcae.common.publishing.DMaaPConfigurationParser; -import org.onap.dcae.common.publishing.EventPublisher; - -@RunWith(MockitoJUnitRunner.Silent.class) -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); - 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 deleted file mode 100644 index 5d8d4efa..00000000 --- a/src/test/java/org/onap/dcae/controller/ConfigParsingTest.java +++ /dev/null @@ -1,73 +0,0 @@ -/*- - * ============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 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 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 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 deleted file mode 100644 index 6c358024..00000000 --- a/src/test/java/org/onap/dcae/controller/EnvPropertiesReaderTest.java +++ /dev/null @@ -1,69 +0,0 @@ -/*- - * ============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 envs = Map( - "CONFIG_BINDING_SERVICE", "doesNotMatter", - "HOSTNAME", "doesNotMatter"); - assertTrue(readEnvProps(envs).isEmpty()); - } - - @Test - public void shouldReturnEmptyOnMissingCBSName() { - Map envs = Map( - "CONSUL_HOST", "doesNotMatter", - "HOSTNAME", "doesNotMatter"); - assertTrue(readEnvProps(envs).isEmpty()); - } - - @Test - public void shouldReturnEmptyOnMissingVESAppName() { - Map envs = Map( - "CONSUL_HOST", "doesNotMatter", - "CONFIG_BINDING_SERVICE", "doesNotMatter"); - assertTrue(readEnvProps(envs).isEmpty()); - } - - @Test - public void shouldReturnSomeOfAllProperties() { - Map envs = Map( - "CONSUL_HOST", "doesNotMatter", - "HOSTNAME", "doesNotMatter", - "CONFIG_BINDING_SERVICE", "doesNotMatter"); - assertFalse(readEnvProps(envs).isEmpty()); - } - -} - diff --git a/src/test/java/org/onap/dcae/controller/EnvPropsTest.java b/src/test/java/org/onap/dcae/controller/EnvPropsTest.java deleted file mode 100644 index ccc083e2..00000000 --- a/src/test/java/org/onap/dcae/controller/EnvPropsTest.java +++ /dev/null @@ -1,39 +0,0 @@ -package org.onap.dcae.controller; -/*- - * ============LICENSE_START======================================================= - * org.onap.dcaegen2.collectors.ves - * ================================================================================ - * Copyright (C) 2020 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========================================================= - */ -import org.jetbrains.annotations.NotNull; -import org.junit.Test; -import static org.assertj.core.api.Assertions.assertThat; -public class EnvPropsTest { - @Test - public void shouldBeEquals() { - // given - EnvProps envPropsOriginal = givenEnvProps(); - EnvProps envPropsCopy = givenEnvProps(); - // when/then - assertThat(envPropsOriginal).isEqualTo(envPropsCopy); - assertThat(envPropsOriginal.hashCode()).isEqualTo(envPropsCopy.hashCode()); - } - @NotNull - private EnvProps givenEnvProps() { - return new EnvProps("https", "localhost", 443, - "https", "cbsName", "appName"); - } -} diff --git a/src/test/java/org/onap/dcae/restapi/VesRestControllerTest.java b/src/test/java/org/onap/dcae/restapi/VesRestControllerTest.java index 765f2b43..0a03c1a4 100644 --- a/src/test/java/org/onap/dcae/restapi/VesRestControllerTest.java +++ b/src/test/java/org/onap/dcae/restapi/VesRestControllerTest.java @@ -41,7 +41,7 @@ import org.onap.dcae.common.EventTransformation; import org.onap.dcae.common.HeaderUtils; import org.onap.dcae.common.JsonDataLoader; import org.onap.dcae.common.validator.StndDefinedDataValidator; -import org.onap.dcae.common.publishing.EventPublisher; +import org.onap.dcae.common.publishing.DMaaPEventPublisher; import org.slf4j.Logger; import org.springframework.http.ResponseEntity; import org.springframework.mock.web.MockHttpServletRequest; @@ -84,7 +84,7 @@ public class VesRestControllerTest { private HeaderUtils headerUtils; @Mock - private EventPublisher eventPublisher; + private DMaaPEventPublisher eventPublisher; @Mock private StndDefinedDataValidator stndDefinedDataValidator; @@ -320,7 +320,7 @@ public class VesRestControllerTest { ); } - private void verifyThatTransformedEventWasSend(EventPublisher eventPublisher, String eventBeforeTransformation) { + private void verifyThatTransformedEventWasSend(DMaaPEventPublisher eventPublisher, String eventBeforeTransformation) { // event before transformation assertThat(eventBeforeTransformation).contains("\"version\": \"4.0.1\""); assertThat(eventBeforeTransformation).contains("\"faultFieldsVersion\": \"4.0\""); -- cgit 1.2.3-korg