GUI stoppig when plenty of deletions
[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 {
22   HttpErrorResponse,
23   HttpEvent,
24   HttpHandler,
25   HttpInterceptor,
26   HttpRequest,
27   HttpResponse,
28 } from "@angular/common/http";
29 import { Injectable, Injector } from "@angular/core";
30 import { Observable, of, throwError } from "rxjs";
31 import * as policyinstance1 from "./mock/policy-instance-1.json";
32 import * as noTypePolicies from "./mock/no-type-policies.json";
33 import * as type0Policies from "./mock/type0-policies.json";
34 import * as type1Policies from "./mock/type1-policies.json";
35 import * as policyinstance2 from "./mock/policy-instance-2.json";
36 import * as noTypePolicyinstance from "./mock/policy-instance-notype.json";
37 import * as policyinstance1Status from "./mock/policy-instance-1-status.json";
38 import * as policyinstance2Status from "./mock/policy-instance-2-status.json";
39 import * as producerIds from "./mock/producerids.json";
40 import * as producer1 from "./mock/producer1.json";
41 import * as producer2 from "./mock/producer2.json";
42 import * as producer3 from "./mock/producer3.json";
43 import * as producerstatus1 from "./mock/producer-status1.json";
44 import * as producerstatus2 from "./mock/producer-status2.json";
45 import * as producerstatus3 from "./mock/producer-status3.json";
46 import * as policytypes1 from "./mock/policy-type1.json";
47 import * as policytypes0 from "./mock/policy-type0.json";
48 import * as policyinstanceedit from "./mock/policy-instance-edit.json";
49 import * as ric1 from "./mock/ric1.json";
50 import * as ric2 from "./mock/ric2.json";
51 import * as ricconfig from "./mock/ric-configuration.json";
52 import * as jobIds from "./mock/jobids.json";
53 import * as infoJob1 from "./mock/info-job1.json";
54 import * as infoJob2 from "./mock/info-job2.json";
55 import * as infoJob3 from "./mock/info-job3.json";
56 import * as job1Status from "./mock/job1-status.json";
57 import * as job2Status from "./mock/job2-status.json";
58 import * as job3Status from "./mock/job3-status.json";
59 import { delay } from "rxjs/operators";
60
61 const POLICY_PATH = "/a1-policy/v2"
62 const INFO_PATH = "/data-producer/v1"
63 const CONSUMER_PATH = "/data-consumer/v1"
64
65 const urls = [
66   {
67     url: POLICY_PATH + "/policy-types/1",
68     json: policytypes1,
69   },
70   {
71     url: POLICY_PATH + "/policy-types/0",
72     json: policytypes0,
73   },
74   {
75     url: POLICY_PATH + "/policies?policytype_id=",
76     json: noTypePolicies,
77   },
78   {
79     url: POLICY_PATH + "/policies?policytype_id=0",
80     json: type0Policies,
81   },
82   {
83     url: POLICY_PATH + "/policies?policytype_id=1",
84     json: type1Policies,
85   },
86   {
87     url: POLICY_PATH + "/policies/2001",
88     json: noTypePolicyinstance,
89   },
90   {
91     url: POLICY_PATH + "/policies/2000",
92     json: policyinstance1,
93   },
94   {
95     url: POLICY_PATH + "/policies/2100",
96     json: policyinstance2,
97   },
98   {
99     url: POLICY_PATH + "/policies/2001/status",
100     json: policyinstance1Status,
101   },
102   {
103     url: POLICY_PATH + "/policies/2000/status",
104     json: policyinstance1Status,
105   },
106   {
107     url: POLICY_PATH + "/policies/2100/status",
108     json: policyinstance2Status,
109   },
110   {
111     url: POLICY_PATH + "/policies/2000?type=",
112     json: policyinstanceedit,
113   },
114   {
115     url: POLICY_PATH + "/policies/2100?type=",
116     json: policyinstanceedit,
117   },
118   {
119     url: POLICY_PATH + "/policies/2000?type=1",
120     json: policyinstanceedit,
121   },
122   {
123     url: POLICY_PATH + "/policies/2100?type=1",
124     json: policyinstanceedit,
125   },
126   {
127     url: POLICY_PATH + "/policies/2000?ric=ric1&type=1",
128     json: "",
129   },
130   {
131     url: POLICY_PATH + "/rics?policytype_id=0",
132     json: ric1,
133   },
134   {
135     url: POLICY_PATH + "/rics?policytype_id=1",
136     json: ric1,
137   },
138   {
139     url: POLICY_PATH + "/rics?policytype_id=",
140     json: ric2,
141   },
142   {
143     url: INFO_PATH + "/info-producers",
144     json: producerIds,
145   },
146   {
147     url: INFO_PATH + "/info-producers/producer1",
148     json: producer1,
149   },
150   {
151     url: INFO_PATH + "/info-producers/producer2",
152     json: producer2,
153   },
154   {
155     url: INFO_PATH + "/info-producers/producer3",
156     json: producer3,
157   },
158   {
159     url: INFO_PATH + "/info-producers/producer1/status",
160     json: producerstatus1,
161   },
162   {
163     url: INFO_PATH + "/info-producers/producer2/status",
164     json: producerstatus2,
165   },
166   {
167     url: INFO_PATH + "/info-producers/producer3/status",
168     json: producerstatus3,
169   },
170   {
171     url: CONSUMER_PATH + "/info-jobs",
172     json: jobIds,
173   },
174   {
175     url: CONSUMER_PATH + "/info-jobs/job1",
176     json: infoJob1,
177   },
178   {
179     url: CONSUMER_PATH + "/info-jobs/job2",
180     json: infoJob2,
181   },
182   {
183     url: CONSUMER_PATH + "/info-jobs/job3",
184     json: infoJob3,
185   },
186   {
187     url: CONSUMER_PATH + "/info-jobs/job1/status",
188     json: job1Status,
189   },
190   {
191     url: CONSUMER_PATH + "/info-jobs/job2/status",
192     json: job2Status,
193   },
194   {
195     url: CONSUMER_PATH + "/info-jobs/job3/status",
196     json: job3Status,
197   },
198     {
199         url: '/a1-policy/v2/configuration',
200         json: ricconfig
201     }
202 ];
203
204 @Injectable()
205 export class HttpMockRequestInterceptor implements HttpInterceptor {
206   private toggleTypes = true;
207
208   constructor(private injector: Injector) {}
209
210   intercept(
211     request: HttpRequest<any>,
212     next: HttpHandler
213   ): Observable<HttpEvent<any>> {
214     let result: HttpResponse<any>;
215     if (request.method === "PUT" && request.url.includes("policies")) {
216       result = new HttpResponse({ status: 200 });
217     } else if (request.method === "DELETE") {
218       result = new HttpResponse({ status: 204 });
219     } else if (request.url === POLICY_PATH + "/policy-types") {
220       result = this.getAlternatingNoOfTypes();
221     } else {
222       for (const element of urls) {
223         if (request.url === element.url) {
224           result = new HttpResponse({
225             status: 200,
226             body: (element.json as any).default,
227           });
228         }
229       }
230     }
231
232   if (result) {
233       console.log(
234         "Mock answering http call :" + request.method + " " + request.url,
235         request.method === "PUT" ? request.body : null
236       );
237       console.log("Returning: " + result.status, result.body);
238       // Adding a delay to simulate real server call.
239       return of(result).pipe(delay(10));
240     } else {
241       return next.handle(request);
242     }
243   }
244
245   getAlternatingNoOfTypes(): HttpResponse<any> {
246     let result: HttpResponse<any>;
247     if (this.toggleTypes) {
248       this.toggleTypes = false;
249       result = new HttpResponse({
250         status: 200,
251         body: JSON.parse('{"policytype_ids": ["","1","0"]}'),
252       });
253     } else {
254       this.toggleTypes = true;
255       result = new HttpResponse({
256         status: 200,
257         body: JSON.parse('{"policytype_ids": ["","1"]}'),
258       });
259     }
260     return result;
261   }
262
263 }