Fix sonar issue with test coverage 92/98892/2
authorDmitry Puzikov <d.puzikov2@partner.samsung.com>
Thu, 21 Nov 2019 14:44:17 +0000 (15:44 +0100)
committerOfir Sonsino <ofir.sonsino@intl.att.com>
Sun, 1 Dec 2019 15:45:34 +0000 (15:45 +0000)
Added tests to cover the case.

Fixed provider load failure:
If more than one provider is configured and if the first if failng
try to load next one.

Change-Id: Idd7edb90e2b9b1cf21ec20e299140d3362bd1138
Issue-ID: SDC-2693
Signed-off-by: Dmitry Puzikov <d.puzikov2@partner.samsung.com>
common/onap-common-configuration-management/onap-configuration-management-api/src/main/java/org/onap/config/api/ConfigurationLoader.java
common/onap-common-configuration-management/onap-configuration-management-api/src/test/java/org/onap/config/api/ConfigurationLoaderTest.java [new file with mode: 0644]
common/onap-common-configuration-management/onap-configuration-management-api/src/test/java/org/onap/config/api/impl/ConfigurationManagerTestImpl.java [new file with mode: 0644]
common/onap-common-configuration-management/onap-configuration-management-api/src/test/resources/META-INF/services/org.onap.config.api.ConfigurationManager [new file with mode: 0644]

index 8d61701..78f8b23 100644 (file)
@@ -17,6 +17,7 @@
 package org.onap.config.api;
 
 import java.util.Iterator;
+import java.util.ServiceConfigurationError;
 import java.util.ServiceLoader;
 
 /**
@@ -34,8 +35,12 @@ class ConfigurationLoader {
 
         ServiceLoader<ConfigurationManager> loader = ServiceLoader.load(ConfigurationManager.class);
         Iterator<ConfigurationManager> configManagers = loader.iterator();
-        if (configManagers.hasNext()) {
-            return configManagers.next();
+        while (configManagers.hasNext()) {
+            try {
+                return configManagers.next();
+            } catch (ServiceConfigurationError e) {
+                // this provider loading has failed, let's try next one
+            }
         }
 
         throw new IllegalStateException("No binding found for configuration service");
diff --git a/common/onap-common-configuration-management/onap-configuration-management-api/src/test/java/org/onap/config/api/ConfigurationLoaderTest.java b/common/onap-common-configuration-management/onap-configuration-management-api/src/test/java/org/onap/config/api/ConfigurationLoaderTest.java
new file mode 100644 (file)
index 0000000..10191a8
--- /dev/null
@@ -0,0 +1,37 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * ONAP SDC
+ * ================================================================================
+ * Copyright (C) 2019 Samsung. 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.config.api;
+
+import org.junit.Test;
+import org.onap.config.api.impl.ConfigurationManagerTestImpl;
+
+import static org.junit.Assert.assertTrue;
+
+public class ConfigurationLoaderTest {
+
+    @Test
+    public void loadServiceProvider() {
+        Configuration config = ConfigurationLoader.load();
+        assertTrue(config instanceof ConfigurationManagerTestImpl);
+    }
+}
\ No newline at end of file
diff --git a/common/onap-common-configuration-management/onap-configuration-management-api/src/test/java/org/onap/config/api/impl/ConfigurationManagerTestImpl.java b/common/onap-common-configuration-management/onap-configuration-management-api/src/test/java/org/onap/config/api/impl/ConfigurationManagerTestImpl.java
new file mode 100644 (file)
index 0000000..b2e6f2b
--- /dev/null
@@ -0,0 +1,74 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * ONAP SDC
+ * ================================================================================
+ * Copyright (C) 2019 Samsung. 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.config.api.impl;
+
+import org.onap.config.api.ConfigurationManager;
+import org.onap.config.api.Hint;
+
+import java.util.Collection;
+import java.util.Map;
+
+/**
+ * Mocking implementation of ConfigurationManager interface
+ */
+public class ConfigurationManagerTestImpl implements ConfigurationManager {
+    @Override
+    public String getConfigurationValue(Map<String, Object> queryData) {
+        return null;
+    }
+
+    @Override
+    public Map<String, String> listConfiguration(Map<String, Object> query) {
+        return null;
+    }
+
+    @Override
+    public Collection<String> getTenants() {
+        return null;
+    }
+
+    @Override
+    public Collection<String> getNamespaces() {
+        return null;
+    }
+
+    @Override
+    public Collection<String> getKeys(String tenant, String namespace) {
+        return null;
+    }
+
+    @Override
+    public <T> T get(String tenant, String namespace, String key, Class<T> clazz, Hint... hints) {
+        return null;
+    }
+
+    @Override
+    public <T> Map<String, T> populateMap(String tenantId, String namespace, String key, Class<T> clazz) {
+        return null;
+    }
+
+    @Override
+    public Map generateMap(String tenantId, String namespace, String key) {
+        return null;
+    }
+}
diff --git a/common/onap-common-configuration-management/onap-configuration-management-api/src/test/resources/META-INF/services/org.onap.config.api.ConfigurationManager b/common/onap-common-configuration-management/onap-configuration-management-api/src/test/resources/META-INF/services/org.onap.config.api.ConfigurationManager
new file mode 100644 (file)
index 0000000..56d60e3
--- /dev/null
@@ -0,0 +1,3 @@
+# the order of providers intentionally follows pattern "wrong - correct"
+org.onap.config.api.impl.NonExistentProvider
+org.onap.config.api.impl.ConfigurationManagerTestImpl