From 3b19af38b08cd6838cf58fbc03064e4a6fc091eb Mon Sep 17 00:00:00 2001 From: Lathish Date: Tue, 19 Jan 2021 21:55:58 +0000 Subject: [PATCH] Add test Coverage for Policy Service Issue-ID: NONRTRIC-392 Change-Id: I7bfc293ecba751afd24fdf2ed06c0635f984c9d6 Signed-off-by: Lathish --- webapp-frontend/src/app/interceptor.mock.ts | 34 ++++++++- webapp-frontend/src/app/interceptor.ts | 20 +++++ ...policy-instance.json => policy-instance-1.json} | 0 .../src/app/mock/policy-instance-2.json | 18 +++++ .../src/app/services/policy/policy.service.spec.ts | 86 ++++++++++++++++++++-- .../src/app/services/policy/policy.service.ts | 4 +- webapp-frontend/src/app/services/ui/ui.service.ts | 6 +- 7 files changed, 155 insertions(+), 13 deletions(-) rename webapp-frontend/src/app/mock/{policy-instance.json => policy-instance-1.json} (100%) create mode 100644 webapp-frontend/src/app/mock/policy-instance-2.json diff --git a/webapp-frontend/src/app/interceptor.mock.ts b/webapp-frontend/src/app/interceptor.mock.ts index e22c99d..89034a8 100644 --- a/webapp-frontend/src/app/interceptor.mock.ts +++ b/webapp-frontend/src/app/interceptor.mock.ts @@ -1,13 +1,33 @@ +/*- + * ========================LICENSE_START================================= + * O-RAN-SC + * %% + * Copyright (C) 2021 Nordix Foundation + * %% + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ========================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 policyinstances2 from './mock/policy-instance-2.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 policytypes from './mock/policytypes.json'; import * as policyinstanceedit from './mock/policy-instance-edit.json'; -import * as policyinstances from './mock/policy-instance.json'; import * as rics from './mock/rics.json'; const urls = [ @@ -17,7 +37,11 @@ const urls = [ }, { url: 'api/policy/policies?type=1', - json: policyinstances + json: policyinstances1 + }, + { + url: 'api/policy/policies?type=2', + json: policyinstances2 }, { url: 'api/policy/policies?type=2', @@ -27,6 +51,10 @@ const urls = [ url: 'api/policy/policies/2000?type=1', json: policyinstanceedit }, + { + url: 'api/policy/policies/2001?type=2', + json: policyinstanceedit + }, { url: 'api/policy/policies/2000?ric=ric1&type=1', json: '' diff --git a/webapp-frontend/src/app/interceptor.ts b/webapp-frontend/src/app/interceptor.ts index 6b46138..a9253f8 100644 --- a/webapp-frontend/src/app/interceptor.ts +++ b/webapp-frontend/src/app/interceptor.ts @@ -1,3 +1,23 @@ +/*- + * ========================LICENSE_START================================= + * O-RAN-SC + * %% + * Copyright (C) 2021 Nordix Foundation + * %% + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ========================LICENSE_END=================================== + */ + import { Injectable, Injector } from '@angular/core'; import { HttpEvent, HttpHandler, HttpInterceptor, HttpRequest, HttpResponse } from '@angular/common/http'; import { Observable, of } from 'rxjs'; diff --git a/webapp-frontend/src/app/mock/policy-instance.json b/webapp-frontend/src/app/mock/policy-instance-1.json similarity index 100% rename from webapp-frontend/src/app/mock/policy-instance.json rename to webapp-frontend/src/app/mock/policy-instance-1.json diff --git a/webapp-frontend/src/app/mock/policy-instance-2.json b/webapp-frontend/src/app/mock/policy-instance-2.json new file mode 100644 index 0000000..25b75ee --- /dev/null +++ b/webapp-frontend/src/app/mock/policy-instance-2.json @@ -0,0 +1,18 @@ +[ + { + "id": "2001", + "type": "2", + "ric": "ric1", + "json": { + "scope": { + "ueId": "ue3100", + "qosId": "qos3100" + }, + "qosObjectives": { + "priorityLevel": 3100 + } + }, + "service": "service1", + "lastModified": "2020-12-08T21:12:43.719084Z" + } + ] \ No newline at end of file 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 135e413..15f849c 100644 --- a/webapp-frontend/src/app/services/policy/policy.service.spec.ts +++ b/webapp-frontend/src/app/services/policy/policy.service.spec.ts @@ -2,7 +2,7 @@ * ========================LICENSE_START================================= * O-RAN-SC * %% - * Copyright (C) 2019 Nordix Foundation + * Copyright (C) 2021 Nordix Foundation * %% * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,15 +20,89 @@ import { TestBed } from '@angular/core/testing'; import { PolicyService } from './policy.service'; -import { HttpClientTestingModule } from '@angular/common/http/testing' +import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing' +import { PolicyInstance, PolicyType } from '../../interfaces/policy.types'; describe('PolicyService', () => { + let basePath = 'api/policy'; + let policyService: PolicyService; + let httpTestingController: HttpTestingController; beforeEach(() => TestBed.configureTestingModule({ - imports: [HttpClientTestingModule] + imports: [HttpClientTestingModule], + providers: [ + PolicyService + ], })); - it('should be created', () => { - const service: PolicyService = TestBed.get(PolicyService); - expect(service).toBeTruthy(); + afterEach(() => { + httpTestingController.verify(); + }); + + describe('#getPolicyTypes', () => { + let expectedPolicytypes: PolicyType[]; + + beforeEach(() => { + policyService = TestBed.get(PolicyService); + httpTestingController = TestBed.get(HttpTestingController); + expectedPolicytypes = [ + { name: '1', 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: '2', 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\"]}' }, + ] as PolicyType[]; + }); + //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 + '/' + policyService.policyTypePath); + expect(req.request.method).toEqual('GET'); + + req.flush(expectedPolicytypes); //Return expectedEmps + }); + + //Policy Type Test Case 2: + it('should return no policy types', () => { + policyService.getPolicyTypes().subscribe( + policytypes => expect(policytypes.length).toEqual(0, 'should return empty array of Policy Types'), + fail + ); + + const req = httpTestingController.expectOne(basePath + '/' + policyService.policyTypePath); + req.flush([]); //Return empty data + }); + }); + 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'), + fail + ); + const req = httpTestingController.expectOne(basePath + '/' + policyService.policyPath + '?type=' + policyTypeId); + expect(req.request.method).toEqual('GET'); + req.flush(expectedPolicyInstances); //Return expectedEmps + }); + + //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 Isntances'), + fail + ); + const req = httpTestingController.expectOne(basePath + '/' + policyService.policyPath + '?type=' + policyTypeId); + req.flush([]); //Return empty data + }); }); }); diff --git a/webapp-frontend/src/app/services/policy/policy.service.ts b/webapp-frontend/src/app/services/policy/policy.service.ts index 93f08e7..f24747b 100644 --- a/webapp-frontend/src/app/services/policy/policy.service.ts +++ b/webapp-frontend/src/app/services/policy/policy.service.ts @@ -34,8 +34,8 @@ import { ControlpanelSuccessTransport } from '../../interfaces/controlpanel.type export class PolicyService { private basePath = 'api/policy'; - private policyTypePath = 'policytypes'; - private policyPath = 'policies'; + policyTypePath = 'policytypes'; + policyPath = 'policies'; private buildPath(...args: any[]) { let result = this.basePath; diff --git a/webapp-frontend/src/app/services/ui/ui.service.ts b/webapp-frontend/src/app/services/ui/ui.service.ts index 30f92d0..c475f1d 100644 --- a/webapp-frontend/src/app/services/ui/ui.service.ts +++ b/webapp-frontend/src/app/services/ui/ui.service.ts @@ -3,6 +3,7 @@ * O-RAN-SC * %% * Copyright (C) 2019 AT&T Intellectual Property + * Modifications Copyright (C) 2021 Nordix Foundation * %% * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,15 +19,16 @@ * ========================LICENSE_END=================================== */ import { Injectable } from '@angular/core'; -import { BehaviorSubject } from 'rxjs'; +import { BehaviorSubject, Observable } from 'rxjs'; @Injectable() export class UiService { darkModeState: BehaviorSubject; + expanded: Observable; constructor() { - // TODO: if the user is signed in get the default value from Firebase this.darkModeState = new BehaviorSubject(true); + this.expanded = new Observable(); } } -- 2.16.6