Move Enrichment Job Logic from backend 71/5571/3
authorelinuxhenrik <henrik.b.andersson@est.tech>
Tue, 2 Feb 2021 17:07:07 +0000 (18:07 +0100)
committerelinuxhenrik <henrik.b.andersson@est.tech>
Wed, 3 Feb 2021 14:13:25 +0000 (15:13 +0100)
Change-Id: Ib64fa7bfed5454bcae695f9364aeb0cf879071c0
Signed-off-by: elinuxhenrik <henrik.b.andersson@est.tech>
Issue-ID: NONRTRIC-408

webapp-frontend/src/app/ei-coordinator/ei-coordinator.component.spec.ts
webapp-frontend/src/app/ei-coordinator/ei-coordinator.component.ts
webapp-frontend/src/app/ei-coordinator/ei-job.datasource.spec.ts [new file with mode: 0644]
webapp-frontend/src/app/ei-coordinator/ei-job.datasource.ts
webapp-frontend/src/app/interceptor.mock.ts
webapp-frontend/src/app/mock/ei-jobs-producer1.json [moved from webapp-frontend/src/app/mock/ei-jobs.json with 89% similarity]
webapp-frontend/src/app/mock/ei-jobs-producer2.json [new file with mode: 0644]
webapp-frontend/src/app/mock/ei-producerids.json [new file with mode: 0644]
webapp-frontend/src/app/services/ei/ei.service.spec.ts
webapp-frontend/src/app/services/ei/ei.service.ts

index 50127a3..b9c2e0f 100644 (file)
@@ -35,7 +35,7 @@ describe('EICoordinatorComponent', () => {
   let fixture: ComponentFixture<EICoordinatorComponent>;
 
   beforeEach(async(() => {
-    const jobDataSourceSpy = jasmine.createSpyObj('EIJobDataSource', [ 'connect', 'loadTable', 'disconnect' ]);
+    const jobDataSourceSpy = jasmine.createSpyObj('EIJobDataSource', [ 'connect', 'getJobs', 'disconnect' ]);
     const producerDataSourceSpy = jasmine.createSpyObj('EIProducerDataSource', [ 'connect', 'loadTable', 'getProducers',  'disconnect' ]);
 
     jobDataSourceSpy.connect.and.returnValue(of([]));
index 90dfef1..bd3f946 100644 (file)
@@ -71,7 +71,7 @@ export class EICoordinatorComponent implements OnInit {
         }
 
     ngOnInit() {
-        this.eiJobsDataSource.loadTable();
+        this.eiJobsDataSource.getJobs();
 
         this.producers$= this.eiProducersDataSource.getProducers();
         this.filteredProducers$ = defer(() => this.formGroup.get("filter")
@@ -148,7 +148,7 @@ export class EICoordinatorComponent implements OnInit {
     }
 
     refreshTables() {
-        this.eiJobsDataSource.loadTable();
+        this.eiJobsDataSource.getJobs();
         this.eiProducersDataSource.loadTable();
     }
 }
diff --git a/webapp-frontend/src/app/ei-coordinator/ei-job.datasource.spec.ts b/webapp-frontend/src/app/ei-coordinator/ei-job.datasource.spec.ts
new file mode 100644 (file)
index 0000000..cb530a6
--- /dev/null
@@ -0,0 +1,61 @@
+/*-
+ * ========================LICENSE_START=================================
+ * O-RAN-SC
+ * %%
+ * Copyright (C) 2021 Nordix Foundation
+ * %%
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ========================LICENSE_END===================================
+ */
+import { async, ComponentFixture, TestBed } from '@angular/core/testing';
+import { BehaviorSubject, of } from 'rxjs';
+
+import { EIJobDataSource } from './ei-job.datasource';
+import { EIService } from '../services/ei/ei.service';
+import { NotificationService } from '../services/ui/notification.service';
+import { ToastrModule } from 'ngx-toastr';
+import { EIJob } from '../interfaces/ei.types';
+
+describe('EIJobDataSource', () => {
+    let dataSource: EIJobDataSource;
+    let eiServiceSpy: any;
+
+    let job = { ei_job_identity: '1', ei_job_data: 'data', ei_type_identity: 'Type ID 1',  target_uri: 'hhtp://url', owner: 'owner'};
+
+    beforeEach(() => {
+        eiServiceSpy = jasmine.createSpyObj('EIService', ['getProducerIds', 'getJobsForProducer']);
+
+        eiServiceSpy.getProducerIds.and.returnValue(of([ 'producer1', 'producer2']));
+        eiServiceSpy.getJobsForProducer.and.returnValue(of([job]));
+        TestBed.configureTestingModule({
+            imports: [ToastrModule.forRoot()],
+            providers: [
+                { provide: EIService, useValue: eiServiceSpy },
+                NotificationService
+            ]
+        });
+    });
+
+    it('should create', () => {
+        dataSource = TestBed.get(EIJobDataSource);
+        expect(dataSource).toBeTruthy();
+    });
+
+    it('#getJobs and connect', () => {
+        dataSource.getJobs();
+        const jobsSubject: BehaviorSubject<EIJob[]> = dataSource.eiJobsSubject;
+        const value = jobsSubject.getValue();
+        expect(value).toEqual([ job, job ]);
+        expect(dataSource.rowCount).toEqual(2);
+    });
+});
index 9e048b6..2e0e0c8 100644 (file)
@@ -36,7 +36,7 @@ import { NotificationService } from '../services/ui/notification.service';
 
 export class EIJobDataSource extends MatTableDataSource<EIJob> {
 
-    private eiJobSubject = new BehaviorSubject<EIJob[]>([]);
+    eiJobsSubject = new BehaviorSubject<EIJob[]>([]);
 
     private loadingSubject = new BehaviorSubject<boolean>(false);
 
@@ -50,9 +50,9 @@ export class EIJobDataSource extends MatTableDataSource<EIJob> {
         super();
     }
 
-    loadTable() {
+    getJobs() {
         this.loadingSubject.next(true);
-        this.eiSvc.getEIJobs()
+        this.eiSvc.getProducerIds()
             .pipe(
                 catchError((her: HttpErrorResponse) => {
                     this.notificationService.error('Failed to get EI jobs: ' + her.error);
@@ -60,18 +60,33 @@ export class EIJobDataSource extends MatTableDataSource<EIJob> {
                 }),
                 finalize(() => this.loadingSubject.next(false))
             )
-            .subscribe((instances: EIJob[]) => {
-                this.rowCount = instances.length;
-                this.eiJobSubject.next(instances);
+            .subscribe((producerIds: String[]) => {
+                producerIds.forEach(id => {
+                    this.getJobsForProducer(id);
+                });
             });
     }
 
+    private getJobsForProducer(id: String) {
+        console.log('Getting jobs for producer ID: ', id);
+        this.eiSvc.getJobsForProducer(id).subscribe(jobs => {
+            this.addJobsToSubject(jobs);
+            this.rowCount = this.eiJobsSubject.getValue().length;
+        });
+    }
+
+    private addJobsToSubject(jobs: EIJob[]) {
+        const currentValue = this.eiJobsSubject.value;
+        const updatedValue = [...currentValue, ...jobs];
+        this.eiJobsSubject.next(updatedValue);
+    }
+
     connect(): BehaviorSubject<EIJob[]> {
-        return this.eiJobSubject;
+        return this.eiJobsSubject;
     }
 
     disconnect(): void {
-        this.eiJobSubject.complete();
+        this.eiJobsSubject.complete();
         this.loadingSubject.complete();
     }
 }
index 05e1a4e..66807d7 100644 (file)
@@ -26,9 +26,10 @@ import * as policies from './mock/policies.json';
 import * as policyinstances2 from './mock/policy-instance-2.json';
 import * as policyinstances1Status from './mock/policy-instance-1-status.json';
 import * as policyinstances2Status from './mock/policy-instance-2-status.json';
-import * as eijobs from './mock/ei-jobs.json';
+import * as eijobsProd1 from './mock/ei-jobs-producer1.json';
+import * as eijobsProd2 from './mock/ei-jobs-producer2.json';
+import * as eiProducerIds from './mock/ei-producerids.json';
 import * as eiproducers from './mock/ei-producers.json';
-import * as policyinstanceNoType from './mock/policy-instance-notype.json';
 import * as policytypesList from './mock/policy-types.json';
 import * as policytypes1 from './mock/policy-type1.json';
 import * as policyinstanceedit from './mock/policy-instance-edit.json';
@@ -101,10 +102,6 @@ const urls = [
         url: '/a1-policy/v2/rics?policytype_id=',
         json: ric2
     },
-    {
-        url: 'api/enrichment/eijobs',
-        json: eijobs
-    },
     {
         url: 'api/enrichment/eiproducers',
         json: eiproducers
@@ -116,6 +113,18 @@ const urls = [
     {
         url: 'api/policy/rics?policyType=2',
         json: rics
+    },
+    {
+        url: '/ei-producer/v1/eiproducers',
+        json: eiProducerIds
+    },
+    {
+        url: '/ei-producer/v1/eiproducers/producer1/eijobs',
+        json: eijobsProd1
+    },
+    {
+        url: '/ei-producer/v1/eiproducers/producer2/eijobs',
+        json: eijobsProd2
     }
 ];
 
@@ -12,7 +12,8 @@
     },
     {
       "ei_job_identity": "job2",
-      "ei_type_identity": "type2",
+      "ei_type_identity": "type1",
+      "owner": "owner1",
       "ei_job_data": {
         "jobparam2": "value2_job2",
         "jobparam3": "value3_job2",
diff --git a/webapp-frontend/src/app/mock/ei-jobs-producer2.json b/webapp-frontend/src/app/mock/ei-jobs-producer2.json
new file mode 100644 (file)
index 0000000..07220a9
--- /dev/null
@@ -0,0 +1,13 @@
+[
+    {
+      "ei_job_identity": "job3",
+      "ei_type_identity": "type2",
+      "owner": "owner1",
+      "ei_job_data": {
+        "jobparam2": "value2_job3",
+        "jobparam3": "value3_job3",
+        "jobparam1": "value1_job3"
+      },
+      "target_uri": "https://ricsim_g3_1:8185/datadelivery"
+    }
+  ]
\ No newline at end of file
diff --git a/webapp-frontend/src/app/mock/ei-producerids.json b/webapp-frontend/src/app/mock/ei-producerids.json
new file mode 100644 (file)
index 0000000..c6789c0
--- /dev/null
@@ -0,0 +1,4 @@
+[
+    "producer1",
+    "producer2"
+]
\ No newline at end of file
index 9e2584b..ca77d0c 100644 (file)
 import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing'
 import { TestBed } from '@angular/core/testing';
 
-import { EIJob, EIProducer } from '../../interfaces/ei.types';
+import { EIJob } from '../../interfaces/ei.types';
 import { EIService } from './ei.service';
 
 describe('EIService', () => {
-  let basePath = 'api/enrichment';
+  let basePath = '/ei-producer/v1';
   let service: EIService;
   let httpTestingController: HttpTestingController;
 
@@ -43,27 +43,24 @@ describe('EIService', () => {
   });
 
   describe('#getEIProducers', () => {
-    let expectedEIProducers: EIProducer[];
+    let expectedEIProducerIds: String[];
 
     beforeEach(() => {
       service = TestBed.get(EIService);
       httpTestingController = TestBed.get(HttpTestingController);
-      expectedEIProducers = [
-        { ei_producer_id: '1', ei_producer_types: ['EI Type 1'], status: 'ENABLED' },
-        { ei_producer_id: '1', ei_producer_types: ['EI Type 1'], status: 'ENABLED' }
-      ] as EIProducer[];
+      expectedEIProducerIds = [ 'producer1', 'producer2' ] as String[];
     });
 
-    it('should return all producers', () => {
-      service.getEIProducers().subscribe(
-        producers => expect(producers).toEqual(expectedEIProducers, 'should return expected EIProducers'),
+  it('should return all producer IDs', () => {
+      service.getProducerIds().subscribe(
+        producers => expect(producers).toEqual(expectedEIProducerIds, 'should return expected EIProducer IDs'),
         fail
       );
 
-      const req = httpTestingController.expectOne(basePath + '/' + service.eiProducerPath);
+      const req = httpTestingController.expectOne(basePath + '/' + service.eiProducersPath);
       expect(req.request.method).toEqual('GET');
 
-      req.flush(expectedEIProducers); //Return expectedEITypes
+      req.flush(expectedEIProducerIds); //Return expected producer IDs
 
       httpTestingController.verify();
     });
@@ -82,12 +79,12 @@ describe('EIService', () => {
     });
 
     it('should return all jobs', () => {
-      service.getEIJobs().subscribe(
+      service.getJobsForProducer('producer1').subscribe(
         jobs => expect(jobs).toEqual(expectedEIJobs, 'should return expected Jobs'),
         fail
       );
 
-      const req = httpTestingController.expectOne(basePath + '/' + service.eiJobPath);
+      const req = httpTestingController.expectOne(basePath + '/' + service.eiProducersPath + '/producer1/' + service.eiJobsPath);
       expect(req.request.method).toEqual('GET');
 
       req.flush(expectedEIJobs); //Return expectedEIJobs
index d09b5ef..3be9b8b 100644 (file)
 
 import { Injectable } from '@angular/core';
 import { HttpClient } from '@angular/common/http';
-import { Observable } from 'rxjs';
-import { map } from 'rxjs/operators';
+import { Observable, of } from 'rxjs';
 import { EIJob, EIProducer } from '../../interfaces/ei.types';
-import { ControlpanelSuccessTransport } from '../../interfaces/controlpanel.types';
 
 /**
  * Services for calling the EI endpoints.
@@ -33,9 +31,9 @@ import { ControlpanelSuccessTransport } from '../../interfaces/controlpanel.type
 })
 export class EIService {
 
-    private basePath = 'api/enrichment';
-    eiJobPath = 'eijobs';
-    eiProducerPath = 'eiproducers';
+    private basePath = '/ei-producer/v1';
+    eiJobsPath = 'eijobs';
+    eiProducersPath = 'eiproducers';
 
     private buildPath(...args: any[]) {
         let result = this.basePath;
@@ -49,13 +47,18 @@ export class EIService {
         // injects to variable httpClient
     }
 
-    getEIJobs(): Observable<EIJob[]> {
-        const url = this.buildPath(this.eiJobPath);
+    getProducerIds(): Observable<String[]> {
+        const url = this.buildPath(this.eiProducersPath);
+        return this.httpClient.get<String[]>(url);
+    }
+
+    getJobsForProducer(producerId: String): Observable<EIJob[]> {
+        const url = this.buildPath(this.eiProducersPath, producerId, this.eiJobsPath);
         return this.httpClient.get<EIJob[]>(url);
     }
 
     getEIProducers(): Observable<EIProducer[]> {
-        const url = this.buildPath(this.eiProducerPath);
+        const url = this.buildPath(this.eiProducersPath);
         return this.httpClient.get<EIProducer[]>(url);
     }
 }