Provide test coverage of PolicyService
[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 {
24   HttpClientTestingModule,
25   HttpTestingController,
26 } from "@angular/common/http/testing";
27 import {
28   CreatePolicyInstance,
29   PolicyInstance,
30   PolicyInstances,
31   PolicyStatus,
32   PolicyType,
33   PolicyTypes,
34 } from "@interfaces/policy.types";
35 import { Ric, Rics } from "@app/interfaces/ric";
36
37 describe("PolicyService", () => {
38   let apiVersion2 = "v2";
39   let basePath = "/a1-policy/";
40   let policyService: PolicyService;
41   let httpTestingController: HttpTestingController;
42   beforeEach(() =>
43     TestBed.configureTestingModule({
44       imports: [HttpClientTestingModule],
45       providers: [PolicyService],
46     })
47   );
48
49   beforeEach(() => {
50     policyService = TestBed.inject(PolicyService);
51     httpTestingController = TestBed.inject(HttpTestingController);
52   });
53
54   afterEach(() => {
55     httpTestingController.verify();
56   });
57
58   it("#getPolicyTypes should return policy types", () => {
59     const expectedPolicytypes = {
60       policytype_ids: ["", "1"],
61     } as PolicyTypes;
62
63     policyService
64       .getPolicyTypes()
65       .subscribe(
66         (policytypes) =>
67           expect(policytypes).toEqual(
68             expectedPolicytypes,
69             "should return expected PolicyTypes"
70           ),
71         fail
72       );
73     const req = httpTestingController.expectOne(
74       basePath + apiVersion2 + "/" + policyService.policyTypesPath
75     );
76     expect(req.request.method).toEqual("GET");
77
78     req.flush(expectedPolicytypes);
79   });
80
81   it("#getPolicyType", () => {
82     const policyTypeId = "1";
83     const expectedPolicyType = { policy_schema: "schema" } as PolicyType;
84
85     policyService
86       .getPolicyType(policyTypeId)
87       .subscribe(
88         (policyType) =>
89           expect(policyType).toEqual(
90             expectedPolicyType,
91             "should return expected policy type"
92           ),
93         fail
94       );
95
96     const req = httpTestingController.expectOne(
97       basePath +
98         apiVersion2 +
99         "/" +
100         policyService.policyTypesPath +
101         "/" +
102         policyTypeId
103     );
104     expect(req.request.method).toEqual("GET");
105
106     req.flush(expectedPolicyType);
107   });
108
109   it("#getPolicyInstancesByType should return policy instances", () => {
110     const policyTypeId = "1";
111     const expectedPolicyInstances = {
112       policy_ids: ["2100", "2000"],
113     } as PolicyInstances;
114
115     policyService
116       .getPolicyInstancesByType(policyTypeId)
117       .subscribe(
118         (policyinstances) =>
119           expect(policyinstances).toEqual(
120             expectedPolicyInstances,
121             "should return expected Policy Instances"
122           ),
123         fail
124       );
125
126     const req = httpTestingController.expectOne(
127       basePath +
128         apiVersion2 +
129         "/" +
130         policyService.policyPath +
131         "?" +
132         "policytype_id=" +
133         policyTypeId
134     );
135     expect(req.request.method).toEqual("GET");
136
137     req.flush(expectedPolicyInstances);
138   });
139
140   it("#getPolicyInstance should return one policy instance", () => {
141     const policyId = "2000";
142     const expectedPolicyInstance = {
143       policy_id: "2000",
144       policytype_id: "1",
145       ric_id: "ric1",
146       policy_data: "",
147       service_id: "service1",
148       lastModified: "",
149     } as PolicyInstance;
150
151     policyService
152       .getPolicyInstance(policyId)
153       .subscribe(
154         (policyinstance) =>
155           expect(policyinstance).toEqual(
156             expectedPolicyInstance,
157             "should return expected Policy Instances"
158           ),
159         fail
160       );
161
162     const req = httpTestingController.expectOne(
163       basePath + apiVersion2 + "/" + policyService.policyPath + "/" + policyId
164     );
165     expect(req.request.method).toEqual("GET");
166
167     req.flush(expectedPolicyInstance);
168   });
169
170   it("#getPolicyStatus should return policy status", () => {
171     const policyId = "2000";
172     const expectedPolicyStatus = {
173       last_modified: "modified",
174     } as PolicyStatus;
175
176     policyService
177       .getPolicyStatus(policyId)
178       .subscribe(
179         (policyinstance) =>
180           expect(policyinstance).toEqual(
181             expectedPolicyStatus,
182             "should return expected Policy status"
183           ),
184         fail
185       );
186
187     const req = httpTestingController.expectOne(
188       basePath +
189         apiVersion2 +
190         "/" +
191         policyService.policyPath +
192         "/" +
193         policyId +
194         "/status"
195     );
196     expect(req.request.method).toEqual("GET");
197
198     req.flush(expectedPolicyStatus);
199   });
200
201   it("#putPolicy should return ok response", () => {
202     const createPolicyInstance = { policy_id: "2000" } as CreatePolicyInstance;
203
204     policyService
205       .putPolicy(createPolicyInstance)
206       .subscribe(
207         (response) =>
208           expect(response.status).toEqual(
209             200,
210             "should return expected response"
211           ),
212         fail
213       );
214
215     const req = httpTestingController.expectOne(
216       basePath + apiVersion2 + "/" + policyService.policyPath
217     );
218     expect(req.request.method).toEqual("PUT");
219
220     req.flush(200);
221   });
222
223   it("#deletePolicy should return ok response", () => {
224     const policyId = "2000";
225
226     policyService
227       .deletePolicy(policyId)
228       .subscribe(
229         (response) =>
230           expect(response.status).toEqual(
231             200,
232             "should return expected response"
233           ),
234         fail
235       );
236
237     const req = httpTestingController.expectOne(
238       basePath + apiVersion2 + "/" + policyService.policyPath + "/2000"
239     );
240     expect(req.request.method).toEqual("DELETE");
241
242     req.flush(200);
243   });
244
245   it("#getRics should return rics", () => {
246     const policyTypeId = "2000";
247     const expectedRic = { ric_id: "1" } as Ric;
248     const expectedRics = {
249       rics: [expectedRic],
250     } as Rics;
251
252     policyService
253       .getRics(policyTypeId)
254       .subscribe(
255         (rics) =>
256           expect(rics).toEqual(
257             expectedRics,
258             "should return expected Rics"
259           ),
260         fail
261       );
262
263     const req = httpTestingController.expectOne(
264       basePath +
265         apiVersion2 +
266         "/rics?policytype_id=2000"
267     );
268     expect(req.request.method).toEqual("GET");
269
270     req.flush(expectedRics);
271   });
272 });