Add test Coverage for Policy Service
[portal/nonrtric-controlpanel.git] / webapp-frontend / src / app / interceptor.mock.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 { HttpEvent, HttpHandler, HttpInterceptor, HttpRequest, HttpResponse } from '@angular/common/http';
22 import { Injectable, Injector } from '@angular/core';
23 import { Observable, of } from 'rxjs';
24 import * as policyinstances1 from './mock/policy-instance-1.json';
25 import * as policyinstances2 from './mock/policy-instance-2.json';
26 import * as eijobs from './mock/ei-jobs.json';
27 import * as eiproducers from './mock/ei-producers.json';
28 import * as nopolicyinstances from './mock/nopolicyinstances.json';
29 import * as policytypes from './mock/policytypes.json';
30 import * as policyinstanceedit from './mock/policy-instance-edit.json';
31 import * as rics from './mock/rics.json';
32
33 const urls = [
34     {
35         url: 'api/policy/policytypes',
36         json: policytypes
37     },
38     {
39         url: 'api/policy/policies?type=1',
40         json: policyinstances1
41     },
42     {
43         url: 'api/policy/policies?type=2',
44         json: policyinstances2
45     },
46     {
47         url: 'api/policy/policies?type=2',
48         json: nopolicyinstances
49     },
50     {
51         url: 'api/policy/policies/2000?type=1',
52         json: policyinstanceedit
53     },
54     {
55         url: 'api/policy/policies/2001?type=2',
56         json: policyinstanceedit
57     },
58     {
59         url: 'api/policy/policies/2000?ric=ric1&type=1',
60         json: ''
61     },
62     {
63         url: 'api/enrichment/eijobs',
64         json: eijobs
65     },
66     {
67         url: 'api/enrichment/eiproducers',
68         json: eiproducers
69     },
70     {
71         url: 'api/policy/rics?policyType=1',
72         json: rics
73     },
74     {
75         url: 'api/policy/rics?policyType=2',
76         json: rics
77     }
78 ];
79
80 @Injectable()
81 export class HttpMockRequestInterceptor implements HttpInterceptor {
82     constructor(private injector: Injector) {}
83
84     intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
85         if (request.method === "PUT" && request.url.includes("policies")) {
86             console.log('Answered PUT policy ' + request.url);
87             return of(new HttpResponse({ status: 200 }));
88         }
89         for (const element of urls) {
90             if (request.url === element.url) {
91                 console.log('Loaded from stub json : ' + request.url);
92                 return of(new HttpResponse({ status: 200, body: ((element.json) as any).default }));
93             }
94         }
95         console.log('Loaded from mock http call :' + request.url);
96         return next.handle(request);
97     }
98 }