a62aea761ae2df3461ee08b6d42d7fdfa47fe839
[sdc.git] / openecomp-be / backend / openecomp-sdc-vendor-software-product-manager / src / test / java / org / openecomp / sdc / vendorsoftwareproduct / impl / onboarding / OnboardingPackageProcessorTest.java
1 /*
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 Nordix Foundation
4  *  ================================================================================
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *  Unless required by applicable law or agreed to in writing, software
11  *  distributed under the License is distributed on an "AS IS" BASIS,
12  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *  See the License for the specific language governing permissions and
14  *  limitations under the License.
15  *
16  *  SPDX-License-Identifier: Apache-2.0
17  *  ============LICENSE_END=========================================================
18  */
19
20 package org.openecomp.sdc.vendorsoftwareproduct.impl.onboarding;
21
22 import static org.hamcrest.Matchers.containsInAnyOrder;
23 import static org.hamcrest.Matchers.equalTo;
24 import static org.hamcrest.Matchers.is;
25 import static org.hamcrest.Matchers.notNullValue;
26 import static org.junit.Assert.assertThat;
27 import static org.junit.Assert.fail;
28 import static org.openecomp.sdc.common.errors.Messages.PACKAGE_EMPTY_ERROR;
29 import static org.openecomp.sdc.common.errors.Messages.PACKAGE_INVALID_EXTENSION;
30
31 import com.google.common.collect.ImmutableSet;
32 import java.io.IOException;
33 import java.net.URISyntaxException;
34 import java.nio.file.Files;
35 import java.nio.file.Path;
36 import java.nio.file.Paths;
37 import java.util.Arrays;
38 import java.util.Collection;
39 import java.util.Collections;
40 import java.util.Set;
41 import org.junit.Test;
42 import org.junit.runner.RunWith;
43 import org.junit.runners.Parameterized;
44 import org.junit.runners.Parameterized.Parameters;
45 import org.openecomp.core.utilities.orchestration.OnboardingTypesEnum;
46 import org.openecomp.sdc.datatypes.error.ErrorLevel;
47 import org.openecomp.sdc.datatypes.error.ErrorMessage;
48 import org.openecomp.sdc.vendorsoftwareproduct.types.OnboardPackageInfo;
49
50 @RunWith(Parameterized.class)
51 public class OnboardingPackageProcessorTest {
52     private static final String BASE_DIR = "/vspmanager.csar/";
53     private final String packageName;
54     private final byte[] packageBytes;
55     private final Set<ErrorMessage> expectedErrorSet;
56     private final OnboardingTypesEnum expectedPackageType;
57
58     public OnboardingPackageProcessorTest(final String packageName, final byte[] packageBytes,
59                                           final Set<ErrorMessage> expectedErrorSet,
60                                           final OnboardingTypesEnum expectedPackageType) {
61         this.packageName = packageName;
62         this.packageBytes = packageBytes;
63         this.expectedErrorSet = expectedErrorSet;
64         this.expectedPackageType = expectedPackageType;
65     }
66
67     @Parameters(name = "Run {index} for {0}")
68     public static Collection<Object[]> data() {
69         return Arrays.asList(new Object[][]{
70             {"emptyPackage.csar", new byte[0],
71                 ImmutableSet.of(
72                     new ErrorMessage(ErrorLevel.ERROR, PACKAGE_EMPTY_ERROR.formatMessage("emptyPackage.csar"))
73                 ), null},
74
75             {"notCsar.txt", getFileBytes("notCsar.txt"),
76                 ImmutableSet.of(
77                     new ErrorMessage(ErrorLevel.ERROR,
78                         PACKAGE_INVALID_EXTENSION.formatMessage("notCsar.txt", "csar, zip"))
79                 ), null},
80
81             {"signed-package.zip", getFileBytes("signing/signed-package.zip"), Collections.emptySet(),
82                 OnboardingTypesEnum.SIGNED_CSAR},
83
84             {"csar-and-cms-in-root.zip", getFileBytes("signing/csar-and-cms-in-root.zip"), Collections.emptySet(),
85                 OnboardingTypesEnum.SIGNED_CSAR},
86
87             {"successfulUpload.csar", getFileBytes("successfulUpload.csar"), Collections.emptySet(),
88                 OnboardingTypesEnum.CSAR},
89
90             {"fakeNonSignedZipPackage.zip", getFileBytes("signing/fakeNonSignedZipPackage.zip"), Collections.emptySet(),
91                 OnboardingTypesEnum.ZIP}
92         });
93     }
94
95     @Test
96     public void processPackage() {
97         final OnboardingPackageProcessor onboardingPackageProcessor = new OnboardingPackageProcessor(packageName, packageBytes);
98         assertThat("Should contains errors", onboardingPackageProcessor.hasErrors(), is(!expectedErrorSet.isEmpty()));
99         assertThat("Should have the same number of errors", onboardingPackageProcessor.getErrorMessageSet().size(), equalTo(expectedErrorSet.size()));
100         if (expectedErrorSet.size() > 0) {
101             assertThat("Should have the expected errors", onboardingPackageProcessor.getErrorMessageSet(), containsInAnyOrder(expectedErrorSet.toArray()));
102             return;
103         }
104         final OnboardPackageInfo onboardPackageInfo = onboardingPackageProcessor.getOnboardPackageInfo().orElse(null);
105         assertThat("Should build onboardPackageInfo", onboardPackageInfo, is(notNullValue()));
106         assertThat("Should have the expected package type", onboardPackageInfo.getPackageType(), is(equalTo(expectedPackageType)));
107     }
108
109     private static byte[] getFileBytes(final String filePath) {
110         final Path path = Paths.get(BASE_DIR, filePath);
111         try {
112             return Files.readAllBytes(Paths.get(
113                 OnboardingPackageProcessorTest.class.getResource(path.toString()).toURI()));
114         } catch (final IOException | URISyntaxException e) {
115             fail(String.format("Could not load file %s", path.toString()));
116         }
117         return null;
118     }
119
120 }