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