82715d3fe6ec3f0c389eb34dc15bbcc220353996
[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.logging.api.Logger;
15 import org.openecomp.sdc.logging.api.LoggerFactory;
16 import org.openecomp.sdc.vendorsoftwareproduct.dao.ComponentDao;
17 import org.openecomp.sdc.vendorsoftwareproduct.dao.ComputeDao;
18 import org.openecomp.sdc.vendorsoftwareproduct.dao.DeploymentFlavorDao;
19 import org.openecomp.sdc.vendorsoftwareproduct.dao.VendorSoftwareProductInfoDao;
20 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComponentEntity;
21 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComputeEntity;
22 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.DeploymentFlavorEntity;
23 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.VspDetails;
24 import org.openecomp.sdc.vendorsoftwareproduct.errors.VendorSoftwareProductErrorCodes;
25 import org.openecomp.sdc.vendorsoftwareproduct.services.composition.CompositionEntityDataManager;
26 import org.openecomp.sdc.vendorsoftwareproduct.types.CompositionEntityResponse;
27 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.ComponentComputeAssociation;
28 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.CompositionEntityType;
29 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.CompositionEntityValidationData;
30 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.DeploymentFlavor;
31 import org.openecomp.sdc.versioning.dao.types.Version;
32 import org.openecomp.sdc.versioning.errors.VersioningErrorCodes;
33 import org.testng.Assert;
34 import org.testng.annotations.BeforeMethod;
35 import org.testng.annotations.Test;
36
37 import java.util.ArrayList;
38 import java.util.Arrays;
39 import java.util.Collection;
40 import java.util.List;
41
42 public class DeploymentFlavorManagerImplTest {
43
44   private final Logger log = (Logger) LoggerFactory.getLogger(this.getClass().getName());
45
46   private static final String USER = "depFlavorTestUser";
47   private static final String VSP_ID = "VSP_ID";
48   private static final Version VERSION = new Version(0, 1);
49   private static final String COMPONENT_ID = "COMPONENT_ID";
50   private static final String DF1_ID = "df1";
51   private static final String DF2_ID = "df2";
52
53   @Mock
54   private CompositionEntityDataManager compositionEntityDataManagerMock;
55   @Mock
56   private VendorSoftwareProductInfoDao vspInfoDao;
57   @Mock
58   DeploymentFlavorDao deploymentFlavorDaoMock;
59   @Mock
60   ComponentDao componentDaoMock;
61   @Mock
62   ComputeDao computeDaoMock;
63   @InjectMocks
64   @Spy
65   private DeploymentFlavorManagerImpl deploymentFlavorManager;
66
67   @BeforeMethod
68   public void setUp() throws Exception {
69     MockitoAnnotations.initMocks(this);
70   }
71
72   @Test
73   public void testListWhenNone() {
74     final Collection<DeploymentFlavorEntity> deploymentFlavorEntities =
75         deploymentFlavorManager.listDeploymentFlavors(VSP_ID, VERSION,  USER);
76     Assert.assertEquals(deploymentFlavorEntities.size(), 0);
77   }
78
79   @Test
80   public void testCreateOnNotManual_negative() {
81
82     testCreate_negative(new DeploymentFlavorEntity(VSP_ID, VERSION,  null), USER,
83         VendorSoftwareProductErrorCodes.CREATE_DEPLOYMENT_FLAVOR_NOT_ALLOWED_IN_HEAT_ONBOARDING);
84   }
85
86   @Test
87   public void testCreateManualDepFlavor() {
88     DeploymentFlavorEntity expected = createDeploymentFlavor(VSP_ID, VERSION,  DF1_ID);
89     doReturn(true).when(vspInfoDao).isManual(anyObject(), anyObject());
90
91     VspDetails vspDetails = new VspDetails(VSP_ID, VERSION);
92     doReturn(vspDetails).when(vspInfoDao).get(anyObject());
93
94     deploymentFlavorManager.createDeploymentFlavor(expected, USER);
95     verify(compositionEntityDataManagerMock).createDeploymentFlavor(expected);
96   }
97
98   @Test
99   public void testCreateManualDepFlavorWithDuplicateName() {
100     DeploymentFlavorEntity expected = createDeploymentFlavor(VSP_ID, VERSION, DF1_ID);
101     doReturn(true).when(vspInfoDao).isManual(anyObject(), anyObject());
102
103     DeploymentFlavorEntity expectedDiffName = createDeploymentFlavor(VSP_ID, VERSION, DF1_ID);
104     DeploymentFlavor deploymentFlavor = expectedDiffName.getDeploymentFlavorCompositionData();
105     deploymentFlavor.setModel(DF1_ID + "Name");
106     expectedDiffName.setDeploymentFlavorCompositionData(deploymentFlavor);
107     List<DeploymentFlavorEntity> list = new ArrayList<DeploymentFlavorEntity>();
108     list.add(expectedDiffName);
109     doReturn(list).when(deploymentFlavorDaoMock).list(anyObject());
110
111     try {
112       deploymentFlavorManager.createDeploymentFlavor(expected, USER);
113       Assert.fail();
114     }
115     catch (CoreException ex) {
116       log.debug("",ex);
117       Assert.assertEquals(VendorSoftwareProductErrorCodes.DUPLICATE_DEPLOYMENT_FLAVOR_MODEL_NOT_ALLOWED,
118           ex.code().id());
119     }
120   }
121
122   @Test
123   public void testCreateManualDepFlavorWithFGNotInVSP() {
124     DeploymentFlavorEntity expected = createDeploymentFlavor(VSP_ID, VERSION, DF1_ID);
125     final DeploymentFlavor deploymentFlavor =
126         JsonUtil.json2Object(expected.getCompositionData(), DeploymentFlavor.class);
127     deploymentFlavor.setFeatureGroupId("fg3");
128     expected.setCompositionData(JsonUtil.object2Json(deploymentFlavor));
129
130     doReturn(true).when(vspInfoDao).isManual(anyObject(), anyObject());
131
132     List<String> featureGrps = new ArrayList<String>();
133     featureGrps.add("fg1");
134     featureGrps.add("fg2");
135
136     VspDetails vspDetails = new VspDetails(VSP_ID, VERSION);
137     vspDetails.setFeatureGroups(featureGrps);
138     doReturn(vspDetails).when(vspInfoDao).get(anyObject());
139
140
141     try {
142       deploymentFlavorManager.createDeploymentFlavor(expected, USER);
143       Assert.fail();
144     }
145     catch (CoreException ex) {
146       log.debug("",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<ComponentComputeAssociation>();
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     doReturn(vspDetails).when(vspInfoDao).get(anyObject());
169
170     try {
171       deploymentFlavorManager.createDeploymentFlavor(expected, USER);
172     }
173     catch (CoreException ex) {
174       log.debug("",ex);
175       Assert.assertEquals(VendorSoftwareProductErrorCodes.INVALID_COMPONENT_COMPUTE_ASSOCIATION,
176           ex.code().id());
177       Assert.assertEquals("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<ComponentComputeAssociation>();
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     doReturn(vspDetails).when(vspInfoDao).get(anyObject());
199
200     ComponentEntity component = new ComponentEntity(VSP_ID, VERSION, USER);
201     doReturn(component).when(componentDaoMock).get(anyObject());
202
203     doReturn(null).when(computeDaoMock).get(anyObject());
204
205     try {
206       deploymentFlavorManager.createDeploymentFlavor(expected, USER);
207     }
208     catch (CoreException ex) {
209       log.debug("",ex);
210       Assert.assertEquals(VendorSoftwareProductErrorCodes.INVALID_COMPUTE_FLAVOR_ID,
211           ex.code().id());
212     }
213   }
214
215   @Test
216   public void testCreateManualDepFlavorWithDuplicateVfcAssociation() {
217     DeploymentFlavorEntity expected = createDeploymentFlavor(VSP_ID, VERSION,  DF1_ID);
218     final DeploymentFlavor deploymentFlavor =
219         JsonUtil.json2Object(expected.getCompositionData(), DeploymentFlavor.class);
220     ComponentComputeAssociation association = new ComponentComputeAssociation();
221     association.setComponentId(COMPONENT_ID);
222     association.setComputeFlavorId("CF1");
223     List<ComponentComputeAssociation> list = new ArrayList<ComponentComputeAssociation>();
224     list.add(association);
225     list.add(association);
226     deploymentFlavor.setComponentComputeAssociations(list);
227     expected.setCompositionData(JsonUtil.object2Json(deploymentFlavor));
228
229     doReturn(true).when(vspInfoDao).isManual(anyObject(), anyObject());
230
231     VspDetails vspDetails = new VspDetails(VSP_ID, VERSION);
232     doReturn(vspDetails).when(vspInfoDao).get(anyObject());
233
234     ComponentEntity component = new ComponentEntity(VSP_ID, VERSION, USER);
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, USER);
242     }
243     catch (CoreException ex) {
244       log.debug("",ex);
245       Assert.assertEquals(VendorSoftwareProductErrorCodes.SAME_VFC_ASSOCIATION_MORE_THAN_ONCE_NOT_ALLOWED,
246           ex.code().id());
247     }
248   }
249
250   @Test
251   public void testList() {
252
253     doReturn(Arrays.asList(
254         createDeploymentFlavor(VSP_ID, VERSION,  DF1_ID),
255         createDeploymentFlavor(VSP_ID, VERSION,  DF2_ID)))
256         .when(deploymentFlavorDaoMock).list(anyObject());
257
258
259     final Collection<DeploymentFlavorEntity> deploymentFlavorEntities =
260         deploymentFlavorManager.listDeploymentFlavors(VSP_ID, VERSION,  USER);
261     Assert.assertEquals(deploymentFlavorEntities.size(), 2);
262     for (DeploymentFlavorEntity deploymentFlavorEntity : deploymentFlavorEntities) {
263       Assert.assertEquals(deploymentFlavorEntity.getDeploymentFlavorCompositionData().getModel()
264           , DF1_ID.equals(deploymentFlavorEntity.getId()) ? DF1_ID+"name" : DF2_ID+"name" );
265     }
266   }
267
268   @Test
269   public void testUpdateHeatDepFlavor() {
270     testUpdate_negative(VSP_ID, VERSION, DF1_ID, USER,
271         VendorSoftwareProductErrorCodes.EDIT_DEPLOYMENT_FLAVOR_NOT_ALLOWED_IN_HEAT_ONBOARDING);
272   }
273
274   @Test
275   public void testUpdateNonExistingManualDepFlavorId_negative() {
276     doReturn(true).when(vspInfoDao).isManual(anyObject(), anyObject());
277     testUpdate_negative(VSP_ID, VERSION, DF1_ID, USER,
278         VersioningErrorCodes.VERSIONABLE_SUB_ENTITY_NOT_FOUND);
279   }
280
281   @Test
282   public void testManualUpdateDepFlavor() {
283     doReturn(true).when(vspInfoDao).isManual(anyObject(), anyObject());
284
285     doReturn(createDeploymentFlavor(VSP_ID, VERSION, DF1_ID))
286         .when(deploymentFlavorDaoMock).get(anyObject());
287
288     doReturn(new CompositionEntityValidationData(CompositionEntityType.image, DF1_ID))
289         .when(compositionEntityDataManagerMock)
290         .validateEntity(anyObject(), anyObject(), anyObject());
291
292     VspDetails vspDetails = new VspDetails(VSP_ID, VERSION);
293     doReturn(vspDetails).when(vspInfoDao).get(anyObject());
294
295     DeploymentFlavorEntity deploymentFlavorEntity = 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, USER);
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, "non existing image id", USER,
310         VersioningErrorCodes.VERSIONABLE_SUB_ENTITY_NOT_FOUND);
311   }
312
313   /*
314   @Test
315   public void testGet() {
316     DeploymentFlavorEntity expected = createDeploymentFlavor(VSP_ID, VERSION, DF1_ID);
317     doReturn(expected).when(deploymentFlavorDaoMock).get(anyObject());
318
319     VspDetails vspDetails = new VspDetails(VSP_ID, VERSION);
320     doReturn(vspDetails).when(vspInfoDao).get(anyObject());
321
322     CompositionEntityResponse<DeploymentFlavor> response =
323         deploymentFlavorManager.getDeploymentFlavor(VSP_ID, VERSION, DF1_ID, USER);
324     Assert.assertEquals(response.getId(), expected.getId());
325     Assert.assertEquals(response.getData().getModel(), expected.getDeploymentFlavorCompositionData().
326         getModel());
327     Assert.assertEquals(response.getData().getDescription(), expected.getDeploymentFlavorCompositionData().
328         getDescription());
329   }
330 */
331   @Test
332   public void testDeleteDepFlavorOnHEAT() {
333     DeploymentFlavorEntity expected = createDeploymentFlavor(VSP_ID, VERSION, DF1_ID);
334     doReturn(expected).when(deploymentFlavorDaoMock).get(anyObject());
335     testDelete_negative(VSP_ID, VERSION,  DF1_ID, USER,
336         VendorSoftwareProductErrorCodes.DELETE_DEPLOYMENT_FLAVOR_NOT_ALLOWED_IN_HEAT_ONBOARDING);
337   }
338
339   @Test
340   public void testDeleteOnNotExistImage() {
341     testDelete_negative(VSP_ID, VERSION,  DF1_ID, USER,
342         VersioningErrorCodes.VERSIONABLE_SUB_ENTITY_NOT_FOUND);
343   }
344
345   @Test
346   public void testDeleteOnManualImage() {
347     DeploymentFlavorEntity expected = createDeploymentFlavor(VSP_ID, VERSION, DF1_ID);
348     doReturn(expected).when(deploymentFlavorDaoMock).get(anyObject());
349     doReturn(true).when(vspInfoDao).isManual(anyObject(), anyObject());
350     deploymentFlavorManager.deleteDeploymentFlavor(VSP_ID, VERSION, DF1_ID, USER);
351     verify(deploymentFlavorDaoMock).delete(anyObject());
352   }
353
354   private void testList_negative(String vspId, Version version, String componentId, String user,
355                                  String expectedErrorCode, String expectedErrorMsg) {
356     try {
357       deploymentFlavorManager.listDeploymentFlavors(vspId, version, user);
358       Assert.fail();
359     } catch (CoreException exception) {
360       log.debug("",exception);
361       Assert.assertEquals(exception.code().id(), expectedErrorCode);
362       Assert.assertEquals(exception.getMessage(), expectedErrorMsg);
363     }
364   }
365
366   private void testCreate_negative(DeploymentFlavorEntity deploymentFlavorEntity, String user, String
367       expectedErrorCode) {
368     try {
369       deploymentFlavorManager.createDeploymentFlavor(deploymentFlavorEntity, user);
370       Assert.fail();
371     } catch (CoreException exception) {
372       log.debug("",exception);
373       Assert.assertEquals(exception.code().id(), expectedErrorCode);
374     }
375   }
376
377   private void testDelete_negative(String vspId, Version version, String deploymentFlavorId,
378                                    String user,
379                                    String expectedErrorCode) {
380     try {
381       deploymentFlavorManager.deleteDeploymentFlavor(vspId, version, deploymentFlavorId, user);
382       Assert.fail();
383     } catch (CoreException exception) {
384       log.debug("",exception);
385       Assert.assertEquals(exception.code().id(), expectedErrorCode);
386     }
387   }
388
389   static DeploymentFlavorEntity createDeploymentFlavor(String vspId, Version version, String deploymentFlavorId) {
390
391     DeploymentFlavorEntity deploymentFlavorEntity = new DeploymentFlavorEntity(vspId, version, deploymentFlavorId);
392     DeploymentFlavor deploymentFlavor = new DeploymentFlavor();
393     deploymentFlavor.setModel(deploymentFlavorId + "name");
394     deploymentFlavor.setDescription(deploymentFlavorId + " desc");
395
396     deploymentFlavorEntity.setDeploymentFlavorCompositionData(deploymentFlavor);
397     return deploymentFlavorEntity;
398   }
399
400   private void testUpdate_negative(String vspId, Version version, String
401       deploymentFlavorId, String user, String expectedErrorCode) {
402     try {
403       DeploymentFlavorEntity deploymentFlavorEntity = new DeploymentFlavorEntity(vspId, version, deploymentFlavorId);
404       DeploymentFlavor deploymentFlavor = new DeploymentFlavor();
405       deploymentFlavor.setModel("Name");
406       deploymentFlavorEntity.setDeploymentFlavorCompositionData(deploymentFlavor);
407       deploymentFlavorManager
408           .updateDeploymentFlavor(deploymentFlavorEntity, user);
409       Assert.fail();
410     } catch (CoreException exception) {
411       log.debug("",exception);
412       Assert.assertEquals(exception.code().id(), expectedErrorCode);
413     }
414   }
415
416   private void testGet_negative(String vspId, Version version, String deploymentFlavorId,
417                                 String user, String expectedErrorCode) {
418     try {
419       deploymentFlavorManager.getDeploymentFlavor(vspId, version, deploymentFlavorId, user);
420       Assert.fail();
421     } catch (CoreException exception) {
422       log.debug("",exception);
423       Assert.assertEquals(exception.code().id(), expectedErrorCode);
424     }
425   }
426
427 }