diff options
author | 2018-09-14 01:19:16 +0800 | |
---|---|---|
committer | 2018-09-14 01:24:18 +0800 | |
commit | e27e96cecfd2f0e370436fa040a5771245684301 (patch) | |
tree | 01b6a0b89d0a87395217521268e513f41b894d06 /vnf-sdk-function-test | |
parent | a95b0a47c3dd4c1f6717022275a87699db80a661 (diff) |
Sonar: code clean up for functest
Misc code smell fixes.
Change-Id: I7645d008667e34c6244783717cf1731477ffc6c3
Issue-ID: VNFSDK-320
Signed-off-by: Kailun Qin <kailun.qin@intel.com>
Diffstat (limited to 'vnf-sdk-function-test')
14 files changed, 47 insertions, 51 deletions
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 ad3049c..9e657f3 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 @@ -23,6 +23,7 @@ import java.io.*; import java.util.ArrayList; import java.util.Enumeration; import java.util.List; +import java.util.Objects; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; @@ -49,9 +50,7 @@ public final class FileUtil { int tryCount = 0; while (tryCount < TRY_COUNT) { tryCount++; - if (!folder.exists() && !folder.mkdirs()) { - continue; - } else { + if (folder.exists() || folder.mkdirs()) { return true; } } @@ -92,7 +91,7 @@ public final class FileUtil { */ public static List<String> unzip(String zipFileName, String extPlace) throws IOException { - ArrayList<String> unzipFileNams = new ArrayList<String>(); + ArrayList<String> unzipFileNams = new ArrayList<>(); try (ZipFile zipFile = new ZipFile(zipFileName)) { Enumeration<?> fileEn = zipFile.entries(); @@ -132,12 +131,7 @@ public final class FileUtil { public static String[] getDirectory(String directory) { File file = new File(directory); - return file.list(new FilenameFilter() { - - public boolean accept(File current, String name) { - return new File(current, name).isDirectory(); - } - }); + return file.list((current, name) -> new File(current, name).isDirectory()); } public static Boolean checkFileExist(String filePath) { @@ -175,18 +169,19 @@ public final class FileUtil { * <br/> * * @param directory + * @return boolean * @since VNFSDK 2.0 */ - public static void deleteDirectory(String directory) { + public static boolean deleteDirectory(String directory) { File file = new File(directory); if (!file.exists()) { - return; + return false; } if (file.isDirectory()) { - for (File sub : file.listFiles()) { + for (File sub : Objects.requireNonNull(file.listFiles())) { deleteFile(sub); } } - file.delete(); + return file.delete(); } } 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 344e458..b27b3b3 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 @@ -68,19 +68,20 @@ public class TaskExecution { mapValues.put(ApplicationConstants.DIR_RESULT, dirResult); String remoteScriptFile = remoteScriptDir + "/" + mapValues.get("MAIN_SCRIPT"); - String remoteArgs = "--argumentfile " + remoteScriptDir + "/" + "config.args "; + String remoteArgs = "--argumentfile " + remoteScriptDir + "/" + ApplicationConstants.CONFIG_ARGS; String remoteCommand = ApplicationConstants.ROBOT_SPACE + "-d " + remoteScriptResult + remoteArgs + remoteScriptFile; mapValues.put("REMOTE_COMMAND", "\"" + remoteCommand + "\""); - String robotvariables = ""; + StringBuilder robotvariablesBuilder = new StringBuilder(); for (Entry<String, String> values : mapValues.entrySet()) { - robotvariables = robotvariables + " -v " + values.getKey() + ":" + values.getValue() + " "; + robotvariablesBuilder.append(" -v ").append(values.getKey()).append(":").append(values.getValue()).append(" "); } + String robotvariables = robotvariablesBuilder.toString(); // Execute the command - String argumentFilePath = confDir + "config.args "; + String argumentFilePath = confDir + ApplicationConstants.CONFIG_ARGS; String robotScript = confDir + "RemoteConnection.robot"; Process process = null; @@ -144,7 +145,7 @@ public class TaskExecution { LOGGER.error("Function Test Environment details are empty, EnvID = {}", envId); } - String remoteConfigArgs = remoteDir + "/" + "config.args "; + String remoteConfigArgs = remoteDir + "/" + ApplicationConstants.CONFIG_ARGS; String remoteScriptFile = remoteDir + "/" + mapValues.get("MAIN_SCRIPT"); String remoteScriptResult = remoteDir + "/" + "output "; String dirResult = mapValues.get(ApplicationConstants.DIR_RESULT) + executeId; @@ -217,11 +218,12 @@ public class TaskExecution { // Form the variables for the upload, transfer and execute command mapValues.put("SCRIPT_DIR", dirPath); - String robotvariables = ""; + StringBuilder robotvariablesBuilder = new StringBuilder(); for (Entry<String, String> values : mapValues.entrySet()) { - robotvariables = robotvariables + " -v " + values.getKey() + ":" + values.getValue() + " "; + robotvariablesBuilder.append(" -v ").append(values.getKey()).append(":").append(values.getValue()).append(" "); } + String robotvariables = robotvariablesBuilder.toString(); // Append the Func test environment variables Environment functestEnv = EnvironmentMap.getInstance().getEnv(uuidEnv); diff --git a/vnf-sdk-function-test/src/main/java/org/onap/vnfsdk/functest/constants/ApplicationConstants.java b/vnf-sdk-function-test/src/main/java/org/onap/vnfsdk/functest/constants/ApplicationConstants.java index 630db46..2d0110c 100644 --- a/vnf-sdk-function-test/src/main/java/org/onap/vnfsdk/functest/constants/ApplicationConstants.java +++ b/vnf-sdk-function-test/src/main/java/org/onap/vnfsdk/functest/constants/ApplicationConstants.java @@ -56,6 +56,14 @@ public class ApplicationConstants { public static final String CATALOG_URI = "file:///C:/RobotScript/RobotScript.zip"; + public static final String TASK_NOT_EXIST = "Task Not Exist."; + + public static final String CREATED = "CREATED"; + + public static final String NOT_CREATED = "NOT CREATED"; + + public static final String CONFIG_ARGS = "config.args "; + private ApplicationConstants() { } diff --git a/vnf-sdk-function-test/src/main/java/org/onap/vnfsdk/functest/externalservice/entity/EnvironmentMap.java b/vnf-sdk-function-test/src/main/java/org/onap/vnfsdk/functest/externalservice/entity/EnvironmentMap.java index c9584e2..8a5d8d5 100644 --- a/vnf-sdk-function-test/src/main/java/org/onap/vnfsdk/functest/externalservice/entity/EnvironmentMap.java +++ b/vnf-sdk-function-test/src/main/java/org/onap/vnfsdk/functest/externalservice/entity/EnvironmentMap.java @@ -22,7 +22,7 @@ import java.util.UUID; public class EnvironmentMap { - private static Map<UUID, Environment> envMap = new HashMap<UUID, Environment>(); + private static Map<UUID, Environment> envMap = new HashMap<>(); private static EnvironmentMap oInstance = new EnvironmentMap(); diff --git a/vnf-sdk-function-test/src/main/java/org/onap/vnfsdk/functest/externalservice/entity/OperationStatus.java b/vnf-sdk-function-test/src/main/java/org/onap/vnfsdk/functest/externalservice/entity/OperationStatus.java index 3bab7d0..4a2ae56 100644 --- a/vnf-sdk-function-test/src/main/java/org/onap/vnfsdk/functest/externalservice/entity/OperationStatus.java +++ b/vnf-sdk-function-test/src/main/java/org/onap/vnfsdk/functest/externalservice/entity/OperationStatus.java @@ -22,7 +22,6 @@ public class OperationStatus { private boolean operFinished = false; private String operResultMessage; - ; public operResultCode getoResultCode() { return oResultCode; diff --git a/vnf-sdk-function-test/src/main/java/org/onap/vnfsdk/functest/externalservice/entity/OperationStatusHandler.java b/vnf-sdk-function-test/src/main/java/org/onap/vnfsdk/functest/externalservice/entity/OperationStatusHandler.java index af511be..c577bd1 100644 --- a/vnf-sdk-function-test/src/main/java/org/onap/vnfsdk/functest/externalservice/entity/OperationStatusHandler.java +++ b/vnf-sdk-function-test/src/main/java/org/onap/vnfsdk/functest/externalservice/entity/OperationStatusHandler.java @@ -29,7 +29,7 @@ public class OperationStatusHandler { private static final Logger LOGGER = LoggerFactory.getLogger(OperationStatusHandler.class); - private static Map<UUID, OperationStatus> operStatusMap = new HashMap<UUID, OperationStatus>(); + private static Map<UUID, OperationStatus> operStatusMap = new HashMap<>(); private static OperationStatusHandler oInstance = new OperationStatusHandler(); diff --git a/vnf-sdk-function-test/src/main/java/org/onap/vnfsdk/functest/externalservice/entity/ServiceRegisterEntity.java b/vnf-sdk-function-test/src/main/java/org/onap/vnfsdk/functest/externalservice/entity/ServiceRegisterEntity.java index 1cf0585..d9947a5 100644 --- a/vnf-sdk-function-test/src/main/java/org/onap/vnfsdk/functest/externalservice/entity/ServiceRegisterEntity.java +++ b/vnf-sdk-function-test/src/main/java/org/onap/vnfsdk/functest/externalservice/entity/ServiceRegisterEntity.java @@ -40,7 +40,7 @@ public class ServiceRegisterEntity { private String visualRange; - private List<ServiceNode> nodes = new ArrayList<ServiceNode>(); + private List<ServiceNode> nodes = new ArrayList<>(); public String getServiceName() { return serviceName; diff --git a/vnf-sdk-function-test/src/main/java/org/onap/vnfsdk/functest/responsehandler/TestResultMap.java b/vnf-sdk-function-test/src/main/java/org/onap/vnfsdk/functest/responsehandler/TestResultMap.java index 4f73db1..f17e751 100644 --- a/vnf-sdk-function-test/src/main/java/org/onap/vnfsdk/functest/responsehandler/TestResultMap.java +++ b/vnf-sdk-function-test/src/main/java/org/onap/vnfsdk/functest/responsehandler/TestResultMap.java @@ -16,9 +16,6 @@ package org.onap.vnfsdk.functest.responsehandler; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - import java.util.HashMap; import java.util.List; import java.util.Map; @@ -26,9 +23,7 @@ import java.util.UUID; public class TestResultMap { - private static final Logger LOGGER = LoggerFactory.getLogger(TestResultMap.class); - - private static Map<UUID, List<TestResult>> testResultMap = new HashMap<UUID, List<TestResult>>(); + private static Map<UUID, List<TestResult>> resultMap = new HashMap<>(); private static TestResultMap oInstance = new TestResultMap(); @@ -41,11 +36,11 @@ public class TestResultMap { } public synchronized Map<UUID, List<TestResult>> getTestResultMap() { - return testResultMap; + return resultMap; } public synchronized void setTestResultMap(UUID uuid, List<TestResult> inputTestResult) { - testResultMap.put(uuid, inputTestResult); + resultMap.put(uuid, inputTestResult); } } 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 feeb2ee..5fe1134 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 @@ -98,11 +98,9 @@ public class TestResultParser { continue; } - if (DOCTAG == descNode.getNodeName()) { - if (null != descNode.getTextContent()) { - descriptionAttr = descNode.getTextContent(); - break; - } + if (DOCTAG == descNode.getNodeName() && null != descNode.getTextContent()) { + descriptionAttr = descNode.getTextContent(); + break; } } } 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 534bb20..39266da 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 @@ -131,7 +131,7 @@ public class ScriptManager { // Unzip the folder String tempDir = System.getProperty("user.dir") + nl + "temp"; - List<String> list = FileUtil.unzip(filePath, tempDir); + LOGGER.info("File path={}", filePath); String[] directories = FileUtil.getDirectory(tempDir); diff --git a/vnf-sdk-function-test/src/main/java/org/onap/vnfsdk/functest/taskmgr/TaskManager.java b/vnf-sdk-function-test/src/main/java/org/onap/vnfsdk/functest/taskmgr/TaskManager.java index fc5e36c..883f22d 100644 --- a/vnf-sdk-function-test/src/main/java/org/onap/vnfsdk/functest/taskmgr/TaskManager.java +++ b/vnf-sdk-function-test/src/main/java/org/onap/vnfsdk/functest/taskmgr/TaskManager.java @@ -82,7 +82,7 @@ public class TaskManager { try { if (taskMgrTaskTblDAO.findByPackageID(packageID).isPresent()) { - throw new Exception("Already Onboard."); + throw new IllegalArgumentException("Already Onboard."); } else { initOnboardTesting(taskRecord, caseRecord, packageID); scriptManager.uploadFuncTestPackage(UUID.fromString(taskRecord.getTaskID()), UUID.fromString(taskRecord.getEnvID()), ApplicationConstants.CATALOG_URI); @@ -117,7 +117,7 @@ public class TaskManager { List<TaskRecord> taskRecordList = taskMgrTaskTblDAO.findByTaskID(taskID); if (taskRecordList.isEmpty()) { - throw new Exception("Task Not Exist."); + throw new IllegalArgumentException(ApplicationConstants.TASK_NOT_EXIST); } else { TaskRecord taskRecord = taskRecordList.get(0); @@ -171,10 +171,10 @@ public class TaskManager { try { CaseRecord caseRecord = taskMgrCaseTblDAO.findByTaskID(taskID); if (null == caseRecord) { - throw new Exception("Task Not Exist."); + throw new IllegalArgumentException(ApplicationConstants.TASK_NOT_EXIST); } else { /* To check whether we have already collected the result of Task: taskID. */ - if ("NOT CREATED".equals(caseRecord.getTestID())) { + if (ApplicationConstants.NOT_CREATED.equals(caseRecord.getTestID())) { return scriptManager.downloadResults(UUID.fromString(taskID)); } else { CaseRecord oldCaseRecord = new CaseRecord(); @@ -185,7 +185,7 @@ public class TaskManager { } } } catch (Exception e) { - if ("Task Not Exist.".equals(e.getMessage())) { + if (ApplicationConstants.TASK_NOT_EXIST.equals(e.getMessage())) { LOGGER.error("The Task " + taskID + " does not exist..!", e); return RestResponseUtil.getNotFoundResponse(taskID); } else { @@ -204,10 +204,10 @@ public class TaskManager { // Setup the environment final UUID envID = scriptManager.setEnvironment(loadEnvConfigurations()); taskRecord.setEnvID(envID.toString()); - taskRecord.setUploadID("NOT CREATED"); - taskRecord.setOperID("NOT CREATED"); + taskRecord.setUploadID(ApplicationConstants.NOT_CREATED); + taskRecord.setOperID(ApplicationConstants.NOT_CREATED); taskRecord.setFuncID(""); - taskRecord.setStatus("CREATED"); + taskRecord.setStatus(ApplicationConstants.CREATED); taskRecord.setOperFinished("False"); taskRecord.setOperResult("FAILURE"); taskRecord.setOperResultMessage(""); @@ -216,7 +216,7 @@ public class TaskManager { /* Create CaseRecord entry in db */ caseRecord.setTaskID(taskID.toString()); caseRecord.setFuncID(""); - caseRecord.setTestID("NOT CREATED"); + caseRecord.setTestID(ApplicationConstants.NOT_CREATED); caseRecord.setTestResult("NULL"); caseRecord.setTestDescription(""); taskMgrCaseTblDAO.saveOrUpdate(caseRecord); 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 6f866b8..a7107bd 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 @@ -93,7 +93,6 @@ public class ZipCompressor { while ((count = bis.read(data, 0, BUFFER)) != -1) { out.write(data, 0, count); } - bis.close(); } catch (IOException e) { LOG.info("Exception wile compress file" + file.getAbsolutePath(), e); } diff --git a/vnf-sdk-function-test/src/test/java/org/onap/vnfsdk/functest/db/TaskMgrCaseTblDAOTest.java b/vnf-sdk-function-test/src/test/java/org/onap/vnfsdk/functest/db/TaskMgrCaseTblDAOTest.java index 11e7f07..7af2c51 100644 --- a/vnf-sdk-function-test/src/test/java/org/onap/vnfsdk/functest/db/TaskMgrCaseTblDAOTest.java +++ b/vnf-sdk-function-test/src/test/java/org/onap/vnfsdk/functest/db/TaskMgrCaseTblDAOTest.java @@ -51,7 +51,7 @@ public class TaskMgrCaseTblDAOTest { assertThat(caseRecord.getTaskID()).isEqualTo("c0a1a373-8635-484d-bc6c-307a606cb8a1"); assertThat(caseRecord.getTestID()).isEqualTo("INTEL"); assertThat(caseRecord.getTestResult()).isEqualTo("SUCCESS"); - assertThat(taskMgrCaseTblDAO.findByTaskID(caseRecord.getTaskID()).equals(caseRecord)); + assertThat(taskMgrCaseTblDAO.findByTaskID(caseRecord.getTaskID()).equals(caseRecord)).isTrue(); } @Test diff --git a/vnf-sdk-function-test/src/test/java/org/onap/vnfsdk/functest/db/TaskMgrTaskTblDAOTest.java b/vnf-sdk-function-test/src/test/java/org/onap/vnfsdk/functest/db/TaskMgrTaskTblDAOTest.java index e6ff003..ab6093c 100644 --- a/vnf-sdk-function-test/src/test/java/org/onap/vnfsdk/functest/db/TaskMgrTaskTblDAOTest.java +++ b/vnf-sdk-function-test/src/test/java/org/onap/vnfsdk/functest/db/TaskMgrTaskTblDAOTest.java @@ -57,7 +57,7 @@ public class TaskMgrTaskTblDAOTest { assertThat(taskRecord.getStatus()).isEqualTo("CREATED"); assertThat(taskRecord.getOperResult()).isEqualTo("SUCCESS"); assertThat(taskMgrTaskTblDAO.findByPackageID(taskRecord.getPackageID())).isEqualTo(Optional.of(taskRecord)); - assertThat(taskMgrTaskTblDAO.findByTaskID(taskRecord.getTaskID()).get(0).equals(taskRecord)); + assertThat(taskMgrTaskTblDAO.findByTaskID(taskRecord.getTaskID()).get(0).equals(taskRecord)).isTrue(); } @Test |