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";
80 private static final String USER1 = "vspTestUser1";
83 private VendorSoftwareProductInfoDao vspInfoDaoMock;
85 private CandidateService candidateServiceMock;
87 private OrchestrationTemplateDao orchestrationTemplateDaoMock;
89 private ComponentDependencyModelDao componentDependencyModelDaoMock;
91 private ComponentDao componentDaoMock;
94 private OrchestrationTemplateCandidateManagerImpl candidateManager;
97 private OrchestrationUtil orchestrationUtil;
100 public void setUp() throws Exception {
101 MockitoAnnotations.initMocks(this);
105 private void resetMocks() {
106 Mockito.reset(vspInfoDaoMock);
107 Mockito.reset(candidateServiceMock);
108 Mockito.reset(orchestrationTemplateDaoMock);
109 Mockito.reset(componentDependencyModelDaoMock);
110 Mockito.reset(componentDaoMock);
114 public void tearDown() {
115 candidateManager = null;
116 orchestrationUtil = null;
120 public void testProcessEmptyUpload() {
121 OrchestrationTemplateCandidateData orchTemplate =
122 new OrchestrationTemplateCandidateData();
124 .setContentData(ByteBuffer.wrap(FileUtils.toByteArray(new ZipFileUtils().getZipInputStream
125 ("/vspmanager/zips/emptyComposition.zip"))));
126 orchTemplate.setFilesDataStructure("{\n" +
127 " \"modules\": [\n" +
129 " \"isBase\": false,\n" +
130 " \"yaml\": \"ep-jsa_net.yaml\"\n" +
134 doReturn(orchTemplate)
135 .when(candidateServiceMock).getOrchestrationTemplateCandidate(any(), any());
137 doReturn(new VspDetails(VSP_ID, VERSION01))
138 .when(vspInfoDaoMock).get(any());
140 .when(orchestrationTemplateDaoMock).getInfo(any(), any());
142 doReturn("{}").when(candidateServiceMock).createManifest(any(), any());
143 doReturn(Optional.empty()).when(candidateServiceMock)
144 .fetchZipFileByteArrayInputStream(any(), any(), any(),
145 eq(OnboardingTypesEnum.ZIP), any());
148 OrchestrationTemplateActionResponse response =
149 candidateManager.process(VSP_ID, VERSION01);
151 assertNotNull(response);
155 public void testUpdateVspComponentDependenciesHeatReuploadMoreComponents() {
156 Collection<ComponentDependencyModelEntity> existingComponentsDependencies =
157 getExistingComponentDependencies();
158 Collection<ComponentEntity> componentListWithMoreComponentsInHeat =
159 getComponentListWithMoreComponentsInHeat();
161 doReturn(componentListWithMoreComponentsInHeat).when(componentDaoMock).list(any());
162 Map<String, String> componentIdNameInfoBeforeProcess = getVspInitComponentIdNameInfo();
163 orchestrationUtil.updateVspComponentDependencies(VSP_ID, VERSION01,
164 componentIdNameInfoBeforeProcess, existingComponentsDependencies);
165 Mockito.verify(componentDependencyModelDaoMock, Mockito.times(2)).create(any());
169 public void testUpdateVspComponentDependenciesHeatReuploadLessComponents() {
170 Collection<ComponentDependencyModelEntity> existingComponentsDependencies =
171 getExistingComponentDependencies();
172 Collection<ComponentEntity> componentListWithLessComponentsInHeat =
173 getComponentListWithLessComponentsInHeat();
174 doReturn(componentListWithLessComponentsInHeat).when(componentDaoMock).list(any());
175 Map<String, String> componentIdNameInfoBeforeProcess = getVspInitComponentIdNameInfo();
176 orchestrationUtil.updateVspComponentDependencies(VSP_ID, VERSION01,
177 componentIdNameInfoBeforeProcess, existingComponentsDependencies);
178 Mockito.verify(componentDependencyModelDaoMock, Mockito.times(1)).create(any());
182 public void testUpdateVspComponentDependenciesHeatReuploadSameComponents() {
183 Collection<ComponentDependencyModelEntity> existingComponentsDependencies =
184 getExistingComponentDependencies();
185 Collection<ComponentEntity> componentListWithSameComponentsInHeat =
186 getComponentListWithSameComponentsInHeat();
187 doReturn(componentListWithSameComponentsInHeat).when(componentDaoMock).list(any());
188 Map<String, String> componentIdNameInfoBeforeProcess = getVspInitComponentIdNameInfo();
189 orchestrationUtil.updateVspComponentDependencies(VSP_ID, VERSION01,
190 componentIdNameInfoBeforeProcess, existingComponentsDependencies);
191 Mockito.verify(componentDependencyModelDaoMock, Mockito.times(2)).create(any());
195 public void testUpdateVspComponentDependenciesHeatReuploadNoComponents() {
196 Collection<ComponentDependencyModelEntity> existingComponentsDependencies =
197 getExistingComponentDependencies();
198 Collection<ComponentEntity> componentListWithMoreComponentsInHeat =
200 doReturn(componentListWithMoreComponentsInHeat).when(componentDaoMock).list(any());
201 Map<String, String> componentIdNameInfoBeforeProcess = getVspInitComponentIdNameInfo();
202 orchestrationUtil.updateVspComponentDependencies(VSP_ID, VERSION01,
203 componentIdNameInfoBeforeProcess, existingComponentsDependencies);
204 Mockito.verify(componentDependencyModelDaoMock, Mockito.times(0)).create(any());
208 public void testVspComponentIdNameInfoNoComponents() {
209 Collection<ComponentEntity> initialVspComponents = new ArrayList<>();
210 doReturn(initialVspComponents).when(componentDaoMock).list(any());
211 Map<String, String> vspComponentIdNameInfo =
212 orchestrationUtil.getVspComponentIdNameInfo(VSP_ID, VERSION01);
213 Assert.assertEquals(vspComponentIdNameInfo.size(), 0);
217 public void testVspComponentIdNameInfo() {
218 doReturn(getInitialVspComponents()).when(componentDaoMock).list(any());
219 Map<String, String> vspComponentIdNameInfo =
220 orchestrationUtil.getVspComponentIdNameInfo(VSP_ID, VERSION01);
222 assertEquals(vspComponentIdNameInfo.size(), 3);
223 assertNotNull(vspComponentIdNameInfo.get(COMPONENT_ORIG_ID_1));
224 assertNotNull(vspComponentIdNameInfo.get(COMPONENT_ORIG_ID_2));
225 assertNotNull(vspComponentIdNameInfo.get(COMPONENT_ORIG_ID_3));
229 public void testVspComponentIdNameInfoEmptyCompositionData() {
230 Collection<ComponentEntity> initialVspComponents = getInitialVspComponents();
231 ComponentEntity componentEntity = new ComponentEntity();
232 componentEntity.setId(COMPONENT_ORIG_ID_4);
233 initialVspComponents.add(componentEntity);
235 doReturn(initialVspComponents).when(componentDaoMock).list(any());
236 Map<String, String> vspComponentIdNameInfo =
237 orchestrationUtil.getVspComponentIdNameInfo(VSP_ID, VERSION01);
239 assertEquals(vspComponentIdNameInfo.size(), 3);
240 assertNotNull(vspComponentIdNameInfo.get(COMPONENT_ORIG_ID_1));
241 assertNotNull(vspComponentIdNameInfo.get(COMPONENT_ORIG_ID_2));
242 assertNotNull(vspComponentIdNameInfo.get(COMPONENT_ORIG_ID_3));
243 Assert.assertNull(vspComponentIdNameInfo.get(COMPONENT_ORIG_ID_4));
247 public void testGetComponentDependenciesBeforeDeleteInvalid() {
248 Collection<ComponentDependencyModelEntity> componentDependenciesBeforeDelete =
249 orchestrationUtil.getComponentDependenciesBeforeDelete(null, null);
250 Assert.assertEquals(componentDependenciesBeforeDelete.size(), 0);
254 public void testGetComponentDependenciesBeforeDeleteValid() {
255 Collection<ComponentDependencyModelEntity> existingComponentsDependencies =
256 getExistingComponentDependencies();
257 doReturn(existingComponentsDependencies).when(componentDependencyModelDaoMock).list(any());
258 Collection<ComponentDependencyModelEntity> componentDependenciesBeforeDelete =
259 orchestrationUtil.getComponentDependenciesBeforeDelete(VSP_ID, VERSION01);
260 Assert.assertEquals(componentDependenciesBeforeDelete.size(), 2);
264 public void testGetFileDataStructure() {
265 Optional<String> jsonFileDataStructure = Optional.of(new String("{\n" +
266 " \"modules\": [\n" +
268 " \"yaml\": \"hot-mog-0108-bs1271.yml\",\n" +
269 " \"env\": \"hot-mog-0108-bs1271.env\"\n" +
272 " \"unassigned\": [],\n" +
273 " \"artifacts\": [],\n" +
274 " \"nested\": []\n" +
276 Optional<FilesDataStructure> filesDataStructureOptional = Optional.of(JsonUtil.json2Object
277 (jsonFileDataStructure.get(), FilesDataStructure.class));
278 doReturn(filesDataStructureOptional).when(candidateServiceMock)
279 .getOrchestrationTemplateCandidateFileDataStructure(VSP_ID, VERSION01);
280 Optional<FilesDataStructure> filesDataStructure = candidateManager.getFilesDataStructure(VSP_ID,
282 assertNotNull(filesDataStructure);
283 assertEquals(filesDataStructureOptional.get().getModules().size(), filesDataStructure.get()
284 .getModules().size());
285 assertEquals(filesDataStructureOptional.get().getModules().get(0).getName(),
286 filesDataStructure.get().getModules().get(0).getName());
290 public void testAbort() {
292 Mockito.doNothing().when(candidateServiceMock).deleteOrchestrationTemplateCandidate(VSP_ID,
294 candidateManager.abort(VSP_ID, VERSION01);
296 Mockito.verify(candidateServiceMock, Mockito.times(1)).deleteOrchestrationTemplateCandidate
300 private Map<String, String> getVspInitComponentIdNameInfo() {
301 Map<String, String> componentIdNameInfoBeforeProcess = new HashMap<>();
302 componentIdNameInfoBeforeProcess.put(COMPONENT_ORIG_ID_1, COMPONENT_NAME_1);
303 componentIdNameInfoBeforeProcess.put(COMPONENT_ORIG_ID_2, COMPONENT_NAME_2);
304 componentIdNameInfoBeforeProcess.put(COMPONENT_ORIG_ID_3, COMPONENT_NAME_3);
305 return componentIdNameInfoBeforeProcess;
308 private Collection<ComponentEntity> getInitialVspComponents() {
309 Collection<ComponentEntity> vspComponents = new ArrayList<>();
310 ComponentEntity component1 = createComponentEntity(COMPONENT_ORIG_ID_1, COMPONENT_NAME_1);
311 ComponentEntity component2 = createComponentEntity(COMPONENT_ORIG_ID_2, COMPONENT_NAME_2);
312 ComponentEntity component3 = createComponentEntity(COMPONENT_ORIG_ID_3, COMPONENT_NAME_3);
313 vspComponents.add(component1);
314 vspComponents.add(component2);
315 vspComponents.add(component3);
316 return vspComponents;
319 private Collection<ComponentEntity> getComponentListWithMoreComponentsInHeat() {
320 Collection<ComponentEntity> vspComponents = getInitialVspComponents();
321 createInitialComponentDependencies(vspComponents);
322 ComponentEntity newComponent = createComponentEntity(COMPONENT_NEW_ID_4, COMPONENT_NAME_4);
323 vspComponents.add(newComponent);
324 return vspComponents;
327 private Collection<ComponentEntity> getComponentListWithLessComponentsInHeat() {
328 Collection<ComponentEntity> vspComponents = getInitialVspComponents();
329 for (Iterator<ComponentEntity> iterator = vspComponents.iterator(); iterator.hasNext(); ) {
330 ComponentEntity componentEntity = iterator.next();
331 switch (componentEntity.getComponentCompositionData().getName()) {
332 case COMPONENT_NAME_1:
335 case COMPONENT_NAME_2:
336 componentEntity.setId(COMPONENT_NEW_ID_2);
338 case COMPONENT_NAME_3:
339 componentEntity.setId(COMPONENT_NEW_ID_3);
343 return vspComponents;
346 private Collection<ComponentEntity> getComponentListWithSameComponentsInHeat() {
347 Collection<ComponentEntity> vspComponents = getInitialVspComponents();
348 createInitialComponentDependencies(vspComponents);
349 return vspComponents;
352 private Collection<ComponentDependencyModelEntity> getExistingComponentDependencies() {
353 Collection<ComponentDependencyModelEntity> newComponents = new ArrayList<>();
354 ComponentDependencyModelEntity entity =
355 createComponentDependencyEntity(COMPONENT_ORIG_ID_1, COMPONENT_ORIG_ID_2);
356 ComponentDependencyModelEntity entity2 =
357 createComponentDependencyEntity(COMPONENT_ORIG_ID_2, COMPONENT_ORIG_ID_3);
358 newComponents.add(entity);
359 newComponents.add(entity2);
360 return newComponents;
363 private ComponentEntity createComponentEntity(String componentId, String componentName) {
364 ComponentEntity componentEntity = new ComponentEntity();
365 componentEntity.setVspId(VSP_ID);
366 componentEntity.setVersion(VERSION01);
367 componentEntity.setId(componentId);
368 ComponentData data = new ComponentData();
369 data.setName(componentName);
370 componentEntity.setComponentCompositionData(data);
371 return componentEntity;
374 private ComponentDependencyModelEntity createComponentDependencyEntity(String sourceComponentId,
375 String targetComponentId) {
376 ComponentDependencyModelEntity componentDependency = new ComponentDependencyModelEntity();
377 componentDependency.setVspId(VSP_ID);
378 componentDependency.setVersion(VERSION01);
379 componentDependency.setRelation("dependsOn");
380 componentDependency.setSourceComponentId(sourceComponentId);
381 componentDependency.setTargetComponentId(targetComponentId);
382 return componentDependency;
385 private void createInitialComponentDependencies(Collection<ComponentEntity> vspComponents) {
386 for (ComponentEntity componentEntity : vspComponents) {
387 switch (componentEntity.getComponentCompositionData().getName()) {
388 case COMPONENT_NAME_1:
389 componentEntity.setId(COMPONENT_NEW_ID_1);
391 case COMPONENT_NAME_2:
392 componentEntity.setId(COMPONENT_NEW_ID_2);
394 case COMPONENT_NAME_3:
395 componentEntity.setId(COMPONENT_NEW_ID_3);