2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.openecomp.sdc.vendorsoftwareproduct.upload.validation;
23 public class UploadFileValidationTest {
26 private static final String USER1 = "UploadFileValidationTest";
27 private static final String EMPTY_ZIP_FILE = "/validation/zips/emptyZip.zip";
28 private static final String MISSING_MANIFEST_IN_ZIP_FILE =
29 "/validation/zips/missingManifestInZip.zip";
30 private static final String ZIP_FILE_WITH_FOLDER = "/validation/zips/zipFileWithFolder.zip";
31 private static VendorSoftwareProductManager vendorSoftwareProductManager =
32 new VendorSoftwareProductManagerImpl();
33 private static String vspId;
35 public static VendorLicenseModelEntity createVendorLicenseModel(String name, String desc,
37 VendorLicenseModelEntity vendorLicenseModel = new VendorLicenseModelEntity();
38 vendorLicenseModel.setVendorName(name);
39 vendorLicenseModel.setDescription(desc);
40 vendorLicenseModel.setIconRef(icon);
41 return vendorLicenseModel;
46 VspDetails vspDetails = new VspDetails();
47 vspDetails.setVendorName("vspName_" + CommonMethods.nextUuId());
48 vspId = vendorSoftwareProductManager.createVsp(vspDetails, USER1).getId();
51 .deleteUniqueValue(VendorSoftwareProductConstants.UniqueValues.VENDOR_SOFTWARE_PRODUCT_NAME,
52 "VSP_syb_upload_various");
54 .deleteUniqueValue(VendorSoftwareProductConstants.UniqueValues.VENDOR_SOFTWARE_PRODUCT_NAME,
55 "vsp_syb_upload_no_error");
57 .deleteUniqueValue(VendorSoftwareProductConstants.UniqueValues.VENDOR_SOFTWARE_PRODUCT_NAME,
62 public void testUploadZipNull() {
64 vendorSoftwareProductManager.upload(vspId, null, USER1);
65 OrchestrationTemplateActionResponse response =
66 vendorSoftwareProductManager.process(vspId, USER1);
67 } catch (Exception exception) {
68 Assert.assertEquals(exception.getMessage(),
69 "Failed to get orchestration template for VSP with id " + vspId);
73 @Test(dependsOnMethods = "testUploadZipNull")
74 public void testUploadEmptyFile() {
75 UploadFileResponse uploadFileResponse = vendorSoftwareProductManager
76 .upload(vspId, new ByteArrayInputStream("".getBytes()),
78 Assert.assertEquals(uploadFileResponse.getErrors().get(SdcCommon.UPLOAD_FILE).get(0)
80 Messages.NO_ZIP_FILE_WAS_UPLOADED_OR_ZIP_NOT_EXIST.getErrorMessage());
83 @Test(dependsOnMethods = "testUploadEmptyFile")
84 public void testUploadEmptyZip() {
85 UploadFileResponse uploadFileResponse = vendorSoftwareProductManager
86 .upload(vspId, getFileInputStream(EMPTY_ZIP_FILE), USER1);
87 Assert.assertEquals(uploadFileResponse.getErrors().get(SdcCommon.UPLOAD_FILE).get(0)
89 Messages.CREATE_MANIFEST_FROM_ZIP.getErrorMessage());
91 /*Assert.assertEquals(exception.getMessage(),
92 Messages.CREATE_MANIFEST_FROM_ZIP.getErrorMessage());*//*
96 @Test(dependsOnMethods = "testUploadEmptyZip")
97 public void testUploadMissingManifestInZip() {
98 vendorSoftwareProductManager.upload(vspId,
99 getFileInputStream(MISSING_MANIFEST_IN_ZIP_FILE), USER1);
100 OrchestrationTemplateActionResponse response =
101 vendorSoftwareProductManager.process(vspId, USER1);
102 Assert.assertEquals(response.getErrors().size(), 2);
105 @Test(dependsOnMethods = "testUploadMissingManifestInZip")
106 public void testUploadZipWithFolder() {
107 vendorSoftwareProductManager
108 .upload(vspId, getFileInputStream(ZIP_FILE_WITH_FOLDER),
110 OrchestrationTemplateActionResponse response =
111 vendorSoftwareProductManager.process(vspId, USER1);
112 Assert.assertEquals(response.getErrors().size(), 2);
115 @Test(dependsOnMethods = "testUploadZipWithFolder")
116 public void testUploadVariousZips() {
118 File[] files = getFileList("/validation/zips/various");
120 for (File file : files) {
122 OrchestrationTemplateActionResponse response;
125 is = new FileInputStream(file);
126 vendorSoftwareProductManager.upload(vspId, is, USER1);
128 vendorSoftwareProductManager.process(vspId, USER1);
130 } catch (FileNotFoundException exception) {
131 throw new RuntimeException(exception);
132 } catch (RuntimeException re) {
134 throw new RuntimeException("failed upload:" + file.getName(), re);
136 System.out.println("zip:" + file.getName() + " Errors:" + calculateNumberOfMessages(
137 MessageContainerUtil.getMessageByLevel(ErrorLevel.ERROR, response.getErrors())) +
138 " Warnings:" + calculateNumberOfMessages(
139 MessageContainerUtil.getMessageByLevel(ErrorLevel.WARNING, response.getErrors())));
144 @Test(dependsOnMethods = "testUploadVariousZips")
145 public void testUploadNoErrorVariousZips() {
148 File[] files = getFileList("/validation/zips/various/noError");
150 for (File file : files) {
153 is = new FileInputStream(file);
154 vendorSoftwareProductManager.upload(vspId, is, USER1);
155 OrchestrationTemplateActionResponse response =
156 vendorSoftwareProductManager.process(vspId, USER1);
157 Map<String, List<ErrorMessage>> errors = response.getErrors();
159 MapUtils.isEmpty(MessageContainerUtil.getMessageByLevel(ErrorLevel.ERROR, errors)));
162 } catch (FileNotFoundException exception) {
163 throw new RuntimeException(exception);
164 } catch (RuntimeException re) {
165 Assert.fail("failed upload:" + file.getName() + " exception:" + re.getMessage());
171 private InputStream getFileInputStream(String fileName) {
172 URL url = this.getClass().getResource(fileName);
174 return url.openStream();
175 } catch (IOException exception) {
176 exception.printStackTrace();
181 private File[] getFileList(String dir) {
182 URL url = UploadFileValidationTest.class.getResource(dir);
184 String path = url.getPath();
185 File pathFile = new File(path);
186 return pathFile.listFiles();
191 private int calculateNumberOfMessages(Map<String, List<ErrorMessage>> messages) {
193 for (List<ErrorMessage> errors : messages.values()) {
194 sum += errors.size();