29adfa9e04e02fe340a0635f25c1e8f57b8828a0
[vid.git] / vid-webpack-master / src / app / instantiationStatus / instantiationStatus.component.service.spec.ts
1 import {getTestBed, TestBed} from '@angular/core/testing';
2 import {COMPLETED_WITH_ERRORS, INPROGRESS, InstantiationStatusComponentService, PAUSE, PENDING, ServiceStatus, STOPPED, SUCCESS_CIRCLE, UNKNOWN, X_O} from './instantiationStatus.component.service';
3 import {ServiceInfoModel} from '../shared/server/serviceInfo/serviceInfo.model';
4 import {AaiService} from "../shared/services/aaiService/aai.service";
5 import {MsoService} from "../shared/services/msoService/mso.service";
6 import {NgRedux} from "@angular-redux/store";
7 import {HttpClientTestingModule} from "@angular/common/http/testing";
8 import {FeatureFlagsService} from "../shared/services/featureFlag/feature-flags.service";
9 import {DrawingBoardModes} from "../drawingBoard/service-planning/drawing-board.modes";
10 import {RouterTestingModule} from "@angular/router/testing";
11 import {of} from "rxjs";
12 import {UrlTree} from "@angular/router";
13 import each from "jest-each";
14 import {ServiceAction} from "../shared/models/serviceInstanceActions";
15
16 class MockAppStore<T> {
17
18   getState() {
19     return {
20       global: {
21         flags: {
22           'FLAG_1902_NEW_VIEW_EDIT': true,
23
24         }
25       }
26     }
27   }
28
29   dispatch() {
30
31   }
32 }
33 describe('Instantiation Status Service', () => {
34   let injector;
35   let aaiService: AaiService;
36   let msoService: MsoService;
37   let service: InstantiationStatusComponentService;
38
39
40   beforeAll(done => (async () => {
41     TestBed.configureTestingModule({
42       imports: [
43         HttpClientTestingModule,
44         RouterTestingModule,
45       ],
46       providers: [
47         InstantiationStatusComponentService,
48         AaiService,
49         MsoService,
50         FeatureFlagsService,
51         {provide: NgRedux, useClass: MockAppStore}]
52     });
53     await TestBed.compileComponents();
54
55     injector = getTestBed();
56     aaiService = injector.get(AaiService);
57     msoService = injector.get(MsoService);
58     service = injector.get(InstantiationStatusComponentService);
59
60   })().then(done).catch(done.fail));
61
62   test('generateServiceInfoDataMapping should return mapping of arrays', () => {
63     let data : ServiceInfoModel[] = generateServiceInfoData();
64     let result = service.generateServiceInfoDataMapping(data);
65
66     expect(result['1']).toBeDefined();
67     expect(result['2']).toBeDefined();
68     expect(result['3']).toBeDefined();
69
70     expect(result['1'].length).toEqual(2);
71     expect(result['2'].length).toEqual(2);
72     expect(result['3'].length).toEqual(1);
73   });
74
75   test('generateServiceInfoDataMapping if array is empty  should return empty object', () => {
76     let result = service.generateServiceInfoDataMapping([]);
77
78     expect(result['1']).not.toBeDefined();
79     expect(result['2']).not.toBeDefined();
80     expect(result['3']).not.toBeDefined();
81   });
82
83   test('convertObjectToArray', () => {
84
85     jest.spyOn(service, 'convertObjectToArray').mockReturnValue(
86       of([])
87     );
88
89     let data : ServiceInfoModel[] = generateServiceInfoData();
90     service.convertObjectToArray(data).subscribe((result) => {
91       expect(result).toBeDefined();
92     });
93   });
94
95   describe('navigations tests:', () => {
96
97     const item = {
98       serviceModelId: '28aeb8f6-5620-4148-8bfb-a5fb406f0309',
99       serviceInstanceId: 'myInstanceId',
100       serviceType: 'myService',
101       subscriberId: 'mySubscriber',
102       jobId: 'aJobId'
103     };
104
105     test('click on "Open" button should open new view edit', () => {
106       let params: UrlTree = service.getNewViewEditUrlTree(<any>item, DrawingBoardModes.VIEW);
107       expect(params.toString().startsWith('/servicePlanning/VIEW')).toBeTruthy();
108       expect(params.queryParams).toEqual(
109         {
110           serviceModelId: item.serviceModelId,
111           serviceInstanceId: item.serviceInstanceId,
112           serviceType: item.serviceType,
113           subscriberId: item.subscriberId,
114           jobId: item.jobId
115         });
116     });
117
118     test('build the View Edit url', () => {
119
120       let serviceModelUrl: string = '/servicePlanning/EDIT?serviceModelId=28aeb8f6-5620-4148-8bfb-a5fb406f0309' +
121         '&serviceInstanceId=myInstanceId&serviceType=myService&subscriberId=mySubscriber&jobId=aJobId';
122       let prefix: string = '../../serviceModels.htm#';
123       let tree: UrlTree = service.getNewViewEditUrlTree(<any>item, DrawingBoardModes.EDIT);
124       let result = service.getViewEditUrl(tree);
125       expect(result).toEqual(prefix + serviceModelUrl);
126     });
127
128     test('recreate url shall contains mode RECREATE and only jobId and serviceModelId', () =>{
129       let params: UrlTree = service.getNewViewEditUrlTree(<any>item, DrawingBoardModes.RECREATE);
130       expect(params.toString().startsWith('/servicePlanning/RECREATE')).toBeTruthy();
131       expect(params.queryParams).toEqual(
132         {
133           serviceModelId: item.serviceModelId,
134           jobId: item.jobId
135         });
136     });
137   });
138
139   for (let [status, tooltip] of Object.entries({
140     'pending': 'Pending: The action required will be sent as soon as possible.',
141     'IN_PROGRESS': 'In-progress: the service is in process of the action required.',
142     'PAUSED': 'Paused: Service has paused and waiting for your action.\n Select actions from the menu to the right.',
143     'FAILED': 'Failed: All planned actions have failed.',
144     'COMPLETED': 'Completed successfully: Service is successfully instantiated, updated or deleted.',
145     'STOPPED': 'Stopped: Due to previous failure, will not be instantiated.',
146     'StOpPeD': 'Stopped: Due to previous failure, will not be instantiated.',
147     'COMPLETED_WITH_ERRORS': 'Completed with errors: some of the planned actions where successfully committed while other have not.\n Open the service to check it out.',
148     'UNEXPECTED_RANDOM_STATUS': 'Unexpected status: "UNEXPECTED_RANDOM_STATUS"',
149   })) {
150
151     test(`getStatusTooltip should return status popover: status=${status}`, () => {
152       expect(service.getStatus(status).tooltip).toEqual(tooltip);
153     });
154
155   }
156
157   test(`service.getStatus should handle undefined status`, () => {
158     const statusResult = service.getStatus(undefined);
159     expect(statusResult.tooltip).toEqual('Unexpected status: "undefined"');
160     expect(statusResult.iconClassName).toEqual(UNKNOWN);
161   });
162
163
164   each([
165       [true, ServiceAction.INSTANTIATE],
166       [false, ServiceAction.UPDATE],
167       [false, ServiceAction.DELETE],
168   ]).
169   test('isRecreateEnabled: should be %s if action is %s', (expected:boolean, action:ServiceAction) => {
170     let serviceInfoModel = new ServiceInfoModel();
171     serviceInfoModel.action = action;
172     expect(service.isRecreateEnabled(serviceInfoModel)).toBe(expected);
173   });
174
175   test('getStatusTooltip should return correct icon per job status', () => {
176     let result : ServiceStatus  = service.getStatus('pending');
177     expect(result.iconClassName).toEqual(PENDING);
178
179     result = service.getStatus('IN_PROGRESS');
180     expect(result.iconClassName).toEqual(INPROGRESS);
181
182     result = service.getStatus('PAUSED');
183     expect(result.iconClassName).toEqual(PAUSE);
184
185     result = service.getStatus('FAILED');
186     expect(result.iconClassName).toEqual(X_O);
187
188     result = service.getStatus('COMPLETED');
189     expect(result.iconClassName).toEqual(SUCCESS_CIRCLE);
190
191     result = service.getStatus('STOPPED');
192     expect(result.iconClassName).toEqual(STOPPED);
193
194     result = service.getStatus('COMPLETED_WITH_ERRORS');
195     expect(result.iconClassName).toEqual(COMPLETED_WITH_ERRORS);
196
197     result = service.getStatus('UNEXPECTED_RANDOM_STATUS');
198     expect(result.iconClassName).toEqual(UNKNOWN);
199
200     result = service.getStatus(undefined);
201     expect(result.iconClassName).toEqual(UNKNOWN);
202   });
203
204   function generateServiceInfoData(){
205     return JSON.parse(JSON.stringify([
206       {
207         "created": 1519956533000,
208         "modified": 1521727738000,
209         "createdId": null,
210         "modifiedId": null,
211         "rowNum": null,
212         "auditUserId": null,
213         "auditTrail": null,
214         "jobId": "6748648484",
215         "userId": "2222",
216         "jobStatus": "FAILED",
217         "pause": false,
218         "owningEntityId": "1234",
219         "owningEntityName": null,
220         "project": null,
221         "aicZoneId": null,
222         "aicZoneName": null,
223         "tenantId": null,
224         "tenantName": null,
225         "regionId": null,
226         "regionName": null,
227         "serviceType": null,
228         "subscriberName": null,
229         "serviceInstanceId": "1",
230         "serviceInstanceName": null,
231         "serviceModelId": null,
232         "serviceModelName": null,
233         "serviceModelVersion": null,
234         "createdBulkDate": 1519956533000,
235         "statusModifiedDate": 1520042933000,
236         "templateId": "1",
237         "hidden": false
238       },
239       {
240         "created": 1519956533000,
241         "modified": 1521727738000,
242         "createdId": null,
243         "modifiedId": null,
244         "rowNum": null,
245         "auditUserId": null,
246         "auditTrail": null,
247         "jobId": "6748648484",
248         "userId": "2222",
249         "jobStatus": "FAILED",
250         "pause": false,
251         "owningEntityId": "1234",
252         "owningEntityName": null,
253         "project": null,
254         "aicZoneId": null,
255         "aicZoneName": null,
256         "tenantId": null,
257         "tenantName": null,
258         "regionId": null,
259         "regionName": null,
260         "serviceType": null,
261         "subscriberName": null,
262         "serviceInstanceId": "1",
263         "serviceInstanceName": null,
264         "serviceModelId": null,
265         "serviceModelName": null,
266         "serviceModelVersion": null,
267         "createdBulkDate": 1519956533000,
268         "statusModifiedDate": 1520042933000,
269         "templateId": "1",
270         "hidden": false
271       },
272       {
273         "created": 1519956533000,
274         "modified": 1521727738000,
275         "createdId": null,
276         "modifiedId": null,
277         "rowNum": null,
278         "auditUserId": null,
279         "auditTrail": null,
280         "jobId": "6748648484",
281         "userId": "2222",
282         "jobStatus": "FAILED",
283         "pause": false,
284         "owningEntityId": "1234",
285         "owningEntityName": null,
286         "project": null,
287         "aicZoneId": null,
288         "aicZoneName": null,
289         "tenantId": null,
290         "tenantName": null,
291         "regionId": null,
292         "regionName": null,
293         "serviceType": null,
294         "subscriberName": null,
295         "serviceInstanceId": "2",
296         "serviceInstanceName": null,
297         "serviceModelId": null,
298         "serviceModelName": null,
299         "serviceModelVersion": null,
300         "createdBulkDate": 1519956533000,
301         "statusModifiedDate": 1520042933000,
302         "templateId": "2",
303         "hidden": false
304       },
305       {
306         "created": 1519956533000,
307         "modified": 1521727738000,
308         "createdId": null,
309         "modifiedId": null,
310         "rowNum": null,
311         "auditUserId": null,
312         "auditTrail": null,
313         "jobId": "6748648484",
314         "userId": "2222",
315         "jobStatus": "FAILED",
316         "pause": false,
317         "owningEntityId": "1234",
318         "owningEntityName": null,
319         "project": null,
320         "aicZoneId": null,
321         "aicZoneName": null,
322         "tenantId": null,
323         "tenantName": null,
324         "regionId": null,
325         "regionName": null,
326         "serviceType": null,
327         "subscriberName": null,
328         "serviceInstanceId": "2",
329         "serviceInstanceName": null,
330         "serviceModelId": null,
331         "serviceModelName": null,
332         "serviceModelVersion": null,
333         "createdBulkDate": 1519956533000,
334         "statusModifiedDate": 1520042933000,
335         "templateId": "2",
336         "hidden": false
337       },
338       {
339         "created": 1519956533000,
340         "modified": 1521727738000,
341         "createdId": null,
342         "modifiedId": null,
343         "rowNum": null,
344         "auditUserId": null,
345         "auditTrail": null,
346         "jobId": "6748648484",
347         "userId": "2222",
348         "jobStatus": "FAILED",
349         "pause": false,
350         "owningEntityId": "1234",
351         "owningEntityName": null,
352         "project": null,
353         "aicZoneId": null,
354         "aicZoneName": null,
355         "tenantId": null,
356         "tenantName": null,
357         "regionId": null,
358         "regionName": null,
359         "serviceType": null,
360         "subscriberName": null,
361         "serviceInstanceId": "3",
362         "serviceInstanceName": null,
363         "serviceModelId": null,
364         "serviceModelName": null,
365         "serviceModelVersion": null,
366         "createdBulkDate": 1519956533000,
367         "statusModifiedDate": 1520042933000,
368         "templateId": "3",
369         "hidden": false
370       }
371     ]));
372   }
373
374 });