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