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 org.mockito.InjectMocks;
20 import org.mockito.Mock;
21 import org.mockito.Mockito;
22 import org.mockito.MockitoAnnotations;
23 import org.openecomp.core.utilities.file.FileUtils;
24 import org.openecomp.core.utilities.json.JsonUtil;
25 import org.openecomp.core.utilities.orchestration.OnboardingTypesEnum;
26 import org.openecomp.sdc.vendorsoftwareproduct.dao.ComponentDao;
27 import org.openecomp.sdc.vendorsoftwareproduct.dao.ComponentDependencyModelDao;
28 import org.openecomp.sdc.vendorsoftwareproduct.dao.OrchestrationTemplateDao;
29 import org.openecomp.sdc.vendorsoftwareproduct.dao.VendorSoftwareProductInfoDao;
30 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComponentDependencyModelEntity;
31 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComponentEntity;
32 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.OrchestrationTemplateCandidateData;
33 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.VspDetails;
34 import org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.OrchestrationUtil;
35 import org.openecomp.sdc.vendorsoftwareproduct.services.filedatastructuremodule.CandidateService;
36 import org.openecomp.sdc.vendorsoftwareproduct.types.OrchestrationTemplateActionResponse;
37 import org.openecomp.sdc.vendorsoftwareproduct.types.candidateheat.FilesDataStructure;
38 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.ComponentData;
39 import org.openecomp.sdc.vendorsoftwareproduct.utils.ZipFileUtils;
40 import org.openecomp.sdc.versioning.dao.types.Version;
41 import org.testng.Assert;
42 import org.testng.annotations.AfterClass;
43 import org.testng.annotations.AfterMethod;
44 import org.testng.annotations.BeforeClass;
45 import org.testng.annotations.Test;
47 import java.nio.ByteBuffer;
50 import static org.mockito.Matchers.any;
51 import static org.mockito.Matchers.eq;
52 import static org.mockito.Mockito.doReturn;
53 import static org.testng.Assert.assertEquals;
54 import static org.testng.Assert.assertNotNull;
56 public class OrchestrationTemplateCandidateManagerImplTest {
57 private static final String VSP_ID = "vspId";
58 private static final Version VERSION01 = new Version("versionId");
60 private static final String COMPONENT_ORIG_ID_1 = "Component_Pd_Server_Id_Orig";
61 private static final String COMPONENT_ORIG_ID_2 = "Component_Sm_Server_Id_Orig";
62 private static final String COMPONENT_ORIG_ID_3 = "Component_Oam_Server_Id_Orig";
63 private static final String COMPONENT_ORIG_ID_4 = "Component_Ps_Server_Id_Orig";
65 private static final String COMPONENT_NEW_ID_1 = "Component_Pd_Server_Id_New";
66 private static final String COMPONENT_NEW_ID_2 = "Component_Sm_Server_Id_New";
67 private static final String COMPONENT_NEW_ID_3 = "Component_Oam_Server_Id_New";
68 private static final String COMPONENT_NEW_ID_4 = "Component_Ps_Server_Id_New";
70 private static final String COMPONENT_NAME_1 = "pd_server";
71 private static final String COMPONENT_NAME_2 = "sm_server";
72 private static final String COMPONENT_NAME_3 = "oam_server";
73 private static final String COMPONENT_NAME_4 = "ps_server";
76 private VendorSoftwareProductInfoDao vspInfoDaoMock;
78 private CandidateService candidateServiceMock;
80 private OrchestrationTemplateDao orchestrationTemplateDaoMock;
82 private ComponentDependencyModelDao componentDependencyModelDaoMock;
84 private ComponentDao componentDaoMock;
87 private OrchestrationTemplateCandidateManagerImpl candidateManager;
90 private OrchestrationUtil orchestrationUtil;
94 MockitoAnnotations.initMocks(this);
98 private void resetMocks() {
99 Mockito.reset(vspInfoDaoMock);
100 Mockito.reset(candidateServiceMock);
101 Mockito.reset(orchestrationTemplateDaoMock);
102 Mockito.reset(componentDependencyModelDaoMock);
103 Mockito.reset(componentDaoMock);
107 public void tearDown() {
108 candidateManager = null;
109 orchestrationUtil = null;
113 public void testProcessEmptyUpload() {
114 OrchestrationTemplateCandidateData orchTemplate =
115 new OrchestrationTemplateCandidateData();
117 .setContentData(ByteBuffer.wrap(FileUtils.toByteArray(new ZipFileUtils().getZipInputStream
118 ("/vspmanager/zips/emptyComposition.zip"))));
119 orchTemplate.setFilesDataStructure("{\n" +
120 " \"modules\": [\n" +
122 " \"isBase\": false,\n" +
123 " \"yaml\": \"ep-jsa_net.yaml\"\n" +
127 doReturn(Optional.of(orchTemplate))
128 .when(candidateServiceMock).getOrchestrationTemplateCandidate(any(), any());
130 doReturn(new VspDetails(VSP_ID, VERSION01))
131 .when(vspInfoDaoMock).get(any());
133 .when(orchestrationTemplateDaoMock).getInfo(any(), any());
135 doReturn("{}").when(candidateServiceMock).createManifest(any(), any());
136 doReturn(Optional.empty()).when(candidateServiceMock)
137 .fetchZipFileByteArrayInputStream(any(), any(), any(),
138 eq(OnboardingTypesEnum.ZIP), any());
141 OrchestrationTemplateActionResponse response =
142 candidateManager.process(VSP_ID, VERSION01);
144 assertNotNull(response);
148 public void testUpdateVspComponentDependenciesHeatReuploadMoreComponents() {
149 Collection<ComponentDependencyModelEntity> existingComponentsDependencies =
150 getExistingComponentDependencies();
151 Collection<ComponentEntity> componentListWithMoreComponentsInHeat =
152 getComponentListWithMoreComponentsInHeat();
154 doReturn(componentListWithMoreComponentsInHeat).when(componentDaoMock).list(any());
155 Map<String, String> componentIdNameInfoBeforeProcess = getVspInitComponentIdNameInfo();
156 orchestrationUtil.updateVspComponentDependencies(VSP_ID, VERSION01,
157 componentIdNameInfoBeforeProcess, existingComponentsDependencies);
158 Mockito.verify(componentDependencyModelDaoMock, Mockito.times(2)).create(any());
162 public void testUpdateVspComponentDependenciesHeatReuploadLessComponents() {
163 Collection<ComponentDependencyModelEntity> existingComponentsDependencies =
164 getExistingComponentDependencies();
165 Collection<ComponentEntity> componentListWithLessComponentsInHeat =
166 getComponentListWithLessComponentsInHeat();
167 doReturn(componentListWithLessComponentsInHeat).when(componentDaoMock).list(any());
168 Map<String, String> componentIdNameInfoBeforeProcess = getVspInitComponentIdNameInfo();
169 orchestrationUtil.updateVspComponentDependencies(VSP_ID, VERSION01,
170 componentIdNameInfoBeforeProcess, existingComponentsDependencies);
171 Mockito.verify(componentDependencyModelDaoMock, Mockito.times(1)).create(any());
175 public void testUpdateVspComponentDependenciesHeatReuploadSameComponents() {
176 Collection<ComponentDependencyModelEntity> existingComponentsDependencies =
177 getExistingComponentDependencies();
178 Collection<ComponentEntity> componentListWithSameComponentsInHeat =
179 getComponentListWithSameComponentsInHeat();
180 doReturn(componentListWithSameComponentsInHeat).when(componentDaoMock).list(any());
181 Map<String, String> componentIdNameInfoBeforeProcess = getVspInitComponentIdNameInfo();
182 orchestrationUtil.updateVspComponentDependencies(VSP_ID, VERSION01,
183 componentIdNameInfoBeforeProcess, existingComponentsDependencies);
184 Mockito.verify(componentDependencyModelDaoMock, Mockito.times(2)).create(any());
188 public void testUpdateVspComponentDependenciesHeatReuploadNoComponents() {
189 Collection<ComponentDependencyModelEntity> existingComponentsDependencies =
190 getExistingComponentDependencies();
191 Collection<ComponentEntity> componentListWithMoreComponentsInHeat =
193 doReturn(componentListWithMoreComponentsInHeat).when(componentDaoMock).list(any());
194 Map<String, String> componentIdNameInfoBeforeProcess = getVspInitComponentIdNameInfo();
195 orchestrationUtil.updateVspComponentDependencies(VSP_ID, VERSION01,
196 componentIdNameInfoBeforeProcess, existingComponentsDependencies);
197 Mockito.verify(componentDependencyModelDaoMock, Mockito.times(0)).create(any());
201 public void testVspComponentIdNameInfoNoComponents() {
202 Collection<ComponentEntity> initialVspComponents = new ArrayList<>();
203 doReturn(initialVspComponents).when(componentDaoMock).list(any());
204 Map<String, String> vspComponentIdNameInfo =
205 orchestrationUtil.getVspComponentIdNameInfo(VSP_ID, VERSION01);
206 Assert.assertEquals(vspComponentIdNameInfo.size(), 0);
210 public void testVspComponentIdNameInfo() {
211 doReturn(getInitialVspComponents()).when(componentDaoMock).list(any());
212 Map<String, String> vspComponentIdNameInfo =
213 orchestrationUtil.getVspComponentIdNameInfo(VSP_ID, VERSION01);
215 assertEquals(vspComponentIdNameInfo.size(), 3);
216 assertNotNull(vspComponentIdNameInfo.get(COMPONENT_ORIG_ID_1));
217 assertNotNull(vspComponentIdNameInfo.get(COMPONENT_ORIG_ID_2));
218 assertNotNull(vspComponentIdNameInfo.get(COMPONENT_ORIG_ID_3));
222 public void testVspComponentIdNameInfoEmptyCompositionData() {
223 Collection<ComponentEntity> initialVspComponents = getInitialVspComponents();
224 ComponentEntity componentEntity = new ComponentEntity();
225 componentEntity.setId(COMPONENT_ORIG_ID_4);
226 initialVspComponents.add(componentEntity);
228 doReturn(initialVspComponents).when(componentDaoMock).list(any());
229 Map<String, String> vspComponentIdNameInfo =
230 orchestrationUtil.getVspComponentIdNameInfo(VSP_ID, VERSION01);
232 assertEquals(vspComponentIdNameInfo.size(), 3);
233 assertNotNull(vspComponentIdNameInfo.get(COMPONENT_ORIG_ID_1));
234 assertNotNull(vspComponentIdNameInfo.get(COMPONENT_ORIG_ID_2));
235 assertNotNull(vspComponentIdNameInfo.get(COMPONENT_ORIG_ID_3));
236 Assert.assertNull(vspComponentIdNameInfo.get(COMPONENT_ORIG_ID_4));
240 public void testGetComponentDependenciesBeforeDeleteInvalid() {
241 Collection<ComponentDependencyModelEntity> componentDependenciesBeforeDelete =
242 orchestrationUtil.getComponentDependenciesBeforeDelete(null, null);
243 Assert.assertEquals(componentDependenciesBeforeDelete.size(), 0);
247 public void testGetComponentDependenciesBeforeDeleteValid() {
248 Collection<ComponentDependencyModelEntity> existingComponentsDependencies =
249 getExistingComponentDependencies();
250 doReturn(existingComponentsDependencies).when(componentDependencyModelDaoMock).list(any());
251 Collection<ComponentDependencyModelEntity> componentDependenciesBeforeDelete =
252 orchestrationUtil.getComponentDependenciesBeforeDelete(VSP_ID, VERSION01);
253 Assert.assertEquals(componentDependenciesBeforeDelete.size(), 2);
257 public void testGetFileDataStructure() {
258 Optional<String> jsonFileDataStructure = Optional.of("{\n" +
259 " \"modules\": [\n" +
261 " \"yaml\": \"hot-mog-0108-bs1271.yml\",\n" +
262 " \"env\": \"hot-mog-0108-bs1271.env\"\n" +
265 " \"unassigned\": [],\n" +
266 " \"artifacts\": [],\n" +
267 " \"nested\": []\n" +
269 Optional<FilesDataStructure> filesDataStructureOptional = Optional.of(JsonUtil.json2Object
270 (jsonFileDataStructure.get(), FilesDataStructure.class));
271 doReturn(filesDataStructureOptional).when(candidateServiceMock)
272 .getOrchestrationTemplateCandidateFileDataStructure(VSP_ID, VERSION01);
273 Optional<FilesDataStructure> filesDataStructure = candidateManager.getFilesDataStructure(VSP_ID,
275 assertNotNull(filesDataStructure);
276 assertEquals(filesDataStructureOptional.get().getModules().size(), filesDataStructure.get()
277 .getModules().size());
278 assertEquals(filesDataStructureOptional.get().getModules().get(0).getName(),
279 filesDataStructure.get().getModules().get(0).getName());
283 public void testAbort() {
285 Mockito.doNothing().when(candidateServiceMock).deleteOrchestrationTemplateCandidate(VSP_ID,
287 candidateManager.abort(VSP_ID, VERSION01);
289 Mockito.verify(candidateServiceMock, Mockito.times(1)).deleteOrchestrationTemplateCandidate
293 private Map<String, String> getVspInitComponentIdNameInfo() {
294 Map<String, String> componentIdNameInfoBeforeProcess = new HashMap<>();
295 componentIdNameInfoBeforeProcess.put(COMPONENT_ORIG_ID_1, COMPONENT_NAME_1);
296 componentIdNameInfoBeforeProcess.put(COMPONENT_ORIG_ID_2, COMPONENT_NAME_2);
297 componentIdNameInfoBeforeProcess.put(COMPONENT_ORIG_ID_3, COMPONENT_NAME_3);
298 return componentIdNameInfoBeforeProcess;
301 private Collection<ComponentEntity> getInitialVspComponents() {
302 Collection<ComponentEntity> vspComponents = new ArrayList<>();
303 ComponentEntity component1 = createComponentEntity(COMPONENT_ORIG_ID_1, COMPONENT_NAME_1);
304 ComponentEntity component2 = createComponentEntity(COMPONENT_ORIG_ID_2, COMPONENT_NAME_2);
305 ComponentEntity component3 = createComponentEntity(COMPONENT_ORIG_ID_3, COMPONENT_NAME_3);
306 vspComponents.add(component1);
307 vspComponents.add(component2);
308 vspComponents.add(component3);
309 return vspComponents;
312 private Collection<ComponentEntity> getComponentListWithMoreComponentsInHeat() {
313 Collection<ComponentEntity> vspComponents = getInitialVspComponents();
314 createInitialComponentDependencies(vspComponents);
315 ComponentEntity newComponent = createComponentEntity(COMPONENT_NEW_ID_4, COMPONENT_NAME_4);
316 vspComponents.add(newComponent);
317 return vspComponents;
320 private Collection<ComponentEntity> getComponentListWithLessComponentsInHeat() {
321 Collection<ComponentEntity> vspComponents = getInitialVspComponents();
322 for (Iterator<ComponentEntity> iterator = vspComponents.iterator(); iterator.hasNext(); ) {
323 ComponentEntity componentEntity = iterator.next();
324 switch (componentEntity.getComponentCompositionData().getName()) {
325 case COMPONENT_NAME_1:
328 case COMPONENT_NAME_2:
329 componentEntity.setId(COMPONENT_NEW_ID_2);
331 case COMPONENT_NAME_3:
332 componentEntity.setId(COMPONENT_NEW_ID_3);
336 return vspComponents;
339 private Collection<ComponentEntity> getComponentListWithSameComponentsInHeat() {
340 Collection<ComponentEntity> vspComponents = getInitialVspComponents();
341 createInitialComponentDependencies(vspComponents);
342 return vspComponents;
345 private Collection<ComponentDependencyModelEntity> getExistingComponentDependencies() {
346 Collection<ComponentDependencyModelEntity> newComponents = new ArrayList<>();
347 ComponentDependencyModelEntity entity =
348 createComponentDependencyEntity(COMPONENT_ORIG_ID_1, COMPONENT_ORIG_ID_2);
349 ComponentDependencyModelEntity entity2 =
350 createComponentDependencyEntity(COMPONENT_ORIG_ID_2, COMPONENT_ORIG_ID_3);
351 newComponents.add(entity);
352 newComponents.add(entity2);
353 return newComponents;
356 private ComponentEntity createComponentEntity(String componentId, String componentName) {
357 ComponentEntity componentEntity = new ComponentEntity();
358 componentEntity.setVspId(VSP_ID);
359 componentEntity.setVersion(VERSION01);
360 componentEntity.setId(componentId);
361 ComponentData data = new ComponentData();
362 data.setName(componentName);
363 componentEntity.setComponentCompositionData(data);
364 return componentEntity;
367 private ComponentDependencyModelEntity createComponentDependencyEntity(String sourceComponentId,
368 String targetComponentId) {
369 ComponentDependencyModelEntity componentDependency = new ComponentDependencyModelEntity();
370 componentDependency.setVspId(VSP_ID);
371 componentDependency.setVersion(VERSION01);
372 componentDependency.setRelation("dependsOn");
373 componentDependency.setSourceComponentId(sourceComponentId);
374 componentDependency.setTargetComponentId(targetComponentId);
375 return componentDependency;
378 private void createInitialComponentDependencies(Collection<ComponentEntity> vspComponents) {
379 for (ComponentEntity componentEntity : vspComponents) {
380 switch (componentEntity.getComponentCompositionData().getName()) {
381 case COMPONENT_NAME_1:
382 componentEntity.setId(COMPONENT_NEW_ID_1);
384 case COMPONENT_NAME_2:
385 componentEntity.setId(COMPONENT_NEW_ID_2);
387 case COMPONENT_NAME_3:
388 componentEntity.setId(COMPONENT_NEW_ID_3);