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