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