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