775daacae3c032dc4f143e15f43afa1d85307a45
[portal/nonrtric-controlpanel.git] / webapp-frontend / src / app / interceptor.spec.ts
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2021 Nordix Foundation
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 import { TestBed } from '@angular/core/testing';
22 import {
23     HttpClientTestingModule,
24     HttpTestingController,
25 } from '@angular/common/http/testing';
26 import { HTTP_INTERCEPTORS } from '@angular/common/http';
27 import { HttpRequestInterceptor } from './interceptor';
28 import { NotificationService } from './services/ui/notification.service';
29 import { EIService } from './services/ei/ei.service';
30 import { of } from 'rxjs/observable/of';
31
32 describe(`HttpRequestInterceptor`, () => {
33     let service: EIService;
34     const notificationServiceSpy = jasmine.createSpyObj('NotificationService', [ 'error' ]);
35     let httpMock: HttpTestingController;
36
37     beforeEach(() => {
38
39       TestBed.configureTestingModule({
40           imports: [HttpClientTestingModule],
41           providers: [
42               {
43                   provide: NotificationService, useValue: notificationServiceSpy
44                 },
45                 {
46                     provide: HTTP_INTERCEPTORS,
47                     useClass: HttpRequestInterceptor,
48                     multi: true,
49                 },
50                 EIService
51             ]
52         });
53
54         httpMock = TestBed.inject(HttpTestingController);
55         service = TestBed.inject(EIService);
56     });
57
58     it('should create', () => {
59         const interceptors = TestBed.inject(HTTP_INTERCEPTORS);
60         let found = false;
61         interceptors.forEach(interceptor => {
62             if (interceptor instanceof HttpRequestInterceptor) {
63                 found = true;
64             };
65         });
66         expect(found).toBeTruthy();
67     })
68
69     it('should pass through when ok', () => {
70     let response: [ 'roducer1' ];
71
72     spyOn(service, 'getProducerIds').and.returnValue(of(response));
73     service.getProducerIds().subscribe(() => {
74         ids => expect(ids).toEqual(response);
75     });
76
77     httpMock.verify();
78   });
79
80     it('should notify when error', () => {
81     let response: any;
82     let errResponse: any;
83
84     service.getProducerIds().subscribe(res => response = res, err => errResponse = err);
85
86     const data = 'Invalid request parameters';
87     const mockErrorResponse = { status: 400, statusText: 'Bad Request' };
88     httpMock.expectOne(`/ei-producer/v1/eiproducers`).flush(data, mockErrorResponse);
89
90     httpMock.verify();
91
92     expect(notificationServiceSpy.error).toHaveBeenCalledWith(containsString('Bad Request'));
93   });
94
95   function containsString(msg: string) {
96     return {
97       asymmetricMatch: function(compareTo: string) {
98         return compareTo.includes(msg);
99       },
100
101       jasmineToString: function() {
102         return '<containsString: ' + msg + '>';
103       }
104     };
105   }
106 });
107