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