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