aboutsummaryrefslogtreecommitdiffstats
path: root/vid-webpack-master/src/app/drawingBoard/service-planning/duplicate/duplicate.service.spec.ts
blob: 6423e8ad1141db7004e662cc7ba676f214ca48d1 (plain)
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
import {DuplicateService} from './duplicate.service';
import {LogService} from '../../../shared/utils/log/log.service';
import {NgRedux} from '@angular-redux/store';
import {ITreeNode} from "angular-tree-component/dist/defs/api";
import {SdcUiServices} from "onap-ui-angular";
import {IModalConfig} from 'onap-ui-angular/dist/components/common';
import {AppState} from "../../../shared/store/reducers";
import {getTestBed, TestBed} from "@angular/core/testing";
import {FeatureFlagsService} from "../../../shared/services/featureFlag/feature-flags.service";
import {SharedTreeService} from "../objectsToTree/shared.tree.service";

class MockAppStore<T> {
  getState(){
    return {
      getState() {
        return {
          service : {
            serviceHierarchy: {
              "serviceId" : {
                vnfs : {
                  "vnfModelName" : {
                    properties : {
                      max_instances : 2
                    }
                  },
                  "vnfModelName2" : {
                    properties : {
                      max_instances  : 2
                    }
                  },
                  "vnfModelName3" : {
                    properties : {
                    }
                  }
                }
              }
            },
            serviceInstance : {
              "serviceId" : {
                existingVNFCounterMap : {
                  "vnfModelId" : 1,
                  "vnfModelId2" : 2,
                  "vnfModelId3" : 0
                }
              }

            }
          }
        }
      }
    }
  }
}

class MockModalService<T> {}

class MockFeatureFlagsService extends  FeatureFlagsService{
  getAllFlags(): { [p: string]: boolean } {
    return {};
  }
}

describe('Drawing board tree service', () => {
  let injector;
  let service: DuplicateService;
  let store : NgRedux<AppState>;
  let featureFlagsService : FeatureFlagsService;
  beforeAll(done => (async () => {
    TestBed.configureTestingModule({
      providers : [
        DuplicateService,
        SharedTreeService,
        LogService,
        {provide: FeatureFlagsService, useClass: MockFeatureFlagsService},
        {provide: NgRedux, useClass: MockAppStore},
        {provide: SdcUiServices.ModalService, useClass: MockModalService}
      ]
    });
    await TestBed.compileComponents();

    injector = getTestBed();
    service = injector.get(DuplicateService);
    store = injector.get(NgRedux);
    featureFlagsService = injector.get(FeatureFlagsService);

  })().then(done).catch(done.fail));


  test('setNumberOfDuplicates should set number of duplicates', ()=>{
    service.setNumberOfDuplicates(10);
    expect(service.numberOfDuplicates).toEqual(10);
  });

  test('isEnabled should return false if type is VNF and has missing data', ()=>{
    let node  = {
      data : {
        type : 'VF',
        menuActions : {
          duplicate : () => {

          }
        }
      }
    };
    spyOn(node.data.menuActions, 'duplicate').and.returnValue(true);
    spyOn(service, 'hasMissingData').and.returnValue(true);
    let result : boolean = service.isEnabled(<any>node, null, null);
    expect(result).toBeFalsy();
  });

  test('openDuplicateModal', ()=>{
    spyOn(service, 'getRemainsInstance').and.returnValue(1);
    let result : IModalConfig = service.openDuplicateModal(
      'currentServiceId',
      'currentServiceId',
      'currentId',
      'storeKey',
      2,
      null,null);
    expect(result.title).toEqual('Duplicate Node');
  });

  test('openDuplicateModal should call getRemainsInstance with correct parameters', ()=>{
    spyOn(service, 'getRemainsInstance');
    service.openDuplicateModal(
      'currentServiceId',
      'currentServiceId',
      'currentId',
      'storeKey',
      2,
      null,null);
    expect(service.getRemainsInstance).toHaveBeenCalledWith('currentServiceId', 'currentId', 'currentServiceId', null, null);
  });

  test('canDuplicate VNF should return true', () => {
    let node : ITreeNode = <any> {data : {type : 'VF'}};

    let result = service.canDuplicate(node);
    expect(result).toBeTruthy();
  });

  test('canDuplicate Network should return true', () => {
    let node : ITreeNode = <any> {data : {type : 'VL'}};

    let result = service.canDuplicate(node);
    expect(result).toBeTruthy();
  });

  test('canDuplicate VFModule should return false', () => {
    let node : ITreeNode = <any> {data : {type : 'VFModule'}};

    let result = service.canDuplicate(node);
    expect(result).toBeFalsy();
  });

  test('Drawing board tree service should be defined', () => {
    expect(service).toBeDefined();
  });

  test('DrawingBoardDuplicateService should be defined', () => {
    expect(service).toBeDefined();
  });

  test('Duplicate multi vnfs should save multi vnfs in redux', () => {
    service.existingNames = {
      "2017488_pasqualevpe": "",
      "rrr": "",
      "ttt": ""
    };
    let newVnfs = service.cloneVnf(<any>{
      "vfModules": {
        "2017488_pasqualevpe0..2017488PasqualeVpe..PASQUALE_vPFE_BV..module-2": {
          "2017488_pasqualevpe0..2017488PasqualeVpe..PASQUALE_vPFE_BV..module-2mtlfi": {
            "instanceName": "rrr",
            "volumeGroupName": "ttt"
          }
        }
      },
      "originalName": null,
      "trackById": "pfs1f0len3",
      "instanceName": "2017488_PASQUALEvPE"
    }, "2017-488_PASQUALE-vPE 0");

    expect(newVnfs.instanceName).toBe("2017488_PASQUALEvPE_001");
    expect(newVnfs.vfModules['2017488_pasqualevpe0..2017488PasqualeVpe..PASQUALE_vPFE_BV..module-2']['2017488_pasqualevpe0..2017488PasqualeVpe..PASQUALE_vPFE_BV..module-2mtlfi'].instanceName).toBe("rrr_001");
    expect(newVnfs.vfModules['2017488_pasqualevpe0..2017488PasqualeVpe..PASQUALE_vPFE_BV..module-2']['2017488_pasqualevpe0..2017488PasqualeVpe..PASQUALE_vPFE_BV..module-2mtlfi'].volumeGroupName).toBe("ttt_001");

    newVnfs = service.cloneVnf(<any>{
      "vfModules": {
        "2017488_pasqualevpe0..2017488PasqualeVpe..PASQUALE_vPFE_BV..module-2": {
          "2017488_pasqualevpe0..2017488PasqualeVpe..PASQUALE_vPFE_BV..module-2mtlfi": {
            "instanceName": "rrr",
            "volumeGroupName": "ttt"
          }
        }
      },
      "originalName": null,
      "trackById": "pfs1f0len3",
      "instanceName": "2017488_PASQUALEvPE"
    }, "2017-488_PASQUALE-vPE 0");

    expect(newVnfs.instanceName).toBe("2017488_PASQUALEvPE_002");
    expect(newVnfs.vfModules['2017488_pasqualevpe0..2017488PasqualeVpe..PASQUALE_vPFE_BV..module-2']['2017488_pasqualevpe0..2017488PasqualeVpe..PASQUALE_vPFE_BV..module-2mtlfi'].instanceName).toBe("rrr_002");
    expect(newVnfs.vfModules['2017488_pasqualevpe0..2017488PasqualeVpe..PASQUALE_vPFE_BV..module-2']['2017488_pasqualevpe0..2017488PasqualeVpe..PASQUALE_vPFE_BV..module-2mtlfi'].volumeGroupName).toBe("ttt_002");
  });

  test('ensure name is unique - send new name - shouldn\'t change name', () => {
    service.existingNames = {
      "2017488_pasqualevpe": "",
      "uniqueinstancename": ""
    };
    const name = "uniqueInstanceName-1";
    let uniqueName: string = service.ensureUniqueNameOrGenerateOne(name);
    expect(uniqueName).toEqual(name);
  });

  test('ensure name is unique send existing name should change name', () => {
    service.existingNames = {
      "2017488_pasqualevpe": "",
      "uniqueinstancename-1": ""
    };
    const name = "uniqueInstanceName-1";
    let uniqueName: string = service.ensureUniqueNameOrGenerateOne(name);
    expect(uniqueName).toEqual(name + "_001");
  });

  test('isAlreadyExist - send new name should return false', () => {
    service.existingNames = {
      "2017488_pasqualevpe": "",
      "uniqueinstancename": ""
    };
    const name = "uniqueinstancename-1";
    let isExist: boolean = service.isAlreadyExists(name, service.existingNames);
    expect(isExist).toBeFalsy();
  });

  test('isAlreadyExist - send existing name should return true', () => {
    service.existingNames = {
      "2017488_pasqualevpe": "",
      "uniqueinstancename-1": ""
    };
    const name = "uniqueinstancename-1";
    let isExist: boolean = service.isAlreadyExists(name, service.existingNames);
    expect(isExist).toBeTruthy();
  });

  test('isAlreadyExist - send existing name case insensitive should return true', () => {
    service.existingNames = {
      "2017488_pasqualevpe": "",
      "uniqueinstancename-1": ""
    };
    const name = "uniqueInstanceName-1";
    let isExist: boolean = service.isAlreadyExists(name, service.existingNames);
    expect(isExist).toBeTruthy();
  });

  test('getNumberAsPaddingString should return 001 if padding is 000 and val is 1 string', () => {
    let paddingNumber: string = service.getNumberAsPaddingString(1, '0000');
    expect(paddingNumber).toEqual('0001');
  });

  test('getNumberAsPaddingString should return 010 if padding is 000 and val is 10 string', () => {
    let paddingNumber: string = service.getNumberAsPaddingString(10, '0000');
    expect(paddingNumber).toEqual('0010');
  });

  test('generateVNFUniqueName should return the next free number', () => {
    const vnfName: string = "VF_vGeraldine 0";
    let result: string = service.generateUniqueStoreKey(
      "6e59c5de-f052-46fa-aa7e-2fca9d674c44",
      vnfName,
      getExistingVNFs(),
      {}
    );

    expect(result).toEqual(vnfName + ":0003");
  });

  test('generateVNFUniqueName on duplicate multi vnfs should return the next free number', () => {
      spyOn(service, 'openDuplicateModal');
      service.openDuplicateModal(null, null, null,null, 2, null, null);

      expect(service.openDuplicateModal).toHaveBeenCalledWith(null, null, null,null, 2, null, null);
  });


  test('isEnabled should return false if node has  missing data', () => {
    let node  = {
      data : {
        missingData: true,
        menuActions : {
          duplicate : () => {

          }
        }
      }
    };
    let result : boolean = service.isEnabled(<any>node, null, null);
    expect(result).toBeFalsy();
  });

  test('isEnabled should return true if not reach to max', () => {
    let node  = {
      data : {
        missingData: false, modelName : "vnfModelName" , modelId : "vnfModelId", type : "VF", children: [], modelCustomizationId : "vnfModelId",modelUniqueId: "vnfModelId",
        menuActions : {
          duplicate : () => {

          }
        }
      }
    };
    let result : boolean = service.isEnabled(<any>node, <any>getStoreState(), "serviceId");
    expect(result).toBeTruthy();
  });

  test('isEnabled should return false if reach to max', () => {
    let node  = {
      data : {
        missingData: false, modelName : "vnfModelName2" , modelId : "vnfModelId2", type : "VF" ,children: [],
        menuActions : {
          duplicate : () => {

          }
        }
      }
    };
    let result : boolean = service.isEnabled(<any>node, <any>getStoreState(), "serviceId");
    expect(result).toBeFalsy();
  });

  test('isEnabled should return true if max is null', () => {
    let node  = {
      data : {
        missingData: false, modelName : "vnfModelName3" , modelId : "vnfModelId3",type : "VF",  children: [], modelUniqueId: "vnfModelId3",
        menuActions : {
          duplicate : () => {

          }
        }
      }
    };
    let result : boolean = service.isEnabled(<any>node, <any>getStoreState(), "serviceId");
    expect(result).toBeTruthy();
  });

  test('isEnabled should return false if type is vf module', () => {
    let node  = {
      data : {
        missingData: false, modelName : "vnfModelName3" , modelId : "vnfModelId3",type : "VFModule",  children: [],
        menuActions : {
        }
      }
    };
    let result : boolean = service.isEnabled(<any>node, <any>getStoreState(), "serviceId");
    expect(result).toBeFalsy();
  });

  test('getRemainsVNFinstance should return the remains 1 VNF', () => {
    let result : number = service.getRemainsInstance("vnfModelId", "vnfModelName" , "serviceId" , <any>getStoreState(), <any>{data : {type : 'VF'}});
    expect(result).toBe(1);
  });

  test('getRemainsVNFinstance should return the remains  0 VNF', () => {
    let result : number = service.getRemainsInstance("vnfModelId2", "vnfModelName2" , "serviceId" , <any>getStoreState(),<any>{data : {type : 'VF'}});
    expect(result).toBe(0);
  });

  test('isVNFChildrensHasMissingData should return true if vnf missing data = true', () => {
    let result : boolean = service.hasMissingData(<any>getTreeNode(true,false));
    expect(result).toBeTruthy();
  });

  test('isVNFChildrensHasMissingData return should false if vnf missing data = false', () => {
    let result : boolean = service.hasMissingData(<any>getTreeNode(false,false));
    expect(result).toBeFalsy();
  });

  test('isVNFChildrensHasMissingData should return true if vfModule missing data = true', () => {
    let result : boolean = service.hasMissingData(<any>getTreeNode(false,true));
    expect(result).toBeTruthy();
  });

  test('isVNFChildrensHasMissingData should return false if no children and  vfModule missing data = false', () => {
    let node = <any>getTreeNode(false,false);
    node.data.children = [];
    let result : boolean = service.hasMissingData(node);
    expect(result).toBeFalsy();
  });

  function getExistingVNFs(){
    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"}}
  }

  function getStoreState(){
    return {
      getState() {
        return {
          service : {
            serviceHierarchy: {
              "serviceId" : {
                vnfs : {
                  "vnfModelName" : {
                    properties : {
                      ecomp_generated_naming: "false",
                      max_instances : 2
                    }
                  },
                  "vnfModelName2" : {
                    properties : {
                      ecomp_generated_naming: "false",
                      max_instances  : 2
                    }
                  },
                  "vnfModelName3" : {
                    properties : {
                      ecomp_generated_naming: "false",
                    }
                  }
                }
              }
            },
            serviceInstance : {
              "serviceId" : {
                existingVNFCounterMap : {
                  "vnfModelId" : 1,
                  "vnfModelId2" : 2,
                  "vnfModelId3" : 0
                }
              }

            }
          },
          global : {
            flags : {

            }
          }
        }
      }
    }
  }


  function getTreeNode(VNfMissingData : boolean, vfModuleMissingData : boolean){
    return {
      data : {
        children : vfModuleMissingData ?
          [{
            missingData : true
          }] :
          [{
            missingData : false
          }
        ],
        missingData : VNfMissingData
      }
    }
  }
});