Added oparent to sdc main
[sdc.git] / openecomp-be / backend / openecomp-sdc-vendor-software-product-manager / src / test / java / org / openecomp / sdc / vendorsoftwareproduct / impl / OrchestrationTemplateCandidateManagerImplTest.java
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  *  Modifications Copyright (c) 2019 Samsung
11  * ================================================================================
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  */
19
20 package org.openecomp.sdc.vendorsoftwareproduct.impl;
21
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;
50
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;
56 import java.util.Map;
57 import java.util.Optional;
58
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;
65
66 public class OrchestrationTemplateCandidateManagerImplTest {
67   private static final String VSP_ID = "vspId";
68   private static final Version VERSION01 = new Version("versionId");
69
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";
74
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";
79
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";
84
85   @Mock
86   private VendorSoftwareProductInfoDao vspInfoDaoMock;
87   @Mock
88   private CandidateService candidateServiceMock;
89   @Mock
90   private OrchestrationTemplateDao orchestrationTemplateDaoMock;
91   @Mock
92   private ComponentDependencyModelDao componentDependencyModelDaoMock;
93   @Mock
94   private ComponentDao componentDaoMock;
95
96   @InjectMocks
97   private OrchestrationTemplateCandidateManagerImpl candidateManager;
98
99   @InjectMocks
100   private OrchestrationUtil orchestrationUtil;
101
102   @Before
103   public void setUp() {
104     MockitoAnnotations.initMocks(this);
105   }
106
107   @After
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;
116   }
117
118   @Test
119   public void testProcessEmptyUpload() {
120     OrchestrationTemplateCandidateData orchTemplate =
121         new OrchestrationTemplateCandidateData();
122     orchTemplate
123         .setContentData(ByteBuffer.wrap(FileUtils.toByteArray(new ZipFileUtils().getZipInputStream
124             ("/vspmanager/zips/emptyComposition.zip"))));
125     orchTemplate.setFilesDataStructure("{\n" +
126         "  \"modules\": [\n" +
127         "    {\n" +
128         "      \"isBase\": false,\n" +
129         "      \"yaml\": \"ep-jsa_net.yaml\"\n" +
130         "    }\n" +
131         "  ]\n" +
132         "}");
133     doReturn(Optional.of(orchTemplate))
134         .when(candidateServiceMock).getOrchestrationTemplateCandidate(any(), any());
135
136     doReturn(new VspDetails(VSP_ID, VERSION01))
137         .when(vspInfoDaoMock).get(any());
138     doReturn(null)
139         .when(orchestrationTemplateDaoMock).getInfo(any(), any());
140
141     doReturn("{}").when(candidateServiceMock).createManifest(any(), any());
142     doReturn(Optional.empty()).when(candidateServiceMock)
143         .fetchZipFileByteArrayInputStream(any(), any(), any(),
144             eq(OnboardingTypesEnum.ZIP), any());
145
146
147     OrchestrationTemplateActionResponse response =
148         candidateManager.process(VSP_ID, VERSION01);
149
150     assertNotNull(response);
151   }
152
153   @Test
154   public void testUpdateVspComponentDependenciesHeatReuploadMoreComponents() {
155     Collection<ComponentDependencyModelEntity> existingComponentsDependencies =
156         getExistingComponentDependencies();
157     Collection<ComponentEntity> componentListWithMoreComponentsInHeat =
158         getComponentListWithMoreComponentsInHeat();
159
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());
165   }
166
167   @Test
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());
178   }
179
180   @Test
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());
191   }
192
193   @Test
194   public void testUpdateVspComponentDependenciesHeatReuploadNoComponents() {
195     Collection<ComponentDependencyModelEntity> existingComponentsDependencies =
196         getExistingComponentDependencies();
197     Collection<ComponentEntity> componentListWithMoreComponentsInHeat =
198         new ArrayList<>();
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());
204   }
205
206   @Test
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);
213   }
214
215   @Test
216   public void testVspComponentIdNameInfo() {
217     doReturn(getInitialVspComponents()).when(componentDaoMock).list(any());
218     Map<String, String> vspComponentIdNameInfo =
219         orchestrationUtil.getVspComponentIdNameInfo(VSP_ID, VERSION01);
220
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));
225   }
226
227   @Test
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);
233
234     doReturn(initialVspComponents).when(componentDaoMock).list(any());
235     Map<String, String> vspComponentIdNameInfo =
236         orchestrationUtil.getVspComponentIdNameInfo(VSP_ID, VERSION01);
237
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));
243   }
244
245   @Test
246   public void testGetComponentDependenciesBeforeDeleteInvalid() {
247     Collection<ComponentDependencyModelEntity> componentDependenciesBeforeDelete =
248         orchestrationUtil.getComponentDependenciesBeforeDelete(null, null);
249     assertEquals(componentDependenciesBeforeDelete.size(), 0);
250   }
251
252   @Test
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);
260   }
261
262   @Test
263   public void testGetFileDataStructure() {
264     Optional<String> jsonFileDataStructure = Optional.of("{\n" +
265         "  \"modules\": [\n" +
266         "    {\n" +
267         "      \"yaml\": \"hot-mog-0108-bs1271.yml\",\n" +
268         "      \"env\": \"hot-mog-0108-bs1271.env\"\n" +
269         "    }\n" +
270         "  ],\n" +
271         "  \"unassigned\": [],\n" +
272         "  \"artifacts\": [],\n" +
273         "  \"nested\": []\n" +
274         "}");
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,
280         VERSION01);
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());
286   }
287
288   @Test
289   public void testAbort() {
290
291     Mockito.doNothing().when(candidateServiceMock).deleteOrchestrationTemplateCandidate(VSP_ID,
292         VERSION01);
293     candidateManager.abort(VSP_ID, VERSION01);
294
295     Mockito.verify(candidateServiceMock, Mockito.times(1)).deleteOrchestrationTemplateCandidate
296         (VSP_ID, VERSION01);
297   }
298
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;
305   }
306
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;
316   }
317
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;
324   }
325
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:
332           iterator.remove();
333           break;
334         case COMPONENT_NAME_2:
335           componentEntity.setId(COMPONENT_NEW_ID_2);
336           break;
337         case COMPONENT_NAME_3:
338           componentEntity.setId(COMPONENT_NEW_ID_3);
339           break;
340       }
341     }
342     return vspComponents;
343   }
344
345   private Collection<ComponentEntity> getComponentListWithSameComponentsInHeat() {
346     Collection<ComponentEntity> vspComponents = getInitialVspComponents();
347     createInitialComponentDependencies(vspComponents);
348     return vspComponents;
349   }
350
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;
360   }
361
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;
371   }
372
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;
382   }
383
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);
389           break;
390         case COMPONENT_NAME_2:
391           componentEntity.setId(COMPONENT_NEW_ID_2);
392           break;
393         case COMPONENT_NAME_3:
394           componentEntity.setId(COMPONENT_NEW_ID_3);
395           break;
396       }
397     }
398   }
399
400   @Test
401   public void testUpdateFilesDataStructure() {
402     assertNotNull(candidateManager.updateFilesDataStructure(VSP_ID, VERSION01, new FilesDataStructure()));
403   }
404
405   @Test
406   public void testGet() throws IOException {
407     Optional<Pair<String, byte[]>> optionalStringPair = candidateManager.get(VSP_ID, VERSION01);
408     assertFalse(optionalStringPair.isPresent());
409   }
410
411   @Test
412   public void testGetInfo() {
413       assertNotNull(candidateManager.getInfo(VSP_ID, VERSION01));
414   }
415 }