80f5e78293180c41eff4a5dddacf6f5a75f1ea54
[portal/nonrtric-controlpanel.git] / webapp-frontend / src / app / policy / no-type-policy-instance-dialog / no-type-policy-instance-dialog.component.spec.ts
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2019 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 { BrowserAnimationsModule } from "@angular/platform-browser/animations";
22 import { ComponentFixture, TestBed } from "@angular/core/testing";
23 import { HarnessLoader } from "@angular/cdk/testing";
24 import { MatButtonModule } from '@angular/material/button';
25 import { MatButtonHarness } from '@angular/material/button/testing';
26 import { MatDialogModule, MatDialogRef, MAT_DIALOG_DATA } from '@angular/material/dialog';
27 import { MatSelectModule } from '@angular/material/select';
28 import { MatSelectHarness } from '@angular/material/select/testing';
29 import { MatInputModule } from '@angular/material/input';
30 import { MatInputHarness } from '@angular/material/input/testing';
31 import { of } from "rxjs/observable/of";
32 import { ReactiveFormsModule } from "@angular/forms";
33 import { TestbedHarnessEnvironment } from "@angular/cdk/testing/testbed";
34 import { ToastrModule } from "ngx-toastr";
35
36 import { PolicyService } from "../../services/policy/policy.service";
37 import { ErrorDialogService } from "../../services/ui/error-dialog.service";
38 import { UiService } from "../../services/ui/ui.service";
39 import { NoTypePolicyInstanceDialogComponent } from "./no-type-policy-instance-dialog.component";
40 import { Ric } from "../../interfaces/ric";
41
42 describe('NoTypePolicyInstanceDialogComponent', () => {
43   let component: NoTypePolicyInstanceDialogComponent;
44   let fixture: ComponentFixture<NoTypePolicyInstanceDialogComponent>;
45   let loader: HarnessLoader;
46   let policyServiceSpy: jasmine.SpyObj<PolicyService>;
47   let errDialogServiceSpy: jasmine.SpyObj<ErrorDialogService>;
48   const ric1: Ric = { ric_id: 'ric1', managed_element_ids: [ 'me1' ], policytype_ids: [ 'type1' ], state: ''};
49   const ric2: Ric = { ric_id: 'ric2', managed_element_ids: [ 'me1' ], policytype_ids: [ 'type1' ], state: ''};
50
51   beforeEach(async () => {
52     policyServiceSpy = jasmine.createSpyObj('PolicyService', [ 'putPolicy', 'getRics' ]);
53     errDialogServiceSpy = jasmine.createSpyObj('ErrorDialogService', [ 'displayError' ]);
54
55     policyServiceSpy.getRics.and.returnValue(of({ rics: [ ric1, ric2 ] }));
56
57     TestBed.configureTestingModule({
58       imports: [
59         BrowserAnimationsModule,
60         MatButtonModule,
61         MatDialogModule,
62         MatInputModule,
63         MatSelectModule,
64         ReactiveFormsModule,
65         ToastrModule.forRoot()
66       ],
67       declarations: [
68         NoTypePolicyInstanceDialogComponent
69       ],
70       providers: [
71         { provide: MatDialogRef, useValue: component },
72         { provide: PolicyService, useValue: policyServiceSpy },
73         { provide: ErrorDialogService, useValue: errDialogServiceSpy },
74         { provide: MAT_DIALOG_DATA, useValue: true },
75         UiService
76       ]
77     });
78   });
79
80   describe('content when creating policy', () => {
81     beforeEach(async () => {
82       ({ fixture, component, loader } = compileAndGetComponents(fixture, component, loader));
83     });
84
85     it('should contain oran logo and create title and no instance info', async () => {
86       let ele = fixture.debugElement.nativeElement.querySelector('img');
87       expect(ele.src).toContain('assets/oran-logo.png');
88
89       ele = fixture.debugElement.nativeElement.querySelector('text');
90       expect(ele.childNodes[0].childNodes[0].textContent).toEqual('Create new policy instance of < No type >');
91
92       ele = fixture.debugElement.nativeElement.querySelector('#instanceInfo');
93       expect(ele).toBeFalsy();
94     });
95
96     it('should contain enabled Target selection with no ric selected and json should be empty', async () => {
97       let ricSelector: MatSelectHarness = await loader.getHarness(MatSelectHarness.with({selector: '#ricSelector'}));
98
99       expect(await ricSelector.isEmpty()).toBeTruthy();
100       expect(await ricSelector.isDisabled()).toBeFalsy();
101       await ricSelector.open();
102       const count = (await ricSelector.getOptions()).length;
103       expect(count).toEqual(2);
104
105       let jsonTextArea: MatInputHarness = await loader.getHarness(MatInputHarness.with({selector: '#policyJsonTextArea'}));
106       expect(await jsonTextArea.isDisabled()).toBeFalsy();
107       const actualJson: string = await jsonTextArea.getValue();
108       expect(actualJson).toEqual('');
109     });
110
111     it('should contain disabled Format and Submit buttons and enabled Close button', async () => {
112       component.ngOnInit();
113
114       let formatButton: MatButtonHarness = await loader.getHarness(MatButtonHarness.with({selector: '#formatButton'}));
115       expect(await formatButton.isDisabled()).toBeTruthy();
116       expect(await formatButton.getText()).toEqual('Format JSON');
117
118       let closeButton: MatButtonHarness = await loader.getHarness(MatButtonHarness.with({selector: '#closeButton'}));
119       expect(await closeButton.isDisabled()).toBeFalsy();
120       expect(await closeButton.getText()).toEqual('Close');
121
122       let submitButton: MatButtonHarness = await loader.getHarness(MatButtonHarness.with({selector: '#submitButton'}));
123       expect(await submitButton.isDisabled()).toBeTruthy();
124       expect(await submitButton.getText()).toEqual('Submit');
125     });
126   });
127
128   describe('content when editing policy', () => {
129     beforeEach(async () => {
130       const policyData = {
131         createSchema: "{}",
132         instanceId: "instanceId",
133         instanceJson: '{"qosObjectives": {"priorityLevel": 3100}}',
134         name: "name",
135         ric: "ric1"
136     };
137       TestBed.overrideProvider(MAT_DIALOG_DATA, {useValue: policyData }); // Should be provided with a policy
138       ({ fixture, component, loader } = compileAndGetComponents(fixture, component, loader));
139     });
140
141     it('should contain oran logo and instance info', async () => {
142         let ele = fixture.debugElement.nativeElement.querySelector('img');
143         expect(ele.src).toContain('assets/oran-logo.png');
144
145         ele = fixture.debugElement.nativeElement.querySelector('text');
146         expect(ele.childNodes[0].childNodes[0]).toBeFalsy(); // No create title
147
148         ele = fixture.debugElement.nativeElement.querySelector('#instanceInfo');
149         expect(ele).toBeTruthy();
150         expect(ele.innerText).toEqual('[ric1] Instance ID: instanceId');
151     });
152
153     it('should contain json and no Target selection', async () => {
154         let ele = fixture.debugElement.nativeElement.querySelector('#ricSelector');
155         expect(ele).toBeFalsy();
156
157         let jsonTextArea: MatInputHarness = await loader.getHarness(MatInputHarness.with({selector: '#policyJsonTextArea'}));
158         expect(await jsonTextArea.isDisabled()).toBeFalsy();
159         const actualJson: string = await jsonTextArea.getValue();
160         expect(actualJson).toContain('qosObjectives');
161     });
162
163     it('should contain enabled Format, Submit and Close buttons', async () => {
164       let formatButton: MatButtonHarness = await loader.getHarness(MatButtonHarness.with({selector: '#formatButton'}));
165       expect(await formatButton.isDisabled()).toBeFalsy();
166       expect(await formatButton.getText()).toEqual('Format JSON');
167
168       let closeButton: MatButtonHarness = await loader.getHarness(MatButtonHarness.with({selector: '#closeButton'}));
169       expect(await closeButton.isDisabled()).toBeFalsy();
170       expect(await closeButton.getText()).toEqual('Close');
171
172       let submitButton: MatButtonHarness = await loader.getHarness(MatButtonHarness.with({selector: '#submitButton'}));
173       expect(await submitButton.isDisabled()).toBeFalsy();
174       expect(await submitButton.getText()).toEqual('Submit');
175     });
176
177   });
178 });
179
180 function compileAndGetComponents(fixture: ComponentFixture<NoTypePolicyInstanceDialogComponent>, component: NoTypePolicyInstanceDialogComponent, loader: HarnessLoader) {
181   TestBed.compileComponents();
182
183   fixture = TestBed.createComponent(NoTypePolicyInstanceDialogComponent);
184   component = fixture.componentInstance;
185   fixture.detectChanges();
186   loader = TestbedHarnessEnvironment.loader(fixture);
187   return { fixture, component, loader };
188 }