summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMurali-P <murali.p@huawei.com>2017-02-15 13:14:06 +0530
committerMurali-P <murali.p@huawei.com>2017-02-15 13:19:12 +0530
commitd0b8cbc046384f257ecebf3cec894fac91ae6d2b (patch)
treec629a3cb7ede8ea1a963d72ae0a1d655754a5da9
parente67498bea611283658bf53490338dae6f15424b1 (diff)
Fix Sonar issues
Resolved:VNFSDK-21 Function test base code Change-Id: I781d3629e3f842112402bba00b91d4dcc5bd6842 Signed-off-by: Murali-P <murali.p@huawei.com>
-rw-r--r--vnf-sdk-function-test/src/main/java/org/openo/vnfsdk/functest/FileUtil.java42
-rw-r--r--vnf-sdk-function-test/src/main/java/org/openo/vnfsdk/functest/responsehandler/VnfFuncTestResponseHandler.java29
-rw-r--r--vnf-sdk-function-test/src/main/java/org/openo/vnfsdk/functest/util/GsonUtil.java7
-rw-r--r--vnf-sdk-function-test/src/main/java/org/openo/vnfsdk/functest/util/RestResponseUtil.java3
-rw-r--r--vnf-sdk-function-test/src/main/java/org/openo/vnfsdk/functest/util/ZipCompressor.java41
-rw-r--r--vnf-sdk-function-test/src/test/java/org/openo/vnfsdk/functests/FileUtilTest.java4
6 files changed, 81 insertions, 45 deletions
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<String> unzip(String zipFileName, String extPlace) throws IOException {
+ public static List<String> unzip(String zipFileName, String extPlace) throws IOException {
ZipFile zipFile = null;
ArrayList<String> unzipFileNams = new ArrayList<String>();
@@ -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 {
* <br/>
*
* @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);
}
}
diff --git a/vnf-sdk-function-test/src/test/java/org/openo/vnfsdk/functests/FileUtilTest.java b/vnf-sdk-function-test/src/test/java/org/openo/vnfsdk/functests/FileUtilTest.java
index ba8648d..106490d 100644
--- a/vnf-sdk-function-test/src/test/java/org/openo/vnfsdk/functests/FileUtilTest.java
+++ b/vnf-sdk-function-test/src/test/java/org/openo/vnfsdk/functests/FileUtilTest.java
@@ -21,7 +21,7 @@ import static org.junit.Assert.assertTrue;
import java.io.File;
import java.io.IOException;
-import java.util.ArrayList;
+import java.util.List;
import org.junit.Test;
import org.openo.vnfsdk.functest.FileUtil;
@@ -50,7 +50,7 @@ public class FileUtilTest {
@Test
public void testUnzip() throws IOException {
- ArrayList<String> files = FileUtil.unzip(zipFileName, extractZip);
+ List<String> files = FileUtil.unzip(zipFileName, extractZip);
assertNotNull(files);
}