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