15f849cf9408b825638a40f378bab84a47737978
[portal/nonrtric-controlpanel.git] / webapp-frontend / src / app / services / policy / policy.service.spec.ts
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2021 Nordix Foundation
6  * %%
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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===================================
19  */
20 import { TestBed } from '@angular/core/testing';
21
22 import { PolicyService } from './policy.service';
23 import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing'
24 import { PolicyInstance, PolicyType } from '../../interfaces/policy.types';
25
26 describe('PolicyService', () => {
27   let basePath = 'api/policy';
28   let policyService: PolicyService;
29   let httpTestingController: HttpTestingController;
30   beforeEach(() => TestBed.configureTestingModule({
31     imports: [HttpClientTestingModule],
32     providers: [
33       PolicyService
34     ],
35   }));
36
37   afterEach(() => {
38     httpTestingController.verify();
39   });
40
41   describe('#getPolicyTypes', () => {
42     let expectedPolicytypes: PolicyType[];
43
44     beforeEach(() => {
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\"]}' },
50       ] as PolicyType[];
51     });
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'),
56         fail
57       );
58
59       const req = httpTestingController.expectOne(basePath + '/' + policyService.policyTypePath);
60       expect(req.request.method).toEqual('GET');
61
62       req.flush(expectedPolicytypes); //Return expectedEmps
63     });
64
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'),
69         fail
70       );
71
72       const req = httpTestingController.expectOne(basePath + '/' + policyService.policyTypePath);
73       req.flush([]); //Return empty data
74     });
75   });
76   describe('#getPolicyInstance', () => {
77     let expectedPolicyInstances: PolicyInstance[];
78     let policyTypeId: string;
79     beforeEach(() => {
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[];
85       policyTypeId = "1";
86     });
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'),
91         fail
92       );
93       const req = httpTestingController.expectOne(basePath + '/' + policyService.policyPath + '?type=' + policyTypeId);
94       expect(req.request.method).toEqual('GET');
95       req.flush(expectedPolicyInstances); //Return expectedEmps
96     });
97
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'),
102         fail
103       );
104       const req = httpTestingController.expectOne(basePath + '/' + policyService.policyPath + '?type=' + policyTypeId);
105       req.flush([]); //Return empty data
106     });
107   });
108 });