diff options
author | vempo <vitaliy.emporopulo@amdocs.com> | 2017-09-07 10:52:29 +0300 |
---|---|---|
committer | Vitaly Emporopulo <Vitaliy.Emporopulo@amdocs.com> | 2017-09-07 13:53:33 +0000 |
commit | b7375b2488df93ba9df57ad13ec628bbff663819 (patch) | |
tree | e8d372535ab98aa76814e16d301ac32efbeb0c75 /openecomp-be/lib/openecomp-core-lib | |
parent | 8bd8cee7d620406036e197f540439b1e226506d5 (diff) |
Fixed a few static analysis violations
Fixed mostly not closing resources (InputStream, ResultSet) and catching
Throwable instead of Exception in some modules.
Issue-ID: SDC-291
Change-Id: I34e331cc3b45c4bb3a71c301f50e1706a7b623fd
Signed-off-by: vempo <vitaliy.emporopulo@amdocs.com>
Diffstat (limited to 'openecomp-be/lib/openecomp-core-lib')
6 files changed, 77 insertions, 68 deletions
diff --git a/openecomp-be/lib/openecomp-core-lib/openecomp-facade-lib/openecomp-facade-core/src/main/java/org/openecomp/core/factory/FactoriesConfigImpl.java b/openecomp-be/lib/openecomp-core-lib/openecomp-facade-lib/openecomp-facade-core/src/main/java/org/openecomp/core/factory/FactoriesConfigImpl.java index 173f9b15b1..b9d03f4d1a 100644 --- a/openecomp-be/lib/openecomp-core-lib/openecomp-facade-lib/openecomp-facade-core/src/main/java/org/openecomp/core/factory/FactoriesConfigImpl.java +++ b/openecomp-be/lib/openecomp-core-lib/openecomp-facade-lib/openecomp-facade-core/src/main/java/org/openecomp/core/factory/FactoriesConfigImpl.java @@ -25,7 +25,9 @@ import org.openecomp.core.factory.api.FactoriesConfiguration; import org.openecomp.core.utilities.file.FileUtils; import org.openecomp.core.utilities.json.JsonUtil; +import java.io.IOException; import java.io.InputStream; +import java.net.URL; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -50,12 +52,16 @@ public final class FactoriesConfigImpl implements FactoriesConfiguration { } private void init() { - List<InputStream> factoryConfigIsList = FileUtils.getFileInputStreams(FACTORY_CONFIG_FILE_NAME); - for (InputStream factoryConfigIs : factoryConfigIsList) { - factoryMap.putAll(JsonUtil.json2Object(factoryConfigIs, Map.class)); - } - } + List<URL> factoryConfigUrlList = FileUtils.getAllLocations(FACTORY_CONFIG_FILE_NAME); + for (URL factoryConfigUrl : factoryConfigUrlList) { + try (InputStream stream = factoryConfigUrl.openStream()) { + factoryMap.putAll(JsonUtil.json2Object(stream, Map.class)); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + } } diff --git a/openecomp-be/lib/openecomp-core-lib/openecomp-nosqldb-lib/openecomp-nosqldb-api/src/main/java/org/openecomp/core/util/UniqueValueUtil.java b/openecomp-be/lib/openecomp-core-lib/openecomp-nosqldb-lib/openecomp-nosqldb-api/src/main/java/org/openecomp/core/util/UniqueValueUtil.java index d2dee5e512..dce34a1c96 100644 --- a/openecomp-be/lib/openecomp-core-lib/openecomp-nosqldb-lib/openecomp-nosqldb-api/src/main/java/org/openecomp/core/util/UniqueValueUtil.java +++ b/openecomp-be/lib/openecomp-core-lib/openecomp-nosqldb-lib/openecomp-nosqldb-api/src/main/java/org/openecomp/core/util/UniqueValueUtil.java @@ -27,7 +27,6 @@ import org.openecomp.core.utilities.CommonMethods; import org.openecomp.sdc.common.errors.CoreException; import org.openecomp.sdc.common.errors.ErrorCategory; import org.openecomp.sdc.common.errors.ErrorCode; -import org.openecomp.sdc.common.utils.CommonUtil; import org.openecomp.sdc.logging.context.impl.MdcDataDebugMessage; import java.util.Optional; @@ -95,9 +94,10 @@ public class UniqueValueUtil { mdcDataDebugMessage.debugEntryMessage(null, null); - if ((newValue != null && oldValue != null - && !newValue.toLowerCase().equals(oldValue.toLowerCase())) - || newValue == null || oldValue == null) { + boolean nonEqual = (newValue != null) && (oldValue != null) + && !newValue.toLowerCase().equals(oldValue.toLowerCase()); + + if (nonEqual || newValue == null || oldValue == null) { createUniqueValue(type, CommonMethods.concat(uniqueContext, new String[]{newValue})); deleteUniqueValue(type, CommonMethods.concat(uniqueContext, new String[]{oldValue})); } diff --git a/openecomp-be/lib/openecomp-core-lib/openecomp-nosqldb-lib/openecomp-nosqldb-core/src/main/java/org/openecomp/core/nosqldb/util/CassandraUtils.java b/openecomp-be/lib/openecomp-core-lib/openecomp-nosqldb-lib/openecomp-nosqldb-core/src/main/java/org/openecomp/core/nosqldb/util/CassandraUtils.java index 2a88d0e521..7a70900873 100644 --- a/openecomp-be/lib/openecomp-core-lib/openecomp-nosqldb-lib/openecomp-nosqldb-core/src/main/java/org/openecomp/core/nosqldb/util/CassandraUtils.java +++ b/openecomp-be/lib/openecomp-core-lib/openecomp-nosqldb-lib/openecomp-nosqldb-core/src/main/java/org/openecomp/core/nosqldb/util/CassandraUtils.java @@ -23,7 +23,6 @@ package org.openecomp.core.nosqldb.util; import org.openecomp.core.utilities.file.FileUtils; import org.openecomp.core.utilities.json.JsonUtil; -import java.io.InputStream; import java.util.HashMap; import java.util.Map; @@ -49,10 +48,12 @@ public class CassandraUtils { * @return the statement */ public static String getStatement(String statementName) { + if (statementMap.size() == 0) { - InputStream statementJson = FileUtils.getFileInputStream(CASSANDRA_STATEMENT_DEFINITION_FILE); - statementMap = JsonUtil.json2Object(statementJson, Map.class); + statementMap = FileUtils.readViaInputStream(CASSANDRA_STATEMENT_DEFINITION_FILE, + stream -> JsonUtil.json2Object(stream, Map.class)); } + return statementMap.get(statementName); } 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 87ce8d37c3..e04f02cce5 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 @@ -28,9 +28,12 @@ import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.net.URL; -import java.util.ArrayList; +import java.util.Collections; import java.util.Enumeration; +import java.util.LinkedList; import java.util.List; +import java.util.Objects; +import java.util.function.Function; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; @@ -40,38 +43,29 @@ import java.util.zip.ZipInputStream; public class FileUtils { /** - * Gets file input stream. + * Allows to consume an input stream open against a resource with a given file name. * * @param fileName the file name - * @return the file input stream + * @param function logic to be applied to the input stream */ - public static InputStream getFileInputStream(String fileName) { - URL urlFile = FileUtils.class.getClassLoader().getResource(fileName); - InputStream is; - try { - assert urlFile != null; - is = urlFile.openStream(); - } catch (IOException exception) { - throw new RuntimeException(exception); - } - return is; + public static <T> T readViaInputStream(String fileName, Function<InputStream, T> function) { + return readViaInputStream(FileUtils.class.getClassLoader().getResource(fileName), function); } /** - * Gets file input stream. + * Allows to consume an input stream open against a resource with a given URL. * * @param urlFile the url file - * @return the file input stream + * @param function logic to be applied to the input stream */ - public static InputStream getFileInputStream(URL urlFile) { - InputStream is; - try { - assert urlFile != null; - is = urlFile.openStream(); + public static <T> T readViaInputStream(URL urlFile, Function<InputStream, T> function) { + + Objects.requireNonNull(urlFile); + try (InputStream is = urlFile.openStream()) { + return function.apply(is); } catch (IOException exception) { throw new RuntimeException(exception); } - return is; } /** @@ -80,24 +74,23 @@ public class FileUtils { * @param fileName the file name * @return the file input streams */ - public static List<InputStream> getFileInputStreams(String fileName) { + public static List<URL> getAllLocations(String fileName) { + + List<URL> urls = new LinkedList<>(); Enumeration<URL> urlFiles; - List<InputStream> streams = new ArrayList<>(); - InputStream is; - URL url; + try { urlFiles = FileUtils.class.getClassLoader().getResources(fileName); while (urlFiles.hasMoreElements()) { - url = urlFiles.nextElement(); - is = url.openStream(); - streams.add(is); + urls.add(urlFiles.nextElement()); } } catch (IOException exception) { throw new RuntimeException(exception); } - return streams; + + return urls.isEmpty() ? Collections.emptyList() : Collections.unmodifiableList(urls); } /** @@ -210,25 +203,22 @@ public class FileUtils { * @throws IOException the io exception */ public static FileContentHandler getFileContentMapFromZip(byte[] zipData) throws IOException { - ZipEntry zipEntry; - FileContentHandler mapFileContent = new FileContentHandler(); - try { - ZipInputStream inputZipStream; - byte[] fileByteContent; - String currentEntryName; - inputZipStream = new ZipInputStream(new ByteArrayInputStream(zipData)); + try (ZipInputStream inputZipStream = new ZipInputStream(new ByteArrayInputStream(zipData))) { + + FileContentHandler mapFileContent = new FileContentHandler(); + + ZipEntry zipEntry; while ((zipEntry = inputZipStream.getNextEntry()) != null) { - currentEntryName = zipEntry.getName(); - fileByteContent = FileUtils.toByteArray(inputZipStream); - mapFileContent.addFile(currentEntryName, fileByteContent); + mapFileContent.addFile(zipEntry.getName(), FileUtils.toByteArray(inputZipStream)); } + return mapFileContent; + } catch (RuntimeException exception) { throw new IOException(exception); } - return mapFileContent; } diff --git a/openecomp-be/lib/openecomp-core-lib/openecomp-utilities-lib/src/test/java/org/openecomp/core/utilities/json/JsonSchemaDataGeneratorTest.java b/openecomp-be/lib/openecomp-core-lib/openecomp-utilities-lib/src/test/java/org/openecomp/core/utilities/json/JsonSchemaDataGeneratorTest.java index ba34d07034..9b21f632cc 100644 --- a/openecomp-be/lib/openecomp-core-lib/openecomp-utilities-lib/src/test/java/org/openecomp/core/utilities/json/JsonSchemaDataGeneratorTest.java +++ b/openecomp-be/lib/openecomp-core-lib/openecomp-utilities-lib/src/test/java/org/openecomp/core/utilities/json/JsonSchemaDataGeneratorTest.java @@ -28,14 +28,17 @@ import org.testng.annotations.Test; public class JsonSchemaDataGeneratorTest { - public static final String SCHEMA_WITHOUT_DEFAULTS = new String( - FileUtils.toByteArray(FileUtils.getFileInputStream("jsonUtil/json_schema/aSchema.json"))); - public static final String SCHEMA_WITH_REFS_AND_DEFAULTS = new String(FileUtils.toByteArray( - FileUtils.getFileInputStream("jsonUtil/json_schema/schemaWithRefsAndDefaults.json"))); - public static final String SCHEMA_WITH_INVALID_DEFAULT = new String(FileUtils.toByteArray( - FileUtils.getFileInputStream("jsonUtil/json_schema/schemaWithInvalidDefault.json"))); - public static final String SCHEMA_NIC = new String( - FileUtils.toByteArray(FileUtils.getFileInputStream("jsonUtil/json_schema/nicSchema.json"))); + public static final String SCHEMA_WITHOUT_DEFAULTS = + readFromFile("jsonUtil/json_schema/aSchema.json"); + + public static final String SCHEMA_WITH_REFS_AND_DEFAULTS = + readFromFile("jsonUtil/json_schema/schemaWithRefsAndDefaults.json"); + + public static final String SCHEMA_WITH_INVALID_DEFAULT = + readFromFile("jsonUtil/json_schema/schemaWithInvalidDefault.json"); + + public static final String SCHEMA_NIC = + readFromFile("jsonUtil/json_schema/nicSchema.json"); @Test public void testSchemaWithoutDefaults() { @@ -67,4 +70,8 @@ public class JsonSchemaDataGeneratorTest { JSONObject dataJson = new JSONObject(data); Assert.assertTrue(expectedData.similar(dataJson)); } + + private static String readFromFile(String filename) { + return FileUtils.readViaInputStream(filename, stream -> new String(FileUtils.toByteArray(stream))); + } } diff --git a/openecomp-be/lib/openecomp-core-lib/openecomp-utilities-lib/src/test/java/org/openecomp/core/utilities/json/JsonUtilTest.java b/openecomp-be/lib/openecomp-core-lib/openecomp-utilities-lib/src/test/java/org/openecomp/core/utilities/json/JsonUtilTest.java index 6b8805797a..d89019baec 100644 --- a/openecomp-be/lib/openecomp-core-lib/openecomp-utilities-lib/src/test/java/org/openecomp/core/utilities/json/JsonUtilTest.java +++ b/openecomp-be/lib/openecomp-core-lib/openecomp-utilities-lib/src/test/java/org/openecomp/core/utilities/json/JsonUtilTest.java @@ -31,10 +31,14 @@ public class JsonUtilTest { @Test public void testValidJsonValidate() throws Exception { - String json = - new String(FileUtils.toByteArray(FileUtils.getFileInputStream("jsonUtil/json/a.json"))); - String jsonSchema = new String( - FileUtils.toByteArray(FileUtils.getFileInputStream("jsonUtil/json_schema/aSchema.json"))); + + + + String json = FileUtils.readViaInputStream("jsonUtil/json/a.json", + stream -> new String(FileUtils.toByteArray(stream))); + + String jsonSchema = FileUtils.readViaInputStream("jsonUtil/json_schema/aSchema.json", + stream -> new String(FileUtils.toByteArray(stream))); List<String> validationErrors = JsonUtil.validate(json, jsonSchema); Assert.assertNull(validationErrors); @@ -42,10 +46,11 @@ public class JsonUtilTest { @Test public void testInValidJsonValidate() throws Exception { - String json = new String( - FileUtils.toByteArray(FileUtils.getFileInputStream("jsonUtil/json/a_invalid.json"))); - String jsonSchema = new String( - FileUtils.toByteArray(FileUtils.getFileInputStream("jsonUtil/json_schema/aSchema.json"))); + + String json = FileUtils.readViaInputStream("jsonUtil/json/a_invalid.json", + stream -> new String(FileUtils.toByteArray(stream))); + String jsonSchema = FileUtils.readViaInputStream("jsonUtil/json_schema/aSchema.json", + stream -> new String(FileUtils.toByteArray(stream))); List<String> validationErrors = JsonUtil.validate(json, jsonSchema); Assert.assertNotNull(validationErrors); |