Add more mocking of 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 import * as rics from './mock/rics.json';
10
11 const urls = [
12     {
13         url: 'api/policy/policytypes',
14         json: policytypes
15     },
16     {
17         url: 'api/policy/policies?type=1',
18         json: policyinstances
19     },
20     {
21         url: 'api/policy/policies/2000?type=1',
22         json: policyinstanceedit
23     },
24     {
25         url: 'api/policy/policies/2000?ric=ric1&type=1',
26         json: ''
27     },
28     {
29         url: 'api/enrichment/eijobs',
30         json: eijobs
31     },
32     {
33         url: 'api/enrichment/eiproducers',
34         json: eiproducers
35     },
36     {
37         url: 'api/policy/rics?policyType=1',
38         json: rics
39     }
40 ];
41
42 @Injectable()
43 export class HttpMockRequestInterceptor implements HttpInterceptor {
44     constructor(private injector: Injector) {}
45
46     intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
47         if (request.method === "PUT" && request.url.includes("policies")) {
48             console.log('Answered PUT policy ' + request.url);
49             return of(new HttpResponse({ status: 200 }));
50         }
51         for (const element of urls) {
52             if (request.url === element.url) {
53                 console.log('Loaded from stub json : ' + request.url);
54                 return of(new HttpResponse({ status: 200, body: ((element.json) as any).default }));
55             }
56         }
57         console.log('Loaded from mock http call :' + request.url);
58         return next.handle(request);
59     }
60 }