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