From 540a174431107a73ea21a38c4d3e5b9358e92e12 Mon Sep 17 00:00:00 2001 From: Kailun Qin Date: Mon, 16 Apr 2018 13:16:39 +0800 Subject: Address functest sonar issues Fix the sonar reported bugs and vulnerabilities. Change-Id: I767b664c1dc86d6d8941c99f60db1b43ee798af8 Issue-ID: VNFSDK-253 Signed-off-by: Kailun Qin --- .../java/org/onap/vnfsdk/functest/FileUtil.java | 4 +- .../org/onap/vnfsdk/functest/TaskExecution.java | 68 +++++++++++++++------- .../onap/vnfsdk/functest/models/CaseRecord.java | 5 ++ .../functest/responsehandler/TestResultParser.java | 13 +++-- .../vnfsdk/functest/scriptmgr/ScriptManager.java | 8 +-- .../onap/vnfsdk/functest/util/ZipCompressor.java | 4 +- 6 files changed, 65 insertions(+), 37 deletions(-) (limited to 'vnf-sdk-function-test/src/main') diff --git a/vnf-sdk-function-test/src/main/java/org/onap/vnfsdk/functest/FileUtil.java b/vnf-sdk-function-test/src/main/java/org/onap/vnfsdk/functest/FileUtil.java index fd66362..2b13022 100644 --- a/vnf-sdk-function-test/src/main/java/org/onap/vnfsdk/functest/FileUtil.java +++ b/vnf-sdk-function-test/src/main/java/org/onap/vnfsdk/functest/FileUtil.java @@ -211,10 +211,8 @@ public final class FileUtil { } byte[] byteArrayFile = new byte[(int) file.length()]; - try { - FileInputStream fileInputStream = new FileInputStream(filename); + try (FileInputStream fileInputStream = new FileInputStream(filename)) { int value = fileInputStream.read(byteArrayFile); - fileInputStream.close(); LOG.debug("Number of bytes read from fileInputStream = " + value); } catch (Exception e) { LOG.error("convertZipFiletoByteArray: " + e); diff --git a/vnf-sdk-function-test/src/main/java/org/onap/vnfsdk/functest/TaskExecution.java b/vnf-sdk-function-test/src/main/java/org/onap/vnfsdk/functest/TaskExecution.java index 51a8799..b7f674a 100644 --- a/vnf-sdk-function-test/src/main/java/org/onap/vnfsdk/functest/TaskExecution.java +++ b/vnf-sdk-function-test/src/main/java/org/onap/vnfsdk/functest/TaskExecution.java @@ -98,12 +98,21 @@ public class TaskExecution { process.waitFor(); inputStream = process.getInputStream(); } - while ((ch = inputStream.read()) != -1) { - LOGGER.info(ApplicationConstants.CHARACTER + Integer.toString(ch)); + if (inputStream != null){ + while ((ch = inputStream.read()) != -1) { + LOGGER.info(ApplicationConstants.CHARACTER + Integer.toString(ch)); + } } - } catch (Exception e) { LOGGER.error(ApplicationConstants.TASKEXE_EXESCRIPT_EXCEPTION, e); + } finally { + if (inputStream != null) { + try { + inputStream.close(); + } catch (IOException e) { + LOGGER.error("executeScript", e); + } + } } } @@ -126,27 +135,27 @@ public class TaskExecution { return; } + String remoteDir = ""; + String remoteArgs = ""; + // Get environment of given UUID Environment functestEnv = EnvironmentMap.getInstance().getEnv(envId); - if (null == functestEnv) { - LOGGER.error("Function Test Environment details are empty,EnvID = " + envId); - } else { + if (null != functestEnv) { LOGGER.info("Function Test Environment path,Path = " + functestEnv.getPath()); + remoteDir = functestEnv.getPath() + mapValues.get("SCRIPT_NAME"); + // set the argument parameters + remoteArgs = remoteArgs + " -v " + "NODE_IP" + ":" + functestEnv.getRemoteIp() + " "; + remoteArgs = remoteArgs + " -v " + "NODE_USERNAME" + ":" + functestEnv.getUserName() + " "; + remoteArgs = remoteArgs + " -v " + "NODE_PASSWORD" + ":" + functestEnv.getPassword() + " "; + } else { + LOGGER.error("Function Test Environment details are empty,EnvID = " + envId); } - String remoteDir = functestEnv.getPath() + mapValues.get("SCRIPT_NAME"); - String remoteConfigArgs = remoteDir + "/" + "config.args "; String remoteScriptFile = remoteDir + "/" + mapValues.get("MAIN_SCRIPT"); String remoteScriptResult = remoteDir + "/" + "output "; String dirResult = mapValues.get(ApplicationConstants.DIR_RESULT) + executeId; - // set the argument parameters - String remoteArgs = ""; - remoteArgs = remoteArgs + " -v " + "NODE_IP" + ":" + functestEnv.getRemoteIp() + " "; - remoteArgs = remoteArgs + " -v " + "NODE_USERNAME" + ":" + functestEnv.getUserName() + " "; - remoteArgs = remoteArgs + " -v " + "NODE_PASSWORD" + ":" + functestEnv.getPassword() + " "; - String remoteCommand = ApplicationConstants.ROBOT_SPACE + "-d " + remoteScriptResult + "--argumentfile " + remoteConfigArgs + remoteScriptFile; @@ -171,12 +180,21 @@ public class TaskExecution { process.waitFor(); inputStream = process.getInputStream(); } - - while ((ch = inputStream.read()) != -1) { - LOGGER.info(ApplicationConstants.CHARACTER + Integer.toString(ch)); + if (inputStream != null) { + while ((ch = inputStream.read()) != -1) { + LOGGER.info(ApplicationConstants.CHARACTER + Integer.toString(ch)); + } } } catch (Exception e) { LOGGER.error(ApplicationConstants.TASKEXE_EXESCRIPT_EXCEPTION, e); + } finally { + if (inputStream != null) { + try { + inputStream.close(); + } catch (IOException e){ + LOGGER.error("executeRobotScript IOException", e); + } + } } OperationStatus operstatus = new OperationStatus(); @@ -236,13 +254,21 @@ public class TaskExecution { process.waitFor(); inputStream = process.getInputStream(); } - - while ((ch = inputStream.read()) != -1) { - LOGGER.info(ApplicationConstants.CHARACTER + Integer.toString(ch)); + if (inputStream != null) { + while ((ch = inputStream.read()) != -1) { + LOGGER.info(ApplicationConstants.CHARACTER + Integer.toString(ch)); + } } - } catch (Exception e) { LOGGER.error(ApplicationConstants.TASKEXE_EXESCRIPT_EXCEPTION, e); + } finally { + if (inputStream != null) { + try { + inputStream.close(); + } catch (IOException e){ + LOGGER.error("uploadScript IOException", e); + } + } } OperationStatus operstatus = new OperationStatus(); diff --git a/vnf-sdk-function-test/src/main/java/org/onap/vnfsdk/functest/models/CaseRecord.java b/vnf-sdk-function-test/src/main/java/org/onap/vnfsdk/functest/models/CaseRecord.java index 683cf2d..595e45f 100644 --- a/vnf-sdk-function-test/src/main/java/org/onap/vnfsdk/functest/models/CaseRecord.java +++ b/vnf-sdk-function-test/src/main/java/org/onap/vnfsdk/functest/models/CaseRecord.java @@ -120,4 +120,9 @@ public class CaseRecord { return Objects.equals(this.taskID, that.taskID); } + + @Override + public int hashCode() { + return Objects.hash(taskID); + } } diff --git a/vnf-sdk-function-test/src/main/java/org/onap/vnfsdk/functest/responsehandler/TestResultParser.java b/vnf-sdk-function-test/src/main/java/org/onap/vnfsdk/functest/responsehandler/TestResultParser.java index d3a6b39..f226695 100644 --- a/vnf-sdk-function-test/src/main/java/org/onap/vnfsdk/functest/responsehandler/TestResultParser.java +++ b/vnf-sdk-function-test/src/main/java/org/onap/vnfsdk/functest/responsehandler/TestResultParser.java @@ -132,12 +132,13 @@ public class TestResultParser { } private Document createDocument(String fileName) throws ParserConfigurationException, SAXException, IOException { - InputStream inputStream = new FileInputStream(fileName); - DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); - DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder(); - Document doc = docBuilder.parse(inputStream); - doc.getDocumentElement().normalize(); - return doc; + try (InputStream inputStream = new FileInputStream(fileName)) { + DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); + DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder(); + Document doc = docBuilder.parse(inputStream); + doc.getDocumentElement().normalize(); + return doc; + } } private String getNodeValue(Node namedItem) { diff --git a/vnf-sdk-function-test/src/main/java/org/onap/vnfsdk/functest/scriptmgr/ScriptManager.java b/vnf-sdk-function-test/src/main/java/org/onap/vnfsdk/functest/scriptmgr/ScriptManager.java index 1dae79f..8b1ff4c 100644 --- a/vnf-sdk-function-test/src/main/java/org/onap/vnfsdk/functest/scriptmgr/ScriptManager.java +++ b/vnf-sdk-function-test/src/main/java/org/onap/vnfsdk/functest/scriptmgr/ScriptManager.java @@ -181,12 +181,12 @@ public class ScriptManager { taskMgrTaskTblDAO.saveOrUpdate(taskRecord); } } catch (InterruptedException e) { - e.printStackTrace(); + Thread.currentThread().interrupt(); + LOGGER.error("uploadFuncTestPackage InterruptedException", e); } catch (ExecutionException e) { - e.printStackTrace(); + LOGGER.error("uploadFuncTestPackage ExecutionException", e); } catch (TimeoutException e) { - LOGGER.info("Time out."); - e.printStackTrace(); + LOGGER.error("uploadFuncTestPackage TimeoutException", e); } return uploadID; diff --git a/vnf-sdk-function-test/src/main/java/org/onap/vnfsdk/functest/util/ZipCompressor.java b/vnf-sdk-function-test/src/main/java/org/onap/vnfsdk/functest/util/ZipCompressor.java index de2875e..30fee60 100644 --- a/vnf-sdk-function-test/src/main/java/org/onap/vnfsdk/functest/util/ZipCompressor.java +++ b/vnf-sdk-function-test/src/main/java/org/onap/vnfsdk/functest/util/ZipCompressor.java @@ -48,9 +48,7 @@ public class ZipCompressor { if (!file.exists()) { throw new FileNotFoundException(srcPathName + "not exist!"); } - try { - FileOutputStream fileOutputStream = new FileOutputStream(zipFile); - CheckedOutputStream cos = new CheckedOutputStream(fileOutputStream, new CRC32()); + try (FileOutputStream fileOutputStream = new FileOutputStream(zipFile); CheckedOutputStream cos = new CheckedOutputStream(fileOutputStream, new CRC32())) { ZipOutputStream out = new ZipOutputStream(cos); String basedir = ""; compress(file, out, basedir); -- cgit 1.2.3-korg