Adding unlimited max value to VNF, NETWORK
[vid.git] / vid-webpack-master / src / app / drawingBoard / service-planning / duplicate / duplicate.service.spec.ts
1 import {DuplicateService} from './duplicate.service';
2 import {LogService} from '../../../shared/utils/log/log.service';
3 import {NgRedux} from '@angular-redux/store';
4 import {ITreeNode} from "angular-tree-component/dist/defs/api";
5 import {SdcUiServices} from "onap-ui-angular";
6 import {IModalConfig} from 'onap-ui-angular/dist/components/common';
7 import {AppState} from "../../../shared/store/reducers";
8 import {getTestBed, TestBed} from "@angular/core/testing";
9 import {FeatureFlagsService} from "../../../shared/services/featureFlag/feature-flags.service";
10
11 class MockAppStore<T> {
12   getState(){
13     return {
14       getState() {
15         return {
16           service : {
17             serviceHierarchy: {
18               "serviceId" : {
19                 vnfs : {
20                   "vnfModelName" : {
21                     properties : {
22                       max_instances : 2
23                     }
24                   },
25                   "vnfModelName2" : {
26                     properties : {
27                       max_instances  : 2
28                     }
29                   },
30                   "vnfModelName3" : {
31                     properties : {
32                     }
33                   }
34                 }
35               }
36             },
37             serviceInstance : {
38               "serviceId" : {
39                 existingVNFCounterMap : {
40                   "vnfModelId" : 1,
41                   "vnfModelId2" : 2,
42                   "vnfModelId3" : 0
43                 }
44               }
45
46             }
47           }
48         }
49       }
50     }
51   }
52 }
53
54 class MockModalService<T> {}
55
56 class MockFeatureFlagsService extends  FeatureFlagsService{
57   getAllFlags(): { [p: string]: boolean } {
58     return {};
59   }
60 }
61
62 describe('Drawing board tree service', () => {
63   let injector;
64   let service: DuplicateService;
65   let store : NgRedux<AppState>;
66   let featureFlagsService : FeatureFlagsService;
67   beforeAll(done => (async () => {
68     TestBed.configureTestingModule({
69       providers : [
70         DuplicateService,
71         LogService,
72         {provide: FeatureFlagsService, useClass: MockFeatureFlagsService},
73         {provide: NgRedux, useClass: MockAppStore},
74         {provide: SdcUiServices.ModalService, useClass: MockModalService}
75       ]
76     });
77     await TestBed.compileComponents();
78
79     injector = getTestBed();
80     service = injector.get(DuplicateService);
81     store = injector.get(NgRedux);
82     featureFlagsService = injector.get(FeatureFlagsService);
83
84   })().then(done).catch(done.fail));
85
86
87   test('setNumberOfDuplicates should set number of duplicates', ()=>{
88     service.setNumberOfDuplicates(10);
89     expect(service.numberOfDuplicates).toEqual(10);
90   });
91
92   test('isEnabled should return false if type is VNF and has missing data', ()=>{
93     let node  = {
94       data : {
95         type : 'VF',
96         menuActions : {
97           duplicate : () => {
98
99           }
100         }
101       }
102     };
103     spyOn(node.data.menuActions, 'duplicate').and.returnValue(true);
104     spyOn(service, 'hasMissingData').and.returnValue(true);
105     let result : boolean = service.isEnabled(<any>node, null, null);
106     expect(result).toBeFalsy();
107   });
108
109   test('openDuplicateModal', ()=>{
110     spyOn(service, 'getRemainsInstance').and.returnValue(1);
111     let result : IModalConfig = service.openDuplicateModal(
112       'currentServiceId',
113       'currentServiceId',
114       'currentId',
115       'storeKey',
116       2,
117       null,null);
118     expect(result.title).toEqual('Duplicate Node');
119   });
120
121   test('openDuplicateModal should call getRemainsInstance with correct parameters', ()=>{
122     spyOn(service, 'getRemainsInstance');
123     service.openDuplicateModal(
124       'currentServiceId',
125       'currentServiceId',
126       'currentId',
127       'storeKey',
128       2,
129       null,null);
130     expect(service.getRemainsInstance).toHaveBeenCalledWith('currentServiceId', 'currentId', 'currentServiceId', null, null);
131   });
132
133   test('canDuplicate VNF should return true', () => {
134     let node : ITreeNode = <any> {data : {type : 'VF'}};
135
136     let result = service.canDuplicate(node);
137     expect(result).toBeTruthy();
138   });
139
140   test('canDuplicate Network should return true', () => {
141     let node : ITreeNode = <any> {data : {type : 'VL'}};
142
143     let result = service.canDuplicate(node);
144     expect(result).toBeTruthy();
145   });
146
147   test('canDuplicate VFModule should return false', () => {
148     let node : ITreeNode = <any> {data : {type : 'VFModule'}};
149
150     let result = service.canDuplicate(node);
151     expect(result).toBeFalsy();
152   });
153
154   test('Drawing board tree service should be defined', () => {
155     expect(service).toBeDefined();
156   });
157
158   test('DrawingBoardDuplicateService should be defined', () => {
159     expect(service).toBeDefined();
160   });
161
162   test('Duplicate multi vnfs should save multi vnfs in redux', () => {
163     service.existingNames = {
164       "2017488_pasqualevpe": "",
165       "rrr": "",
166       "ttt": ""
167     };
168     let newVnfs = service.cloneVnf(<any>{
169       "vfModules": {
170         "2017488_pasqualevpe0..2017488PasqualeVpe..PASQUALE_vPFE_BV..module-2": {
171           "2017488_pasqualevpe0..2017488PasqualeVpe..PASQUALE_vPFE_BV..module-2mtlfi": {
172             "instanceName": "rrr",
173             "volumeGroupName": "ttt"
174           }
175         }
176       },
177       "originalName": null,
178       "trackById": "pfs1f0len3",
179       "instanceName": "2017488_PASQUALEvPE"
180     }, "2017-488_PASQUALE-vPE 0");
181
182     expect(newVnfs.instanceName).toBe("2017488_PASQUALEvPE_001");
183     expect(newVnfs.vfModules['2017488_pasqualevpe0..2017488PasqualeVpe..PASQUALE_vPFE_BV..module-2']['2017488_pasqualevpe0..2017488PasqualeVpe..PASQUALE_vPFE_BV..module-2mtlfi'].instanceName).toBe("rrr_001");
184     expect(newVnfs.vfModules['2017488_pasqualevpe0..2017488PasqualeVpe..PASQUALE_vPFE_BV..module-2']['2017488_pasqualevpe0..2017488PasqualeVpe..PASQUALE_vPFE_BV..module-2mtlfi'].volumeGroupName).toBe("ttt_001");
185
186     newVnfs = service.cloneVnf(<any>{
187       "vfModules": {
188         "2017488_pasqualevpe0..2017488PasqualeVpe..PASQUALE_vPFE_BV..module-2": {
189           "2017488_pasqualevpe0..2017488PasqualeVpe..PASQUALE_vPFE_BV..module-2mtlfi": {
190             "instanceName": "rrr",
191             "volumeGroupName": "ttt"
192           }
193         }
194       },
195       "originalName": null,
196       "trackById": "pfs1f0len3",
197       "instanceName": "2017488_PASQUALEvPE"
198     }, "2017-488_PASQUALE-vPE 0");
199
200     expect(newVnfs.instanceName).toBe("2017488_PASQUALEvPE_002");
201     expect(newVnfs.vfModules['2017488_pasqualevpe0..2017488PasqualeVpe..PASQUALE_vPFE_BV..module-2']['2017488_pasqualevpe0..2017488PasqualeVpe..PASQUALE_vPFE_BV..module-2mtlfi'].instanceName).toBe("rrr_002");
202     expect(newVnfs.vfModules['2017488_pasqualevpe0..2017488PasqualeVpe..PASQUALE_vPFE_BV..module-2']['2017488_pasqualevpe0..2017488PasqualeVpe..PASQUALE_vPFE_BV..module-2mtlfi'].volumeGroupName).toBe("ttt_002");
203   });
204
205   test('ensure name is unique - send new name - shouldn\'t change name', () => {
206     service.existingNames = {
207       "2017488_pasqualevpe": "",
208       "uniqueinstancename": ""
209     };
210     const name = "uniqueInstanceName-1";
211     let uniqueName: string = service.ensureUniqueNameOrGenerateOne(name);
212     expect(uniqueName).toEqual(name);
213   });
214
215   test('ensure name is unique send existing name should change name', () => {
216     service.existingNames = {
217       "2017488_pasqualevpe": "",
218       "uniqueinstancename-1": ""
219     };
220     const name = "uniqueInstanceName-1";
221     let uniqueName: string = service.ensureUniqueNameOrGenerateOne(name);
222     expect(uniqueName).toEqual(name + "_001");
223   });
224
225   test('isAlreadyExist - send new name should return false', () => {
226     service.existingNames = {
227       "2017488_pasqualevpe": "",
228       "uniqueinstancename": ""
229     };
230     const name = "uniqueinstancename-1";
231     let isExist: boolean = service.isAlreadyExists(name, service.existingNames);
232     expect(isExist).toBeFalsy();
233   });
234
235   test('isAlreadyExist - send existing name should return true', () => {
236     service.existingNames = {
237       "2017488_pasqualevpe": "",
238       "uniqueinstancename-1": ""
239     };
240     const name = "uniqueinstancename-1";
241     let isExist: boolean = service.isAlreadyExists(name, service.existingNames);
242     expect(isExist).toBeTruthy();
243   });
244
245   test('isAlreadyExist - send existing name case insensitive should return true', () => {
246     service.existingNames = {
247       "2017488_pasqualevpe": "",
248       "uniqueinstancename-1": ""
249     };
250     const name = "uniqueInstanceName-1";
251     let isExist: boolean = service.isAlreadyExists(name, service.existingNames);
252     expect(isExist).toBeTruthy();
253   });
254
255   test('getNumberAsPaddingString should return 001 if padding is 000 and val is 1 string', () => {
256     let paddingNumber: string = service.getNumberAsPaddingString(1, '0000');
257     expect(paddingNumber).toEqual('0001');
258   });
259
260   test('getNumberAsPaddingString should return 010 if padding is 000 and val is 10 string', () => {
261     let paddingNumber: string = service.getNumberAsPaddingString(10, '0000');
262     expect(paddingNumber).toEqual('0010');
263   });
264
265   test('generateVNFUniqueName should return the next free number', () => {
266     const vnfName: string = "VF_vGeraldine 0";
267     let result: string = service.generateUniqueStoreKey(
268       "6e59c5de-f052-46fa-aa7e-2fca9d674c44",
269       vnfName,
270       getExistingVNFs(),
271       {}
272     );
273
274     expect(result).toEqual(vnfName + ":0003");
275   });
276
277   test('generateVNFUniqueName on duplicate multi vnfs should return the next free number', () => {
278       spyOn(service, 'openDuplicateModal');
279       service.openDuplicateModal(null, null, null,null, 2, null, null);
280
281       expect(service.openDuplicateModal).toHaveBeenCalledWith(null, null, null,null, 2, null, null);
282   });
283
284
285   test('isEnabled should return false if node has  missing data', () => {
286     let node  = {
287       data : {
288         missingData: true,
289         menuActions : {
290           duplicate : () => {
291
292           }
293         }
294       }
295     };
296     let result : boolean = service.isEnabled(<any>node, null, null);
297     expect(result).toBeFalsy();
298   });
299
300   test('isEnabled should return true if not reach to max', () => {
301     let node  = {
302       data : {
303         missingData: false, modelName : "vnfModelName" , modelId : "vnfModelId", type : "VF", children: [], modelCustomizationId : "vnfModelId",modelUniqueId: "vnfModelId",
304         menuActions : {
305           duplicate : () => {
306
307           }
308         }
309       }
310     };
311     let result : boolean = service.isEnabled(<any>node, <any>getStoreState(), "serviceId");
312     expect(result).toBeTruthy();
313   });
314
315   test('isEnabled should return false if reach to max', () => {
316     let node  = {
317       data : {
318         missingData: false, modelName : "vnfModelName2" , modelId : "vnfModelId2", type : "VF" ,children: [],
319         menuActions : {
320           duplicate : () => {
321
322           }
323         }
324       }
325     };
326     let result : boolean = service.isEnabled(<any>node, <any>getStoreState(), "serviceId");
327     expect(result).toBeFalsy();
328   });
329
330   test('isEnabled should return true if max is null', () => {
331     let node  = {
332       data : {
333         missingData: false, modelName : "vnfModelName3" , modelId : "vnfModelId3",type : "VF",  children: [], modelUniqueId: "vnfModelId3",
334         menuActions : {
335           duplicate : () => {
336
337           }
338         }
339       }
340     };
341     let result : boolean = service.isEnabled(<any>node, <any>getStoreState(), "serviceId");
342     expect(result).toBeTruthy();
343   });
344
345   test('isEnabled should return false if type is vf module', () => {
346     let node  = {
347       data : {
348         missingData: false, modelName : "vnfModelName3" , modelId : "vnfModelId3",type : "VFModule",  children: [],
349         menuActions : {
350         }
351       }
352     };
353     let result : boolean = service.isEnabled(<any>node, <any>getStoreState(), "serviceId");
354     expect(result).toBeFalsy();
355   });
356
357   test('getRemainsVNFinstance should return the remains 1 VNF', () => {
358     let result : number = service.getRemainsInstance("vnfModelId", "vnfModelName" , "serviceId" , <any>getStoreState(), <any>{data : {type : 'VF'}});
359     expect(result).toBe(1);
360   });
361
362   test('getRemainsVNFinstance should return the remains  0 VNF', () => {
363     let result : number = service.getRemainsInstance("vnfModelId2", "vnfModelName2" , "serviceId" , <any>getStoreState(),<any>{data : {type : 'VF'}});
364     expect(result).toBe(0);
365   });
366
367   test('isVNFChildrensHasMissingData should return true if vnf missing data = true', () => {
368     let result : boolean = service.hasMissingData(<any>getTreeNode(true,false));
369     expect(result).toBeTruthy();
370   });
371
372   test('isVNFChildrensHasMissingData return should false if vnf missing data = false', () => {
373     let result : boolean = service.hasMissingData(<any>getTreeNode(false,false));
374     expect(result).toBeFalsy();
375   });
376
377   test('isVNFChildrensHasMissingData should return true if vfModule missing data = true', () => {
378     let result : boolean = service.hasMissingData(<any>getTreeNode(false,true));
379     expect(result).toBeTruthy();
380   });
381
382   test('isVNFChildrensHasMissingData should return false if no children and  vfModule missing data = false', () => {
383     let node = <any>getTreeNode(false,false);
384     node.data.children = [];
385     let result : boolean = service.hasMissingData(node);
386     expect(result).toBeFalsy();
387   });
388
389   function getExistingVNFs(){
390     return {"VF_vGeraldine 0":{"rollbackOnFailure":"true","vfModules":{"vf_vgeraldine0..VfVgeraldine..vflorence_vlc..module-1":{"vf_vgeraldine0..VfVgeraldine..vflorence_vlc..module-1dgbxq":{"modelInfo":{"modelInvariantId":"98a7c88b-b577-476a-90e4-e25a5871e02b","modelVersionId":"522159d5-d6e0-4c2a-aa44-5a542a12a830","modelName":"VfVgeraldine..vflorence_vlc..module-1","modelVersion":"2","modelCustomizationId":"55b1be94-671a-403e-a26c-667e9c47d091","modelCustomizationName":"VfVgeraldine..vflorence_vlc..module-1"},"instanceParams":[{}]}}},"productFamilyId":"17cc1042-527b-11e6-beb8-9e71128cae77","lcpCloudRegionId":"hvf6","tenantId":"bae71557c5bb4d5aac6743a4e5f1d054","lineOfBusiness":"ONAP","platformName":"platform","modelInfo":{"modelInvariantId":"4160458e-f648-4b30-a176-43881ffffe9e","modelVersionId":"d6557200-ecf2-4641-8094-5393ae3aae60","modelName":"VF_vGeraldine","modelVersion":"2.0","modelCustomizationId":"91415b44-753d-494c-926a-456a9172bbb9","modelCustomizationName":"VF_vGeraldine 0"}},"VF_vGeraldine 0:0001":{"rollbackOnFailure":"true","vfModules":{"vf_vgeraldine0..VfVgeraldine..vflorence_vlc..module-1":{"vf_vgeraldine0..VfVgeraldine..vflorence_vlc..module-1dgbxq":{"modelInfo":{"modelInvariantId":"98a7c88b-b577-476a-90e4-e25a5871e02b","modelVersionId":"522159d5-d6e0-4c2a-aa44-5a542a12a830","modelName":"VfVgeraldine..vflorence_vlc..module-1","modelVersion":"2","modelCustomizationId":"55b1be94-671a-403e-a26c-667e9c47d091","modelCustomizationName":"VfVgeraldine..vflorence_vlc..module-1"},"instanceParams":[{}]}}},"productFamilyId":"17cc1042-527b-11e6-beb8-9e71128cae77","lcpCloudRegionId":"hvf6","tenantId":"bae71557c5bb4d5aac6743a4e5f1d054","lineOfBusiness":"ONAP","platformName":"platform","modelInfo":{"modelInvariantId":"4160458e-f648-4b30-a176-43881ffffe9e","modelVersionId":"d6557200-ecf2-4641-8094-5393ae3aae60","modelName":"VF_vGeraldine","modelVersion":"2.0","modelCustomizationId":"91415b44-753d-494c-926a-456a9172bbb9","modelCustomizationName":"VF_vGeraldine 0"},"originalName":"VF_vGeraldine 0"},"VF_vGeraldine 0:0002":{"rollbackOnFailure":"true","vfModules":{"vf_vgeraldine0..VfVgeraldine..vflorence_vlc..module-1":{"vf_vgeraldine0..VfVgeraldine..vflorence_vlc..module-1dgbxq":{"modelInfo":{"modelInvariantId":"98a7c88b-b577-476a-90e4-e25a5871e02b","modelVersionId":"522159d5-d6e0-4c2a-aa44-5a542a12a830","modelName":"VfVgeraldine..vflorence_vlc..module-1","modelVersion":"2","modelCustomizationId":"55b1be94-671a-403e-a26c-667e9c47d091","modelCustomizationName":"VfVgeraldine..vflorence_vlc..module-1"},"instanceParams":[{}]}}},"productFamilyId":"17cc1042-527b-11e6-beb8-9e71128cae77","lcpCloudRegionId":"hvf6","tenantId":"bae71557c5bb4d5aac6743a4e5f1d054","lineOfBusiness":"ONAP","platformName":"platform","modelInfo":{"modelInvariantId":"4160458e-f648-4b30-a176-43881ffffe9e","modelVersionId":"d6557200-ecf2-4641-8094-5393ae3aae60","modelName":"VF_vGeraldine","modelVersion":"2.0","modelCustomizationId":"91415b44-753d-494c-926a-456a9172bbb9","modelCustomizationName":"VF_vGeraldine 0"},"originalName":"VF_vGeraldine 0"}}
391   }
392
393   function getStoreState(){
394     return {
395       getState() {
396         return {
397           service : {
398             serviceHierarchy: {
399               "serviceId" : {
400                 vnfs : {
401                   "vnfModelName" : {
402                     properties : {
403                       ecomp_generated_naming: "false",
404                       max_instances : 2
405                     }
406                   },
407                   "vnfModelName2" : {
408                     properties : {
409                       ecomp_generated_naming: "false",
410                       max_instances  : 2
411                     }
412                   },
413                   "vnfModelName3" : {
414                     properties : {
415                       ecomp_generated_naming: "false",
416                     }
417                   }
418                 }
419               }
420             },
421             serviceInstance : {
422               "serviceId" : {
423                 existingVNFCounterMap : {
424                   "vnfModelId" : 1,
425                   "vnfModelId2" : 2,
426                   "vnfModelId3" : 0
427                 }
428               }
429
430             }
431           },
432           global : {
433             flags : {
434
435             }
436           }
437         }
438       }
439     }
440   }
441
442
443   function getTreeNode(VNfMissingData : boolean, vfModuleMissingData : boolean){
444     return {
445       data : {
446         children : vfModuleMissingData ?
447           [{
448             missingData : true
449           }] :
450           [{
451             missingData : false
452           }
453         ],
454         missingData : VNfMissingData
455       }
456     }
457   }
458 });
459
460
461