Relax VNF Change Management filters for VNFs
[vid.git] / vid-app-common / src / main / webapp / app / vid / scripts / modals / new-change-management / new-change-management.controller.test.js
1 /*-
2  * ============LICENSE_START=======================================================
3  * VID
4  * ================================================================================
5  * Copyright (C) 2017 - 2019 AT&T Intellectual Property. All rights reserved.
6  * Copyright (C) 2020 Nokia Intellectual Property. All rights reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  * 
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  * 
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 require('./new-change-management.controller');
23 const jestMock = require('jest-mock');
24
25 describe('Testing workFlows from SO', () => {
26   let $notNeeded;
27   let $controller;
28   let $changeManagementService;
29   let $featureFlags;
30
31   beforeEach(
32       angular.mock.module('app')
33   );
34
35   beforeEach(inject(function (_$controller_) {
36     $notNeeded = jestMock.fn();
37     let lodash = require('lodash')
38
39     // mock ChangeManagementService
40     $changeManagementService = jestMock.fn();
41     $changeManagementService.getAllSDCServices = jestMock.fn(() => Promise.resolve([]));
42
43     // mock q
44     $q = jestMock.fn();
45     $defer = jestMock.fn();
46     $flags = jestMock.fn();
47     $flags.FEATURE_FLAGS = {FLAG_HANDLE_SO_WORKFLOWS: ''};
48     $featureFlags = jestMock.fn();
49     $featureFlags.isOn = jestMock.fn(() => true);
50     $q.defer = jestMock.fn(() => $defer);
51     $defer.promise = Promise.resolve({});
52     // mock AaiService
53     $aaiService = jestMock.fn();
54     $aaiService.getLoggedInUserID = jestMock.fn();
55     $aaiService.getSubscribers = jestMock.fn();
56     $controller = _$controller_('newChangeManagementModalController', {
57       $uibModalInstance: $notNeeded,
58       $uibModal: $notNeeded,
59       $q: $q,
60       AaiService: $aaiService,
61       changeManagementService: $changeManagementService,
62       Upload: $notNeeded,
63       $log: $notNeeded,
64       _: lodash,
65       COMPONENT: $flags,
66       VIDCONFIGURATION: $notNeeded,
67       DataService: $notNeeded,
68       featureFlags: $featureFlags,
69       $scope: $notNeeded,
70     });
71   }));
72
73   test('Verify load workflows from SO will call getSOWorkflow and return only names of workflows', () => {
74     // given
75     $controller.changeManagement.vnfNames = [{name: 'test1'}, {name: "test2"}];
76     let getSOWorkflowsPromiseStub = Promise.resolve({"data": [{"id": "1", "name": "workflow 1"}, {"id": "2", "name": "workflow 2"}]});
77     $changeManagementService.getSOWorkflows = () => getSOWorkflowsPromiseStub;
78     $controller.workflows = [];
79     // when
80     return $controller.loadRemoteWorkFlows()
81     .then(() => {
82            remoteWorkflows = $controller.remoteWorkflows.map(item => item.name);
83            expect(remoteWorkflows).toContain('workflow 1');
84            expect(remoteWorkflows).toContain('workflow 2');
85         }
86      );
87   });
88
89   test('Verify load workflows wont load parameters from local service', () => {
90     // given
91     let getWorkflowsStub = Promise.resolve({"data": {"workflows": ["workflow 0"]}});
92     let getLocalWorkflowsParametersStub = Promise.resolve({"data":{
93         "parameterDefinitions": [
94           {
95             "id": 1,
96             "name": "Configuration Parameters",
97             "required": true,
98             "type": "text",
99             "pattern": ".*",
100             "msgOnPatternError": null,
101             "msgOnContentError": null,
102             "acceptableFileType": null
103           }
104         ],
105       }});
106     let getSOWorkflowsPromiseStub = Promise.resolve({"data":[{
107
108         "id": "ab6478e4-ea33-3346-ac12-ab121484a333",
109         "workflowName": "inPlaceSoftwareUpdate",
110         "name": "inPlaceSoftwareUpdate",
111         "source": "sdc",
112         "workflowInputParameters": [
113             {
114                 "label": "New Software Version",
115                 "inputType": "text",
116                 "required": true,
117                 "soFieldName": "new_software_version",
118                 "soPayloadLocation": "userParams",
119                 "validation":[]
120             }
121         ]
122     }]
123     });
124
125     $controller.changeManagement.vnfNames = [{modelVersionId: 'test1', name:'test'}];
126     $changeManagementService.getWorkflows = () => getWorkflowsStub;
127     $changeManagementService.getLocalWorkflowParameter = () => getLocalWorkflowsParametersStub;
128     $changeManagementService.getSOWorkflows = () =>  getSOWorkflowsPromiseStub;
129     // when
130     return $controller.loadWorkFlows().then(() => {
131       expect($controller.workflows).toContain('inPlaceSoftwareUpdate');
132       expect($controller.localWorkflowsParameters).toEqual(new Map());
133     });
134   });
135
136   test('Verify load workflows will set workflows and parameters', () => {
137     // given
138     let getWorkflowsStub = Promise.resolve({"data": {"workflows": ["workflow 0"]}});
139     let getLocalWorkflowsParametersStub = Promise.resolve({"data": {}});
140     let getSOWorkflowsPromiseStub = Promise.resolve({"data":[{
141
142             "id": "ab6478e4-ea33-3346-ac12-ab121484a333",
143             "workflowName": "inPlaceSoftwareUpdate",
144             "name": "inPlaceSoftwareUpdate",
145             "source": "sdc",
146           "workflowInputParameters": [
147             {
148               "label": "New Software Version",
149               "inputType": "text",
150               "required": true,
151               "soFieldName": "new_software_version",
152               "soPayloadLocation": "userParams",
153                 "validation":[]
154             }
155           ]
156         }]
157       });
158     $controller.changeManagement.vnfNames = [{modelVersionId: 'test1', name:'test'}];
159     $changeManagementService.getWorkflows = () => getWorkflowsStub;
160     $changeManagementService.getLocalWorkflowParameter = () => getLocalWorkflowsParametersStub;
161     $changeManagementService.getSOWorkflows = () =>  getSOWorkflowsPromiseStub;
162     $changeManagementService.getSOWorkflowParameter = () =>  getSOWorkflowsParametersPromiseStub;
163     // when
164     return $controller.loadWorkFlows()
165     .then(() => {
166       expect($controller.workflows).toEqual(["inPlaceSoftwareUpdate"]);
167       expect($controller.remoteWorkflowsParameters).toEqual(new Map([["inPlaceSoftwareUpdate",
168         [{
169           "name": "New Software Version",
170           "required": true,
171           "id": "new_software_version",
172           "soFieldName": "new_software_version",
173           "maxLength": '500',
174           "pattern": '.*',
175           "type": "text"
176         }]]
177       ]));
178     });
179   });
180
181   test('Verify load workflows wont load workflows parameters from SO if feature flag is disabled', () => {
182     // given
183     $featureFlags.isOn = jestMock.fn(() => false);
184     let getWorkflowsStub = Promise.resolve({"data": {"workflows": ["workflow 0"]}});
185     let getLocalWorkflowsParametersStub = Promise.resolve({"data": {}});
186     let getSOWorkflowsPromiseStub = Promise.resolve({"data": [{"id": "1", "name": "workflow 0"}]});
187     let getSOWorkflowsParametersPromiseStub = Promise.resolve({"data":{"parameterDefinitions": [
188           {"id": 1, "name": "parameter 1", "required": true, "type": "text", "pattern": "[0-9]*"},
189           {"id": 2, "name": "parameter 2", "required": true, "type": "text", "pattern": ".*"},
190           {"id": 3, "name": "parameter 3", "required": false, "type": "text", "pattern": "[0-9]*"}]}});
191
192     $controller.changeManagement.vnfNames = [{name: 'test1'}, {name: "test2"}];
193     $changeManagementService.getWorkflows = () => getWorkflowsStub;
194     $changeManagementService.getLocalWorkflowParameter = () => getLocalWorkflowsParametersStub;
195     $changeManagementService.getSOWorkflows = () =>  getSOWorkflowsPromiseStub;
196     $changeManagementService.getSOWorkflowParameter = () =>  getSOWorkflowsParametersPromiseStub;
197     // when
198     return $controller.loadWorkFlows()
199     .then(() => {
200       expect($controller.workflows).toEqual(["workflow 0"]);
201       expect($controller.remoteWorkflowsParameters).toEqual(new Map());
202     });
203   });
204
205   test('Verify load workflows will call load workflows parameters from local service', () => {
206     // given
207     $featureFlags.isOn = jestMock.fn(() => false);
208     let getWorkflowsStub = Promise.resolve({"data": {"workflows": ["VNF Scale Out"]}});
209     let getLocalWorkflowsParametersStub = Promise.resolve({"data":{
210         "parameterDefinitions": [
211           {
212             "id": 1,
213             "name": "Configuration Parameters",
214             "required": true,
215             "type": "text",
216             "pattern": ".*",
217             "msgOnPatternError": null,
218             "msgOnContentError": null,
219             "acceptableFileType": null
220           }
221         ],
222       }});
223
224     $controller.changeManagement.vnfNames = [{name: 'test1'}];
225     $changeManagementService.getWorkflows = () => getWorkflowsStub;
226     $changeManagementService.getLocalWorkflowParameter = () => getLocalWorkflowsParametersStub;
227     // when
228
229     let result = new Map();
230     const scaleOutResult = [
231       {
232         "acceptableFileType": null,
233         "id": 1,
234         "msgOnContentError": null,
235         "msgOnPatternError": null,
236         "name": "Configuration Parameters",
237         "pattern": ".*",
238         "required": true,
239         "type": "text",
240       }
241     ];
242     result.set("VNF Scale Out", scaleOutResult);
243
244     return $controller.loadWorkFlows()
245     .then(() => {
246       expect($controller.localWorkflowsParameters).toEqual(result);
247     });
248   });
249
250   test('Verify broken SO workflows will return empty list of workflows', () => {
251     // given
252     let getSOWorkflowsPromiseStub = Promise.reject(new Error("Broken SO workflows service."));
253
254     $controller.changeManagement.vnfNames = [{name:"any"}];
255     $changeManagementService.getSOWorkflows = () =>  getSOWorkflowsPromiseStub;
256     // when
257     $controller.loadWorkFlows()
258     .then(() => {
259       expect($controller.workflows).toEqual([]);
260     });
261   });
262
263   test('Verify get internal workflow parameters should return an empty list if not such workflow exist', () => {
264   // given
265     $featureFlags.isOn = jestMock.fn(() => false);
266     let getWorkflowsStub = Promise.resolve({"data": {"workflows": ["VNF Scale Out"]}});
267     let getLocalWorkflowsParametersStub = Promise.resolve({"data":{
268         "parameterDefinitions": [
269           {
270             "id": 1,
271             "name": "Configuration Parameters",
272             "required": true,
273             "type": "text",
274             "pattern": ".*",
275             "msgOnPatternError": null,
276             "msgOnContentError": null,
277             "acceptableFileType": null
278           }
279         ],
280       }});
281
282     $controller.changeManagement.vnfNames = [{name: 'test1'}];
283     $changeManagementService.getWorkflows = () => getWorkflowsStub;
284     $changeManagementService.getLocalWorkflowParameter = () => getLocalWorkflowsParametersStub;
285     // when
286     return $controller.loadWorkFlows()
287     .then(() => {
288       let internalWorkFlowParameters = $controller.getInternalWorkFlowParameters("NON-EXISTENT WF", "text");
289       expect(internalWorkFlowParameters).toEqual([]);
290     });
291   });
292
293   test('Verify get internal workflow parameters should return an empty list if not such type exist', () => {
294     // given
295     $featureFlags.isOn = jestMock.fn(() => false);
296     let getWorkflowsStub = Promise.resolve({"data": {"workflows": ["VNF Scale Out"]}});
297     let getLocalWorkflowsParametersStub = Promise.resolve({"data":{
298         "parameterDefinitions": [
299           {
300             "id": 1,
301             "name": "Configuration Parameters",
302             "required": true,
303             "type": "text",
304             "pattern": ".*",
305             "msgOnPatternError": null,
306             "msgOnContentError": null,
307             "acceptableFileType": null
308           }
309         ],
310       }});
311
312     $controller.changeManagement.vnfNames = [{name: 'test1'}];
313     $changeManagementService.getWorkflows = () => getWorkflowsStub;
314     $changeManagementService.getLocalWorkflowParameter = () => getLocalWorkflowsParametersStub;
315     // when
316     return $controller.loadWorkFlows()
317     .then(() => {
318       let internalWorkFlowParameters = $controller.getInternalWorkFlowParameters("VNF Scale Out", "FILE");
319       expect(internalWorkFlowParameters).toEqual([]);
320     });
321   });
322
323   test('Verify get internal workflow parameters should return an empty list if type exist but mapped to undefined', () => {
324     // given
325     $featureFlags.isOn = jestMock.fn(() => false);
326     let getWorkflowsStub = Promise.resolve({"data": {"workflows": ["VNF Scale Out"]}});
327     let getLocalWorkflowsParametersStub = Promise.resolve({"data": undefined});
328
329     $controller.changeManagement.vnfNames = [{name: 'test1'}];
330     $changeManagementService.getWorkflows = () => getWorkflowsStub;
331     $changeManagementService.getLocalWorkflowParameter = () => getLocalWorkflowsParametersStub;
332     // when
333     return $controller.loadWorkFlows()
334     .then(() => {
335       let internalWorkFlowParameters = $controller.getInternalWorkFlowParameters("VNF Scale Out", "FILE");
336       expect(internalWorkFlowParameters).toEqual([]);
337     });
338   });
339
340   test('Verify get internal workflow parameters should return a list if such workflow and type exist', () => {
341     // given
342     $featureFlags.isOn = jestMock.fn(() => false);
343     let getWorkflowsStub = Promise.resolve({"data": {"workflows": ["VNF Scale Out"]}});
344     let getLocalWorkflowsParametersStub = Promise.resolve({"data":{
345         "parameterDefinitions": [
346           {
347             "id": 1,
348             "name": "Configuration Parameters",
349             "required": true,
350             "type": "text",
351             "pattern": ".*",
352             "msgOnPatternError": null,
353             "msgOnContentError": null,
354             "acceptableFileType": null
355           }
356         ],
357       }});
358     $controller.changeManagement.vnfNames = [{name: 'test1'}];
359     $changeManagementService.getWorkflows = () => getWorkflowsStub;
360     $changeManagementService.getLocalWorkflowParameter = () => getLocalWorkflowsParametersStub;
361
362     let result = [{
363         "acceptableFileType": null,
364         "id": 1,
365         "msgOnContentError": null,
366         "msgOnPatternError": null,
367         "name": "Configuration Parameters",
368         "pattern": ".*",
369         "required": true,
370         "type": "text",
371         }];
372     // when
373     return $controller.loadWorkFlows()
374     .then(() => {
375       let internalWorkFlowParameters = $controller.getInternalWorkFlowParameters("VNF Scale Out", "text");
376       expect(internalWorkFlowParameters).toEqual(result);
377     });
378   });
379
380   test('Verify get remote workflow should call internal service for params when workflow is native', () =>{
381       let getWorkflowsStub = Promise.resolve({"data": {"workflows": ["workflow 0"]}});
382       let getLocalWorkflowsParametersStub = Promise.resolve({"data":{
383               "parameterDefinitions": [
384                   {
385                       "id": 1,
386                       "name": "Configuration Parameters",
387                       "required": true,
388                       "type": "text",
389                       "pattern": ".*",
390                       "msgOnPatternError": null,
391                       "msgOnContentError": null,
392                       "acceptableFileType": null
393                   }
394               ],
395           }});
396       let getSOWorkflowsPromiseStub = Promise.resolve({"data":[{
397
398         "id": "ab6478e4-ea33-3346-ac12-ab121484a333",
399         "workflowName": "inPlaceSoftwareUpdate",
400         "name": "inPlaceSoftwareUpdate",
401         "source": "native",
402         "workflowInputParameters": [
403         ]
404     }]
405   });
406
407   $controller.changeManagement.vnfNames = [{modelVersionId: 'test1', name:'test'}];
408   $changeManagementService.getWorkflows = () => getWorkflowsStub;
409   $changeManagementService.getLocalWorkflowParameter = () => getLocalWorkflowsParametersStub;
410   $changeManagementService.getSOWorkflows = () =>  getSOWorkflowsPromiseStub;
411
412   return $controller.loadWorkFlows().then(() => {
413     expect($controller.workflows).toContain('inPlaceSoftwareUpdate');
414     expect($controller.localWorkflowsParameters.get('inPlaceSoftwareUpdate')).toEqual([{
415         "id": 1,
416         "name": "Configuration Parameters",
417         "required": true,
418         "type": "text",
419         "pattern": ".*",
420         "msgOnPatternError": null,
421         "msgOnContentError": null,
422         "acceptableFileType": null
423     }]);
424   });
425 });
426
427     test('Verify that vm.searchVNFs return only generic-vnfs with relation to vserver', () => {
428         // given
429         $controller.changeManagement.serviceType = [];
430         let getVnfsByCustomerIdAndServiceType = Promise.resolve({"data":
431                 { "results" : [
432                         { "id": "1",
433                             "node-type": "generic-vnf",
434                             "properties": {
435                                 "nf-role": "vLB"
436                             },
437                             "related-to": [
438                                 { "id": "11",
439                                     "node-type": "vf-module"
440                                 },
441                                 { "id": "12",
442                                     "node-type": "tenant"
443                                 }
444                             ]
445                         },
446                         { "id": "2",
447                             "node-type": "generic-vnf",
448                             "properties": {
449                                 "nf-role": "vLB"
450                             },
451                             "related-to": [
452                                 { "id": "21",
453                                     "node-type": "tenant"
454                                 }
455                             ]
456                         },
457                         { "id": "3",
458                             "node-type": "generic-vnf",
459                             "properties": {
460                                 "nf-role": "vLB"
461                             },
462                             "related-to": [
463                                 { "id": "31",
464                                     "node-type": "vf-module"
465                                 },
466                                 { "id": "32",
467                                     "node-type": "tenant"
468                                 },
469                                 { "id": "33",
470                                     "node-type": "vserver"
471                                 }
472                             ]
473                         },
474                         { "id": "11",
475                             "node-type": "vf-module",
476                             "related-to": [
477                                 { "id": "111",
478                                     "node-type": "vserver"
479                                 }
480                             ]
481                         },
482                         { "id": "31",
483                             "node-type": "vf-module",
484                             "related-to": [
485                                 { "id": "311",
486                                     "node-type": "vserver"
487                                 }
488                             ]
489                         }
490                     ]
491                 }
492         });
493         let expectedVnfs = [
494             {
495                 "id": "1",
496                 "node-type": "generic-vnf",
497                 "properties": {"nf-role": "vLB"},
498                 "related-to": [
499                     {"id": "11", "node-type": "vf-module"},
500                     {"id": "12", "node-type": "tenant"}]},
501             {
502                 "id": "3",
503                 "node-type": "generic-vnf",
504                 "properties": {"nf-role": "vLB"},
505                 "related-to": [
506                     {"id": "31", "node-type": "vf-module"},
507                     {"id": "32", "node-type": "tenant"},
508                     {"id": "33", "node-type": "vserver"}
509                 ]}];
510         $aaiService.getVnfsByCustomerIdAndServiceType = () => getVnfsByCustomerIdAndServiceType;
511
512         // when
513         $controller.searchVNFs().then(() => {
514             expect($controller.vnfs).toHaveLength(2);
515             expect($controller.vnfs).toEqual(expectedVnfs);
516         });
517     });
518 });