2 * Copyright © 2016-2018 European Support Limited
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.openecomp.sdc.vendorsoftwareproduct.impl;
19 import static org.mockito.Matchers.any;
20 import static org.mockito.Matchers.eq;
21 import static org.mockito.Mockito.doReturn;
22 import static org.testng.Assert.assertEquals;
23 import static org.testng.Assert.assertNotNull;
25 import org.mockito.InjectMocks;
26 import org.mockito.Mock;
27 import org.mockito.Mockito;
28 import org.mockito.MockitoAnnotations;
29 import org.openecomp.core.utilities.file.FileUtils;
30 import org.openecomp.core.utilities.json.JsonUtil;
31 import org.openecomp.core.utilities.orchestration.OnboardingTypesEnum;
32 import org.openecomp.sdc.vendorsoftwareproduct.dao.ComponentDao;
33 import org.openecomp.sdc.vendorsoftwareproduct.dao.ComponentDependencyModelDao;
34 import org.openecomp.sdc.vendorsoftwareproduct.dao.OrchestrationTemplateDao;
35 import org.openecomp.sdc.vendorsoftwareproduct.dao.VendorSoftwareProductInfoDao;
36 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComponentDependencyModelEntity;
37 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComponentEntity;
38 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.OrchestrationTemplateCandidateData;
39 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.VspDetails;
40 import org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.OrchestrationUtil;
41 import org.openecomp.sdc.vendorsoftwareproduct.services.filedatastructuremodule.CandidateService;
42 import org.openecomp.sdc.vendorsoftwareproduct.types.OrchestrationTemplateActionResponse;
43 import org.openecomp.sdc.vendorsoftwareproduct.types.candidateheat.FilesDataStructure;
44 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.ComponentData;
45 import org.openecomp.sdc.vendorsoftwareproduct.utils.ZipFileUtils;
46 import org.openecomp.sdc.versioning.dao.types.Version;
47 import org.testng.Assert;
48 import org.testng.annotations.AfterClass;
49 import org.testng.annotations.AfterMethod;
50 import org.testng.annotations.BeforeClass;
51 import org.testng.annotations.Test;
53 import java.nio.ByteBuffer;
54 import java.util.ArrayList;
55 import java.util.Collection;
56 import java.util.HashMap;
57 import java.util.Iterator;
59 import java.util.Optional;
61 public class OrchestrationTemplateCandidateManagerImplTest {
62 private static final String VSP_ID = "vspId";
63 private static final Version VERSION01 = new Version("versionId");
65 private static final String COMPONENT_ORIG_ID_1 = "Component_Pd_Server_Id_Orig";
66 private static final String COMPONENT_ORIG_ID_2 = "Component_Sm_Server_Id_Orig";
67 private static final String COMPONENT_ORIG_ID_3 = "Component_Oam_Server_Id_Orig";
68 private static final String COMPONENT_ORIG_ID_4 = "Component_Ps_Server_Id_Orig";
70 private static final String COMPONENT_NEW_ID_1 = "Component_Pd_Server_Id_New";
71 private static final String COMPONENT_NEW_ID_2 = "Component_Sm_Server_Id_New";
72 private static final String COMPONENT_NEW_ID_3 = "Component_Oam_Server_Id_New";
73 private static final String COMPONENT_NEW_ID_4 = "Component_Ps_Server_Id_New";
75 private static final String COMPONENT_NAME_1 = "pd_server";
76 private static final String COMPONENT_NAME_2 = "sm_server";
77 private static final String COMPONENT_NAME_3 = "oam_server";
78 private static final String COMPONENT_NAME_4 = "ps_server";
81 private VendorSoftwareProductInfoDao vspInfoDaoMock;
83 private CandidateService candidateServiceMock;
85 private OrchestrationTemplateDao orchestrationTemplateDaoMock;
87 private ComponentDependencyModelDao componentDependencyModelDaoMock;
89 private ComponentDao componentDaoMock;
92 private OrchestrationTemplateCandidateManagerImpl candidateManager;
95 private OrchestrationUtil orchestrationUtil;
98 public void setUp() throws Exception {
99 MockitoAnnotations.initMocks(this);
103 private void resetMocks() {
104 Mockito.reset(vspInfoDaoMock);
105 Mockito.reset(candidateServiceMock);
106 Mockito.reset(orchestrationTemplateDaoMock);
107 Mockito.reset(componentDependencyModelDaoMock);
108 Mockito.reset(componentDaoMock);
112 public void tearDown() {
113 candidateManager = null;
114 orchestrationUtil = null;
118 public void testProcessEmptyUpload() {
119 OrchestrationTemplateCandidateData orchTemplate =
120 new OrchestrationTemplateCandidateData();
122 .setContentData(ByteBuffer.wrap(FileUtils.toByteArray(new ZipFileUtils().getZipInputStream
123 ("/vspmanager/zips/emptyComposition.zip"))));
124 orchTemplate.setFilesDataStructure("{\n" +
125 " \"modules\": [\n" +
127 " \"isBase\": false,\n" +
128 " \"yaml\": \"ep-jsa_net.yaml\"\n" +
132 doReturn(orchTemplate)
133 .when(candidateServiceMock).getOrchestrationTemplateCandidate(any(), any());
135 doReturn(new VspDetails(VSP_ID, VERSION01))
136 .when(vspInfoDaoMock).get(any());
138 .when(orchestrationTemplateDaoMock).getInfo(any(), any());
140 doReturn("{}").when(candidateServiceMock).createManifest(any(), any());
141 doReturn(Optional.empty()).when(candidateServiceMock)
142 .fetchZipFileByteArrayInputStream(any(), any(), any(),
143 eq(OnboardingTypesEnum.ZIP), any());
146 OrchestrationTemplateActionResponse response =
147 candidateManager.process(VSP_ID, VERSION01);
149 assertNotNull(response);
153 public void testUpdateVspComponentDependenciesHeatReuploadMoreComponents() {
154 Collection<ComponentDependencyModelEntity> existingComponentsDependencies =
155 getExistingComponentDependencies();
156 Collection<ComponentEntity> componentListWithMoreComponentsInHeat =
157 getComponentListWithMoreComponentsInHeat();
159 doReturn(componentListWithMoreComponentsInHeat).when(componentDaoMock).list(any());
160 Map<String, String> componentIdNameInfoBeforeProcess = getVspInitComponentIdNameInfo();
161 orchestrationUtil.updateVspComponentDependencies(VSP_ID, VERSION01,
162 componentIdNameInfoBeforeProcess, existingComponentsDependencies);
163 Mockito.verify(componentDependencyModelDaoMock, Mockito.times(2)).create(any());
167 public void testUpdateVspComponentDependenciesHeatReuploadLessComponents() {
168 Collection<ComponentDependencyModelEntity> existingComponentsDependencies =
169 getExistingComponentDependencies();
170 Collection<ComponentEntity> componentListWithLessComponentsInHeat =
171 getComponentListWithLessComponentsInHeat();
172 doReturn(componentListWithLessComponentsInHeat).when(componentDaoMock).list(any());
173 Map<String, String> componentIdNameInfoBeforeProcess = getVspInitComponentIdNameInfo();
174 orchestrationUtil.updateVspComponentDependencies(VSP_ID, VERSION01,
175 componentIdNameInfoBeforeProcess, existingComponentsDependencies);
176 Mockito.verify(componentDependencyModelDaoMock, Mockito.times(1)).create(any());
180 public void testUpdateVspComponentDependenciesHeatReuploadSameComponents() {
181 Collection<ComponentDependencyModelEntity> existingComponentsDependencies =
182 getExistingComponentDependencies();
183 Collection<ComponentEntity> componentListWithSameComponentsInHeat =
184 getComponentListWithSameComponentsInHeat();
185 doReturn(componentListWithSameComponentsInHeat).when(componentDaoMock).list(any());
186 Map<String, String> componentIdNameInfoBeforeProcess = getVspInitComponentIdNameInfo();
187 orchestrationUtil.updateVspComponentDependencies(VSP_ID, VERSION01,
188 componentIdNameInfoBeforeProcess, existingComponentsDependencies);
189 Mockito.verify(componentDependencyModelDaoMock, Mockito.times(2)).create(any());
193 public void testUpdateVspComponentDependenciesHeatReuploadNoComponents() {
194 Collection<ComponentDependencyModelEntity> existingComponentsDependencies =
195 getExistingComponentDependencies();
196 Collection<ComponentEntity> componentListWithMoreComponentsInHeat =
198 doReturn(componentListWithMoreComponentsInHeat).when(componentDaoMock).list(any());
199 Map<String, String> componentIdNameInfoBeforeProcess = getVspInitComponentIdNameInfo();
200 orchestrationUtil.updateVspComponentDependencies(VSP_ID, VERSION01,
201 componentIdNameInfoBeforeProcess, existingComponentsDependencies);
202 Mockito.verify(componentDependencyModelDaoMock, Mockito.times(0)).create(any());
206 public void testVspComponentIdNameInfoNoComponents() {
207 Collection<ComponentEntity> initialVspComponents = new ArrayList<>();
208 doReturn(initialVspComponents).when(componentDaoMock).list(any());
209 Map<String, String> vspComponentIdNameInfo =
210 orchestrationUtil.getVspComponentIdNameInfo(VSP_ID, VERSION01);
211 Assert.assertEquals(vspComponentIdNameInfo.size(), 0);
215 public void testVspComponentIdNameInfo() {
216 doReturn(getInitialVspComponents()).when(componentDaoMock).list(any());
217 Map<String, String> vspComponentIdNameInfo =
218 orchestrationUtil.getVspComponentIdNameInfo(VSP_ID, VERSION01);
220 assertEquals(vspComponentIdNameInfo.size(), 3);
221 assertNotNull(vspComponentIdNameInfo.get(COMPONENT_ORIG_ID_1));
222 assertNotNull(vspComponentIdNameInfo.get(COMPONENT_ORIG_ID_2));
223 assertNotNull(vspComponentIdNameInfo.get(COMPONENT_ORIG_ID_3));
227 public void testVspComponentIdNameInfoEmptyCompositionData() {
228 Collection<ComponentEntity> initialVspComponents = getInitialVspComponents();
229 ComponentEntity componentEntity = new ComponentEntity();
230 componentEntity.setId(COMPONENT_ORIG_ID_4);
231 initialVspComponents.add(componentEntity);
233 doReturn(initialVspComponents).when(componentDaoMock).list(any());
234 Map<String, String> vspComponentIdNameInfo =
235 orchestrationUtil.getVspComponentIdNameInfo(VSP_ID, VERSION01);
237 assertEquals(vspComponentIdNameInfo.size(), 3);
238 assertNotNull(vspComponentIdNameInfo.get(COMPONENT_ORIG_ID_1));
239 assertNotNull(vspComponentIdNameInfo.get(COMPONENT_ORIG_ID_2));
240 assertNotNull(vspComponentIdNameInfo.get(COMPONENT_ORIG_ID_3));
241 Assert.assertNull(vspComponentIdNameInfo.get(COMPONENT_ORIG_ID_4));
245 public void testGetComponentDependenciesBeforeDeleteInvalid() {
246 Collection<ComponentDependencyModelEntity> componentDependenciesBeforeDelete =
247 orchestrationUtil.getComponentDependenciesBeforeDelete(null, null);
248 Assert.assertEquals(componentDependenciesBeforeDelete.size(), 0);
252 public void testGetComponentDependenciesBeforeDeleteValid() {
253 Collection<ComponentDependencyModelEntity> existingComponentsDependencies =
254 getExistingComponentDependencies();
255 doReturn(existingComponentsDependencies).when(componentDependencyModelDaoMock).list(any());
256 Collection<ComponentDependencyModelEntity> componentDependenciesBeforeDelete =
257 orchestrationUtil.getComponentDependenciesBeforeDelete(VSP_ID, VERSION01);
258 Assert.assertEquals(componentDependenciesBeforeDelete.size(), 2);
262 public void testGetFileDataStructure() {
263 Optional<String> jsonFileDataStructure = Optional.of(new String("{\n" +
264 " \"modules\": [\n" +
266 " \"yaml\": \"hot-mog-0108-bs1271.yml\",\n" +
267 " \"env\": \"hot-mog-0108-bs1271.env\"\n" +
270 " \"unassigned\": [],\n" +
271 " \"artifacts\": [],\n" +
272 " \"nested\": []\n" +
274 Optional<FilesDataStructure> filesDataStructureOptional = Optional.of(JsonUtil.json2Object
275 (jsonFileDataStructure.get(), FilesDataStructure.class));
276 doReturn(filesDataStructureOptional).when(candidateServiceMock)
277 .getOrchestrationTemplateCandidateFileDataStructure(VSP_ID, VERSION01);
278 Optional<FilesDataStructure> filesDataStructure = candidateManager.getFilesDataStructure(VSP_ID,
280 assertNotNull(filesDataStructure);
281 assertEquals(filesDataStructureOptional.get().getModules().size(), filesDataStructure.get()
282 .getModules().size());
283 assertEquals(filesDataStructureOptional.get().getModules().get(0).getName(),
284 filesDataStructure.get().getModules().get(0).getName());
288 public void testAbort() {
290 Mockito.doNothing().when(candidateServiceMock).deleteOrchestrationTemplateCandidate(VSP_ID,
292 candidateManager.abort(VSP_ID, VERSION01);
294 Mockito.verify(candidateServiceMock, Mockito.times(1)).deleteOrchestrationTemplateCandidate
298 private Map<String, String> getVspInitComponentIdNameInfo() {
299 Map<String, String> componentIdNameInfoBeforeProcess = new HashMap<>();
300 componentIdNameInfoBeforeProcess.put(COMPONENT_ORIG_ID_1, COMPONENT_NAME_1);
301 componentIdNameInfoBeforeProcess.put(COMPONENT_ORIG_ID_2, COMPONENT_NAME_2);
302 componentIdNameInfoBeforeProcess.put(COMPONENT_ORIG_ID_3, COMPONENT_NAME_3);
303 return componentIdNameInfoBeforeProcess;
306 private Collection<ComponentEntity> getInitialVspComponents() {
307 Collection<ComponentEntity> vspComponents = new ArrayList<>();
308 ComponentEntity component1 = createComponentEntity(COMPONENT_ORIG_ID_1, COMPONENT_NAME_1);
309 ComponentEntity component2 = createComponentEntity(COMPONENT_ORIG_ID_2, COMPONENT_NAME_2);
310 ComponentEntity component3 = createComponentEntity(COMPONENT_ORIG_ID_3, COMPONENT_NAME_3);
311 vspComponents.add(component1);
312 vspComponents.add(component2);
313 vspComponents.add(component3);
314 return vspComponents;
317 private Collection<ComponentEntity> getComponentListWithMoreComponentsInHeat() {
318 Collection<ComponentEntity> vspComponents = getInitialVspComponents();
319 createInitialComponentDependencies(vspComponents);
320 ComponentEntity newComponent = createComponentEntity(COMPONENT_NEW_ID_4, COMPONENT_NAME_4);
321 vspComponents.add(newComponent);
322 return vspComponents;
325 private Collection<ComponentEntity> getComponentListWithLessComponentsInHeat() {
326 Collection<ComponentEntity> vspComponents = getInitialVspComponents();
327 for (Iterator<ComponentEntity> iterator = vspComponents.iterator(); iterator.hasNext(); ) {
328 ComponentEntity componentEntity = iterator.next();
329 switch (componentEntity.getComponentCompositionData().getName()) {
330 case COMPONENT_NAME_1:
333 case COMPONENT_NAME_2:
334 componentEntity.setId(COMPONENT_NEW_ID_2);
336 case COMPONENT_NAME_3:
337 componentEntity.setId(COMPONENT_NEW_ID_3);
341 return vspComponents;
344 private Collection<ComponentEntity> getComponentListWithSameComponentsInHeat() {
345 Collection<ComponentEntity> vspComponents = getInitialVspComponents();
346 createInitialComponentDependencies(vspComponents);
347 return vspComponents;
350 private Collection<ComponentDependencyModelEntity> getExistingComponentDependencies() {
351 Collection<ComponentDependencyModelEntity> newComponents = new ArrayList<>();
352 ComponentDependencyModelEntity entity =
353 createComponentDependencyEntity(COMPONENT_ORIG_ID_1, COMPONENT_ORIG_ID_2);
354 ComponentDependencyModelEntity entity2 =
355 createComponentDependencyEntity(COMPONENT_ORIG_ID_2, COMPONENT_ORIG_ID_3);
356 newComponents.add(entity);
357 newComponents.add(entity2);
358 return newComponents;
361 private ComponentEntity createComponentEntity(String componentId, String componentName) {
362 ComponentEntity componentEntity = new ComponentEntity();
363 componentEntity.setVspId(VSP_ID);
364 componentEntity.setVersion(VERSION01);
365 componentEntity.setId(componentId);
366 ComponentData data = new ComponentData();
367 data.setName(componentName);
368 componentEntity.setComponentCompositionData(data);
369 return componentEntity;
372 private ComponentDependencyModelEntity createComponentDependencyEntity(String sourceComponentId,
373 String targetComponentId) {
374 ComponentDependencyModelEntity componentDependency = new ComponentDependencyModelEntity();
375 componentDependency.setVspId(VSP_ID);
376 componentDependency.setVersion(VERSION01);
377 componentDependency.setRelation("dependsOn");
378 componentDependency.setSourceComponentId(sourceComponentId);
379 componentDependency.setTargetComponentId(targetComponentId);
380 return componentDependency;
383 private void createInitialComponentDependencies(Collection<ComponentEntity> vspComponents) {
384 for (ComponentEntity componentEntity : vspComponents) {
385 switch (componentEntity.getComponentCompositionData().getName()) {
386 case COMPONENT_NAME_1:
387 componentEntity.setId(COMPONENT_NEW_ID_1);
389 case COMPONENT_NAME_2:
390 componentEntity.setId(COMPONENT_NEW_ID_2);
392 case COMPONENT_NAME_3:
393 componentEntity.setId(COMPONENT_NEW_ID_3);