be53a1a71a88422fd06c7b8f749b3f895a4c40b9
[sdc.git] /
1 package org.openecomp.sdc.vendorsoftwareproduct.impl;
2
3
4 import org.mockito.InjectMocks;
5 import org.mockito.Mock;
6 import org.mockito.MockitoAnnotations;
7 import org.mockito.Spy;
8 import org.openecomp.core.utilities.json.JsonUtil;
9 import org.openecomp.sdc.common.errors.CoreException;
10 import org.openecomp.sdc.vendorsoftwareproduct.dao.ComponentDao;
11 import org.openecomp.sdc.vendorsoftwareproduct.dao.ComputeDao;
12 import org.openecomp.sdc.vendorsoftwareproduct.dao.DeploymentFlavorDao;
13 import org.openecomp.sdc.vendorsoftwareproduct.dao.VendorSoftwareProductInfoDao;
14 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComponentEntity;
15 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComputeEntity;
16 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.DeploymentFlavorEntity;
17 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.VspDetails;
18 import org.openecomp.sdc.vendorsoftwareproduct.errors.VendorSoftwareProductErrorCodes;
19 import org.openecomp.sdc.vendorsoftwareproduct.services.composition.CompositionEntityDataManager;
20 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.ComponentComputeAssociation;
21 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.CompositionEntityType;
22 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.CompositionEntityValidationData;
23 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.DeploymentFlavor;
24 import org.openecomp.sdc.versioning.dao.types.Version;
25 import org.openecomp.sdc.versioning.errors.VersioningErrorCodes;
26 import org.testng.Assert;
27 import org.testng.annotations.AfterMethod;
28 import org.testng.annotations.BeforeMethod;
29 import org.testng.annotations.Test;
30
31 import java.util.ArrayList;
32 import java.util.Arrays;
33 import java.util.Collection;
34 import java.util.Collections;
35 import java.util.List;
36
37 import static org.mockito.Matchers.anyObject;
38 import static org.mockito.Mockito.doReturn;
39 import static org.mockito.Mockito.verify;
40
41 public class DeploymentFlavorManagerImplTest {
42   private static final String VSP_ID = "VSP_ID";
43   private static final Version VERSION = new Version("version_id");
44   private static final String COMPONENT_ID = "COMPONENT_ID";
45   private static final String DF1_ID = "df1";
46   private static final String DF2_ID = "df2";
47   private static final String FG_ID = "FG_ID";
48   private static final List<String> fgs = Collections.singletonList(FG_ID);
49
50   @Mock
51   private CompositionEntityDataManager compositionEntityDataManagerMock;
52   @Mock
53   private VendorSoftwareProductInfoDao vspInfoDao;
54   @Mock
55   private DeploymentFlavorDao deploymentFlavorDaoMock;
56   @Mock
57   private ComponentDao componentDaoMock;
58   @Mock
59   private ComputeDao computeDaoMock;
60   @InjectMocks
61   @Spy
62   private DeploymentFlavorManagerImpl deploymentFlavorManager;
63
64   @BeforeMethod
65   public void setUp() throws Exception {
66     MockitoAnnotations.initMocks(this);
67   }
68
69   @AfterMethod
70   public void tearDown() {
71     deploymentFlavorManager = null;
72   }
73
74   @Test
75   public void testListWhenNone() {
76     final Collection<DeploymentFlavorEntity> deploymentFlavorEntities =
77         deploymentFlavorManager.listDeploymentFlavors(VSP_ID, VERSION);
78     Assert.assertEquals(deploymentFlavorEntities.size(), 0);
79   }
80
81   @Test
82   public void testCreateOnNotManual_negative() {
83
84     testCreate_negative(new DeploymentFlavorEntity(VSP_ID, VERSION, null),
85         VendorSoftwareProductErrorCodes.CREATE_DEPLOYMENT_FLAVOR_NOT_ALLOWED_IN_HEAT_ONBOARDING);
86   }
87
88   @Test
89   public void testCreateManualDepFlavor() {
90     DeploymentFlavorEntity expected = createDeploymentFlavor(VSP_ID, VERSION, DF1_ID);
91     doReturn(true).when(vspInfoDao).isManual(anyObject(), anyObject());
92
93     VspDetails vspDetails = new VspDetails(VSP_ID, VERSION);
94     vspDetails.setFeatureGroups(fgs);
95     doReturn(vspDetails).when(vspInfoDao).get(anyObject());
96
97     deploymentFlavorManager.createDeploymentFlavor(expected);
98     verify(compositionEntityDataManagerMock).createDeploymentFlavor(expected);
99   }
100
101   @Test
102   public void testCreateManualDepFlavorWithDuplicateName() {
103     DeploymentFlavorEntity expected = createDeploymentFlavor(VSP_ID, VERSION, DF1_ID);
104     doReturn(true).when(vspInfoDao).isManual(anyObject(), anyObject());
105
106     DeploymentFlavorEntity expectedDiffName = createDeploymentFlavor(VSP_ID, VERSION, DF1_ID);
107     DeploymentFlavor deploymentFlavor = expectedDiffName.getDeploymentFlavorCompositionData();
108     deploymentFlavor.setModel(DF1_ID + "Name");
109     expectedDiffName.setDeploymentFlavorCompositionData(deploymentFlavor);
110     List<DeploymentFlavorEntity> list = new ArrayList<>();
111     list.add(expectedDiffName);
112     doReturn(list).when(deploymentFlavorDaoMock).list(anyObject());
113
114     try {
115       deploymentFlavorManager.createDeploymentFlavor(expected);
116       Assert.fail();
117     } catch (CoreException ex) {
118       Assert.assertEquals(
119           VendorSoftwareProductErrorCodes.DUPLICATE_DEPLOYMENT_FLAVOR_MODEL_NOT_ALLOWED,
120           ex.code().id());
121     }
122   }
123
124   @Test
125   public void testCreateManualDepFlavorWithFGNotInVSP() {
126     DeploymentFlavorEntity expected = createDeploymentFlavor(VSP_ID, VERSION, DF1_ID);
127     final DeploymentFlavor deploymentFlavor =
128         JsonUtil.json2Object(expected.getCompositionData(), DeploymentFlavor.class);
129     deploymentFlavor.setFeatureGroupId("fg3");
130     expected.setCompositionData(JsonUtil.object2Json(deploymentFlavor));
131
132     doReturn(true).when(vspInfoDao).isManual(anyObject(), anyObject());
133
134     List<String> featureGrps = new ArrayList<>();
135     featureGrps.add("fg1");
136     featureGrps.add("fg2");
137
138     VspDetails vspDetails = new VspDetails(VSP_ID, VERSION);
139     vspDetails.setFeatureGroups(featureGrps);
140     doReturn(vspDetails).when(vspInfoDao).get(anyObject());
141
142
143     try {
144       deploymentFlavorManager.createDeploymentFlavor(expected);
145       Assert.fail();
146     } catch (CoreException ex) {
147       Assert.assertEquals(VendorSoftwareProductErrorCodes.FEATURE_GROUP_NOT_EXIST_FOR_VSP,
148           ex.code().id());
149     }
150   }
151
152   @Test
153   public void testCreateManualDepFlavorWithNullCompInAssociation() {
154     DeploymentFlavorEntity expected = createDeploymentFlavor(VSP_ID, VERSION, DF1_ID);
155     final DeploymentFlavor deploymentFlavor =
156         JsonUtil.json2Object(expected.getCompositionData(), DeploymentFlavor.class);
157     ComponentComputeAssociation association = new ComponentComputeAssociation();
158     association.setComponentId(null);
159     association.setComputeFlavorId("CF1");
160     List<ComponentComputeAssociation> list = new ArrayList<>();
161     list.add(association);
162     deploymentFlavor.setComponentComputeAssociations(list);
163     expected.setCompositionData(JsonUtil.object2Json(deploymentFlavor));
164
165     doReturn(true).when(vspInfoDao).isManual(anyObject(), anyObject());
166
167     VspDetails vspDetails = new VspDetails(VSP_ID, VERSION);
168     vspDetails.setFeatureGroups(fgs);
169     doReturn(vspDetails).when(vspInfoDao).get(anyObject());
170
171     try {
172       deploymentFlavorManager.createDeploymentFlavor(expected);
173     } catch (CoreException ex) {
174       Assert.assertEquals(VendorSoftwareProductErrorCodes.INVALID_COMPONENT_COMPUTE_ASSOCIATION,
175           ex.code().id());
176       Assert.assertEquals(
177           "Invalid request,for valid association please provide ComponentId for Compute Flavor",
178           ex.getMessage());
179     }
180   }
181
182   @Test
183   public void testCreateManualDepFlavorWithInvalidComputeInAssociation() {
184     DeploymentFlavorEntity expected = createDeploymentFlavor(VSP_ID, VERSION, DF1_ID);
185     final DeploymentFlavor deploymentFlavor =
186         JsonUtil.json2Object(expected.getCompositionData(), DeploymentFlavor.class);
187     ComponentComputeAssociation association = new ComponentComputeAssociation();
188     association.setComponentId(COMPONENT_ID);
189     association.setComputeFlavorId("CF1");
190     List<ComponentComputeAssociation> list = new ArrayList<>();
191     list.add(association);
192     deploymentFlavor.setComponentComputeAssociations(list);
193     expected.setCompositionData(JsonUtil.object2Json(deploymentFlavor));
194
195     doReturn(true).when(vspInfoDao).isManual(anyObject(), anyObject());
196
197     VspDetails vspDetails = new VspDetails(VSP_ID, VERSION);
198     vspDetails.setFeatureGroups(fgs);
199     doReturn(vspDetails).when(vspInfoDao).get(anyObject());
200
201     ComponentEntity component = new ComponentEntity(VSP_ID, VERSION, null);
202     doReturn(component).when(componentDaoMock).get(anyObject());
203
204     doReturn(null).when(computeDaoMock).get(anyObject());
205
206     try {
207       deploymentFlavorManager.createDeploymentFlavor(expected);
208     } catch (CoreException ex) {
209       Assert.assertEquals(VendorSoftwareProductErrorCodes.INVALID_COMPUTE_FLAVOR_ID,
210           ex.code().id());
211     }
212   }
213
214   @Test
215   public void testCreateManualDepFlavorWithDuplicateVfcAssociation() {
216     DeploymentFlavorEntity expected = createDeploymentFlavor(VSP_ID, VERSION, DF1_ID);
217     final DeploymentFlavor deploymentFlavor =
218         JsonUtil.json2Object(expected.getCompositionData(), DeploymentFlavor.class);
219     ComponentComputeAssociation association = new ComponentComputeAssociation();
220     association.setComponentId(COMPONENT_ID);
221     association.setComputeFlavorId("CF1");
222     List<ComponentComputeAssociation> list = new ArrayList<>();
223     list.add(association);
224     list.add(association);
225     deploymentFlavor.setComponentComputeAssociations(list);
226     expected.setCompositionData(JsonUtil.object2Json(deploymentFlavor));
227
228     doReturn(true).when(vspInfoDao).isManual(anyObject(), anyObject());
229
230     VspDetails vspDetails = new VspDetails(VSP_ID, VERSION);
231     vspDetails.setFeatureGroups(fgs);
232     doReturn(vspDetails).when(vspInfoDao).get(anyObject());
233
234     ComponentEntity component = new ComponentEntity(VSP_ID, VERSION, null);
235     doReturn(component).when(componentDaoMock).get(anyObject());
236
237     ComputeEntity computeEntity = new ComputeEntity(VSP_ID, VERSION, COMPONENT_ID, "CF1");
238     doReturn(computeEntity).when(computeDaoMock).get(anyObject());
239
240     try {
241       deploymentFlavorManager.createDeploymentFlavor(expected);
242     } catch (CoreException ex) {
243       Assert.assertEquals(
244           VendorSoftwareProductErrorCodes.SAME_VFC_ASSOCIATION_MORE_THAN_ONCE_NOT_ALLOWED,
245           ex.code().id());
246     }
247   }
248
249   @Test
250   public void testList() {
251
252     doReturn(Arrays.asList(
253         createDeploymentFlavor(VSP_ID, VERSION, DF1_ID),
254         createDeploymentFlavor(VSP_ID, VERSION, DF2_ID)))
255         .when(deploymentFlavorDaoMock).list(anyObject());
256
257
258     final Collection<DeploymentFlavorEntity> deploymentFlavorEntities =
259         deploymentFlavorManager.listDeploymentFlavors(VSP_ID, VERSION);
260     Assert.assertEquals(deploymentFlavorEntities.size(), 2);
261     for (DeploymentFlavorEntity deploymentFlavorEntity : deploymentFlavorEntities) {
262       Assert.assertEquals(deploymentFlavorEntity.getDeploymentFlavorCompositionData().getModel()
263           , DF1_ID.equals(deploymentFlavorEntity.getId()) ? DF1_ID + "name" : DF2_ID + "name");
264     }
265   }
266
267   @Test
268   public void testUpdateHeatDepFlavor() {
269     testUpdate_negative(VSP_ID, VERSION, DF1_ID,
270         VendorSoftwareProductErrorCodes.EDIT_DEPLOYMENT_FLAVOR_NOT_ALLOWED_IN_HEAT_ONBOARDING);
271   }
272
273   @Test
274   public void testUpdateNonExistingManualDepFlavorId_negative() {
275     doReturn(true).when(vspInfoDao).isManual(anyObject(), anyObject());
276     testUpdate_negative(VSP_ID, VERSION, DF1_ID,
277         VersioningErrorCodes.VERSIONABLE_SUB_ENTITY_NOT_FOUND);
278   }
279
280   @Test
281   public void testManualUpdateDepFlavor() {
282     doReturn(true).when(vspInfoDao).isManual(anyObject(), anyObject());
283
284     doReturn(createDeploymentFlavor(VSP_ID, VERSION, DF1_ID))
285         .when(deploymentFlavorDaoMock).get(anyObject());
286
287     doReturn(new CompositionEntityValidationData(CompositionEntityType.image, DF1_ID))
288         .when(compositionEntityDataManagerMock)
289         .validateEntity(anyObject(), anyObject(), anyObject());
290
291     VspDetails vspDetails = new VspDetails(VSP_ID, VERSION);
292     doReturn(vspDetails).when(vspInfoDao).get(anyObject());
293
294     DeploymentFlavorEntity deploymentFlavorEntity =
295         new DeploymentFlavorEntity(VSP_ID, VERSION, DF1_ID);
296     DeploymentFlavor deploymentFlavor = new DeploymentFlavor();
297     deploymentFlavor.setModel(DF1_ID + "_name");
298     deploymentFlavor.setDescription(DF1_ID + " desc updated");
299     deploymentFlavorEntity.setDeploymentFlavorCompositionData(deploymentFlavor);
300
301     CompositionEntityValidationData validationData =
302         deploymentFlavorManager.updateDeploymentFlavor(deploymentFlavorEntity);
303     Assert.assertTrue(validationData == null || validationData.getErrors() == null);
304     verify(deploymentFlavorDaoMock).update(deploymentFlavorEntity);
305   }
306
307   @Test
308   public void testGetNonExistingDepFlavorId_negative() {
309     testGet_negative(VSP_ID, VERSION,
310         VersioningErrorCodes.VERSIONABLE_SUB_ENTITY_NOT_FOUND);
311   }
312
313
314   @Test
315   public void testDeleteDepFlavorOnHEAT() {
316     DeploymentFlavorEntity expected = createDeploymentFlavor(VSP_ID, VERSION, DF1_ID);
317     doReturn(expected).when(deploymentFlavorDaoMock).get(anyObject());
318     testDelete_negative(VSP_ID, VERSION, DF1_ID,
319         VendorSoftwareProductErrorCodes.DELETE_DEPLOYMENT_FLAVOR_NOT_ALLOWED_IN_HEAT_ONBOARDING);
320   }
321
322   @Test
323   public void testDeleteOnNotExistImage() {
324     testDelete_negative(VSP_ID, VERSION, DF1_ID,
325         VersioningErrorCodes.VERSIONABLE_SUB_ENTITY_NOT_FOUND);
326   }
327
328   @Test
329   public void testDeleteOnManualImage() {
330     DeploymentFlavorEntity expected = createDeploymentFlavor(VSP_ID, VERSION, DF1_ID);
331     doReturn(expected).when(deploymentFlavorDaoMock).get(anyObject());
332     doReturn(true).when(vspInfoDao).isManual(anyObject(), anyObject());
333     deploymentFlavorManager.deleteDeploymentFlavor(VSP_ID, VERSION, DF1_ID);
334     verify(deploymentFlavorDaoMock).delete(anyObject());
335   }
336
337   private void testCreate_negative(DeploymentFlavorEntity deploymentFlavorEntity,
338                                    String expectedErrorCode) {
339     try {
340       deploymentFlavorManager.createDeploymentFlavor(deploymentFlavorEntity);
341       Assert.fail();
342     } catch (CoreException exception) {
343       Assert.assertEquals(exception.code().id(), expectedErrorCode);
344     }
345   }
346
347   private void testDelete_negative(String vspId, Version version, String deploymentFlavorId,
348                                    String expectedErrorCode) {
349     try {
350       deploymentFlavorManager.deleteDeploymentFlavor(vspId, version, deploymentFlavorId);
351       Assert.fail();
352     } catch (CoreException exception) {
353       Assert.assertEquals(exception.code().id(), expectedErrorCode);
354     }
355   }
356
357   private static DeploymentFlavorEntity createDeploymentFlavor(String vspId, Version version,
358                                                                String deploymentFlavorId) {
359
360     DeploymentFlavorEntity deploymentFlavorEntity =
361         new DeploymentFlavorEntity(vspId, version, deploymentFlavorId);
362     DeploymentFlavor deploymentFlavor = new DeploymentFlavor();
363     deploymentFlavor.setModel(deploymentFlavorId + "name");
364     deploymentFlavor.setDescription(deploymentFlavorId + " desc");
365     deploymentFlavor.setFeatureGroupId(FG_ID);
366
367     deploymentFlavorEntity.setDeploymentFlavorCompositionData(deploymentFlavor);
368     return deploymentFlavorEntity;
369   }
370
371   private void testUpdate_negative(String vspId, Version version, String deploymentFlavorId,
372                                    String expectedErrorCode) {
373     try {
374       DeploymentFlavorEntity deploymentFlavorEntity =
375           new DeploymentFlavorEntity(vspId, version, deploymentFlavorId);
376       DeploymentFlavor deploymentFlavor = new DeploymentFlavor();
377       deploymentFlavor.setModel("Name");
378       deploymentFlavorEntity.setDeploymentFlavorCompositionData(deploymentFlavor);
379       deploymentFlavorManager
380           .updateDeploymentFlavor(new DeploymentFlavorEntity(vspId, version, deploymentFlavorId));
381       Assert.fail();
382     } catch (CoreException exception) {
383       Assert.assertEquals(exception.code().id(), expectedErrorCode);
384     }
385   }
386
387   private void testGet_negative(String vspId, Version version,
388                                 String expectedErrorCode) {
389     try {
390       deploymentFlavorManager.getDeploymentFlavor(vspId, version, "non existing image id");
391       Assert.fail();
392     } catch (CoreException exception) {
393       Assert.assertEquals(exception.code().id(), expectedErrorCode);
394     }
395   }
396
397 }