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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
|
/*
* Copyright © 2018 European Support Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
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.sdc.common.errors.CoreException;
import org.openecomp.sdc.vendorsoftwareproduct.CompositionEntityDataManager;
import org.openecomp.sdc.vendorsoftwareproduct.NetworkManager;
import org.openecomp.sdc.vendorsoftwareproduct.dao.NicDao;
import org.openecomp.sdc.vendorsoftwareproduct.dao.VendorSoftwareProductInfoDao;
import org.openecomp.sdc.vendorsoftwareproduct.dao.type.NetworkEntity;
import org.openecomp.sdc.vendorsoftwareproduct.dao.type.NicEntity;
import org.openecomp.sdc.vendorsoftwareproduct.errors.VendorSoftwareProductErrorCodes;
import org.openecomp.sdc.vendorsoftwareproduct.types.CompositionEntityResponse;
import org.openecomp.sdc.vendorsoftwareproduct.types.QuestionnaireResponse;
import org.openecomp.sdc.vendorsoftwareproduct.types.composition.CompositionEntityType;
import org.openecomp.sdc.vendorsoftwareproduct.types.composition.CompositionEntityValidationData;
import org.openecomp.sdc.vendorsoftwareproduct.types.composition.NetworkType;
import org.openecomp.sdc.vendorsoftwareproduct.types.composition.Nic;
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 static org.mockito.Matchers.anyObject;
import static org.mockito.Mockito.*;
public class NicManagerImplTest {
private static final String NIC_NOT_EXIST_MSG =
"Vendor Software Product NIC with Id nic1 does not exist for Vendor Software Product with " +
"id VSP_ID and version version_id";
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 NIC1_ID = "nic1";
private static final String NIC2_ID = "nic2";
private static final String NETWORK1_ID = "network1";
private static final String NETWORK2_ID = "network2";
@Mock
private NicDao nicDao;
@Mock
private CompositionEntityDataManager compositionEntityDataManagerMock;
@Mock
private NetworkManager networkManagerMock;
@Mock
private VendorSoftwareProductInfoDao vspInfoDao;
@InjectMocks
@Spy
private NicManagerImpl nicManager;
@BeforeMethod
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
}
@AfterMethod
public void tearDown() {
nicManager = null;
}
@Test
public void testListWhenNone() {
Collection<NicEntity> nics = nicManager.listNics(VSP_ID, VERSION, COMPONENT_ID);
Assert.assertEquals(nics.size(), 0);
}
@Test
public void testList() {
doReturn(Arrays.asList(
createNic(VSP_ID, VERSION, COMPONENT_ID, NIC1_ID, NETWORK1_ID),
createNic(VSP_ID, VERSION, COMPONENT_ID, NIC2_ID, NETWORK2_ID)))
.when(nicDao).list(anyObject());
NetworkEntity network1 = NetworkManagerImplTest.createNetwork(VSP_ID, VERSION, NETWORK1_ID);
NetworkEntity network2 = NetworkManagerImplTest.createNetwork(VSP_ID, VERSION, NETWORK2_ID);
doReturn(Arrays.asList(network1, network2))
.when(networkManagerMock).listNetworks(VSP_ID, VERSION);
Collection<NicEntity> nics = nicManager.listNics(VSP_ID, VERSION, COMPONENT_ID);
Assert.assertEquals(nics.size(), 2);
for (NicEntity nic : nics) {
Assert.assertEquals(nic.getNicCompositionData().getNetworkName(),
NIC1_ID.equals(nic.getId())
? network1.getNetworkCompositionData().getName()
: network2.getNetworkCompositionData().getName());
}
}
@Test
public void testCreate() {
NicEntity nicEntity = createNic(VSP_ID, VERSION, COMPONENT_ID, NIC1_ID, NETWORK1_ID);
Nic nic = nicEntity.getNicCompositionData();
nic.setNetworkType(NetworkType.Internal);
nicEntity.setNicCompositionData(nic);
doReturn(true).when(vspInfoDao).isManual(anyObject(), anyObject());
Collection<NicEntity> nicEntities = new ArrayList<>();
doReturn(nicEntities).when(nicDao).list(anyObject());
doReturn(nicEntity).when(compositionEntityDataManagerMock).createNic(anyObject());
NicEntity created = nicManager.createNic(nicEntity);
Assert.assertNotNull(created);
}
@Test
public void testCreateWithIncorrectNicNameFormat() {
NicEntity nicEntity = createNic(VSP_ID, VERSION, COMPONENT_ID, NIC1_ID, NETWORK1_ID);
Nic nic = nicEntity.getNicCompositionData();
nic.setNetworkType(NetworkType.Internal);
nicEntity.setNicCompositionData(nic);
doReturn(true).when(vspInfoDao).isManual(anyObject(), anyObject());
Collection<NicEntity> nicEntities = new ArrayList<>();
NicEntity nicEntityDiffName = createNic(VSP_ID, VERSION, COMPONENT_ID, NIC1_ID, NETWORK1_ID);
Nic newNameNic = nicEntityDiffName.getNicCompositionData();
newNameNic.setName(NIC1_ID + "_Name/*");
nicEntityDiffName.setNicCompositionData(newNameNic);
nicEntities.add(nicEntityDiffName);
doReturn(nicEntities).when(nicDao).list(anyObject());
doReturn(nicEntity).when(compositionEntityDataManagerMock).createNic(anyObject());
try {
NicEntity created = nicManager.createNic(nicEntity);
} catch (CoreException exception) {
Assert.assertEquals(VendorSoftwareProductErrorCodes.NIC_NAME_FORMAT_NOT_ALLOWED,
exception.code().id());
}
}
@Test
public void testCreateWithDupNicName() {
NicEntity nicEntity = createNic(VSP_ID, VERSION, COMPONENT_ID, NIC1_ID, NETWORK1_ID);
Nic nic = nicEntity.getNicCompositionData();
nic.setNetworkType(NetworkType.Internal);
nicEntity.setNicCompositionData(nic);
doReturn(true).when(vspInfoDao).isManual(anyObject(), anyObject());
Collection<NicEntity> nicEntities = new ArrayList<>();
NicEntity nicEntityDiffName = createNic(VSP_ID, VERSION, COMPONENT_ID, NIC1_ID, NETWORK1_ID);
Nic newNameNic = nicEntityDiffName.getNicCompositionData();
newNameNic.setName(NIC1_ID + "_Name");
nicEntityDiffName.setNicCompositionData(newNameNic);
nicEntities.add(nicEntityDiffName);
doReturn(nicEntities).when(nicDao).list(anyObject());
doReturn(nicEntity).when(compositionEntityDataManagerMock).createNic(anyObject());
try {
NicEntity created = nicManager.createNic(nicEntity);
} catch (CoreException exception) {
Assert.assertEquals("Invalid request, NIC with name " + nic.getName() +
" already exist for component with ID " + nicEntity.getComponentId() + ".",
exception.code().message());
Assert.assertEquals(VendorSoftwareProductErrorCodes.DUPLICATE_NIC_NAME_NOT_ALLOWED,
exception.code().id());
}
}
@Test
public void testCreateWithExternalNetworkType() {
NicEntity nicEntity = createNic(VSP_ID, VERSION, COMPONENT_ID, NIC1_ID, NETWORK1_ID);
Nic nic = nicEntity.getNicCompositionData();
nic.setNetworkType(NetworkType.External);
nicEntity.setNicCompositionData(nic);
doReturn(true).when(vspInfoDao).isManual(anyObject(), anyObject());
Collection<NicEntity> nicEntities = new ArrayList<>();
doReturn(nicEntities).when(nicDao).list(anyObject());
doReturn(nicEntity).when(compositionEntityDataManagerMock).createNic(anyObject());
try {
NicEntity created = nicManager.createNic(nicEntity);
} catch (CoreException exception) {
Assert.assertEquals("Invalid request,NetworkId not allowed for External Networks",
exception.code().message());
Assert
.assertEquals(VendorSoftwareProductErrorCodes.NETWORKID_NOT_ALLOWED_FOR_EXTERNAL_NETWORK,
exception.code().id());
}
}
@Test
public void testCreateWithNetworkDesc() {
NicEntity nicEntity = createNic(VSP_ID, VERSION, COMPONENT_ID, NIC1_ID, NETWORK1_ID);
Nic nic = nicEntity.getNicCompositionData();
nic.setNetworkType(NetworkType.Internal);
nic.setNetworkDescription(NIC1_ID);
nicEntity.setNicCompositionData(nic);
doReturn(true).when(vspInfoDao).isManual(anyObject(), anyObject());
Collection<NicEntity> nicEntities = new ArrayList<>();
doReturn(nicEntities).when(nicDao).list(anyObject());
doReturn(nicEntity).when(compositionEntityDataManagerMock).createNic(anyObject());
try {
NicEntity created = nicManager.createNic(nicEntity);
} catch (CoreException exception) {
Assert.assertEquals("Invalid request, Network Description not allowed for Internal Networks",
exception.code().message());
Assert.assertEquals(VendorSoftwareProductErrorCodes
.NETWORK_DESCRIPTION_NOT_ALLOWED_FOR_INTERNAL_NETWORK, exception.code().id());
}
}
@Test
public void testDeleteNic() {
NicEntity nicEntity = createNic(VSP_ID, VERSION, COMPONENT_ID, NIC1_ID, NETWORK1_ID);
doReturn(true).when(vspInfoDao).isManual(anyObject(), anyObject());
doReturn(nicEntity).when(nicDao).get(anyObject());
nicManager.deleteNic(VSP_ID, VERSION, COMPONENT_ID, NIC1_ID);
}
@Test
public void testUpdateNicQuestionnaire() {
NicEntity nicEntity = createNic(VSP_ID, VERSION, COMPONENT_ID, NIC1_ID, NETWORK1_ID);
doReturn(nicEntity).when(nicDao).get(anyObject());
doReturn("{}").when(nicManager).getNicCompositionSchema(anyObject());
nicManager
.updateNicQuestionnaire(VSP_ID, VERSION, COMPONENT_ID, NIC1_ID, "Ques");
verify(nicDao).updateQuestionnaireData(VSP_ID, VERSION, COMPONENT_ID, NIC1_ID, "Ques");
}
// @Test(dependsOnMethods = "testListWhenNone")
// public void testCreate() {
// NIC1_ID = testCreate(VSP_ID, COMPONENT_ID, NETWORK1_ID, NETWORK1_ID.getNetworkCompositionData().getName());
// }
/* @Test(dependsOnMethods = {"testCreate"})
public void testCreateWithExistingName_negative() {
NicEntity nic = new NicEntity(VSP_ID, null, COMPONENT_ID, null);
Nic nicData = new Nic();
nicData.setName("nic1 name");
nic.setNicCompositionData(nicData);
testCreate_negative(nic, UniqueValueUtil.UNIQUE_VALUE_VIOLATION);
}*/
// @Test(dependsOnMethods = {"testCreate"})
// public void testCreateWithExistingNameUnderOtherComponent() {
// ComponentEntity component12 = new ComponentEntity(VSP_ID, null, null);
// ComponentData compData12 = new ComponentData();
// compData12.setName("comp12 name");
// compData12.setDescription("comp12 desc");
// component12.setComponentCompositionData(compData12);
//
// String component12Id = nicManager.createComponent(component12).getId();
// testCreate(VSP_ID, component12Id, NETWORK1_ID, NETWORK1_ID.getNetworkCompositionData().getName());
// }
// @Test(dependsOnMethods = {"testCreate"})
// public void testCreateWithExistingNameUnderOtherVsp() {
// testCreate(vsp2Id, component21Id, network2.getId(), network2.getNetworkCompositionData().getName());
// }
@Test
public void testCreateOnUploadVsp_negative() {
testCreate_negative(new NicEntity(VSP_ID, VERSION, COMPONENT_ID, null),
VendorSoftwareProductErrorCodes.ADD_NIC_NOT_ALLOWED_IN_HEAT_ONBOARDING);
}
@Test
public void testUpdateNonExistingNicId_negative() {
doReturn(null).when(nicDao).get(anyObject());
testUpdate_negative(VSP_ID, VERSION, COMPONENT_ID, NIC1_ID,
VersioningErrorCodes.VERSIONABLE_SUB_ENTITY_NOT_FOUND);
}
@Test
public void testUpdateOnUploadVsp() {
doReturn(createNic(VSP_ID, VERSION, COMPONENT_ID, NIC1_ID, NETWORK1_ID))
.when(nicDao).get(anyObject());
doReturn(new CompositionEntityValidationData(CompositionEntityType.nic, NIC1_ID))
.when(compositionEntityDataManagerMock)
.validateEntity(anyObject(), anyObject(), anyObject());
NicEntity nicEntity = new NicEntity(VSP_ID, VERSION, COMPONENT_ID, NIC1_ID);
Nic nicData = new Nic();
nicData.setName(NIC1_ID + " name");
nicData.setDescription(NIC1_ID + " desc updated");
nicData.setNetworkId(NETWORK1_ID);
nicEntity.setNicCompositionData(nicData);
CompositionEntityValidationData validationData =
nicManager.updateNic(nicEntity);
Assert.assertTrue(validationData == null || validationData.getErrors() == null);
verify(nicDao).update(nicEntity);
}
@Test
public void testIllegalUpdateOnUploadVsp() {
doReturn(createNic(VSP_ID, VERSION, COMPONENT_ID, NIC1_ID, NETWORK1_ID))
.when(nicDao).get(anyObject());
CompositionEntityValidationData toBeReturned =
new CompositionEntityValidationData(CompositionEntityType.nic, NIC1_ID);
toBeReturned.setErrors(Arrays.asList("error1", "error2"));
doReturn(toBeReturned)
.when(compositionEntityDataManagerMock)
.validateEntity(anyObject(), anyObject(), anyObject());
NicEntity nicEntity = new NicEntity(VSP_ID, VERSION, COMPONENT_ID, NIC1_ID);
Nic nicData = new Nic();
nicData.setName(NIC1_ID + "_name_updated");
nicData.setDescription(NIC1_ID + " desc updated");
nicData.setNetworkId(NETWORK1_ID);
nicEntity.setNicCompositionData(nicData);
CompositionEntityValidationData validationData = nicManager.updateNic(nicEntity);
Assert.assertNotNull(validationData);
Assert.assertEquals(validationData.getErrors().size(), 2);
verify(nicDao, never()).update(nicEntity);
}
@Test
public void testUpdateIncorrectNameFormat() {
doReturn(createNic(VSP_ID, VERSION, COMPONENT_ID, NIC1_ID, NETWORK1_ID))
.when(nicDao).get(anyObject());
CompositionEntityValidationData toBeReturned =
new CompositionEntityValidationData(CompositionEntityType.nic, NIC1_ID);
toBeReturned.setErrors(Arrays.asList("Field does not conform to predefined criteria:name : must match [a-zA-Z0-9_]*$"));
doReturn(toBeReturned)
.when(compositionEntityDataManagerMock)
.validateEntity(anyObject(), anyObject(), anyObject());
doReturn(true).when(vspInfoDao).isManual(anyObject(), anyObject());
NicEntity nicEntity = new NicEntity(VSP_ID, VERSION, COMPONENT_ID, NIC1_ID);
Nic nicData = new Nic();
nicData.setName(NIC1_ID + "_name_updated/*");
nicData.setDescription(NIC1_ID + " desc updated");
nicData.setNetworkId(NETWORK1_ID);
nicEntity.setNicCompositionData(nicData);
CompositionEntityValidationData output = nicManager.updateNic(nicEntity);
Assert.assertEquals(output.getErrors(),toBeReturned.getErrors());
}
@Test
public void testGetNonExistingNicId_negative() {
testGet_negative(VSP_ID, VERSION, COMPONENT_ID, "non existing nic id",
VersioningErrorCodes.VERSIONABLE_SUB_ENTITY_NOT_FOUND);
}
@Test
public void testGet() {
NicEntity expected = createNic(VSP_ID, VERSION, COMPONENT_ID, NIC1_ID, NETWORK1_ID);
doReturn(expected).when(nicDao).get(anyObject());
String compositionSchema = "schema string";
doReturn(compositionSchema).when(nicManager).getNicCompositionSchema(anyObject());
CompositionEntityResponse<Nic> response =
nicManager.getNic(VSP_ID, VERSION, COMPONENT_ID, NIC1_ID);
Assert.assertEquals(response.getId(), expected.getId());
Assert.assertEquals(response.getData(), expected.getNicCompositionData());
Assert.assertEquals(response.getSchema(), compositionSchema);
}
/*
@Test(dependsOnMethods = {"testUpdateOnUploadVsp", "testList"})
public void testCreateWithRemovedName() {
testCreate(VSP_ID, COMPONENT_ID);
}
@Test
public void testDeleteNonExistingNicId_negative() {
testDelete_negative(VSP_ID, COMPONENT_ID, "non existing nic id", VersioningErrorCodes.VERSIONABLE_SUB_ENTITY_NOT_FOUND);
}
@Test(dependsOnMethods = "testList")
public void testDeleteNonExistingComponentId_negative() {
testDelete_negative(VSP_ID, "non existing component id", NIC1_ID, VersioningErrorCodes.VERSIONABLE_SUB_ENTITY_NOT_FOUND);
}*/
/*
@Test(dependsOnMethods = "testList")
public void testDelete() {
nicManager.deleteNic(VSP_ID, COMPONENT_ID, NIC1_ID);
NicEntity actual = vendorSoftwareProductDao.getNic(VSP_ID, VERSION, COMPONENT_ID, NIC1_ID);
Assert.assertNull(actual);
}*/
@Test
public void testDeleteOnUploadVsp_negative() {
testDelete_negative(VSP_ID, VERSION, COMPONENT_ID, NIC1_ID,
VendorSoftwareProductErrorCodes.DELETE_NIC_NOT_ALLOWED);
}
@Test(expectedExceptions = CoreException.class,
expectedExceptionsMessageRegExp = NIC_NOT_EXIST_MSG)
public void testGetNonExistingNicQuestionnaire() throws Exception {
nicManager.getNicQuestionnaire(VSP_ID, VERSION, COMPONENT_ID, NIC1_ID);
}
@Test
public void testGetQuestionnaire() throws Exception {
NicEntity nic = new NicEntity(VSP_ID, VERSION, COMPONENT_ID, NIC1_ID);
nic.setQuestionnaireData("{}");
doReturn(nic).when(nicDao).getQuestionnaireData(VSP_ID, VERSION, COMPONENT_ID, NIC1_ID);
String schema = "schema string";
doReturn(schema).when(nicManager).getNicQuestionnaireSchema(anyObject());
QuestionnaireResponse questionnaire =
nicManager.getNicQuestionnaire(VSP_ID, VERSION, COMPONENT_ID, NIC1_ID);
Assert.assertNotNull(questionnaire);
Assert.assertEquals(questionnaire.getData(), nic.getQuestionnaireData());
Assert.assertEquals(questionnaire.getSchema(), schema);
Assert.assertNull(questionnaire.getErrorMessage());
}
@Test(expectedExceptions = CoreException.class,
expectedExceptionsMessageRegExp = NIC_NOT_EXIST_MSG)
public void testUpdateNonExistingNicQuestionnaire() throws Exception {
doReturn(null).when(nicDao).get(anyObject());
nicManager
.updateNicQuestionnaire(VSP_ID, VERSION, COMPONENT_ID, NIC1_ID, "questionnaire data");
}
@Test
public void testUpdateQuestionnaire() throws Exception {
}
private void testCreate_negative(NicEntity nic, String expectedErrorCode) {
try {
nicManager.createNic(nic);
Assert.fail();
} catch (CoreException exception) {
Assert.assertEquals(exception.code().id(), expectedErrorCode);
}
}
private void testGet_negative(String vspId, Version version, String componentId, String nicId,
String expectedErrorCode) {
try {
nicManager.getNic(vspId, version, componentId, nicId);
Assert.fail();
} catch (CoreException exception) {
Assert.assertEquals(exception.code().id(), expectedErrorCode);
}
}
private void testUpdate_negative(String vspId, Version version, String componentId, String nicId,
String expectedErrorCode) {
try {
nicManager.updateNic(new NicEntity(vspId, version, componentId, nicId));
Assert.fail();
} catch (CoreException exception) {
Assert.assertEquals(exception.code().id(), expectedErrorCode);
}
}
private void testDelete_negative(String vspId, Version version, String componentId, String nicId,
String expectedErrorCode) {
try {
nicManager.deleteNic(vspId, version, componentId, nicId);
Assert.fail();
} catch (CoreException exception) {
Assert.assertEquals(exception.code().id(), expectedErrorCode);
}
}
static NicEntity createNic(String vspId, Version version, String compId, String nicId,
String networkId) {
NicEntity nicEntity = new NicEntity(vspId, version, compId, nicId);
Nic nicData = new Nic();
nicData.setName(nicId + "_name");
nicData.setDescription(nicId + " desc");
nicData.setNetworkId(networkId);
nicEntity.setNicCompositionData(nicData);
return nicEntity;
}
}
|