Add coverage of ei coordinator component
[portal/nonrtric-controlpanel.git] / webapp-frontend / src / app / ei-coordinator / ei-coordinator.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 import { async, ComponentFixture, TestBed } from '@angular/core/testing';
21 import { BrowserAnimationsModule } from '@angular/platform-browser/animations'
22 import { CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
23 import { FormBuilder, ReactiveFormsModule } from '@angular/forms';
24 import {HarnessLoader} from '@angular/cdk/testing';
25 import { MatIconModule } from '@angular/material/icon';
26 import { MatInputHarness } from '@angular/material/input/testing'
27 import { MatTableModule } from '@angular/material/table';
28 import { MatTableHarness } from '@angular/material/table/testing';
29 import {TestbedHarnessEnvironment} from '@angular/cdk/testing/testbed';
30
31 import { EICoordinatorComponent } from './ei-coordinator.component';
32 import { EIJobDataSource } from './ei-job.datasource';
33 import { EIProducerDataSource } from './ei-producer.datasource';
34 import { UiService } from '../services/ui/ui.service';
35 import { EIJob, EIProducer } from '../interfaces/ei.types';
36
37 describe('EICoordinatorComponent', () => {
38   let component: EICoordinatorComponent;
39   let fixture: ComponentFixture<EICoordinatorComponent>;
40   let loader: HarnessLoader;
41   let producerDataSourceSpy: jasmine.SpyObj<EIProducerDataSource>;
42   let jobDataSourceSpy: jasmine.SpyObj<EIJobDataSource>;
43
44   const producer1 = {
45     ei_producer_id: 'producer1',
46     ei_producer_types: [ 'type1', 'type2' ],
47     status: 'ENABLED'
48   } as EIProducer;
49   const producer2 = {
50       ei_producer_id: 'producer2',
51       ei_producer_types: [ 'type2', 'type3' ],
52       status: 'DISABLED'
53   } as EIProducer;
54
55   const job1 = {
56     ei_job_identity: 'job1',
57     ei_type_identity: 'type1',
58     owner: 'owner1',
59     target_uri: 'http://one'
60   } as EIJob;
61   const job2 = {
62     ei_job_identity: 'job2',
63     ei_type_identity: 'type2',
64     owner: 'owner2',
65     target_uri: 'http://two'
66   } as EIJob;
67
68   beforeEach(async () => {
69     producerDataSourceSpy = jasmine.createSpyObj('EIProducerDataSource', [ 'loadProducers', 'eiProducers' ]);
70     jobDataSourceSpy = jasmine.createSpyObj('EIJobDataSource', [ 'loadJobs', 'eiJobs' ]);
71
72     await TestBed.configureTestingModule({
73       imports: [
74         MatIconModule,
75         MatTableModule,
76         BrowserAnimationsModule,
77         ReactiveFormsModule
78       ],
79       schemas: [
80         CUSTOM_ELEMENTS_SCHEMA
81       ],
82       declarations: [
83         EICoordinatorComponent
84       ],
85       providers: [
86         { provide: EIJobDataSource, useValue: jobDataSourceSpy },
87         { provide: EIProducerDataSource, useValue: producerDataSourceSpy },
88         UiService,
89         FormBuilder,
90       ]
91     })
92     .compileComponents();
93
94     fixture = TestBed.createComponent(EICoordinatorComponent);
95     component = fixture.componentInstance;
96     fixture.detectChanges();
97     loader = TestbedHarnessEnvironment.loader(fixture);
98   });
99
100   it('should create', () => {
101     expect(component).toBeTruthy();
102   });
103
104   describe('#content', () => {
105     it('should contain refresh button with coorect icon', () => {
106       const button = fixture.debugElement.nativeElement.querySelector('#refreshButton');
107       expect(button).toBeTruthy();
108       expect(button.innerHTML).toContain('refresh');
109     });
110
111     it('should contain producers table with correct columns', async () => {
112       let producersTable = await loader.getHarness(MatTableHarness.with({selector: '#producersTable'}));
113       let headerRow = (await producersTable.getHeaderRows())[0];
114       let headers = await headerRow.getCellTextByColumnName();
115
116       expect(headers).toEqual({id: 'Producer ID', types: 'Producer types', status: 'Producer status'});
117     });
118
119     it('should contain jobs table with correct columns', async () => {
120       let producersTable = await loader.getHarness(MatTableHarness.with({selector: '#jobsTable'}));
121       let headerRow = (await producersTable.getHeaderRows())[0];
122       let headers = await headerRow.getCellTextByColumnName();
123
124       expect(headers).toEqual({id: 'Job ID', typeId: 'Type ID', owner: 'Owner', targetUri: 'Target URI'});
125     });
126
127     it('should set correct dark mode from UIService', () => {
128       const uiService: UiService = TestBed.inject(UiService);
129       expect(component.darkMode).toBeTruthy();
130
131       uiService.darkModeState.next(false);
132       fixture.detectChanges();
133       expect(component.darkMode).toBeFalsy();
134
135     });
136   });
137
138   describe('#producersTable', () => {
139     const expectedProducer1Row = { id: 'producer1', types: 'type1,type2', status: 'ENABLED' };
140     beforeEach(() => {
141       const producers: EIProducer[] =[ producer1, producer2 ];
142       producerDataSourceSpy.eiProducers.and.returnValue(producers);
143     });
144
145     it('should contain data after initialization', async () => {
146       component.ngOnInit();
147       const expectedProducerRows = [
148         expectedProducer1Row,
149         {id: 'producer2', types: 'type2,type3', status: 'DISABLED'}
150       ];
151       let producersTable = await loader.getHarness(MatTableHarness.with({selector: '#producersTable'}));
152       let producerRows = await producersTable.getRows();
153       expect(producerRows.length).toEqual(2);
154       producerRows.forEach(row => {
155         row.getCellTextByColumnName().then(values => {
156           expect(expectedProducerRows).toContain(jasmine.objectContaining(values));
157         });
158       });
159     });
160
161     describe('should display default values for non required properties', () => {
162       it('producer defaults', async () => {
163         const producerMissingProperties = {
164           ei_producer_id: 'producer1'
165         } as EIProducer;
166         const producers: EIProducer[] =[ producerMissingProperties ];
167         producerDataSourceSpy.eiProducers.and.returnValue(producers);
168         component.ngOnInit();
169
170         const expectedProducerRow = { id: 'producer1', types: '< No types >', status: '< No status >' };
171         let producersTable = await loader.getHarness(MatTableHarness.with({selector: '#producersTable'}));
172         let producerRows = await producersTable.getRows();
173         expect(await producerRows[0].getCellTextByColumnName()).toEqual(expectedProducerRow);
174         });
175
176       it('job defaults', async () => {
177         const jobMissingProperties = {
178           ei_job_identity: 'job1',
179           target_uri: 'http://one'
180         } as EIJob;
181         const jobs: EIJob[] =[ jobMissingProperties ];
182         jobDataSourceSpy.eiJobs.and.returnValue(jobs);
183         component.ngOnInit();
184
185         const expectedJobRow = { id: 'job1', typeId: '< No type >', owner: '< No owner >', targetUri: 'http://one' };
186         let jobsTable = await loader.getHarness(MatTableHarness.with({selector: '#jobsTable'}));
187         let jobRows = await jobsTable.getRows();
188         expect(await jobRows[0].getCellTextByColumnName()).toEqual(expectedJobRow);
189         });
190     });
191
192     it('filtering', async () => {
193       component.ngOnInit();
194       let producersTable = await loader.getHarness(MatTableHarness.with({selector: '#producersTable'}));
195
196       let idFilterInput = await loader.getHarness(MatInputHarness.with({selector: '#producerIdFilter'}));
197       await idFilterInput.setValue("1");
198       let producerRows = await producersTable.getRows();
199       expect(producerRows.length).toEqual(1);
200       expect(await producerRows[0].getCellTextByColumnName()).toEqual(expectedProducer1Row);
201
202       idFilterInput.setValue('');
203       let typesFilterInput = await loader.getHarness(MatInputHarness.with({selector: '#producerTypesFilter'}));
204       await typesFilterInput.setValue("1");
205       producerRows = await producersTable.getRows();
206       expect(producerRows.length).toEqual(1);
207       expect(await producerRows[0].getCellTextByColumnName()).toEqual(expectedProducer1Row);
208       await typesFilterInput.setValue("2");
209       producerRows = await producersTable.getRows();
210       expect(producerRows.length).toEqual(2);
211
212       typesFilterInput.setValue('');
213       let statusFilterInput = await loader.getHarness(MatInputHarness.with({selector: '#producerStatusFilter'}));
214       await statusFilterInput.setValue("enabled");
215       producerRows = await producersTable.getRows();
216       expect(producerRows.length).toEqual(1);
217       expect(await producerRows[0].getCellTextByColumnName()).toEqual(expectedProducer1Row);
218     });
219   });
220
221   describe('#jobsTable', () => {
222     const expectedJob1Row = { id: 'job1', typeId: 'type1', owner: 'owner1', targetUri: 'http://one' };
223     beforeEach(() => {
224       const jobs: EIJob[] =[ job1, job2 ];
225       jobDataSourceSpy.eiJobs.and.returnValue(jobs);
226     });
227
228     it('should contain data after initialization', async () => {
229       component.ngOnInit();
230       const expectedJobRows = [
231         expectedJob1Row,
232         { id: 'job2', typeId: 'type2', owner: 'owner2', targetUri: 'http://two' }
233       ];
234       let jobsTable = await loader.getHarness(MatTableHarness.with({selector: '#jobsTable'}));
235       let jobRows = await jobsTable.getRows();
236       expect(jobRows.length).toEqual(2);
237       jobRows.forEach(row => {
238         row.getCellTextByColumnName().then(values => {
239           expect(expectedJobRows).toContain(jasmine.objectContaining(values));
240         });
241       });
242     });
243
244     it('filtering', async () => {
245       component.ngOnInit();
246       let jobsTable = await loader.getHarness(MatTableHarness.with({selector: '#jobsTable'}));
247
248       let idFilterInput = await loader.getHarness(MatInputHarness.with({selector: '#jobIdFilter'}));
249       await idFilterInput.setValue("1");
250       let jobRows = await jobsTable.getRows();
251       expect(jobRows.length).toEqual(1);
252       expect(await jobRows[0].getCellTextByColumnName()).toEqual(expectedJob1Row);
253
254       idFilterInput.setValue('');
255       let typeIdFilterInput = await loader.getHarness(MatInputHarness.with({selector: '#jobTypeIdFilter'}));
256       await typeIdFilterInput.setValue("1");
257       jobRows = await jobsTable.getRows();
258       expect(jobRows.length).toEqual(1);
259
260       typeIdFilterInput.setValue('');
261       let ownerFilterInput = await loader.getHarness(MatInputHarness.with({selector: '#jobOwnerFilter'}));
262       await ownerFilterInput.setValue("1");
263       jobRows = await jobsTable.getRows();
264       expect(jobRows.length).toEqual(1);
265       expect(await jobRows[0].getCellTextByColumnName()).toEqual(expectedJob1Row);
266
267       ownerFilterInput.setValue('');
268       let targetUriFilterInput = await loader.getHarness(MatInputHarness.with({selector: '#jobTargetUriFilter'}));
269       await targetUriFilterInput.setValue("one");
270       jobRows = await jobsTable.getRows();
271       expect(jobRows.length).toEqual(1);
272       expect(await jobRows[0].getCellTextByColumnName()).toEqual(expectedJob1Row);
273     });
274   });
275 });