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