From 1599617246f0ffec1b1c7840f9c7c42318183dcd Mon Sep 17 00:00:00 2001 From: Bogumil Zebek Date: Fri, 30 Oct 2020 10:23:45 +0100 Subject: Fetch configuration from CBS - Fix memory leak. - Add reactive configuration fetching from Consul. Now configuration is updated when any change in VES configuration has been done in Consul. Change-Id: I9cd42e04844c9e99d4d03951185523b569dc9483 Issue-ID: DCAEGEN2-2495 Signed-off-by: Zebek Bogumil --- .../dcae/ApplicationConfigurationListenerTest.java | 67 ++++++++++++ .../configuration/CbsConfigurationHandlerTest.java | 117 +++++++++++++++++++++ .../configuration/ConfigLoaderFactoryTest.java | 43 -------- .../onap/dcae/configuration/ConfigLoaderTest.java | 102 ++++++++---------- .../configuration/ConfigUpdaterFactoryTest.java | 43 ++++++++ .../cbs/CbsClientConfigurationProviderTest.java | 47 +++++++++ .../cbs/CbsClientConfigurationResolverTest.java | 47 --------- .../configuration/cbs/CbsClientFactoryTest.java | 36 ------- .../configuration/cbs/CbsConfigResolverTest.java | 88 ---------------- 9 files changed, 316 insertions(+), 274 deletions(-) create mode 100644 src/test/java/org/onap/dcae/ApplicationConfigurationListenerTest.java create mode 100644 src/test/java/org/onap/dcae/configuration/CbsConfigurationHandlerTest.java delete mode 100644 src/test/java/org/onap/dcae/configuration/ConfigLoaderFactoryTest.java create mode 100644 src/test/java/org/onap/dcae/configuration/ConfigUpdaterFactoryTest.java create mode 100644 src/test/java/org/onap/dcae/configuration/cbs/CbsClientConfigurationProviderTest.java delete mode 100644 src/test/java/org/onap/dcae/configuration/cbs/CbsClientConfigurationResolverTest.java delete mode 100644 src/test/java/org/onap/dcae/configuration/cbs/CbsClientFactoryTest.java delete mode 100644 src/test/java/org/onap/dcae/configuration/cbs/CbsConfigResolverTest.java (limited to 'src/test/java/org/onap/dcae') diff --git a/src/test/java/org/onap/dcae/ApplicationConfigurationListenerTest.java b/src/test/java/org/onap/dcae/ApplicationConfigurationListenerTest.java new file mode 100644 index 00000000..49132f1e --- /dev/null +++ b/src/test/java/org/onap/dcae/ApplicationConfigurationListenerTest.java @@ -0,0 +1,67 @@ +/*- + * ============LICENSE_START======================================================= + * VES Collector + * ================================================================================ + * 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; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.Spy; +import org.mockito.junit.MockitoJUnitRunner; +import org.onap.dcae.configuration.ConfigurationHandler; + +import java.time.Duration; + +import static org.mockito.ArgumentMatchers.any; + +@RunWith(MockitoJUnitRunner.class) +public class ApplicationConfigurationListenerTest { + + @Mock + private ConfigurationHandler configurationHandler; + + @InjectMocks + @Spy + private ApplicationConfigurationListener applicationConfigurationListener; + + @Test + public void shouldStopJobAndCloseConnectionWhenErrorOccurredDuringListenAtConsulChange() { + + // given + Mockito.doThrow(new RuntimeException("Simulate exception")).when(configurationHandler).startListen(any()); + + // when + applicationConfigurationListener.run(); + + // then + Mockito.verify(applicationConfigurationListener).stopListeningForConfigurationUpdates(any()); + } + + @Test + public void shouldSendReloadAction() { + + // when + applicationConfigurationListener.reload(Duration.ofMillis(1)); + + // then + Mockito.verify(applicationConfigurationListener).sendReloadAction(); + } +} diff --git a/src/test/java/org/onap/dcae/configuration/CbsConfigurationHandlerTest.java b/src/test/java/org/onap/dcae/configuration/CbsConfigurationHandlerTest.java new file mode 100644 index 00000000..7b1af1de --- /dev/null +++ b/src/test/java/org/onap/dcae/configuration/CbsConfigurationHandlerTest.java @@ -0,0 +1,117 @@ +/* + * ============LICENSE_START======================================================= + * VES Collector + * ================================================================================ + * Copyright (C) 2020 Nokia. All rights reserved.s + * ================================================================================ + * 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 com.google.gson.JsonObject; +import com.google.gson.JsonParser; +import io.vavr.control.Option; +import org.json.JSONObject; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.ArgumentCaptor; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Spy; +import org.mockito.junit.MockitoJUnitRunner; +import org.onap.dcae.configuration.cbs.CbsClientConfigurationProvider; +import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.api.CbsClient; +import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.CbsClientConfiguration; +import reactor.core.Disposable; +import reactor.core.publisher.Mono; + +import java.time.Duration; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +@RunWith(MockitoJUnitRunner.class) +public class CbsConfigurationHandlerTest { + + private static final String VES_CONFIG = "{\"collector.port\": 8081}"; + private static final String VES_CONSUL_CONFIG = String.format("{\"config\":%s}", VES_CONFIG); + + @Mock + private CbsClientConfigurationProvider cbsClientConfigurationProvider; + @Mock + private CbsClientConfiguration cbsClientConfiguration; + @Mock + private Mono cbsClient; + @Mock + private ConfigUpdater configLoader; + + @InjectMocks + @Spy + private ConfigurationHandler cbsConfigurationHandler; + + @Test + public void shouldCreateCbsConfigurationHandler() { + // given + + when(cbsConfigurationHandler.createCbsClient(cbsClientConfiguration)).thenReturn(cbsClient); + when(cbsClientConfigurationProvider.get()).thenReturn(cbsClientConfiguration); + + // when + final Disposable handler = cbsConfigurationHandler.startListen(Duration.ofMinutes(5)); + + // then + assertThat(handler).isNotNull(); + } + + @Test + public void shouldUpdateAppConfigurationWhenConfigurationIsValid() { + // given + final JsonObject configuration = createConsulConfiguration(VES_CONSUL_CONFIG); + + // when + this.cbsConfigurationHandler.handleConfigurationFromConsul(configuration); + + // then + final ArgumentCaptor> acConfiguration = ArgumentCaptor.forClass(Option.class); + verify(configLoader).updateConfig(acConfiguration.capture()); + assertThat(acConfiguration.getValue().get().toString()).hasToString(createJSONObject(VES_CONFIG)); + } + + @Test + public void shouldReportAnErrorWhenConsulReturnsEmptyConfiguration() { + // given + final JsonObject configuration = createConsulConfiguration("{}"); + + // when + assertThatThrownBy(() -> this.cbsConfigurationHandler.handleConfigurationFromConsul(configuration)) + .isInstanceOf(IllegalArgumentException.class).hasMessageContaining(String.format("Invalid application configuration: %s ", "{}")); + + // then + verify(configLoader, never()).updateConfig(any()); + } + + private String createJSONObject(String vesConfig) { + return new JSONObject(vesConfig).toString(); + } + + private JsonObject createConsulConfiguration(String vesConsulConfig) { + return new JsonParser().parse(vesConsulConfig).getAsJsonObject(); + } + + +} diff --git a/src/test/java/org/onap/dcae/configuration/ConfigLoaderFactoryTest.java b/src/test/java/org/onap/dcae/configuration/ConfigLoaderFactoryTest.java deleted file mode 100644 index e9421909..00000000 --- a/src/test/java/org/onap/dcae/configuration/ConfigLoaderFactoryTest.java +++ /dev/null @@ -1,43 +0,0 @@ -/*- - * ============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 index 46d864a0..4d5fd921 100644 --- a/src/test/java/org/onap/dcae/configuration/ConfigLoaderTest.java +++ b/src/test/java/org/onap/dcae/configuration/ConfigLoaderTest.java @@ -40,7 +40,6 @@ 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 { @@ -53,14 +52,12 @@ public class ConfigLoaderTest { 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; + private ConfigUpdater configLoader; @Mock private Runnable applicationRestarter; @@ -72,29 +69,15 @@ public class ConfigLoaderTest { 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); + Map properties = HashMap.of(COLLECTOR_PORT, PORT_8080); mockVesInitialProperties(properties); - mockVesConfigInCbs(properties); + final Option configuration = givenVesConfigInCbs(properties); // when - configLoader.updateConfig(); + configLoader.updateConfig(configuration); // then verify(configFilesFacadeMock, never()).writeProperties(any()); @@ -104,13 +87,13 @@ public class ConfigLoaderTest { @Test public void shouldUpdatePropertiesWhenSameKeySetButDifferentValues() { // given - HashMap initialProperties = HashMap.of(COLLECTOR_PORT, PORT_8080); - HashMap cbsProperties = HashMap.of(COLLECTOR_PORT, PORT_8081); + Map initialProperties = HashMap.of(COLLECTOR_PORT, PORT_8080); + Map cbsProperties = HashMap.of(COLLECTOR_PORT, PORT_8081); mockVesInitialProperties(initialProperties); - mockVesConfigInCbs(cbsProperties); + final Option configuration = givenVesConfigInCbs(cbsProperties); // when - configLoader.updateConfig(); + configLoader.updateConfig(configuration); // then verify(configFilesFacadeMock, times(1)).writeProperties(cbsProperties); @@ -120,16 +103,16 @@ public class ConfigLoaderTest { @Test public void shouldUpdatePropertiesWhenVesKeysAreSubsetOfCbsKeysAndSubsetHasSameValues() { // given - HashMap initialProperties = HashMap.of( + Map initialProperties = HashMap.of( COLLECTOR_PORT, PORT_8080); - HashMap cbsProperties = HashMap.of( + Map cbsProperties = HashMap.of( COLLECTOR_PORT, PORT_8080, COLLECTOR_KEYSTORE_FILE_LOCATION, SOME_PATH); mockVesInitialProperties(initialProperties); - mockVesConfigInCbs(cbsProperties); + final Option configuration = givenVesConfigInCbs(cbsProperties); // when - configLoader.updateConfig(); + configLoader.updateConfig(configuration); // then verify(configFilesFacadeMock, times(1)).writeProperties(cbsProperties); @@ -138,16 +121,16 @@ public class ConfigLoaderTest { @Test public void shouldUpdatePropertiesWhenVesKeysAreSubsetOfCbsKeysAndSubsetHasDifferentValues() { - HashMap initialProperties = HashMap.of( + Map initialProperties = HashMap.of( COLLECTOR_PORT, PORT_8080); - HashMap cbsProperties = HashMap.of( + Map cbsProperties = HashMap.of( COLLECTOR_PORT, PORT_8081, COLLECTOR_KEYSTORE_FILE_LOCATION, SOME_PATH); mockVesInitialProperties(initialProperties); - mockVesConfigInCbs(cbsProperties); + final Option configuration = givenVesConfigInCbs(cbsProperties); // when - configLoader.updateConfig(); + configLoader.updateConfig(configuration); // then verify(configFilesFacadeMock, times(1)).writeProperties(cbsProperties); @@ -156,16 +139,16 @@ public class ConfigLoaderTest { @Test public void shouldNotUpdatePropertiesWhenCbsKeysAreSubsetOfVesKeysAndSubsetHasSameValues() { - HashMap initialProperties = HashMap.of( + Map initialProperties = HashMap.of( COLLECTOR_PORT, PORT_8080, COLLECTOR_KEYSTORE_FILE_LOCATION, SOME_PATH); - HashMap cbsProperties = HashMap.of( + Map cbsProperties = HashMap.of( COLLECTOR_PORT, PORT_8080); mockVesInitialProperties(initialProperties); - mockVesConfigInCbs(cbsProperties); + final Option configuration = givenVesConfigInCbs(cbsProperties); // when - configLoader.updateConfig(); + configLoader.updateConfig(configuration); // then verify(configFilesFacadeMock, never()).writeProperties(any()); @@ -174,16 +157,16 @@ public class ConfigLoaderTest { @Test public void shouldUpdatePropertiesWhenCbsKeysAreSubsetOfVesKeysAndSubsetHasDifferentValues() { - HashMap initialProperties = HashMap.of( + Map initialProperties = HashMap.of( COLLECTOR_PORT, PORT_8080, COLLECTOR_KEYSTORE_FILE_LOCATION, SOME_PATH); - HashMap cbsProperties = HashMap.of( + Map cbsProperties = HashMap.of( COLLECTOR_PORT, PORT_8081); mockVesInitialProperties(initialProperties); - mockVesConfigInCbs(cbsProperties); + final Option configuration = givenVesConfigInCbs(cbsProperties); // when - configLoader.updateConfig(); + configLoader.updateConfig(configuration); // then verify(configFilesFacadeMock, times(1)).writeProperties(cbsProperties); @@ -192,18 +175,18 @@ public class ConfigLoaderTest { @Test public void shouldUpdatePropertiesWhenVesAndCbsKeySetsIntersectAndIntersectingKeysHaveSameValues() { - HashMap initialProperties = HashMap.of( + Map initialProperties = HashMap.of( COLLECTOR_PORT, PORT_8080, COLLECTOR_KEYSTORE_FILE_LOCATION, SOME_PATH); - HashMap cbsProperties = HashMap.of( + Map cbsProperties = HashMap.of( COLLECTOR_PORT, PORT_8080, COLLECTOR_SCHEMA_FILE, SOME_SCHEMA ); mockVesInitialProperties(initialProperties); - mockVesConfigInCbs(cbsProperties); + final Option configuration = givenVesConfigInCbs(cbsProperties); // when - configLoader.updateConfig(); + configLoader.updateConfig(configuration); // then verify(configFilesFacadeMock, times(1)).writeProperties(cbsProperties); @@ -212,18 +195,17 @@ public class ConfigLoaderTest { @Test public void shouldUpdatePropertiesWhenVesAndCbsKeySetsIntersectAndIntersectingKeysHaveDifferentValues() { - HashMap initialProperties = HashMap.of( + Map initialProperties = HashMap.of( COLLECTOR_PORT, PORT_8080, COLLECTOR_KEYSTORE_FILE_LOCATION, SOME_PATH); - HashMap cbsProperties = HashMap.of( + Map cbsProperties = HashMap.of( COLLECTOR_PORT, PORT_8081, COLLECTOR_SCHEMA_FILE, SOME_SCHEMA ); mockVesInitialProperties(initialProperties); - mockVesConfigInCbs(cbsProperties); - + final Option configuration = givenVesConfigInCbs(cbsProperties); // when - configLoader.updateConfig(); + configLoader.updateConfig(configuration); // then verify(configFilesFacadeMock, times(1)).writeProperties(cbsProperties); @@ -236,10 +218,10 @@ public class ConfigLoaderTest { JSONObject emptyDmaapConfig = new JSONObject(); JSONObject dmaapConfig = loadSampleDmaapConfig(); mockVesInitialDmaapConfig(emptyDmaapConfig); - mockVesDmaapConfigInCbs(dmaapConfig); + final Option configuration = givenVesDmaapConfigInCbs(dmaapConfig); // when - configLoader.updateConfig(); + configLoader.updateConfig(configuration); // then verify(configFilesFacadeMock).writeDMaaPConfiguration(argThat(dmaapConfig::similar)); @@ -251,10 +233,10 @@ public class ConfigLoaderTest { // given JSONObject dmaapConf = loadSampleDmaapConfig(); mockVesInitialDmaapConfig(dmaapConf); - mockVesDmaapConfigInCbs(dmaapConf); + final Option configuration = givenVesDmaapConfigInCbs(dmaapConf); // when - configLoader.updateConfig(); + configLoader.updateConfig(configuration); // then verify(configFilesFacadeMock, never()).writeDMaaPConfiguration(any()); @@ -265,16 +247,16 @@ public class ConfigLoaderTest { when(configFilesFacadeMock.readDMaaPConfiguration()).thenReturn(Try.of(() -> dmaapConf)); } - private void mockVesDmaapConfigInCbs(JSONObject dmaapConf) { + private Option givenVesDmaapConfigInCbs(JSONObject dmaapConf) { JSONObject jsonObject = new JSONObject(f("{\"streams_publishes\": %s}}", dmaapConf)); - when(cbsConfigResolverMock.getAppConfig()).thenReturn(Option.of(jsonObject)); + return Option.of(jsonObject); } - private void mockVesConfigInCbs(HashMap properties) { - when(cbsConfigResolverMock.getAppConfig()).thenReturn(Option.of(prepareConfigurationJson(properties))); + private Option givenVesConfigInCbs(Map properties) { + return Option.of(prepareConfigurationJson(properties)); } - private void mockVesInitialProperties(HashMap properties) { + private void mockVesInitialProperties(Map properties) { when(configFilesFacadeMock.readCollectorProperties()).thenReturn(Try.of(() -> properties)); } @@ -291,4 +273,4 @@ public class ConfigLoaderTest { 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/ConfigUpdaterFactoryTest.java b/src/test/java/org/onap/dcae/configuration/ConfigUpdaterFactoryTest.java new file mode 100644 index 00000000..352fba1d --- /dev/null +++ b/src/test/java/org/onap/dcae/configuration/ConfigUpdaterFactoryTest.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 ConfigUpdaterFactoryTest { + + @Test + public void createsCbsConfigUpdaterSuccessfully() { + // given + Path testPropertiesPath = Path.of("src/test/resources/testcollector.properties"); + Path testDmaapConfigPath = Path.of("src/test/resources/testParseDMaaPCredentialsGen2.json"); + + // when + ConfigUpdater configLoader = ConfigUpdaterFactory.create( + testPropertiesPath, + testDmaapConfigPath); + + // then + assertThat(configLoader).isNotNull(); + } +} diff --git a/src/test/java/org/onap/dcae/configuration/cbs/CbsClientConfigurationProviderTest.java b/src/test/java/org/onap/dcae/configuration/cbs/CbsClientConfigurationProviderTest.java new file mode 100644 index 00000000..b3c06d9c --- /dev/null +++ b/src/test/java/org/onap/dcae/configuration/cbs/CbsClientConfigurationProviderTest.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 CbsClientConfigurationProviderTest { + + 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 CbsClientConfigurationProvider().get(); + + // 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); + } +} diff --git a/src/test/java/org/onap/dcae/configuration/cbs/CbsClientConfigurationResolverTest.java b/src/test/java/org/onap/dcae/configuration/cbs/CbsClientConfigurationResolverTest.java deleted file mode 100644 index 57824595..00000000 --- a/src/test/java/org/onap/dcae/configuration/cbs/CbsClientConfigurationResolverTest.java +++ /dev/null @@ -1,47 +0,0 @@ -/*- - * ============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 deleted file mode 100644 index 4b17f68d..00000000 --- a/src/test/java/org/onap/dcae/configuration/cbs/CbsClientFactoryTest.java +++ /dev/null @@ -1,36 +0,0 @@ -/*- - * ============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 deleted file mode 100644 index 6f729997..00000000 --- a/src/test/java/org/onap/dcae/configuration/cbs/CbsConfigResolverTest.java +++ /dev/null @@ -1,88 +0,0 @@ -/*- - * ============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.InjectMocks; -import org.mockito.Mock; -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; -import static org.mockito.Mockito.when; - -@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()); - - @Mock - private CbsClientConfigurationResolver cbsClientConfigurationResolver; - - @InjectMocks - private CbsConfigResolver cbsConfigResolver; - - @Test - public void shouldFetchConfigurationFromCBS() { - // given - stubCBSToReturnAppConfig(); - mockCbsClientConfiguration(); - - // when - JSONObject appConfig = cbsConfigResolver.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))); - } - - private void mockCbsClientConfiguration() { - final int PORT = wireMockRule.port(); - CbsClientConfiguration cbsClientConfiguration = ImmutableCbsClientConfiguration.builder() - .protocol(PROTOCOL) - .hostname(HOSTNAME) - .port(PORT) - .appName(APP_NAME) - .build(); - when(cbsClientConfigurationResolver.resolveCbsClientConfiguration()).thenReturn(cbsClientConfiguration); - } -} \ No newline at end of file -- cgit 1.2.3-korg