From 86f345d67896af891a0938d5ee4028632a4da4cc Mon Sep 17 00:00:00 2001 From: vempo Date: Tue, 24 Oct 2017 13:11:19 +0300 Subject: Finished fixing unreleased resources in tests Fixed high-severity static analysis violations (e.g. not closing InputStreams) in tests. Some code cleanup. Change-Id: I2eb95f0b0ec8f6c932a093b8033a5ea27297cd93 Issue-ID: SDC-291 Signed-off-by: vempo --- .../QuestionnaireDataServiceTest.java | 38 +++++----- .../impl/MonitoringUploadsManagerImplTest.java | 83 ++++++++++------------ .../impl/VendorSoftwareProductManagerImplTest.java | 60 ++++++++-------- .../sdc/vendorsoftwareproduct/utils/VSPCommon.java | 22 +++--- .../vendorsoftwareproduct/utils/ZipFileUtils.java | 19 +++-- 5 files changed, 107 insertions(+), 115 deletions(-) (limited to 'openecomp-be') diff --git a/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/test/java/org/openecomp/sdc/vendorsoftwareproduct/QuestionnaireDataServiceTest.java b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/test/java/org/openecomp/sdc/vendorsoftwareproduct/QuestionnaireDataServiceTest.java index 3e42a23c6a..b544d13ec7 100644 --- a/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/test/java/org/openecomp/sdc/vendorsoftwareproduct/QuestionnaireDataServiceTest.java +++ b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/test/java/org/openecomp/sdc/vendorsoftwareproduct/QuestionnaireDataServiceTest.java @@ -49,6 +49,7 @@ import org.openecomp.sdc.versioning.dao.types.Version; import org.testng.Assert; import org.testng.annotations.BeforeMethod; +import java.io.IOException; import java.io.InputStream; import java.util.List; import java.util.Objects; @@ -81,7 +82,7 @@ public class QuestionnaireDataServiceTest { @InjectMocks private OrchestrationTemplateCandidateManagerImpl candidateManager; - private UploadFileTest uploadFileTest = new UploadFileTest(); + private final UploadFileTest uploadFileTest = new UploadFileTest(); private static String vspId; private static Version vspActiveVersion; @@ -93,7 +94,7 @@ public class QuestionnaireDataServiceTest { } // TODO: 3/15/2017 fix and enable //@Test - public void testQuestionnaireDataAfterLegalUploadWithComposition() { + public void testQuestionnaireDataAfterLegalUploadWithComposition() throws IOException { InformationArtifactData informationArtifactData = uploadFileAndValidateInformationArtifactData("/fullComposition", 5); @@ -102,16 +103,18 @@ public class QuestionnaireDataServiceTest { // TODO: 3/15/2017 fix and enable //@Test - public void testQuestionnaireDataAfterLegalUploadEmptyComposition() { + public void testQuestionnaireDataAfterLegalUploadEmptyComposition() throws IOException { uploadFileAndValidateInformationArtifactData("/emptyComposition", 0); } // TODO: 3/15/2017 fix and enable //@Test - public void testQuestionnaireDataAfterIllegalUpload() { - InputStream zipInputStream = uploadFileTest.getZipInputStream("/missingYml"); - UploadFileResponse uploadFileResponse = candidateManager - .upload(vspId, VERSION, zipInputStream, USER1, "zip", "missingYml"); + public void testQuestionnaireDataAfterIllegalUpload() throws IOException { + + try (InputStream zipInputStream = uploadFileTest.getZipInputStream("/missingYml")) { + UploadFileResponse uploadFileResponse = candidateManager + .upload(vspId, VERSION, zipInputStream, USER1, "zip", "missingYml"); + } InformationArtifactData informationArtifactData = questionnaireDataService .generateQuestionnaireDataForInformationArtifact(vspId, vspActiveVersion); @@ -119,15 +122,18 @@ public class QuestionnaireDataServiceTest { } private InformationArtifactData uploadFileAndValidateInformationArtifactData(String filePath, - int listSizeToCheck) { - InputStream zipInputStream = uploadFileTest.getZipInputStream(filePath); - UploadFileResponse uploadFileResponse = candidateManager - .upload(vspId, VERSION, - zipInputStream, USER1,"zip", "file"); - candidateManager.process(vspId, VERSION, USER1); - - Assert.assertTrue(MapUtils.isEmpty( - MessageContainerUtil.getMessageByLevel(ErrorLevel.ERROR, uploadFileResponse.getErrors()))); + int listSizeToCheck) throws IOException { + + try (InputStream zipInputStream = uploadFileTest.getZipInputStream(filePath)) { + UploadFileResponse uploadFileResponse = candidateManager + .upload(vspId, VERSION, + zipInputStream, USER1, "zip", "file"); + + candidateManager.process(vspId, VERSION, USER1); + + Assert.assertTrue(MapUtils.isEmpty( + MessageContainerUtil.getMessageByLevel(ErrorLevel.ERROR, uploadFileResponse.getErrors()))); + } InformationArtifactData informationArtifactData = questionnaireDataService .generateQuestionnaireDataForInformationArtifact(vspId, vspActiveVersion); diff --git a/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/test/java/org/openecomp/sdc/vendorsoftwareproduct/impl/MonitoringUploadsManagerImplTest.java b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/test/java/org/openecomp/sdc/vendorsoftwareproduct/impl/MonitoringUploadsManagerImplTest.java index e4dd39cfda..966e1f20bf 100644 --- a/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/test/java/org/openecomp/sdc/vendorsoftwareproduct/impl/MonitoringUploadsManagerImplTest.java +++ b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/test/java/org/openecomp/sdc/vendorsoftwareproduct/impl/MonitoringUploadsManagerImplTest.java @@ -5,8 +5,6 @@ import org.mockito.Mock; import org.mockito.MockitoAnnotations; import org.openecomp.core.enrichment.types.MonitoringUploadType; import org.openecomp.sdc.common.errors.CoreException; -import org.openecomp.sdc.logging.api.Logger; -import org.openecomp.sdc.logging.api.LoggerFactory; import org.openecomp.sdc.vendorsoftwareproduct.dao.ComponentArtifactDao; import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComponentMonitoringUploadEntity; import org.openecomp.sdc.vendorsoftwareproduct.types.schemagenerator.MonitoringUploadStatus; @@ -20,6 +18,7 @@ import java.io.InputStream; import java.net.URL; import java.util.Arrays; import java.util.Optional; +import java.util.function.Consumer; import static org.mockito.Matchers.any; import static org.mockito.Matchers.anyObject; @@ -29,25 +28,23 @@ import static org.mockito.Mockito.verify; public class MonitoringUploadsManagerImplTest { - private final Logger log = (Logger) LoggerFactory.getLogger(this.getClass().getName()); - private static final String USER1 = "ComponentsUploadTestUser"; private static final String COMPONENT_ID = "COMPONENT_ID"; private static final String VSP_ID = "vspId"; private static final Version VERSION = new Version(0, 1); - private static final String trapFileName = "MMSC.zip"; - private static final String pollFileName = "MNS OAM FW.zip"; - private static final String vesFileName = "vesTest-yml_only.zip"; - private static final String invalidVesFileName = "invalid_ves_file.zip"; - private static final String notZipFileName = "notZipFile"; - private static final String zipWithFoldersFileName = "zipFileWithFolder.zip"; - private static final String emptyZipFileName = "emptyZip.zip"; + private static final String TRAP_FILE_NAME = "MMSC.zip"; + private static final String POLL_FILE_NAME = "MNS OAM FW.zip"; + private static final String VES_FILE_NAME = "vesTest-yml_only.zip"; + private static final String INVALID_VES_FILE_NAME = "invalid_ves_file.zip"; + private static final String NOT_ZIP_FILE_NAME = "notZipFile"; + private static final String ZIP_WITH_FOLDERS_FILE_NAME = "zipFileWithFolder.zip"; + private static final String EMPTY_ZIP_FILE_NAME = "emptyZip.zip"; private static final String ZIP_DIR = "/vspmanager/zips/"; @Mock private ComponentArtifactDao componentArtifactDaoMock; @InjectMocks - private MonitoringUploadsManagerImpl moitoringUploadsManager; + private MonitoringUploadsManagerImpl monitoringUploadsManager; @BeforeMethod public void setUp() throws Exception { @@ -56,51 +53,47 @@ public class MonitoringUploadsManagerImplTest { @Test(expectedExceptions = CoreException.class) public void testUploadEmptyZip() { - InputStream zis = getFileInputStream(ZIP_DIR + emptyZipFileName); - moitoringUploadsManager.upload(zis, emptyZipFileName, VSP_ID, VERSION, COMPONENT_ID, - MonitoringUploadType.SNMP_TRAP, USER1); + processFile(ZIP_DIR + EMPTY_ZIP_FILE_NAME, inputStream -> + monitoringUploadsManager.upload(inputStream, EMPTY_ZIP_FILE_NAME, VSP_ID, VERSION, COMPONENT_ID, + MonitoringUploadType.SNMP_TRAP, USER1)); } @Test public void testUploadInvalidZip() { - URL url = this.getClass().getResource("/notZipFile"); + try { - moitoringUploadsManager - .upload(url.openStream(), notZipFileName, VSP_ID, VERSION, COMPONENT_ID, - MonitoringUploadType.VES_EVENTS, USER1); + processFile("/notZipFile", inputStream -> + monitoringUploadsManager + .upload(inputStream, NOT_ZIP_FILE_NAME, VSP_ID, VERSION, COMPONENT_ID, + MonitoringUploadType.VES_EVENTS, USER1)); Assert.fail(); } catch (Exception exception) { - log.debug("",exception); Assert.assertEquals(exception.getMessage(), "Invalid zip file"); } } @Test public void testUploadZipWithFolders() { - InputStream zis = getFileInputStream(ZIP_DIR + zipWithFoldersFileName); try { - moitoringUploadsManager - .upload(zis, zipWithFoldersFileName, VSP_ID, VERSION, COMPONENT_ID, - MonitoringUploadType.SNMP_TRAP, USER1); + processFile(ZIP_DIR + ZIP_WITH_FOLDERS_FILE_NAME, inputStream -> monitoringUploadsManager + .upload(inputStream, ZIP_WITH_FOLDERS_FILE_NAME, VSP_ID, VERSION, COMPONENT_ID, + MonitoringUploadType.SNMP_TRAP, USER1)); Assert.fail(); } catch (Exception exception) { - log.debug("",exception); Assert.assertEquals(exception.getMessage(), "Zip file should not contain folders"); } } @Test public void testUploadVEsEventZipWithNonYamlFiles() { - InputStream zis = getFileInputStream(ZIP_DIR + invalidVesFileName); try { - moitoringUploadsManager - .upload(zis, invalidVesFileName, VSP_ID, VERSION, COMPONENT_ID, - MonitoringUploadType.VES_EVENTS, USER1); + processFile(ZIP_DIR + INVALID_VES_FILE_NAME, inputStream -> monitoringUploadsManager + .upload(inputStream, INVALID_VES_FILE_NAME, VSP_ID, VERSION, COMPONENT_ID, + MonitoringUploadType.VES_EVENTS, USER1)); Assert.fail(); } catch (Exception exception) { - log.debug("",exception); Assert.assertEquals(exception.getMessage(), "Wrong VES EVENT Artifact was uploaded - all files contained in Artifact must be YAML " + "files (using .yaml/.yml extensions)"); @@ -113,33 +106,33 @@ public class MonitoringUploadsManagerImplTest { ComponentMonitoringUploadEntity artifact1 = new ComponentMonitoringUploadEntity(VSP_ID, VERSION, COMPONENT_ID, "artifact1"); artifact1.setType(MonitoringUploadType.SNMP_TRAP); - artifact1.setArtifactName(trapFileName); + artifact1.setArtifactName(TRAP_FILE_NAME); ComponentMonitoringUploadEntity artifact2 = new ComponentMonitoringUploadEntity(VSP_ID, VERSION, COMPONENT_ID, "artifact2"); artifact2.setType(MonitoringUploadType.SNMP_POLL); - artifact2.setArtifactName(pollFileName); + artifact2.setArtifactName(POLL_FILE_NAME); ComponentMonitoringUploadEntity artifact3 = new ComponentMonitoringUploadEntity(VSP_ID, VERSION, COMPONENT_ID, "artifact3"); artifact3.setType(MonitoringUploadType.VES_EVENTS); - artifact3.setArtifactName(vesFileName); + artifact3.setArtifactName(VES_FILE_NAME); doReturn(Arrays.asList(artifact1, artifact2, artifact3)) .when(componentArtifactDaoMock).list(anyObject()); MonitoringUploadStatus monitoringUploadStatus = - moitoringUploadsManager.listFilenames(VSP_ID, VERSION, COMPONENT_ID, USER1); + monitoringUploadsManager.listFilenames(VSP_ID, VERSION, COMPONENT_ID, USER1); - Assert.assertEquals(monitoringUploadStatus.getSnmpTrap(), trapFileName); - Assert.assertEquals(monitoringUploadStatus.getSnmpPoll(), pollFileName); - Assert.assertEquals(monitoringUploadStatus.getVesEvent(), vesFileName); + Assert.assertEquals(monitoringUploadStatus.getSnmpTrap(), TRAP_FILE_NAME); + Assert.assertEquals(monitoringUploadStatus.getSnmpPoll(), POLL_FILE_NAME); + Assert.assertEquals(monitoringUploadStatus.getVesEvent(), VES_FILE_NAME); } @Test (expectedExceptions = CoreException.class) public void testDeleteComponentMibWhenNone() { doReturn(Optional.empty()).when(componentArtifactDaoMock).getByType(any()); - moitoringUploadsManager + monitoringUploadsManager .delete(VSP_ID, VERSION, COMPONENT_ID, MonitoringUploadType.SNMP_POLL, USER1); verify(componentArtifactDaoMock, never()).delete(anyObject()); @@ -152,20 +145,20 @@ public class MonitoringUploadsManagerImplTest { .when (componentArtifactDaoMock).getByType(anyObject()); - moitoringUploadsManager + monitoringUploadsManager .delete(VSP_ID, VERSION, COMPONENT_ID, MonitoringUploadType.SNMP_POLL, USER1); verify(componentArtifactDaoMock).delete(anyObject()); } - private InputStream getFileInputStream(String fileName) { + private void processFile(String fileName, Consumer processor) { + URL url = this.getClass().getResource(fileName); - try { - return url.openStream(); - } catch (IOException exception) { - log.debug("",exception); - return null; + try (InputStream inputStream = url.openStream()) { + processor.accept(inputStream); + } catch (IOException e) { + throw new RuntimeException("Failed to process file: " + fileName, e); } } } diff --git a/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/test/java/org/openecomp/sdc/vendorsoftwareproduct/impl/VendorSoftwareProductManagerImplTest.java b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/test/java/org/openecomp/sdc/vendorsoftwareproduct/impl/VendorSoftwareProductManagerImplTest.java index 77a847a2af..5199dfd365 100644 --- a/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/test/java/org/openecomp/sdc/vendorsoftwareproduct/impl/VendorSoftwareProductManagerImplTest.java +++ b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/test/java/org/openecomp/sdc/vendorsoftwareproduct/impl/VendorSoftwareProductManagerImplTest.java @@ -104,12 +104,12 @@ import static org.mockito.Mockito.verify; public class VendorSoftwareProductManagerImplTest { - private final Logger log = (Logger) LoggerFactory.getLogger(this.getClass().getName()); + private static final Logger LOG = LoggerFactory.getLogger(VendorSoftwareProductManagerImplTest.class); private static final String INVALID_VERSION_MSG = "Invalid requested version."; - private static String VSP_ID = "vspId"; - private static String VERSION_ID = "versionId"; + private static final String VSP_ID = "vspId"; + private static final String VERSION_ID = "versionId"; public static final Version VERSION01 = new Version(0, 1); private static final Version VERSION10 = new Version(1, 0); private static final String USER1 = "vspTestUser1"; @@ -348,14 +348,14 @@ public class VendorSoftwareProductManagerImplTest { doReturn(versionInfo).when(versioningManagerMock).getEntityVersionInfo( VendorSoftwareProductConstants.VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE, VSP_ID, USER1, VersionableEntityAction.Write); - List fgs = new ArrayList(); + List fgs = new ArrayList<>(); fgs.add("fg1"); fgs.add("fg2"); VspDetails existingVsp = createVspDetails(VSP_ID, VERSION01, "VSP1", null, "vendorName", "vlm1Id", "icon", "category", "subCategory", "456", fgs); - List updFgs = new ArrayList(); + List updFgs = new ArrayList<>(); updFgs.add("fg2"); VspDetails updatedVsp = createVspDetails(VSP_ID, VERSION01, "VSP1_updated", null, "vendorName", "vlm1Id", "icon", @@ -372,7 +372,7 @@ public class VendorSoftwareProductManagerImplTest { flavor.setFeatureGroupId("fg1"); dfEntity.setDeploymentFlavorCompositionData(flavor); - List dfList = new ArrayList(); + List dfList = new ArrayList<>(); dfList.add(dfEntity); doReturn(dfList).when(deploymentFlavorDaoMock).list(anyObject()); @@ -561,7 +561,7 @@ public class VendorSoftwareProductManagerImplTest { doReturn(vsp).when(vspInfoDaoMock).get(anyObject()); UploadDataEntity uploadData = new UploadDataEntity(VSP_ID, VERSION01); uploadData.setContentData( - ByteBuffer.wrap(FileUtils.toByteArray(getFileInputStream("/emptyComposition")))); + ByteBuffer.wrap(getBytes("/emptyComposition"))); doReturn(uploadData).when(orchestrationTemplateDataDaoMock) .getOrchestrationTemplate(anyObject(), anyObject()); doReturn(new ToscaServiceModel(new FileContentHandler(), new HashMap<>(), @@ -597,7 +597,7 @@ public class VendorSoftwareProductManagerImplTest { doReturn(vsp).when(vspInfoDaoMock).get(anyObject()); UploadDataEntity uploadData = new UploadDataEntity(VSP_ID, VERSION01); uploadData.setContentData( - ByteBuffer.wrap(FileUtils.toByteArray(getFileInputStream("/emptyComposition")))); + ByteBuffer.wrap(getBytes("/emptyComposition"))); doReturn(uploadData).when(orchestrationTemplateDataDaoMock) .getOrchestrationTemplate(anyObject(), anyObject()); doReturn(new ToscaServiceModel(new FileContentHandler(), new HashMap<>(), @@ -654,27 +654,28 @@ public class VendorSoftwareProductManagerImplTest { // TODO: 3/15/2017 fix and enable //@Test(dependsOnMethods = {"testListFinals"}) - public void testUploadFileMissingFile() { - InputStream zis = getFileInputStream("/vspmanager/zips/missingYml.zip"); + public void testUploadFileMissingFile() throws IOException { - UploadFileResponse uploadFileResponse = - candidateManager.upload(VSP_ID, VERSION01, zis, USER1, "zip", "missingYml"); + try (InputStream zis = this.getClass().getResourceAsStream("/vspmanager/zips/missingYml.zip")) { + UploadFileResponse uploadFileResponse = + candidateManager.upload(VSP_ID, VERSION01, zis, USER1, "zip", "missingYml"); - Assert.assertEquals(uploadFileResponse.getErrors().size(), 0); + Assert.assertEquals(uploadFileResponse.getErrors().size(), 0); + } } // TODO: 3/15/2017 fix and enable //@Test(dependsOnMethods = {"testUploadFileMissingFile"}) public void testUploadNotZipFile() throws IOException { + URL url = this.getClass().getResource("/notZipFile"); - try { + try (InputStream inputStream = url.openStream()) { candidateManager .upload(VSP_ID, VERSION01, - url.openStream(), USER1, "zip", "notZipFile"); + inputStream, USER1, "zip", "notZipFile"); candidateManager.process(VSP_ID, VERSION01, USER1); } catch (Exception ce) { - log.debug("",ce); Assert.assertEquals(ce.getMessage(), Messages.CREATE_MANIFEST_FROM_ZIP.getErrorMessage()); } @@ -692,11 +693,12 @@ public class VendorSoftwareProductManagerImplTest { private List getFileNamesFromFolderInCsar(File csar, String folderName) throws IOException { + List fileNames = new ArrayList<>(); - FileInputStream fileInputStream = new FileInputStream(csar); - try { - ZipInputStream zip = new ZipInputStream(fileInputStream); + try (FileInputStream fileInputStream = new FileInputStream(csar); + ZipInputStream zip = new ZipInputStream(fileInputStream)) { + ZipEntry ze; while ((ze = zip.getNextEntry()) != null) { @@ -705,8 +707,6 @@ public class VendorSoftwareProductManagerImplTest { fileNames.add(name); } } - }finally { - fileInputStream.close(); } return fileNames; @@ -718,10 +718,13 @@ public class VendorSoftwareProductManagerImplTest { checkinSubmitCreatePackage(vspId, user); } - private void uploadFileAndProcess(String vspId, String user, String filePath) { + private void uploadFileAndProcess(String vspId, String user, String filePath) throws IOException { vendorSoftwareProductManager.checkout(vspId, user); - candidateManager.upload(vspId, VERSION01, getFileInputStream(filePath), user, "zip", "file"); - candidateManager.process(vspId, VERSION01, user); + + try (InputStream inputStream = this.getClass().getResourceAsStream(filePath)) { + candidateManager.upload(vspId, VERSION01, inputStream, user, "zip", "file"); + candidateManager.process(vspId, VERSION01, user); + } } private void checkinSubmitCreatePackage(String vspId, String user) throws IOException { @@ -792,13 +795,10 @@ public class VendorSoftwareProductManagerImplTest { capabilities.put(entryValueKey + "_" + key, value); } - public InputStream getFileInputStream(String fileName) { + private byte[] getBytes(String fileName) throws IOException { URL url = this.getClass().getResource(fileName); - try { - return url.openStream(); - } catch (IOException exception) { - exception.printStackTrace(); - return null; + try (InputStream inputStream = url.openStream()) { + return FileUtils.toByteArray(inputStream); } } diff --git a/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/test/java/org/openecomp/sdc/vendorsoftwareproduct/utils/VSPCommon.java b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/test/java/org/openecomp/sdc/vendorsoftwareproduct/utils/VSPCommon.java index 947f33922e..8f87c40cc1 100644 --- a/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/test/java/org/openecomp/sdc/vendorsoftwareproduct/utils/VSPCommon.java +++ b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/test/java/org/openecomp/sdc/vendorsoftwareproduct/utils/VSPCommon.java @@ -21,8 +21,6 @@ package org.openecomp.sdc.vendorsoftwareproduct.utils; import org.openecomp.core.utilities.file.FileUtils; -import org.openecomp.sdc.logging.api.Logger; -import org.openecomp.sdc.logging.api.LoggerFactory; import org.openecomp.sdc.vendorlicense.dao.types.VendorLicenseModelEntity; import org.openecomp.sdc.vendorsoftwareproduct.dao.type.VspDetails; import org.openecomp.sdc.versioning.dao.types.Version; @@ -37,8 +35,6 @@ import java.util.zip.ZipOutputStream; public class VSPCommon { - private static final Logger log = (Logger) LoggerFactory.getLogger(VSPCommon.class.getName()); - public static VspDetails createVspDetails(String id, Version version, String name, String desc, String vendorName, String vlm, String icon, String category, String subCategory, @@ -86,18 +82,18 @@ public class VSPCommon { } } else { - try { if (!path.isEmpty()) { path += File.separator; } - zos.putNextEntry(new ZipEntry(path + file.getName())); - InputStream is = new FileInputStream(file); - byte[] data = FileUtils.toByteArray(is); - zos.write(data); - zos.closeEntry(); - } catch (IOException exception) { - log.debug("",exception); - } + + try (InputStream is = new FileInputStream(file)) { + zos.putNextEntry(new ZipEntry(path + file.getName())); + byte[] data = FileUtils.toByteArray(is); + zos.write(data); + zos.closeEntry(); + } catch (IOException exception) { + throw new RuntimeException("Failed to create Zip entry for file: " + file, exception); + } } } diff --git a/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/test/java/org/openecomp/sdc/vendorsoftwareproduct/utils/ZipFileUtils.java b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/test/java/org/openecomp/sdc/vendorsoftwareproduct/utils/ZipFileUtils.java index b2a6f2fe87..b5f57e9f77 100644 --- a/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/test/java/org/openecomp/sdc/vendorsoftwareproduct/utils/ZipFileUtils.java +++ b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/test/java/org/openecomp/sdc/vendorsoftwareproduct/utils/ZipFileUtils.java @@ -20,9 +20,6 @@ package org.openecomp.sdc.vendorsoftwareproduct.utils; -import org.openecomp.sdc.logging.api.Logger; -import org.openecomp.sdc.logging.api.LoggerFactory; - import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.File; @@ -36,20 +33,20 @@ import java.util.zip.ZipOutputStream; * @since November 08, 2016 */ public class ZipFileUtils { - private static final Logger log = (Logger) LoggerFactory.getLogger(ZipFileUtils.class.getName()); + public InputStream getZipInputStream(String name) { + URL url = getClass().getResource(name); File templateDir = new File(url.getFile()); - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - ZipOutputStream zos = new ZipOutputStream(baos); + try (ByteArrayOutputStream baos = new ByteArrayOutputStream(); + ZipOutputStream zos = new ZipOutputStream(baos)) { + + VSPCommon.zipDir(templateDir, "", zos, true); + return new ByteArrayInputStream(baos.toByteArray()); - VSPCommon.zipDir(templateDir, "", zos, true); - try { - zos.close(); } catch (IOException exception) { - log.debug("",exception); + throw new RuntimeException("Failed to read from Zip file: " + name, exception); } - return new ByteArrayInputStream(baos.toByteArray()); } } -- cgit 1.2.3-korg