716341207f5f06f6df93eb208f701a6e5c717dfe
[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 eijobsProd1 from "./mock/ei-jobs-producer1.json";
39 import * as eijobsProd2 from "./mock/ei-jobs-producer2.json";
40 import * as eiProducerIds from "./mock/ei-producerids.json";
41 import * as eiproducer1 from "./mock/ei-producer1.json";
42 import * as eiproducer2 from "./mock/ei-producer2.json";
43 import * as eiproducerstatus1 from "./mock/ei-producer-status1.json";
44 import * as eiproducerstatus2 from "./mock/ei-producer-status2.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 { delay } from "rxjs/operators";
51
52 const urls = [
53   {
54     url: "/a1-policy/v2/policy-types/1",
55     json: policytypes1,
56   },
57   {
58     url: "/a1-policy/v2/policy-types/0",
59     json: policytypes0,
60   },
61   {
62     url: "/a1-policy/v2/policies?policytype_id=",
63     json: noTypePolicies,
64   },
65   {
66     url: "/a1-policy/v2/policies?policytype_id=0",
67     json: type0Policies,
68   },
69   {
70     url: "/a1-policy/v2/policies?policytype_id=1",
71     json: type1Policies,
72   },
73   {
74     url: "/a1-policy/v2/policies/2001",
75     json: noTypePolicyinstance,
76   },
77   {
78     url: "/a1-policy/v2/policies/2000",
79     json: policyinstance1,
80   },
81   {
82     url: "/a1-policy/v2/policies/2100",
83     json: policyinstance2,
84   },
85   {
86     url: "/a1-policy/v2/policies/2001/status",
87     json: policyinstance1Status,
88   },
89   {
90     url: "/a1-policy/v2/policies/2000/status",
91     json: policyinstance1Status,
92   },
93   {
94     url: "/a1-policy/v2/policies/2100/status",
95     json: policyinstance2Status,
96   },
97   {
98     url: "/a1-policy/v2/policies/2000?type=",
99     json: policyinstanceedit,
100   },
101   {
102     url: "/a1-policy/v2/policies/2100?type=",
103     json: policyinstanceedit,
104   },
105   {
106     url: "/a1-policy/v2/policies/2000?type=1",
107     json: policyinstanceedit,
108   },
109   {
110     url: "/a1-policy/v2/policies/2100?type=1",
111     json: policyinstanceedit,
112   },
113   {
114     url: "/a1-policy/v2/policies/2000?ric=ric1&type=1",
115     json: "",
116   },
117   {
118     url: "/a1-policy/v2/rics?policytype_id=0",
119     json: ric1,
120   },
121   {
122     url: "/a1-policy/v2/rics?policytype_id=1",
123     json: ric1,
124   },
125   {
126     url: "/a1-policy/v2/rics?policytype_id=",
127     json: ric2,
128   },
129   {
130     url: "/ei-producer/v1/eiproducers",
131     json: eiProducerIds,
132   },
133   {
134     url: "/ei-producer/v1/eiproducers/producer1",
135     json: eiproducer1,
136   },
137   {
138     url: "/ei-producer/v1/eiproducers/producer2",
139     json: eiproducer2,
140   },
141   {
142     url: "/ei-producer/v1/eiproducers/producer1/status",
143     json: eiproducerstatus1,
144   },
145   {
146     url: "/ei-producer/v1/eiproducers/producer2/status",
147     json: eiproducerstatus2,
148   },
149   {
150     url: "/ei-producer/v1/eiproducers/producer1/eijobs",
151     json: eijobsProd1,
152   },
153   {
154     url: "/ei-producer/v1/eiproducers/producer2/eijobs",
155     json: eijobsProd2,
156   },
157 ];
158
159 @Injectable()
160 export class HttpMockRequestInterceptor implements HttpInterceptor {
161   private toggleTypes = true;
162
163   constructor(private injector: Injector) {}
164
165   intercept(
166     request: HttpRequest<any>,
167     next: HttpHandler
168   ): Observable<HttpEvent<any>> {
169     let result: HttpResponse<any>;
170     if (request.method === "PUT" && request.url.includes("policies")) {
171       result = new HttpResponse({ status: 200 });
172     } else if (request.method === "DELETE") {
173       result = new HttpResponse({ status: 204 });
174     } else if (request.url === "/a1-policy/v2/policy-types") {
175       result = this.getAlternatingNoOfTypes();
176     } else {
177       for (const element of urls) {
178         if (request.url === element.url) {
179           result = new HttpResponse({
180             status: 200,
181             body: (element.json as any).default,
182           });
183         }
184       }
185     }
186
187     if (result) {
188       console.log(
189         "Mock answering http call :" + request.method + " " + request.url,
190         request.method === "PUT" ? request.body : null
191       );
192       console.log("Returning: " + result.status, result.body);
193       // Adding a delay to simulate real server call.
194       return of(result).pipe(delay(10));
195     } else {
196       return next.handle(request);
197     }
198   }
199
200   getAlternatingNoOfTypes(): HttpResponse<any> {
201     let result: HttpResponse<any>;
202     if (this.toggleTypes) {
203       this.toggleTypes = false;
204       result = new HttpResponse({
205         status: 200,
206         body: JSON.parse('{"policytype_ids": ["","1","0"]}'),
207       });
208     } else {
209       this.toggleTypes = true;
210       result = new HttpResponse({
211         status: 200,
212         body: JSON.parse('{"policytype_ids": ["","1"]}'),
213       });
214     }
215     return result;
216   }
217 }