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 policyinstanceedit from './mock/policy-instance-edit.json';
41 import * as ric1 from './mock/ric1.json';
42 import * as ric2 from './mock/ric2.json';
46 url: '/a1-policy/v2/policy-types',
50 url: '/a1-policy/v2/policy-types/',
54 url: '/a1-policy/v2/policy-types/1',
58 url: '/a1-policy/v2/policies?policytype_id=',
62 url: '/a1-policy/v2/policies?policytype_id=1',
66 url: '/a1-policy/v2/policies/2001',
67 json: noTypePolicyinstance
70 url: '/a1-policy/v2/policies/2000',
74 url: '/a1-policy/v2/policies/2100',
78 url: '/a1-policy/v2/policies/2001/status',
79 json: policyinstance1Status
82 url: '/a1-policy/v2/policies/2000/status',
83 json: policyinstance1Status
86 url: '/a1-policy/v2/policies/2100/status',
87 json: policyinstance2Status
90 url: '/a1-policy/v2/policies/2000?type=',
91 json: policyinstanceedit
94 url: '/a1-policy/v2/policies/2100?type=',
95 json: policyinstanceedit
98 url: '/a1-policy/v2/policies/2000?type=1',
99 json: policyinstanceedit
102 url: '/a1-policy/v2/policies/2100?type=1',
103 json: policyinstanceedit
106 url: '/a1-policy/v2/policies/2000?ric=ric1&type=1',
110 url: '/a1-policy/v2/rics?policytype_id=1',
114 url: '/a1-policy/v2/rics?policytype_id=',
118 url: '/ei-producer/v1/eiproducers',
122 url: '/ei-producer/v1/eiproducers/producer1',
126 url: '/ei-producer/v1/eiproducers/producer2',
130 url: '/ei-producer/v1/eiproducers/producer1/status',
131 json: eiproducerstatus1
134 url: '/ei-producer/v1/eiproducers/producer2/status',
135 json: eiproducerstatus2
138 url: '/ei-producer/v1/eiproducers/producer1/eijobs',
142 url: '/ei-producer/v1/eiproducers/producer2/eijobs',
148 export class HttpMockRequestInterceptor implements HttpInterceptor {
149 constructor(private injector: Injector) { }
151 intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
152 if (request.method === "PUT" && request.url.includes("policies")) {
153 console.log('Answered PUT policy ', request.url, request.body);
154 return of(new HttpResponse({ status: 200 }));
156 for (const element of urls) {
157 if (request.url === element.url) {
158 console.log('Loaded from stub json : ' + request.url);
159 if (request.method === 'DELETE') {
160 return of(new HttpResponse({ status: 204 }));
162 return of(new HttpResponse({ status: 200, body: ((element.json) as any).default }));
165 console.log('Loaded from mock http call :' + request.url);
166 return next.handle(request);