a86b1ef0c56871d8a67bc8a7b9316b4a89a109ab
[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   @Mock
81   private VendorSoftwareProductInfoDao vspInfoDaoMock;
82   @Mock
83   private CandidateService candidateServiceMock;
84   @Mock
85   private OrchestrationTemplateDao orchestrationTemplateDaoMock;
86   @Mock
87   private ComponentDependencyModelDao componentDependencyModelDaoMock;
88   @Mock
89   private ComponentDao componentDaoMock;
90
91   @InjectMocks
92   private OrchestrationTemplateCandidateManagerImpl candidateManager;
93
94   @InjectMocks
95   private OrchestrationUtil orchestrationUtil;
96
97   @BeforeClass
98   public void setUp() throws Exception {
99     MockitoAnnotations.initMocks(this);
100   }
101
102   @AfterMethod
103   private void resetMocks() {
104     Mockito.reset(vspInfoDaoMock);
105     Mockito.reset(candidateServiceMock);
106     Mockito.reset(orchestrationTemplateDaoMock);
107     Mockito.reset(componentDependencyModelDaoMock);
108     Mockito.reset(componentDaoMock);
109   }
110
111   @AfterClass
112   public void tearDown() {
113     candidateManager = null;
114     orchestrationUtil = null;
115   }
116
117   @Test
118   public void testProcessEmptyUpload() {
119     OrchestrationTemplateCandidateData orchTemplate =
120         new OrchestrationTemplateCandidateData();
121     orchTemplate
122         .setContentData(ByteBuffer.wrap(FileUtils.toByteArray(new ZipFileUtils().getZipInputStream
123             ("/vspmanager/zips/emptyComposition.zip"))));
124     orchTemplate.setFilesDataStructure("{\n" +
125         "  \"modules\": [\n" +
126         "    {\n" +
127         "      \"isBase\": false,\n" +
128         "      \"yaml\": \"ep-jsa_net.yaml\"\n" +
129         "    }\n" +
130         "  ]\n" +
131         "}");
132     doReturn(orchTemplate)
133         .when(candidateServiceMock).getOrchestrationTemplateCandidate(any(), any());
134
135     doReturn(new VspDetails(VSP_ID, VERSION01))
136         .when(vspInfoDaoMock).get(any());
137     doReturn(null)
138         .when(orchestrationTemplateDaoMock).getInfo(any(), any());
139
140     doReturn("{}").when(candidateServiceMock).createManifest(any(), any());
141     doReturn(Optional.empty()).when(candidateServiceMock)
142         .fetchZipFileByteArrayInputStream(any(), any(), any(),
143             eq(OnboardingTypesEnum.ZIP), any());
144
145
146     OrchestrationTemplateActionResponse response =
147         candidateManager.process(VSP_ID, VERSION01);
148
149     assertNotNull(response);
150   }
151
152   @Test
153   public void testUpdateVspComponentDependenciesHeatReuploadMoreComponents() {
154     Collection<ComponentDependencyModelEntity> existingComponentsDependencies =
155         getExistingComponentDependencies();
156     Collection<ComponentEntity> componentListWithMoreComponentsInHeat =
157         getComponentListWithMoreComponentsInHeat();
158
159     doReturn(componentListWithMoreComponentsInHeat).when(componentDaoMock).list(any());
160     Map<String, String> componentIdNameInfoBeforeProcess = getVspInitComponentIdNameInfo();
161     orchestrationUtil.updateVspComponentDependencies(VSP_ID, VERSION01,
162         componentIdNameInfoBeforeProcess, existingComponentsDependencies);
163     Mockito.verify(componentDependencyModelDaoMock, Mockito.times(2)).create(any());
164   }
165
166   @Test
167   public void testUpdateVspComponentDependenciesHeatReuploadLessComponents() {
168     Collection<ComponentDependencyModelEntity> existingComponentsDependencies =
169         getExistingComponentDependencies();
170     Collection<ComponentEntity> componentListWithLessComponentsInHeat =
171         getComponentListWithLessComponentsInHeat();
172     doReturn(componentListWithLessComponentsInHeat).when(componentDaoMock).list(any());
173     Map<String, String> componentIdNameInfoBeforeProcess = getVspInitComponentIdNameInfo();
174     orchestrationUtil.updateVspComponentDependencies(VSP_ID, VERSION01,
175         componentIdNameInfoBeforeProcess, existingComponentsDependencies);
176     Mockito.verify(componentDependencyModelDaoMock, Mockito.times(1)).create(any());
177   }
178
179   @Test
180   public void testUpdateVspComponentDependenciesHeatReuploadSameComponents() {
181     Collection<ComponentDependencyModelEntity> existingComponentsDependencies =
182         getExistingComponentDependencies();
183     Collection<ComponentEntity> componentListWithSameComponentsInHeat =
184         getComponentListWithSameComponentsInHeat();
185     doReturn(componentListWithSameComponentsInHeat).when(componentDaoMock).list(any());
186     Map<String, String> componentIdNameInfoBeforeProcess = getVspInitComponentIdNameInfo();
187     orchestrationUtil.updateVspComponentDependencies(VSP_ID, VERSION01,
188         componentIdNameInfoBeforeProcess, existingComponentsDependencies);
189     Mockito.verify(componentDependencyModelDaoMock, Mockito.times(2)).create(any());
190   }
191
192   @Test
193   public void testUpdateVspComponentDependenciesHeatReuploadNoComponents() {
194     Collection<ComponentDependencyModelEntity> existingComponentsDependencies =
195         getExistingComponentDependencies();
196     Collection<ComponentEntity> componentListWithMoreComponentsInHeat =
197         new ArrayList<>();
198     doReturn(componentListWithMoreComponentsInHeat).when(componentDaoMock).list(any());
199     Map<String, String> componentIdNameInfoBeforeProcess = getVspInitComponentIdNameInfo();
200     orchestrationUtil.updateVspComponentDependencies(VSP_ID, VERSION01,
201         componentIdNameInfoBeforeProcess, existingComponentsDependencies);
202     Mockito.verify(componentDependencyModelDaoMock, Mockito.times(0)).create(any());
203   }
204
205   @Test
206   public void testVspComponentIdNameInfoNoComponents() {
207     Collection<ComponentEntity> initialVspComponents = new ArrayList<>();
208     doReturn(initialVspComponents).when(componentDaoMock).list(any());
209     Map<String, String> vspComponentIdNameInfo =
210         orchestrationUtil.getVspComponentIdNameInfo(VSP_ID, VERSION01);
211     Assert.assertEquals(vspComponentIdNameInfo.size(), 0);
212   }
213
214   @Test
215   public void testVspComponentIdNameInfo() {
216     doReturn(getInitialVspComponents()).when(componentDaoMock).list(any());
217     Map<String, String> vspComponentIdNameInfo =
218         orchestrationUtil.getVspComponentIdNameInfo(VSP_ID, VERSION01);
219
220     assertEquals(vspComponentIdNameInfo.size(), 3);
221     assertNotNull(vspComponentIdNameInfo.get(COMPONENT_ORIG_ID_1));
222     assertNotNull(vspComponentIdNameInfo.get(COMPONENT_ORIG_ID_2));
223     assertNotNull(vspComponentIdNameInfo.get(COMPONENT_ORIG_ID_3));
224   }
225
226   @Test
227   public void testVspComponentIdNameInfoEmptyCompositionData() {
228     Collection<ComponentEntity> initialVspComponents = getInitialVspComponents();
229     ComponentEntity componentEntity = new ComponentEntity();
230     componentEntity.setId(COMPONENT_ORIG_ID_4);
231     initialVspComponents.add(componentEntity);
232
233     doReturn(initialVspComponents).when(componentDaoMock).list(any());
234     Map<String, String> vspComponentIdNameInfo =
235         orchestrationUtil.getVspComponentIdNameInfo(VSP_ID, VERSION01);
236
237     assertEquals(vspComponentIdNameInfo.size(), 3);
238     assertNotNull(vspComponentIdNameInfo.get(COMPONENT_ORIG_ID_1));
239     assertNotNull(vspComponentIdNameInfo.get(COMPONENT_ORIG_ID_2));
240     assertNotNull(vspComponentIdNameInfo.get(COMPONENT_ORIG_ID_3));
241     Assert.assertNull(vspComponentIdNameInfo.get(COMPONENT_ORIG_ID_4));
242   }
243
244   @Test
245   public void testGetComponentDependenciesBeforeDeleteInvalid() {
246     Collection<ComponentDependencyModelEntity> componentDependenciesBeforeDelete =
247         orchestrationUtil.getComponentDependenciesBeforeDelete(null, null);
248     Assert.assertEquals(componentDependenciesBeforeDelete.size(), 0);
249   }
250
251   @Test
252   public void testGetComponentDependenciesBeforeDeleteValid() {
253     Collection<ComponentDependencyModelEntity> existingComponentsDependencies =
254         getExistingComponentDependencies();
255     doReturn(existingComponentsDependencies).when(componentDependencyModelDaoMock).list(any());
256     Collection<ComponentDependencyModelEntity> componentDependenciesBeforeDelete =
257         orchestrationUtil.getComponentDependenciesBeforeDelete(VSP_ID, VERSION01);
258     Assert.assertEquals(componentDependenciesBeforeDelete.size(), 2);
259   }
260
261   @Test
262   public void testGetFileDataStructure() {
263     Optional<String> jsonFileDataStructure = Optional.of(new String("{\n" +
264         "  \"modules\": [\n" +
265         "    {\n" +
266         "      \"yaml\": \"hot-mog-0108-bs1271.yml\",\n" +
267         "      \"env\": \"hot-mog-0108-bs1271.env\"\n" +
268         "    }\n" +
269         "  ],\n" +
270         "  \"unassigned\": [],\n" +
271         "  \"artifacts\": [],\n" +
272         "  \"nested\": []\n" +
273         "}"));
274     Optional<FilesDataStructure> filesDataStructureOptional = Optional.of(JsonUtil.json2Object
275         (jsonFileDataStructure.get(), FilesDataStructure.class));
276     doReturn(filesDataStructureOptional).when(candidateServiceMock)
277         .getOrchestrationTemplateCandidateFileDataStructure(VSP_ID, VERSION01);
278     Optional<FilesDataStructure> filesDataStructure = candidateManager.getFilesDataStructure(VSP_ID,
279         VERSION01);
280     assertNotNull(filesDataStructure);
281     assertEquals(filesDataStructureOptional.get().getModules().size(), filesDataStructure.get()
282         .getModules().size());
283     assertEquals(filesDataStructureOptional.get().getModules().get(0).getName(),
284         filesDataStructure.get().getModules().get(0).getName());
285   }
286
287   @Test
288   public void testAbort() {
289
290     Mockito.doNothing().when(candidateServiceMock).deleteOrchestrationTemplateCandidate(VSP_ID,
291         VERSION01);
292     candidateManager.abort(VSP_ID, VERSION01);
293
294     Mockito.verify(candidateServiceMock, Mockito.times(1)).deleteOrchestrationTemplateCandidate
295         (VSP_ID, VERSION01);
296   }
297
298   private Map<String, String> getVspInitComponentIdNameInfo() {
299     Map<String, String> componentIdNameInfoBeforeProcess = new HashMap<>();
300     componentIdNameInfoBeforeProcess.put(COMPONENT_ORIG_ID_1, COMPONENT_NAME_1);
301     componentIdNameInfoBeforeProcess.put(COMPONENT_ORIG_ID_2, COMPONENT_NAME_2);
302     componentIdNameInfoBeforeProcess.put(COMPONENT_ORIG_ID_3, COMPONENT_NAME_3);
303     return componentIdNameInfoBeforeProcess;
304   }
305
306   private Collection<ComponentEntity> getInitialVspComponents() {
307     Collection<ComponentEntity> vspComponents = new ArrayList<>();
308     ComponentEntity component1 = createComponentEntity(COMPONENT_ORIG_ID_1, COMPONENT_NAME_1);
309     ComponentEntity component2 = createComponentEntity(COMPONENT_ORIG_ID_2, COMPONENT_NAME_2);
310     ComponentEntity component3 = createComponentEntity(COMPONENT_ORIG_ID_3, COMPONENT_NAME_3);
311     vspComponents.add(component1);
312     vspComponents.add(component2);
313     vspComponents.add(component3);
314     return vspComponents;
315   }
316
317   private Collection<ComponentEntity> getComponentListWithMoreComponentsInHeat() {
318     Collection<ComponentEntity> vspComponents = getInitialVspComponents();
319     createInitialComponentDependencies(vspComponents);
320     ComponentEntity newComponent = createComponentEntity(COMPONENT_NEW_ID_4, COMPONENT_NAME_4);
321     vspComponents.add(newComponent);
322     return vspComponents;
323   }
324
325   private Collection<ComponentEntity> getComponentListWithLessComponentsInHeat() {
326     Collection<ComponentEntity> vspComponents = getInitialVspComponents();
327     for (Iterator<ComponentEntity> iterator = vspComponents.iterator(); iterator.hasNext(); ) {
328       ComponentEntity componentEntity = iterator.next();
329       switch (componentEntity.getComponentCompositionData().getName()) {
330         case COMPONENT_NAME_1:
331           iterator.remove();
332           break;
333         case COMPONENT_NAME_2:
334           componentEntity.setId(COMPONENT_NEW_ID_2);
335           break;
336         case COMPONENT_NAME_3:
337           componentEntity.setId(COMPONENT_NEW_ID_3);
338           break;
339       }
340     }
341     return vspComponents;
342   }
343
344   private Collection<ComponentEntity> getComponentListWithSameComponentsInHeat() {
345     Collection<ComponentEntity> vspComponents = getInitialVspComponents();
346     createInitialComponentDependencies(vspComponents);
347     return vspComponents;
348   }
349
350   private Collection<ComponentDependencyModelEntity> getExistingComponentDependencies() {
351     Collection<ComponentDependencyModelEntity> newComponents = new ArrayList<>();
352     ComponentDependencyModelEntity entity =
353         createComponentDependencyEntity(COMPONENT_ORIG_ID_1, COMPONENT_ORIG_ID_2);
354     ComponentDependencyModelEntity entity2 =
355         createComponentDependencyEntity(COMPONENT_ORIG_ID_2, COMPONENT_ORIG_ID_3);
356     newComponents.add(entity);
357     newComponents.add(entity2);
358     return newComponents;
359   }
360
361   private ComponentEntity createComponentEntity(String componentId, String componentName) {
362     ComponentEntity componentEntity = new ComponentEntity();
363     componentEntity.setVspId(VSP_ID);
364     componentEntity.setVersion(VERSION01);
365     componentEntity.setId(componentId);
366     ComponentData data = new ComponentData();
367     data.setName(componentName);
368     componentEntity.setComponentCompositionData(data);
369     return componentEntity;
370   }
371
372   private ComponentDependencyModelEntity createComponentDependencyEntity(String sourceComponentId,
373                                                                          String targetComponentId) {
374     ComponentDependencyModelEntity componentDependency = new ComponentDependencyModelEntity();
375     componentDependency.setVspId(VSP_ID);
376     componentDependency.setVersion(VERSION01);
377     componentDependency.setRelation("dependsOn");
378     componentDependency.setSourceComponentId(sourceComponentId);
379     componentDependency.setTargetComponentId(targetComponentId);
380     return componentDependency;
381   }
382
383   private void createInitialComponentDependencies(Collection<ComponentEntity> vspComponents) {
384     for (ComponentEntity componentEntity : vspComponents) {
385       switch (componentEntity.getComponentCompositionData().getName()) {
386         case COMPONENT_NAME_1:
387           componentEntity.setId(COMPONENT_NEW_ID_1);
388           break;
389         case COMPONENT_NAME_2:
390           componentEntity.setId(COMPONENT_NEW_ID_2);
391           break;
392         case COMPONENT_NAME_3:
393           componentEntity.setId(COMPONENT_NEW_ID_3);
394           break;
395       }
396     }
397   }
398 }