2 * ========================LICENSE_START=================================
5 * Copyright (C) 2021 Nordix Foundation
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ========================LICENSE_END===================================
20 import { TestBed } from '@angular/core/testing';
22 import { PolicyService } from './policy.service';
23 import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing'
24 import { PolicyInstance, PolicyType } from '../../interfaces/policy.types';
26 describe('PolicyService', () => {
27 let basePath = 'api/policy';
28 let policyService: PolicyService;
29 let httpTestingController: HttpTestingController;
30 beforeEach(() => TestBed.configureTestingModule({
31 imports: [HttpClientTestingModule],
38 httpTestingController.verify();
41 describe('#getPolicyTypes', () => {
42 let expectedPolicytypes: PolicyType[];
45 policyService = TestBed.get(PolicyService);
46 httpTestingController = TestBed.get(HttpTestingController);
47 expectedPolicytypes = [
48 { 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\"]}' },
49 { 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\"]}' },
52 //Policy Type Test Case 1:
53 it('should return all policy types', () => {
54 policyService.getPolicyTypes().subscribe(
55 policytypes => expect(policytypes).toEqual(expectedPolicytypes, 'should return expected PolicyTypes'),
59 const req = httpTestingController.expectOne(basePath + '/' + policyService.policyTypePath);
60 expect(req.request.method).toEqual('GET');
62 req.flush(expectedPolicytypes); //Return expectedEmps
65 //Policy Type Test Case 2:
66 it('should return no policy types', () => {
67 policyService.getPolicyTypes().subscribe(
68 policytypes => expect(policytypes.length).toEqual(0, 'should return empty array of Policy Types'),
72 const req = httpTestingController.expectOne(basePath + '/' + policyService.policyTypePath);
73 req.flush([]); //Return empty data
76 describe('#getPolicyInstance', () => {
77 let expectedPolicyInstances: PolicyInstance[];
78 let policyTypeId: string;
80 policyService = TestBed.get(PolicyService);
81 httpTestingController = TestBed.get(HttpTestingController);
82 expectedPolicyInstances = [
83 { id: '2000', json: '{"scope": {"ueId": "ue3100","qosId": "qos3100"},"qosObjectives": {"priorityLevel": 3100}}', service: 'service1', lastModified: '2020-12-08T21:12:43.719084Z' }
84 ] as PolicyInstance[];
87 //Policy Instances Test Case 1:
88 it('should return all policy instances', () => {
89 policyService.getPolicyInstances(policyTypeId).subscribe(
90 policyinstances => expect(policyinstances).toEqual(expectedPolicyInstances, 'should return expected Policy Instances'),
93 const req = httpTestingController.expectOne(basePath + '/' + policyService.policyPath + '?type=' + policyTypeId);
94 expect(req.request.method).toEqual('GET');
95 req.flush(expectedPolicyInstances); //Return expectedEmps
98 //Policy Instances Test Case 2:
99 it('should return no policy instances', () => {
100 policyService.getPolicyInstances(policyTypeId).subscribe(
101 policyinstances => expect(policyinstances.length).toEqual(0, 'should return empty array of Policy Isntances'),
104 const req = httpTestingController.expectOne(basePath + '/' + policyService.policyPath + '?type=' + policyTypeId);
105 req.flush([]); //Return empty data