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