aboutsummaryrefslogtreecommitdiffstats
path: root/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/test/java/org/openecomp/sdc/vendorsoftwareproduct/upload/validation/UploadFileValidationTest.java
blob: 3ec8cb2e404126e993e39fcf906da012fc60d65d (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
package org.openecomp.sdc.vendorsoftwareproduct.upload.validation;

import org.openecomp.sdc.common.errors.CoreException;
import org.openecomp.sdc.common.utils.AsdcCommon;
import org.openecomp.sdc.datatypes.error.ErrorMessage;

import org.openecomp.sdc.vendorsoftwareproduct.VendorSoftwareProductManager;
import org.openecomp.sdc.vendorsoftwareproduct.dao.type.VspDetails;
import org.openecomp.sdc.vendorsoftwareproduct.impl.VendorSoftwareProductManagerImpl;
import org.openecomp.sdc.vendorsoftwareproduct.types.UploadFileResponse;
import org.openecomp.core.util.UniqueValueUtil;
import org.openecomp.core.utilities.CommonMethods;
import org.openecomp.core.validation.errors.Messages;
import org.openecomp.core.validation.types.MessageContainerUtil;
import org.apache.commons.collections4.MapUtils;
import org.testng.Assert;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

import java.io.*;
import java.net.URL;
import java.util.List;
import java.util.Map;

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 org.openecomp.sdc.vendorlicense.dao.types.VendorLicenseModelEntity createVendorLicenseModel(String name, String desc,
                                                                                                            String icon) {
    org.openecomp.sdc.vendorlicense.dao.types.VendorLicenseModelEntity
        vendorLicenseModel = new org.openecomp.sdc.vendorlicense.dao.types.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.createNewVsp(vspDetails, USER1).getId();

    UniqueValueUtil
        .deleteUniqueValue(org.openecomp.sdc.vendorsoftwareproduct.VendorSoftwareProductConstants.UniqueValues.VENDOR_SOFTWARE_PRODUCT_NAME,
            "VSP_syb_upload_various");
    UniqueValueUtil
        .deleteUniqueValue(org.openecomp.sdc.vendorsoftwareproduct.VendorSoftwareProductConstants.UniqueValues.VENDOR_SOFTWARE_PRODUCT_NAME,
            "vsp_syb_upload_no_error");
    UniqueValueUtil
        .deleteUniqueValue(org.openecomp.sdc.vendorsoftwareproduct.VendorSoftwareProductConstants.UniqueValues.VENDOR_SOFTWARE_PRODUCT_NAME,
            "vsp_syb");
  }

  @Test
  public void testUploadZipNull() {
    UploadFileResponse response = vendorSoftwareProductManager.uploadFile(vspId, null, USER1);
    Assert.assertEquals(response.getErrors().size(), 1);
    Assert.assertTrue(response.getErrors().containsKey(AsdcCommon.UPLOAD_FILE));
    Assert.assertEquals(response.getErrors().get(AsdcCommon.UPLOAD_FILE).get(0).getMessage(),
        Messages.NO_ZIP_FILE_WAS_UPLOADED_OR_ZIP_NOT_EXIST.getErrorMessage());
  }

  @Test(dependsOnMethods = "testUploadZipNull")
  public void testUploadEmptyFile() {
    UploadFileResponse response = vendorSoftwareProductManager
        .uploadFile(vspId, new ByteArrayInputStream("".getBytes()), USER1);
    Assert.assertEquals(response.getErrors().size(), 1);
    Assert.assertTrue(response.getErrors().containsKey(AsdcCommon.UPLOAD_FILE));
    Assert.assertEquals(response.getErrors().get(AsdcCommon.UPLOAD_FILE).get(0).getMessage(),
        Messages.NO_ZIP_FILE_WAS_UPLOADED_OR_ZIP_NOT_EXIST.getErrorMessage());
  }

  @Test(dependsOnMethods = "testUploadEmptyFile")
  public void testUploadEmptyZip() {
    UploadFileResponse response =
        vendorSoftwareProductManager.uploadFile(vspId, getFileInputStream(EMPTY_ZIP_FILE), USER1);
    Assert.assertEquals(response.getErrors().size(), 1);
    Assert.assertTrue(response.getErrors().containsKey(AsdcCommon.UPLOAD_FILE));
    Assert.assertEquals(response.getErrors().get(AsdcCommon.UPLOAD_FILE).get(0).getMessage(),
        Messages.INVALID_ZIP_FILE.getErrorMessage());
  }

  @Test(dependsOnMethods = "testUploadEmptyZip")
  public void testUploadMissingManifestInZip() {
    UploadFileResponse response = vendorSoftwareProductManager
        .uploadFile(vspId, getFileInputStream(MISSING_MANIFEST_IN_ZIP_FILE), USER1);
    Assert.assertEquals(response.getErrors().size(), 1);
    Assert.assertTrue(response.getErrors().containsKey(AsdcCommon.MANIFEST_NAME));
    Assert.assertEquals(response.getErrors().get(AsdcCommon.MANIFEST_NAME).get(0).getMessage(),
        Messages.MANIFEST_NOT_EXIST.getErrorMessage());
  }

  @Test(dependsOnMethods = "testUploadMissingManifestInZip")
  public void testUploadZipWithFolder() {
    UploadFileResponse response = vendorSoftwareProductManager
        .uploadFile(vspId, getFileInputStream(ZIP_FILE_WITH_FOLDER), USER1);
    Assert.assertEquals(response.getErrors().size(), 1);
    Assert.assertTrue(response.getErrors().containsKey(AsdcCommon.UPLOAD_FILE));
    Assert.assertEquals(response.getErrors().get(AsdcCommon.UPLOAD_FILE).get(0).getMessage(),
        Messages.ZIP_SHOULD_NOT_CONTAIN_FOLDERS.getErrorMessage());
  }

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

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

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

        } catch (FileNotFoundException e) {
          throw new RuntimeException(e);
        } catch (CoreException ce) {
          throw new RuntimeException("failed upload:" + file.getName(), ce);
        } catch (RuntimeException re) {

          throw new RuntimeException("failed upload:" + file.getName(), re);
        }
        System.out.println("zip:" + file.getName() + " Errors:" + calculateNumberOfMessages(
            MessageContainerUtil.getMessageByLevel(org.openecomp.sdc.datatypes.error.ErrorLevel.ERROR, response.getErrors())) +
            " Warnings:" + calculateNumberOfMessages(
            MessageContainerUtil.getMessageByLevel(org.openecomp.sdc.datatypes.error.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);
          UploadFileResponse response = vendorSoftwareProductManager.uploadFile(vspId, is, USER1);
          Map<String, List<ErrorMessage>> errors = response.getErrors();
          Assert.assertTrue(
              MapUtils.isEmpty(MessageContainerUtil.getMessageByLevel(
                  org.openecomp.sdc.datatypes.error.ErrorLevel.ERROR, errors)));


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

        } 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 e) {
      e.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;
  }


}