From 47662b25338a46e61e30ac8185353abb45529a35 Mon Sep 17 00:00:00 2001 From: elinuxhenrik Date: Wed, 14 Apr 2021 15:42:53 +0200 Subject: [PATCH] Provide test coverage of PolicyService Change-Id: Idae7ae95d40d8d019501e1ffcac8916b0139a63b Issue-ID: NONRTRIC-491 Signed-off-by: elinuxhenrik --- .../src/app/services/policy/policy.service.spec.ts | 351 +++++++++++++-------- .../src/app/services/policy/policy.service.ts | 12 - 2 files changed, 211 insertions(+), 152 deletions(-) diff --git a/webapp-frontend/src/app/services/policy/policy.service.spec.ts b/webapp-frontend/src/app/services/policy/policy.service.spec.ts index 71fd62e..a7fd56b 100644 --- a/webapp-frontend/src/app/services/policy/policy.service.spec.ts +++ b/webapp-frontend/src/app/services/policy/policy.service.spec.ts @@ -25,10 +25,14 @@ import { HttpTestingController, } from "@angular/common/http/testing"; import { + CreatePolicyInstance, PolicyInstance, PolicyInstances, + PolicyStatus, + PolicyType, PolicyTypes, } from "@interfaces/policy.types"; +import { Ric, Rics } from "@app/interfaces/ric"; describe("PolicyService", () => { let apiVersion2 = "v2"; @@ -42,160 +46,227 @@ describe("PolicyService", () => { }) ); + beforeEach(() => { + policyService = TestBed.inject(PolicyService); + httpTestingController = TestBed.inject(HttpTestingController); + }); + afterEach(() => { httpTestingController.verify(); }); - describe("#getPolicyTypes", () => { - let expectedPolicytypes: PolicyTypes; - let emptyPolicyType: PolicyTypes; - - beforeEach(() => { - policyService = TestBed.inject(PolicyService); - httpTestingController = TestBed.inject(HttpTestingController); - expectedPolicytypes = { - policytype_ids: ["", "1"], - } as PolicyTypes; - }); - //Policy Type Test Case 1: - it("should return all policy types", () => { - policyService - .getPolicyTypes() - .subscribe( - (policytypes) => - expect(policytypes).toEqual( - expectedPolicytypes, - "should return expected PolicyTypes" - ), - fail - ); - const req = httpTestingController.expectOne( - basePath + apiVersion2 + "/" + policyService.policyTypesPath + it("#getPolicyTypes should return policy types", () => { + const expectedPolicytypes = { + policytype_ids: ["", "1"], + } as PolicyTypes; + + policyService + .getPolicyTypes() + .subscribe( + (policytypes) => + expect(policytypes).toEqual( + expectedPolicytypes, + "should return expected PolicyTypes" + ), + fail ); - expect(req.request.method).toEqual("GET"); + const req = httpTestingController.expectOne( + basePath + apiVersion2 + "/" + policyService.policyTypesPath + ); + expect(req.request.method).toEqual("GET"); + + req.flush(expectedPolicytypes); + }); - req.flush(expectedPolicytypes); - }); + it("#getPolicyType", () => { + const policyTypeId = "1"; + const expectedPolicyType = { policy_schema: "schema" } as PolicyType; - //Policy Type Test Case 2: - emptyPolicyType = { - policytype_ids: [], - } as PolicyTypes; - it("should return no policy types", () => { - policyService - .getPolicyTypes() - .subscribe( - (policytypes) => - expect(policytypes).toEqual( - emptyPolicyType, - "should return empty array of Policy Types" - ), - fail - ); - - const req = httpTestingController.expectOne( - basePath + apiVersion2 + "/" + policyService.policyTypesPath + policyService + .getPolicyType(policyTypeId) + .subscribe( + (policyType) => + expect(policyType).toEqual( + expectedPolicyType, + "should return expected policy type" + ), + fail ); - req.flush(emptyPolicyType); //Return empty data - }); + + const req = httpTestingController.expectOne( + basePath + + apiVersion2 + + "/" + + policyService.policyTypesPath + + "/" + + policyTypeId + ); + expect(req.request.method).toEqual("GET"); + + req.flush(expectedPolicyType); }); - describe("#getPolicyInstances", () => { - let expectedPolicyInstances: PolicyInstances; - let emptyPolicyInstances: PolicyInstances; - let policyTypeId = "1"; - beforeEach(() => { - policyService = TestBed.inject(PolicyService); - httpTestingController = TestBed.inject(HttpTestingController); - expectedPolicyInstances = { - policy_ids: ["2100", "2000"], - } as PolicyInstances; - }); - //Policy Instances Test Case 1: - it("should return all policy instances", () => { - policyService - .getPolicyInstancesByType(policyTypeId) - .subscribe( - (policyinstances) => - expect(policyinstances).toEqual( - expectedPolicyInstances, - "should return expected Policy Instances" - ), - fail - ); - const req = httpTestingController.expectOne( - basePath + - apiVersion2 + - "/" + - policyService.policyPath + - "?" + - "policytype_id=" + - policyTypeId - ); - expect(req.request.method).toEqual("GET"); - req.flush(expectedPolicyInstances); - }); - //Policy Instances Test Case 2: - emptyPolicyInstances = { - policy_ids: [], + it("#getPolicyInstancesByType should return policy instances", () => { + const policyTypeId = "1"; + const expectedPolicyInstances = { + policy_ids: ["2100", "2000"], } as PolicyInstances; - it("should return no policy instances", () => { - policyService - .getPolicyInstancesByType(policyTypeId) - .subscribe( - (policyinstances) => - expect(policyinstances.policy_ids.length).toEqual( - 0, - "should return empty array of Policy Instances" - ), - fail - ); - const req = httpTestingController.expectOne( - basePath + - apiVersion2 + - "/" + - policyService.policyPath + - "?" + - "policytype_id=" + - policyTypeId + + policyService + .getPolicyInstancesByType(policyTypeId) + .subscribe( + (policyinstances) => + expect(policyinstances).toEqual( + expectedPolicyInstances, + "should return expected Policy Instances" + ), + fail + ); + + const req = httpTestingController.expectOne( + basePath + + apiVersion2 + + "/" + + policyService.policyPath + + "?" + + "policytype_id=" + + policyTypeId + ); + expect(req.request.method).toEqual("GET"); + + req.flush(expectedPolicyInstances); + }); + + it("#getPolicyInstance should return one policy instance", () => { + const policyId = "2000"; + const expectedPolicyInstance = { + policy_id: "2000", + policytype_id: "1", + ric_id: "ric1", + policy_data: "", + service_id: "service1", + lastModified: "", + } as PolicyInstance; + + policyService + .getPolicyInstance(policyId) + .subscribe( + (policyinstance) => + expect(policyinstance).toEqual( + expectedPolicyInstance, + "should return expected Policy Instances" + ), + fail + ); + + const req = httpTestingController.expectOne( + basePath + apiVersion2 + "/" + policyService.policyPath + "/" + policyId + ); + expect(req.request.method).toEqual("GET"); + + req.flush(expectedPolicyInstance); + }); + + it("#getPolicyStatus should return policy status", () => { + const policyId = "2000"; + const expectedPolicyStatus = { + last_modified: "modified", + } as PolicyStatus; + + policyService + .getPolicyStatus(policyId) + .subscribe( + (policyinstance) => + expect(policyinstance).toEqual( + expectedPolicyStatus, + "should return expected Policy status" + ), + fail + ); + + const req = httpTestingController.expectOne( + basePath + + apiVersion2 + + "/" + + policyService.policyPath + + "/" + + policyId + + "/status" + ); + expect(req.request.method).toEqual("GET"); + + req.flush(expectedPolicyStatus); + }); + + it("#putPolicy should return ok response", () => { + const createPolicyInstance = { policy_id: "2000" } as CreatePolicyInstance; + + policyService + .putPolicy(createPolicyInstance) + .subscribe( + (response) => + expect(response.status).toEqual( + 200, + "should return expected response" + ), + fail + ); + + const req = httpTestingController.expectOne( + basePath + apiVersion2 + "/" + policyService.policyPath + ); + expect(req.request.method).toEqual("PUT"); + + req.flush(200); + }); + + it("#deletePolicy should return ok response", () => { + const policyId = "2000"; + + policyService + .deletePolicy(policyId) + .subscribe( + (response) => + expect(response.status).toEqual( + 200, + "should return expected response" + ), + fail ); - req.flush(emptyPolicyInstances); //Return empty data - }); + + const req = httpTestingController.expectOne( + basePath + apiVersion2 + "/" + policyService.policyPath + "/2000" + ); + expect(req.request.method).toEqual("DELETE"); + + req.flush(200); }); - describe("#getPolicyInstance", () => { - let expectedPolicyInstance: PolicyInstance; - let emptyPolicyInstances: PolicyInstances; - let policyId = "2000"; - beforeEach(() => { - policyService = TestBed.inject(PolicyService); - httpTestingController = TestBed.inject(HttpTestingController); - expectedPolicyInstance = { - policy_id: "2000", - policytype_id: "1", - ric_id: "ric1", - policy_data: "", - service_id: "service1", - lastModified: "", - } as PolicyInstance; - }); - //Policy Instances Test Case 1: - it("should return one policy instance", () => { - policyService - .getPolicyInstance(policyId) - .subscribe( - (policyinstance) => - expect(policyinstance).toEqual( - expectedPolicyInstance, - "should return expected Policy Instances" - ), - fail - ); - const req = httpTestingController.expectOne( - basePath + apiVersion2 + "/" + policyService.policyPath + "/" + policyId + it("#getRics should return rics", () => { + const policyTypeId = "2000"; + const expectedRic = { ric_id: "1" } as Ric; + const expectedRics = { + rics: [expectedRic], + } as Rics; + + policyService + .getRics(policyTypeId) + .subscribe( + (rics) => + expect(rics).toEqual( + expectedRics, + "should return expected Rics" + ), + fail ); - expect(req.request.method).toEqual("GET"); - req.flush(expectedPolicyInstance); - }); + + const req = httpTestingController.expectOne( + basePath + + apiVersion2 + + "/rics?policytype_id=2000" + ); + expect(req.request.method).toEqual("GET"); + + req.flush(expectedRics); }); }); diff --git a/webapp-frontend/src/app/services/policy/policy.service.ts b/webapp-frontend/src/app/services/policy/policy.service.ts index 8c6d015..879b758 100644 --- a/webapp-frontend/src/app/services/policy/policy.service.ts +++ b/webapp-frontend/src/app/services/policy/policy.service.ts @@ -83,18 +83,6 @@ export class PolicyService { return this.httpClient.get(url); } - /** - * Gets policy parameters. - * @returns Observable that should yield a policy instance - */ - getPolicy(policyTypeId: string, policyInstanceId: string): Observable { - const url = - this.buildPath(this.policyPath, policyInstanceId) + - "?type=" + - policyTypeId; - return this.httpClient.get(url); - } - /** * Creates or replaces policy instance. * @param policyTypeId ID of the policy type that the instance will have -- 2.16.6