Remove sdc-tosca-parser dependency
[sdc.git] / openecomp-be / lib / openecomp-sdc-vendor-software-product-lib / openecomp-sdc-vendor-software-product-core / src / test / java / org / openecomp / sdc / vendorsoftwareproduct / services / impl / etsi / ETSIServiceImplTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2019 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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.openecomp.sdc.vendorsoftwareproduct.services.impl.etsi;
22
23 import static org.hamcrest.CoreMatchers.containsString;
24 import static org.hamcrest.CoreMatchers.hasItem;
25 import static org.hamcrest.CoreMatchers.not;
26 import static org.hamcrest.core.Is.is;
27 import static org.junit.Assert.assertFalse;
28 import static org.junit.Assert.assertThat;
29 import static org.junit.Assert.assertTrue;
30 import static org.mockito.Mockito.doReturn;
31 import static org.mockito.Mockito.mock;
32 import static org.mockito.Mockito.when;
33
34 import java.io.File;
35 import java.io.IOException;
36 import java.io.InputStream;
37 import java.nio.charset.StandardCharsets;
38 import java.nio.file.Files;
39 import java.nio.file.Path;
40 import java.nio.file.Paths;
41 import java.util.ArrayList;
42 import java.util.Arrays;
43 import java.util.Collections;
44 import java.util.HashMap;
45 import java.util.List;
46 import java.util.Map;
47 import java.util.Optional;
48 import org.junit.After;
49 import org.junit.Before;
50 import org.junit.Test;
51 import org.mockito.Mockito;
52 import org.onap.sdc.tosca.datatypes.model.ServiceTemplate;
53 import org.onap.sdc.tosca.services.YamlUtil;
54 import org.openecomp.core.utilities.file.FileContentHandler;
55 import org.openecomp.sdc.be.config.NonManoConfiguration;
56 import org.openecomp.sdc.tosca.csar.Manifest;
57 import org.openecomp.sdc.tosca.datatypes.ToscaServiceModel;
58 import org.yaml.snakeyaml.Yaml;
59
60 public class ETSIServiceImplTest {
61
62     private ETSIService etsiService;
63     private String sol004MetaFile = "TOSCA-Meta-Version: 1.0\n" +
64         "CSAR-Version: 1.0\n" +
65         "Created-By: Kuku\n" +
66         "Entry-Definitions: MainServiceTemplate.yaml\n" +
67         "ETSI-Entry-Manifest: MainServiceTemplate.mf\n" +
68         "ETSI-Entry-Change-Log: MainServiceTemplate.log";
69     private String metaFile = "TOSCA-Meta-Version: 1.0\n" +
70         "CSAR-Version: 1.0\n" +
71         "Created-By: Kuku\n" +
72         "Entry-Definitions: MainServiceTemplate.yaml";
73
74     private String finalNonManoLocation = "Deployment/VES_EVENTS/test.xml";
75
76     @Before
77     public void setUp() throws IOException {
78         final String fullFileName = Paths.get("src", "test", "resources", "nonManoConfig.yaml").toString();
79         final NonManoConfiguration configuration = convert(fullFileName, NonManoConfiguration.class);
80         etsiService = Mockito.spy(new ETSIServiceImpl(configuration));
81     }
82
83     @After
84     public void tearDown() {
85         etsiService = null;
86     }
87
88     @Test
89     public void testIsSol004TrueOrigin() throws IOException {
90         FileContentHandler fileContentHandler = new FileContentHandler();
91         fileContentHandler
92             .addFile("TOSCA-Metadata/TOSCA.meta.original", sol004MetaFile.getBytes(StandardCharsets.UTF_8));
93         assertTrue(etsiService.isSol004WithToscaMetaDirectory(fileContentHandler));
94     }
95
96     @Test
97     public void testIsSol004True() throws IOException {
98         FileContentHandler fileContentHandler = new FileContentHandler();
99         fileContentHandler.addFile("TOSCA-Metadata/TOSCA.meta", sol004MetaFile.getBytes(StandardCharsets.UTF_8));
100         assertTrue(etsiService.isSol004WithToscaMetaDirectory(fileContentHandler));
101     }
102
103     @Test
104     public void testIsSol004False() throws IOException {
105         FileContentHandler fileContentHandler = new FileContentHandler();
106         fileContentHandler.addFile("TOSCA-Metadata/TOSCA.meta.original", metaFile.getBytes(StandardCharsets.UTF_8));
107         assertFalse(etsiService.isSol004WithToscaMetaDirectory(fileContentHandler));
108     }
109
110     @Test
111     public void testIsSol004FalseWithNull() throws IOException {
112         FileContentHandler fileContentHandler = new FileContentHandler();
113         assertFalse(etsiService.isSol004WithToscaMetaDirectory(fileContentHandler));
114     }
115
116     @Test
117     public void testMoveNonManoFileToArtifactFolder() throws IOException {
118         final Map<String, List<String>> nonManoTypeAndSourceMapInManifest = new HashMap<>();
119         nonManoTypeAndSourceMapInManifest.put("Some", Collections.singletonList("Some/test.xml"));
120         final Manifest manifest = mock(Manifest.class);
121         when(manifest.getNonManoSources()).thenReturn(nonManoTypeAndSourceMapInManifest);
122         final FileContentHandler fileContentHandler = new FileContentHandler();
123         fileContentHandler.addFile("Some/test.xml", new byte[1]);
124         fileContentHandler.addFile("TOSCA-Metadata/TOSCA.meta.original", new byte[1]);
125         fileContentHandler.addFile("MainServiceTemplate.mf", new byte[1]);
126         doReturn(manifest).when(etsiService).getManifest(fileContentHandler);
127         doReturn(Paths.get("")).when(etsiService).getOriginalManifestPath(fileContentHandler);
128         etsiService.moveNonManoFileToArtifactFolder(fileContentHandler);
129         assertThat("Should contain moved file", fileContentHandler.getFileList(), hasItem(finalNonManoLocation));
130     }
131
132
133     @Test
134     public void testMoveNonManoFileInArtifactFolderToNonManoOnapPath() throws IOException {
135         final Map<String, List<String>> nonManoTypeAndSourceMapInManifest = new HashMap<>();
136         nonManoTypeAndSourceMapInManifest.put("Some", Collections.singletonList("Artifacts/Some/test.xml"));
137         final FileContentHandler fileContentHandler = new FileContentHandler();
138         fileContentHandler.addFile("Some/test.xml", new byte[1]);
139         final Manifest manifest = mock(Manifest.class);
140         doReturn(manifest).when(etsiService).getManifest(fileContentHandler);
141         doReturn(Paths.get("")).when(etsiService).getOriginalManifestPath(fileContentHandler);
142         when(manifest.getNonManoSources()).thenReturn(nonManoTypeAndSourceMapInManifest);
143         etsiService.moveNonManoFileToArtifactFolder(fileContentHandler);
144         assertThat("Should contain moved file", fileContentHandler.getFileList(), hasItem(finalNonManoLocation));
145     }
146
147     @Test
148     public void testMoveNonManoFileToArtifactFolderFileNotInFolder() throws IOException {
149         Map<String, List<String>> nonManoSources = new HashMap<>();
150         List<String> sources = new ArrayList<>();
151         sources.add("test.xml");
152         nonManoSources.put("foo", sources);
153         FileContentHandler fileContentHandler = new FileContentHandler();
154         fileContentHandler.addFile("test.xml", new byte[1]);
155         Manifest manifest = mock(Manifest.class);
156         doReturn(manifest).when(etsiService).getManifest(fileContentHandler);
157         doReturn(Paths.get("")).when(etsiService).getOriginalManifestPath(fileContentHandler);
158         when(manifest.getNonManoSources()).thenReturn(nonManoSources);
159         etsiService.moveNonManoFileToArtifactFolder(fileContentHandler);
160         String finalOtherNonManoLocation = "Informational/OTHER/test.xml";
161         assertFalse(fileContentHandler.containsFile(finalOtherNonManoLocation));
162     }
163
164     @Test
165     public void testMoveNonManoFileToArtifactFolderNoMove() throws IOException {
166         Map<String, List<String>> nonManoSources = new HashMap<>();
167         List<String> sources = new ArrayList<>();
168         sources.add(finalNonManoLocation);
169         nonManoSources.put("Some", sources);
170         FileContentHandler fileContentHandler = new FileContentHandler();
171         fileContentHandler.addFile(finalNonManoLocation, new byte[1]);
172         Manifest manifest = mock(Manifest.class);
173         doReturn(manifest).when(etsiService).getManifest(fileContentHandler);
174         doReturn(Paths.get("")).when(etsiService).getOriginalManifestPath(fileContentHandler);
175         when(manifest.getNonManoSources()).thenReturn(nonManoSources);
176         etsiService.moveNonManoFileToArtifactFolder(fileContentHandler);
177         assertTrue(fileContentHandler.containsFile(finalNonManoLocation));
178     }
179
180     @Test
181     public void testMoveNonManoFileToArtifactFolderMoveToKeyFolder() throws IOException {
182         Map<String, List<String>> nonManoSources = new HashMap<>();
183         List<String> sources = new ArrayList<>();
184         sources.add("Artifacts/Deployment/test.xml");
185         nonManoSources.put("Some", sources);
186         FileContentHandler fileContentHandler = new FileContentHandler();
187         fileContentHandler.addFile("Deployment/test.xml", new byte[1]);
188         Manifest manifest = mock(Manifest.class);
189         doReturn(manifest).when(etsiService).getManifest(fileContentHandler);
190         doReturn(Paths.get("")).when(etsiService).getOriginalManifestPath(fileContentHandler);
191         when(manifest.getNonManoSources()).thenReturn(nonManoSources);
192         etsiService.moveNonManoFileToArtifactFolder(fileContentHandler);
193         assertTrue(fileContentHandler.containsFile(finalNonManoLocation));
194     }
195
196     @Test
197     public void givenManifestNotInRoot_moveNonManoFileToNonManoOnapFolder() throws IOException {
198         //given manifest non mano files under key "onap_other", inside and outside Artifacts folder,
199         // with relative and absolute paths.
200         final Map<String, List<String>> nonManoTypeAndSourceMapInManifest = new HashMap<>();
201         nonManoTypeAndSourceMapInManifest.put("onap_other",
202             Arrays.asList("../../Artifacts/Artifacts/Deployment/relativePathInsideSubArtifact.xml",
203                 "../../Files/scriptInFilesPath.sh",
204                 "/Artifacts/Deployment/absolutePathInsideArtifact.xml"));
205         //given ONAP package fileHandler
206         final FileContentHandler fileContentHandler = new FileContentHandler();
207         fileContentHandler.addFile("Artifacts/Deployment/relativePathInsideSubArtifact.xml", new byte[1]);
208         fileContentHandler.addFile("Deployment/absolutePathInsideArtifact.xml", new byte[1]);
209         fileContentHandler.addFile("Files/scriptInFilesPath.sh", new byte[1]);
210         //given onboarded manifest in two/lvlFolder folder
211         final Manifest manifest = mock(Manifest.class);
212         doReturn(manifest).when(etsiService).getManifest(fileContentHandler);
213         doReturn(Paths.get("two/lvlFolder")).when(etsiService).getOriginalManifestPath(fileContentHandler);
214         when(manifest.getNonManoSources()).thenReturn(nonManoTypeAndSourceMapInManifest);
215         //when files are non mano moved
216         etsiService.moveNonManoFileToArtifactFolder(fileContentHandler);
217         assertThat("Should contain moved file", fileContentHandler.getFileList(),
218             hasItem("Deployment/OTHER/relativePathInsideSubArtifact.xml"));
219         assertThat("Should contain moved file", fileContentHandler.getFileList(),
220             hasItem("Deployment/OTHER/absolutePathInsideArtifact.xml"));
221         assertThat("Should contain moved file", fileContentHandler.getFileList(),
222             hasItem("Deployment/OTHER/scriptInFilesPath.sh"));
223     }
224
225     @Test
226     public void givenManifestInRoot_moveNonManoFileToNonManoOnapFolder() throws IOException {
227         //given manifest non mano files under key "onap_other", inside and outside Artifacts folder
228         final Map<String, List<String>> nonManoSourceMap = new HashMap<>();
229         nonManoSourceMap.put("onap_other",
230             Arrays.asList("Artifacts/Deployment/ANOTHER/authorized_keys",
231                 "Files/scriptInFilesPath.sh")
232         );
233         //given manifest non mano file under key "Some"
234         nonManoSourceMap.put("Some",
235             Collections.singletonList("Files/willMoveToSome.sh")
236         );
237         //given ONAP package fileHandler
238         final FileContentHandler fileContentHandler = new FileContentHandler();
239         fileContentHandler.addFile("Deployment/ANOTHER/authorized_keys", new byte[1]);
240         fileContentHandler.addFile("Files/scriptInFilesPath.sh", new byte[1]);
241         fileContentHandler.addFile("Files/willMoveToSome.sh", new byte[1]);
242         fileContentHandler.addFile("Deployment/willNotMove.xml", new byte[1]);
243         //given onboarded manifest in root folder
244         final Manifest manifest = mock(Manifest.class);
245         when(manifest.getNonManoSources()).thenReturn(nonManoSourceMap);
246         doReturn(manifest).when(etsiService).getManifest(fileContentHandler);
247         doReturn(Paths.get("")).when(etsiService).getOriginalManifestPath(fileContentHandler);
248         final Optional<Map<String, Path>> fromToPathMap = etsiService
249             .moveNonManoFileToArtifactFolder(fileContentHandler);
250         assertThat("Files should be moved", fromToPathMap.isPresent(), is(true));
251         assertThat("Should contain moved file", fileContentHandler.getFileList(),
252             hasItem("Deployment/OTHER/authorized_keys"));
253         assertThat("Should contain moved file", fileContentHandler.getFileList(),
254             hasItem("Deployment/OTHER/scriptInFilesPath.sh"));
255         assertThat("Should contain moved file", fileContentHandler.getFileList(),
256             hasItem("Deployment/VES_EVENTS/willMoveToSome.sh"));
257         assertThat("Should contain not moved file", fileContentHandler.getFileList(),
258             hasItem("Deployment/willNotMove.xml"));
259     }
260
261     @Test
262     public void givenMovedFiles_updateDescriptorReferences() {
263         //given moved files
264         final Map<String, Path> fromToPathMap = new HashMap<>();
265         final String file1OriginalPath = "Artifacts/Deployment/ANOTHER/authorized_keys";
266         final Path file1Path = Paths.get("Artifacts", "Deployment", "OTHER", "authorized_keys");
267         fromToPathMap.put(file1OriginalPath, file1Path);
268         final String file2OriginalPath = "Artifacts/Deployment/ANOTHER/image";
269         final Path file2Path = Paths.get("Artifacts", "Deployment", "OTHER", "image");
270         fromToPathMap.put(file2OriginalPath, file2Path);
271         //given main descriptor
272         final InputStream mainServiceTemplateYamlFile = getClass().getClassLoader()
273             .getResourceAsStream("vnfPackage/vnf1/Definitions/MainServiceTemplate.yaml");
274         final ServiceTemplate mainServiceTemplate = new YamlUtil()
275             .yamlToObject(mainServiceTemplateYamlFile, ServiceTemplate.class);
276         final HashMap<String, ServiceTemplate> serviceTemplateMap = new HashMap<>();
277         serviceTemplateMap.put("MainServiceTemplate.yaml", mainServiceTemplate);
278         final ToscaServiceModel toscaServiceModel = new ToscaServiceModel(null, serviceTemplateMap,
279             "MainServiceTemplate.yaml");
280         //when descriptor is updated
281         etsiService.updateMainDescriptorPaths(toscaServiceModel, fromToPathMap);
282         //then
283         final String serviceTemplatesAsYaml = new YamlUtil().objectToYaml(toscaServiceModel.getServiceTemplates());
284         assertThat("Descriptor should not contain reference to file", serviceTemplatesAsYaml,
285             not(containsString(file1OriginalPath)));
286         assertThat("Descriptor should not contain reference to file", serviceTemplatesAsYaml,
287             not(containsString(file2OriginalPath)));
288         assertThat("Descriptor should contain reference to file", serviceTemplatesAsYaml,
289             containsString(file1Path.toString()));
290         assertThat("Descriptor should contain reference to file", serviceTemplatesAsYaml,
291             containsString(file2Path.toString()));
292     }
293
294     private <T> T convert(final String fullFileName, final Class<T> className) throws IOException {
295         assertTrue((new File(fullFileName)).exists());
296
297         try (final InputStream in = Files.newInputStream(Paths.get(fullFileName));) {
298             return (new Yaml()).loadAs(in, className);
299         } catch (final IOException e) {
300             throw new IOException(e);
301         }
302     }
303
304 }