2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.csar.validation;
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.ToscaMetaEntryVersion261.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;
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;
42 class SOL004Version4MetaDirectoryValidatorTest extends SOL004MetaDirectoryValidatorTest {
44 private final ValidatorFactory validatorFactory = new ValidatorFactory();
47 public SOL004MetaDirectoryValidator getSOL004MetaDirectoryValidator() {
48 return new SOL004Version4MetaDirectoryValidator();
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");
58 protected SOL004MetaDirectoryValidator getSol004WithSecurity(SecurityManager securityManagerMock) {
59 return new SOL004Version4MetaDirectoryValidator(securityManagerMock);
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");
76 protected ManifestBuilder getPnfManifestSampleBuilder() {
77 return super.getPnfManifestSampleBuilder()
78 .withMetaData(ManifestTokenType.COMPATIBLE_SPECIFICATION_VERSIONS.getToken(), "2.6.1, 2.7.1, 3.3.1");
82 protected int getManifestDefinitionErrorCount() {
87 void testGivenManifestFile_withValidSourceAndNonManoSources_thenNoErrorIsReturned() throws IOException {
88 final ManifestBuilder manifestBuilder = getVnfManifestSampleBuilder();
90 handler.addFile(TOSCA_META_PATH_FILE_NAME, metaFileBuilder.toString().getBytes(StandardCharsets.UTF_8));
91 manifestBuilder.withSource(TOSCA_META_PATH_FILE_NAME);
93 handler.addFile(TOSCA_CHANGELOG_FILEPATH, "".getBytes());
94 manifestBuilder.withSource(TOSCA_CHANGELOG_FILEPATH);
96 handler.addFile(TOSCA_DEFINITION_FILEPATH, getResourceBytesOrFail(SAMPLE_DEFINITION_FILE_PATH));
97 manifestBuilder.withSource(TOSCA_DEFINITION_FILEPATH);
99 handler.addFile(SAMPLE_SOURCE, "".getBytes());
100 manifestBuilder.withSource(SAMPLE_SOURCE);
102 handler.addFile(SAMPLE_DEFINITION_IMPORT_FILE_PATH, getResourceBytesOrFail(SAMPLE_DEFINITION_FILE_PATH));
103 manifestBuilder.withSource(SAMPLE_DEFINITION_IMPORT_FILE_PATH);
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);
109 manifestBuilder.withSource(TOSCA_MANIFEST_FILEPATH);
110 handler.addFile(TOSCA_MANIFEST_FILEPATH, manifestBuilder.build().getBytes(StandardCharsets.UTF_8));
112 final Validator validator = validatorFactory.getValidator(handler);
113 final ValidationResult validationResult = validator.validate(handler);
114 assertTrue(validationResult.getErrors().isEmpty());
118 void testGivenManifestFile_withNotReferencedNonManoSources_thenErrorIsReturned() throws IOException {
119 final ManifestBuilder manifestBuilder = getVnfManifestSampleBuilder();
121 handler.addFile(TOSCA_META_PATH_FILE_NAME, metaFileBuilder.toString().getBytes(StandardCharsets.UTF_8));
122 manifestBuilder.withSource(TOSCA_META_PATH_FILE_NAME);
124 handler.addFile(TOSCA_CHANGELOG_FILEPATH, "".getBytes());
125 manifestBuilder.withSource(TOSCA_CHANGELOG_FILEPATH);
127 handler.addFile(TOSCA_DEFINITION_FILEPATH, getResourceBytesOrFail(SAMPLE_DEFINITION_FILE_PATH));
128 manifestBuilder.withSource(TOSCA_DEFINITION_FILEPATH);
130 handler.addFile(SAMPLE_SOURCE, "".getBytes());
131 manifestBuilder.withSource(SAMPLE_SOURCE);
133 handler.addFile(SAMPLE_DEFINITION_IMPORT_FILE_PATH, getResourceBytesOrFail(SAMPLE_DEFINITION_FILE_PATH));
134 manifestBuilder.withSource(SAMPLE_DEFINITION_IMPORT_FILE_PATH);
136 //non existent reference
137 manifestBuilder.withNonManoArtifact(ONAP_CNF_HELM.getType(), "validation.files/notReferencedFile.yaml");
139 manifestBuilder.withSource(TOSCA_MANIFEST_FILEPATH);
140 handler.addFile(TOSCA_MANIFEST_FILEPATH, manifestBuilder.build().getBytes(StandardCharsets.UTF_8));
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);
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");
153 handler.addFile(TOSCA_META_PATH_FILE_NAME, metaFileBuilder.toString().getBytes(StandardCharsets.UTF_8));
154 manifestBuilder.withSource(TOSCA_META_PATH_FILE_NAME);
156 handler.addFile(TOSCA_CHANGELOG_FILEPATH, "".getBytes());
157 manifestBuilder.withSource(TOSCA_CHANGELOG_FILEPATH);
159 handler.addFile(TOSCA_DEFINITION_FILEPATH, getResourceBytesOrFail(SAMPLE_DEFINITION_FILE_PATH));
160 manifestBuilder.withSource(TOSCA_DEFINITION_FILEPATH);
162 handler.addFile(SAMPLE_DEFINITION_IMPORT_FILE_PATH, "".getBytes());
163 manifestBuilder.withSource(SAMPLE_DEFINITION_IMPORT_FILE_PATH);
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);
169 manifestBuilder.withSource(TOSCA_MANIFEST_FILEPATH);
170 handler.addFile(TOSCA_MANIFEST_FILEPATH, manifestBuilder.build().getBytes(StandardCharsets.UTF_8));
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);