2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019 Nordix Foundation
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 * SPDX-License-Identifier: Apache-2.0
17 * ============LICENSE_END=========================================================
20 package org.openecomp.core.impl;
22 import static org.hamcrest.MatcherAssert.assertThat;
23 import static org.hamcrest.Matchers.containsInAnyOrder;
24 import static org.hamcrest.Matchers.hasSize;
25 import static org.openecomp.core.util.TestResourcesUtil.getResourceBytesOrFail;
27 import java.util.ArrayList;
28 import java.util.Arrays;
29 import java.util.Collections;
30 import java.util.HashMap;
31 import java.util.List;
34 import org.junit.Before;
35 import org.junit.Test;
36 import org.openecomp.sdc.common.errors.Messages;
37 import org.openecomp.sdc.datatypes.error.ErrorLevel;
38 import org.openecomp.sdc.datatypes.error.ErrorMessage;
40 public class ToscaDefinitionImportHandlerTest {
42 private static final String RESOURCES_FILE_PATH = "/toscaDefinitionImportHandler/";
43 private Map<String, byte[]> descriptorFileMap;
47 descriptorFileMap = new HashMap<>();
51 * Tests correct descriptor files.
54 public void testGivenDescriptorFiles_whenMainDescriptorImportsAreHandled_allDescriptorsAreProcessedWithoutError() {
55 final List<String> filesToHandleList = Arrays.asList("Definitions/Main.yaml", "Definitions/descriptorBasicImport.yaml",
56 "Definitions/descriptorWithRelativePaths.yaml", "Artifacts/descriptorWithAbsolutePaths.yaml",
57 "Artifacts/descriptorCyclicReference.yaml");
59 filesToHandleList.forEach(file -> descriptorFileMap.put(file, getResourceBytesOrFail(RESOURCES_FILE_PATH + file)));
61 final ToscaDefinitionImportHandler toscaDefinitionImportHandler = new ToscaDefinitionImportHandler(
63 "Definitions/Main.yaml");
64 final Set<String> actualHandledFiles = toscaDefinitionImportHandler.getHandledDefinitionFilesList();
66 assertThat("The handled files should be the same", actualHandledFiles, hasSize(filesToHandleList.size()));
67 assertThat("The handled files should be the same"
68 , actualHandledFiles, containsInAnyOrder(filesToHandleList.toArray(new String[0]))
71 final List<ErrorMessage> validationErrorList = toscaDefinitionImportHandler.getErrors();
72 assertThat("No errors should be detected", validationErrorList, hasSize(0));
76 * Tests an empty package.
79 public void testGivenEmptyPackage_whenMainDescriptorIsHandled_aMissingFileErrorIsReported() {
80 final List<String> filesToHandleList = Collections.emptyList();
82 final ToscaDefinitionImportHandler toscaDefinitionImportHandler = new ToscaDefinitionImportHandler(
84 "Definitions/Main.yaml");
85 final Set<String> actualHandledFiles = toscaDefinitionImportHandler.getHandledDefinitionFilesList();
87 assertThat("The handled files should be the same", actualHandledFiles, hasSize(filesToHandleList.size()));
88 assertThat("The handled files should be the same"
89 , actualHandledFiles, containsInAnyOrder(filesToHandleList.toArray(new String[0]))
92 final List<ErrorMessage> expectedErrorList = new ArrayList<>();
93 expectedErrorList.add(new ErrorMessage(ErrorLevel.ERROR,
94 Messages.MISSING_IMPORT_FILE.formatMessage("Definitions/Main.yaml")));
96 final List<ErrorMessage> validationErrorList = toscaDefinitionImportHandler.getErrors();
97 assertThat("The errors should be the same", validationErrorList, hasSize(expectedErrorList.size()));
98 assertThat("The errors should be the same"
99 , validationErrorList, containsInAnyOrder(expectedErrorList.toArray(new ErrorMessage[0]))
104 * Tests a file imported in a descriptor but missing in the package.
107 public void testGivenOneMissingDescriptorFile_whenMainDescriptorImportsAreHandled_aMissingFileErrorIsReported() {
108 final List<String> filesToHandleList = Arrays.asList("Definitions/Main.yaml",
109 "Definitions/descriptorBasicImport.yaml", "Definitions/descriptorWithRelativePaths.yaml",
110 "Artifacts/descriptorWithAbsolutePaths.yaml");
111 filesToHandleList.forEach(file -> descriptorFileMap.put(file, getResourceBytesOrFail(RESOURCES_FILE_PATH + file)));
113 final List<ErrorMessage> expectedErrorList = new ArrayList<>();
114 expectedErrorList.add(new ErrorMessage(ErrorLevel.ERROR,
115 String.format(Messages.MISSING_IMPORT_FILE.getErrorMessage(), "Artifacts/descriptorCyclicReference.yaml")));
117 final ToscaDefinitionImportHandler toscaDefinitionImportHandler = new ToscaDefinitionImportHandler(
119 "Definitions/Main.yaml");
120 final Set<String> actualHandledFiles = toscaDefinitionImportHandler.getHandledDefinitionFilesList();
122 assertThat("The handled files should be the same", actualHandledFiles, hasSize(filesToHandleList.size()));
123 assertThat("The handled files should be the same"
124 , actualHandledFiles, containsInAnyOrder(filesToHandleList.toArray(new String[0]))
127 final List<ErrorMessage> validationErrorList = toscaDefinitionImportHandler.getErrors();
128 assertThat("The errors should be the same", validationErrorList, hasSize(expectedErrorList.size()));
129 assertThat("The errors should be the same"
130 , validationErrorList, containsInAnyOrder(expectedErrorList.toArray(new ErrorMessage[0]))
135 * Tests a descriptor with invalid import statements.
138 public void testGivenDescriptorWithInvalidImportStatement_whenMainDescriptorImportsAreHandled_aInvalidImportStatementErrorIsReported() {
139 final String mainDefinitionFile = "Definitions/MainWithInvalidImportedFile.yaml";
141 final List<String> filesToHandleList = Arrays.asList(mainDefinitionFile,
142 "Definitions/descriptorInvalidImportStatement.yaml");
143 filesToHandleList.forEach(file -> descriptorFileMap.put(file, getResourceBytesOrFail(RESOURCES_FILE_PATH + file)));
145 final List<ErrorMessage> expectedErrorList = new ArrayList<>();
146 expectedErrorList.add(new ErrorMessage(ErrorLevel.ERROR,
147 Messages.INVALID_IMPORT_STATEMENT.formatMessage("Definitions/descriptorInvalidImportStatement.yaml", "null")));
149 final ToscaDefinitionImportHandler toscaDefinitionImportHandler = new ToscaDefinitionImportHandler(
152 final Set<String> actualHandledFiles = toscaDefinitionImportHandler.getHandledDefinitionFilesList();
154 assertThat("The handled files should be the same", actualHandledFiles, hasSize(filesToHandleList.size()));
155 assertThat("The handled files should be the same"
156 , actualHandledFiles, containsInAnyOrder(filesToHandleList.toArray(new String[0]))
159 final List<ErrorMessage> validationErrorList = toscaDefinitionImportHandler.getErrors();
160 assertThat("The errors should be the same", validationErrorList, hasSize(expectedErrorList.size()));
161 assertThat("The errors should be the same"
162 , validationErrorList, containsInAnyOrder(expectedErrorList.toArray(new ErrorMessage[0]))
167 * Tests an invalid main descriptor file path.
170 public void testGivenInvalidMainDescriptorFilePath_whenDescriptorIsHandled_aMissingImportErrorIsReported() {
171 final String mainDefinitionFilePath = "Definitions/Main1.yaml";
172 final String invalidMainDefinitionFilePath = "../Definitions/InvalidMainDefinitionFile.yaml";
174 final List<String> filesToHandleList = Arrays.asList(mainDefinitionFilePath);
175 filesToHandleList.forEach(file -> descriptorFileMap.put(file, getResourceBytesOrFail(RESOURCES_FILE_PATH + file)));
177 final List<ErrorMessage> expectedErrorList = new ArrayList<>();
178 expectedErrorList.add(new ErrorMessage(ErrorLevel.ERROR, Messages.MISSING_IMPORT_FILE.formatMessage(invalidMainDefinitionFilePath)));
180 final ToscaDefinitionImportHandler toscaDefinitionImportHandler = new ToscaDefinitionImportHandler(
182 invalidMainDefinitionFilePath);
183 final Set<String> actualHandledFiles = toscaDefinitionImportHandler.getHandledDefinitionFilesList();
185 assertThat("No files should be handled", actualHandledFiles, hasSize(0));
187 final List<ErrorMessage> validationErrorList = toscaDefinitionImportHandler.getErrors();
189 assertThat("The errors should be the same", validationErrorList, hasSize(expectedErrorList.size()));
190 assertThat("The errors should be the same"
191 , validationErrorList, containsInAnyOrder(expectedErrorList.toArray(new ErrorMessage[0]))
196 * Tests a descriptor with invalid yaml.
199 public void testGivenInvalidYamlDescriptorFile_whenDescriptorIsHandled_aInvalidYamlFormatErrorIsReported() {
200 final String mainDefinitionFile = "Definitions/descriptorInvalid.yaml";
202 final List<String> filesToHandleList = Arrays.asList(mainDefinitionFile);
203 filesToHandleList.forEach(file -> descriptorFileMap.put(file, getResourceBytesOrFail(RESOURCES_FILE_PATH + file)));
205 final List<ErrorMessage> expectedErrorList = new ArrayList<>();
206 expectedErrorList.add(new ErrorMessage(ErrorLevel.ERROR, String.format(Messages.INVALID_YAML_FORMAT.getErrorMessage()
207 , "while scanning a simple key\n"
208 + " in 'string', line 5, column 3:\n"
209 + " template_author= onap\n"
211 + "could not find expected ':'\n"
212 + " in 'string', line 6, column 1:\n"
213 + " description: vCPE_vgw\n"
216 final ToscaDefinitionImportHandler toscaDefinitionImportHandler = new ToscaDefinitionImportHandler(
219 final Set<String> actualHandledFiles = toscaDefinitionImportHandler.getHandledDefinitionFilesList();
221 assertThat("No files should be handled", actualHandledFiles, hasSize(0));
223 final List<ErrorMessage> validationErrorList = toscaDefinitionImportHandler.getErrors();
225 assertThat("The errors should be the same", validationErrorList, hasSize(expectedErrorList.size()));
226 assertThat("The errors should be the same"
227 , validationErrorList, containsInAnyOrder(expectedErrorList.toArray(new ErrorMessage[0]))
232 * Tests all forms of import statements.
235 public void testGivenDescriptorFiles_whenMainDescriptorWithDifferentImportStatementsIsHandled_noErrorsAreReported() {
236 final String mainDefinitionFile = "Definitions/descriptorFileWithValidImportStatements.yaml";
238 final List<String> filesToHandleList = Arrays.asList(mainDefinitionFile, "Artifacts/descriptorCyclicReference.yaml");
239 filesToHandleList.forEach(file -> descriptorFileMap.put(file, getResourceBytesOrFail(RESOURCES_FILE_PATH + file)));
241 final ToscaDefinitionImportHandler toscaDefinitionImportHandler =
242 new ToscaDefinitionImportHandler(descriptorFileMap, mainDefinitionFile);
243 final Set<String> actualHandledFiles = toscaDefinitionImportHandler.getHandledDefinitionFilesList();
245 assertThat("The handled files should be the same", actualHandledFiles, hasSize(filesToHandleList.size()));
246 assertThat("The handled files should be the same"
247 , actualHandledFiles, containsInAnyOrder(filesToHandleList.toArray(new String[0]))
250 final List<ErrorMessage> validationErrorList = toscaDefinitionImportHandler.getErrors();
251 assertThat("No errors should be detected", validationErrorList, hasSize(0));
255 * Tests a descriptor with nonexistent import paths.
258 public void testGivenDescriptorFileWithNonexistentRelativeImport_whenIncorrectMainDescriptorIsHandled_aMissingFileErrorIsReported() {
259 final String mainDefinitionFile = "Definitions/MainWithNonexistentReferences.yaml";
261 final List<String> filesToHandleList = Arrays.asList(mainDefinitionFile, "Definitions/descriptorNonexistentImport.yaml",
262 "Artifacts/descriptorCyclicReference.yaml");
263 filesToHandleList.forEach(file -> descriptorFileMap.put(file, getResourceBytesOrFail(RESOURCES_FILE_PATH + file)));
265 final List<ErrorMessage> expectedErrorList = new ArrayList<>();
266 expectedErrorList.add(new ErrorMessage(ErrorLevel.ERROR,
267 String.format(Messages.MISSING_IMPORT_FILE.getErrorMessage(), "Definitions/descriptorCyclicReference.yaml")));
268 expectedErrorList.add(new ErrorMessage(ErrorLevel.ERROR,
269 String.format(Messages.MISSING_IMPORT_FILE.getErrorMessage(), "Definitions/descriptorCyclicReference.yaml")));
270 expectedErrorList.add(new ErrorMessage(ErrorLevel.ERROR,
271 String.format(Messages.MISSING_IMPORT_FILE.getErrorMessage(), "Definitions/descriptorCyclicReference.yaml")));
273 final ToscaDefinitionImportHandler toscaDefinitionImportHandler = new ToscaDefinitionImportHandler(
276 final Set<String> actualHandledFiles = toscaDefinitionImportHandler.getHandledDefinitionFilesList();
278 assertThat("The handled files should be the same", actualHandledFiles, hasSize(filesToHandleList.size()));
279 assertThat("The handled files should be the same"
280 , actualHandledFiles, containsInAnyOrder(filesToHandleList.toArray(new String[0]))
283 final List<ErrorMessage> validationErrorList = toscaDefinitionImportHandler.getErrors();
285 assertThat("The errors should be the same", validationErrorList, hasSize(expectedErrorList.size()));
286 assertThat("The errors should be the same"
287 , validationErrorList, containsInAnyOrder(expectedErrorList.toArray(new ErrorMessage[0]))