2 * ========================LICENSE_START=================================
5 * Copyright (C) 2021 Nordix Foundation
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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===================================
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 jobsProd1 from "./mock/jobs-producer1.json";
39 import * as jobsProd2 from "./mock/jobs-producer2.json";
40 import * as producerIds from "./mock/producerids.json";
41 import * as producer1 from "./mock/producer1.json";
42 import * as producer2 from "./mock/producer2.json";
43 import * as producerstatus1 from "./mock/producer-status1.json";
44 import * as producerstatus2 from "./mock/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 * as ricconfig from "./mock/ric-configuration.json";
51 import { delay } from "rxjs/operators";
53 const POLICY_PATH = "/a1-policy/v2"
54 const INFO_PATH = "/data-producer/v1"
58 url: POLICY_PATH + "/policy-types/1",
62 url: POLICY_PATH + "/policy-types/0",
66 url: POLICY_PATH + "/policies?policytype_id=",
70 url: POLICY_PATH + "/policies?policytype_id=0",
74 url: POLICY_PATH + "/policies?policytype_id=1",
78 url: POLICY_PATH + "/policies/2001",
79 json: noTypePolicyinstance,
82 url: POLICY_PATH + "/policies/2000",
83 json: policyinstance1,
86 url: POLICY_PATH + "/policies/2100",
87 json: policyinstance2,
90 url: POLICY_PATH + "/policies/2001/status",
91 json: policyinstance1Status,
94 url: POLICY_PATH + "/policies/2000/status",
95 json: policyinstance1Status,
98 url: POLICY_PATH + "/policies/2100/status",
99 json: policyinstance2Status,
102 url: POLICY_PATH + "/policies/2000?type=",
103 json: policyinstanceedit,
106 url: POLICY_PATH + "/policies/2100?type=",
107 json: policyinstanceedit,
110 url: POLICY_PATH + "/policies/2000?type=1",
111 json: policyinstanceedit,
114 url: POLICY_PATH + "/policies/2100?type=1",
115 json: policyinstanceedit,
118 url: POLICY_PATH + "/policies/2000?ric=ric1&type=1",
122 url: POLICY_PATH + "/rics?policytype_id=0",
126 url: POLICY_PATH + "/rics?policytype_id=1",
130 url: POLICY_PATH + "/rics?policytype_id=",
134 url: INFO_PATH + "/info-producers",
138 url: INFO_PATH + "/info-producers/producer1",
142 url: INFO_PATH + "/info-producers/producer2",
146 url: INFO_PATH + "/info-producers/producer1/status",
147 json: producerstatus1,
150 url: INFO_PATH + "/info-producers/producer2/status",
151 json: producerstatus2,
154 url: INFO_PATH + "/info-producers/producer1/info-jobs",
158 url: INFO_PATH + "/info-producers/producer2/info-jobs",
162 url: '/a1-policy/v2/configuration',
168 export class HttpMockRequestInterceptor implements HttpInterceptor {
169 private toggleTypes = true;
171 constructor(private injector: Injector) {}
174 request: HttpRequest<any>,
176 ): Observable<HttpEvent<any>> {
177 let result: HttpResponse<any>;
178 if (request.method === "PUT" && request.url.includes("policies")) {
179 result = new HttpResponse({ status: 200 });
180 } else if (request.method === "DELETE") {
181 result = new HttpResponse({ status: 204 });
182 } else if (request.url === POLICY_PATH + "/policy-types") {
183 result = this.getAlternatingNoOfTypes();
185 for (const element of urls) {
186 if (request.url === element.url) {
187 result = new HttpResponse({
189 body: (element.json as any).default,
197 "Mock answering http call :" + request.method + " " + request.url,
198 request.method === "PUT" ? request.body : null
200 console.log("Returning: " + result.status, result.body);
201 // Adding a delay to simulate real server call.
202 return of(result).pipe(delay(10));
204 return next.handle(request);
208 getAlternatingNoOfTypes(): HttpResponse<any> {
209 let result: HttpResponse<any>;
210 if (this.toggleTypes) {
211 this.toggleTypes = false;
212 result = new HttpResponse({
214 body: JSON.parse('{"policytype_ids": ["","1","0"]}'),
217 this.toggleTypes = true;
218 result = new HttpResponse({
220 body: JSON.parse('{"policytype_ids": ["","1"]}'),