ad0d080dda60aa299856f1d9c954f32a08b3bccb
[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 policyinstance1 from './mock/policy-instance-1.json';
25 import * as noTypePolicies from './mock/no-type-policies.json';
26 import * as typedPolicies from './mock/policies.json';
27 import * as policyinstance2 from './mock/policy-instance-2.json';
28 import * as noTypePolicyinstance from './mock/policy-instance-notype.json';
29 import * as policyinstance1Status from './mock/policy-instance-1-status.json';
30 import * as policyinstance2Status from './mock/policy-instance-2-status.json';
31 import * as eijobsProd1 from './mock/ei-jobs-producer1.json';
32 import * as eijobsProd2 from './mock/ei-jobs-producer2.json';
33 import * as eiProducerIds from './mock/ei-producerids.json';
34 import * as eiproducer1 from './mock/ei-producer1.json';
35 import * as eiproducer2 from './mock/ei-producer2.json';
36 import * as eiproducerstatus1 from './mock/ei-producer-status1.json';
37 import * as eiproducerstatus2 from './mock/ei-producer-status2.json';
38 import * as policytypesList from './mock/policy-types.json';
39 import * as policytypes1 from './mock/policy-type1.json';
40 import * as policytypes0 from './mock/policy-type0.json';
41 import * as policyinstanceedit from './mock/policy-instance-edit.json';
42 import * as ric1 from './mock/ric1.json';
43 import * as ric2 from './mock/ric2.json';
44
45 const urls = [
46     {
47         url: '/a1-policy/v2/policy-types',
48         json: policytypesList
49     },
50     {
51         url: '/a1-policy/v2/policy-types/1',
52         json: policytypes1
53     },
54     {
55         url: '/a1-policy/v2/policy-types/0',
56         json: policytypes0
57     },
58     {
59         url: '/a1-policy/v2/policies?policytype_id=',
60         json: noTypePolicies
61     },
62     {
63         url: '/a1-policy/v2/policies?policytype_id=1',
64         json: typedPolicies
65     },
66     {
67         url: '/a1-policy/v2/policies/2001',
68         json: noTypePolicyinstance
69     },
70     {
71         url: '/a1-policy/v2/policies/2000',
72         json: policyinstance1
73     },
74     {
75         url: '/a1-policy/v2/policies/2100',
76         json: policyinstance2
77     },
78     {
79         url: '/a1-policy/v2/policies/2001/status',
80         json: policyinstance1Status
81     },
82     {
83         url: '/a1-policy/v2/policies/2000/status',
84         json: policyinstance1Status
85     },
86     {
87         url: '/a1-policy/v2/policies/2100/status',
88         json: policyinstance2Status
89     },
90     {
91         url: '/a1-policy/v2/policies/2000?type=',
92         json: policyinstanceedit
93     },
94     {
95         url: '/a1-policy/v2/policies/2100?type=',
96         json: policyinstanceedit
97     },
98     {
99         url: '/a1-policy/v2/policies/2000?type=1',
100         json: policyinstanceedit
101     },
102     {
103         url: '/a1-policy/v2/policies/2100?type=1',
104         json: policyinstanceedit
105     },
106     {
107         url: '/a1-policy/v2/policies/2000?ric=ric1&type=1',
108         json: ''
109     },
110     {
111         url: '/a1-policy/v2/rics?policytype_id=1',
112         json: ric1
113     },
114     {
115         url: '/a1-policy/v2/rics?policytype_id=',
116         json: ric2
117     },
118     {
119         url: '/ei-producer/v1/eiproducers',
120         json: eiProducerIds
121     },
122     {
123         url: '/ei-producer/v1/eiproducers/producer1',
124         json: eiproducer1
125     },
126     {
127         url: '/ei-producer/v1/eiproducers/producer2',
128         json: eiproducer2
129     },
130     {
131         url: '/ei-producer/v1/eiproducers/producer1/status',
132         json: eiproducerstatus1
133     },
134     {
135         url: '/ei-producer/v1/eiproducers/producer2/status',
136         json: eiproducerstatus2
137     },
138     {
139         url: '/ei-producer/v1/eiproducers/producer1/eijobs',
140         json: eijobsProd1
141     },
142     {
143         url: '/ei-producer/v1/eiproducers/producer2/eijobs',
144         json: eijobsProd2
145     }
146 ];
147
148 @Injectable()
149 export class HttpMockRequestInterceptor implements HttpInterceptor {
150     constructor(private injector: Injector) { }
151     private numberOfTypes = 0;
152
153     intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
154         if (request.method === "PUT" && request.url.includes("policies")) {
155             console.log('Answered PUT policy ', request.url, request.body);
156             return of(new HttpResponse({ status: 200 }));
157         }
158         if (request.url === "/a1-policy/v2/policy-types" && this.numberOfTypes > 0) {
159             this.numberOfTypes = 0;
160             return of(new HttpResponse({status: 200, body:JSON.parse('{"policytype_ids": ["","1"]}')}));
161         } else {
162             this.numberOfTypes = 1;
163         }
164         for (const element of urls) {
165             if (request.url === element.url) {
166                 console.log('Loaded from stub json : ' + request.url);
167                 if (request.method === 'DELETE') {
168                     return of(new HttpResponse({ status: 204 }));
169                 }
170                 return of(new HttpResponse({ status: 200, body: ((element.json) as any).default }));
171             }
172         }
173         console.log('Loaded from mock http call :' + request.url);
174         return next.handle(request);
175     }
176 }