2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdc.vendorsoftwareproduct.impl;
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;
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;
57 import static org.mockito.ArgumentMatchers.any;
58 import static org.mockito.Mockito.doReturn;
59 import static org.mockito.Mockito.verify;
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);
71 private CompositionEntityDataManager compositionEntityDataManagerMock;
73 private VendorSoftwareProductInfoDao vspInfoDao;
75 private DeploymentFlavorDao deploymentFlavorDaoMock;
77 private ComponentDao componentDaoMock;
79 private ComputeDao computeDaoMock;
82 private DeploymentFlavorManagerImpl deploymentFlavorManager;
85 public void setUp() throws Exception {
86 MockitoAnnotations.initMocks(this);
90 public void tearDown() {
91 deploymentFlavorManager = null;
95 public void testListWhenNone() {
96 final Collection<DeploymentFlavorEntity> deploymentFlavorEntities =
97 deploymentFlavorManager.listDeploymentFlavors(VSP_ID, VERSION);
98 Assert.assertEquals(deploymentFlavorEntities.size(), 0);
102 public void testCreateOnNotManual_negative() {
104 testCreate_negative(new DeploymentFlavorEntity(VSP_ID, VERSION, null),
105 VendorSoftwareProductErrorCodes.CREATE_DEPLOYMENT_FLAVOR_NOT_ALLOWED_IN_HEAT_ONBOARDING);
109 public void testCreateManualDepFlavor() {
110 DeploymentFlavorEntity expected = createDeploymentFlavor(VSP_ID, VERSION, DF1_ID);
111 doReturn(true).when(vspInfoDao).isManual(any(), any());
113 VspDetails vspDetails = new VspDetails(VSP_ID, VERSION);
114 vspDetails.setFeatureGroups(fgs);
115 doReturn(vspDetails).when(vspInfoDao).get(any());
117 deploymentFlavorManager.createDeploymentFlavor(expected);
118 verify(compositionEntityDataManagerMock).createDeploymentFlavor(expected);
122 public void testCreateManualDepFlavorWithDuplicateName() {
123 DeploymentFlavorEntity expected = createDeploymentFlavor(VSP_ID, VERSION, DF1_ID);
124 doReturn(true).when(vspInfoDao).isManual(any(), any());
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());
135 deploymentFlavorManager.createDeploymentFlavor(expected);
137 } catch (CoreException ex) {
139 VendorSoftwareProductErrorCodes.DUPLICATE_DEPLOYMENT_FLAVOR_MODEL_NOT_ALLOWED,
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));
152 doReturn(true).when(vspInfoDao).isManual(any(), any());
154 List<String> featureGrps = new ArrayList<>();
155 featureGrps.add("fg1");
156 featureGrps.add("fg2");
158 VspDetails vspDetails = new VspDetails(VSP_ID, VERSION);
159 vspDetails.setFeatureGroups(featureGrps);
160 doReturn(vspDetails).when(vspInfoDao).get(any());
164 deploymentFlavorManager.createDeploymentFlavor(expected);
166 } catch (CoreException ex) {
167 Assert.assertEquals(VendorSoftwareProductErrorCodes.FEATURE_GROUP_NOT_EXIST_FOR_VSP,
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));
180 doReturn(true).when(vspInfoDao).isManual(any(), any());
182 VspDetails vspDetails = new VspDetails(VSP_ID, VERSION);
183 doReturn(vspDetails).when(vspInfoDao).get(any());
184 deploymentFlavorManager.createDeploymentFlavor(expected);
185 verify(compositionEntityDataManagerMock).createDeploymentFlavor(expected);
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));
201 doReturn(true).when(vspInfoDao).isManual(any(), any());
203 VspDetails vspDetails = new VspDetails(VSP_ID, VERSION);
204 vspDetails.setFeatureGroups(fgs);
205 doReturn(vspDetails).when(vspInfoDao).get(any());
208 deploymentFlavorManager.createDeploymentFlavor(expected);
209 } catch (CoreException ex) {
210 Assert.assertEquals(VendorSoftwareProductErrorCodes.INVALID_COMPONENT_COMPUTE_ASSOCIATION,
213 "Invalid request,for valid association please provide ComponentId for Compute Flavor",
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));
231 doReturn(true).when(vspInfoDao).isManual(any(), any());
233 VspDetails vspDetails = new VspDetails(VSP_ID, VERSION);
234 vspDetails.setFeatureGroups(fgs);
235 doReturn(vspDetails).when(vspInfoDao).get(any());
237 ComponentEntity component = new ComponentEntity(VSP_ID, VERSION, null);
238 doReturn(component).when(componentDaoMock).get(any());
240 doReturn(null).when(computeDaoMock).get(any());
243 deploymentFlavorManager.createDeploymentFlavor(expected);
244 } catch (CoreException ex) {
245 Assert.assertEquals(VendorSoftwareProductErrorCodes.INVALID_COMPUTE_FLAVOR_ID,
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));
264 doReturn(true).when(vspInfoDao).isManual(any(), any());
266 VspDetails vspDetails = new VspDetails(VSP_ID, VERSION);
267 vspDetails.setFeatureGroups(fgs);
268 doReturn(vspDetails).when(vspInfoDao).get(any());
270 ComponentEntity component = new ComponentEntity(VSP_ID, VERSION, null);
271 doReturn(component).when(componentDaoMock).get(any());
273 ComputeEntity computeEntity = new ComputeEntity(VSP_ID, VERSION, COMPONENT_ID, "CF1");
274 doReturn(computeEntity).when(computeDaoMock).get(any());
277 deploymentFlavorManager.createDeploymentFlavor(expected);
278 } catch (CoreException ex) {
280 VendorSoftwareProductErrorCodes.SAME_VFC_ASSOCIATION_MORE_THAN_ONCE_NOT_ALLOWED,
286 public void testList() {
288 doReturn(Arrays.asList(
289 createDeploymentFlavor(VSP_ID, VERSION, DF1_ID),
290 createDeploymentFlavor(VSP_ID, VERSION, DF2_ID)))
291 .when(deploymentFlavorDaoMock).list(any());
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");
304 public void testUpdateHeatDepFlavor() {
305 testUpdate_negative(VSP_ID, VERSION, DF1_ID,
306 VendorSoftwareProductErrorCodes.EDIT_DEPLOYMENT_FLAVOR_NOT_ALLOWED_IN_HEAT_ONBOARDING);
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);
317 public void testManualUpdateDepFlavor() {
318 doReturn(true).when(vspInfoDao).isManual(any(), any());
320 doReturn(createDeploymentFlavor(VSP_ID, VERSION, DF1_ID))
321 .when(deploymentFlavorDaoMock).get(any());
323 doReturn(new CompositionEntityValidationData(CompositionEntityType.image, DF1_ID))
324 .when(compositionEntityDataManagerMock)
325 .validateEntity(any(), any(), any());
327 VspDetails vspDetails = new VspDetails(VSP_ID, VERSION);
328 doReturn(vspDetails).when(vspInfoDao).get(any());
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);
337 CompositionEntityValidationData validationData =
338 deploymentFlavorManager.updateDeploymentFlavor(deploymentFlavorEntity);
339 Assert.assertTrue(validationData == null || validationData.getErrors() == null);
340 verify(deploymentFlavorDaoMock).update(deploymentFlavorEntity);
344 public void testGetNonExistingDepFlavorId_negative() {
345 testGet_negative(VSP_ID, VERSION,
346 VersioningErrorCodes.VERSIONABLE_SUB_ENTITY_NOT_FOUND);
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);
359 public void testDeleteOnNotExistImage() {
360 testDelete_negative(VSP_ID, VERSION, DF1_ID,
361 VersioningErrorCodes.VERSIONABLE_SUB_ENTITY_NOT_FOUND);
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());
373 private void testCreate_negative(DeploymentFlavorEntity deploymentFlavorEntity,
374 String expectedErrorCode) {
376 deploymentFlavorManager.createDeploymentFlavor(deploymentFlavorEntity);
378 } catch (CoreException exception) {
379 Assert.assertEquals(exception.code().id(), expectedErrorCode);
383 private void testDelete_negative(String vspId, Version version, String deploymentFlavorId,
384 String expectedErrorCode) {
386 deploymentFlavorManager.deleteDeploymentFlavor(vspId, version, deploymentFlavorId);
388 } catch (CoreException exception) {
389 Assert.assertEquals(exception.code().id(), expectedErrorCode);
393 private static DeploymentFlavorEntity createDeploymentFlavor(String vspId, Version version,
394 String deploymentFlavorId) {
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);
403 deploymentFlavorEntity.setDeploymentFlavorCompositionData(deploymentFlavor);
404 return deploymentFlavorEntity;
407 private void testUpdate_negative(String vspId, Version version, String deploymentFlavorId,
408 String expectedErrorCode) {
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));
418 } catch (CoreException exception) {
419 Assert.assertEquals(exception.code().id(), expectedErrorCode);
423 private void testGet_negative(String vspId, Version version,
424 String expectedErrorCode) {
426 deploymentFlavorManager.getDeploymentFlavor(vspId, version, "non existing image id");
428 } catch (CoreException exception) {
429 Assert.assertEquals(exception.code().id(), expectedErrorCode);