diff options
Diffstat (limited to 'vnfmarket-be')
3 files changed, 128 insertions, 80 deletions
diff --git a/vnfmarket-be/vnf-sdk-marketplace/src/main/java/org/onap/vnfsdk/marketplace/filemanage/http/ToolUtil.java b/vnfmarket-be/vnf-sdk-marketplace/src/main/java/org/onap/vnfsdk/marketplace/filemanage/http/ToolUtil.java index e45d31bf..48a7d0ca 100644 --- a/vnfmarket-be/vnf-sdk-marketplace/src/main/java/org/onap/vnfsdk/marketplace/filemanage/http/ToolUtil.java +++ b/vnfmarket-be/vnf-sdk-marketplace/src/main/java/org/onap/vnfsdk/marketplace/filemanage/http/ToolUtil.java @@ -34,7 +34,7 @@ public class ToolUtil { } /** - * copy in directory. + * simple copy directory * @param srcDirName source directory name * @param destDirName destination directory name * @param files file list in directory @@ -42,24 +42,44 @@ public class ToolUtil { * @return boolean * @throws IOException e */ - public static boolean copyInDirectory(String srcDirName, String destDirName, File[] files, boolean overlay) + public static boolean simpleCopyDirectory(String srcDirName, String destDirName) throws IOException { + File srcDir = new File(srcDirName); + File[] files = srcDir.listFiles(); for (int i = 0; i < files.length; i++) { boolean flag = false; if (files[i].isFile()) { flag = copyFile(files[i].getAbsolutePath(), destDirName + files[i].getName(), true); } if (files[i].isDirectory()) { - flag = copyDirectory(files[i].getAbsolutePath(), destDirName + files[i].getName(), overlay); + flag = simpleCopyDirectory(files[i].getAbsolutePath(), destDirName + files[i].getName()); } if (!flag) { String message = "Copy catagory " + srcDirName + " to " + destDirName + " failed!"; LOGGER.error(message); return false; } - } - return true; + } + return true; + } + + /** + * create destDir + * @param destDirName destination directory name + * @param overlay overwrite or not + * @return boolean + */ + public static boolean createDestDir(String destDirName, boolean overlay) + { + File destDir = new File(destDirName); + if (destDir.exists() && overlay) { + new File(destDirName).delete(); + } else if (destDir.exists() && !overlay) { + return false; + } + + return destDir.mkdirs(); } /** @@ -77,24 +97,17 @@ public class ToolUtil { return false; } - String useDestDirName = destDirName; - if (!useDestDirName.endsWith(File.separator)) { - useDestDirName += File.separator; - } - File destDir = new File(useDestDirName); - if (destDir.exists() && overlay) { - new File(useDestDirName).delete(); - } else if (destDir.exists() && !overlay) { - return false; + String fullDestDirName = destDirName; + if (!fullDestDirName.endsWith(File.separator)) { + fullDestDirName += File.separator; } - if (!destDir.mkdirs()) { - return false; + if (!createDestDir(fullDestDirName, overlay)) + { + return false; } - File[] files = srcDir.listFiles(); - - return copyInDirectory(srcDirName, destDirName, files, overlay); + return simpleCopyDirectory(srcDirName, fullDestDirName); } /** diff --git a/vnfmarket-be/vnf-sdk-marketplace/src/main/java/org/onap/vnfsdk/marketplace/wrapper/PackageWrapper.java b/vnfmarket-be/vnf-sdk-marketplace/src/main/java/org/onap/vnfsdk/marketplace/wrapper/PackageWrapper.java index 02ae5e30..de77d240 100644 --- a/vnfmarket-be/vnf-sdk-marketplace/src/main/java/org/onap/vnfsdk/marketplace/wrapper/PackageWrapper.java +++ b/vnfmarket-be/vnf-sdk-marketplace/src/main/java/org/onap/vnfsdk/marketplace/wrapper/PackageWrapper.java @@ -34,6 +34,7 @@ import javax.ws.rs.core.Response.Status; import org.apache.commons.io.IOUtils; import org.apache.commons.lang3.StringUtils; import org.glassfish.jersey.media.multipart.FormDataContentDisposition; +import org.onap.validation.csar.CsarValidator; import org.onap.vnfsdk.marketplace.common.CommonConstant; import org.onap.vnfsdk.marketplace.common.FileUtil; import org.onap.vnfsdk.marketplace.common.JsonUtil; @@ -58,8 +59,6 @@ import org.onap.vnfsdk.marketplace.onboarding.onboardmanager.OnBoardingHandler; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.onap.validation.csar.CsarValidator; - import net.sf.json.JSONObject; public class PackageWrapper { @@ -184,6 +183,57 @@ public class PackageWrapper { return Response.status(Status.INTERNAL_SERVER_ERROR).build(); } + private UploadPackageResponse manageUpload(String packageId, String fileName, String fileLocation, + String details, String contentRange) throws IOException, MarketplaceResourceException + { + String localDirName = ToolUtil.getTempDir(CommonConstant.CATALOG_CSAR_DIR_NAME, fileName); + PackageBasicInfo basicInfo = PackageWrapperUtil.getPacageBasicInfo(fileLocation); + UploadPackageResponse result = new UploadPackageResponse(); + Boolean isEnd = PackageWrapperUtil.isUploadEnd(contentRange); + if (isEnd) + { + PackageMeta packageMeta = PackageWrapperUtil.getPackageMeta(packageId, fileName, fileLocation, basicInfo, details); + try { + String path = basicInfo.getType().toString() + File.separator + basicInfo.getProvider() + File.separator + packageMeta.getCsarId() + File.separator + fileName.replace(".csar", "") + File.separator + basicInfo.getVersion(); + + String dowloadUri = File.separator + path + File.separator; + packageMeta.setDownloadUri(dowloadUri); + + LOG.info("dest path is : " + path); + LOG.info("packageMeta = " + ToolUtil.objectToString(packageMeta)); + + PackageData packageData = PackageWrapperUtil.getPackageData(packageMeta); + + String destPath = File.separator + path + File.separator + File.separator; + boolean uploadResult = FileManagerFactory.createFileManager().upload(localDirName, destPath); + if (uploadResult) + { + OnBoradingRequest oOnboradingRequest = new OnBoradingRequest(); + oOnboradingRequest.setCsarId(packageId); + oOnboradingRequest.setPackageName(fileName); + oOnboradingRequest.setPackagePath(localDirName); + + packageData.setCsarId(packageId); + packageData.setDownloadCount(-1); + PackageData packateDbData = PackageManager.getInstance().addPackage(packageData); + + LOG.info("Store package data to database succed ! packateDbData = " + ToolUtil.objectToString(packateDbData)); + LOG.info("upload package file end, fileName:" + fileName); + + result.setCsarId(packateDbData.getCsarId()); + + addOnBoardingRequest(oOnboradingRequest); + + LOG.info("OnboradingRequest Data : " + ToolUtil.objectToString(oOnboradingRequest)); + } + } catch (NullPointerException e) { + LOG.error("Package basicInfo is incorrect ! basicIonfo = " + ToolUtil.objectToString(basicInfo), e); + return null; + } + } + return result; + } + /** * Interface for Uploading package * @param packageId @@ -232,66 +282,25 @@ public class PackageWrapper { uploadedInputStream.close(); - try { - CsarValidator cv = new CsarValidator(packageId, fileLocation); - - if (!cv.validateCsar()) { - LOG.error("Could not validate failed"); - return Response.status(Status.EXPECTATION_FAILED).build(); - } - + try { + CsarValidator cv = new CsarValidator(packageId, fileLocation); - } catch (Exception e) { - LOG.error("CSAR validation panicked", e); - return Response.status(Status.EXPECTATION_FAILED).build(); - } - + if (!cv.validateCsar()) { + LOG.error("Could not validate failed"); + return Response.status(Status.EXPECTATION_FAILED).build(); + } + } catch (Exception e) { + LOG.error("CSAR validation panicked", e); + return Response.status(Status.EXPECTATION_FAILED).build(); + } - PackageBasicInfo basicInfo = PackageWrapperUtil.getPacageBasicInfo(fileLocation); - UploadPackageResponse result = new UploadPackageResponse(); - Boolean isEnd = PackageWrapperUtil.isUploadEnd(contentRange); - if (isEnd) + UploadPackageResponse result = manageUpload(packageId, fileName, fileLocation, details, contentRange); + if (null != result) { - PackageMeta packageMeta = PackageWrapperUtil.getPackageMeta(packageId,fileName, fileLocation, basicInfo, details); - try { - String path = basicInfo.getType().toString() + File.separator + basicInfo.getProvider() + File.separator + packageMeta.getCsarId() + File.separator + fileName.replace(".csar", "") + File.separator + basicInfo.getVersion(); - - String dowloadUri = File.separator + path + File.separator; - packageMeta.setDownloadUri(dowloadUri); - - LOG.info("dest path is : " + path); - LOG.info("packageMeta = " + ToolUtil.objectToString(packageMeta)); - - PackageData packageData = PackageWrapperUtil.getPackageData(packageMeta); - - String destPath = File.separator + path + File.separator + File.separator; - boolean uploadResult = FileManagerFactory.createFileManager().upload(localDirName, destPath); - if (uploadResult) - { - OnBoradingRequest oOnboradingRequest = new OnBoradingRequest(); - oOnboradingRequest.setCsarId(packageId); - oOnboradingRequest.setPackageName(fileName); - oOnboradingRequest.setPackagePath(localDirName); - - packageData.setCsarId(packageId); - packageData.setDownloadCount(-1); - PackageData packateDbData = PackageManager.getInstance().addPackage(packageData); - - LOG.info("Store package data to database succed ! packateDbData = " + ToolUtil.objectToString(packateDbData)); - LOG.info("upload package file end, fileName:" + fileName); - - result.setCsarId(packateDbData.getCsarId()); - - addOnBoardingRequest(oOnboradingRequest); - - LOG.info("OnboradingRequest Data : " + ToolUtil.objectToString(oOnboradingRequest)); - } - } catch (NullPointerException e) { - LOG.error("Package basicInfo is incorrect ! basicIonfo = " + ToolUtil.objectToString(basicInfo), e); - return Response.serverError().build(); - } + return Response.ok(ToolUtil.objectToString(result), MediaType.APPLICATION_JSON).build(); + } else { + return Response.serverError().build(); } - return Response.ok(ToolUtil.objectToString(result), MediaType.APPLICATION_JSON).build(); } /** diff --git a/vnfmarket-be/vnf-sdk-marketplace/src/test/filemanage/FileManageTest.java b/vnfmarket-be/vnf-sdk-marketplace/src/test/filemanage/FileManageTest.java index c2597ff1..2fbaf756 100644 --- a/vnfmarket-be/vnf-sdk-marketplace/src/test/filemanage/FileManageTest.java +++ b/vnfmarket-be/vnf-sdk-marketplace/src/test/filemanage/FileManageTest.java @@ -18,13 +18,15 @@ package org.onap.vnfsdk.marketplace.filemanage; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; import java.io.File; import java.io.IOException; +import java.lang.reflect.Constructor; +import java.lang.reflect.Modifier; import org.junit.Before; import org.junit.Test; -import org.onap.vnfsdk.marketplace.filemanage.http.HttpFileManagerImpl; import org.onap.vnfsdk.marketplace.filemanage.http.ToolUtil; import mockit.Mock; @@ -45,6 +47,16 @@ public class FileManageTest { assertNull(manager); } + @Test + public void testFileManagerFactoryConstructor() { + try { + Constructor<FileManagerFactory> constructor = FileManagerFactory.class.getDeclaredConstructor(); + assertTrue(Modifier.isPrivate(constructor.getModifiers())); + } catch (NoSuchMethodException e) { + e.printStackTrace(); + } + } + @Before public void createTestFile() { @@ -58,7 +70,14 @@ public class FileManageTest { @Test public void testDelete() throws IOException { - HttpFileManagerImpl ManagerImpl = new HttpFileManagerImpl(); + new MockUp<FileManagerFactory>() { + @Mock + private FileManagerType getType() { + return FileManagerType.http; + } + }; + + FileManager ManagerImpl = FileManagerFactory.createFileManager(); String srcPath = "./srcPathForTest"; new MockUp<ToolUtil>() { @Mock @@ -78,7 +97,14 @@ public class FileManageTest { @Test public void testUpload() throws IOException { - HttpFileManagerImpl ManagerImpl = new HttpFileManagerImpl(); + new MockUp<FileManagerFactory>() { + @Mock + private FileManagerType getType() { + return FileManagerType.http; + } + }; + + FileManager ManagerImpl = FileManagerFactory.createFileManager(); String srcPath = "./srcPathForTest"; String dstPath = "./dstPathForTest"; new MockUp<ToolUtil>() { |