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===================================
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';
47 url: '/a1-policy/v2/policy-types',
51 url: '/a1-policy/v2/policy-types/1',
55 url: '/a1-policy/v2/policy-types/0',
59 url: '/a1-policy/v2/policies?policytype_id=',
63 url: '/a1-policy/v2/policies?policytype_id=1',
67 url: '/a1-policy/v2/policies/2001',
68 json: noTypePolicyinstance
71 url: '/a1-policy/v2/policies/2000',
75 url: '/a1-policy/v2/policies/2100',
79 url: '/a1-policy/v2/policies/2001/status',
80 json: policyinstance1Status
83 url: '/a1-policy/v2/policies/2000/status',
84 json: policyinstance1Status
87 url: '/a1-policy/v2/policies/2100/status',
88 json: policyinstance2Status
91 url: '/a1-policy/v2/policies/2000?type=',
92 json: policyinstanceedit
95 url: '/a1-policy/v2/policies/2100?type=',
96 json: policyinstanceedit
99 url: '/a1-policy/v2/policies/2000?type=1',
100 json: policyinstanceedit
103 url: '/a1-policy/v2/policies/2100?type=1',
104 json: policyinstanceedit
107 url: '/a1-policy/v2/policies/2000?ric=ric1&type=1',
111 url: '/a1-policy/v2/rics?policytype_id=1',
115 url: '/a1-policy/v2/rics?policytype_id=',
119 url: '/ei-producer/v1/eiproducers',
123 url: '/ei-producer/v1/eiproducers/producer1',
127 url: '/ei-producer/v1/eiproducers/producer2',
131 url: '/ei-producer/v1/eiproducers/producer1/status',
132 json: eiproducerstatus1
135 url: '/ei-producer/v1/eiproducers/producer2/status',
136 json: eiproducerstatus2
139 url: '/ei-producer/v1/eiproducers/producer1/eijobs',
143 url: '/ei-producer/v1/eiproducers/producer2/eijobs',
149 export class HttpMockRequestInterceptor implements HttpInterceptor {
150 constructor(private injector: Injector) { }
151 private numberOfTypes = 0;
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 }));
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"]}')}));
162 this.numberOfTypes = 1;
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 }));
170 return of(new HttpResponse({ status: 200, body: ((element.json) as any).default }));
173 console.log('Loaded from mock http call :' + request.url);
174 return next.handle(request);