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