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