61ba253850448c194dab758f5854563505816856
[portal/nonrtric-controlpanel.git] / webapp-frontend / src / app / services / ei / producer.service.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 { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing'
21 import { TestBed } from '@angular/core/testing';
22
23 import { JobInfo, ProducerStatus, OperationalState, ProducerRegistrationInfo } from '@interfaces/producer.types';
24 import { ProducerService } from './producer.service';
25
26 describe('ProducerService', () => {
27   let basePath = '/data-producer/v1';
28   let service: ProducerService;
29   let httpTestingController: HttpTestingController;
30
31   beforeEach(() => TestBed.configureTestingModule({
32     imports: [
33       HttpClientTestingModule
34     ],
35     providers: [
36       ProducerService
37     ]
38   }));
39
40   it('should be created', () => {
41     service = TestBed.inject(ProducerService);
42     expect(service).toBeTruthy();
43   });
44
45   describe('#getProducerIds', () => {
46     let expectedEIProducerIds: string[];
47
48     beforeEach(() => {
49       service = TestBed.inject(ProducerService);
50       httpTestingController = TestBed.inject(HttpTestingController);
51       expectedEIProducerIds = [ 'producer1', 'producer2' ] as string[];
52     });
53
54   it('should return all producer IDs', () => {
55       service.getProducerIds().subscribe(
56         producers => expect(producers).toEqual(expectedEIProducerIds, 'should return expected EIProducer IDs'),
57         fail
58       );
59
60       const req = httpTestingController.expectOne(basePath + '/' + service.producersPath);
61       expect(req.request.method).toEqual('GET');
62
63       req.flush(expectedEIProducerIds); //Return expected producer IDs
64
65       httpTestingController.verify();
66     });
67   });
68
69   describe('#getJobsForProducer', () => {
70     let expectedEIJobs: JobInfo[];
71
72     beforeEach(() => {
73       service = TestBed.inject(ProducerService);
74       httpTestingController = TestBed.inject(HttpTestingController);
75       expectedEIJobs = [
76         { info_job_identity: '1', info_job_data: 'data', info_type_identity: 'Type ID 1',  target_uri: 'hhtp://url', owner: 'owner'},
77         { info_job_identity: '2', info_job_data: 'EI Job 2', info_type_identity: 'Type ID 2',  target_uri: 'hhtp://url', owner: 'owner'}
78       ] as JobInfo[];
79     });
80
81     it('should return all jobs', () => {
82       service.getJobsForProducer('producer1').subscribe(
83         jobs => expect(jobs).toEqual(expectedEIJobs, 'should return expected Jobs'),
84         fail
85       );
86
87       const req = httpTestingController.expectOne(basePath + '/' + service.producersPath + '/producer1/' + service.jobsPath);
88       expect(req.request.method).toEqual('GET');
89
90       req.flush(expectedEIJobs); //Return expectedEIJobs
91
92       httpTestingController.verify();
93      });
94   });
95
96   describe('#getProducer', () => {
97     let expectedProducer: ProducerRegistrationInfo;
98
99     beforeEach(() => {
100       service = TestBed.inject(ProducerService);
101       httpTestingController = TestBed.inject(HttpTestingController);
102       expectedProducer = {
103         supported_info_types: [ 'type1', 'type2' ]
104       } as ProducerRegistrationInfo;
105     });
106
107     it('should return producer', () => {
108       service.getProducer('producer1').subscribe(
109         producer => expect(producer).toEqual(expectedProducer, 'should return expected producer'),
110         fail
111       );
112
113       const req = httpTestingController.expectOne(basePath + '/' + service.producersPath + '/producer1');
114       expect(req.request.method).toEqual('GET');
115
116       req.flush(expectedProducer); //Return expected producer
117
118       httpTestingController.verify();
119      });
120   });
121
122   describe('#getProducerStatus', () => {
123     let expectedProducerStatus: ProducerStatus;
124
125     beforeEach(() => {
126       service = TestBed.inject(ProducerService);
127       httpTestingController = TestBed.inject(HttpTestingController);
128       expectedProducerStatus = {
129         operational_state: OperationalState.ENABLED
130       } as ProducerStatus;
131     });
132
133     it('should return producer status', () => {
134       service.getProducerStatus('producer1').subscribe(
135         producerStatus => expect(producerStatus).toEqual(expectedProducerStatus, 'should return expected producer'),
136         fail
137       );
138
139       const req = httpTestingController.expectOne(basePath + '/' + service.producersPath + '/producer1/' + service.producerStatusPath);
140       expect(req.request.method).toEqual('GET');
141
142       req.flush(expectedProducerStatus); //Return expected status
143
144       httpTestingController.verify();
145      });
146   });
147 });