e1d58c6a1846f65db9fd553481f9db53fddbe74c
[sdc.git] /
1 /*
2  * Copyright © 2016-2018 European Support Limited
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.openecomp.sdc.vendorsoftwareproduct.impl;
18
19 import static org.mockito.Matchers.any;
20 import static org.mockito.Matchers.eq;
21 import static org.mockito.Mockito.doReturn;
22 import static org.testng.Assert.assertEquals;
23 import static org.testng.Assert.assertNotNull;
24
25 import org.mockito.InjectMocks;
26 import org.mockito.Mock;
27 import org.mockito.Mockito;
28 import org.mockito.MockitoAnnotations;
29 import org.openecomp.core.utilities.file.FileUtils;
30 import org.openecomp.core.utilities.json.JsonUtil;
31 import org.openecomp.core.utilities.orchestration.OnboardingTypesEnum;
32 import org.openecomp.sdc.vendorsoftwareproduct.dao.ComponentDao;
33 import org.openecomp.sdc.vendorsoftwareproduct.dao.ComponentDependencyModelDao;
34 import org.openecomp.sdc.vendorsoftwareproduct.dao.OrchestrationTemplateDao;
35 import org.openecomp.sdc.vendorsoftwareproduct.dao.VendorSoftwareProductInfoDao;
36 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComponentDependencyModelEntity;
37 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComponentEntity;
38 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.OrchestrationTemplateCandidateData;
39 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.VspDetails;
40 import org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.OrchestrationUtil;
41 import org.openecomp.sdc.vendorsoftwareproduct.services.filedatastructuremodule.CandidateService;
42 import org.openecomp.sdc.vendorsoftwareproduct.types.OrchestrationTemplateActionResponse;
43 import org.openecomp.sdc.vendorsoftwareproduct.types.candidateheat.FilesDataStructure;
44 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.ComponentData;
45 import org.openecomp.sdc.vendorsoftwareproduct.utils.ZipFileUtils;
46 import org.openecomp.sdc.versioning.dao.types.Version;
47 import org.testng.Assert;
48 import org.testng.annotations.AfterClass;
49 import org.testng.annotations.AfterMethod;
50 import org.testng.annotations.BeforeClass;
51 import org.testng.annotations.Test;
52
53 import java.nio.ByteBuffer;
54 import java.util.ArrayList;
55 import java.util.Collection;
56 import java.util.HashMap;
57 import java.util.Iterator;
58 import java.util.Map;
59 import java.util.Optional;
60
61 public class OrchestrationTemplateCandidateManagerImplTest {
62   private static final String VSP_ID = "vspId";
63   private static final Version VERSION01 = new Version("versionId");
64
65   private static final String COMPONENT_ORIG_ID_1 = "Component_Pd_Server_Id_Orig";
66   private static final String COMPONENT_ORIG_ID_2 = "Component_Sm_Server_Id_Orig";
67   private static final String COMPONENT_ORIG_ID_3 = "Component_Oam_Server_Id_Orig";
68   private static final String COMPONENT_ORIG_ID_4 = "Component_Ps_Server_Id_Orig";
69
70   private static final String COMPONENT_NEW_ID_1 = "Component_Pd_Server_Id_New";
71   private static final String COMPONENT_NEW_ID_2 = "Component_Sm_Server_Id_New";
72   private static final String COMPONENT_NEW_ID_3 = "Component_Oam_Server_Id_New";
73   private static final String COMPONENT_NEW_ID_4 = "Component_Ps_Server_Id_New";
74
75   private static final String COMPONENT_NAME_1 = "pd_server";
76   private static final String COMPONENT_NAME_2 = "sm_server";
77   private static final String COMPONENT_NAME_3 = "oam_server";
78   private static final String COMPONENT_NAME_4 = "ps_server";
79
80   private static final String USER1 = "vspTestUser1";
81
82   @Mock
83   private VendorSoftwareProductInfoDao vspInfoDaoMock;
84   @Mock
85   private CandidateService candidateServiceMock;
86   @Mock
87   private OrchestrationTemplateDao orchestrationTemplateDaoMock;
88   @Mock
89   private ComponentDependencyModelDao componentDependencyModelDaoMock;
90   @Mock
91   private ComponentDao componentDaoMock;
92
93   @InjectMocks
94   private OrchestrationTemplateCandidateManagerImpl candidateManager;
95
96   @InjectMocks
97   private OrchestrationUtil orchestrationUtil;
98
99   @BeforeClass
100   public void setUp() throws Exception {
101     MockitoAnnotations.initMocks(this);
102   }
103
104   @AfterMethod
105   private void resetMocks() {
106     Mockito.reset(vspInfoDaoMock);
107     Mockito.reset(candidateServiceMock);
108     Mockito.reset(orchestrationTemplateDaoMock);
109     Mockito.reset(componentDependencyModelDaoMock);
110     Mockito.reset(componentDaoMock);
111   }
112
113   @AfterClass
114   public void tearDown() {
115     candidateManager = null;
116     orchestrationUtil = null;
117   }
118
119   @Test
120   public void testProcessEmptyUpload() {
121     OrchestrationTemplateCandidateData orchTemplate =
122         new OrchestrationTemplateCandidateData();
123     orchTemplate
124         .setContentData(ByteBuffer.wrap(FileUtils.toByteArray(new ZipFileUtils().getZipInputStream
125             ("/vspmanager/zips/emptyComposition.zip"))));
126     orchTemplate.setFilesDataStructure("{\n" +
127         "  \"modules\": [\n" +
128         "    {\n" +
129         "      \"isBase\": false,\n" +
130         "      \"yaml\": \"ep-jsa_net.yaml\"\n" +
131         "    }\n" +
132         "  ]\n" +
133         "}");
134     doReturn(orchTemplate)
135         .when(candidateServiceMock).getOrchestrationTemplateCandidate(any(), any());
136
137     doReturn(new VspDetails(VSP_ID, VERSION01))
138         .when(vspInfoDaoMock).get(any());
139     doReturn(null)
140         .when(orchestrationTemplateDaoMock).getInfo(any(), any());
141
142     doReturn("{}").when(candidateServiceMock).createManifest(any(), any());
143     doReturn(Optional.empty()).when(candidateServiceMock)
144         .fetchZipFileByteArrayInputStream(any(), any(), any(),
145             eq(OnboardingTypesEnum.ZIP), any());
146
147
148     OrchestrationTemplateActionResponse response =
149         candidateManager.process(VSP_ID, VERSION01);
150
151     assertNotNull(response);
152   }
153
154   @Test
155   public void testUpdateVspComponentDependenciesHeatReuploadMoreComponents() {
156     Collection<ComponentDependencyModelEntity> existingComponentsDependencies =
157         getExistingComponentDependencies();
158     Collection<ComponentEntity> componentListWithMoreComponentsInHeat =
159         getComponentListWithMoreComponentsInHeat();
160
161     doReturn(componentListWithMoreComponentsInHeat).when(componentDaoMock).list(any());
162     Map<String, String> componentIdNameInfoBeforeProcess = getVspInitComponentIdNameInfo();
163     orchestrationUtil.updateVspComponentDependencies(VSP_ID, VERSION01,
164         componentIdNameInfoBeforeProcess, existingComponentsDependencies);
165     Mockito.verify(componentDependencyModelDaoMock, Mockito.times(2)).create(any());
166   }
167
168   @Test
169   public void testUpdateVspComponentDependenciesHeatReuploadLessComponents() {
170     Collection<ComponentDependencyModelEntity> existingComponentsDependencies =
171         getExistingComponentDependencies();
172     Collection<ComponentEntity> componentListWithLessComponentsInHeat =
173         getComponentListWithLessComponentsInHeat();
174     doReturn(componentListWithLessComponentsInHeat).when(componentDaoMock).list(any());
175     Map<String, String> componentIdNameInfoBeforeProcess = getVspInitComponentIdNameInfo();
176     orchestrationUtil.updateVspComponentDependencies(VSP_ID, VERSION01,
177         componentIdNameInfoBeforeProcess, existingComponentsDependencies);
178     Mockito.verify(componentDependencyModelDaoMock, Mockito.times(1)).create(any());
179   }
180
181   @Test
182   public void testUpdateVspComponentDependenciesHeatReuploadSameComponents() {
183     Collection<ComponentDependencyModelEntity> existingComponentsDependencies =
184         getExistingComponentDependencies();
185     Collection<ComponentEntity> componentListWithSameComponentsInHeat =
186         getComponentListWithSameComponentsInHeat();
187     doReturn(componentListWithSameComponentsInHeat).when(componentDaoMock).list(any());
188     Map<String, String> componentIdNameInfoBeforeProcess = getVspInitComponentIdNameInfo();
189     orchestrationUtil.updateVspComponentDependencies(VSP_ID, VERSION01,
190         componentIdNameInfoBeforeProcess, existingComponentsDependencies);
191     Mockito.verify(componentDependencyModelDaoMock, Mockito.times(2)).create(any());
192   }
193
194   @Test
195   public void testUpdateVspComponentDependenciesHeatReuploadNoComponents() {
196     Collection<ComponentDependencyModelEntity> existingComponentsDependencies =
197         getExistingComponentDependencies();
198     Collection<ComponentEntity> componentListWithMoreComponentsInHeat =
199         new ArrayList<>();
200     doReturn(componentListWithMoreComponentsInHeat).when(componentDaoMock).list(any());
201     Map<String, String> componentIdNameInfoBeforeProcess = getVspInitComponentIdNameInfo();
202     orchestrationUtil.updateVspComponentDependencies(VSP_ID, VERSION01,
203         componentIdNameInfoBeforeProcess, existingComponentsDependencies);
204     Mockito.verify(componentDependencyModelDaoMock, Mockito.times(0)).create(any());
205   }
206
207   @Test
208   public void testVspComponentIdNameInfoNoComponents() {
209     Collection<ComponentEntity> initialVspComponents = new ArrayList<>();
210     doReturn(initialVspComponents).when(componentDaoMock).list(any());
211     Map<String, String> vspComponentIdNameInfo =
212         orchestrationUtil.getVspComponentIdNameInfo(VSP_ID, VERSION01);
213     Assert.assertEquals(vspComponentIdNameInfo.size(), 0);
214   }
215
216   @Test
217   public void testVspComponentIdNameInfo() {
218     doReturn(getInitialVspComponents()).when(componentDaoMock).list(any());
219     Map<String, String> vspComponentIdNameInfo =
220         orchestrationUtil.getVspComponentIdNameInfo(VSP_ID, VERSION01);
221
222     assertEquals(vspComponentIdNameInfo.size(), 3);
223     assertNotNull(vspComponentIdNameInfo.get(COMPONENT_ORIG_ID_1));
224     assertNotNull(vspComponentIdNameInfo.get(COMPONENT_ORIG_ID_2));
225     assertNotNull(vspComponentIdNameInfo.get(COMPONENT_ORIG_ID_3));
226   }
227
228   @Test
229   public void testVspComponentIdNameInfoEmptyCompositionData() {
230     Collection<ComponentEntity> initialVspComponents = getInitialVspComponents();
231     ComponentEntity componentEntity = new ComponentEntity();
232     componentEntity.setId(COMPONENT_ORIG_ID_4);
233     initialVspComponents.add(componentEntity);
234
235     doReturn(initialVspComponents).when(componentDaoMock).list(any());
236     Map<String, String> vspComponentIdNameInfo =
237         orchestrationUtil.getVspComponentIdNameInfo(VSP_ID, VERSION01);
238
239     assertEquals(vspComponentIdNameInfo.size(), 3);
240     assertNotNull(vspComponentIdNameInfo.get(COMPONENT_ORIG_ID_1));
241     assertNotNull(vspComponentIdNameInfo.get(COMPONENT_ORIG_ID_2));
242     assertNotNull(vspComponentIdNameInfo.get(COMPONENT_ORIG_ID_3));
243     Assert.assertNull(vspComponentIdNameInfo.get(COMPONENT_ORIG_ID_4));
244   }
245
246   @Test
247   public void testGetComponentDependenciesBeforeDeleteInvalid() {
248     Collection<ComponentDependencyModelEntity> componentDependenciesBeforeDelete =
249         orchestrationUtil.getComponentDependenciesBeforeDelete(null, null);
250     Assert.assertEquals(componentDependenciesBeforeDelete.size(), 0);
251   }
252
253   @Test
254   public void testGetComponentDependenciesBeforeDeleteValid() {
255     Collection<ComponentDependencyModelEntity> existingComponentsDependencies =
256         getExistingComponentDependencies();
257     doReturn(existingComponentsDependencies).when(componentDependencyModelDaoMock).list(any());
258     Collection<ComponentDependencyModelEntity> componentDependenciesBeforeDelete =
259         orchestrationUtil.getComponentDependenciesBeforeDelete(VSP_ID, VERSION01);
260     Assert.assertEquals(componentDependenciesBeforeDelete.size(), 2);
261   }
262
263   @Test
264   public void testGetFileDataStructure() {
265     Optional<String> jsonFileDataStructure = Optional.of(new String("{\n" +
266         "  \"modules\": [\n" +
267         "    {\n" +
268         "      \"yaml\": \"hot-mog-0108-bs1271.yml\",\n" +
269         "      \"env\": \"hot-mog-0108-bs1271.env\"\n" +
270         "    }\n" +
271         "  ],\n" +
272         "  \"unassigned\": [],\n" +
273         "  \"artifacts\": [],\n" +
274         "  \"nested\": []\n" +
275         "}"));
276     Optional<FilesDataStructure> filesDataStructureOptional = Optional.of(JsonUtil.json2Object
277         (jsonFileDataStructure.get(), FilesDataStructure.class));
278     doReturn(filesDataStructureOptional).when(candidateServiceMock)
279         .getOrchestrationTemplateCandidateFileDataStructure(VSP_ID, VERSION01);
280     Optional<FilesDataStructure> filesDataStructure = candidateManager.getFilesDataStructure(VSP_ID,
281         VERSION01);
282     assertNotNull(filesDataStructure);
283     assertEquals(filesDataStructureOptional.get().getModules().size(), filesDataStructure.get()
284         .getModules().size());
285     assertEquals(filesDataStructureOptional.get().getModules().get(0).getName(),
286         filesDataStructure.get().getModules().get(0).getName());
287   }
288
289   @Test
290   public void testAbort() {
291
292     Mockito.doNothing().when(candidateServiceMock).deleteOrchestrationTemplateCandidate(VSP_ID,
293         VERSION01);
294     candidateManager.abort(VSP_ID, VERSION01);
295
296     Mockito.verify(candidateServiceMock, Mockito.times(1)).deleteOrchestrationTemplateCandidate
297         (VSP_ID, VERSION01);
298   }
299
300   private Map<String, String> getVspInitComponentIdNameInfo() {
301     Map<String, String> componentIdNameInfoBeforeProcess = new HashMap<>();
302     componentIdNameInfoBeforeProcess.put(COMPONENT_ORIG_ID_1, COMPONENT_NAME_1);
303     componentIdNameInfoBeforeProcess.put(COMPONENT_ORIG_ID_2, COMPONENT_NAME_2);
304     componentIdNameInfoBeforeProcess.put(COMPONENT_ORIG_ID_3, COMPONENT_NAME_3);
305     return componentIdNameInfoBeforeProcess;
306   }
307
308   private Collection<ComponentEntity> getInitialVspComponents() {
309     Collection<ComponentEntity> vspComponents = new ArrayList<>();
310     ComponentEntity component1 = createComponentEntity(COMPONENT_ORIG_ID_1, COMPONENT_NAME_1);
311     ComponentEntity component2 = createComponentEntity(COMPONENT_ORIG_ID_2, COMPONENT_NAME_2);
312     ComponentEntity component3 = createComponentEntity(COMPONENT_ORIG_ID_3, COMPONENT_NAME_3);
313     vspComponents.add(component1);
314     vspComponents.add(component2);
315     vspComponents.add(component3);
316     return vspComponents;
317   }
318
319   private Collection<ComponentEntity> getComponentListWithMoreComponentsInHeat() {
320     Collection<ComponentEntity> vspComponents = getInitialVspComponents();
321     createInitialComponentDependencies(vspComponents);
322     ComponentEntity newComponent = createComponentEntity(COMPONENT_NEW_ID_4, COMPONENT_NAME_4);
323     vspComponents.add(newComponent);
324     return vspComponents;
325   }
326
327   private Collection<ComponentEntity> getComponentListWithLessComponentsInHeat() {
328     Collection<ComponentEntity> vspComponents = getInitialVspComponents();
329     for (Iterator<ComponentEntity> iterator = vspComponents.iterator(); iterator.hasNext(); ) {
330       ComponentEntity componentEntity = iterator.next();
331       switch (componentEntity.getComponentCompositionData().getName()) {
332         case COMPONENT_NAME_1:
333           iterator.remove();
334           break;
335         case COMPONENT_NAME_2:
336           componentEntity.setId(COMPONENT_NEW_ID_2);
337           break;
338         case COMPONENT_NAME_3:
339           componentEntity.setId(COMPONENT_NEW_ID_3);
340           break;
341       }
342     }
343     return vspComponents;
344   }
345
346   private Collection<ComponentEntity> getComponentListWithSameComponentsInHeat() {
347     Collection<ComponentEntity> vspComponents = getInitialVspComponents();
348     createInitialComponentDependencies(vspComponents);
349     return vspComponents;
350   }
351
352   private Collection<ComponentDependencyModelEntity> getExistingComponentDependencies() {
353     Collection<ComponentDependencyModelEntity> newComponents = new ArrayList<>();
354     ComponentDependencyModelEntity entity =
355         createComponentDependencyEntity(COMPONENT_ORIG_ID_1, COMPONENT_ORIG_ID_2);
356     ComponentDependencyModelEntity entity2 =
357         createComponentDependencyEntity(COMPONENT_ORIG_ID_2, COMPONENT_ORIG_ID_3);
358     newComponents.add(entity);
359     newComponents.add(entity2);
360     return newComponents;
361   }
362
363   private ComponentEntity createComponentEntity(String componentId, String componentName) {
364     ComponentEntity componentEntity = new ComponentEntity();
365     componentEntity.setVspId(VSP_ID);
366     componentEntity.setVersion(VERSION01);
367     componentEntity.setId(componentId);
368     ComponentData data = new ComponentData();
369     data.setName(componentName);
370     componentEntity.setComponentCompositionData(data);
371     return componentEntity;
372   }
373
374   private ComponentDependencyModelEntity createComponentDependencyEntity(String sourceComponentId,
375                                                                          String targetComponentId) {
376     ComponentDependencyModelEntity componentDependency = new ComponentDependencyModelEntity();
377     componentDependency.setVspId(VSP_ID);
378     componentDependency.setVersion(VERSION01);
379     componentDependency.setRelation("dependsOn");
380     componentDependency.setSourceComponentId(sourceComponentId);
381     componentDependency.setTargetComponentId(targetComponentId);
382     return componentDependency;
383   }
384
385   private void createInitialComponentDependencies(Collection<ComponentEntity> vspComponents) {
386     for (ComponentEntity componentEntity : vspComponents) {
387       switch (componentEntity.getComponentCompositionData().getName()) {
388         case COMPONENT_NAME_1:
389           componentEntity.setId(COMPONENT_NEW_ID_1);
390           break;
391         case COMPONENT_NAME_2:
392           componentEntity.setId(COMPONENT_NEW_ID_2);
393           break;
394         case COMPONENT_NAME_3:
395           componentEntity.setId(COMPONENT_NEW_ID_3);
396           break;
397       }
398     }
399   }
400 }