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