30cba677f2d7325ecb5e0c6c0c0c086f5647b9a0
[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.core.util.TestResourcesUtil.getResourceBytesOrFail;
26
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;
32 import java.util.Map;
33 import java.util.Set;
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;
39
40 public class ToscaDefinitionImportHandlerTest {
41
42     private static final String RESOURCES_FILE_PATH = "/toscaDefinitionImportHandler/";
43     private Map<String, byte[]> descriptorFileMap;
44
45     @Before
46     public void setUp() {
47         descriptorFileMap = new HashMap<>();
48     }
49
50     /**
51      * Tests correct descriptor files.
52      */
53     @Test
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");
58
59         filesToHandleList.forEach(file -> descriptorFileMap.put(file, getResourceBytesOrFail(RESOURCES_FILE_PATH + file)));
60
61         final ToscaDefinitionImportHandler toscaDefinitionImportHandler = new ToscaDefinitionImportHandler(
62             descriptorFileMap,
63             "Definitions/Main.yaml");
64         final Set<String> actualHandledFiles = toscaDefinitionImportHandler.getHandledDefinitionFilesList();
65
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]))
69         );
70
71         final List<ErrorMessage> validationErrorList = toscaDefinitionImportHandler.getErrors();
72         assertThat("No errors should be detected", validationErrorList, hasSize(0));
73     }
74
75     /**
76      * Tests an empty package.
77      */
78     @Test
79     public void testGivenEmptyPackage_whenMainDescriptorIsHandled_aMissingFileErrorIsReported() {
80         final List<String> filesToHandleList = Collections.emptyList();
81
82         final ToscaDefinitionImportHandler toscaDefinitionImportHandler = new ToscaDefinitionImportHandler(
83             descriptorFileMap,
84             "Definitions/Main.yaml");
85         final Set<String> actualHandledFiles = toscaDefinitionImportHandler.getHandledDefinitionFilesList();
86
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]))
90         );
91
92         final List<ErrorMessage> expectedErrorList = new ArrayList<>();
93         expectedErrorList.add(new ErrorMessage(ErrorLevel.ERROR,
94             Messages.MISSING_IMPORT_FILE.formatMessage("Definitions/Main.yaml")));
95
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]))
100         );
101     }
102
103     /**
104      * Tests a file imported in a descriptor but missing in the package.
105      */
106     @Test
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)));
112
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")));
116
117         final ToscaDefinitionImportHandler toscaDefinitionImportHandler = new ToscaDefinitionImportHandler(
118             descriptorFileMap,
119             "Definitions/Main.yaml");
120         final Set<String> actualHandledFiles = toscaDefinitionImportHandler.getHandledDefinitionFilesList();
121
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]))
125         );
126
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]))
131         );
132     }
133
134     /**
135      * Tests a descriptor with invalid import statements.
136      */
137     @Test
138     public void testGivenDescriptorWithInvalidImportStatement_whenMainDescriptorImportsAreHandled_aInvalidImportStatementErrorIsReported() {
139         final String mainDefinitionFile = "Definitions/MainWithInvalidImportedFile.yaml";
140
141         final List<String> filesToHandleList = Arrays.asList(mainDefinitionFile,
142             "Definitions/descriptorInvalidImportStatement.yaml");
143         filesToHandleList.forEach(file -> descriptorFileMap.put(file, getResourceBytesOrFail(RESOURCES_FILE_PATH + file)));
144
145         final List<ErrorMessage> expectedErrorList = new ArrayList<>();
146         expectedErrorList.add(new ErrorMessage(ErrorLevel.ERROR,
147             Messages.INVALID_IMPORT_STATEMENT.formatMessage("Definitions/descriptorInvalidImportStatement.yaml", "null")));
148
149         final ToscaDefinitionImportHandler toscaDefinitionImportHandler = new ToscaDefinitionImportHandler(
150             descriptorFileMap,
151             mainDefinitionFile);
152         final Set<String> actualHandledFiles = toscaDefinitionImportHandler.getHandledDefinitionFilesList();
153
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]))
157         );
158
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]))
163         );
164     }
165
166     /**
167      * Tests an invalid main descriptor file path.
168      */
169     @Test
170     public void testGivenInvalidMainDescriptorFilePath_whenDescriptorIsHandled_aMissingImportErrorIsReported() {
171         final String mainDefinitionFilePath = "Definitions/Main1.yaml";
172         final String invalidMainDefinitionFilePath = "../Definitions/InvalidMainDefinitionFile.yaml";
173
174         final List<String> filesToHandleList = Arrays.asList(mainDefinitionFilePath);
175         filesToHandleList.forEach(file -> descriptorFileMap.put(file, getResourceBytesOrFail(RESOURCES_FILE_PATH + file)));
176
177         final List<ErrorMessage> expectedErrorList = new ArrayList<>();
178         expectedErrorList.add(new ErrorMessage(ErrorLevel.ERROR, Messages.MISSING_IMPORT_FILE.formatMessage(invalidMainDefinitionFilePath)));
179
180         final ToscaDefinitionImportHandler toscaDefinitionImportHandler = new ToscaDefinitionImportHandler(
181             descriptorFileMap,
182             invalidMainDefinitionFilePath);
183         final Set<String> actualHandledFiles = toscaDefinitionImportHandler.getHandledDefinitionFilesList();
184
185         assertThat("No files should be handled", actualHandledFiles, hasSize(0));
186
187         final List<ErrorMessage> validationErrorList = toscaDefinitionImportHandler.getErrors();
188
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]))
192         );
193     }
194
195     /**
196      * Tests a descriptor with invalid yaml.
197      */
198     @Test
199     public void testGivenInvalidYamlDescriptorFile_whenDescriptorIsHandled_aInvalidYamlFormatErrorIsReported() {
200         final String mainDefinitionFile = "Definitions/descriptorInvalid.yaml";
201
202         final List<String> filesToHandleList = Arrays.asList(mainDefinitionFile);
203         filesToHandleList.forEach(file -> descriptorFileMap.put(file, getResourceBytesOrFail(RESOURCES_FILE_PATH + file)));
204
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"
210                 + "      ^\n"
211                 + "could not find expected ':'\n"
212                 + " in 'string', line 6, column 1:\n"
213                 + "    description: vCPE_vgw\n"
214                 + "    ^\n")));
215
216         final ToscaDefinitionImportHandler toscaDefinitionImportHandler = new ToscaDefinitionImportHandler(
217             descriptorFileMap,
218             mainDefinitionFile);
219         final Set<String> actualHandledFiles = toscaDefinitionImportHandler.getHandledDefinitionFilesList();
220
221         assertThat("No files should be handled", actualHandledFiles, hasSize(0));
222
223         final List<ErrorMessage> validationErrorList = toscaDefinitionImportHandler.getErrors();
224
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]))
228         );
229     }
230
231     /**
232      * Tests all forms of import statements.
233      */
234     @Test
235     public void testGivenDescriptorFiles_whenMainDescriptorWithDifferentImportStatementsIsHandled_noErrorsAreReported() {
236         final String mainDefinitionFile = "Definitions/descriptorFileWithValidImportStatements.yaml";
237
238         final List<String> filesToHandleList = Arrays.asList(mainDefinitionFile, "Artifacts/descriptorCyclicReference.yaml");
239         filesToHandleList.forEach(file -> descriptorFileMap.put(file, getResourceBytesOrFail(RESOURCES_FILE_PATH + file)));
240
241         final ToscaDefinitionImportHandler toscaDefinitionImportHandler =
242             new ToscaDefinitionImportHandler(descriptorFileMap, mainDefinitionFile);
243         final Set<String> actualHandledFiles = toscaDefinitionImportHandler.getHandledDefinitionFilesList();
244
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]))
248         );
249
250         final List<ErrorMessage> validationErrorList = toscaDefinitionImportHandler.getErrors();
251         assertThat("No errors should be detected", validationErrorList, hasSize(0));
252     }
253
254     /**
255      * Tests a descriptor with nonexistent import paths.
256      */
257     @Test
258     public void testGivenDescriptorFileWithNonexistentRelativeImport_whenIncorrectMainDescriptorIsHandled_aMissingFileErrorIsReported() {
259         final String mainDefinitionFile = "Definitions/MainWithNonexistentReferences.yaml";
260
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)));
264
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")));
272
273         final ToscaDefinitionImportHandler toscaDefinitionImportHandler = new ToscaDefinitionImportHandler(
274             descriptorFileMap,
275             mainDefinitionFile);
276         final Set<String> actualHandledFiles = toscaDefinitionImportHandler.getHandledDefinitionFilesList();
277
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]))
281         );
282
283         final List<ErrorMessage> validationErrorList = toscaDefinitionImportHandler.getErrors();
284
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]))
288         );
289     }
290
291 }