From d0b8cbc046384f257ecebf3cec894fac91ae6d2b Mon Sep 17 00:00:00 2001 From: Murali-P Date: Wed, 15 Feb 2017 13:14:06 +0530 Subject: Fix Sonar issues Resolved:VNFSDK-21 Function test base code Change-Id: I781d3629e3f842112402bba00b91d4dcc5bd6842 Signed-off-by: Murali-P --- .../java/org/openo/vnfsdk/functest/FileUtil.java | 42 +++++++++++----------- .../VnfFuncTestResponseHandler.java | 29 ++++++++++----- .../org/openo/vnfsdk/functest/util/GsonUtil.java | 7 ++-- .../vnfsdk/functest/util/RestResponseUtil.java | 3 ++ .../openo/vnfsdk/functest/util/ZipCompressor.java | 41 +++++++++++++++------ 5 files changed, 79 insertions(+), 43 deletions(-) (limited to 'vnf-sdk-function-test/src/main') diff --git a/vnf-sdk-function-test/src/main/java/org/openo/vnfsdk/functest/FileUtil.java b/vnf-sdk-function-test/src/main/java/org/openo/vnfsdk/functest/FileUtil.java index de1b809..e97b4c4 100644 --- a/vnf-sdk-function-test/src/main/java/org/openo/vnfsdk/functest/FileUtil.java +++ b/vnf-sdk-function-test/src/main/java/org/openo/vnfsdk/functest/FileUtil.java @@ -26,6 +26,7 @@ import java.io.InputStream; import java.io.OutputStream; import java.util.ArrayList; import java.util.Enumeration; +import java.util.List; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; @@ -96,7 +97,7 @@ public final class FileUtil { * @return unzip file name * @throws IOException e1 */ - public static ArrayList unzip(String zipFileName, String extPlace) throws IOException { + public static List unzip(String zipFileName, String extPlace) throws IOException { ZipFile zipFile = null; ArrayList unzipFileNams = new ArrayList(); @@ -142,15 +143,12 @@ public final class FileUtil { public static String[] getDirectory(String directory) { File file = new File(directory); - String[] directories = file.list(new FilenameFilter() { + return file.list(new FilenameFilter() { - @Override public boolean accept(File current, String name) { return new File(current, name).isDirectory(); } }); - - return directories; } /** @@ -163,8 +161,8 @@ public final class FileUtil { if(inputStream != null) { inputStream.close(); } - } catch(Exception e1) { - LOG.info("close InputStream error!"); + } catch(Exception ex) { + LOG.error("close InputStream error!: " + ex); } } @@ -178,8 +176,8 @@ public final class FileUtil { if(outputStream != null) { outputStream.close(); } - } catch(Exception e1) { - LOG.info("close OutputStream error!"); + } catch(Exception ex) { + LOG.error("close OutputStream error!: " + ex); } } @@ -190,12 +188,12 @@ public final class FileUtil { */ private static void closeZipFile(ZipFile zipFile) { try { - if(zipFile != null) { - zipFile.close(); - zipFile = null; + ZipFile tempZipFile = zipFile; + if(tempZipFile != null) { + tempZipFile.close(); } - } catch(IOException e1) { - LOG.info("close ZipFile error!"); + } catch(IOException ioe) { + LOG.error("close ZipFile error!: " + ioe); } } @@ -214,17 +212,19 @@ public final class FileUtil { public static byte[] convertZipFiletoByteArray(String filename) { File file = new File(filename); + byte[] emptyArray = new byte[0]; if(!file.exists()) { - return null; + return emptyArray; } byte[] byteArrayFile = new byte[(int)file.length()]; try { FileInputStream fileInputStream = new FileInputStream(filename); - fileInputStream.read(byteArrayFile); + int value = fileInputStream.read(byteArrayFile); fileInputStream.close(); + LOG.debug("Number of bytes read from fileInputStream = "+value); } catch(Exception e) { - e.printStackTrace(); + LOG.error("convertZipFiletoByteArray: " + e); } return byteArrayFile; } @@ -234,15 +234,15 @@ public final class FileUtil { *
* * @param directory - * @since VNFSDK 2.0 + * @since VNFSDK 2.0 */ public static void deleteDirectory(String directory) { File file = new File(directory); if(!file.exists()) { - return ; + return; } - if (file.isDirectory()) { - for (File sub : file.listFiles()) { + if(file.isDirectory()) { + for(File sub : file.listFiles()) { deleteFile(sub); } } diff --git a/vnf-sdk-function-test/src/main/java/org/openo/vnfsdk/functest/responsehandler/VnfFuncTestResponseHandler.java b/vnf-sdk-function-test/src/main/java/org/openo/vnfsdk/functest/responsehandler/VnfFuncTestResponseHandler.java index eb3174b..97f410d 100644 --- a/vnf-sdk-function-test/src/main/java/org/openo/vnfsdk/functest/responsehandler/VnfFuncTestResponseHandler.java +++ b/vnf-sdk-function-test/src/main/java/org/openo/vnfsdk/functest/responsehandler/VnfFuncTestResponseHandler.java @@ -69,8 +69,10 @@ public class VnfFuncTestResponseHandler { String resultPath = mapConfigValues.get(resultpathkey); - // Check whether file Exists for the Request received !!! - // ----------------------------------------------------- + /* + * Check whether file Exists for the Request received !!! + * ----------------------------------------------------- + */ String fileName = resultPath + File.separator + funcTestId; if(!FileUtil.checkFileExist(fileName)) { logger.warn("Resquested function Test result not avaliable/In-Progress !!!"); @@ -78,18 +80,29 @@ public class VnfFuncTestResponseHandler { } String zipFileName = fileName + ".zip"; - new ZipCompressor(zipFileName).compress(fileName); + try { + new ZipCompressor(zipFileName).compress(fileName); + } catch(IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } - // Convert Zip-file byteCode and to response !!! - // ----------------------------------------------------- + /* + * Convert Zip-file byteCode and to response !!! + * ----------------------------------------------------- + */ byte[] byteArrayFile = FileUtil.convertZipFiletoByteArray(zipFileName); if(null != byteArrayFile) { - // Delete Result folders present if Success !!! - // ---------------------------------------------- + /* + * Delete Result folders present if Success !!! + * ---------------------------------------------- + */ FileUtil.deleteFile(zipFileName); - // Later will delete this file..FileUtil.deleteDirectory(fileName); + /* + * Later will delete this file..FileUtil.deleteDirectory(fileName); + */ logger.warn("Resquested function Test result Sucess !!!"); return RestResponseUtil.getSuccessResponse(byteArrayFile); diff --git a/vnf-sdk-function-test/src/main/java/org/openo/vnfsdk/functest/util/GsonUtil.java b/vnf-sdk-function-test/src/main/java/org/openo/vnfsdk/functest/util/GsonUtil.java index acf8cc3..78936da 100644 --- a/vnf-sdk-function-test/src/main/java/org/openo/vnfsdk/functest/util/GsonUtil.java +++ b/vnf-sdk-function-test/src/main/java/org/openo/vnfsdk/functest/util/GsonUtil.java @@ -20,14 +20,13 @@ import java.text.SimpleDateFormat; import java.util.Date; import java.util.UUID; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - import com.google.gson.Gson; public class GsonUtil { - private final static Logger logger = LoggerFactory.getLogger(GsonUtil.class); + private GsonUtil() { + + } public static String generateId() { return UUID.randomUUID().toString(); diff --git a/vnf-sdk-function-test/src/main/java/org/openo/vnfsdk/functest/util/RestResponseUtil.java b/vnf-sdk-function-test/src/main/java/org/openo/vnfsdk/functest/util/RestResponseUtil.java index 317f957..1c5404f 100644 --- a/vnf-sdk-function-test/src/main/java/org/openo/vnfsdk/functest/util/RestResponseUtil.java +++ b/vnf-sdk-function-test/src/main/java/org/openo/vnfsdk/functest/util/RestResponseUtil.java @@ -21,6 +21,9 @@ import javax.ws.rs.core.Response.Status; public class RestResponseUtil { + private RestResponseUtil() { + } + public static Response getSuccessResponse(Object obj) { if(obj != null) { return Response.ok(GsonUtil.objectToString(obj)).build(); diff --git a/vnf-sdk-function-test/src/main/java/org/openo/vnfsdk/functest/util/ZipCompressor.java b/vnf-sdk-function-test/src/main/java/org/openo/vnfsdk/functest/util/ZipCompressor.java index 7ca6bcb..7f3562c 100644 --- a/vnf-sdk-function-test/src/main/java/org/openo/vnfsdk/functest/util/ZipCompressor.java +++ b/vnf-sdk-function-test/src/main/java/org/openo/vnfsdk/functest/util/ZipCompressor.java @@ -19,7 +19,9 @@ package org.openo.vnfsdk.functest.util; import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStream; +import java.io.FileNotFoundException; import java.io.FileOutputStream; +import java.io.IOException; import java.util.zip.CRC32; import java.util.zip.CheckedOutputStream; import java.util.zip.ZipEntry; @@ -44,20 +46,38 @@ public class ZipCompressor { * compress file according several path. * * @param pathName file path name + * @throws Exception */ - public void compress(String... pathName) { + public void compress(String... pathName) throws Exception { ZipOutputStream out = null; + FileOutputStream fileOutputStream = null; + CheckedOutputStream cos = null; try { - FileOutputStream fileOutputStream = new FileOutputStream(zipFile); - CheckedOutputStream cos = new CheckedOutputStream(fileOutputStream, new CRC32()); + fileOutputStream = new FileOutputStream(zipFile); + cos = new CheckedOutputStream(fileOutputStream, new CRC32()); out = new ZipOutputStream(cos); String basedir = ""; for(int i = 0; i < pathName.length; i++) { compress(new File(pathName[i]), out, basedir); } - out.close(); + } catch(Exception e1) { - throw new RuntimeException(e1); + try { + throw new FileNotFoundException("Failed to find file: "); + } catch(FileNotFoundException e) { + LOG.error("compress: " + e); + } + } finally { + try { + if(out != null) + out.close(); + if(fileOutputStream != null) + fileOutputStream.close(); + if(cos != null) + cos.close(); + } catch(Exception e1) { + throw new IOException(e1); + } } } @@ -65,11 +85,12 @@ public class ZipCompressor { * compress file according file path. * * @param srcPathName file path name + * @throws IOException */ - public void compress(String srcPathName) { + public void compress(String srcPathName) throws IOException { File file = new File(srcPathName); if(!file.exists()) { - throw new RuntimeException(srcPathName + "not exist!"); + throw new FileNotFoundException(srcPathName + "not exist!"); } try { FileOutputStream fileOutputStream = new FileOutputStream(zipFile); @@ -79,16 +100,16 @@ public class ZipCompressor { compress(file, out, basedir); out.close(); } catch(Exception e1) { - throw new RuntimeException(e1); + throw new IOException(e1); } } private void compress(File file, ZipOutputStream out, String basedir) { if(file.isDirectory()) { - System.out.println("compress: " + basedir + file.getName()); + LOG.info("compress: " + basedir + file.getName()); this.compressDirectory(file, out, basedir); } else { - System.out.println("compress: " + basedir + file.getName()); + LOG.info("compress: " + basedir + file.getName()); this.compressFile(file, out, basedir); } } -- cgit 1.2.3-korg