9587f59e72a2f6bfe71782b2446dd060e3521d9b
[sdc.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.csar.validation;
22
23 import static org.junit.jupiter.api.Assertions.assertTrue;
24 import static org.openecomp.sdc.be.config.NonManoArtifactType.ONAP_CNF_HELM;
25 import static org.openecomp.sdc.be.test.util.TestResourcesHandler.getResourceBytesOrFail;
26 import static org.openecomp.sdc.tosca.csar.ManifestTokenType.ATTRIBUTE_VALUE_SEPARATOR;
27 import static org.openecomp.sdc.tosca.csar.ToscaMetaEntry.OTHER_DEFINITIONS;
28 import static org.openecomp.sdc.tosca.csar.ToscaMetadataFileInfo.TOSCA_META_PATH_FILE_NAME;
29 import static org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.csar.validation.TestConstants.SAMPLE_DEFINITION_FILE_PATH;
30 import static org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.csar.validation.TestConstants.SAMPLE_DEFINITION_IMPORT_FILE_PATH;
31 import static org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.csar.validation.TestConstants.SAMPLE_SOURCE;
32 import static org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.csar.validation.TestConstants.TOSCA_CHANGELOG_FILEPATH;
33 import static org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.csar.validation.TestConstants.TOSCA_DEFINITION_FILEPATH;
34 import static org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.csar.validation.TestConstants.TOSCA_MANIFEST_FILEPATH;
35
36 import java.io.IOException;
37 import java.nio.charset.StandardCharsets;
38 import org.junit.jupiter.api.Test;
39 import org.openecomp.sdc.tosca.csar.ManifestTokenType;
40 import org.openecomp.sdc.vendorsoftwareproduct.security.SecurityManager;
41
42 class SOL004Version4MetaDirectoryValidatorTest extends SOL004MetaDirectoryValidatorTest {
43
44     @Override
45     public SOL004MetaDirectoryValidator getSOL004MetaDirectoryValidator() {
46         return new SOL004Version4MetaDirectoryValidator();
47     }
48
49     @Override
50     public StringBuilder getMetaFileBuilder() {
51         return super.getMetaFileBuilder().append(OTHER_DEFINITIONS.getName())
52         .append(ATTRIBUTE_VALUE_SEPARATOR.getToken()).append(" ").append(TOSCA_DEFINITION_FILEPATH).append("\n");
53     }
54
55     @Override
56     protected SOL004MetaDirectoryValidator getSol004WithSecurity(SecurityManager securityManagerMock) {
57         return new SOL004Version4MetaDirectoryValidator(securityManagerMock);
58     }
59
60     @Override
61     protected ManifestBuilder getVnfManifestSampleBuilder() {
62         return super.getVnfManifestSampleBuilder()
63             .withMetaData(ManifestTokenType.VNF_SOFTWARE_VERSION.getToken(), "1.0.0")
64             .withMetaData(ManifestTokenType.VNFD_ID.getToken(), "2116fd24-83f2-416b-bf3c-ca1964793aca")
65             .withMetaData(ManifestTokenType.COMPATIBLE_SPECIFICATION_VERSIONS.getToken(), "2.6.1, 2.7.1, 3.3.1")
66             .withMetaData(ManifestTokenType.VNF_PROVIDER_ID.getToken(), "ACME")
67             .withMetaData(ManifestTokenType.VNF_RELEASE_DATE_TIME.getToken(), "2021-02-11T11:25:00+00:00")
68             .withMetaData(ManifestTokenType.VNF_PACKAGE_VERSION.getToken(), "1.0")
69             .withMetaData(ManifestTokenType.VNFM_INFO.getToken(), "etsivnfm:v2.3.1,0:myGreatVnfm-1")
70             .withMetaData(ManifestTokenType.VNF_PRODUCT_NAME.getToken(), "RadioNode");
71     }
72
73     @Override
74     protected ManifestBuilder getPnfManifestSampleBuilder() {
75         return super.getPnfManifestSampleBuilder()
76             .withMetaData(ManifestTokenType.COMPATIBLE_SPECIFICATION_VERSIONS.getToken(), "2.6.1, 2.7.1, 3.3.1");
77     }
78
79     @Override
80     protected int getManifestDefinitionErrorCount() {
81         return 2;
82     }
83
84     @Test
85     void testGivenManifestFile_withValidSourceAndNonManoSources_thenNoErrorIsReturned() throws IOException {
86         final ManifestBuilder manifestBuilder = getVnfManifestSampleBuilder();
87
88         handler.addFile(TOSCA_META_PATH_FILE_NAME, metaFileBuilder.toString().getBytes(StandardCharsets.UTF_8));
89         manifestBuilder.withSource(TOSCA_META_PATH_FILE_NAME);
90
91         handler.addFile(TOSCA_CHANGELOG_FILEPATH, "".getBytes());
92         manifestBuilder.withSource(TOSCA_CHANGELOG_FILEPATH);
93
94         handler.addFile(TOSCA_DEFINITION_FILEPATH, getResourceBytesOrFail(SAMPLE_DEFINITION_FILE_PATH));
95         manifestBuilder.withSource(TOSCA_DEFINITION_FILEPATH);
96
97         handler.addFile(SAMPLE_SOURCE, "".getBytes());
98         manifestBuilder.withSource(SAMPLE_SOURCE);
99
100         handler.addFile(SAMPLE_DEFINITION_IMPORT_FILE_PATH, getResourceBytesOrFail(SAMPLE_DEFINITION_FILE_PATH));
101         manifestBuilder.withSource(SAMPLE_DEFINITION_IMPORT_FILE_PATH);
102
103         final String nonManoSource = "Artifacts/Deployment/non-mano/onap-cnf-helm-valid.yaml";
104         handler.addFile(nonManoSource, getResourceBytesOrFail("validation.files/empty.yaml"));
105         manifestBuilder.withNonManoArtifact(ONAP_CNF_HELM.getType(), nonManoSource);
106
107         manifestBuilder.withSource(TOSCA_MANIFEST_FILEPATH);
108         handler.addFile(TOSCA_MANIFEST_FILEPATH, manifestBuilder.build().getBytes(StandardCharsets.UTF_8));
109
110         final Validator validator = ValidatorFactory.getValidator(handler);
111         final ValidationResult validationResult = validator.validate(handler);
112         assertTrue(validationResult.getErrors().isEmpty());
113     }
114
115     @Test
116     void testGivenManifestFile_withNotReferencedNonManoSources_thenErrorIsReturned() throws IOException {
117         final ManifestBuilder manifestBuilder = getVnfManifestSampleBuilder();
118
119         handler.addFile(TOSCA_META_PATH_FILE_NAME, metaFileBuilder.toString().getBytes(StandardCharsets.UTF_8));
120         manifestBuilder.withSource(TOSCA_META_PATH_FILE_NAME);
121
122         handler.addFile(TOSCA_CHANGELOG_FILEPATH, "".getBytes());
123         manifestBuilder.withSource(TOSCA_CHANGELOG_FILEPATH);
124
125         handler.addFile(TOSCA_DEFINITION_FILEPATH, getResourceBytesOrFail(SAMPLE_DEFINITION_FILE_PATH));
126         manifestBuilder.withSource(TOSCA_DEFINITION_FILEPATH);
127
128         handler.addFile(SAMPLE_SOURCE, "".getBytes());
129         manifestBuilder.withSource(SAMPLE_SOURCE);
130
131         handler.addFile(SAMPLE_DEFINITION_IMPORT_FILE_PATH, getResourceBytesOrFail(SAMPLE_DEFINITION_FILE_PATH));
132         manifestBuilder.withSource(SAMPLE_DEFINITION_IMPORT_FILE_PATH);
133
134         //non existent reference
135         manifestBuilder.withNonManoArtifact(ONAP_CNF_HELM.getType(), "validation.files/notReferencedFile.yaml");
136
137         manifestBuilder.withSource(TOSCA_MANIFEST_FILEPATH);
138         handler.addFile(TOSCA_MANIFEST_FILEPATH, manifestBuilder.build().getBytes(StandardCharsets.UTF_8));
139
140         final Validator validator = ValidatorFactory.getValidator(handler);
141         final ValidationResult validationResult = validator.validate(handler);
142         assertExpectedErrors("Non-MANO file does not exist", validationResult.getErrors(), 1);
143     }
144
145     @Test
146     void testGivenManifestFile_withNonExistentSourceFile_thenErrorIsReturned() throws IOException {
147         final ManifestBuilder manifestBuilder = getPnfManifestSampleBuilder();
148         //non existent reference
149         manifestBuilder.withSource("Artifacts/Deployment/non-mano/RadioNode.yaml");
150
151         handler.addFile(TOSCA_META_PATH_FILE_NAME, metaFileBuilder.toString().getBytes(StandardCharsets.UTF_8));
152         manifestBuilder.withSource(TOSCA_META_PATH_FILE_NAME);
153
154         handler.addFile(TOSCA_CHANGELOG_FILEPATH, "".getBytes());
155         manifestBuilder.withSource(TOSCA_CHANGELOG_FILEPATH);
156
157         handler.addFile(TOSCA_DEFINITION_FILEPATH, getResourceBytesOrFail(SAMPLE_DEFINITION_FILE_PATH));
158         manifestBuilder.withSource(TOSCA_DEFINITION_FILEPATH);
159
160         handler.addFile(SAMPLE_DEFINITION_IMPORT_FILE_PATH, "".getBytes());
161         manifestBuilder.withSource(SAMPLE_DEFINITION_IMPORT_FILE_PATH);
162
163         final String nonManoSource = "Artifacts/Deployment/non-mano/onap-cnf-helm-valid.yaml";
164         handler.addFile(nonManoSource, getResourceBytesOrFail("validation.files/empty.yaml"));
165         manifestBuilder.withNonManoArtifact(ONAP_CNF_HELM.getType(), nonManoSource);
166
167         manifestBuilder.withSource(TOSCA_MANIFEST_FILEPATH);
168         handler.addFile(TOSCA_MANIFEST_FILEPATH, manifestBuilder.build().getBytes(StandardCharsets.UTF_8));
169
170         final Validator validator = ValidatorFactory.getValidator(handler);
171         final ValidationResult validationResult = validator.validate(handler);
172         assertExpectedErrors("Manifest with non existent source files", validationResult.getErrors(), 1);
173     }
174
175 }