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";
24 HttpClientTestingModule,
25 HttpTestingController,
26 } from "@angular/common/http/testing";
31 } from "@interfaces/policy.types";
33 describe("PolicyService", () => {
34 let apiVersion2 = "v2";
35 let basePath = "/a1-policy/";
36 let policyService: PolicyService;
37 let httpTestingController: HttpTestingController;
39 TestBed.configureTestingModule({
40 imports: [HttpClientTestingModule],
41 providers: [PolicyService],
46 httpTestingController.verify();
49 describe("#getPolicyTypes", () => {
50 let expectedPolicytypes: PolicyTypes;
51 let emptyPolicyType: PolicyTypes;
54 policyService = TestBed.inject(PolicyService);
55 httpTestingController = TestBed.inject(HttpTestingController);
56 expectedPolicytypes = {
57 policytype_ids: ["", "1"],
60 //Policy Type Test Case 1:
61 it("should return all policy types", () => {
66 expect(policytypes).toEqual(
68 "should return expected PolicyTypes"
72 const req = httpTestingController.expectOne(
73 basePath + apiVersion2 + "/" + policyService.policyTypesPath
75 expect(req.request.method).toEqual("GET");
77 req.flush(expectedPolicytypes);
80 //Policy Type Test Case 2:
84 it("should return no policy types", () => {
89 expect(policytypes).toEqual(
91 "should return empty array of Policy Types"
96 const req = httpTestingController.expectOne(
97 basePath + apiVersion2 + "/" + policyService.policyTypesPath
99 req.flush(emptyPolicyType); //Return empty data
102 describe("#getPolicyInstances", () => {
103 let expectedPolicyInstances: PolicyInstances;
104 let emptyPolicyInstances: PolicyInstances;
105 let policyTypeId = "1";
107 policyService = TestBed.inject(PolicyService);
108 httpTestingController = TestBed.inject(HttpTestingController);
109 expectedPolicyInstances = {
110 policy_ids: ["2100", "2000"],
111 } as PolicyInstances;
113 //Policy Instances Test Case 1:
114 it("should return all policy instances", () => {
116 .getPolicyInstancesByType(policyTypeId)
119 expect(policyinstances).toEqual(
120 expectedPolicyInstances,
121 "should return expected Policy Instances"
125 const req = httpTestingController.expectOne(
129 policyService.policyPath +
134 expect(req.request.method).toEqual("GET");
135 req.flush(expectedPolicyInstances);
138 //Policy Instances Test Case 2:
139 emptyPolicyInstances = {
141 } as PolicyInstances;
142 it("should return no policy instances", () => {
144 .getPolicyInstancesByType(policyTypeId)
147 expect(policyinstances.policy_ids.length).toEqual(
149 "should return empty array of Policy Instances"
153 const req = httpTestingController.expectOne(
157 policyService.policyPath +
162 req.flush(emptyPolicyInstances); //Return empty data
166 describe("#getPolicyInstance", () => {
167 let expectedPolicyInstance: PolicyInstance;
168 let emptyPolicyInstances: PolicyInstances;
169 let policyId = "2000";
171 policyService = TestBed.inject(PolicyService);
172 httpTestingController = TestBed.inject(HttpTestingController);
173 expectedPolicyInstance = {
178 service_id: "service1",
182 //Policy Instances Test Case 1:
183 it("should return one policy instance", () => {
185 .getPolicyInstance(policyId)
188 expect(policyinstance).toEqual(
189 expectedPolicyInstance,
190 "should return expected Policy Instances"
194 const req = httpTestingController.expectOne(
195 basePath + apiVersion2 + "/" + policyService.policyPath + "/" + policyId
197 expect(req.request.method).toEqual("GET");
198 req.flush(expectedPolicyInstance);