Merge "Mock the backend API calls"
[portal/nonrtric-controlpanel.git] / webapp-frontend / src / app / interceptor.mock.ts
1 import { Injectable, Injector } from '@angular/core';
2 import { HttpEvent, HttpHandler, HttpInterceptor, HttpRequest, HttpResponse } from '@angular/common/http';
3 import { Observable, of } from 'rxjs';
4 import * as policytypes from './mock/policytypes.json';
5 import * as policyinstances from './mock/policy-instance.json';
6 import * as policyinstanceedit from './mock/policy-instance-edit.json';
7 import * as eijobs from './mock/ei-jobs.json';
8 import * as eiproducers from './mock/ei-producers.json';
9
10 const urls = [
11     {
12         url: 'api/policy/policytypes',
13         json: policytypes
14     },
15     {
16         url: 'api/policy/policies?type=1',
17         json: policyinstances
18     },
19     {
20         url: 'api/policy/policies/2000?type=1',
21         json: policyinstanceedit
22     },
23     {
24         url: 'api/policy/policies/2000?ric=ric1&type=1',
25         json: ''
26     },
27     {
28         url: 'api/enrichment/eijobs',
29         json: eijobs
30     },
31     {
32         url: 'api/enrichment/eiproducers',
33         json: eiproducers
34     }
35 ];
36
37 @Injectable()
38 export class HttpMockRequestInterceptor implements HttpInterceptor {
39     constructor(private injector: Injector) {}
40
41     intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
42         for (const element of urls) {
43             if (request.url === element.url) {
44                 console.log('Loaded from stub json : ' + request.url);
45                 return of(new HttpResponse({ status: 200, body: ((element.json) as any).default }));
46             }
47         }
48         console.log('Loaded from mock http call :' + request.url);
49         return next.handle(request);
50     }
51 }