Fix PolicyControlComponent
[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   PolicyInstance,
29   PolicyInstances,
30   PolicyTypes,
31 } from "@interfaces/policy.types";
32
33 describe("PolicyService", () => {
34   let apiVersion2 = "v2";
35   let basePath = "/a1-policy/";
36   let policyService: PolicyService;
37   let httpTestingController: HttpTestingController;
38   beforeEach(() =>
39     TestBed.configureTestingModule({
40       imports: [HttpClientTestingModule],
41       providers: [PolicyService],
42     })
43   );
44
45   afterEach(() => {
46     httpTestingController.verify();
47   });
48
49   describe("#getPolicyTypes", () => {
50     let expectedPolicytypes: PolicyTypes;
51     let emptyPolicyType: PolicyTypes;
52
53     beforeEach(() => {
54       policyService = TestBed.inject(PolicyService);
55       httpTestingController = TestBed.inject(HttpTestingController);
56       expectedPolicytypes = {
57         policytype_ids: ["", "1"],
58       } as PolicyTypes;
59     });
60     //Policy Type Test Case 1:
61     it("should return all policy types", () => {
62       policyService
63         .getPolicyTypes()
64         .subscribe(
65           (policytypes) =>
66             expect(policytypes).toEqual(
67               expectedPolicytypes,
68               "should return expected PolicyTypes"
69             ),
70           fail
71         );
72       const req = httpTestingController.expectOne(
73         basePath + apiVersion2 + "/" + policyService.policyTypesPath
74       );
75       expect(req.request.method).toEqual("GET");
76
77       req.flush(expectedPolicytypes);
78     });
79
80     //Policy Type Test Case 2:
81     emptyPolicyType = {
82       policytype_ids: [],
83     } as PolicyTypes;
84     it("should return no policy types", () => {
85       policyService
86         .getPolicyTypes()
87         .subscribe(
88           (policytypes) =>
89             expect(policytypes).toEqual(
90               emptyPolicyType,
91               "should return empty array of Policy Types"
92             ),
93           fail
94         );
95
96       const req = httpTestingController.expectOne(
97         basePath + apiVersion2 + "/" + policyService.policyTypesPath
98       );
99       req.flush(emptyPolicyType); //Return empty data
100     });
101   });
102   describe("#getPolicyInstances", () => {
103     let expectedPolicyInstances: PolicyInstances;
104     let emptyPolicyInstances: PolicyInstances;
105     let policyTypeId = "1";
106     beforeEach(() => {
107       policyService = TestBed.inject(PolicyService);
108       httpTestingController = TestBed.inject(HttpTestingController);
109       expectedPolicyInstances = {
110         policy_ids: ["2100", "2000"],
111       } as PolicyInstances;
112     });
113     //Policy Instances Test Case 1:
114     it("should return all policy instances", () => {
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       const req = httpTestingController.expectOne(
126         basePath +
127           apiVersion2 +
128           "/" +
129           policyService.policyPath +
130           "?" +
131           "policytype_id=" +
132           policyTypeId
133       );
134       expect(req.request.method).toEqual("GET");
135       req.flush(expectedPolicyInstances);
136     });
137
138     //Policy Instances Test Case 2:
139     emptyPolicyInstances = {
140       policy_ids: [],
141     } as PolicyInstances;
142     it("should return no policy instances", () => {
143       policyService
144         .getPolicyInstancesByType(policyTypeId)
145         .subscribe(
146           (policyinstances) =>
147             expect(policyinstances.policy_ids.length).toEqual(
148               0,
149               "should return empty array of Policy Instances"
150             ),
151           fail
152         );
153       const req = httpTestingController.expectOne(
154         basePath +
155           apiVersion2 +
156           "/" +
157           policyService.policyPath +
158           "?" +
159           "policytype_id=" +
160           policyTypeId
161       );
162       req.flush(emptyPolicyInstances); //Return empty data
163     });
164   });
165
166   describe("#getPolicyInstance", () => {
167     let expectedPolicyInstance: PolicyInstance;
168     let emptyPolicyInstances: PolicyInstances;
169     let policyId = "2000";
170     beforeEach(() => {
171       policyService = TestBed.inject(PolicyService);
172       httpTestingController = TestBed.inject(HttpTestingController);
173       expectedPolicyInstance = {
174         policy_id: "2000",
175         policytype_id: "1",
176         ric_id: "ric1",
177         policy_data: "",
178         service_id: "service1",
179         lastModified: "",
180       } as PolicyInstance;
181     });
182     //Policy Instances Test Case 1:
183     it("should return one policy instance", () => {
184       policyService
185         .getPolicyInstance(policyId)
186         .subscribe(
187           (policyinstance) =>
188             expect(policyinstance).toEqual(
189               expectedPolicyInstance,
190               "should return expected Policy Instances"
191             ),
192           fail
193         );
194       const req = httpTestingController.expectOne(
195         basePath + apiVersion2 + "/" + policyService.policyPath + "/" + policyId
196       );
197       expect(req.request.method).toEqual("GET");
198       req.flush(expectedPolicyInstance);
199     });
200   });
201 });