summaryrefslogtreecommitdiffstats
path: root/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/test/java/org/openecomp/sdc/vendorsoftwareproduct/impl/onboarding/OnboardingPackageProcessorTest.java
blob: 6c847a713eb73c4c81c455ec10c6d5787f9e859b (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
/*
 * ============LICENSE_START=======================================================
 *  Copyright (C) 2019 Nordix Foundation
 *  Copyright (C) 2021 Nokia
 *  ================================================================================
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 *  SPDX-License-Identifier: Apache-2.0
 *  ============LICENSE_END=========================================================
 */

package org.openecomp.sdc.vendorsoftwareproduct.impl.onboarding;

import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;
import static org.openecomp.sdc.common.errors.Messages.COULD_NOT_READ_MANIFEST_FILE;
import static org.openecomp.sdc.common.errors.Messages.MANIFEST_VALIDATION_HELM_IS_BASE_MISSING;
import static org.openecomp.sdc.common.errors.Messages.MANIFEST_VALIDATION_HELM_IS_BASE_NOT_SET;
import static org.openecomp.sdc.common.errors.Messages.PACKAGE_EMPTY_ERROR;
import static org.openecomp.sdc.common.errors.Messages.PACKAGE_INVALID_EXTENSION;

import com.google.common.collect.ImmutableSet;
import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Set;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
import org.openecomp.core.utilities.orchestration.OnboardingTypesEnum;
import org.openecomp.sdc.datatypes.error.ErrorLevel;
import org.openecomp.sdc.datatypes.error.ErrorMessage;
import org.openecomp.sdc.vendorsoftwareproduct.impl.onboarding.validation.CnfPackageValidator;
import org.openecomp.sdc.vendorsoftwareproduct.types.OnboardPackageInfo;

@RunWith(Parameterized.class)
public class OnboardingPackageProcessorTest {

    private static final String BASE_DIR = "/vspmanager.csar/";
    private final String packageName;
    private final byte[] packageBytes;
    private final Set<ErrorMessage> expectedErrorSet;
    private final OnboardingTypesEnum expectedPackageType;
    private final CnfPackageValidator cnfPackageValidator;

    public OnboardingPackageProcessorTest(final String packageName, final byte[] packageBytes,
        final Set<ErrorMessage> expectedErrorSet,
        final OnboardingTypesEnum expectedPackageType) {
        this.packageName = packageName;
        this.packageBytes = packageBytes;
        this.expectedErrorSet = expectedErrorSet;
        this.expectedPackageType = expectedPackageType;
        this.cnfPackageValidator =  new CnfPackageValidator();
    }

    @Parameters(name = "Run {index} for {0}")
    public static Collection<Object[]> data() {
        return Arrays.asList(new Object[][]{
            {"emptyPackage.csar", new byte[0],
                ImmutableSet.of(
                    new ErrorMessage(ErrorLevel.ERROR, PACKAGE_EMPTY_ERROR.formatMessage("emptyPackage.csar"))
                ), null},

            {"notCsar.txt", getFileBytes("notCsar.txt"),
                ImmutableSet.of(
                    new ErrorMessage(ErrorLevel.ERROR,
                        PACKAGE_INVALID_EXTENSION.formatMessage("notCsar.txt", "csar, zip"))
                ), null},

            {"signed-package.zip", getFileBytes("signing/signed-package.zip"), Collections.emptySet(),
                OnboardingTypesEnum.SIGNED_CSAR},

            {"csar-and-cms-in-root.zip", getFileBytes("signing/csar-and-cms-in-root.zip"), Collections.emptySet(),
                OnboardingTypesEnum.SIGNED_CSAR},

            {"successfulUpload.csar", getFileBytes("successfulUpload.csar"), Collections.emptySet(),
                OnboardingTypesEnum.CSAR},

            {"helm-package-valid.zip", getFileBytes("helm-package-valid.zip"), Collections.emptySet(),
                OnboardingTypesEnum.ZIP},

            {"helm-package-invalid.zip", getFileBytes("helm-package-invalid-missing-flag-isbase.zip"), ImmutableSet.of(
                new ErrorMessage(ErrorLevel.ERROR,
                    MANIFEST_VALIDATION_HELM_IS_BASE_NOT_SET.getErrorMessage()),
                new ErrorMessage(ErrorLevel.ERROR,
                    MANIFEST_VALIDATION_HELM_IS_BASE_MISSING.formatMessage(3)
                )
            ),
                OnboardingTypesEnum.ZIP},

            {"fakeNonSignedZipPackage.zip", getFileBytes("signing/fakeNonSignedZipPackage.zip"), ImmutableSet.of(
                new ErrorMessage(ErrorLevel.ERROR,
                    COULD_NOT_READ_MANIFEST_FILE.formatMessage("MANIFEST.json", "fakeNonSignedZipPackage.zip")
                )),
                OnboardingTypesEnum.ZIP}
        });
    }

    @Test
    public void processPackage() {
        final OnboardingPackageProcessor onboardingPackageProcessor = new OnboardingPackageProcessor(packageName,
            packageBytes, cnfPackageValidator);
        assertThat("Should contains errors", onboardingPackageProcessor.hasErrors(), is(!expectedErrorSet.isEmpty()));
        assertThat("Should have the same number of errors", onboardingPackageProcessor.getErrorMessages().size(),
            equalTo(expectedErrorSet.size()));
        if (expectedErrorSet.size() > 0) {
            assertThat("Should have the expected errors", onboardingPackageProcessor.getErrorMessages(),
                containsInAnyOrder(expectedErrorSet.toArray()));
            return;
        }
        final OnboardPackageInfo onboardPackageInfo = onboardingPackageProcessor.getOnboardPackageInfo().orElse(null);
        assertThat("Should build onboardPackageInfo", onboardPackageInfo, is(notNullValue()));
        assertThat("Should have the expected package type", onboardPackageInfo.getPackageType(),
            is(equalTo(expectedPackageType)));
    }

    private static byte[] getFileBytes(final String filePath) {
        final Path path = Paths.get(BASE_DIR, filePath);
        try {
            return Files.readAllBytes(Paths.get(
                OnboardingPackageProcessorTest.class.getResource(path.toString()).toURI()));
        } catch (final IOException | URISyntaxException e) {
            fail(String.format("Could not load file %s", path.toString()));
        }
        return null;
    }

}