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';
23 import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing'
24 import { PolicyInstance, PolicyInstances, PolicyTypes } from '../../interfaces/policy.types';
26 describe('PolicyService', () => {
27 let apiVersion2 = 'v2'
29 let policyService: PolicyService;
30 let httpTestingController: HttpTestingController;
31 beforeEach(() => TestBed.configureTestingModule({
32 imports: [HttpClientTestingModule],
39 httpTestingController.verify();
42 describe('#getPolicyTypes', () => {
43 let expectedPolicytypes: PolicyTypes;
44 let emptyPolicyType: PolicyTypes;
47 policyService = TestBed.get(PolicyService);
48 httpTestingController = TestBed.get(HttpTestingController);
49 expectedPolicytypes = {
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'),
63 const req = httpTestingController.expectOne(basePath + apiVersion2 + '/' + policyService.policyTypesPath);
64 expect(req.request.method).toEqual('GET');
66 req.flush(expectedPolicytypes);
69 //Policy Type Test Case 2:
74 it('should return no policy types', () => {
75 policyService.getPolicyTypes().subscribe(
76 policytypes => expect(policytypes).toEqual(emptyPolicyType, 'should return empty array of Policy Types'),
80 const req = httpTestingController.expectOne(basePath + apiVersion2 + '/' + policyService.policyTypesPath);
81 req.flush(emptyPolicyType); //Return empty data
84 describe('#getPolicyInstances', () => {
85 let expectedPolicyInstances: PolicyInstances;
86 let emptyPolicyInstances: PolicyInstances;
87 let policyTypeId = '1';
89 policyService = TestBed.get(PolicyService);
90 httpTestingController = TestBed.get(HttpTestingController);
91 expectedPolicyInstances = {
98 //Policy Instances Test Case 1:
99 it('should return all policy instances', () => {
100 policyService.getPolicyInstancesByType(policyTypeId).subscribe(
101 policyinstances => expect(policyinstances).toEqual(expectedPolicyInstances, 'should return expected Policy Instances'),
104 const req = httpTestingController.expectOne(basePath + apiVersion2 + '/' + policyService.policyPath + '?' + 'policytype_id=' + policyTypeId);
105 expect(req.request.method).toEqual('GET');
106 req.flush(expectedPolicyInstances);
109 //Policy Instances Test Case 2:
110 emptyPolicyInstances = {
113 } as PolicyInstances;
114 it('should return no policy instances', () => {
115 policyService.getPolicyInstancesByType(policyTypeId).subscribe(
116 policyinstances => expect(policyinstances.policy_ids.length).toEqual(0, 'should return empty array of Policy Instances'),
119 const req = httpTestingController.expectOne(basePath + apiVersion2 + '/' + policyService.policyPath + '?' + 'policytype_id=' + policyTypeId);
120 req.flush(emptyPolicyInstances); //Return empty data
124 describe('#getPolicyInstance', () => {
125 let expectedPolicyInstance: PolicyInstance;
126 let emptyPolicyInstances: PolicyInstances;
127 let policyId = "2000";
129 policyService = TestBed.get(PolicyService);
130 httpTestingController = TestBed.get(HttpTestingController);
131 expectedPolicyInstance = {
133 "policytype_id": "1",
136 "service_id": "service1",
140 //Policy Instances Test Case 1:
141 it('should return one policy instance', () => {
142 policyService.getPolicyInstance(policyId).subscribe(
143 policyinstance => expect(policyinstance).toEqual(expectedPolicyInstance, 'should return expected Policy Instances'),
146 const req = httpTestingController.expectOne(basePath + apiVersion2 + '/' + policyService.policyPath + '/' + policyId);
147 expect(req.request.method).toEqual('GET');
148 req.flush(expectedPolicyInstance);