Improve mock interceptor

Change-Id: I98e5e0a2d131e54975621b432d4e8738dc80bc9b
Issue-ID: NONRTRIC-492
Signed-off-by: elinuxhenrik <henrik.b.andersson@est.tech>
diff --git a/webapp-frontend/src/app/interceptor.mock.ts b/webapp-frontend/src/app/interceptor.mock.ts
index ad0d080..42e6855 100644
--- a/webapp-frontend/src/app/interceptor.mock.ts
+++ b/webapp-frontend/src/app/interceptor.mock.ts
@@ -18,159 +18,196 @@
  * ========================LICENSE_END===================================
  */
 
-import { HttpEvent, HttpHandler, HttpInterceptor, HttpRequest, HttpResponse } from '@angular/common/http';
-import { Injectable, Injector } from '@angular/core';
-import { Observable, of } from 'rxjs';
-import * as policyinstance1 from './mock/policy-instance-1.json';
-import * as noTypePolicies from './mock/no-type-policies.json';
-import * as typedPolicies from './mock/policies.json';
-import * as policyinstance2 from './mock/policy-instance-2.json';
-import * as noTypePolicyinstance from './mock/policy-instance-notype.json';
-import * as policyinstance1Status from './mock/policy-instance-1-status.json';
-import * as policyinstance2Status from './mock/policy-instance-2-status.json';
-import * as eijobsProd1 from './mock/ei-jobs-producer1.json';
-import * as eijobsProd2 from './mock/ei-jobs-producer2.json';
-import * as eiProducerIds from './mock/ei-producerids.json';
-import * as eiproducer1 from './mock/ei-producer1.json';
-import * as eiproducer2 from './mock/ei-producer2.json';
-import * as eiproducerstatus1 from './mock/ei-producer-status1.json';
-import * as eiproducerstatus2 from './mock/ei-producer-status2.json';
-import * as policytypesList from './mock/policy-types.json';
-import * as policytypes1 from './mock/policy-type1.json';
-import * as policytypes0 from './mock/policy-type0.json';
-import * as policyinstanceedit from './mock/policy-instance-edit.json';
-import * as ric1 from './mock/ric1.json';
-import * as ric2 from './mock/ric2.json';
+import {
+  HttpEvent,
+  HttpHandler,
+  HttpInterceptor,
+  HttpRequest,
+  HttpResponse,
+} from "@angular/common/http";
+import { Injectable, Injector } from "@angular/core";
+import { Observable, of } from "rxjs";
+import * as policyinstance1 from "./mock/policy-instance-1.json";
+import * as noTypePolicies from "./mock/no-type-policies.json";
+import * as type0Policies from "./mock/type0-policies.json";
+import * as type1Policies from "./mock/type1-policies.json";
+import * as policyinstance2 from "./mock/policy-instance-2.json";
+import * as noTypePolicyinstance from "./mock/policy-instance-notype.json";
+import * as policyinstance1Status from "./mock/policy-instance-1-status.json";
+import * as policyinstance2Status from "./mock/policy-instance-2-status.json";
+import * as eijobsProd1 from "./mock/ei-jobs-producer1.json";
+import * as eijobsProd2 from "./mock/ei-jobs-producer2.json";
+import * as eiProducerIds from "./mock/ei-producerids.json";
+import * as eiproducer1 from "./mock/ei-producer1.json";
+import * as eiproducer2 from "./mock/ei-producer2.json";
+import * as eiproducerstatus1 from "./mock/ei-producer-status1.json";
+import * as eiproducerstatus2 from "./mock/ei-producer-status2.json";
+import * as policytypes1 from "./mock/policy-type1.json";
+import * as policytypes0 from "./mock/policy-type0.json";
+import * as policyinstanceedit from "./mock/policy-instance-edit.json";
+import * as ric1 from "./mock/ric1.json";
+import * as ric2 from "./mock/ric2.json";
+import { delay } from "rxjs/operators";
 
 const urls = [
-    {
-        url: '/a1-policy/v2/policy-types',
-        json: policytypesList
-    },
-    {
-        url: '/a1-policy/v2/policy-types/1',
-        json: policytypes1
-    },
-    {
-        url: '/a1-policy/v2/policy-types/0',
-        json: policytypes0
-    },
-    {
-        url: '/a1-policy/v2/policies?policytype_id=',
-        json: noTypePolicies
-    },
-    {
-        url: '/a1-policy/v2/policies?policytype_id=1',
-        json: typedPolicies
-    },
-    {
-        url: '/a1-policy/v2/policies/2001',
-        json: noTypePolicyinstance
-    },
-    {
-        url: '/a1-policy/v2/policies/2000',
-        json: policyinstance1
-    },
-    {
-        url: '/a1-policy/v2/policies/2100',
-        json: policyinstance2
-    },
-    {
-        url: '/a1-policy/v2/policies/2001/status',
-        json: policyinstance1Status
-    },
-    {
-        url: '/a1-policy/v2/policies/2000/status',
-        json: policyinstance1Status
-    },
-    {
-        url: '/a1-policy/v2/policies/2100/status',
-        json: policyinstance2Status
-    },
-    {
-        url: '/a1-policy/v2/policies/2000?type=',
-        json: policyinstanceedit
-    },
-    {
-        url: '/a1-policy/v2/policies/2100?type=',
-        json: policyinstanceedit
-    },
-    {
-        url: '/a1-policy/v2/policies/2000?type=1',
-        json: policyinstanceedit
-    },
-    {
-        url: '/a1-policy/v2/policies/2100?type=1',
-        json: policyinstanceedit
-    },
-    {
-        url: '/a1-policy/v2/policies/2000?ric=ric1&type=1',
-        json: ''
-    },
-    {
-        url: '/a1-policy/v2/rics?policytype_id=1',
-        json: ric1
-    },
-    {
-        url: '/a1-policy/v2/rics?policytype_id=',
-        json: ric2
-    },
-    {
-        url: '/ei-producer/v1/eiproducers',
-        json: eiProducerIds
-    },
-    {
-        url: '/ei-producer/v1/eiproducers/producer1',
-        json: eiproducer1
-    },
-    {
-        url: '/ei-producer/v1/eiproducers/producer2',
-        json: eiproducer2
-    },
-    {
-        url: '/ei-producer/v1/eiproducers/producer1/status',
-        json: eiproducerstatus1
-    },
-    {
-        url: '/ei-producer/v1/eiproducers/producer2/status',
-        json: eiproducerstatus2
-    },
-    {
-        url: '/ei-producer/v1/eiproducers/producer1/eijobs',
-        json: eijobsProd1
-    },
-    {
-        url: '/ei-producer/v1/eiproducers/producer2/eijobs',
-        json: eijobsProd2
-    }
+  {
+    url: "/a1-policy/v2/policy-types/1",
+    json: policytypes1,
+  },
+  {
+    url: "/a1-policy/v2/policy-types/0",
+    json: policytypes0,
+  },
+  {
+    url: "/a1-policy/v2/policies?policytype_id=",
+    json: noTypePolicies,
+  },
+  {
+    url: "/a1-policy/v2/policies?policytype_id=0",
+    json: type0Policies,
+  },
+  {
+    url: "/a1-policy/v2/policies?policytype_id=1",
+    json: type1Policies,
+  },
+  {
+    url: "/a1-policy/v2/policies/2001",
+    json: noTypePolicyinstance,
+  },
+  {
+    url: "/a1-policy/v2/policies/2000",
+    json: policyinstance1,
+  },
+  {
+    url: "/a1-policy/v2/policies/2100",
+    json: policyinstance2,
+  },
+  {
+    url: "/a1-policy/v2/policies/2001/status",
+    json: policyinstance1Status,
+  },
+  {
+    url: "/a1-policy/v2/policies/2000/status",
+    json: policyinstance1Status,
+  },
+  {
+    url: "/a1-policy/v2/policies/2100/status",
+    json: policyinstance2Status,
+  },
+  {
+    url: "/a1-policy/v2/policies/2000?type=",
+    json: policyinstanceedit,
+  },
+  {
+    url: "/a1-policy/v2/policies/2100?type=",
+    json: policyinstanceedit,
+  },
+  {
+    url: "/a1-policy/v2/policies/2000?type=1",
+    json: policyinstanceedit,
+  },
+  {
+    url: "/a1-policy/v2/policies/2100?type=1",
+    json: policyinstanceedit,
+  },
+  {
+    url: "/a1-policy/v2/policies/2000?ric=ric1&type=1",
+    json: "",
+  },
+  {
+    url: "/a1-policy/v2/rics?policytype_id=1",
+    json: ric1,
+  },
+  {
+    url: "/a1-policy/v2/rics?policytype_id=",
+    json: ric2,
+  },
+  {
+    url: "/ei-producer/v1/eiproducers",
+    json: eiProducerIds,
+  },
+  {
+    url: "/ei-producer/v1/eiproducers/producer1",
+    json: eiproducer1,
+  },
+  {
+    url: "/ei-producer/v1/eiproducers/producer2",
+    json: eiproducer2,
+  },
+  {
+    url: "/ei-producer/v1/eiproducers/producer1/status",
+    json: eiproducerstatus1,
+  },
+  {
+    url: "/ei-producer/v1/eiproducers/producer2/status",
+    json: eiproducerstatus2,
+  },
+  {
+    url: "/ei-producer/v1/eiproducers/producer1/eijobs",
+    json: eijobsProd1,
+  },
+  {
+    url: "/ei-producer/v1/eiproducers/producer2/eijobs",
+    json: eijobsProd2,
+  },
 ];
 
 @Injectable()
 export class HttpMockRequestInterceptor implements HttpInterceptor {
-    constructor(private injector: Injector) { }
-    private numberOfTypes = 0;
+  private toggleTypes = true;
 
-    intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
-        if (request.method === "PUT" && request.url.includes("policies")) {
-            console.log('Answered PUT policy ', request.url, request.body);
-            return of(new HttpResponse({ status: 200 }));
+  constructor(private injector: Injector) {}
+
+  intercept(
+    request: HttpRequest<any>,
+    next: HttpHandler
+  ): Observable<HttpEvent<any>> {
+    let result: HttpResponse<any>;
+    if (request.method === "PUT" && request.url.includes("policies")) {
+      result = new HttpResponse({ status: 200 });
+    } else if (request.method === "DELETE") {
+      result = new HttpResponse({ status: 204 });
+    } else if (request.url === "/a1-policy/v2/policy-types") {
+      result = this.getAlternatingNoOfTypes();
+    } else {
+      for (const element of urls) {
+        if (request.url === element.url) {
+          result = new HttpResponse({
+            status: 200,
+            body: (element.json as any).default,
+          });
         }
-        if (request.url === "/a1-policy/v2/policy-types" && this.numberOfTypes > 0) {
-            this.numberOfTypes = 0;
-            return of(new HttpResponse({status: 200, body:JSON.parse('{"policytype_ids": ["","1"]}')}));
-        } else {
-            this.numberOfTypes = 1;
-        }
-        for (const element of urls) {
-            if (request.url === element.url) {
-                console.log('Loaded from stub json : ' + request.url);
-                if (request.method === 'DELETE') {
-                    return of(new HttpResponse({ status: 204 }));
-                }
-                return of(new HttpResponse({ status: 200, body: ((element.json) as any).default }));
-            }
-        }
-        console.log('Loaded from mock http call :' + request.url);
-        return next.handle(request);
+      }
     }
-}
\ No newline at end of file
+
+    if (result) {
+      console.log(
+        "Mock answering http call :" + request.method + " " + request.url,
+        request.method === "PUT" ? request.body : null
+      );
+      console.log("Returning: " + result.status, result.body);
+      // Adding a delay to simulate real server call.
+      return of(result).pipe(delay(10));
+    } else {
+      return next.handle(request);
+    }
+  }
+
+  getAlternatingNoOfTypes(): HttpResponse<any> {
+    let result: HttpResponse<any>;
+    if (this.toggleTypes) {
+      this.toggleTypes = false;
+      result = new HttpResponse({
+        status: 200,
+        body: JSON.parse('{"policytype_ids": ["","1","0"]}'),
+      });
+    } else {
+      this.toggleTypes = true;
+      result = new HttpResponse({
+        status: 200,
+        body: JSON.parse('{"policytype_ids": ["","1"]}'),
+      });
+    }
+    return result;
+  }
+}
diff --git a/webapp-frontend/src/app/mock/policy-types.json b/webapp-frontend/src/app/mock/policy-types.json
deleted file mode 100644
index c65b820..0000000
--- a/webapp-frontend/src/app/mock/policy-types.json
+++ /dev/null
@@ -1,7 +0,0 @@
-{
-  "policytype_ids": [
-    "",
-    "1",
-    "0"
-  ]
-}
\ No newline at end of file
diff --git a/webapp-frontend/src/app/mock/policytypes.json b/webapp-frontend/src/app/mock/policytypes.json
deleted file mode 100644
index a59e12d..0000000
--- a/webapp-frontend/src/app/mock/policytypes.json
+++ /dev/null
@@ -1,14 +0,0 @@
-[
-  {
-    "name": "2",
-    "schema": "{\"$schema\":\"http://json-schema.org/draft-07/schema#\",\"description\":\"Type 2 policy type\",\"additionalProperties\":false,\"title\":\"2\",\"type\":\"object\",\"properties\":{\"qosObjectives\":{\"additionalProperties\":false,\"type\":\"object\",\"properties\":{\"priorityLevel\":{\"type\":\"number\"}},\"required\":[\"priorityLevel\"]},\"scope\":{\"additionalProperties\":false,\"type\":\"object\",\"properties\":{\"qosId\":{\"type\":\"string\"},\"ueId\":{\"type\":\"string\"}},\"required\":[\"ueId\",\"qosId\"]}},\"required\":[\"scope\",\"qosObjectives\"]}"
-  },
-  {
-    "name": "1",
-    "schema": "{\"$schema\":\"http://json-schema.org/draft-07/schema#\",\"description\":\"Type 1 policy type\",\"additionalProperties\":false,\"title\":\"1\",\"type\":\"object\",\"properties\":{\"qosObjectives\":{\"additionalProperties\":false,\"type\":\"object\",\"properties\":{\"priorityLevel\":{\"type\":\"number\"}},\"required\":[\"priorityLevel\"]},\"scope\":{\"additionalProperties\":false,\"type\":\"object\",\"properties\":{\"qosId\":{\"type\":\"string\"},\"ueId\":{\"type\":\"string\"}},\"required\":[\"ueId\",\"qosId\"]}},\"required\":[\"scope\",\"qosObjectives\"]}"
-  },
-  {
-    "name": "0",
-    "schema": "{\"$schema\":\"http://json-schema.org/draft-07/schema#\",\"description\":\"Type 0 policy type (no instances)\",\"additionalProperties\":false,\"title\":\"1\",\"type\":\"object\",\"properties\":{\"qosObjectives\":{\"additionalProperties\":false,\"type\":\"object\",\"properties\":{\"priorityLevel\":{\"type\":\"number\"}},\"required\":[\"priorityLevel\"]},\"scope\":{\"additionalProperties\":false,\"type\":\"object\",\"properties\":{\"qosId\":{\"type\":\"string\"},\"ueId\":{\"type\":\"string\"}},\"required\":[\"ueId\",\"qosId\"]}},\"required\":[\"scope\",\"qosObjectives\"]}"
-  }
-  ]
\ No newline at end of file
diff --git a/webapp-frontend/src/app/mock/type0-policies.json b/webapp-frontend/src/app/mock/type0-policies.json
new file mode 100644
index 0000000..e70a064
--- /dev/null
+++ b/webapp-frontend/src/app/mock/type0-policies.json
@@ -0,0 +1,3 @@
+{
+  "policy_ids": []
+}
\ No newline at end of file
diff --git a/webapp-frontend/src/app/mock/policies.json b/webapp-frontend/src/app/mock/type1-policies.json
similarity index 100%
rename from webapp-frontend/src/app/mock/policies.json
rename to webapp-frontend/src/app/mock/type1-policies.json