[SDC-29] Amdocs OnBoard 1707 initial commit.
[sdc.git] / openecomp-be / backend / openecomp-sdc-vendor-software-product-manager / src / test / java / org / openecomp / sdc / vendorsoftwareproduct / upload / validation / UploadFileValidationTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.openecomp.sdc.vendorsoftwareproduct.upload.validation;
22
23 public class UploadFileValidationTest {
24 /*
25
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;
34
35   public static VendorLicenseModelEntity createVendorLicenseModel(String name, String desc,
36                                                                   String icon) {
37     VendorLicenseModelEntity vendorLicenseModel = new VendorLicenseModelEntity();
38     vendorLicenseModel.setVendorName(name);
39     vendorLicenseModel.setDescription(desc);
40     vendorLicenseModel.setIconRef(icon);
41     return vendorLicenseModel;
42   }
43
44   @BeforeTest
45   private void init() {
46     VspDetails vspDetails = new VspDetails();
47     vspDetails.setVendorName("vspName_" + CommonMethods.nextUuId());
48     vspId = vendorSoftwareProductManager.createVsp(vspDetails, USER1).getId();
49
50     UniqueValueUtil
51         .deleteUniqueValue(VendorSoftwareProductConstants.UniqueValues.VENDOR_SOFTWARE_PRODUCT_NAME,
52             "VSP_syb_upload_various");
53     UniqueValueUtil
54         .deleteUniqueValue(VendorSoftwareProductConstants.UniqueValues.VENDOR_SOFTWARE_PRODUCT_NAME,
55             "vsp_syb_upload_no_error");
56     UniqueValueUtil
57         .deleteUniqueValue(VendorSoftwareProductConstants.UniqueValues.VENDOR_SOFTWARE_PRODUCT_NAME,
58             "vsp_syb");
59   }
60
61   @Test
62   public void testUploadZipNull() {
63     try {
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);
70     }
71   }
72
73   @Test(dependsOnMethods = "testUploadZipNull")
74   public void testUploadEmptyFile() {
75     UploadFileResponse uploadFileResponse = vendorSoftwareProductManager
76         .upload(vspId, new ByteArrayInputStream("".getBytes()),
77             USER1);
78     Assert.assertEquals(uploadFileResponse.getErrors().get(SdcCommon.UPLOAD_FILE).get(0)
79             .getMessage(),
80         Messages.NO_ZIP_FILE_WAS_UPLOADED_OR_ZIP_NOT_EXIST.getErrorMessage());
81   }
82
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)
88               .getMessage(),
89           Messages.CREATE_MANIFEST_FROM_ZIP.getErrorMessage());
90       */
91 /*Assert.assertEquals(exception.getMessage(),
92           Messages.CREATE_MANIFEST_FROM_ZIP.getErrorMessage());*//*
93
94   }
95
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);
103   }
104
105   @Test(dependsOnMethods = "testUploadMissingManifestInZip")
106   public void testUploadZipWithFolder() {
107     vendorSoftwareProductManager
108         .upload(vspId, getFileInputStream(ZIP_FILE_WITH_FOLDER),
109             USER1);
110     OrchestrationTemplateActionResponse response =
111         vendorSoftwareProductManager.process(vspId, USER1);
112     Assert.assertEquals(response.getErrors().size(), 2);
113   }
114
115   @Test(dependsOnMethods = "testUploadZipWithFolder")
116   public void testUploadVariousZips() {
117
118     File[] files = getFileList("/validation/zips/various");
119     InputStream is;
120     for (File file : files) {
121       if (file.isFile()) {
122         OrchestrationTemplateActionResponse response;
123         try {
124
125           is = new FileInputStream(file);
126           vendorSoftwareProductManager.upload(vspId, is, USER1);
127           response =
128               vendorSoftwareProductManager.process(vspId, USER1);
129
130         } catch (FileNotFoundException exception) {
131           throw new RuntimeException(exception);
132         } catch (RuntimeException re) {
133
134           throw new RuntimeException("failed upload:" + file.getName(), re);
135         }
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())));
140       }
141     }
142   }
143
144   @Test(dependsOnMethods = "testUploadVariousZips")
145   public void testUploadNoErrorVariousZips() {
146
147
148     File[] files = getFileList("/validation/zips/various/noError");
149     InputStream is;
150     for (File file : files) {
151       if (file.isFile()) {
152         try {
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();
158           Assert.assertTrue(
159               MapUtils.isEmpty(MessageContainerUtil.getMessageByLevel(ErrorLevel.ERROR, errors)));
160
161
162         } catch (FileNotFoundException exception) {
163           throw new RuntimeException(exception);
164         } catch (RuntimeException re) {
165           Assert.fail("failed upload:" + file.getName() + " exception:" + re.getMessage());
166         }
167       }
168     }
169   }
170
171   private InputStream getFileInputStream(String fileName) {
172     URL url = this.getClass().getResource(fileName);
173     try {
174       return url.openStream();
175     } catch (IOException exception) {
176       exception.printStackTrace();
177       return null;
178     }
179   }
180
181   private File[] getFileList(String dir) {
182     URL url = UploadFileValidationTest.class.getResource(dir);
183
184     String path = url.getPath();
185     File pathFile = new File(path);
186     return pathFile.listFiles();
187
188
189   }
190
191   private int calculateNumberOfMessages(Map<String, List<ErrorMessage>> messages) {
192     int sum = 0;
193     for (List<ErrorMessage> errors : messages.values()) {
194       sum += errors.size();
195     }
196     return sum;
197   }
198
199 */
200
201 }