aboutsummaryrefslogtreecommitdiffstats
path: root/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/test/java/org/openecomp/sdc/vendorsoftwareproduct/upload/validation/UploadFileValidationTest.java
blob: 0be5bef352d084c421f4c2bd894efa0cade50679 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/*-
 * ============LICENSE_START=======================================================
 * SDC
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.openecomp.sdc.vendorsoftwareproduct.upload.validation;

public class UploadFileValidationTest {
/*

  private static final String USER1 = "UploadFileValidationTest";
  private static final String EMPTY_ZIP_FILE = "/validation/zips/emptyZip.zip";
  private static final String MISSING_MANIFEST_IN_ZIP_FILE =
      "/validation/zips/missingManifestInZip.zip";
  private static final String ZIP_FILE_WITH_FOLDER = "/validation/zips/zipFileWithFolder.zip";
  private static VendorSoftwareProductManager vendorSoftwareProductManager =
      new VendorSoftwareProductManagerImpl();
  private static String vspId;

  public static VendorLicenseModelEntity createVendorLicenseModel(String name, String desc,
                                                                  String icon) {
    VendorLicenseModelEntity vendorLicenseModel = new VendorLicenseModelEntity();
    vendorLicenseModel.setVendorName(name);
    vendorLicenseModel.setDescription(desc);
    vendorLicenseModel.setIconRef(icon);
    return vendorLicenseModel;
  }

  @BeforeTest
  private void init() {
    VspDetails vspDetails = new VspDetails();
    vspDetails.setVendorName("vspName_" + CommonMethods.nextUuId());
    vspId = vendorSoftwareProductManager.createVsp(vspDetails, USER1).getId();

    UniqueValueUtil
        .deleteUniqueValue(VendorSoftwareProductConstants.UniqueValues.VENDOR_SOFTWARE_PRODUCT_NAME,
            "VSP_syb_upload_various");
    UniqueValueUtil
        .deleteUniqueValue(VendorSoftwareProductConstants.UniqueValues.VENDOR_SOFTWARE_PRODUCT_NAME,
            "vsp_syb_upload_no_error");
    UniqueValueUtil
        .deleteUniqueValue(VendorSoftwareProductConstants.UniqueValues.VENDOR_SOFTWARE_PRODUCT_NAME,
            "vsp_syb");
  }

  @Test
  public void testUploadZipNull() {
    try {
      vendorSoftwareProductManager.upload(vspId, null, USER1);
      OrchestrationTemplateActionResponse response =
          vendorSoftwareProductManager.process(vspId, USER1);
    } catch (Exception exception) {
      Assert.assertEquals(exception.getMessage(),
          "Failed to get orchestration template for VSP with id " + vspId);
    }
  }

  @Test(dependsOnMethods = "testUploadZipNull")
  public void testUploadEmptyFile() {
    UploadFileResponse uploadFileResponse = vendorSoftwareProductManager
        .upload(vspId, new ByteArrayInputStream("".getBytes()),
            USER1);
    Assert.assertEquals(uploadFileResponse.getErrors().get(SdcCommon.UPLOAD_FILE).get(0)
            .getMessage(),
        Messages.NO_ZIP_FILE_WAS_UPLOADED_OR_ZIP_NOT_EXIST.getErrorMessage());
  }

  @Test(dependsOnMethods = "testUploadEmptyFile")
  public void testUploadEmptyZip() {
      UploadFileResponse uploadFileResponse = vendorSoftwareProductManager
          .upload(vspId, getFileInputStream(EMPTY_ZIP_FILE), USER1);
      Assert.assertEquals(uploadFileResponse.getErrors().get(SdcCommon.UPLOAD_FILE).get(0)
              .getMessage(),
          Messages.CREATE_MANIFEST_FROM_ZIP.getErrorMessage());
      */
/*Assert.assertEquals(exception.getMessage(),
          Messages.CREATE_MANIFEST_FROM_ZIP.getErrorMessage());*//*

  }

  @Test(dependsOnMethods = "testUploadEmptyZip")
  public void testUploadMissingManifestInZip() {
    vendorSoftwareProductManager.upload(vspId,
        getFileInputStream(MISSING_MANIFEST_IN_ZIP_FILE), USER1);
    OrchestrationTemplateActionResponse response =
        vendorSoftwareProductManager.process(vspId, USER1);
    Assert.assertEquals(response.getErrors().size(), 2);
  }

  @Test(dependsOnMethods = "testUploadMissingManifestInZip")
  public void testUploadZipWithFolder() {
    vendorSoftwareProductManager
        .upload(vspId, getFileInputStream(ZIP_FILE_WITH_FOLDER),
            USER1);
    OrchestrationTemplateActionResponse response =
        vendorSoftwareProductManager.process(vspId, USER1);
    Assert.assertEquals(response.getErrors().size(), 2);
  }

  @Test(dependsOnMethods = "testUploadZipWithFolder")
  public void testUploadVariousZips() {

    File[] files = getFileList("/validation/zips/various");
    InputStream is;
    for (File file : files) {
      if (file.isFile()) {
        OrchestrationTemplateActionResponse response;
        try {

          is = new FileInputStream(file);
          vendorSoftwareProductManager.upload(vspId, is, USER1);
          response =
              vendorSoftwareProductManager.process(vspId, USER1);

        } catch (FileNotFoundException exception) {
          throw new RuntimeException(exception);
        } catch (RuntimeException re) {

          throw new RuntimeException("failed upload:" + file.getName(), re);
        }
        System.out.println("zip:" + file.getName() + " Errors:" + calculateNumberOfMessages(
            MessageContainerUtil.getMessageByLevel(ErrorLevel.ERROR, response.getErrors())) +
            " Warnings:" + calculateNumberOfMessages(
            MessageContainerUtil.getMessageByLevel(ErrorLevel.WARNING, response.getErrors())));
      }
    }
  }

  @Test(dependsOnMethods = "testUploadVariousZips")
  public void testUploadNoErrorVariousZips() {


    File[] files = getFileList("/validation/zips/various/noError");
    InputStream is;
    for (File file : files) {
      if (file.isFile()) {
        try {
          is = new FileInputStream(file);
          vendorSoftwareProductManager.upload(vspId, is, USER1);
          OrchestrationTemplateActionResponse response =
              vendorSoftwareProductManager.process(vspId, USER1);
          Map<String, List<ErrorMessage>> errors = response.getErrors();
          Assert.assertTrue(
              MapUtils.isEmpty(MessageContainerUtil.getMessageByLevel(ErrorLevel.ERROR, errors)));


        } catch (FileNotFoundException exception) {
          throw new RuntimeException(exception);
        } catch (RuntimeException re) {
          Assert.fail("failed upload:" + file.getName() + " exception:" + re.getMessage());
        }
      }
    }
  }

  private InputStream getFileInputStream(String fileName) {
    URL url = this.getClass().getResource(fileName);
    try {
      return url.openStream();
    } catch (IOException exception) {
      exception.printStackTrace();
      return null;
    }
  }

  private File[] getFileList(String dir) {
    URL url = UploadFileValidationTest.class.getResource(dir);

    String path = url.getPath();
    File pathFile = new File(path);
    return pathFile.listFiles();


  }

  private int calculateNumberOfMessages(Map<String, List<ErrorMessage>> messages) {
    int sum = 0;
    for (List<ErrorMessage> errors : messages.values()) {
      sum += errors.size();
    }
    return sum;
  }

*/

}