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 policyinstances1 from './mock/policy-instance-1.json';
25 import * as policyinstances2 from './mock/policy-instance-2.json';
26 import * as eijobs from './mock/ei-jobs.json';
27 import * as eiproducers from './mock/ei-producers.json';
28 import * as nopolicyinstances from './mock/nopolicyinstances.json';
29 import * as policytypes from './mock/policytypes.json';
30 import * as policyinstanceedit from './mock/policy-instance-edit.json';
31 import * as rics from './mock/rics.json';
35 url: 'api/policy/policytypes',
39 url: 'api/policy/policies?type=1',
40 json: policyinstances1
43 url: 'api/policy/policies?type=2',
44 json: policyinstances2
47 url: 'api/policy/policies?type=2',
48 json: nopolicyinstances
51 url: 'api/policy/policies/2000?type=1',
52 json: policyinstanceedit
55 url: 'api/policy/policies/2001?type=2',
56 json: policyinstanceedit
59 url: 'api/policy/policies/2000?ric=ric1&type=1',
63 url: 'api/enrichment/eijobs',
67 url: 'api/enrichment/eiproducers',
71 url: 'api/policy/rics?policyType=1',
75 url: 'api/policy/rics?policyType=2',
81 export class HttpMockRequestInterceptor implements HttpInterceptor {
82 constructor(private injector: Injector) {}
84 intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
85 if (request.method === "PUT" && request.url.includes("policies")) {
86 console.log('Answered PUT policy ' + request.url);
87 return of(new HttpResponse({ status: 200 }));
89 for (const element of urls) {
90 if (request.url === element.url) {
91 console.log('Loaded from stub json : ' + request.url);
92 return of(new HttpResponse({ status: 200, body: ((element.json) as any).default }));
95 console.log('Loaded from mock http call :' + request.url);
96 return next.handle(request);