Add new SOL004 ETSI Validator
[sdc.git] / openecomp-be / backend / openecomp-sdc-vendor-software-product-manager / src / test / java / org / openecomp / sdc / vendorsoftwareproduct / impl / orchestration / csar / validation / SOL004Version4MetaDirectoryValidatorTest.java
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.Assert.assertEquals;
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 java.util.List;
39 import java.util.Map;
40 import org.junit.jupiter.api.Test;
41 import org.openecomp.sdc.datatypes.error.ErrorMessage;
42 import org.openecomp.sdc.tosca.csar.ManifestTokenType;
43 import org.openecomp.sdc.vendorsoftwareproduct.security.SecurityManager;
44
45 public class SOL004Version4MetaDirectoryValidatorTest extends SOL004MetaDirectoryValidatorTest {
46
47     private static int manifestDefinitionErrorCountVersion4 = 2;
48
49     @Override
50     public SOL004MetaDirectoryValidator getSOL004MetaDirectoryValidator() {
51         return new SOL004Version4MetaDirectoryValidator();
52     }
53
54     @Override
55     public StringBuilder getMetaFileBuilder() {
56         return super.getMetaFileBuilder().append(OTHER_DEFINITIONS.getName())
57         .append(ATTRIBUTE_VALUE_SEPARATOR.getToken()).append(" ").append(TOSCA_DEFINITION_FILEPATH).append("\n");
58     }
59
60     @Override
61     protected SOL004MetaDirectoryValidator getSol004WithSecurity(SecurityManager securityManagerMock) {
62         return new SOL004Version4MetaDirectoryValidator(securityManagerMock);
63     }
64
65     @Override
66     protected ManifestBuilder getVnfManifestSampleBuilder() {
67         return super.getVnfManifestSampleBuilder()
68             .withMetaData(ManifestTokenType.VNF_SOFTWARE_VERSION.getToken(), "1.0.0")
69             .withMetaData(ManifestTokenType.VNFD_ID.getToken(), "2116fd24-83f2-416b-bf3c-ca1964793aca")
70             .withMetaData(ManifestTokenType.COMPATIBLE_SPECIFICATION_VERSIONS.getToken(), "2.6.1, 2.7.1, 3.3.1")
71             .withMetaData(ManifestTokenType.VNF_PROVIDER_ID.getToken(), "ACME")
72             .withMetaData(ManifestTokenType.VNF_RELEASE_DATE_TIME.getToken(), "2021-02-11T11:25:00+00:00")
73             .withMetaData(ManifestTokenType.VNF_PACKAGE_VERSION.getToken(), "1.0")
74             .withMetaData(ManifestTokenType.VNFM_INFO.getToken(), "etsivnfm:v2.3.1,0:myGreatVnfm-1")
75             .withMetaData(ManifestTokenType.VNF_PRODUCT_NAME.getToken(), "RadioNode");
76     }
77
78     @Override
79     protected ManifestBuilder getPnfManifestSampleBuilder() {
80         return super.getPnfManifestSampleBuilder()
81             .withMetaData(ManifestTokenType.COMPATIBLE_SPECIFICATION_VERSIONS.getToken(), "2.6.1, 2.7.1, 3.3.1");
82     }
83
84     @Override
85     protected int getManifestDefinitionErrorCount() {
86         return manifestDefinitionErrorCountVersion4;
87     }
88
89     @Test
90     public void testGivenManifestFile_withValidSourceAndNonManoSources_thenNoErrorIsReturned() throws IOException {
91         final ManifestBuilder manifestBuilder = getVnfManifestSampleBuilder();
92
93         handler.addFile(TOSCA_META_PATH_FILE_NAME, metaFileBuilder.toString().getBytes(StandardCharsets.UTF_8));
94         manifestBuilder.withSource(TOSCA_META_PATH_FILE_NAME);
95
96         handler.addFile(TOSCA_CHANGELOG_FILEPATH, "".getBytes());
97         manifestBuilder.withSource(TOSCA_CHANGELOG_FILEPATH);
98
99         handler.addFile(TOSCA_DEFINITION_FILEPATH, getResourceBytesOrFail(SAMPLE_DEFINITION_FILE_PATH));
100         manifestBuilder.withSource(TOSCA_DEFINITION_FILEPATH);
101
102         handler.addFile(SAMPLE_SOURCE, "".getBytes());
103         manifestBuilder.withSource(SAMPLE_SOURCE);
104
105         handler.addFile(SAMPLE_DEFINITION_IMPORT_FILE_PATH, getResourceBytesOrFail(SAMPLE_DEFINITION_FILE_PATH));
106         manifestBuilder.withSource(SAMPLE_DEFINITION_IMPORT_FILE_PATH);
107
108         final String nonManoSource = "Artifacts/Deployment/non-mano/onap-cnf-helm-valid.yaml";
109         handler.addFile(nonManoSource, getResourceBytesOrFail("validation.files/empty.yaml"));
110         manifestBuilder.withNonManoArtifact(ONAP_CNF_HELM.getType(), nonManoSource);
111
112         manifestBuilder.withSource(TOSCA_MANIFEST_FILEPATH);
113         handler.addFile(TOSCA_MANIFEST_FILEPATH, manifestBuilder.build().getBytes(StandardCharsets.UTF_8));
114
115         final Validator validator = ValidatorFactory.getValidator(handler);
116         final Map<String, List<ErrorMessage>> errors = validator.validateContent(handler);
117         assertEquals(0, errors.size());
118
119
120     }
121
122     @Test
123     public void testGivenManifestFile_withNotReferencedNonManoSources_thenErrorIsReturned() throws IOException {
124         final ManifestBuilder manifestBuilder = getVnfManifestSampleBuilder();
125
126         handler.addFile(TOSCA_META_PATH_FILE_NAME, metaFileBuilder.toString().getBytes(StandardCharsets.UTF_8));
127         manifestBuilder.withSource(TOSCA_META_PATH_FILE_NAME);
128
129         handler.addFile(TOSCA_CHANGELOG_FILEPATH, "".getBytes());
130         manifestBuilder.withSource(TOSCA_CHANGELOG_FILEPATH);
131
132         handler.addFile(TOSCA_DEFINITION_FILEPATH, getResourceBytesOrFail(SAMPLE_DEFINITION_FILE_PATH));
133         manifestBuilder.withSource(TOSCA_DEFINITION_FILEPATH);
134
135         handler.addFile(SAMPLE_SOURCE, "".getBytes());
136         manifestBuilder.withSource(SAMPLE_SOURCE);
137
138         handler.addFile(SAMPLE_DEFINITION_IMPORT_FILE_PATH, getResourceBytesOrFail(SAMPLE_DEFINITION_FILE_PATH));
139         manifestBuilder.withSource(SAMPLE_DEFINITION_IMPORT_FILE_PATH);
140
141         //non existent reference
142         manifestBuilder.withNonManoArtifact(ONAP_CNF_HELM.getType(), "validation.files/notReferencedFile.yaml");
143
144         manifestBuilder.withSource(TOSCA_MANIFEST_FILEPATH);
145         handler.addFile(TOSCA_MANIFEST_FILEPATH, manifestBuilder.build().getBytes(StandardCharsets.UTF_8));
146
147         final Validator validator = ValidatorFactory.getValidator(handler);
148         final Map<String, List<ErrorMessage>> errors = validator.validateContent(handler);
149         assertExpectedErrors("Non-MANO file does not exist", errors, 1);
150     }
151
152     @Test
153     public void testGivenManifestFile_withNonExistentSourceFile_thenErrorIsReturned() throws IOException {
154         final ManifestBuilder manifestBuilder = getPnfManifestSampleBuilder();
155         //non existent reference
156         manifestBuilder.withSource("Artifacts/Deployment/non-mano/RadioNode.yaml");
157
158         handler.addFile(TOSCA_META_PATH_FILE_NAME, metaFileBuilder.toString().getBytes(StandardCharsets.UTF_8));
159         manifestBuilder.withSource(TOSCA_META_PATH_FILE_NAME);
160
161         handler.addFile(TOSCA_CHANGELOG_FILEPATH, "".getBytes());
162         manifestBuilder.withSource(TOSCA_CHANGELOG_FILEPATH);
163
164         handler.addFile(TOSCA_DEFINITION_FILEPATH, getResourceBytesOrFail(SAMPLE_DEFINITION_FILE_PATH));
165         manifestBuilder.withSource(TOSCA_DEFINITION_FILEPATH);
166
167         handler.addFile(SAMPLE_DEFINITION_IMPORT_FILE_PATH, "".getBytes());
168         manifestBuilder.withSource(SAMPLE_DEFINITION_IMPORT_FILE_PATH);
169
170         final String nonManoSource = "Artifacts/Deployment/non-mano/onap-cnf-helm-valid.yaml";
171         handler.addFile(nonManoSource, getResourceBytesOrFail("validation.files/empty.yaml"));
172         manifestBuilder.withNonManoArtifact(ONAP_CNF_HELM.getType(), nonManoSource);
173
174         manifestBuilder.withSource(TOSCA_MANIFEST_FILEPATH);
175         handler.addFile(TOSCA_MANIFEST_FILEPATH, manifestBuilder.build().getBytes(StandardCharsets.UTF_8));
176
177         final Validator validator = ValidatorFactory.getValidator(handler);
178         final Map<String, List<ErrorMessage>> errors = validator.validateContent(handler);
179         assertExpectedErrors("Manifest with non existent source files", errors, 1);
180     }
181
182 }