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