X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=webapp-frontend%2Fsrc%2Fapp%2Fservices%2Fpolicy%2Fpolicy.service.spec.ts;h=e0afb464d0d336a17c8f7e54dc112eb3abf3e53e;hb=refs%2Fchanges%2F71%2F6071%2F2;hp=ee20509e5a02055f2fef30643d2c73576f517098;hpb=e409ac955e6f0654f1721c69e1885bda23e1cf99;p=portal%2Fnonrtric-controlpanel.git 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 ee20509..e0afb46 100644 --- a/webapp-frontend/src/app/services/policy/policy.service.spec.ts +++ b/webapp-frontend/src/app/services/policy/policy.service.spec.ts @@ -17,100 +17,256 @@ * limitations under the License. * ========================LICENSE_END=================================== */ -import { TestBed } from '@angular/core/testing'; +import { TestBed } from "@angular/core/testing"; -import { PolicyService } from './policy.service'; -import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing' -import { PolicyInstance, PolicyTypes } from '../../interfaces/policy.types'; +import { PolicyService } from "./policy.service"; +import { + HttpClientTestingModule, + HttpTestingController, +} from "@angular/common/http/testing"; +import { + CreatePolicyInstance, + PolicyInstance, + PolicyInstances, + PolicyStatus, + PolicyType, + PolicyTypes, +} from "@interfaces/policy.types"; +import { Ric, Rics } from "@interfaces/ric"; -describe('PolicyService', () => { - let apiVersion2 = 'v2' - let basePath = ''; +describe("PolicyService", () => { + let apiVersion2 = "v2"; + let basePath = "/a1-policy/"; let policyService: PolicyService; let httpTestingController: HttpTestingController; - beforeEach(() => TestBed.configureTestingModule({ - imports: [HttpClientTestingModule], - providers: [ - PolicyService - ], - })); + beforeEach(() => + TestBed.configureTestingModule({ + imports: [HttpClientTestingModule], + providers: [PolicyService], + }) + ); + + beforeEach(() => { + policyService = TestBed.inject(PolicyService); + httpTestingController = TestBed.inject(HttpTestingController); + }); afterEach(() => { httpTestingController.verify(); }); - describe('#getPolicyTypes', () => { - let expectedPolicytypes: PolicyTypes; - let emptyPolicyType: PolicyTypes; - - beforeEach(() => { - policyService = TestBed.get(PolicyService); - httpTestingController = TestBed.get(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'), + 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 ); - - const req = httpTestingController.expectOne(basePath + apiVersion2 + '/' + policyService.policyTypesPath); - 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); + }); - //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'), + it("#getPolicyType", () => { + const policyTypeId = "1"; + const expectedPolicyType = { policy_schema: "schema" } as PolicyType; + + policyService + .getPolicyType(policyTypeId) + .subscribe( + (policyType) => + expect(policyType).toEqual( + expectedPolicyType, + "should return expected policy type" + ), fail ); - const req = httpTestingController.expectOne(basePath + apiVersion2 + '/' + policyService.policyTypesPath); - 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('#getPolicyInstance', () => { - let expectedPolicyInstances: PolicyInstance[]; - let policyTypeId: string; - beforeEach(() => { - policyService = TestBed.get(PolicyService); - httpTestingController = TestBed.get(HttpTestingController); - expectedPolicyInstances = [ - { id: '2000', json: '{"scope": {"ueId": "ue3100","qosId": "qos3100"},"qosObjectives": {"priorityLevel": 3100}}', service: 'service1', lastModified: '2020-12-08T21:12:43.719084Z' } - ] as PolicyInstance[]; - policyTypeId = "1"; - }); - //Policy Instances Test Case 1: - it('should return all policy instances', () => { - policyService.getPolicyInstances(policyTypeId).subscribe( - policyinstances => expect(policyinstances).toEqual(expectedPolicyInstances, 'should return expected Policy Instances'), + + it("#getPolicyInstancesByType should return policy instances", () => { + const policyTypeId = "1"; + const expectedPolicyInstances = { + policy_ids: ["2100", "2000"], + } as PolicyInstances; + + policyService + .getPolicyInstancesByType(policyTypeId) + .subscribe( + (policyinstances) => + expect(policyinstances).toEqual( + expectedPolicyInstances, + "should return expected Policy Instances" + ), fail ); - const req = httpTestingController.expectOne(basePath + apiVersion2 + '/' + policyService.policyPath + '?type=' + policyTypeId); - expect(req.request.method).toEqual('GET'); - req.flush(expectedPolicyInstances); - }); - - //Policy Instances Test Case 2: - it('should return no policy instances', () => { - policyService.getPolicyInstances(policyTypeId).subscribe( - policyinstances => expect(policyinstances.length).toEqual(0, 'should return empty array of Policy Instances'), + + 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 + '?type=' + policyTypeId); - req.flush([]); //Return empty data - }); + + 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 + ); + + const req = httpTestingController.expectOne( + basePath + apiVersion2 + "/" + policyService.policyPath + "/2000" + ); + expect(req.request.method).toEqual("DELETE"); + + req.flush(200); + }); + + 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 + ); + + const req = httpTestingController.expectOne( + basePath + + apiVersion2 + + "/rics?policytype_id=2000" + ); + expect(req.request.method).toEqual("GET"); + + req.flush(expectedRics); }); });