6c847a713eb73c4c81c455ec10c6d5787f9e859b
[sdc.git] /
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.impl.onboarding.validation.CnfPackageValidator;
53 import org.openecomp.sdc.vendorsoftwareproduct.types.OnboardPackageInfo;
54
55 @RunWith(Parameterized.class)
56 public class OnboardingPackageProcessorTest {
57
58     private static final String BASE_DIR = "/vspmanager.csar/";
59     private final String packageName;
60     private final byte[] packageBytes;
61     private final Set<ErrorMessage> expectedErrorSet;
62     private final OnboardingTypesEnum expectedPackageType;
63     private final CnfPackageValidator cnfPackageValidator;
64
65     public OnboardingPackageProcessorTest(final String packageName, final byte[] packageBytes,
66         final Set<ErrorMessage> expectedErrorSet,
67         final OnboardingTypesEnum expectedPackageType) {
68         this.packageName = packageName;
69         this.packageBytes = packageBytes;
70         this.expectedErrorSet = expectedErrorSet;
71         this.expectedPackageType = expectedPackageType;
72         this.cnfPackageValidator =  new CnfPackageValidator();
73     }
74
75     @Parameters(name = "Run {index} for {0}")
76     public static Collection<Object[]> data() {
77         return Arrays.asList(new Object[][]{
78             {"emptyPackage.csar", new byte[0],
79                 ImmutableSet.of(
80                     new ErrorMessage(ErrorLevel.ERROR, PACKAGE_EMPTY_ERROR.formatMessage("emptyPackage.csar"))
81                 ), null},
82
83             {"notCsar.txt", getFileBytes("notCsar.txt"),
84                 ImmutableSet.of(
85                     new ErrorMessage(ErrorLevel.ERROR,
86                         PACKAGE_INVALID_EXTENSION.formatMessage("notCsar.txt", "csar, zip"))
87                 ), null},
88
89             {"signed-package.zip", getFileBytes("signing/signed-package.zip"), Collections.emptySet(),
90                 OnboardingTypesEnum.SIGNED_CSAR},
91
92             {"csar-and-cms-in-root.zip", getFileBytes("signing/csar-and-cms-in-root.zip"), Collections.emptySet(),
93                 OnboardingTypesEnum.SIGNED_CSAR},
94
95             {"successfulUpload.csar", getFileBytes("successfulUpload.csar"), Collections.emptySet(),
96                 OnboardingTypesEnum.CSAR},
97
98             {"helm-package-valid.zip", getFileBytes("helm-package-valid.zip"), Collections.emptySet(),
99                 OnboardingTypesEnum.ZIP},
100
101             {"helm-package-invalid.zip", getFileBytes("helm-package-invalid-missing-flag-isbase.zip"), ImmutableSet.of(
102                 new ErrorMessage(ErrorLevel.ERROR,
103                     MANIFEST_VALIDATION_HELM_IS_BASE_NOT_SET.getErrorMessage()),
104                 new ErrorMessage(ErrorLevel.ERROR,
105                     MANIFEST_VALIDATION_HELM_IS_BASE_MISSING.formatMessage(3)
106                 )
107             ),
108                 OnboardingTypesEnum.ZIP},
109
110             {"fakeNonSignedZipPackage.zip", getFileBytes("signing/fakeNonSignedZipPackage.zip"), ImmutableSet.of(
111                 new ErrorMessage(ErrorLevel.ERROR,
112                     COULD_NOT_READ_MANIFEST_FILE.formatMessage("MANIFEST.json", "fakeNonSignedZipPackage.zip")
113                 )),
114                 OnboardingTypesEnum.ZIP}
115         });
116     }
117
118     @Test
119     public void processPackage() {
120         final OnboardingPackageProcessor onboardingPackageProcessor = new OnboardingPackageProcessor(packageName,
121             packageBytes, cnfPackageValidator);
122         assertThat("Should contains errors", onboardingPackageProcessor.hasErrors(), is(!expectedErrorSet.isEmpty()));
123         assertThat("Should have the same number of errors", onboardingPackageProcessor.getErrorMessages().size(),
124             equalTo(expectedErrorSet.size()));
125         if (expectedErrorSet.size() > 0) {
126             assertThat("Should have the expected errors", onboardingPackageProcessor.getErrorMessages(),
127                 containsInAnyOrder(expectedErrorSet.toArray()));
128             return;
129         }
130         final OnboardPackageInfo onboardPackageInfo = onboardingPackageProcessor.getOnboardPackageInfo().orElse(null);
131         assertThat("Should build onboardPackageInfo", onboardPackageInfo, is(notNullValue()));
132         assertThat("Should have the expected package type", onboardPackageInfo.getPackageType(),
133             is(equalTo(expectedPackageType)));
134     }
135
136     private static byte[] getFileBytes(final String filePath) {
137         final Path path = Paths.get(BASE_DIR, filePath);
138         try {
139             return Files.readAllBytes(Paths.get(
140                 OnboardingPackageProcessorTest.class.getResource(path.toString()).toURI()));
141         } catch (final IOException | URISyntaxException e) {
142             fail(String.format("Could not load file %s", path.toString()));
143         }
144         return null;
145     }
146
147 }