5b833686258d7a47b3e3a208e4abee324595b46d
[portal/nonrtric-controlpanel.git] / webapp-frontend / src / app / policy-control / policy-type.datasource.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
21 import { TestBed } from '@angular/core/testing';
22 import { BehaviorSubject, of } from 'rxjs';
23 import { NotificationService } from '../services/ui/notification.service';
24 import { ToastrModule } from 'ngx-toastr';
25 import { PolicyTypeDataSource } from './policy-type.datasource';
26 import { PolicyService } from '../services/policy/policy.service';
27 import { PolicyTypeSchema } from '../interfaces/policy.types';
28
29 describe('PolicyTypeDataSource', () => {
30   let policyTypeDataSource: PolicyTypeDataSource;
31   let policyServiceSpy: any;
32
33   let policySchema = {
34     policy_schema: {
35       "$schema": "http://json-schema.org/draft-07/schema#",
36       "description": "Type 1 policy type",
37       "additionalProperties": false,
38       "title": "1",
39       "type": "object"
40     }
41   };
42
43   beforeEach(() => {
44     policyServiceSpy = jasmine.createSpyObj('PolicyService', ['getPolicyTypes', 'getPolicyType']);
45
46     policyServiceSpy.getPolicyTypes.and.returnValue(of({ policytype_ids: ['1', '2'] }));
47     policyServiceSpy.getPolicyType.and.returnValue(of(policySchema));
48     TestBed.configureTestingModule({
49       imports: [ToastrModule.forRoot()],
50       providers: [
51         { provide: PolicyService, useValue: policyServiceSpy },
52         NotificationService
53       ]
54     });
55   });
56
57   describe('#getPolicyTypes', () => {
58     let expectedPolicyTypeValue: PolicyTypeSchema[];
59     beforeEach(() => {
60       expectedPolicyTypeValue = [
61         {
62           'id': '1',
63           'name': '1',
64           'schemaObject': policySchema.policy_schema
65         },
66         {
67           'id': '2',
68           'name': '1',
69           'schemaObject': policySchema.policy_schema
70         }
71       ];
72     });
73
74     it('should create', () => {
75       policyTypeDataSource = TestBed.get(PolicyTypeDataSource);
76       expect(policyTypeDataSource).toBeTruthy();
77     });
78
79     it('should return all policy type with Schema', () => {
80       policyTypeDataSource.getPolicyTypes();
81       const jobsSubject: BehaviorSubject<PolicyTypeSchema[]> = policyTypeDataSource.policyTypeSubject;
82       const value = jobsSubject.getValue();
83       expect(value).toEqual(expectedPolicyTypeValue);
84     });
85   });
86 });