Consumer service
[portal/nonrtric-controlpanel.git] / webapp-frontend / src / app / services / ei / consumer.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 { ConsumerService } from "./consumer.service";
21 import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
22 import { TestBed } from '@angular/core/testing';
23 import { JobInfo, ConsumerStatus, OperationalState } from '@app/interfaces/consumer.types';
24
25 describe('ConsumerService', () => {
26     let basePath = '/data-consumer/v1';
27     let service: ConsumerService;
28     let httpTestingController: HttpTestingController;
29     let expectedEIJobs: string[];
30     let expectedConsumerStatus: ConsumerStatus;
31     let expectedJobInfo: JobInfo;
32
33     beforeEach(() => TestBed.configureTestingModule({
34         imports: [
35             HttpClientTestingModule
36         ],
37         providers: [
38             ConsumerService
39         ]
40     }));
41
42     beforeEach(() => {
43         service = TestBed.inject(ConsumerService);
44         httpTestingController = TestBed.inject(HttpTestingController);
45         expectedEIJobs = ['job1', 'job2'];
46         expectedConsumerStatus = {
47             info_job_status: OperationalState.ENABLED,
48             producers: [
49                 'producer1'
50             ]
51         }
52         expectedJobInfo = {
53           job_definition: 'data',
54           info_type_id: 'type1',
55           job_result_uri: 'uri',
56           job_owner: 'owner1',
57           status_notification_uri: 'status_uri'
58         }
59     })
60
61     it('should be created', () => {
62         service = TestBed.inject(ConsumerService);
63         expect(service).toBeTruthy();
64     });
65
66     it('should get all the jobs ids', () => {
67         service.getJobIds().subscribe(
68             jobs => expect(jobs).toEqual(expectedEIJobs, 'should return expected Jobs'),
69             fail
70         );
71
72         const req = httpTestingController.expectOne(basePath + '/' + service.jobsPath);
73         expect(req.request.method).toEqual('GET');
74
75         req.flush(expectedEIJobs); //Return expected jobs ids
76
77         httpTestingController.verify();
78     });
79
80
81     it('should return consumer status', () => {
82         service.getConsumerStatus('job1').subscribe(
83             consumerStatus => expect(consumerStatus).toEqual(expectedConsumerStatus, 'should return expected status'),
84             fail
85         );
86
87         const req = httpTestingController.expectOne(basePath + '/' + service.jobsPath + '/job1/' + service.consumerStatusPath);
88         expect(req.request.method).toEqual('GET');
89
90         req.flush(expectedConsumerStatus); //Return expected status
91
92         httpTestingController.verify();
93     });
94
95     it('should return the job info', () => {
96         service.getJobInfo('job1').subscribe(
97             jobInfo => expect(jobInfo).toEqual(expectedJobInfo, 'should return expected job info'),
98             fail
99         );
100
101         const req = httpTestingController.expectOne(basePath + '/' + service.jobsPath + '/job1');
102         expect(req.request.method).toEqual('GET');
103
104         req.flush(expectedJobInfo); //Return expected job info
105
106         httpTestingController.verify();
107     });
108 });