5af39551b487b8b7bb2a9b86ac22747672591aaa
[sdc.git] /
1 package org.openecomp.sdc.vendorsoftwareproduct.impl;
2
3 import static org.mockito.Matchers.any;
4 import static org.mockito.Matchers.eq;
5 import static org.mockito.Mockito.doReturn;
6 import static org.testng.Assert.assertEquals;
7 import static org.testng.Assert.assertNotNull;
8
9 import org.mockito.InjectMocks;
10 import org.mockito.Mock;
11 import org.mockito.Mockito;
12 import org.mockito.MockitoAnnotations;
13 import org.openecomp.core.utilities.file.FileUtils;
14 import org.openecomp.core.utilities.orchestration.OnboardingTypesEnum;
15 import org.openecomp.sdc.vendorsoftwareproduct.dao.ComponentDao;
16 import org.openecomp.sdc.vendorsoftwareproduct.dao.ComponentDependencyModelDao;
17 import org.openecomp.sdc.vendorsoftwareproduct.dao.OrchestrationTemplateDao;
18 import org.openecomp.sdc.vendorsoftwareproduct.dao.VendorSoftwareProductInfoDao;
19 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComponentDependencyModelEntity;
20 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComponentEntity;
21 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.OrchestrationTemplateCandidateData;
22 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.VspDetails;
23 import org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.OrchestrationUtil;
24 import org.openecomp.sdc.vendorsoftwareproduct.services.filedatastructuremodule.CandidateService;
25 import org.openecomp.sdc.vendorsoftwareproduct.types.OrchestrationTemplateActionResponse;
26 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.ComponentData;
27 import org.openecomp.sdc.vendorsoftwareproduct.utils.ZipFileUtils;
28 import org.openecomp.sdc.versioning.dao.types.Version;
29 import org.testng.Assert;
30 import org.testng.annotations.AfterClass;
31 import org.testng.annotations.AfterMethod;
32 import org.testng.annotations.BeforeClass;
33 import org.testng.annotations.Test;
34
35 import java.nio.ByteBuffer;
36 import java.util.ArrayList;
37 import java.util.Collection;
38 import java.util.HashMap;
39 import java.util.Iterator;
40 import java.util.Map;
41 import java.util.Optional;
42
43 public class OrchestrationTemplateCandidateManagerImplTest {
44   private static final String VSP_ID = "vspId";
45   private static final Version VERSION01 = new Version("versionId");
46
47   private static final String COMPONENT_ORIG_ID_1 = "Component_Pd_Server_Id_Orig";
48   private static final String COMPONENT_ORIG_ID_2 = "Component_Sm_Server_Id_Orig";
49   private static final String COMPONENT_ORIG_ID_3 = "Component_Oam_Server_Id_Orig";
50   private static final String COMPONENT_ORIG_ID_4 = "Component_Ps_Server_Id_Orig";
51
52   private static final String COMPONENT_NEW_ID_1 = "Component_Pd_Server_Id_New";
53   private static final String COMPONENT_NEW_ID_2 = "Component_Sm_Server_Id_New";
54   private static final String COMPONENT_NEW_ID_3 = "Component_Oam_Server_Id_New";
55   private static final String COMPONENT_NEW_ID_4 = "Component_Ps_Server_Id_New";
56
57   private static final String COMPONENT_NAME_1 = "pd_server";
58   private static final String COMPONENT_NAME_2 = "sm_server";
59   private static final String COMPONENT_NAME_3 = "oam_server";
60   private static final String COMPONENT_NAME_4 = "ps_server";
61
62   @Mock
63   private VendorSoftwareProductInfoDao vspInfoDaoMock;
64   @Mock
65   private CandidateService candidateServiceMock;
66   @Mock
67   private OrchestrationTemplateDao orchestrationTemplateDaoMock;
68   @Mock
69   private ComponentDependencyModelDao componentDependencyModelDaoMock;
70   @Mock
71   private ComponentDao componentDaoMock;
72
73   @InjectMocks
74   private OrchestrationTemplateCandidateManagerImpl candidateManager;
75
76   @InjectMocks
77   private OrchestrationUtil orchestrationUtil;
78
79   @BeforeClass
80   public void setUp() throws Exception {
81     MockitoAnnotations.initMocks(this);
82   }
83
84   @AfterMethod
85   private void resetMocks() {
86     Mockito.reset(vspInfoDaoMock);
87     Mockito.reset(candidateServiceMock);
88     Mockito.reset(orchestrationTemplateDaoMock);
89     Mockito.reset(componentDependencyModelDaoMock);
90     Mockito.reset(componentDaoMock);
91   }
92
93   @AfterClass
94   public void tearDown() {
95     candidateManager = null;
96     orchestrationUtil = null;
97   }
98
99   @Test
100   public void testProcessEmptyUpload() {
101     OrchestrationTemplateCandidateData orchTemplate =
102         new OrchestrationTemplateCandidateData();
103     orchTemplate
104         .setContentData(ByteBuffer.wrap(FileUtils.toByteArray(new ZipFileUtils().getZipInputStream
105             ("/vspmanager/zips/emptyComposition.zip"))));
106     orchTemplate.setFilesDataStructure("{\n" +
107         "  \"modules\": [\n" +
108         "    {\n" +
109         "      \"isBase\": false,\n" +
110         "      \"yaml\": \"ep-jsa_net.yaml\"\n" +
111         "    }\n" +
112         "  ]\n" +
113         "}");
114     doReturn(orchTemplate)
115         .when(candidateServiceMock).getOrchestrationTemplateCandidate(any(), any());
116
117     doReturn(new VspDetails(VSP_ID, VERSION01))
118         .when(vspInfoDaoMock).get(any());
119     doReturn(null)
120         .when(orchestrationTemplateDaoMock).getInfo(any(), any());
121
122     doReturn("{}").when(candidateServiceMock).createManifest(any(), any());
123     doReturn(Optional.empty()).when(candidateServiceMock)
124         .fetchZipFileByteArrayInputStream(any(), any(), any(),
125             eq(OnboardingTypesEnum.ZIP), any());
126
127
128     OrchestrationTemplateActionResponse response =
129         candidateManager.process(VSP_ID, VERSION01);
130
131     assertNotNull(response);
132   }
133
134   @Test
135   public void testUpdateVspComponentDependenciesHeatReuploadMoreComponents() {
136     Collection<ComponentDependencyModelEntity> existingComponentsDependencies =
137         getExistingComponentDependencies();
138     Collection<ComponentEntity> componentListWithMoreComponentsInHeat =
139         getComponentListWithMoreComponentsInHeat();
140
141     doReturn(componentListWithMoreComponentsInHeat).when(componentDaoMock).list(any());
142     Map<String, String> componentIdNameInfoBeforeProcess = getVspInitComponentIdNameInfo();
143     orchestrationUtil.updateVspComponentDependencies(VSP_ID, VERSION01,
144         componentIdNameInfoBeforeProcess, existingComponentsDependencies);
145     Mockito.verify(componentDependencyModelDaoMock, Mockito.times(2)).create(any());
146   }
147
148   @Test
149   public void testUpdateVspComponentDependenciesHeatReuploadLessComponents() {
150     Collection<ComponentDependencyModelEntity> existingComponentsDependencies =
151         getExistingComponentDependencies();
152     Collection<ComponentEntity> componentListWithLessComponentsInHeat =
153         getComponentListWithLessComponentsInHeat();
154     doReturn(componentListWithLessComponentsInHeat).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(1)).create(any());
159   }
160
161   @Test
162   public void testUpdateVspComponentDependenciesHeatReuploadSameComponents() {
163     Collection<ComponentDependencyModelEntity> existingComponentsDependencies =
164         getExistingComponentDependencies();
165     Collection<ComponentEntity> componentListWithSameComponentsInHeat =
166         getComponentListWithSameComponentsInHeat();
167     doReturn(componentListWithSameComponentsInHeat).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(2)).create(any());
172   }
173
174   @Test
175   public void testUpdateVspComponentDependenciesHeatReuploadNoComponents() {
176     Collection<ComponentDependencyModelEntity> existingComponentsDependencies =
177         getExistingComponentDependencies();
178     Collection<ComponentEntity> componentListWithMoreComponentsInHeat =
179         new ArrayList<>();
180     doReturn(componentListWithMoreComponentsInHeat).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(0)).create(any());
185   }
186
187   @Test
188   public void testVspComponentIdNameInfoNoComponents() {
189     Collection<ComponentEntity> initialVspComponents = new ArrayList<>();
190     doReturn(initialVspComponents).when(componentDaoMock).list(any());
191     Map<String, String> vspComponentIdNameInfo =
192         orchestrationUtil.getVspComponentIdNameInfo(VSP_ID, VERSION01);
193     Assert.assertEquals(vspComponentIdNameInfo.size(), 0);
194   }
195
196   @Test
197   public void testVspComponentIdNameInfo() {
198     doReturn(getInitialVspComponents()).when(componentDaoMock).list(any());
199     Map<String, String> vspComponentIdNameInfo =
200         orchestrationUtil.getVspComponentIdNameInfo(VSP_ID, VERSION01);
201
202     assertEquals(vspComponentIdNameInfo.size(), 3);
203     assertNotNull(vspComponentIdNameInfo.get(COMPONENT_ORIG_ID_1));
204     assertNotNull(vspComponentIdNameInfo.get(COMPONENT_ORIG_ID_2));
205     assertNotNull(vspComponentIdNameInfo.get(COMPONENT_ORIG_ID_3));
206   }
207
208   @Test
209   public void testVspComponentIdNameInfoEmptyCompositionData() {
210     Collection<ComponentEntity> initialVspComponents = getInitialVspComponents();
211     ComponentEntity componentEntity = new ComponentEntity();
212     componentEntity.setId(COMPONENT_ORIG_ID_4);
213     initialVspComponents.add(componentEntity);
214
215     doReturn(initialVspComponents).when(componentDaoMock).list(any());
216     Map<String, String> vspComponentIdNameInfo =
217         orchestrationUtil.getVspComponentIdNameInfo(VSP_ID, VERSION01);
218
219     assertEquals(vspComponentIdNameInfo.size(), 3);
220     assertNotNull(vspComponentIdNameInfo.get(COMPONENT_ORIG_ID_1));
221     assertNotNull(vspComponentIdNameInfo.get(COMPONENT_ORIG_ID_2));
222     assertNotNull(vspComponentIdNameInfo.get(COMPONENT_ORIG_ID_3));
223     Assert.assertNull(vspComponentIdNameInfo.get(COMPONENT_ORIG_ID_4));
224   }
225
226   @Test
227   public void testGetComponentDependenciesBeforeDeleteInvalid() {
228     Collection<ComponentDependencyModelEntity> componentDependenciesBeforeDelete =
229         orchestrationUtil.getComponentDependenciesBeforeDelete(null, null);
230     Assert.assertEquals(componentDependenciesBeforeDelete.size(), 0);
231   }
232
233   @Test
234   public void testGetComponentDependenciesBeforeDeleteValid() {
235     Collection<ComponentDependencyModelEntity> existingComponentsDependencies =
236         getExistingComponentDependencies();
237     doReturn(existingComponentsDependencies).when(componentDependencyModelDaoMock).list(any());
238     Collection<ComponentDependencyModelEntity> componentDependenciesBeforeDelete =
239         orchestrationUtil.getComponentDependenciesBeforeDelete(VSP_ID, VERSION01);
240     Assert.assertEquals(componentDependenciesBeforeDelete.size(), 2);
241   }
242
243   private Map<String, String> getVspInitComponentIdNameInfo() {
244     Map<String, String> componentIdNameInfoBeforeProcess = new HashMap<>();
245     componentIdNameInfoBeforeProcess.put(COMPONENT_ORIG_ID_1, COMPONENT_NAME_1);
246     componentIdNameInfoBeforeProcess.put(COMPONENT_ORIG_ID_2, COMPONENT_NAME_2);
247     componentIdNameInfoBeforeProcess.put(COMPONENT_ORIG_ID_3, COMPONENT_NAME_3);
248     return componentIdNameInfoBeforeProcess;
249   }
250
251   private Collection<ComponentEntity> getInitialVspComponents() {
252     Collection<ComponentEntity> vspComponents = new ArrayList<>();
253     ComponentEntity component1 = createComponentEntity(COMPONENT_ORIG_ID_1, COMPONENT_NAME_1);
254     ComponentEntity component2 = createComponentEntity(COMPONENT_ORIG_ID_2, COMPONENT_NAME_2);
255     ComponentEntity component3 = createComponentEntity(COMPONENT_ORIG_ID_3, COMPONENT_NAME_3);
256     vspComponents.add(component1);
257     vspComponents.add(component2);
258     vspComponents.add(component3);
259     return vspComponents;
260   }
261
262   private Collection<ComponentEntity> getComponentListWithMoreComponentsInHeat() {
263     Collection<ComponentEntity> vspComponents = getInitialVspComponents();
264     createInitialComponentDependencies(vspComponents);
265     ComponentEntity newComponent = createComponentEntity(COMPONENT_NEW_ID_4, COMPONENT_NAME_4);
266     vspComponents.add(newComponent);
267     return vspComponents;
268   }
269
270   private Collection<ComponentEntity> getComponentListWithLessComponentsInHeat() {
271     Collection<ComponentEntity> vspComponents = getInitialVspComponents();
272     for (Iterator<ComponentEntity> iterator = vspComponents.iterator(); iterator.hasNext(); ) {
273       ComponentEntity componentEntity = iterator.next();
274       switch (componentEntity.getComponentCompositionData().getName()) {
275         case COMPONENT_NAME_1:
276           iterator.remove();
277           break;
278         case COMPONENT_NAME_2:
279           componentEntity.setId(COMPONENT_NEW_ID_2);
280           break;
281         case COMPONENT_NAME_3:
282           componentEntity.setId(COMPONENT_NEW_ID_3);
283           break;
284       }
285     }
286     return vspComponents;
287   }
288
289   private Collection<ComponentEntity> getComponentListWithSameComponentsInHeat() {
290     Collection<ComponentEntity> vspComponents = getInitialVspComponents();
291     createInitialComponentDependencies(vspComponents);
292     return vspComponents;
293   }
294
295   private Collection<ComponentDependencyModelEntity> getExistingComponentDependencies() {
296     Collection<ComponentDependencyModelEntity> newComponents = new ArrayList<>();
297     ComponentDependencyModelEntity entity =
298         createComponentDependencyEntity(COMPONENT_ORIG_ID_1, COMPONENT_ORIG_ID_2);
299     ComponentDependencyModelEntity entity2 =
300         createComponentDependencyEntity(COMPONENT_ORIG_ID_2, COMPONENT_ORIG_ID_3);
301     newComponents.add(entity);
302     newComponents.add(entity2);
303     return newComponents;
304   }
305
306   private ComponentEntity createComponentEntity(String componentId, String componentName) {
307     ComponentEntity componentEntity = new ComponentEntity();
308     componentEntity.setVspId(VSP_ID);
309     componentEntity.setVersion(VERSION01);
310     componentEntity.setId(componentId);
311     ComponentData data = new ComponentData();
312     data.setName(componentName);
313     componentEntity.setComponentCompositionData(data);
314     return componentEntity;
315   }
316
317   private ComponentDependencyModelEntity createComponentDependencyEntity(String sourceComponentId,
318                                                                          String targetComponentId) {
319     ComponentDependencyModelEntity componentDependency = new ComponentDependencyModelEntity();
320     componentDependency.setVspId(VSP_ID);
321     componentDependency.setVersion(VERSION01);
322     componentDependency.setRelation("dependsOn");
323     componentDependency.setSourceComponentId(sourceComponentId);
324     componentDependency.setTargetComponentId(targetComponentId);
325     return componentDependency;
326   }
327
328   private void createInitialComponentDependencies(Collection<ComponentEntity> vspComponents) {
329     for (ComponentEntity componentEntity : vspComponents) {
330       switch (componentEntity.getComponentCompositionData().getName()) {
331         case COMPONENT_NAME_1:
332           componentEntity.setId(COMPONENT_NEW_ID_1);
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   }
343 }