1 package org.openecomp.sdc.vendorsoftwareproduct.impl;
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;
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;
33 import java.util.Optional;
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;
44 public class OrchestrationTemplateCandidateManagerImplTest {
45 private static final String VSP_ID = "vspId";
46 private static final Version VERSION01 = new Version("versionId");
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";
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";
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";
64 private VendorSoftwareProductInfoDao vspInfoDaoMock;
66 private CandidateService candidateServiceMock;
68 private OrchestrationTemplateDao orchestrationTemplateDaoMock;
70 private ComponentDependencyModelDao componentDependencyModelDaoMock;
72 private ComponentDao componentDaoMock;
75 private OrchestrationTemplateCandidateManagerImpl candidateManager;
78 private OrchestrationUtil orchestrationUtil;
81 public void setUp() throws Exception {
82 MockitoAnnotations.initMocks(this);
86 public void tearDown() {
87 candidateManager = null;
88 orchestrationUtil = null;
92 public void testProcessEmptyUpload() {
93 OrchestrationTemplateCandidateData orchTemplate =
94 new OrchestrationTemplateCandidateData();
96 .setContentData(ByteBuffer.wrap(FileUtils.toByteArray(new ZipFileUtils().getZipInputStream
97 ("/vspmanager/zips/emptyComposition.zip"))));
98 orchTemplate.setFilesDataStructure("{\n" +
101 " \"isBase\": false,\n" +
102 " \"yaml\": \"ep-jsa_net.yaml\"\n" +
106 doReturn(orchTemplate)
107 .when(candidateServiceMock).getOrchestrationTemplateCandidate(anyObject(), anyObject());
109 doReturn(new VspDetails(VSP_ID, VERSION01))
110 .when(vspInfoDaoMock).get(anyObject());
112 .when(orchestrationTemplateDaoMock).getInfo(anyObject(), anyObject());
114 doReturn("{}").when(candidateServiceMock).createManifest(anyObject(), anyObject());
115 doReturn(Optional.empty()).when(candidateServiceMock)
116 .fetchZipFileByteArrayInputStream(anyObject(), anyObject(), anyObject(),
117 eq(OnboardingTypesEnum.ZIP), anyObject());
120 OrchestrationTemplateActionResponse response =
121 candidateManager.process(VSP_ID, VERSION01);
123 assertNotNull(response);
127 public void testUpdateVspComponentDependenciesHeatReuploadMoreComponents() {
128 doReturn(getExistingComponentDependencies())
129 .when(componentDependencyModelDaoMock).list(anyObject());
130 doReturn(getComponentListWithMoreComponentsInHeat())
131 .when(componentDaoMock).list(anyObject());
134 .updateVspComponentDependencies(VSP_ID, VERSION01, getVspInitComponentIdNameInfo());
136 verify(componentDependencyModelDaoMock, times(2)).update(anyObject());
137 verify(componentDependencyModelDaoMock, never()).delete(anyObject());
141 public void testUpdateVspComponentDependenciesHeatReuploadLessComponents() {
142 doReturn(getExistingComponentDependencies())
143 .when(componentDependencyModelDaoMock).list(anyObject());
144 doReturn(getComponentListWithLessComponentsInHeat()).when(componentDaoMock).list(anyObject());
146 orchestrationUtil.updateVspComponentDependencies(VSP_ID, VERSION01,
147 getVspInitComponentIdNameInfo());
149 verify(componentDependencyModelDaoMock).update(anyObject());
150 verify(componentDependencyModelDaoMock).delete(anyObject());
154 public void testUpdateVspComponentDependenciesHeatReuploadSameComponents() {
155 doReturn(getExistingComponentDependencies())
156 .when(componentDependencyModelDaoMock).list(anyObject());
157 doReturn(getComponentListWithSameComponentsInHeat()).when(componentDaoMock).list(anyObject());
160 .updateVspComponentDependencies(VSP_ID, VERSION01, getVspInitComponentIdNameInfo());
162 verify(componentDependencyModelDaoMock, times(2)).update(anyObject());
163 verify(componentDependencyModelDaoMock, never()).delete(anyObject());
167 public void testUpdateVspComponentDependenciesHeatReuploadNoComponents() {
168 doReturn(getExistingComponentDependencies())
169 .when(componentDependencyModelDaoMock).list(anyObject());
170 doReturn(new ArrayList<>()).when(componentDaoMock).list(anyObject());
173 .updateVspComponentDependencies(VSP_ID, VERSION01, getVspInitComponentIdNameInfo());
175 verify(componentDependencyModelDaoMock, never()).update(anyObject());
176 verify(componentDependencyModelDaoMock, never()).delete(anyObject());
180 public void testVspComponentIdNameInfoNoComponents() {
181 Map<String, String> vspComponentIdNameInfo =
182 orchestrationUtil.getVspComponentIdNameInfo(VSP_ID, VERSION01);
184 assertEquals(vspComponentIdNameInfo.size(), 0);
188 public void testVspComponentIdNameInfo() {
189 doReturn(getInitialVspComponents()).when(componentDaoMock).list(anyObject());
191 Map<String, String> vspComponentIdNameInfo =
192 orchestrationUtil.getVspComponentIdNameInfo(VSP_ID, VERSION01);
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));
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);
207 doReturn(initialVspComponents).when(componentDaoMock).list(anyObject());
209 Map<String, String> vspComponentIdNameInfo =
210 orchestrationUtil.getVspComponentIdNameInfo(VSP_ID, VERSION01);
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));
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;
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;
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;
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:
254 case COMPONENT_NAME_2:
255 componentEntity.setId(COMPONENT_NEW_ID_2);
257 case COMPONENT_NAME_3:
258 componentEntity.setId(COMPONENT_NEW_ID_3);
262 return vspComponents;
265 private Collection<ComponentEntity> getComponentListWithSameComponentsInHeat() {
266 Collection<ComponentEntity> vspComponents = getInitialVspComponents();
267 createInitialComponentDependencies(vspComponents);
268 return vspComponents;
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;
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;
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;
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);
305 case COMPONENT_NAME_2:
306 componentEntity.setId(COMPONENT_NEW_ID_2);
308 case COMPONENT_NAME_3:
309 componentEntity.setId(COMPONENT_NEW_ID_3);