2 * Copyright © 2016-2018 European Support Limited
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
9 * ================================================================================
10 * Modifications Copyright (c) 2019 Samsung
11 * ================================================================================
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.
20 package org.openecomp.sdc.vendorsoftwareproduct.impl;
22 import java.io.IOException;
23 import org.apache.commons.lang3.tuple.Pair;
24 import org.junit.After;
25 import org.junit.Assert;
26 import org.junit.Before;
27 import org.junit.Test;
28 import org.mockito.InjectMocks;
29 import org.mockito.Mock;
30 import org.mockito.Mockito;
31 import org.mockito.MockitoAnnotations;
32 import org.openecomp.core.utilities.file.FileUtils;
33 import org.openecomp.core.utilities.json.JsonUtil;
34 import org.openecomp.core.utilities.orchestration.OnboardingTypesEnum;
35 import org.openecomp.sdc.vendorsoftwareproduct.dao.ComponentDao;
36 import org.openecomp.sdc.vendorsoftwareproduct.dao.ComponentDependencyModelDao;
37 import org.openecomp.sdc.vendorsoftwareproduct.dao.OrchestrationTemplateDao;
38 import org.openecomp.sdc.vendorsoftwareproduct.dao.VendorSoftwareProductInfoDao;
39 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComponentDependencyModelEntity;
40 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComponentEntity;
41 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.OrchestrationTemplateCandidateData;
42 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.VspDetails;
43 import org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.OrchestrationUtil;
44 import org.openecomp.sdc.vendorsoftwareproduct.services.filedatastructuremodule.CandidateService;
45 import org.openecomp.sdc.vendorsoftwareproduct.types.OrchestrationTemplateActionResponse;
46 import org.openecomp.sdc.vendorsoftwareproduct.types.candidateheat.FilesDataStructure;
47 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.ComponentData;
48 import org.openecomp.sdc.vendorsoftwareproduct.utils.ZipFileUtils;
49 import org.openecomp.sdc.versioning.dao.types.Version;
51 import java.nio.ByteBuffer;
52 import java.util.ArrayList;
53 import java.util.Collection;
54 import java.util.HashMap;
55 import java.util.Iterator;
57 import java.util.Optional;
59 import static org.junit.Assert.assertEquals;
60 import static org.junit.Assert.assertFalse;
61 import static org.junit.Assert.assertNotNull;
62 import static org.mockito.ArgumentMatchers.any;
63 import static org.mockito.ArgumentMatchers.eq;
64 import static org.mockito.Mockito.doReturn;
66 public class OrchestrationTemplateCandidateManagerImplTest {
67 private static final String VSP_ID = "vspId";
68 private static final Version VERSION01 = new Version("versionId");
70 private static final String COMPONENT_ORIG_ID_1 = "Component_Pd_Server_Id_Orig";
71 private static final String COMPONENT_ORIG_ID_2 = "Component_Sm_Server_Id_Orig";
72 private static final String COMPONENT_ORIG_ID_3 = "Component_Oam_Server_Id_Orig";
73 private static final String COMPONENT_ORIG_ID_4 = "Component_Ps_Server_Id_Orig";
75 private static final String COMPONENT_NEW_ID_1 = "Component_Pd_Server_Id_New";
76 private static final String COMPONENT_NEW_ID_2 = "Component_Sm_Server_Id_New";
77 private static final String COMPONENT_NEW_ID_3 = "Component_Oam_Server_Id_New";
78 private static final String COMPONENT_NEW_ID_4 = "Component_Ps_Server_Id_New";
80 private static final String COMPONENT_NAME_1 = "pd_server";
81 private static final String COMPONENT_NAME_2 = "sm_server";
82 private static final String COMPONENT_NAME_3 = "oam_server";
83 private static final String COMPONENT_NAME_4 = "ps_server";
86 private VendorSoftwareProductInfoDao vspInfoDaoMock;
88 private CandidateService candidateServiceMock;
90 private OrchestrationTemplateDao orchestrationTemplateDaoMock;
92 private ComponentDependencyModelDao componentDependencyModelDaoMock;
94 private ComponentDao componentDaoMock;
97 private OrchestrationTemplateCandidateManagerImpl candidateManager;
100 private OrchestrationUtil orchestrationUtil;
103 public void setUp() {
104 MockitoAnnotations.openMocks(this);
108 public void resetMocks() {
109 Mockito.reset(vspInfoDaoMock);
110 Mockito.reset(candidateServiceMock);
111 Mockito.reset(orchestrationTemplateDaoMock);
112 Mockito.reset(componentDependencyModelDaoMock);
113 Mockito.reset(componentDaoMock);
114 candidateManager = null;
115 orchestrationUtil = null;
119 public void testProcessEmptyUpload() {
120 OrchestrationTemplateCandidateData orchTemplate =
121 new OrchestrationTemplateCandidateData();
123 .setContentData(ByteBuffer.wrap(FileUtils.toByteArray(new ZipFileUtils().getZipInputStream
124 ("/vspmanager/zips/emptyComposition.zip"))));
125 orchTemplate.setFilesDataStructure("{\n" +
126 " \"modules\": [\n" +
128 " \"isBase\": false,\n" +
129 " \"yaml\": \"ep-jsa_net.yaml\"\n" +
133 doReturn(Optional.of(orchTemplate))
134 .when(candidateServiceMock).getOrchestrationTemplateCandidate(any(), any());
136 doReturn(new VspDetails(VSP_ID, VERSION01))
137 .when(vspInfoDaoMock).get(any());
139 .when(orchestrationTemplateDaoMock).getInfo(any(), any());
141 doReturn("{}").when(candidateServiceMock).createManifest(any(), any());
142 doReturn(Optional.empty()).when(candidateServiceMock)
143 .fetchZipFileByteArrayInputStream(any(), any(), any(),
144 eq(OnboardingTypesEnum.ZIP), any());
147 OrchestrationTemplateActionResponse response =
148 candidateManager.process(VSP_ID, VERSION01);
150 assertNotNull(response);
154 public void testUpdateVspComponentDependenciesHeatReuploadMoreComponents() {
155 Collection<ComponentDependencyModelEntity> existingComponentsDependencies =
156 getExistingComponentDependencies();
157 Collection<ComponentEntity> componentListWithMoreComponentsInHeat =
158 getComponentListWithMoreComponentsInHeat();
160 doReturn(componentListWithMoreComponentsInHeat).when(componentDaoMock).list(any());
161 Map<String, String> componentIdNameInfoBeforeProcess = getVspInitComponentIdNameInfo();
162 orchestrationUtil.updateVspComponentDependencies(VSP_ID, VERSION01,
163 componentIdNameInfoBeforeProcess, existingComponentsDependencies);
164 Mockito.verify(componentDependencyModelDaoMock, Mockito.times(2)).create(any());
168 public void testUpdateVspComponentDependenciesHeatReuploadLessComponents() {
169 Collection<ComponentDependencyModelEntity> existingComponentsDependencies =
170 getExistingComponentDependencies();
171 Collection<ComponentEntity> componentListWithLessComponentsInHeat =
172 getComponentListWithLessComponentsInHeat();
173 doReturn(componentListWithLessComponentsInHeat).when(componentDaoMock).list(any());
174 Map<String, String> componentIdNameInfoBeforeProcess = getVspInitComponentIdNameInfo();
175 orchestrationUtil.updateVspComponentDependencies(VSP_ID, VERSION01,
176 componentIdNameInfoBeforeProcess, existingComponentsDependencies);
177 Mockito.verify(componentDependencyModelDaoMock, Mockito.times(1)).create(any());
181 public void testUpdateVspComponentDependenciesHeatReuploadSameComponents() {
182 Collection<ComponentDependencyModelEntity> existingComponentsDependencies =
183 getExistingComponentDependencies();
184 Collection<ComponentEntity> componentListWithSameComponentsInHeat =
185 getComponentListWithSameComponentsInHeat();
186 doReturn(componentListWithSameComponentsInHeat).when(componentDaoMock).list(any());
187 Map<String, String> componentIdNameInfoBeforeProcess = getVspInitComponentIdNameInfo();
188 orchestrationUtil.updateVspComponentDependencies(VSP_ID, VERSION01,
189 componentIdNameInfoBeforeProcess, existingComponentsDependencies);
190 Mockito.verify(componentDependencyModelDaoMock, Mockito.times(2)).create(any());
194 public void testUpdateVspComponentDependenciesHeatReuploadNoComponents() {
195 Collection<ComponentDependencyModelEntity> existingComponentsDependencies =
196 getExistingComponentDependencies();
197 Collection<ComponentEntity> componentListWithMoreComponentsInHeat =
199 doReturn(componentListWithMoreComponentsInHeat).when(componentDaoMock).list(any());
200 Map<String, String> componentIdNameInfoBeforeProcess = getVspInitComponentIdNameInfo();
201 orchestrationUtil.updateVspComponentDependencies(VSP_ID, VERSION01,
202 componentIdNameInfoBeforeProcess, existingComponentsDependencies);
203 Mockito.verify(componentDependencyModelDaoMock, Mockito.times(0)).create(any());
207 public void testVspComponentIdNameInfoNoComponents() {
208 Collection<ComponentEntity> initialVspComponents = new ArrayList<>();
209 doReturn(initialVspComponents).when(componentDaoMock).list(any());
210 Map<String, String> vspComponentIdNameInfo =
211 orchestrationUtil.getVspComponentIdNameInfo(VSP_ID, VERSION01);
212 assertEquals(vspComponentIdNameInfo.size(), 0);
216 public void testVspComponentIdNameInfo() {
217 doReturn(getInitialVspComponents()).when(componentDaoMock).list(any());
218 Map<String, String> vspComponentIdNameInfo =
219 orchestrationUtil.getVspComponentIdNameInfo(VSP_ID, VERSION01);
221 assertEquals(vspComponentIdNameInfo.size(), 3);
222 assertNotNull(vspComponentIdNameInfo.get(COMPONENT_ORIG_ID_1));
223 assertNotNull(vspComponentIdNameInfo.get(COMPONENT_ORIG_ID_2));
224 assertNotNull(vspComponentIdNameInfo.get(COMPONENT_ORIG_ID_3));
228 public void testVspComponentIdNameInfoEmptyCompositionData() {
229 Collection<ComponentEntity> initialVspComponents = getInitialVspComponents();
230 ComponentEntity componentEntity = new ComponentEntity();
231 componentEntity.setId(COMPONENT_ORIG_ID_4);
232 initialVspComponents.add(componentEntity);
234 doReturn(initialVspComponents).when(componentDaoMock).list(any());
235 Map<String, String> vspComponentIdNameInfo =
236 orchestrationUtil.getVspComponentIdNameInfo(VSP_ID, VERSION01);
238 assertEquals(vspComponentIdNameInfo.size(), 3);
239 assertNotNull(vspComponentIdNameInfo.get(COMPONENT_ORIG_ID_1));
240 assertNotNull(vspComponentIdNameInfo.get(COMPONENT_ORIG_ID_2));
241 assertNotNull(vspComponentIdNameInfo.get(COMPONENT_ORIG_ID_3));
242 Assert.assertNull(vspComponentIdNameInfo.get(COMPONENT_ORIG_ID_4));
246 public void testGetComponentDependenciesBeforeDeleteInvalid() {
247 Collection<ComponentDependencyModelEntity> componentDependenciesBeforeDelete =
248 orchestrationUtil.getComponentDependenciesBeforeDelete(null, null);
249 assertEquals(componentDependenciesBeforeDelete.size(), 0);
253 public void testGetComponentDependenciesBeforeDeleteValid() {
254 Collection<ComponentDependencyModelEntity> existingComponentsDependencies =
255 getExistingComponentDependencies();
256 doReturn(existingComponentsDependencies).when(componentDependencyModelDaoMock).list(any());
257 Collection<ComponentDependencyModelEntity> componentDependenciesBeforeDelete =
258 orchestrationUtil.getComponentDependenciesBeforeDelete(VSP_ID, VERSION01);
259 assertEquals(componentDependenciesBeforeDelete.size(), 2);
263 public void testGetFileDataStructure() {
264 Optional<String> jsonFileDataStructure = Optional.of("{\n" +
265 " \"modules\": [\n" +
267 " \"yaml\": \"hot-mog-0108-bs1271.yml\",\n" +
268 " \"env\": \"hot-mog-0108-bs1271.env\"\n" +
271 " \"unassigned\": [],\n" +
272 " \"artifacts\": [],\n" +
273 " \"nested\": []\n" +
275 Optional<FilesDataStructure> filesDataStructureOptional = Optional.of(JsonUtil.json2Object
276 (jsonFileDataStructure.get(), FilesDataStructure.class));
277 doReturn(filesDataStructureOptional).when(candidateServiceMock)
278 .getOrchestrationTemplateCandidateFileDataStructure(VSP_ID, VERSION01);
279 Optional<FilesDataStructure> filesDataStructure = candidateManager.getFilesDataStructure(VSP_ID,
281 assertNotNull(filesDataStructure);
282 assertEquals(filesDataStructureOptional.get().getModules().size(), filesDataStructure.get()
283 .getModules().size());
284 assertEquals(filesDataStructureOptional.get().getModules().get(0).getName(),
285 filesDataStructure.get().getModules().get(0).getName());
289 public void testAbort() {
291 Mockito.doNothing().when(candidateServiceMock).deleteOrchestrationTemplateCandidate(VSP_ID,
293 candidateManager.abort(VSP_ID, VERSION01);
295 Mockito.verify(candidateServiceMock, Mockito.times(1)).deleteOrchestrationTemplateCandidate
299 private Map<String, String> getVspInitComponentIdNameInfo() {
300 Map<String, String> componentIdNameInfoBeforeProcess = new HashMap<>();
301 componentIdNameInfoBeforeProcess.put(COMPONENT_ORIG_ID_1, COMPONENT_NAME_1);
302 componentIdNameInfoBeforeProcess.put(COMPONENT_ORIG_ID_2, COMPONENT_NAME_2);
303 componentIdNameInfoBeforeProcess.put(COMPONENT_ORIG_ID_3, COMPONENT_NAME_3);
304 return componentIdNameInfoBeforeProcess;
307 private Collection<ComponentEntity> getInitialVspComponents() {
308 Collection<ComponentEntity> vspComponents = new ArrayList<>();
309 ComponentEntity component1 = createComponentEntity(COMPONENT_ORIG_ID_1, COMPONENT_NAME_1);
310 ComponentEntity component2 = createComponentEntity(COMPONENT_ORIG_ID_2, COMPONENT_NAME_2);
311 ComponentEntity component3 = createComponentEntity(COMPONENT_ORIG_ID_3, COMPONENT_NAME_3);
312 vspComponents.add(component1);
313 vspComponents.add(component2);
314 vspComponents.add(component3);
315 return vspComponents;
318 private Collection<ComponentEntity> getComponentListWithMoreComponentsInHeat() {
319 Collection<ComponentEntity> vspComponents = getInitialVspComponents();
320 createInitialComponentDependencies(vspComponents);
321 ComponentEntity newComponent = createComponentEntity(COMPONENT_NEW_ID_4, COMPONENT_NAME_4);
322 vspComponents.add(newComponent);
323 return vspComponents;
326 private Collection<ComponentEntity> getComponentListWithLessComponentsInHeat() {
327 Collection<ComponentEntity> vspComponents = getInitialVspComponents();
328 for (Iterator<ComponentEntity> iterator = vspComponents.iterator(); iterator.hasNext(); ) {
329 ComponentEntity componentEntity = iterator.next();
330 switch (componentEntity.getComponentCompositionData().getName()) {
331 case COMPONENT_NAME_1:
334 case COMPONENT_NAME_2:
335 componentEntity.setId(COMPONENT_NEW_ID_2);
337 case COMPONENT_NAME_3:
338 componentEntity.setId(COMPONENT_NEW_ID_3);
342 return vspComponents;
345 private Collection<ComponentEntity> getComponentListWithSameComponentsInHeat() {
346 Collection<ComponentEntity> vspComponents = getInitialVspComponents();
347 createInitialComponentDependencies(vspComponents);
348 return vspComponents;
351 private Collection<ComponentDependencyModelEntity> getExistingComponentDependencies() {
352 Collection<ComponentDependencyModelEntity> newComponents = new ArrayList<>();
353 ComponentDependencyModelEntity entity =
354 createComponentDependencyEntity(COMPONENT_ORIG_ID_1, COMPONENT_ORIG_ID_2);
355 ComponentDependencyModelEntity entity2 =
356 createComponentDependencyEntity(COMPONENT_ORIG_ID_2, COMPONENT_ORIG_ID_3);
357 newComponents.add(entity);
358 newComponents.add(entity2);
359 return newComponents;
362 private ComponentEntity createComponentEntity(String componentId, String componentName) {
363 ComponentEntity componentEntity = new ComponentEntity();
364 componentEntity.setVspId(VSP_ID);
365 componentEntity.setVersion(VERSION01);
366 componentEntity.setId(componentId);
367 ComponentData data = new ComponentData();
368 data.setName(componentName);
369 componentEntity.setComponentCompositionData(data);
370 return componentEntity;
373 private ComponentDependencyModelEntity createComponentDependencyEntity(String sourceComponentId,
374 String targetComponentId) {
375 ComponentDependencyModelEntity componentDependency = new ComponentDependencyModelEntity();
376 componentDependency.setVspId(VSP_ID);
377 componentDependency.setVersion(VERSION01);
378 componentDependency.setRelation("dependsOn");
379 componentDependency.setSourceComponentId(sourceComponentId);
380 componentDependency.setTargetComponentId(targetComponentId);
381 return componentDependency;
384 private void createInitialComponentDependencies(Collection<ComponentEntity> vspComponents) {
385 for (ComponentEntity componentEntity : vspComponents) {
386 switch (componentEntity.getComponentCompositionData().getName()) {
387 case COMPONENT_NAME_1:
388 componentEntity.setId(COMPONENT_NEW_ID_1);
390 case COMPONENT_NAME_2:
391 componentEntity.setId(COMPONENT_NEW_ID_2);
393 case COMPONENT_NAME_3:
394 componentEntity.setId(COMPONENT_NEW_ID_3);
401 public void testUpdateFilesDataStructure() {
402 assertNotNull(candidateManager.updateFilesDataStructure(VSP_ID, VERSION01, new FilesDataStructure()));
406 public void testGet() throws IOException {
407 Optional<Pair<String, byte[]>> optionalStringPair = candidateManager.get(VSP_ID, VERSION01);
408 assertFalse(optionalStringPair.isPresent());
412 public void testGetInfo() {
413 assertNotNull(candidateManager.getInfo(VSP_ID, VERSION01));