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
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.openecomp.sdc.vendorsoftwareproduct.impl.onboarding;
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;
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;
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;
54 @RunWith(Parameterized.class)
55 public class OnboardingPackageProcessorTest {
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;
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;
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],
77 new ErrorMessage(ErrorLevel.ERROR, PACKAGE_EMPTY_ERROR.formatMessage("emptyPackage.csar"))
80 {"notCsar.txt", getFileBytes("notCsar.txt"),
82 new ErrorMessage(ErrorLevel.ERROR,
83 PACKAGE_INVALID_EXTENSION.formatMessage("notCsar.txt", "csar, zip"))
86 {"signed-package.zip", getFileBytes("signing/signed-package.zip"), Collections.emptySet(),
87 OnboardingTypesEnum.SIGNED_CSAR},
89 {"csar-and-cms-in-root.zip", getFileBytes("signing/csar-and-cms-in-root.zip"), Collections.emptySet(),
90 OnboardingTypesEnum.SIGNED_CSAR},
92 {"successfulUpload.csar", getFileBytes("successfulUpload.csar"), Collections.emptySet(),
93 OnboardingTypesEnum.CSAR},
95 {"helm-package-valid.zip", getFileBytes("helm-package-valid.zip"), Collections.emptySet(),
96 OnboardingTypesEnum.ZIP},
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)
105 OnboardingTypesEnum.ZIP},
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")
111 OnboardingTypesEnum.ZIP}
116 public void processPackage() {
117 final OnboardingPackageProcessor onboardingPackageProcessor = new OnboardingPackageProcessor(packageName,
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()));
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)));
133 private static byte[] getFileBytes(final String filePath) {
134 final Path path = Paths.get(BASE_DIR, filePath);
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()));