fe0d5a86209f173362d69f99044e9a3367530558
[sdc.git] /
1 /*
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
8  *
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.
15  *
16  *  SPDX-License-Identifier: Apache-2.0
17  *  ============LICENSE_END=========================================================
18  */
19
20 package org.openecomp.core.impl;
21
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;
26
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;
34 import java.util.Map;
35 import java.util.Set;
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;
41
42 public class ToscaDefinitionImportHandlerTest {
43
44     private static final Path RESOURCES_FILE_PATH = Paths.get("toscaDefinitionImportHandler");
45     private Map<String, byte[]> descriptorFileMap;
46
47     @Before
48     public void setUp() {
49         descriptorFileMap = new HashMap<>();
50     }
51
52     /**
53      * Tests correct descriptor files.
54      */
55     @Test
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");
60
61         filesToHandleList.forEach(file ->
62             descriptorFileMap.put(file, getResourceBytesOrFail(RESOURCES_FILE_PATH.resolve(file)))
63         );
64
65         final ToscaDefinitionImportHandler toscaDefinitionImportHandler = new ToscaDefinitionImportHandler(
66             descriptorFileMap,
67             "Definitions/Main.yaml");
68         final Set<String> actualHandledFiles = toscaDefinitionImportHandler.getHandledDefinitionFilesList();
69
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]))
73         );
74
75         final List<ErrorMessage> validationErrorList = toscaDefinitionImportHandler.getErrors();
76         assertThat("No errors should be detected", validationErrorList, hasSize(0));
77     }
78
79     /**
80      * Tests an empty package.
81      */
82     @Test
83     public void testGivenEmptyPackage_whenMainDescriptorIsHandled_aMissingFileErrorIsReported() {
84         final List<String> filesToHandleList = Collections.emptyList();
85
86         final ToscaDefinitionImportHandler toscaDefinitionImportHandler = new ToscaDefinitionImportHandler(
87             descriptorFileMap,
88             "Definitions/Main.yaml");
89         final Set<String> actualHandledFiles = toscaDefinitionImportHandler.getHandledDefinitionFilesList();
90
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]))
94         );
95
96         final List<ErrorMessage> expectedErrorList = new ArrayList<>();
97         expectedErrorList.add(new ErrorMessage(ErrorLevel.ERROR,
98             Messages.MISSING_IMPORT_FILE.formatMessage("Definitions/Main.yaml")));
99
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]))
104         );
105     }
106
107     /**
108      * Tests a file imported in a descriptor but missing in the package.
109      */
110     @Test
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)))
117         );
118
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")));
122
123         final ToscaDefinitionImportHandler toscaDefinitionImportHandler = new ToscaDefinitionImportHandler(
124             descriptorFileMap,
125             "Definitions/Main.yaml");
126         final Set<String> actualHandledFiles = toscaDefinitionImportHandler.getHandledDefinitionFilesList();
127
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]))
131         );
132
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]))
137         );
138     }
139
140     /**
141      * Tests a descriptor with invalid import statements.
142      */
143     @Test
144     public void testGivenDescriptorWithInvalidImportStatement_whenMainDescriptorImportsAreHandled_aInvalidImportStatementErrorIsReported() {
145         final String mainDefinitionFile = "Definitions/MainWithInvalidImportedFile.yaml";
146
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)))
151         );
152
153         final List<ErrorMessage> expectedErrorList = new ArrayList<>();
154         expectedErrorList.add(new ErrorMessage(ErrorLevel.ERROR,
155             Messages.INVALID_IMPORT_STATEMENT.formatMessage("Definitions/descriptorInvalidImportStatement.yaml", "null")));
156
157         final ToscaDefinitionImportHandler toscaDefinitionImportHandler = new ToscaDefinitionImportHandler(
158             descriptorFileMap,
159             mainDefinitionFile);
160         final Set<String> actualHandledFiles = toscaDefinitionImportHandler.getHandledDefinitionFilesList();
161
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]))
165         );
166
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]))
171         );
172     }
173
174     /**
175      * Tests an invalid main descriptor file path.
176      */
177     @Test
178     public void testGivenInvalidMainDescriptorFilePath_whenDescriptorIsHandled_aMissingImportErrorIsReported() {
179         final String mainDefinitionFilePath = "Definitions/Main1.yaml";
180         final String invalidMainDefinitionFilePath = "../Definitions/InvalidMainDefinitionFile.yaml";
181
182         final List<String> filesToHandleList = Arrays.asList(mainDefinitionFilePath);
183         filesToHandleList.forEach(file ->
184             descriptorFileMap.put(file, getResourceBytesOrFail(RESOURCES_FILE_PATH.resolve(file)))
185         );
186
187         final List<ErrorMessage> expectedErrorList = new ArrayList<>();
188         expectedErrorList.add(new ErrorMessage(ErrorLevel.ERROR, Messages.MISSING_IMPORT_FILE.formatMessage(invalidMainDefinitionFilePath)));
189
190         final ToscaDefinitionImportHandler toscaDefinitionImportHandler = new ToscaDefinitionImportHandler(
191             descriptorFileMap,
192             invalidMainDefinitionFilePath);
193         final Set<String> actualHandledFiles = toscaDefinitionImportHandler.getHandledDefinitionFilesList();
194
195         assertThat("No files should be handled", actualHandledFiles, hasSize(0));
196
197         final List<ErrorMessage> validationErrorList = toscaDefinitionImportHandler.getErrors();
198
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]))
202         );
203     }
204
205     /**
206      * Tests a descriptor with invalid yaml.
207      */
208     @Test
209     public void testGivenInvalidYamlDescriptorFile_whenDescriptorIsHandled_aInvalidYamlFormatErrorIsReported() {
210         final String mainDefinitionFile = "Definitions/descriptorInvalid.yaml";
211
212         final List<String> filesToHandleList = Arrays.asList(mainDefinitionFile);
213         filesToHandleList.forEach(file ->
214             descriptorFileMap.put(file, getResourceBytesOrFail(RESOURCES_FILE_PATH.resolve(file)))
215         );
216
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"
222                 + "      ^\n"
223                 + "could not find expected ':'\n"
224                 + " in 'string', line 6, column 1:\n"
225                 + "    description: vCPE_vgw\n"
226                 + "    ^\n")));
227
228         final ToscaDefinitionImportHandler toscaDefinitionImportHandler = new ToscaDefinitionImportHandler(
229             descriptorFileMap,
230             mainDefinitionFile);
231         final Set<String> actualHandledFiles = toscaDefinitionImportHandler.getHandledDefinitionFilesList();
232
233         assertThat("No files should be handled", actualHandledFiles, hasSize(0));
234
235         final List<ErrorMessage> validationErrorList = toscaDefinitionImportHandler.getErrors();
236
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]))
240         );
241     }
242
243     /**
244      * Tests all forms of import statements.
245      */
246     @Test
247     public void testGivenDescriptorFiles_whenMainDescriptorWithDifferentImportStatementsIsHandled_noErrorsAreReported() {
248         final String mainDefinitionFile = "Definitions/descriptorFileWithValidImportStatements.yaml";
249
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)))
253         );
254
255         final ToscaDefinitionImportHandler toscaDefinitionImportHandler =
256             new ToscaDefinitionImportHandler(descriptorFileMap, mainDefinitionFile);
257         final Set<String> actualHandledFiles = toscaDefinitionImportHandler.getHandledDefinitionFilesList();
258
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]))
262         );
263
264         final List<ErrorMessage> validationErrorList = toscaDefinitionImportHandler.getErrors();
265         assertThat("No errors should be detected", validationErrorList, hasSize(0));
266     }
267
268     /**
269      * Tests a descriptor with nonexistent import paths.
270      */
271     @Test
272     public void testGivenDescriptorFileWithNonexistentRelativeImport_whenIncorrectMainDescriptorIsHandled_aMissingFileErrorIsReported() {
273         final String mainDefinitionFile = "Definitions/MainWithNonexistentReferences.yaml";
274
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)))
279         );
280
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"))
285         );
286         expectedErrorList.add(new ErrorMessage(ErrorLevel.ERROR,
287             String.format(Messages.MISSING_IMPORT_FILE.getErrorMessage(),
288                 "Definitions/descriptorCyclicReference.yaml"))
289         );
290         expectedErrorList.add(new ErrorMessage(ErrorLevel.ERROR,
291             String.format(Messages.MISSING_IMPORT_FILE.getErrorMessage(),
292                 "Definitions/descriptorCyclicReference.yaml"))
293         );
294
295         final ToscaDefinitionImportHandler toscaDefinitionImportHandler = new ToscaDefinitionImportHandler(
296             descriptorFileMap,
297             mainDefinitionFile);
298         final Set<String> actualHandledFiles = toscaDefinitionImportHandler.getHandledDefinitionFilesList();
299
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]))
303         );
304
305         final List<ErrorMessage> validationErrorList = toscaDefinitionImportHandler.getErrors();
306
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]))
310         );
311     }
312
313 }