2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdc.vendorsoftwareproduct.services.impl.etsi;
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;
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;
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.onap.sdc.tosca.services.YamlUtil;
53 import org.openecomp.core.utilities.file.FileContentHandler;
54 import org.openecomp.sdc.tosca.csar.Manifest;
55 import org.openecomp.sdc.tosca.datatypes.ToscaServiceModel;
57 public class ETSIServiceImplTest {
59 private ETSIService etsiService;
60 private String sol004MetaFile = "TOSCA-Meta-Version: 1.0\n" +
61 "CSAR-Version: 1.0\n" +
62 "Created-By: Kuku\n" +
63 "Entry-Definitions: MainServiceTemplate.yaml\n" +
64 "ETSI-Entry-Manifest: MainServiceTemplate.mf\n" +
65 "ETSI-Entry-Change-Log: MainServiceTemplate.log";
66 private String metaFile = "TOSCA-Meta-Version: 1.0\n" +
67 "CSAR-Version: 1.0\n" +
68 "Created-By: Kuku\n" +
69 "Entry-Definitions: MainServiceTemplate.yaml";
71 private String finalNonManoLocation = "Deployment/VES_EVENTS/test.xml";
74 public void setUp() throws IOException {
75 YamlToObjectConverter yamlToObjectConverter = new YamlToObjectConverter();
76 Configuration configuration = yamlToObjectConverter.convert("src/test/resources",
77 Configuration.class, "nonManoConfig.yaml");
78 etsiService = Mockito.spy(new ETSIServiceImpl(configuration));
82 public void tearDown() {
87 public void testIsSol004TrueOrigin() throws IOException {
88 FileContentHandler fileContentHandler = new FileContentHandler();
90 .addFile("TOSCA-Metadata/TOSCA.meta.original", sol004MetaFile.getBytes(StandardCharsets.UTF_8));
91 assertTrue(etsiService.isSol004WithToscaMetaDirectory(fileContentHandler));
95 public void testIsSol004True() throws IOException {
96 FileContentHandler fileContentHandler = new FileContentHandler();
97 fileContentHandler.addFile("TOSCA-Metadata/TOSCA.meta", sol004MetaFile.getBytes(StandardCharsets.UTF_8));
98 assertTrue(etsiService.isSol004WithToscaMetaDirectory(fileContentHandler));
102 public void testIsSol004False() throws IOException {
103 FileContentHandler fileContentHandler = new FileContentHandler();
104 fileContentHandler.addFile("TOSCA-Metadata/TOSCA.meta.original", metaFile.getBytes(StandardCharsets.UTF_8));
105 assertFalse(etsiService.isSol004WithToscaMetaDirectory(fileContentHandler));
109 public void testIsSol004FalseWithNull() throws IOException {
110 FileContentHandler fileContentHandler = new FileContentHandler();
111 assertFalse(etsiService.isSol004WithToscaMetaDirectory(fileContentHandler));
115 public void testMoveNonManoFileToArtifactFolder() throws IOException {
116 final Map<String, List<String>> nonManoTypeAndSourceMapInManifest = new HashMap<>();
117 nonManoTypeAndSourceMapInManifest.put("Some", Collections.singletonList("Some/test.xml"));
118 final Manifest manifest = mock(Manifest.class);
119 when(manifest.getNonManoSources()).thenReturn(nonManoTypeAndSourceMapInManifest);
120 final FileContentHandler fileContentHandler = new FileContentHandler();
121 fileContentHandler.addFile("Some/test.xml", new byte[1]);
122 fileContentHandler.addFile("TOSCA-Metadata/TOSCA.meta.original", new byte[1]);
123 fileContentHandler.addFile("MainServiceTemplate.mf", new byte[1]);
124 doReturn(manifest).when(etsiService).getManifest(fileContentHandler);
125 doReturn(Paths.get("")).when(etsiService).getOriginalManifestPath(fileContentHandler);
126 etsiService.moveNonManoFileToArtifactFolder(fileContentHandler);
127 assertThat("Should contain moved file", fileContentHandler.getFileList(), hasItem(finalNonManoLocation));
132 public void testMoveNonManoFileInArtifactFolderToNonManoOnapPath() throws IOException {
133 final Map<String, List<String>> nonManoTypeAndSourceMapInManifest = new HashMap<>();
134 nonManoTypeAndSourceMapInManifest.put("Some", Collections.singletonList("Artifacts/Some/test.xml"));
135 final FileContentHandler fileContentHandler = new FileContentHandler();
136 fileContentHandler.addFile("Some/test.xml", new byte[1]);
137 final Manifest manifest = mock(Manifest.class);
138 doReturn(manifest).when(etsiService).getManifest(fileContentHandler);
139 doReturn(Paths.get("")).when(etsiService).getOriginalManifestPath(fileContentHandler);
140 when(manifest.getNonManoSources()).thenReturn(nonManoTypeAndSourceMapInManifest);
141 etsiService.moveNonManoFileToArtifactFolder(fileContentHandler);
142 assertThat("Should contain moved file", fileContentHandler.getFileList(), hasItem(finalNonManoLocation));
146 public void testMoveNonManoFileToArtifactFolderFileNotInFolder() throws IOException {
147 Map<String, List<String>> nonManoSources = new HashMap<>();
148 List<String> sources = new ArrayList<>();
149 sources.add("test.xml");
150 nonManoSources.put("foo", sources);
151 FileContentHandler fileContentHandler = new FileContentHandler();
152 fileContentHandler.addFile("test.xml", new byte[1]);
153 Manifest manifest = mock(Manifest.class);
154 doReturn(manifest).when(etsiService).getManifest(fileContentHandler);
155 doReturn(Paths.get("")).when(etsiService).getOriginalManifestPath(fileContentHandler);
156 when(manifest.getNonManoSources()).thenReturn(nonManoSources);
157 etsiService.moveNonManoFileToArtifactFolder(fileContentHandler);
158 String finalOtherNonManoLocation = "Informational/OTHER/test.xml";
159 assertFalse(fileContentHandler.containsFile(finalOtherNonManoLocation));
163 public void testMoveNonManoFileToArtifactFolderNoMove() throws IOException {
164 Map<String, List<String>> nonManoSources = new HashMap<>();
165 List<String> sources = new ArrayList<>();
166 sources.add(finalNonManoLocation);
167 nonManoSources.put("Some", sources);
168 FileContentHandler fileContentHandler = new FileContentHandler();
169 fileContentHandler.addFile(finalNonManoLocation, new byte[1]);
170 Manifest manifest = mock(Manifest.class);
171 doReturn(manifest).when(etsiService).getManifest(fileContentHandler);
172 doReturn(Paths.get("")).when(etsiService).getOriginalManifestPath(fileContentHandler);
173 when(manifest.getNonManoSources()).thenReturn(nonManoSources);
174 etsiService.moveNonManoFileToArtifactFolder(fileContentHandler);
175 assertTrue(fileContentHandler.containsFile(finalNonManoLocation));
179 public void testMoveNonManoFileToArtifactFolderMoveToKeyFolder() throws IOException {
180 Map<String, List<String>> nonManoSources = new HashMap<>();
181 List<String> sources = new ArrayList<>();
182 sources.add("Artifacts/Deployment/test.xml");
183 nonManoSources.put("Some", sources);
184 FileContentHandler fileContentHandler = new FileContentHandler();
185 fileContentHandler.addFile("Deployment/test.xml", new byte[1]);
186 Manifest manifest = mock(Manifest.class);
187 doReturn(manifest).when(etsiService).getManifest(fileContentHandler);
188 doReturn(Paths.get("")).when(etsiService).getOriginalManifestPath(fileContentHandler);
189 when(manifest.getNonManoSources()).thenReturn(nonManoSources);
190 etsiService.moveNonManoFileToArtifactFolder(fileContentHandler);
191 assertTrue(fileContentHandler.containsFile(finalNonManoLocation));
195 public void givenManifestNotInRoot_moveNonManoFileToNonManoOnapFolder() throws IOException {
196 //given manifest non mano files under key "onap_other", inside and outside Artifacts folder,
197 // with relative and absolute paths.
198 final Map<String, List<String>> nonManoTypeAndSourceMapInManifest = new HashMap<>();
199 nonManoTypeAndSourceMapInManifest.put("onap_other",
200 Arrays.asList("../../Artifacts/Artifacts/Deployment/relativePathInsideSubArtifact.xml",
201 "../../Files/scriptInFilesPath.sh",
202 "/Artifacts/Deployment/absolutePathInsideArtifact.xml"));
203 //given ONAP package fileHandler
204 final FileContentHandler fileContentHandler = new FileContentHandler();
205 fileContentHandler.addFile("Artifacts/Deployment/relativePathInsideSubArtifact.xml", new byte[1]);
206 fileContentHandler.addFile("Deployment/absolutePathInsideArtifact.xml", new byte[1]);
207 fileContentHandler.addFile("Files/scriptInFilesPath.sh", new byte[1]);
208 //given onboarded manifest in two/lvlFolder folder
209 final Manifest manifest = mock(Manifest.class);
210 doReturn(manifest).when(etsiService).getManifest(fileContentHandler);
211 doReturn(Paths.get("two/lvlFolder")).when(etsiService).getOriginalManifestPath(fileContentHandler);
212 when(manifest.getNonManoSources()).thenReturn(nonManoTypeAndSourceMapInManifest);
213 //when files are non mano moved
214 etsiService.moveNonManoFileToArtifactFolder(fileContentHandler);
215 assertThat("Should contain moved file", fileContentHandler.getFileList(),
216 hasItem("Deployment/OTHER/relativePathInsideSubArtifact.xml"));
217 assertThat("Should contain moved file", fileContentHandler.getFileList(),
218 hasItem("Deployment/OTHER/absolutePathInsideArtifact.xml"));
219 assertThat("Should contain moved file", fileContentHandler.getFileList(),
220 hasItem("Deployment/OTHER/scriptInFilesPath.sh"));
224 public void givenManifestInRoot_moveNonManoFileToNonManoOnapFolder() throws IOException {
225 //given manifest non mano files under key "onap_other", inside and outside Artifacts folder
226 final Map<String, List<String>> nonManoSourceMap = new HashMap<>();
227 nonManoSourceMap.put("onap_other",
228 Arrays.asList("Artifacts/Deployment/ANOTHER/authorized_keys",
229 "Files/scriptInFilesPath.sh")
231 //given manifest non mano file under key "Some"
232 nonManoSourceMap.put("Some",
233 Collections.singletonList("Files/willMoveToSome.sh")
235 //given ONAP package fileHandler
236 final FileContentHandler fileContentHandler = new FileContentHandler();
237 fileContentHandler.addFile("Deployment/ANOTHER/authorized_keys", new byte[1]);
238 fileContentHandler.addFile("Files/scriptInFilesPath.sh", new byte[1]);
239 fileContentHandler.addFile("Files/willMoveToSome.sh", new byte[1]);
240 fileContentHandler.addFile("Deployment/willNotMove.xml", new byte[1]);
241 //given onboarded manifest in root folder
242 final Manifest manifest = mock(Manifest.class);
243 when(manifest.getNonManoSources()).thenReturn(nonManoSourceMap);
244 doReturn(manifest).when(etsiService).getManifest(fileContentHandler);
245 doReturn(Paths.get("")).when(etsiService).getOriginalManifestPath(fileContentHandler);
246 final Optional<Map<String, Path>> fromToPathMap = etsiService
247 .moveNonManoFileToArtifactFolder(fileContentHandler);
248 assertThat("Files should be moved", fromToPathMap.isPresent(), is(true));
249 assertThat("Should contain moved file", fileContentHandler.getFileList(),
250 hasItem("Deployment/OTHER/authorized_keys"));
251 assertThat("Should contain moved file", fileContentHandler.getFileList(),
252 hasItem("Deployment/OTHER/scriptInFilesPath.sh"));
253 assertThat("Should contain moved file", fileContentHandler.getFileList(),
254 hasItem("Deployment/VES_EVENTS/willMoveToSome.sh"));
255 assertThat("Should contain not moved file", fileContentHandler.getFileList(),
256 hasItem("Deployment/willNotMove.xml"));
260 public void givenMovedFiles_updateDescriptorReferences() {
262 final Map<String, Path> fromToPathMap = new HashMap<>();
263 final String file1OriginalPath = "Artifacts/Deployment/ANOTHER/authorized_keys";
264 final Path file1Path = Paths.get("Artifacts", "Deployment", "OTHER", "authorized_keys");
265 fromToPathMap.put(file1OriginalPath, file1Path);
266 final String file2OriginalPath = "Artifacts/Deployment/ANOTHER/image";
267 final Path file2Path = Paths.get("Artifacts", "Deployment", "OTHER", "image");
268 fromToPathMap.put(file2OriginalPath, file2Path);
269 //given main descriptor
270 final InputStream mainServiceTemplateYamlFile = getClass().getClassLoader()
271 .getResourceAsStream("vnfPackage/vnf1/Definitions/MainServiceTemplate.yaml");
272 final ServiceTemplate mainServiceTemplate = new YamlUtil()
273 .yamlToObject(mainServiceTemplateYamlFile, ServiceTemplate.class);
274 final HashMap<String, ServiceTemplate> serviceTemplateMap = new HashMap<>();
275 serviceTemplateMap.put("MainServiceTemplate.yaml", mainServiceTemplate);
276 final ToscaServiceModel toscaServiceModel = new ToscaServiceModel(null, serviceTemplateMap,
277 "MainServiceTemplate.yaml");
278 //when descriptor is updated
279 etsiService.updateMainDescriptorPaths(toscaServiceModel, fromToPathMap);
281 final String serviceTemplatesAsYaml = new YamlUtil().objectToYaml(toscaServiceModel.getServiceTemplates());
282 assertThat("Descriptor should not contain reference to file", serviceTemplatesAsYaml,
283 not(containsString(file1OriginalPath)));
284 assertThat("Descriptor should not contain reference to file", serviceTemplatesAsYaml,
285 not(containsString(file2OriginalPath)));
286 assertThat("Descriptor should contain reference to file", serviceTemplatesAsYaml,
287 containsString(file1Path.toString()));
288 assertThat("Descriptor should contain reference to file", serviceTemplatesAsYaml,
289 containsString(file2Path.toString()));