de521ca0598580d2770781ffae4ac35e44d279ee
[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 policies from './mock/policies.json';
26 import * as policyinstances2 from './mock/policy-instance-2.json';
27 import * as policyinstances1Status from './mock/policy-instance-1-status.json';
28 import * as policyinstances2Status from './mock/policy-instance-2-status.json';
29 import * as eijobs from './mock/ei-jobs.json';
30 import * as eiproducers from './mock/ei-producers.json';
31 import * as policyinstanceNoType from './mock/policy-instance-notype.json';
32 import * as policytypesList from './mock/policy-types.json';
33 import * as policytypes1 from './mock/policy-type1.json';
34 import * as policyinstanceedit from './mock/policy-instance-edit.json';
35 import * as rics from './mock/rics.json';
36
37 const urls = [
38     {
39         url: 'a1-policy/v2/policy-types',
40         json: policytypesList
41     },
42     {
43         url: 'a1-policy/v2/policy-types/',
44         json: policytypes1
45     },
46     {
47         url: 'a1-policy/v2/policy-types/1',
48         json: policytypes1
49     },
50     {
51         url: 'a1-policy/v2/policies?policytype_id=',
52         json: policies
53     },
54     {
55         url: 'a1-policy/v2/policies?policytype_id=1',
56         json: policies
57     },
58     {
59         url: 'a1-policy/v2/policies/2000',
60         json: policyinstances1
61     },
62     {
63         url: 'a1-policy/v2/policies/2100',
64         json: policyinstances2
65     },
66     {
67         url: 'a1-policy/v2/policies/2000/status',
68         json: policyinstances1Status
69     },
70     {
71         url: 'a1-policy/v2/policies/2100/status',
72         json: policyinstances2Status
73     },
74     {
75         url: 'a1-policy/v2/policies/2000?type=',
76         json: policyinstanceedit
77     },
78     {
79         url: 'a1-policy/v2/policies/2100?type=',
80         json: policyinstanceedit
81     },
82     {
83         url: 'a1-policy/v2/policies/2000?type=1',
84         json: policyinstanceedit
85     },
86     {
87         url: 'a1-policy/v2/policies/2100?type=1',
88         json: policyinstanceedit
89     },
90     {
91         url: 'a1-policy/v2/policies/2000?ric=ric1&type=1',
92         json: ''
93     },
94     {
95         url: 'api/enrichment/eijobs',
96         json: eijobs
97     },
98     {
99         url: 'api/enrichment/eiproducers',
100         json: eiproducers
101     },
102     {
103         url: 'api/policy/rics?policyType=1',
104         json: rics
105     },
106     {
107         url: 'api/policy/rics?policyType=2',
108         json: rics
109     }
110 ];
111
112 @Injectable()
113 export class HttpMockRequestInterceptor implements HttpInterceptor {
114     constructor(private injector: Injector) {}
115
116     intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
117         if (request.method === "PUT" && request.url.includes("policies")) {
118             console.log('Answered PUT policy ' + request.url);
119             return of(new HttpResponse({ status: 200 }));
120         }
121         for (const element of urls) {
122             if (request.url === element.url) {
123                 console.log('Loaded from stub json : ' + request.url);
124                 return of(new HttpResponse({ status: 200, body: ((element.json) as any).default }));
125             }
126         }
127         console.log('Loaded from mock http call :' + request.url);
128         return next.handle(request);
129     }
130 }