fix incorrect dependency
[sdc.git] / openecomp-be / backend / openecomp-sdc-vendor-software-product-manager / src / test / java / org / openecomp / sdc / vendorsoftwareproduct / impl / DeplomentFlavorManagerImplTest.java
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 DeplomentFlavorManagerImplTest {
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 testCreateManualDepFlavorWithIncorrectNameFormat() {
118     DeploymentFlavorEntity expected = createDeploymentFlavor(VSP_ID, VERSION, DF1_ID);
119     doReturn(true).when(vspInfoDao).isManual(anyObject(), anyObject());
120
121     DeploymentFlavorEntity expectedDiffName = createDeploymentFlavor(VSP_ID, VERSION, DF1_ID);
122     DeploymentFlavor deploymentFlavor = expectedDiffName.getDeploymentFlavorCompositionData();
123     deploymentFlavor.setModel(DF1_ID + "Name/*");
124     expectedDiffName.setDeploymentFlavorCompositionData(deploymentFlavor);
125     List<DeploymentFlavorEntity> list = new ArrayList<DeploymentFlavorEntity>();
126     list.add(expectedDiffName);
127     doReturn(list).when(deploymentFlavorDaoMock).list(anyObject());
128
129     try {
130       deploymentFlavorManager.createDeploymentFlavor(expectedDiffName, USER);
131       Assert.fail();
132     }
133     catch (CoreException ex) {
134       Assert.assertEquals(VendorSoftwareProductErrorCodes.DEPLOYMENT_FLAVOR_NAME_FORMAT_NOT_ALLOWED,
135               ex.code().id());
136     }
137   }
138   @Test
139   public void testCreateManualDepFlavorWithFGNotInVSP() {
140     DeploymentFlavorEntity expected = createDeploymentFlavor(VSP_ID, VERSION, DF1_ID);
141     final DeploymentFlavor deploymentFlavor =
142         JsonUtil.json2Object(expected.getCompositionData(), DeploymentFlavor.class);
143     deploymentFlavor.setFeatureGroupId("fg3");
144     expected.setCompositionData(JsonUtil.object2Json(deploymentFlavor));
145
146     doReturn(true).when(vspInfoDao).isManual(anyObject(), anyObject());
147
148     List<String> featureGrps = new ArrayList<String>();
149     featureGrps.add("fg1");
150     featureGrps.add("fg2");
151
152     VspDetails vspDetails = new VspDetails(VSP_ID, VERSION);
153     vspDetails.setFeatureGroups(featureGrps);
154     doReturn(vspDetails).when(vspInfoDao).get(anyObject());
155
156
157     try {
158       deploymentFlavorManager.createDeploymentFlavor(expected, USER);
159       Assert.fail();
160     }
161     catch (CoreException ex) {
162       Assert.assertEquals(VendorSoftwareProductErrorCodes.FEATURE_GROUP_NOT_EXIST_FOR_VSP,
163           ex.code().id());
164     }
165   }
166
167   @Test
168   public void testCreateManualDepFlavorWithNullCompInAssociation() {
169     DeploymentFlavorEntity expected = createDeploymentFlavor(VSP_ID, VERSION,  DF1_ID);
170     final DeploymentFlavor deploymentFlavor =
171         JsonUtil.json2Object(expected.getCompositionData(), DeploymentFlavor.class);
172     ComponentComputeAssociation association = new ComponentComputeAssociation();
173     association.setComponentId(null);
174     association.setComputeFlavorId("CF1");
175     List<ComponentComputeAssociation> list = new ArrayList<ComponentComputeAssociation>();
176     list.add(association);
177     deploymentFlavor.setComponentComputeAssociations(list);
178     expected.setCompositionData(JsonUtil.object2Json(deploymentFlavor));
179
180     doReturn(true).when(vspInfoDao).isManual(anyObject(), anyObject());
181
182     VspDetails vspDetails = new VspDetails(VSP_ID, VERSION);
183     doReturn(vspDetails).when(vspInfoDao).get(anyObject());
184
185     try {
186       deploymentFlavorManager.createDeploymentFlavor(expected, USER);
187     }
188     catch (CoreException ex) {
189       Assert.assertEquals(VendorSoftwareProductErrorCodes.INVALID_COMPONENT_COMPUTE_ASSOCIATION,
190           ex.code().id());
191       Assert.assertEquals("Invalid request,for valid association please provide ComponentId for Compute Flavor",
192           ex.getMessage());
193     }
194   }
195
196   @Test
197   public void testCreateManualDepFlavorWithInvalidComputeInAssociation() {
198     DeploymentFlavorEntity expected = createDeploymentFlavor(VSP_ID, VERSION,  DF1_ID);
199     final DeploymentFlavor deploymentFlavor =
200         JsonUtil.json2Object(expected.getCompositionData(), DeploymentFlavor.class);
201     ComponentComputeAssociation association = new ComponentComputeAssociation();
202     association.setComponentId(COMPONENT_ID);
203     association.setComputeFlavorId("CF1");
204     List<ComponentComputeAssociation> list = new ArrayList<ComponentComputeAssociation>();
205     list.add(association);
206     deploymentFlavor.setComponentComputeAssociations(list);
207     expected.setCompositionData(JsonUtil.object2Json(deploymentFlavor));
208
209     doReturn(true).when(vspInfoDao).isManual(anyObject(), anyObject());
210
211     VspDetails vspDetails = new VspDetails(VSP_ID, VERSION);
212     doReturn(vspDetails).when(vspInfoDao).get(anyObject());
213
214     ComponentEntity component = new ComponentEntity(VSP_ID, VERSION, USER);
215     doReturn(component).when(componentDaoMock).get(anyObject());
216
217     doReturn(null).when(computeDaoMock).get(anyObject());
218
219     try {
220       deploymentFlavorManager.createDeploymentFlavor(expected, USER);
221     }
222     catch (CoreException ex) {
223       Assert.assertEquals(VendorSoftwareProductErrorCodes.INVALID_COMPUTE_FLAVOR_ID,
224           ex.code().id());
225     }
226   }
227
228   @Test
229   public void testCreateManualDepFlavorWithDuplicateVfcAssociation() {
230     DeploymentFlavorEntity expected = createDeploymentFlavor(VSP_ID, VERSION,  DF1_ID);
231     final DeploymentFlavor deploymentFlavor =
232         JsonUtil.json2Object(expected.getCompositionData(), DeploymentFlavor.class);
233     ComponentComputeAssociation association = new ComponentComputeAssociation();
234     association.setComponentId(COMPONENT_ID);
235     association.setComputeFlavorId("CF1");
236     List<ComponentComputeAssociation> list = new ArrayList<ComponentComputeAssociation>();
237     list.add(association);
238     list.add(association);
239     deploymentFlavor.setComponentComputeAssociations(list);
240     expected.setCompositionData(JsonUtil.object2Json(deploymentFlavor));
241
242     doReturn(true).when(vspInfoDao).isManual(anyObject(), anyObject());
243
244     VspDetails vspDetails = new VspDetails(VSP_ID, VERSION);
245     doReturn(vspDetails).when(vspInfoDao).get(anyObject());
246
247     ComponentEntity component = new ComponentEntity(VSP_ID, VERSION, USER);
248     doReturn(component).when(componentDaoMock).get(anyObject());
249
250     ComputeEntity computeEntity = new ComputeEntity(VSP_ID, VERSION, COMPONENT_ID, "CF1");
251     doReturn(computeEntity).when(computeDaoMock).get(anyObject());
252
253     try {
254       deploymentFlavorManager.createDeploymentFlavor(expected, USER);
255     }
256     catch (CoreException ex) {
257       Assert.assertEquals(VendorSoftwareProductErrorCodes.SAME_VFC_ASSOCIATION_MORE_THAN_ONCE_NOT_ALLOWED,
258           ex.code().id());
259     }
260   }
261
262   @Test
263   public void testList() {
264
265     doReturn(Arrays.asList(
266         createDeploymentFlavor(VSP_ID, VERSION,  DF1_ID),
267         createDeploymentFlavor(VSP_ID, VERSION,  DF2_ID)))
268         .when(deploymentFlavorDaoMock).list(anyObject());
269
270
271     final Collection<DeploymentFlavorEntity> deploymentFlavorEntities =
272         deploymentFlavorManager.listDeploymentFlavors(VSP_ID, VERSION,  USER);
273     Assert.assertEquals(deploymentFlavorEntities.size(), 2);
274     for (DeploymentFlavorEntity deploymentFlavorEntity : deploymentFlavorEntities) {
275       Assert.assertEquals(deploymentFlavorEntity.getDeploymentFlavorCompositionData().getModel()
276           , DF1_ID.equals(deploymentFlavorEntity.getId()) ? DF1_ID+"name" : DF2_ID+"name" );
277     }
278   }
279
280   @Test
281   public void testUpdateHeatDepFlavor() {
282     testUpdate_negative(VSP_ID, VERSION, DF1_ID, USER,
283         VendorSoftwareProductErrorCodes.EDIT_DEPLOYMENT_FLAVOR_NOT_ALLOWED_IN_HEAT_ONBOARDING);
284   }
285
286   @Test
287   public void testUpdateNonExistingManualDepFlavorId_negative() {
288     doReturn(true).when(vspInfoDao).isManual(anyObject(), anyObject());
289     testUpdate_negative(VSP_ID, VERSION, DF1_ID, USER,
290         VersioningErrorCodes.VERSIONABLE_SUB_ENTITY_NOT_FOUND);
291   }
292
293   @Test
294   public void testManualUpdateDepFlavor() {
295     doReturn(true).when(vspInfoDao).isManual(anyObject(), anyObject());
296
297     doReturn(createDeploymentFlavor(VSP_ID, VERSION, DF1_ID))
298         .when(deploymentFlavorDaoMock).get(anyObject());
299
300     doReturn(new CompositionEntityValidationData(CompositionEntityType.image, DF1_ID))
301         .when(compositionEntityDataManagerMock)
302         .validateEntity(anyObject(), anyObject(), anyObject());
303
304     VspDetails vspDetails = new VspDetails(VSP_ID, VERSION);
305     doReturn(vspDetails).when(vspInfoDao).get(anyObject());
306
307     DeploymentFlavorEntity deploymentFlavorEntity = new DeploymentFlavorEntity(VSP_ID, VERSION, DF1_ID);
308     DeploymentFlavor deploymentFlavor = new DeploymentFlavor();
309     deploymentFlavor.setModel(DF1_ID + "_name");
310     deploymentFlavor.setDescription(DF1_ID + " desc updated");
311     deploymentFlavorEntity.setDeploymentFlavorCompositionData(deploymentFlavor);
312
313     CompositionEntityValidationData validationData =
314         deploymentFlavorManager.updateDeploymentFlavor(deploymentFlavorEntity, USER);
315     Assert.assertTrue(validationData == null || validationData.getErrors() == null);
316     verify(deploymentFlavorDaoMock).update(deploymentFlavorEntity);
317   }
318
319     @Test
320     public void testManualUpdateDepFlavorIncorrectNameFormat() {
321         doReturn(true).when(vspInfoDao).isManual(anyObject(), anyObject());
322
323         doReturn(createDeploymentFlavor(VSP_ID, VERSION, DF1_ID))
324                 .when(deploymentFlavorDaoMock).get(anyObject());
325
326         doReturn(new CompositionEntityValidationData(CompositionEntityType.image, DF1_ID))
327                 .when(compositionEntityDataManagerMock)
328                 .validateEntity(anyObject(), anyObject(), anyObject());
329
330         VspDetails vspDetails = new VspDetails(VSP_ID, VERSION);
331         doReturn(vspDetails).when(vspInfoDao).get(anyObject());
332
333         DeploymentFlavorEntity deploymentFlavorEntity = new DeploymentFlavorEntity(VSP_ID, VERSION, DF1_ID);
334         DeploymentFlavor deploymentFlavor = new DeploymentFlavor();
335         deploymentFlavor.setModel(DF1_ID + "_name/*");
336         deploymentFlavor.setDescription(DF1_ID + " desc updated");
337         deploymentFlavorEntity.setDeploymentFlavorCompositionData(deploymentFlavor);
338
339         try {
340             deploymentFlavorManager.updateDeploymentFlavor(deploymentFlavorEntity, USER);
341             Assert.fail();
342         }
343         catch (CoreException ex) {
344             Assert.assertEquals(VendorSoftwareProductErrorCodes.DEPLOYMENT_FLAVOR_NAME_FORMAT_NOT_ALLOWED,
345                     ex.code().id());
346         }
347     }
348
349   @Test
350   public void testGetNonExistingDepFlavorId_negative() {
351     testGet_negative(VSP_ID, VERSION, "non existing image id", USER,
352         VersioningErrorCodes.VERSIONABLE_SUB_ENTITY_NOT_FOUND);
353   }
354
355   @Test
356   public void testGet() {
357     DeploymentFlavorEntity expected = createDeploymentFlavor(VSP_ID, VERSION, DF1_ID);
358     doReturn(expected).when(deploymentFlavorDaoMock).get(anyObject());
359
360     VspDetails vspDetails = new VspDetails(VSP_ID, VERSION);
361     doReturn(vspDetails).when(vspInfoDao).get(anyObject());
362
363     CompositionEntityResponse<DeploymentFlavor> response =
364         deploymentFlavorManager.getDeploymentFlavor(VSP_ID, VERSION, DF1_ID, USER);
365     Assert.assertEquals(response.getId(), expected.getId());
366     Assert.assertEquals(response.getData().getModel(), expected.getDeploymentFlavorCompositionData().
367         getModel());
368     Assert.assertEquals(response.getData().getDescription(), expected.getDeploymentFlavorCompositionData().
369         getDescription());
370   }
371
372   @Test
373   public void testDeleteDepFlavorOnHEAT() {
374     DeploymentFlavorEntity expected = createDeploymentFlavor(VSP_ID, VERSION, DF1_ID);
375     doReturn(expected).when(deploymentFlavorDaoMock).get(anyObject());
376     testDelete_negative(VSP_ID, VERSION,  DF1_ID, USER,
377         VendorSoftwareProductErrorCodes.DELETE_DEPLOYMENT_FLAVOR_NOT_ALLOWED_IN_HEAT_ONBOARDING);
378   }
379
380   @Test
381   public void testDeleteOnNotExistImage() {
382     testDelete_negative(VSP_ID, VERSION,  DF1_ID, USER,
383         VersioningErrorCodes.VERSIONABLE_SUB_ENTITY_NOT_FOUND);
384   }
385
386   @Test
387   public void testDeleteOnManualImage() {
388     DeploymentFlavorEntity expected = createDeploymentFlavor(VSP_ID, VERSION, DF1_ID);
389     doReturn(expected).when(deploymentFlavorDaoMock).get(anyObject());
390     doReturn(true).when(vspInfoDao).isManual(anyObject(), anyObject());
391     deploymentFlavorManager.deleteDeploymentFlavor(VSP_ID, VERSION, DF1_ID, USER);
392     verify(deploymentFlavorDaoMock).delete(anyObject());
393   }
394
395   private void testList_negative(String vspId, Version version, String componentId, String user,
396                                  String expectedErrorCode, String expectedErrorMsg) {
397     try {
398       deploymentFlavorManager.listDeploymentFlavors(vspId, version, user);
399       Assert.fail();
400     } catch (CoreException exception) {
401       Assert.assertEquals(exception.code().id(), expectedErrorCode);
402       Assert.assertEquals(exception.getMessage(), expectedErrorMsg);
403     }
404   }
405
406   private void testCreate_negative(DeploymentFlavorEntity deploymentFlavorEntity, String user, String
407       expectedErrorCode) {
408     try {
409       deploymentFlavorManager.createDeploymentFlavor(deploymentFlavorEntity, user);
410       Assert.fail();
411     } catch (CoreException exception) {
412       Assert.assertEquals(exception.code().id(), expectedErrorCode);
413     }
414   }
415
416   private void testDelete_negative(String vspId, Version version, String deploymentFlavorId,
417                                    String user,
418                                    String expectedErrorCode) {
419     try {
420       deploymentFlavorManager.deleteDeploymentFlavor(vspId, version, deploymentFlavorId, user);
421       Assert.fail();
422     } catch (CoreException exception) {
423       Assert.assertEquals(exception.code().id(), expectedErrorCode);
424     }
425   }
426
427   static DeploymentFlavorEntity createDeploymentFlavor(String vspId, Version version, String deploymentFlavorId) {
428
429     DeploymentFlavorEntity deploymentFlavorEntity = new DeploymentFlavorEntity(vspId, version, deploymentFlavorId);
430     DeploymentFlavor deploymentFlavor = new DeploymentFlavor();
431     deploymentFlavor.setModel(deploymentFlavorId + "name");
432     deploymentFlavor.setDescription(deploymentFlavorId + " desc");
433
434     deploymentFlavorEntity.setDeploymentFlavorCompositionData(deploymentFlavor);
435     return deploymentFlavorEntity;
436   }
437
438   private void testUpdate_negative(String vspId, Version version, String
439       deploymentFlavorId, String user, String expectedErrorCode) {
440     try {
441       DeploymentFlavorEntity deploymentFlavorEntity = new DeploymentFlavorEntity(vspId, version, deploymentFlavorId);
442       DeploymentFlavor deploymentFlavor = new DeploymentFlavor();
443       deploymentFlavor.setModel("Name");
444       deploymentFlavorEntity.setDeploymentFlavorCompositionData(deploymentFlavor);
445       deploymentFlavorManager
446           .updateDeploymentFlavor(deploymentFlavorEntity, user);
447       Assert.fail();
448     } catch (CoreException exception) {
449       Assert.assertEquals(exception.code().id(), expectedErrorCode);
450     }
451   }
452
453   private void testGet_negative(String vspId, Version version, String deploymentFlavorId,
454                                 String user, String expectedErrorCode) {
455     try {
456       deploymentFlavorManager.getDeploymentFlavor(vspId, version, deploymentFlavorId, user);
457       Assert.fail();
458     } catch (CoreException exception) {
459       Assert.assertEquals(exception.code().id(), expectedErrorCode);
460     }
461   }
462
463 }