diff options
author | k.kedron <k.kedron@partner.samsung.com> | 2019-05-24 11:06:02 +0200 |
---|---|---|
committer | Avi Gaffa <avi.gaffa@amdocs.com> | 2019-05-30 13:13:12 +0000 |
commit | 7c62480edb52c829102cddd030384176bf16780b (patch) | |
tree | 7bb20d3ad28d3ce30f68e209f6159e23a2374d85 /common/onap-common-configuration-management/onap-configuration-management-core/src/main | |
parent | 937803bdd4b6e459093e42d784195a60556a7021 (diff) |
Improve unit test for ConfigurationUtils and sonar fixes
Add more test for ConfigurationUtils.
Fix error in getCompatibleCollectionForAbstractDef method bad return value
for TransferQueue.class.
Sonar fixes:
- use not deprecated method IOUtils.toString.
- remove code duplication.
- return empty collection instead of null.
Issue-ID: SDC-2327
Signed-off-by: Krystian Kedron <k.kedron@partner.samsung.com>
Change-Id: I5abb10bca0a1c409ec20daf6c22066836a0b76a3
Diffstat (limited to 'common/onap-common-configuration-management/onap-configuration-management-core/src/main')
-rw-r--r-- | common/onap-common-configuration-management/onap-configuration-management-core/src/main/java/org/onap/config/ConfigurationUtils.java | 35 |
1 files changed, 19 insertions, 16 deletions
diff --git a/common/onap-common-configuration-management/onap-configuration-management-core/src/main/java/org/onap/config/ConfigurationUtils.java b/common/onap-common-configuration-management/onap-configuration-management-core/src/main/java/org/onap/config/ConfigurationUtils.java index bdba27a420..b18649181d 100644 --- a/common/onap-common-configuration-management/onap-configuration-management-core/src/main/java/org/onap/config/ConfigurationUtils.java +++ b/common/onap-common-configuration-management/onap-configuration-management-core/src/main/java/org/onap/config/ConfigurationUtils.java @@ -12,6 +12,9 @@ * 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. + * + * Modifications Copyright (c) 2019 Samsung + * */ package org.onap.config; @@ -28,6 +31,7 @@ import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; import java.net.MalformedURLException; import java.net.URL; +import java.nio.charset.Charset; import java.nio.file.Files; import java.nio.file.Path; import java.util.ArrayDeque; @@ -108,7 +112,7 @@ public class ConfigurationUtils { ArrayList<File> collection = new ArrayList<>(); if (file.isDirectory() && file.exists()) { File[] files = file.listFiles(); - for (File innerFile : files) { + for (File innerFile : Objects.requireNonNull(files)) { if (innerFile.isFile() && !onlyDirectory) { collection.add(innerFile); } else if (innerFile.isDirectory()) { @@ -430,20 +434,17 @@ public class ConfigurationUtils { Matcher matcher = pattern.matcher(data); if (matcher.matches()) { String key = matcher.group(1); + String value; if (key.toUpperCase().startsWith("ENV:")) { - String envValue = System.getenv(key.substring(4)); - return processVariablesIfPresent(tenant, namespace, data.replaceAll("\\$\\{" + key + "\\}", - envValue == null ? "" : envValue.replace("\\", "\\\\"))); + value = System.getenv(key.substring(4)); } else if (key.toUpperCase().startsWith("SYS:")) { - String sysValue = System.getProperty(key.substring(4)); - return processVariablesIfPresent(tenant, namespace, data.replaceAll("\\$\\{" + key + "\\}", - sysValue == null ? "" : sysValue.replace("\\", "\\\\"))); + value = System.getProperty(key.substring(4)); } else { - String propertyValue = ConfigurationUtils.getCollectionString( + value = ConfigurationUtils.getCollectionString( ConfigurationManager.lookup().getAsStringValues(tenant, namespace, key).toString()); - return processVariablesIfPresent(tenant, namespace, data.replaceAll("\\$\\{" + key + "\\}", - propertyValue == null ? "" : propertyValue.replace("\\", "\\\\"))); } + return processVariablesIfPresent(tenant, namespace, data.replaceAll("\\$\\{" + key + "}", + value == null ? "" : value.replace("\\", "\\\\"))); } else { return data; } @@ -452,7 +453,7 @@ public class ConfigurationUtils { public static String getFileContents(String path) { try { if (path != null) { - return IOUtils.toString(new URL(path)); + return IOUtils.toString(new URL(path), Charset.defaultCharset()); } } catch (Exception exception) { exception.printStackTrace(); @@ -491,12 +492,12 @@ public class ConfigurationUtils { } public static Collection getCompatibleCollectionForAbstractDef(Class clazz) { - if (BlockingQueue.class.isAssignableFrom(clazz)) { - return getConcreteCollection(BlockingQueue.class); - } if (TransferQueue.class.isAssignableFrom(clazz)) { return getConcreteCollection(TransferQueue.class); } + if (BlockingQueue.class.isAssignableFrom(clazz)) { + return getConcreteCollection(BlockingQueue.class); + } if (Deque.class.isAssignableFrom(clazz)) { return getConcreteCollection(Deque.class); } @@ -512,7 +513,8 @@ public class ConfigurationUtils { if (List.class.isAssignableFrom(clazz)) { return getConcreteCollection(List.class); } - return null; + throw new IllegalArgumentException("Only corresponding array classes and any are allowed as argument." + + "assignable from TransferQueue, BlockingQueue, Deque, Queue, SortedSet, Set, List class"); } public static Collection getConcreteCollection(Class clazz) { @@ -533,7 +535,8 @@ public class ConfigurationUtils { case "java.util.concurrent.BlockingQueue": return new LinkedBlockingQueue<>(); default: - return null; + throw new IllegalArgumentException("Only corresponding array classes and any are allowed as argument." + + "assignable from TransferQueue, BlockingQueue, Deque, Queue, SortedSet, Set, List class"); } } |