Update license headers
[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  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 require('./new-change-management.controller');
22 const jestMock = require('jest-mock');
23
24 describe('Testing workFlows from SO', () => {
25   let $notNeeded;
26   let $controller;
27   let $changeManagementService;
28   beforeEach(
29       angular.mock.module('app')
30   );
31
32   beforeEach(inject(function (_$controller_) {
33     $notNeeded = jestMock.fn();
34     // mock ChangeManagementService
35     $changeManagementService = jestMock.fn();
36     $changeManagementService.getAllSDCServices = jestMock.fn(() => Promise.resolve([]));
37
38     // mock q
39     $q = jestMock.fn();
40     $defer = jestMock.fn();
41     $q.defer = jestMock.fn(() => $defer);
42     $defer.promise = Promise.resolve({});
43     // mock AaiService
44     $aaiService = jestMock.fn();
45     $aaiService.getLoggedInUserID = jestMock.fn();
46     $aaiService.getSubscribers = jestMock.fn();
47     $controller = _$controller_('newChangeManagementModalController', {
48       $uibModalInstance: $notNeeded,
49       $uibModal: $notNeeded,
50       $q: $q,
51       AaiService: $aaiService,
52       changeManagementService: $changeManagementService,
53       Upload: $notNeeded,
54       $log: $notNeeded,
55       _: $notNeeded,
56       COMPONENT: $notNeeded,
57       VIDCONFIGURATION: $notNeeded,
58       DataService: $notNeeded,
59       featureFlags: $notNeeded,
60       $scope: $notNeeded,
61     });
62   }));
63
64   test('Verify load workflows from SO will call getSOWorkflow and return only names of workflows', () => {
65     // given
66     $controller.changeManagement.vnfNames = [{name: 'test1'}, {name: "test2"}];
67     let getSOWorkflowsPromiseStub = Promise.resolve({"data": [{"id": "1", "name": "workflow 1"}, {"id": "2", "name": "workflow 2"}]});
68     $changeManagementService.getSOWorkflows = () => getSOWorkflowsPromiseStub;
69     $controller.workflows = [];
70     // when
71     return $controller.loadRemoteWorkFlows()
72     .then(() => {
73            remoteWorkflows = $controller.remoteWorkflows.map(item => item.name);
74            expect(remoteWorkflows).toContain('workflow 1');
75            expect(remoteWorkflows).toContain('workflow 2');
76         }
77      );
78   });
79
80   test('Verify load workflows will call load from SO and join workflow lists', () => {
81     // given
82     let getWorkflowsStub = Promise.resolve({"data": {"workflows": ["workflow 0"]}});
83     let getSOWorkflowsPromiseStub = Promise.resolve({"data": [{"id": "1", "name": "workflow 1"}, {"id": "2", "name": "workflow 2"}]});
84     let getSOWorkflowsParametersPromiseStub = Promise.resolve({"data":{"parameterDefinitions": []}});
85
86     $controller.changeManagement.vnfNames = [{name: 'test1'}, {name: "test2"}];
87     $changeManagementService.getWorkflows = () => getWorkflowsStub;
88     $changeManagementService.getSOWorkflows = () =>  getSOWorkflowsPromiseStub;
89     $changeManagementService.getSOWorkflowParameter = () =>  getSOWorkflowsParametersPromiseStub;
90     // when
91     return $controller.loadWorkFlows().then(() => {
92       expect($controller.workflows).toContain('workflow 0');
93       expect($controller.workflows).toContain('workflow 1');
94       expect($controller.workflows).toContain('workflow 2');
95     });
96   });
97
98   test('Verify load workflows will call load workflows parameters from SO', () => {
99     // given
100     let getWorkflowsStub = Promise.resolve({"data": {"workflows": ["workflow 0"]}});
101     let getSOWorkflowsPromiseStub = Promise.resolve({"data": [{"id": "1", "name": "workflow 0"}]});
102     let getSOWorkflowsParametersPromiseStub = Promise.resolve({"data":{"parameterDefinitions": [
103           {"id": 1, "name": "parameter 1", "required": true, "type": "STRING", "pattern": "[0-9]*"},
104           {"id": 2, "name": "parameter 2", "required": true, "type": "STRING", "pattern": ".*"},
105           {"id": 3, "name": "parameter 3", "required": false, "type": "STRING", "pattern": "[0-9]*"}]}});
106
107     $controller.changeManagement.vnfNames = [{name: 'test1'}, {name: "test2"}];
108     $changeManagementService.getWorkflows = () => getWorkflowsStub;
109     $changeManagementService.getSOWorkflows = () =>  getSOWorkflowsPromiseStub;
110     $changeManagementService.getSOWorkflowParameter = () =>  getSOWorkflowsParametersPromiseStub;
111     // when
112     return $controller.loadWorkFlows()
113     .then(() => {
114       expect($controller.remoteWorkflowsParameters).toEqual(new Map([["workflow 0",
115         [{"id": 1, "name": "parameter 1", "pattern": "[0-9]*", "required": true, "type": "STRING"},
116          {"id": 2, "name": "parameter 2", "pattern": ".*", "required": true, "type": "STRING"},
117          {"id": 3, "name": "parameter 3", "pattern": "[0-9]*", "required": false, "type": "STRING"}]]]));
118     });
119   });
120
121   test('Verify broken SO workflows wont change content of local workflows', () => {
122     // given
123     let getWorkflowsStub = Promise.resolve({"data": {"workflows": ["workflow 0"]}});
124     let getSOWorkflowsPromiseStub = Promise.reject(new Error("Broken SO workflows service."));
125
126     $controller.changeManagement.vnfNames = "any";
127     $changeManagementService.getWorkflows = () => getWorkflowsStub;
128     $changeManagementService.getSOWorkflows = () =>  getSOWorkflowsPromiseStub;
129     // when
130     $controller.loadWorkFlows()
131     .then(() => {
132       expect($controller.workflows).toEqual(['workflow 0']);
133     });
134   });
135 });
136