From 5acfbc6eb741836a63cd75ebc2dfc60c2ff1bc5a Mon Sep 17 00:00:00 2001 From: siddharth0905 Date: Wed, 27 Dec 2017 15:28:38 +0530 Subject: Fix sonar issues - Common Methods Junit and Logger removed Change-Id: Ic1a987600a8a5ab8c236be242f0f0cd12faf9624 Issue-ID: SDC-343 Signed-off-by: siddharth0905 --- .../openecomp/core/utilities/CommonMethods.java | 55 ++++++++-------------- .../core/utilities/CommonMethodsTest.java | 31 ++++++++++++ 2 files changed, 50 insertions(+), 36 deletions(-) create mode 100644 openecomp-be/lib/openecomp-core-lib/openecomp-utilities-lib/src/test/java/org/openecomp/core/utilities/CommonMethodsTest.java (limited to 'openecomp-be/lib/openecomp-core-lib') diff --git a/openecomp-be/lib/openecomp-core-lib/openecomp-utilities-lib/src/main/java/org/openecomp/core/utilities/CommonMethods.java b/openecomp-be/lib/openecomp-core-lib/openecomp-utilities-lib/src/main/java/org/openecomp/core/utilities/CommonMethods.java index 81a82c6298..5a17f887d9 100644 --- a/openecomp-be/lib/openecomp-core-lib/openecomp-utilities-lib/src/main/java/org/openecomp/core/utilities/CommonMethods.java +++ b/openecomp-be/lib/openecomp-core-lib/openecomp-utilities-lib/src/main/java/org/openecomp/core/utilities/CommonMethods.java @@ -1,21 +1,17 @@ -/*- - * ============LICENSE_START======================================================= - * SDC - * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. - * ================================================================================ +/* + * Copyright © 2016-2017 European Support Limited + * * 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.openecomp.core.utilities; @@ -140,7 +136,7 @@ public class CommonMethods { * @return true - if the Object is null, false otherwise. */ public static boolean isEmpty(byte[] byteArray) { - return (byteArray == null || byteArray.length == 0); + return byteArray == null || byteArray.length == 0; } /** @@ -232,9 +228,10 @@ public class CommonMethods { } private static void long2string(long lng, StringBuilder buff) { + long value = lng; for (int i = 0; i < 16; i++) { - long nextByte = lng & 0xF000000000000000L; - lng <<= 4; + long nextByte = value & 0xF000000000000000L; + value <<= 4; boolean isNegative = nextByte < 0; nextByte = rightShift(nextByte, 60); @@ -265,7 +262,7 @@ public class CommonMethods { */ @SuppressWarnings("unchecked") public static T[] concat(T[] left, T[] right) { - T[] res = null; + T[] res; if (isEmpty(left)) { res = right; @@ -365,9 +362,7 @@ public class CommonMethods { public static T newInstance(Class cls) { try { return cls.newInstance(); - } catch (InstantiationException exception) { - throw new RuntimeException(exception); - } catch (IllegalAccessException exception) { + } catch (InstantiationException | IllegalAccessException exception) { throw new RuntimeException(exception); } } @@ -380,10 +375,8 @@ public class CommonMethods { */ public static String getResourcesPath(String resourceName) { URL resourceUrl = CommonMethods.class.getClassLoader().getResource(resourceName); - String resourcePath = resourceUrl.getPath(); - String dirPath = resourcePath.substring(0, resourcePath.lastIndexOf("/") + 1); - - return dirPath; + return resourceUrl != null ? resourceUrl.getPath() + .substring(0, resourceUrl.getPath().lastIndexOf("/") + 1) : null; } /** @@ -407,21 +400,13 @@ public class CommonMethods { * @return the string */ public static String printStackTrace() { - - StringWriter sw = new StringWriter(); + StringBuilder sb = new StringBuilder(); StackTraceElement[] trace = Thread.currentThread().getStackTrace(); for (StackTraceElement traceElement : trace) { - sw.write("\t " + traceElement); - sw.write(System.lineSeparator()); + sb.append("\t ").append(traceElement); + sb.append(System.lineSeparator()); } - String str = sw.toString(); - try { - sw.close(); - } catch (IOException exception) { - System.err.println(exception); - } - return str; - + return sb.toString(); } /** @@ -461,7 +446,7 @@ public class CommonMethods { */ public static String collectionToCommaSeparatedString(Collection elementCollection) { List list = new ArrayList<>(); - elementCollection.stream().forEach(element -> list.add(element)); + list.addAll(elementCollection); return listToSeparatedString(list, ','); } @@ -509,7 +494,6 @@ public class CommonMethods { */ public static String duplicateStringWithDelimiter(String arg, char separator, int numberOfDuplications) { - String res = null; StringBuilder sb = new StringBuilder(); for (int i = 0; i < numberOfDuplications; i++) { @@ -518,8 +502,7 @@ public class CommonMethods { } sb.append(arg); } - res = sb.toString(); - return res; + return sb.toString(); } /** diff --git a/openecomp-be/lib/openecomp-core-lib/openecomp-utilities-lib/src/test/java/org/openecomp/core/utilities/CommonMethodsTest.java b/openecomp-be/lib/openecomp-core-lib/openecomp-utilities-lib/src/test/java/org/openecomp/core/utilities/CommonMethodsTest.java new file mode 100644 index 0000000000..c675f8927f --- /dev/null +++ b/openecomp-be/lib/openecomp-core-lib/openecomp-utilities-lib/src/test/java/org/openecomp/core/utilities/CommonMethodsTest.java @@ -0,0 +1,31 @@ +/* + * Copyright © 2016-2017 European Support Limited + * + * 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. + */ + +package org.openecomp.core.utilities; + +import org.testng.annotations.Test; + +import static org.testng.Assert.assertTrue; + +public class CommonMethodsTest { + @Test + public void testPrintStackTrace() throws Exception { + assertTrue(CommonMethods.printStackTrace().contains("org.openecomp.core.utilities" + + ".CommonMethods.printStackTrace(CommonMethods.java:")); + assertTrue(CommonMethods.printStackTrace().contains("org.openecomp.core.utilities" + + ".CommonMethodsTest.testPrintStackTrace(CommonMethodsTest.java")); + } +} -- cgit 1.2.3-korg