Test manual-tasks-controller
[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         $MsoService.getManualTasks = jestMock.fn().mockResolvedValue(
36             {data: [manualTaskResponse]});
37         $log.error = jestMock.fn();
38
39         $controller = _$controller_('changeManagementManualTasksController', {
40             "MsoService": $MsoService,
41             "$uibModalInstance": $uibModalInstance,
42             "$log": $log,
43             "jobInfo": {
44                 requestState: job.requestStatus.requestState,
45                 details: job.requestStatus.statusMessage,
46                 job: job,
47             },
48             "COMPONENT": {
49                 MANUAL_TASKS: ["manualTaskName1", "manualTaskName2"]
50             },
51         });
52     }));
53
54     const job = {
55         "requestId": "db775fac-d9b5-480e-8b3e-4f0d0ae67890",
56         "requestScope": "vnf",
57         "requestStatus": {
58             "percentProgress": 100.0,
59             "requestState": "FAILED",
60             "statusMessage": "Error validating request. No valid catalog entry is specified",
61             "finishTime": "Thu, 05 Oct 2017 18:58:29 GMT"
62         },
63         "requestType": "replaceInstance",
64         "startTime": "Thu, 05 Oct 2017 18:58:29 GMT",
65         "instanceReferences": {
66             "serviceInstanceId": "cc8fa0a9-7576-4c39-af31-7ad61d057ac9",
67             "vnfInstanceId": "bec0c3d3-09ae-4eb1-b694-057987a10982",
68             "requestorId": "pa2396"
69         }
70     };
71
72     const manualTaskResponse = {
73         "taskId": "db775fac-d9b5-480e-8b3e-4f0d0ae67890",
74         "validResponses": ["rollback", "abort", "skip", "resume", "retry"]
75     };
76
77     test('should populate vm.manualTasks (while init)', () => {
78         expect($controller.manualTasks).toEqual(
79             manualTaskResponse.validResponses);
80     });
81
82     test('should populate vm.MANUAL_TASKS from COMPONENT (while init)', () => {
83         expect($controller.MANUAL_TASKS).toEqual(
84             ["manualTaskName1", "manualTaskName2"]);
85     });
86
87     test('should populate vm.task (while init)', () => {
88         expect($controller.task).toEqual(manualTaskResponse);
89     });
90
91     test('should find manual task using isTaskAvailable', () => {
92         expect($controller.isTaskAvailable('abort')).toBeTruthy();
93         expect($controller.isTaskAvailable('resume')).toBeTruthy();
94
95         expect($controller.isTaskAvailable('foo')).toBeFalsy();
96         expect($controller.isTaskAvailable(undefined)).toBeFalsy();
97     });
98
99     test('should call MsoService upon completeTask', () => {
100         $MsoService.completeTask = jestMock.fn().mockResolvedValue({data: {}});
101
102         $controller.completeTask("taskName");
103         expect($MsoService.completeTask).toBeCalledWith(
104             manualTaskResponse.taskId, "taskName");
105     });
106
107     test('should close modal upon completeTask', done => {
108         $MsoService.completeTask = jestMock.fn().mockResolvedValue({data: {}});
109         $uibModalInstance.close = jestMock.fn(() => {
110             done();
111         });
112
113         $controller.completeTask("taskName");
114     });
115
116     test('should close modal upon failed completeTask', done => {
117         $MsoService.completeTask = jestMock.fn().mockRejectedValue();
118         $uibModalInstance.close = jestMock.fn(() => {
119             done();
120         });
121
122         $controller.completeTask("taskName");
123     });
124
125 });
126