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