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