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.sdc.be.test.util.TestResourcesHandler.getResourceBytesOrFail;
27 import java.nio.file.Path;
28 import java.nio.file.Paths;
29 import java.util.ArrayList;
30 import java.util.Arrays;
31 import java.util.Collections;
32 import java.util.HashMap;
33 import java.util.List;
36 import org.junit.Before;
37 import org.junit.Test;
38 import org.openecomp.sdc.common.errors.Messages;
39 import org.openecomp.sdc.datatypes.error.ErrorLevel;
40 import org.openecomp.sdc.datatypes.error.ErrorMessage;
42 public class ToscaDefinitionImportHandlerTest {
44 private static final Path RESOURCES_FILE_PATH = Paths.get("toscaDefinitionImportHandler");
45 private Map<String, byte[]> descriptorFileMap;
49 descriptorFileMap = new HashMap<>();
53 * Tests correct descriptor files.
56 public void testGivenDescriptorFiles_whenMainDescriptorImportsAreHandled_allDescriptorsAreProcessedWithoutError() {
57 final List<String> filesToHandleList = Arrays.asList("Definitions/Main.yaml", "Definitions/descriptorBasicImport.yaml",
58 "Definitions/descriptorWithRelativePaths.yaml", "Artifacts/descriptorWithAbsolutePaths.yaml",
59 "Artifacts/descriptorCyclicReference.yaml");
61 filesToHandleList.forEach(file ->
62 descriptorFileMap.put(file, getResourceBytesOrFail(RESOURCES_FILE_PATH.resolve(file)))
65 final ToscaDefinitionImportHandler toscaDefinitionImportHandler = new ToscaDefinitionImportHandler(
67 "Definitions/Main.yaml");
68 final Set<String> actualHandledFiles = toscaDefinitionImportHandler.getHandledDefinitionFilesList();
70 assertThat("The handled files should be the same", actualHandledFiles, hasSize(filesToHandleList.size()));
71 assertThat("The handled files should be the same"
72 , actualHandledFiles, containsInAnyOrder(filesToHandleList.toArray(new String[0]))
75 final List<ErrorMessage> validationErrorList = toscaDefinitionImportHandler.getErrors();
76 assertThat("No errors should be detected", validationErrorList, hasSize(0));
80 * Tests an empty package.
83 public void testGivenEmptyPackage_whenMainDescriptorIsHandled_aMissingFileErrorIsReported() {
84 final List<String> filesToHandleList = Collections.emptyList();
86 final ToscaDefinitionImportHandler toscaDefinitionImportHandler = new ToscaDefinitionImportHandler(
88 "Definitions/Main.yaml");
89 final Set<String> actualHandledFiles = toscaDefinitionImportHandler.getHandledDefinitionFilesList();
91 assertThat("The handled files should be the same", actualHandledFiles, hasSize(filesToHandleList.size()));
92 assertThat("The handled files should be the same"
93 , actualHandledFiles, containsInAnyOrder(filesToHandleList.toArray(new String[0]))
96 final List<ErrorMessage> expectedErrorList = new ArrayList<>();
97 expectedErrorList.add(new ErrorMessage(ErrorLevel.ERROR,
98 Messages.MISSING_IMPORT_FILE.formatMessage("Definitions/Main.yaml")));
100 final List<ErrorMessage> validationErrorList = toscaDefinitionImportHandler.getErrors();
101 assertThat("The errors should be the same", validationErrorList, hasSize(expectedErrorList.size()));
102 assertThat("The errors should be the same"
103 , validationErrorList, containsInAnyOrder(expectedErrorList.toArray(new ErrorMessage[0]))
108 * Tests a file imported in a descriptor but missing in the package.
111 public void testGivenOneMissingDescriptorFile_whenMainDescriptorImportsAreHandled_aMissingFileErrorIsReported() {
112 final List<String> filesToHandleList = Arrays.asList("Definitions/Main.yaml",
113 "Definitions/descriptorBasicImport.yaml", "Definitions/descriptorWithRelativePaths.yaml",
114 "Artifacts/descriptorWithAbsolutePaths.yaml");
115 filesToHandleList.forEach(file ->
116 descriptorFileMap.put(file, getResourceBytesOrFail(RESOURCES_FILE_PATH.resolve(file)))
119 final List<ErrorMessage> expectedErrorList = new ArrayList<>();
120 expectedErrorList.add(new ErrorMessage(ErrorLevel.ERROR,
121 String.format(Messages.MISSING_IMPORT_FILE.getErrorMessage(), "Artifacts/descriptorCyclicReference.yaml")));
123 final ToscaDefinitionImportHandler toscaDefinitionImportHandler = new ToscaDefinitionImportHandler(
125 "Definitions/Main.yaml");
126 final Set<String> actualHandledFiles = toscaDefinitionImportHandler.getHandledDefinitionFilesList();
128 assertThat("The handled files should be the same", actualHandledFiles, hasSize(filesToHandleList.size()));
129 assertThat("The handled files should be the same"
130 , actualHandledFiles, containsInAnyOrder(filesToHandleList.toArray(new String[0]))
133 final List<ErrorMessage> validationErrorList = toscaDefinitionImportHandler.getErrors();
134 assertThat("The errors should be the same", validationErrorList, hasSize(expectedErrorList.size()));
135 assertThat("The errors should be the same"
136 , validationErrorList, containsInAnyOrder(expectedErrorList.toArray(new ErrorMessage[0]))
141 * Tests a descriptor with invalid import statements.
144 public void testGivenDescriptorWithInvalidImportStatement_whenMainDescriptorImportsAreHandled_aInvalidImportStatementErrorIsReported() {
145 final String mainDefinitionFile = "Definitions/MainWithInvalidImportedFile.yaml";
147 final List<String> filesToHandleList = Arrays.asList(mainDefinitionFile,
148 "Definitions/descriptorInvalidImportStatement.yaml");
149 filesToHandleList.forEach(file ->
150 descriptorFileMap.put(file, getResourceBytesOrFail(RESOURCES_FILE_PATH.resolve(file)))
153 final List<ErrorMessage> expectedErrorList = new ArrayList<>();
154 expectedErrorList.add(new ErrorMessage(ErrorLevel.ERROR,
155 Messages.INVALID_IMPORT_STATEMENT.formatMessage("Definitions/descriptorInvalidImportStatement.yaml", "null")));
157 final ToscaDefinitionImportHandler toscaDefinitionImportHandler = new ToscaDefinitionImportHandler(
160 final Set<String> actualHandledFiles = toscaDefinitionImportHandler.getHandledDefinitionFilesList();
162 assertThat("The handled files should be the same", actualHandledFiles, hasSize(filesToHandleList.size()));
163 assertThat("The handled files should be the same"
164 , actualHandledFiles, containsInAnyOrder(filesToHandleList.toArray(new String[0]))
167 final List<ErrorMessage> validationErrorList = toscaDefinitionImportHandler.getErrors();
168 assertThat("The errors should be the same", validationErrorList, hasSize(expectedErrorList.size()));
169 assertThat("The errors should be the same"
170 , validationErrorList, containsInAnyOrder(expectedErrorList.toArray(new ErrorMessage[0]))
175 * Tests an invalid main descriptor file path.
178 public void testGivenInvalidMainDescriptorFilePath_whenDescriptorIsHandled_aMissingImportErrorIsReported() {
179 final String mainDefinitionFilePath = "Definitions/Main1.yaml";
180 final String invalidMainDefinitionFilePath = "../Definitions/InvalidMainDefinitionFile.yaml";
182 final List<String> filesToHandleList = Arrays.asList(mainDefinitionFilePath);
183 filesToHandleList.forEach(file ->
184 descriptorFileMap.put(file, getResourceBytesOrFail(RESOURCES_FILE_PATH.resolve(file)))
187 final List<ErrorMessage> expectedErrorList = new ArrayList<>();
188 expectedErrorList.add(new ErrorMessage(ErrorLevel.ERROR, Messages.MISSING_IMPORT_FILE.formatMessage(invalidMainDefinitionFilePath)));
190 final ToscaDefinitionImportHandler toscaDefinitionImportHandler = new ToscaDefinitionImportHandler(
192 invalidMainDefinitionFilePath);
193 final Set<String> actualHandledFiles = toscaDefinitionImportHandler.getHandledDefinitionFilesList();
195 assertThat("No files should be handled", actualHandledFiles, hasSize(0));
197 final List<ErrorMessage> validationErrorList = toscaDefinitionImportHandler.getErrors();
199 assertThat("The errors should be the same", validationErrorList, hasSize(expectedErrorList.size()));
200 assertThat("The errors should be the same"
201 , validationErrorList, containsInAnyOrder(expectedErrorList.toArray(new ErrorMessage[0]))
206 * Tests a descriptor with invalid yaml.
209 public void testGivenInvalidYamlDescriptorFile_whenDescriptorIsHandled_aInvalidYamlFormatErrorIsReported() {
210 final String mainDefinitionFile = "Definitions/descriptorInvalid.yaml";
212 final List<String> filesToHandleList = Arrays.asList(mainDefinitionFile);
213 filesToHandleList.forEach(file ->
214 descriptorFileMap.put(file, getResourceBytesOrFail(RESOURCES_FILE_PATH.resolve(file)))
217 final List<ErrorMessage> expectedErrorList = new ArrayList<>();
218 expectedErrorList.add(new ErrorMessage(ErrorLevel.ERROR, String.format(Messages.INVALID_YAML_FORMAT.getErrorMessage()
219 , "while scanning a simple key\n"
220 + " in 'string', line 5, column 3:\n"
221 + " template_author= onap\n"
223 + "could not find expected ':'\n"
224 + " in 'string', line 6, column 1:\n"
225 + " description: vCPE_vgw\n"
228 final ToscaDefinitionImportHandler toscaDefinitionImportHandler = new ToscaDefinitionImportHandler(
231 final Set<String> actualHandledFiles = toscaDefinitionImportHandler.getHandledDefinitionFilesList();
233 assertThat("No files should be handled", actualHandledFiles, hasSize(0));
235 final List<ErrorMessage> validationErrorList = toscaDefinitionImportHandler.getErrors();
237 assertThat("The errors should be the same", validationErrorList, hasSize(expectedErrorList.size()));
238 assertThat("The errors should be the same"
239 , validationErrorList, containsInAnyOrder(expectedErrorList.toArray(new ErrorMessage[0]))
244 * Tests all forms of import statements.
247 public void testGivenDescriptorFiles_whenMainDescriptorWithDifferentImportStatementsIsHandled_noErrorsAreReported() {
248 final String mainDefinitionFile = "Definitions/descriptorFileWithValidImportStatements.yaml";
250 final List<String> filesToHandleList = Arrays.asList(mainDefinitionFile, "Artifacts/descriptorCyclicReference.yaml");
251 filesToHandleList.forEach(file ->
252 descriptorFileMap.put(file, getResourceBytesOrFail(RESOURCES_FILE_PATH.resolve(file)))
255 final ToscaDefinitionImportHandler toscaDefinitionImportHandler =
256 new ToscaDefinitionImportHandler(descriptorFileMap, mainDefinitionFile);
257 final Set<String> actualHandledFiles = toscaDefinitionImportHandler.getHandledDefinitionFilesList();
259 assertThat("The handled files should be the same", actualHandledFiles, hasSize(filesToHandleList.size()));
260 assertThat("The handled files should be the same"
261 , actualHandledFiles, containsInAnyOrder(filesToHandleList.toArray(new String[0]))
264 final List<ErrorMessage> validationErrorList = toscaDefinitionImportHandler.getErrors();
265 assertThat("No errors should be detected", validationErrorList, hasSize(0));
269 * Tests a descriptor with nonexistent import paths.
272 public void testGivenDescriptorFileWithNonexistentRelativeImport_whenIncorrectMainDescriptorIsHandled_aMissingFileErrorIsReported() {
273 final String mainDefinitionFile = "Definitions/MainWithNonexistentReferences.yaml";
275 final List<String> filesToHandleList = Arrays.asList(mainDefinitionFile,
276 "Definitions/descriptorNonexistentImport.yaml", "Artifacts/descriptorCyclicReference.yaml");
277 filesToHandleList.forEach(file ->
278 descriptorFileMap.put(file, getResourceBytesOrFail(RESOURCES_FILE_PATH.resolve(file)))
281 final List<ErrorMessage> expectedErrorList = new ArrayList<>();
282 expectedErrorList.add(new ErrorMessage(ErrorLevel.ERROR,
283 String.format(Messages.MISSING_IMPORT_FILE.getErrorMessage(),
284 "Definitions/descriptorCyclicReference.yaml"))
286 expectedErrorList.add(new ErrorMessage(ErrorLevel.ERROR,
287 String.format(Messages.MISSING_IMPORT_FILE.getErrorMessage(),
288 "Definitions/descriptorCyclicReference.yaml"))
290 expectedErrorList.add(new ErrorMessage(ErrorLevel.ERROR,
291 String.format(Messages.MISSING_IMPORT_FILE.getErrorMessage(),
292 "Definitions/descriptorCyclicReference.yaml"))
295 final ToscaDefinitionImportHandler toscaDefinitionImportHandler = new ToscaDefinitionImportHandler(
298 final Set<String> actualHandledFiles = toscaDefinitionImportHandler.getHandledDefinitionFilesList();
300 assertThat("The handled files should be the same", actualHandledFiles, hasSize(filesToHandleList.size()));
301 assertThat("The handled files should be the same"
302 , actualHandledFiles, containsInAnyOrder(filesToHandleList.toArray(new String[0]))
305 final List<ErrorMessage> validationErrorList = toscaDefinitionImportHandler.getErrors();
307 assertThat("The errors should be the same", validationErrorList, hasSize(expectedErrorList.size()));
308 assertThat("The errors should be the same"
309 , validationErrorList, containsInAnyOrder(expectedErrorList.toArray(new ErrorMessage[0]))