diff options
4 files changed, 105 insertions, 43 deletions
diff --git a/prh-app-server/src/main/java/org/onap/dcaegen2/services/bootstrap/CbsJsonToPropertyMapConverter.java b/prh-app-server/src/main/java/org/onap/dcaegen2/services/bootstrap/CbsJsonToPropertyMapConverter.java index bf4077b9..79bf4656 100644 --- a/prh-app-server/src/main/java/org/onap/dcaegen2/services/bootstrap/CbsJsonToPropertyMapConverter.java +++ b/prh-app-server/src/main/java/org/onap/dcaegen2/services/bootstrap/CbsJsonToPropertyMapConverter.java @@ -20,12 +20,12 @@ package org.onap.dcaegen2.services.bootstrap; -import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.JsonPrimitive; import java.util.Map; -import java.util.stream.Collectors; + +import static java.util.stream.Collectors.toMap; class CbsJsonToPropertyMapConverter { @@ -34,8 +34,9 @@ class CbsJsonToPropertyMapConverter { Map<String, Object> convertToMap(JsonObject jsonObject) { verifyExpectedCbsJsonFormat(jsonObject); JsonObject config = jsonObject.getAsJsonObject(CBS_CONFIG_ROOT_PROPERTY); - return config.entrySet().stream().collect( - Collectors.toMap(Map.Entry::getKey, entry -> unpack(entry.getValue()))); + return config.entrySet().stream() + .filter(entry -> entry.getValue().isJsonPrimitive()) + .collect(toMap(Map.Entry::getKey, entry -> unpack(entry.getValue().getAsJsonPrimitive()))); } private static void verifyExpectedCbsJsonFormat(JsonObject jsonObject) { @@ -45,20 +46,17 @@ class CbsJsonToPropertyMapConverter { } } - private Object unpack(JsonElement value) { - if (value.isJsonPrimitive()) { - JsonPrimitive primitiveValue = value.getAsJsonPrimitive(); - if (primitiveValue.isString()) { - return primitiveValue.getAsString(); - } - if (primitiveValue.isBoolean()) { - return primitiveValue.getAsBoolean(); - } - if (primitiveValue.isNumber()) { - return primitiveValue.getAsLong(); - } + private Object unpack(JsonPrimitive value) { + if (value.isString()) { + return value.getAsString(); + } + if (value.isBoolean()) { + return value.getAsBoolean(); + } + if (value.isNumber()) { + return value.getAsLong(); } - return value; + throw new IllegalArgumentException("Unexpected JsonPrimitive type found in configuration form CBS: " + value); } } diff --git a/prh-app-server/src/test/java/org/onap/dcaegen2/services/bootstrap/CbsClientConfigurationResolverTest.java b/prh-app-server/src/test/java/org/onap/dcaegen2/services/bootstrap/CbsClientConfigurationResolverTest.java index 2f871618..87dd18ca 100644 --- a/prh-app-server/src/test/java/org/onap/dcaegen2/services/bootstrap/CbsClientConfigurationResolverTest.java +++ b/prh-app-server/src/test/java/org/onap/dcaegen2/services/bootstrap/CbsClientConfigurationResolverTest.java @@ -20,32 +20,32 @@ package org.onap.dcaegen2.services.bootstrap; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.condition.*; -import org.junit.jupiter.api.extension.ExtendWith; -import org.mockito.Mock; -import org.mockito.junit.jupiter.MockitoExtension; +import org.junit.jupiter.api.condition.DisabledIfEnvironmentVariable; import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.CbsClientConfiguration; -import static org.junit.jupiter.api.Assertions.assertSame; -import static org.mockito.Mockito.when; +import static org.assertj.core.api.Assertions.assertThat; -@ExtendWith(MockitoExtension.class) class CbsClientConfigurationResolverTest { - @Mock private CbsProperties cbsProperties; - @Mock - private CbsClientConfiguration defaultCbsClientConfigFromSpringProps; + + @BeforeEach + void setUp() { + cbsProperties = new CbsProperties(); + cbsProperties.setHostname("some-cbs-host"); + cbsProperties.setPort(123); + cbsProperties.setAppName("client-app-name"); + } @Test @DisabledIfEnvironmentVariable(named = "CONFIG_BINDING_SERVICE", matches = ".+") - void whenCbsEnvPropertiesAreNotePresentInEnvironment_ShouldFallbackToLoadingDefaults() { - when(cbsProperties.toCbsClientConfiguration()).thenReturn(defaultCbsClientConfigFromSpringProps); - CbsClientConfigurationResolver cbsClientConfigurationResolver = new CbsClientConfigurationResolver(cbsProperties); - - CbsClientConfiguration config = cbsClientConfigurationResolver.resolveCbsClientConfiguration(); + void whenCbsEnvPropertiesAreNotePresentInEnvironment_ShouldFallbackToLoadingDefaultsFromCbsProperties() { + CbsClientConfiguration config = new CbsClientConfigurationResolver(cbsProperties).resolveCbsClientConfiguration(); - assertSame(defaultCbsClientConfigFromSpringProps, config); + assertThat(config.hostname()).isEqualTo(cbsProperties.getHostname()); + assertThat(config.port()).isEqualTo(cbsProperties.getPort()); + assertThat(config.appName()).isEqualTo(cbsProperties.getAppName()); } }
\ No newline at end of file diff --git a/prh-app-server/src/test/java/org/onap/dcaegen2/services/bootstrap/CbsJsonToPropertyMapConverterTest.java b/prh-app-server/src/test/java/org/onap/dcaegen2/services/bootstrap/CbsJsonToPropertyMapConverterTest.java index da9a0008..55e1b014 100644 --- a/prh-app-server/src/test/java/org/onap/dcaegen2/services/bootstrap/CbsJsonToPropertyMapConverterTest.java +++ b/prh-app-server/src/test/java/org/onap/dcaegen2/services/bootstrap/CbsJsonToPropertyMapConverterTest.java @@ -22,7 +22,6 @@ package org.onap.dcaegen2.services.bootstrap; import com.google.gson.JsonObject; import com.google.gson.JsonParser; -import org.assertj.core.api.Condition; import org.junit.jupiter.api.Test; import java.util.Map; @@ -38,6 +37,7 @@ class CbsJsonToPropertyMapConverterTest { " \"someStringProp\": \"foo\",\n" + " \"someNumericalProp\": 123,\n" + " \"someBooleanProp\": true,\n" + + " \"someArrayProp\": [],\n" + " \"someObjectProp\": {\n" + " \"bar\": \"baz\"\n" + " }\n" + @@ -58,10 +58,11 @@ class CbsJsonToPropertyMapConverterTest { } @Test - void shouldLeaveComplexPropertiesAsJsonTypes() { + void shouldSkipComplexProperties() { Map<String, Object> map = cbsJsonToPropertyMapConverter.convertToMap(SOME_JSON_FROM_CBS); - assertThat(map).hasEntrySatisfying("someObjectProp", hasClass(JsonObject.class)); + assertThat(map).doesNotContainKeys("someObjectProp"); + assertThat(map).doesNotContainKeys("someArrayProp"); } @Test @@ -76,11 +77,4 @@ class CbsJsonToPropertyMapConverterTest { return new JsonParser().parse(jsonString).getAsJsonObject(); } - private static Condition<Object> hasClass(Class clazz) { - return new Condition<Object>(clazz.getCanonicalName()){ - public boolean matches(Object value) { - return value.getClass().equals(clazz); - } - }; - } }
\ No newline at end of file diff --git a/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/configuration/CbsConfigurationTest.java b/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/configuration/CbsConfigurationTest.java new file mode 100644 index 00000000..7f5d26fc --- /dev/null +++ b/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/configuration/CbsConfigurationTest.java @@ -0,0 +1,70 @@ +/* + * ============LICENSE_START======================================================= + * PNF-REGISTRATION-HANDLER + * ================================================================================ + * Copyright (C) 2019 NOKIA 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.dcaegen2.services.prh.configuration; + +import com.google.gson.Gson; +import com.google.gson.JsonObject; +import org.junit.jupiter.api.Test; + +import java.nio.file.Files; +import java.nio.file.Paths; + +import static java.lang.ClassLoader.getSystemResource; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +class CbsConfigurationTest { + + private static final String EXPECTED_ERROR_MESSAGE_WHEN_CBS_CONFIG_IS_NOT_INITIALIZED = "CBS config missing"; + + @Test + void whenConfigurationIsNotInitializedBasedOnDataReceivedFromCbs_shouldThrowExceptionWithDescriptiveMessage() { + assertThatThrownBy(() -> new CbsConfiguration().getAaiClientConfiguration()) + .hasMessage(EXPECTED_ERROR_MESSAGE_WHEN_CBS_CONFIG_IS_NOT_INITIALIZED); + assertThatThrownBy(() -> new CbsConfiguration().getMessageRouterPublisher()) + .hasMessage(EXPECTED_ERROR_MESSAGE_WHEN_CBS_CONFIG_IS_NOT_INITIALIZED); + assertThatThrownBy(() -> new CbsConfiguration().getMessageRouterSubscriber()) + .hasMessage(EXPECTED_ERROR_MESSAGE_WHEN_CBS_CONFIG_IS_NOT_INITIALIZED); + assertThatThrownBy(() -> new CbsConfiguration().getMessageRouterSubscribeRequest()) + .hasMessage(EXPECTED_ERROR_MESSAGE_WHEN_CBS_CONFIG_IS_NOT_INITIALIZED); + assertThatThrownBy(() -> new CbsConfiguration().getMessageRouterPublishRequest()) + .hasMessage(EXPECTED_ERROR_MESSAGE_WHEN_CBS_CONFIG_IS_NOT_INITIALIZED); + assertThatThrownBy(() -> new CbsConfiguration().getMessageRouterUpdatePublishRequest()) + .hasMessage(EXPECTED_ERROR_MESSAGE_WHEN_CBS_CONFIG_IS_NOT_INITIALIZED); + } + + + @Test + void cbsConfigurationShouldExposeDataReceivedAsJsonFromCbs() throws Exception { + JsonObject cbsConfigJson = new Gson().fromJson(new String(Files.readAllBytes(Paths.get( + getSystemResource("configurationFromCbs.json").toURI()))), JsonObject.class); + CbsConfiguration cbsConfiguration = new CbsConfiguration(); + + cbsConfiguration.parseCBSConfig(cbsConfigJson); + + assertThat(cbsConfiguration.getAaiClientConfiguration()).isNotNull(); + assertThat(cbsConfiguration.getMessageRouterPublisher()).isNotNull(); + assertThat(cbsConfiguration.getMessageRouterSubscriber()).isNotNull(); + assertThat(cbsConfiguration.getMessageRouterPublishRequest()).isNotNull(); + assertThat(cbsConfiguration.getMessageRouterSubscribeRequest()).isNotNull(); + assertThat(cbsConfiguration.getMessageRouterUpdatePublishRequest()).isNotNull(); + } +}
\ No newline at end of file |