93bc1c2de27ec8a6620a43c159cd08babdc10e99
[vid.git] / vid-app-common / src / main / webapp / app / vid / scripts / modals / change-management-manual-tasks-controller / change-management-manual-tasks.controller.test.js
1 /*-
2  * ============LICENSE_START=======================================================
3  * VID
4  * ================================================================================
5  * Copyright (C) 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('./change-management-manual-tasks.controller');
22 const jestMock = require('jest-mock');
23
24 describe('changeManagementManualTasksController testing', () => {
25     let $controller;
26     let $uibModalInstance = jestMock.fn();
27     let $MsoService = jestMock.fn();
28     let $log = jestMock.fn();
29
30     beforeEach(
31         angular.mock.module('app')
32     );
33
34     beforeEach(inject(function (_$controller_) {
35         $log.error = jestMock.fn();
36         $uibModalInstance.close = jestMock.fn();
37
38         mockManualTaskResponse(manualTaskResponse);
39
40         $controller = _$controller_('changeManagementManualTasksController', {
41             "MsoService": $MsoService,
42             "$uibModalInstance": $uibModalInstance,
43             "$log": $log,
44             "jobInfo": {
45                 requestState: job.requestStatus.requestState,
46                 details: job.requestStatus.statusMessage,
47                 job: job,
48             },
49             "COMPONENT": {
50                 MANUAL_TASKS: ["manualTaskName1", "manualTaskName2"]
51             },
52         });
53     }));
54
55     function mockManualTaskResponse(manualTaskResponse) {
56         $MsoService.getManualTasks = jestMock.fn().mockResolvedValue(
57             {data: [manualTaskResponse]}
58         );
59     }
60
61     const job = {
62         "requestId": "db775fac-d9b5-480e-8b3e-4f0d0ae67890",
63         "requestScope": "vnf",
64         "requestStatus": {
65             "percentProgress": 100.0,
66             "requestState": "FAILED",
67             "statusMessage": "Error validating request. No valid catalog entry is specified",
68             "finishTime": "Thu, 05 Oct 2017 18:58:29 GMT"
69         },
70         "requestType": "replaceInstance",
71         "startTime": "Thu, 05 Oct 2017 18:58:29 GMT",
72         "instanceReferences": {
73             "serviceInstanceId": "cc8fa0a9-7576-4c39-af31-7ad61d057ac9",
74             "vnfInstanceId": "bec0c3d3-09ae-4eb1-b694-057987a10982",
75             "requestorId": "pa2396"
76         }
77     };
78
79     const manualTaskResponseWithoutValidResponses = {
80         "taskId": "db775fac-d9b5-480e-8b3e-4f0d0ae67890",
81     };
82
83     const manualTaskResponse = Object.assign({
84         "validResponses": ["rollback", "abort", "skip", "resume", "retry"],
85     }, manualTaskResponseWithoutValidResponses);
86
87     const manualTaskResponseWithTimeout = Object.assign({
88         description: 'description',
89         timeout: 'timeout',
90     }, manualTaskResponse);
91
92     test('should populate vm.manualTasks (while init)', () => {
93         expect($controller.manualTasks).toEqual(
94             manualTaskResponse.validResponses);
95     });
96
97     test('should undefine vm.manualTasks when ValidResponses not given', () => {
98         // given
99         mockManualTaskResponse(manualTaskResponseWithoutValidResponses);
100         // when
101         return $controller.__test_only__.loadAvailableTasks('anything')
102         .then(() => {
103             expect($controller.manualTasks).toBeUndefined()
104         });
105     });
106
107     test('should populate vm.MANUAL_TASKS from COMPONENT (while init)', () => {
108         expect($controller.MANUAL_TASKS).toEqual(
109             ["manualTaskName1", "manualTaskName2"]);
110     });
111
112     test('should populate vm.task (while init)', () => {
113         expect($controller.task).toEqual(manualTaskResponse);
114     });
115
116     test('should nullify vm.description (while init)', () => {
117         expect($controller.description).toBeNull();
118     });
119
120     test('should nullify vm.timeout (while init)', () => {
121         expect($controller.timeout).toBeNull();
122     });
123
124     test('should populate vm.description', () => {
125         // given
126         mockManualTaskResponse(manualTaskResponseWithTimeout);
127         // when
128         return $controller.__test_only__.loadAvailableTasks('anything')
129         .then(() => {
130             expect($controller.description).toEqual('description');
131         });
132     });
133
134     test('should populate vm.timeout', () => {
135         // given
136         mockManualTaskResponse(manualTaskResponseWithTimeout);
137         // when
138         return $controller.__test_only__.loadAvailableTasks('anything')
139         .then(() => {
140             expect($controller.timeout).toEqual('timeout');
141         });
142     });
143
144     test('should find manual task using isTaskAvailable', () => {
145         expect($controller.isTaskAvailable('abort')).toBeTruthy();
146         expect($controller.isTaskAvailable('resume')).toBeTruthy();
147
148         expect($controller.isTaskAvailable('foo')).toBeFalsy();
149         expect($controller.isTaskAvailable(undefined)).toBeFalsy();
150     });
151
152     test('should call MsoService upon completeTask', () => {
153         $MsoService.completeTask = jestMock.fn().mockResolvedValue({data: {}});
154
155         $controller.completeTask("taskName");
156         expect($MsoService.completeTask).toBeCalledWith(
157             manualTaskResponse.taskId, "taskName");
158     });
159
160     test('should close modal upon completeTask', done => {
161         $MsoService.completeTask = jestMock.fn().mockResolvedValue({data: {}});
162         $uibModalInstance.close = jestMock.fn(() => {
163             done();
164         });
165
166         $controller.completeTask("taskName");
167     });
168
169     test('should close modal upon failed completeTask', done => {
170         $MsoService.completeTask = jestMock.fn().mockRejectedValue();
171         $uibModalInstance.close = jestMock.fn(() => {
172             done();
173         });
174
175         $controller.completeTask("taskName");
176     });
177
178 });
179