185a8ebbe3c4c1eb26a8745a919186baf43c1e16
[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, PolicyInstances, PolicyTypes } from '@interfaces/policy.types';
25
26 describe('PolicyService', () => {
27   let apiVersion2 = 'v2'
28   let basePath = '/a1-policy/';
29   let policyService: PolicyService;
30   let httpTestingController: HttpTestingController;
31   beforeEach(() => TestBed.configureTestingModule({
32     imports: [HttpClientTestingModule],
33     providers: [
34       PolicyService
35     ],
36   }));
37
38   afterEach(() => {
39     httpTestingController.verify();
40   });
41
42   describe('#getPolicyTypes', () => {
43     let expectedPolicytypes: PolicyTypes;
44     let emptyPolicyType: PolicyTypes;
45
46     beforeEach(() => {
47       policyService = TestBed.inject(PolicyService);
48       httpTestingController = TestBed.inject(HttpTestingController);
49       expectedPolicytypes = {
50         "policytype_ids": [
51           "",
52           "1"
53         ]
54       } as PolicyTypes;
55     });
56     //Policy Type Test Case 1:
57     it('should return all policy types', () => {
58       policyService.getPolicyTypes().subscribe(
59         policytypes => expect(policytypes).toEqual(expectedPolicytypes, 'should return expected PolicyTypes'),
60         fail
61       );
62       const req = httpTestingController.expectOne(basePath + apiVersion2 + '/' + policyService.policyTypesPath);
63       expect(req.request.method).toEqual('GET');
64
65       req.flush(expectedPolicytypes);
66     });
67
68     //Policy Type Test Case 2:
69     emptyPolicyType = {
70       "policytype_ids": [
71       ]
72     } as PolicyTypes;
73     it('should return no policy types', () => {
74       policyService.getPolicyTypes().subscribe(
75         policytypes => expect(policytypes).toEqual(emptyPolicyType, 'should return empty array of Policy Types'),
76         fail
77       );
78
79       const req = httpTestingController.expectOne(basePath + apiVersion2 + '/' + policyService.policyTypesPath);
80       req.flush(emptyPolicyType); //Return empty data
81     });
82   });
83   describe('#getPolicyInstances', () => {
84     let expectedPolicyInstances: PolicyInstances;
85     let emptyPolicyInstances: PolicyInstances;
86     let policyTypeId = '1';
87     beforeEach(() => {
88       policyService = TestBed.inject(PolicyService);
89       httpTestingController = TestBed.inject(HttpTestingController);
90       expectedPolicyInstances = {
91         "policy_ids": [
92           "2100",
93           "2000"
94         ]
95       } as PolicyInstances;
96     });
97     //Policy Instances Test Case 1:
98     it('should return all policy instances', () => {
99       policyService.getPolicyInstancesByType(policyTypeId).subscribe(
100         policyinstances => expect(policyinstances).toEqual(expectedPolicyInstances, 'should return expected Policy Instances'),
101         fail
102       );
103       const req = httpTestingController.expectOne(basePath + apiVersion2 + '/' + policyService.policyPath + '?' + 'policytype_id=' + policyTypeId);
104       expect(req.request.method).toEqual('GET');
105       req.flush(expectedPolicyInstances);
106     });
107
108     //Policy Instances Test Case 2:
109     emptyPolicyInstances = {
110       "policy_ids": [
111       ]
112     } as PolicyInstances;
113     it('should return no policy instances', () => {
114       policyService.getPolicyInstancesByType(policyTypeId).subscribe(
115         policyinstances => expect(policyinstances.policy_ids.length).toEqual(0, 'should return empty array of Policy Instances'),
116         fail
117       );
118       const req = httpTestingController.expectOne(basePath + apiVersion2 + '/' + policyService.policyPath + '?' + 'policytype_id=' + policyTypeId);
119       req.flush(emptyPolicyInstances); //Return empty data
120     });
121   });
122
123   describe('#getPolicyInstance', () => {
124     let expectedPolicyInstance: PolicyInstance;
125     let emptyPolicyInstances: PolicyInstances;
126     let policyId = "2000";
127     beforeEach(() => {
128       policyService = TestBed.inject(PolicyService);
129       httpTestingController = TestBed.inject(HttpTestingController);
130       expectedPolicyInstance = {
131         "policy_id": "2000",
132         "policytype_id": "1",
133         "ric_id": "ric1",
134         "policy_data": "",
135         "service_id": "service1",
136         "lastModified": ""
137       } as PolicyInstance;
138     });
139     //Policy Instances Test Case 1:
140     it('should return one policy instance', () => {
141       policyService.getPolicyInstance(policyId).subscribe(
142         policyinstance => expect(policyinstance).toEqual(expectedPolicyInstance, 'should return expected Policy Instances'),
143         fail
144       );
145       const req = httpTestingController.expectOne(basePath + apiVersion2 + '/' + policyService.policyPath + '/' + policyId);
146       expect(req.request.method).toEqual('GET');
147       req.flush(expectedPolicyInstance);
148     });
149   });
150 });