X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=webapp-frontend%2Fsrc%2Fapp%2Finterceptor.mock.ts;h=d40a0fca8e06507886fa56d4d570bf197feeb7f4;hb=e8ded903d4c1dade40184e297aee9dd3a3232bd7;hp=e444d91aca42cef1a10094e45c441ab6765d2c91;hpb=9872b8b01da34a6677844ebd56352c9d8c3ec09b;p=portal%2Fnonrtric-controlpanel.git diff --git a/webapp-frontend/src/app/interceptor.mock.ts b/webapp-frontend/src/app/interceptor.mock.ts index e444d91..d40a0fc 100644 --- a/webapp-frontend/src/app/interceptor.mock.ts +++ b/webapp-frontend/src/app/interceptor.mock.ts @@ -18,94 +18,244 @@ * ========================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 policyinstances1 from './mock/policy-instance-1.json'; -import * as policyinstancesNoType from './mock/policy-instance-notype.json'; -import * as eijobs from './mock/ei-jobs.json'; -import * as eiproducers from './mock/ei-producers.json'; -import * as nopolicyinstances from './mock/nopolicyinstances.json'; -import * as policytypesList from './mock/policy-types.json'; -import * as policytypes1 from './mock/policy-type1.json'; -import * as policyinstanceedit from './mock/policy-instance-edit.json'; -import * as rics from './mock/rics.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 producerIds from "./mock/producerids.json"; +import * as producer1 from "./mock/producer1.json"; +import * as producer2 from "./mock/producer2.json"; +import * as producer3 from "./mock/producer3.json"; +import * as producerstatus1 from "./mock/producer-status1.json"; +import * as producerstatus2 from "./mock/producer-status2.json"; +import * as producerstatus3 from "./mock/producer-status3.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 * as ricconfig from "./mock/ric-configuration.json"; +import * as jobIds from "./mock/jobids.json"; +import * as infoJob1 from "./mock/info-job1.json"; +import * as infoJob2 from "./mock/info-job2.json"; +import * as infoJob3 from "./mock/info-job3.json"; +import * as job1Status from "./mock/job1-status.json"; +import * as job2Status from "./mock/job2-status.json"; +import * as job3Status from "./mock/job3-status.json"; +import { delay } from "rxjs/operators"; + +const POLICY_PATH = "/a1-policy/v2" +const INFO_PATH = "/data-producer/v1" +const CONSUMER_PATH = "/data-consumer/v1" const urls = [ + { + url: POLICY_PATH + "/policy-types/1", + json: policytypes1, + }, + { + url: POLICY_PATH + "/policy-types/0", + json: policytypes0, + }, + { + url: POLICY_PATH + "/policies?policytype_id=", + json: noTypePolicies, + }, + { + url: POLICY_PATH + "/policies?policytype_id=0", + json: type0Policies, + }, + { + url: POLICY_PATH + "/policies?policytype_id=1", + json: type1Policies, + }, + { + url: POLICY_PATH + "/policies/2001", + json: noTypePolicyinstance, + }, + { + url: POLICY_PATH + "/policies/2000", + json: policyinstance1, + }, + { + url: POLICY_PATH + "/policies/2100", + json: policyinstance2, + }, + { + url: POLICY_PATH + "/policies/2001/status", + json: policyinstance1Status, + }, + { + url: POLICY_PATH + "/policies/2000/status", + json: policyinstance1Status, + }, + { + url: POLICY_PATH + "/policies/2100/status", + json: policyinstance2Status, + }, + { + url: POLICY_PATH + "/policies/2000?type=", + json: policyinstanceedit, + }, + { + url: POLICY_PATH + "/policies/2100?type=", + json: policyinstanceedit, + }, + { + url: POLICY_PATH + "/policies/2000?type=1", + json: policyinstanceedit, + }, + { + url: POLICY_PATH + "/policies/2100?type=1", + json: policyinstanceedit, + }, + { + url: POLICY_PATH + "/policies/2000?ric=ric1&type=1", + json: "", + }, + { + url: POLICY_PATH + "/rics?policytype_id=0", + json: ric1, + }, + { + url: POLICY_PATH + "/rics?policytype_id=1", + json: ric1, + }, + { + url: POLICY_PATH + "/rics?policytype_id=", + json: ric2, + }, + { + url: INFO_PATH + "/info-producers", + json: producerIds, + }, + { + url: INFO_PATH + "/info-producers/producer1", + json: producer1, + }, + { + url: INFO_PATH + "/info-producers/producer2", + json: producer2, + }, + { + url: INFO_PATH + "/info-producers/producer3", + json: producer3, + }, + { + url: INFO_PATH + "/info-producers/producer1/status", + json: producerstatus1, + }, + { + url: INFO_PATH + "/info-producers/producer2/status", + json: producerstatus2, + }, + { + url: INFO_PATH + "/info-producers/producer3/status", + json: producerstatus3, + }, + { + url: CONSUMER_PATH + "/info-jobs", + json: jobIds, + }, + { + url: CONSUMER_PATH + "/info-jobs/job1", + json: infoJob1, + }, + { + url: CONSUMER_PATH + "/info-jobs/job2", + json: infoJob2, + }, + { + url: CONSUMER_PATH + "/info-jobs/job3", + json: infoJob3, + }, + { + url: CONSUMER_PATH + "/info-jobs/job1/status", + json: job1Status, + }, + { + url: CONSUMER_PATH + "/info-jobs/job2/status", + json: job2Status, + }, + { + url: CONSUMER_PATH + "/info-jobs/job3/status", + json: job3Status, + }, { - url: 'v2/policy-types', - json: policytypesList - }, - { - url: 'v2/policy-types/', - json: policytypes1 - }, - { - url: 'v2/policy-types/1', - json: policytypes1 - }, - { - url: 'v2/policies?type=1', - json: policyinstances1 - }, - { - url: 'v2/policies?type=', - json: policyinstancesNoType - }, - { - url: 'v2/policies?type=2', - json: nopolicyinstances - }, - { - url: 'v2/policies/2000?type=1', - json: policyinstanceedit - }, - { - url: 'v2/policies/3000?type=1', - json: policyinstanceedit - }, - { - url: 'v2/policies/2001?type=', - json: policyinstanceedit - }, - { - url: 'v2/policies/2000?ric=ric1&type=1', - json: '' - }, - { - url: 'api/enrichment/eijobs', - json: eijobs - }, - { - url: 'api/enrichment/eiproducers', - json: eiproducers - }, - { - url: 'api/policy/rics?policyType=1', - json: rics - }, - { - url: 'api/policy/rics?policyType=2', - json: rics + url: '/a1-policy/v2/configuration', + json: ricconfig } ]; @Injectable() export class HttpMockRequestInterceptor implements HttpInterceptor { - constructor(private injector: Injector) {} + private toggleTypes = true; - intercept(request: HttpRequest, next: HttpHandler): Observable> { - if (request.method === "PUT" && request.url.includes("policies")) { - console.log('Answered PUT policy ' + request.url); - return of(new HttpResponse({ status: 200 })); - } - for (const element of urls) { - if (request.url === element.url) { - console.log('Loaded from stub json : ' + request.url); - return of(new HttpResponse({ status: 200, body: ((element.json) as any).default })); - } + constructor(private injector: Injector) {} + + intercept( + request: HttpRequest, + next: HttpHandler + ): Observable> { + let result: HttpResponse; + 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 === POLICY_PATH + "/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, + }); } - console.log('Loaded from mock http call :' + request.url); - return next.handle(request); + } + } + + 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 { + let result: HttpResponse; + 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"]}'), + }); } -} \ No newline at end of file + return result; + } +}