aboutsummaryrefslogtreecommitdiffstats
path: root/openecomp-be/lib/openecomp-core-lib/openecomp-utilities-lib
diff options
context:
space:
mode:
authoravigaffa <avi.gaffa@amdocs.com>2017-12-31 15:07:39 +0200
committervempo <vitaliy.emporopulo@amdocs.com>2018-01-02 09:41:41 +0200
commitda5dfcd1a354807d13849c7f4ead535ccfa722fa (patch)
treeb4ef0b97d4c25a38bf4a7a8aa5d3626d6b820a17 /openecomp-be/lib/openecomp-core-lib/openecomp-utilities-lib
parent25ca73d85757869a6d9cc33861070778503ff153 (diff)
Fixing sonar Exception Handling
Change-Id: I04eb047973a3f5c07dd9dc410cb13af974e8ded1 Issue-ID: SDC-810 Signed-off-by: avigaffa <avi.gaffa@amdocs.com> Signed-off-by: vempo <vitaliy.emporopulo@amdocs.com>
Diffstat (limited to 'openecomp-be/lib/openecomp-core-lib/openecomp-utilities-lib')
-rw-r--r--openecomp-be/lib/openecomp-core-lib/openecomp-utilities-lib/src/main/java/org/openecomp/core/utilities/CommonMethods.java284
-rw-r--r--openecomp-be/lib/openecomp-core-lib/openecomp-utilities-lib/src/main/java/org/openecomp/core/utilities/file/FileUtils.java20
-rw-r--r--openecomp-be/lib/openecomp-core-lib/openecomp-utilities-lib/src/test/java/org/openecomp/core/utilities/CommonMethodsTest.java92
3 files changed, 106 insertions, 290 deletions
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 5a17f887d9..7f207c59a9 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
@@ -16,21 +16,12 @@
package org.openecomp.core.utilities;
-import org.apache.commons.codec.binary.Base64;
import org.apache.commons.collections4.MapUtils;
+import org.apache.commons.lang3.ArrayUtils;
+import org.apache.commons.lang3.StringUtils;
-import java.io.ByteArrayInputStream;
-import java.io.ByteArrayOutputStream;
-import java.io.IOException;
-import java.io.ObjectInputStream;
-import java.io.ObjectOutputStream;
-import java.io.PrintWriter;
-import java.io.Serializable;
-import java.io.StringWriter;
import java.lang.reflect.Array;
-import java.net.URL;
import java.util.ArrayList;
-import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
@@ -49,7 +40,6 @@ public class CommonMethods {
'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
};
- private static final char[] hexArray = "0123456789ABCDEF".toCharArray();
/**
* Private default constructor to prevent instantiation of the class objects.
@@ -58,108 +48,6 @@ public class CommonMethods {
}
/**
- * Serializes an object instance into byte array.
- *
- * @param object An instance to be serialized.
- * @return Java array of bytes.
- * @see #deserializeObject(byte[]) #deserializeObject(byte[])
- */
- public static byte[] serializeObject(Serializable object) {
- ByteArrayOutputStream byteArray = new ByteArrayOutputStream(2048);
- try {
- ObjectOutputStream ds = new ObjectOutputStream(byteArray);
- ds.writeObject(object);
- ds.close();
- } catch (IOException exception) {
- throw new RuntimeException(exception);
- }
-
- return byteArray.toByteArray();
- } // serializeObject
-
- /**
- * Deserializes an object instance.
- *
- * @param bytes Java array of bytes.
- * @return Deserialized instance of an object.
- * @see #serializeObject(Serializable) #serializeObject(Serializable)
- */
- public static Serializable deserializeObject(byte[] bytes) {
- Serializable obj = null;
- try {
- ObjectInputStream stream = new ObjectInputStream(new ByteArrayInputStream(bytes));
- obj = (Serializable) stream.readObject();
- stream.close();
- } catch (IOException | ClassNotFoundException exception) {
- throw new RuntimeException(exception);
- }
-
- return obj;
- } // deserializeObject
-
- /**
- * Encodes binary byte stream to ASCII format.
- *
- * @param binary An Java array of bytes in binary format.
- * @return An Java array of bytes encoded in ASCII format.
- * @see #decode(byte[]) #decode(byte[])
- */
- public static byte[] encode(byte[] binary) {
- return Base64.encodeBase64(binary);
- }
-
- /**
- * Decodes ASCII byte stream into binary format.
- *
- * @param ascii An Java array of bytes in ASCII format.
- * @return An Java array of bytes encoded in binary format.
- * @see #encode(byte[]) #encode(byte[])
- */
- public static byte[] decode(byte[] ascii) {
- return Base64.decodeBase64(ascii);
- }
-
- /**
- * Checks whether the given <tt>Object</tt> is empty.
- *
- * @param obj Object to be checked.
- * @return <tt>true</tt> - if the Object is null, <tt>false</tt> otherwise.
- */
- public static boolean isEmpty(Object obj) {
- return obj == null;
- }
-
- /**
- * Checks whether the given <tt>Object</tt> is empty.
- *
- * @param byteArray Object to be checked.
- * @return <tt>true</tt> - if the Object is null, <tt>false</tt> otherwise.
- */
- public static boolean isEmpty(byte[] byteArray) {
- return byteArray == null || byteArray.length == 0;
- }
-
- /**
- * Checks whether the given <tt>String</tt> is empty.
- *
- * @param str String object to be checked.
- * @return <tt>true</tt> - if the String is null or empty, <tt>false</tt> - otherwise.
- */
- public static boolean isEmpty(String str) {
- return str == null || str.length() == 0;
- }
-
- /**
- * Checks whether the given Java array is empty.
- *
- * @param array Java array to be checked.
- * @return <tt>true</tt> - if the array is null or empty, <tt>false</tt> - otherwise.
- */
- public static boolean isEmpty(Object[] array) {
- return array == null || array.length == 0;
- }
-
- /**
* Checks whether the given collection is empty.
*
* @param collection A collection to be checked.
@@ -170,49 +58,6 @@ public class CommonMethods {
}
/**
- * Checks whether the given map is empty.
- *
- * @param map A map to be checked.
- * @return <tt>true</tt> - if the map is null or empty, <tt>false</tt> - otherwise.
- */
- public static boolean isEmpty(Map<?, ?> map) {
- return map == null || map.isEmpty();
- }
-
- /**
- * Converts the array with Long elements to the array with long (primitive type).
- *
- * @param array input array with Long elements
- * @return array with the same elements converted to the long type (primitive)
- */
- public static long[] toPrimitive(Long[] array) {
- if (array == null) {
- return null;
- }
-
- long[] result = new long[array.length];
- for (int i = 0; i < array.length; i++) {
- result[i] = array[i] != null ? array[i] : 0L;
- }
- return result;
- }
-
- /**
- * Converts a collection to Java array.
- *
- * @param <T> Java type of the collection element.
- * @param col Collection to be converted to array
- * @param type Java type of collection/array element
- * @return An Java array of collection elements, or empty array if collection is null or empty.
- */
- @SuppressWarnings("unchecked")
- public static <T> T[] toArray(Collection<? extends T> col, Class<T> type) {
- int length = isEmpty(col) ? 0 : col.size();
- T[] array = (T[]) Array.newInstance(type, length);
- return col != null ? col.toArray(array) : array;
- }
-
- /**
* Gets an universally unique identifier (UUID).
*
* @return String representation of generated UUID.
@@ -233,7 +78,7 @@ public class CommonMethods {
long nextByte = value & 0xF000000000000000L;
value <<= 4;
boolean isNegative = nextByte < 0;
- nextByte = rightShift(nextByte, 60);
+ nextByte = nextByte >>> 60;
if (isNegative) {
nextByte |= 0x08;
@@ -243,10 +88,6 @@ public class CommonMethods {
}
}
- private static long rightShift(long lng, int num) {
- return lng >>> num;
- }
-
/**
* Concatenates two Java arrays. The method allocates a new array and copies
* all elements to it or returns one of input arrays if another one is
@@ -264,9 +105,9 @@ public class CommonMethods {
public static <T> T[] concat(T[] left, T[] right) {
T[] res;
- if (isEmpty(left)) {
+ if (ArrayUtils.isEmpty(left)) {
res = right;
- } else if (isEmpty(right)) {
+ } else if (ArrayUtils.isEmpty(right)) {
res = left;
} else {
res = (T[]) Array.newInstance(left[0].getClass(), left.length + right.length);
@@ -278,36 +119,6 @@ public class CommonMethods {
} // concat
/**
- * Casts an object to the class or interface represented by the specified
- * <tt>Class</tt> object. The method logic is similar to Java method
- * <tt>Class.cast(Object)</tt> with the only difference that unlike Java's
- * version the type name of the current object instance is specified in the
- * error message if casting fails to simplify error tracking.
- *
- * @param <B> the type parameter
- * @param <D> the type parameter
- * @param b1 An object instance to be casted to the specified Java type.
- * @param cls Target Java type.
- * @return Object instance safely casted to the requested Java type.
- * @throws ClassCastException In case which is the given object is not instance of the specified
- * Java type.
- */
- @SuppressWarnings("unchecked")
- public static <B, D> D cast(B b1, Class<D> cls) {
- D d1 = null;
- if (b1 != null) {
- if (!cls.isInstance(b1)) {
- throw new ClassCastException(String
- .format("Failed to cast from '%s' to '%s'", b1.getClass().getName(), cls.getName()));
- } else {
- d1 = (D) b1;
- }
- }
-
- return d1;
- } // cast
-
- /**
* New instance object.
*
* @param classname the classname
@@ -328,7 +139,7 @@ public class CommonMethods {
@SuppressWarnings("unchecked")
public static <T> T newInstance(String classname, Class<T> cls) {
- if (isEmpty(classname)) {
+ if (StringUtils.isEmpty(classname)) {
throw new IllegalArgumentException();
}
@@ -368,33 +179,6 @@ public class CommonMethods {
}
/**
- * Gets resources path.
- *
- * @param resourceName the resource name
- * @return the resources path
- */
- public static String getResourcesPath(String resourceName) {
- URL resourceUrl = CommonMethods.class.getClassLoader().getResource(resourceName);
- return resourceUrl != null ? resourceUrl.getPath()
- .substring(0, resourceUrl.getPath().lastIndexOf("/") + 1) : null;
- }
-
- /**
- * Gets stack trace.
- *
- * @param throwable the throwable
- * @return the stack trace
- */
- public static String getStackTrace(Throwable throwable) {
- if (null == throwable) {
- return "";
- }
- StringWriter sw = new StringWriter();
- throwable.printStackTrace(new PrintWriter(sw));
- return sw.toString();
- }
-
- /**
* Print stack trace string.
*
* @return the string
@@ -410,25 +194,6 @@ public class CommonMethods {
}
/**
- * Is equal object boolean.
- *
- * @param obj1 the obj 1
- * @param obj2 the obj 2
- * @return the boolean
- */
- public static boolean isEqualObject(Object obj1, Object obj2) {
- boolean isEqualValue = false;
- if (obj1 == null && obj2 == null) {
- isEqualValue = true;
- }
-
- if (!isEqualValue && obj1 != null && obj2 != null && obj1.equals(obj2)) {
- isEqualValue = true;
- }
- return isEqualValue;
- }
-
- /**
* Converts array of strings to comma-separated string.
*
* @param arr array of strings
@@ -445,9 +210,7 @@ public class CommonMethods {
* @return the string
*/
public static String collectionToCommaSeparatedString(Collection<String> elementCollection) {
- List<String> list = new ArrayList<>();
- list.addAll(elementCollection);
- return listToSeparatedString(list, ',');
+ return String.join(",", elementCollection);
}
/**
@@ -458,7 +221,7 @@ public class CommonMethods {
* @return the string
*/
public static String arrayToSeparatedString(String[] arr, char separator) {
- return listToSeparatedString(Arrays.asList(arr), separator);
+ return String.join(Character.toString(separator), arr);
}
/**
@@ -469,19 +232,7 @@ public class CommonMethods {
* @return the string
*/
public static String listToSeparatedString(List<String> list, char separator) {
- String res = null;
- if (null != list) {
- StringBuilder sb = new StringBuilder();
- int sz = list.size();
- for (int i = 0; i < sz; i++) {
- if (i > 0) {
- sb.append(separator);
- }
- sb.append(list.get(i));
- }
- res = sb.toString();
- }
- return res;
+ return String.join(Character.toString(separator), list);
}
/**
@@ -506,23 +257,6 @@ public class CommonMethods {
}
/**
- * Bytes to hex string.
- *
- * @param bytes the bytes
- * @return the string
- */
- public static String bytesToHex(byte[] bytes) {
- char[] hexChars = new char[bytes.length * 2];
- for (int j = 0; j < bytes.length; j++) {
- int var = bytes[j] & 0xFF;
- int x1 = j << 1;
- hexChars[x1] = hexArray[var >>> 4];
- hexChars[x1 + 1] = hexArray[var & 0x0F];
- }
- return new String(hexChars);
- }
-
- /**
* To single element set set.
*
* @param <T> the class of the objects in the set
diff --git a/openecomp-be/lib/openecomp-core-lib/openecomp-utilities-lib/src/main/java/org/openecomp/core/utilities/file/FileUtils.java b/openecomp-be/lib/openecomp-core-lib/openecomp-utilities-lib/src/main/java/org/openecomp/core/utilities/file/FileUtils.java
index 786def961a..e70ad923a2 100644
--- a/openecomp-be/lib/openecomp-core-lib/openecomp-utilities-lib/src/main/java/org/openecomp/core/utilities/file/FileUtils.java
+++ b/openecomp-be/lib/openecomp-core-lib/openecomp-utilities-lib/src/main/java/org/openecomp/core/utilities/file/FileUtils.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.file;
@@ -189,7 +185,7 @@ public class FileUtils {
return IOUtils.toByteArray(input);
} catch (IOException exception) {
throw new RuntimeException(
- "error will convertion input stream to byte array:" + exception.getMessage());
+ "error while converting input stream to byte array", exception);
}
}
@@ -203,7 +199,7 @@ public class FileUtils {
if (!fileName.contains(".")) {
return fileName;
}
- return fileName.substring(0, fileName.lastIndexOf("."));
+ return fileName.substring(0, fileName.lastIndexOf('.'));
}
public static String getFileExtension(String filename) {
@@ -264,7 +260,7 @@ public class FileUtils {
*/
YML("yml");
- private String displayName;
+ private final String displayName;
FileExtension(String displayName) {
this.displayName = displayName;
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
index c675f8927f..ff3584a8a1 100644
--- 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
@@ -18,14 +18,100 @@ package org.openecomp.core.utilities;
import org.testng.annotations.Test;
+import java.util.Arrays;
+import java.util.Collections;
+
+import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertNull;
import static org.testng.Assert.assertTrue;
public class CommonMethodsTest {
+
+ private static final String[] ARRAY = { "A", "B", "C" };
+
@Test
- public void testPrintStackTrace() throws Exception {
- assertTrue(CommonMethods.printStackTrace().contains("org.openecomp.core.utilities" +
+ public void testPrintStackTrace() {
+
+ String trace = CommonMethods.printStackTrace();
+ assertTrue(trace.contains("org.openecomp.core.utilities" +
".CommonMethods.printStackTrace(CommonMethods.java:"));
- assertTrue(CommonMethods.printStackTrace().contains("org.openecomp.core.utilities" +
+ assertTrue(trace.contains("org.openecomp.core.utilities" +
".CommonMethodsTest.testPrintStackTrace(CommonMethodsTest.java"));
}
+
+ @Test
+ public void testArrayToCommaSeparatedString() {
+ assertEquals(CommonMethods.arrayToCommaSeparatedString(ARRAY), "A,B,C");
+ }
+
+ @Test
+ public void testArrayToCommaSeparatedStringEmpty() {
+ assertEquals(CommonMethods.arrayToCommaSeparatedString(new String[0]), "");
+ }
+
+ @Test
+ public void testArrayToCommaSeparatedStringNulls() {
+ assertEquals(CommonMethods.arrayToCommaSeparatedString(new String[] { null, null }), "null,null");
+ }
+
+ @Test
+ public void testArrayToCommaSeparatedStringEmptyStrings() {
+ assertEquals(CommonMethods.arrayToCommaSeparatedString(new String[] { "", "" }), ",");
+ }
+
+ @Test(expectedExceptions = NullPointerException.class)
+ public void testArrayToCommaSeparatedStringNull() {
+ CommonMethods.arrayToCommaSeparatedString(null);
+ }
+
+ @Test
+ public void testArrayToSeparatedString() {
+ assertEquals(CommonMethods.arrayToSeparatedString(ARRAY, '/'),"A/B/C");
+ }
+
+ @Test
+ public void testArrayToSeparatedStringEmpty() {
+ assertEquals(CommonMethods.arrayToSeparatedString(new String[0], '/'),"");
+ }
+
+ @Test
+ public void testArrayToSeparatedStringNulls() {
+ assertEquals(CommonMethods.arrayToSeparatedString(new String[] {null, null}, '/'),"null/null");
+ }
+
+ @Test
+ public void testArrayToSeparatedStringEmptyStrings() {
+ assertEquals(CommonMethods.arrayToSeparatedString(new String[] {"", ""}, '/'),"/");
+ }
+
+ @Test(expectedExceptions = NullPointerException.class)
+ public void testArrayToSeparatedStringNull() {
+ CommonMethods.arrayToSeparatedString(null, '/');
+ }
+
+ @Test
+ public void testCollectionToCommaSeparatedString() {
+ assertEquals(CommonMethods.collectionToCommaSeparatedString(Arrays.asList(ARRAY)), "A,B,C");
+ }
+
+ @Test
+ public void testCollectionToCommaSeparatedStringNulls() {
+ assertEquals(CommonMethods.collectionToCommaSeparatedString(Arrays.asList(null, null)), "null,null");
+ }
+
+ @Test
+ public void testCollectionToCommaSeparatedStringEmptyStrings() {
+ assertEquals(CommonMethods.collectionToCommaSeparatedString(Arrays.asList("", "")), ",");
+ }
+
+ @Test
+ public void testCollectionToCommaSeparatedStringEmtpy() {
+ assertEquals(CommonMethods.collectionToCommaSeparatedString(Collections.emptySet()), "");
+ }
+
+ @Test(expectedExceptions = NullPointerException.class)
+ public void testCollectionToCommaSeparatedStringNull() {
+ assertNull(CommonMethods.collectionToCommaSeparatedString(null));
+ }
+
}