Fix Policy-type Datasource Test cases 83/5583/1
authorLathish <lathishbabu.ganesan@est.tech>
Wed, 3 Feb 2021 10:34:32 +0000 (10:34 +0000)
committerLathish <lathishbabu.ganesan@est.tech>
Wed, 3 Feb 2021 10:34:38 +0000 (10:34 +0000)
Issue-ID: NONRTRIC-391
Change-Id: I161eac6db867e51650646163e72e73fc31a55f23
Signed-off-by: Lathish <lathishbabu.ganesan@est.tech>
webapp-frontend/src/app/policy-control/policy-type.datasource.spec.ts [new file with mode: 0644]
webapp-frontend/src/app/policy-control/policy-type.datasource.ts

diff --git a/webapp-frontend/src/app/policy-control/policy-type.datasource.spec.ts b/webapp-frontend/src/app/policy-control/policy-type.datasource.spec.ts
new file mode 100644 (file)
index 0000000..5b83368
--- /dev/null
@@ -0,0 +1,86 @@
+/*-
+ * ========================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 { TestBed } from '@angular/core/testing';
+import { BehaviorSubject, of } from 'rxjs';
+import { NotificationService } from '../services/ui/notification.service';
+import { ToastrModule } from 'ngx-toastr';
+import { PolicyTypeDataSource } from './policy-type.datasource';
+import { PolicyService } from '../services/policy/policy.service';
+import { PolicyTypeSchema } from '../interfaces/policy.types';
+
+describe('PolicyTypeDataSource', () => {
+  let policyTypeDataSource: PolicyTypeDataSource;
+  let policyServiceSpy: any;
+
+  let policySchema = {
+    policy_schema: {
+      "$schema": "http://json-schema.org/draft-07/schema#",
+      "description": "Type 1 policy type",
+      "additionalProperties": false,
+      "title": "1",
+      "type": "object"
+    }
+  };
+
+  beforeEach(() => {
+    policyServiceSpy = jasmine.createSpyObj('PolicyService', ['getPolicyTypes', 'getPolicyType']);
+
+    policyServiceSpy.getPolicyTypes.and.returnValue(of({ policytype_ids: ['1', '2'] }));
+    policyServiceSpy.getPolicyType.and.returnValue(of(policySchema));
+    TestBed.configureTestingModule({
+      imports: [ToastrModule.forRoot()],
+      providers: [
+        { provide: PolicyService, useValue: policyServiceSpy },
+        NotificationService
+      ]
+    });
+  });
+
+  describe('#getPolicyTypes', () => {
+    let expectedPolicyTypeValue: PolicyTypeSchema[];
+    beforeEach(() => {
+      expectedPolicyTypeValue = [
+        {
+          'id': '1',
+          'name': '1',
+          'schemaObject': policySchema.policy_schema
+        },
+        {
+          'id': '2',
+          'name': '1',
+          'schemaObject': policySchema.policy_schema
+        }
+      ];
+    });
+
+    it('should create', () => {
+      policyTypeDataSource = TestBed.get(PolicyTypeDataSource);
+      expect(policyTypeDataSource).toBeTruthy();
+    });
+
+    it('should return all policy type with Schema', () => {
+      policyTypeDataSource.getPolicyTypes();
+      const jobsSubject: BehaviorSubject<PolicyTypeSchema[]> = policyTypeDataSource.policyTypeSubject;
+      const value = jobsSubject.getValue();
+      expect(value).toEqual(expectedPolicyTypeValue);
+    });
+  });
+});
\ No newline at end of file
index 2fed8f3..2e10f55 100644 (file)
  * ========================LICENSE_END===================================
  */
 
+import { HttpErrorResponse } from '@angular/common/http';
 import { CollectionViewer, DataSource } from '@angular/cdk/collections';
 import { Injectable } from '@angular/core';
 import { BehaviorSubject } from 'rxjs/BehaviorSubject';
 import { of } from 'rxjs/observable/of';
 import { Observable } from 'rxjs/Observable';
+import { catchError, finalize, map } from 'rxjs/operators';
 
-import { PolicyTypeSchema } from '../interfaces/policy.types';
+import { PolicyType, PolicyTypes, PolicyTypeSchema } from '../interfaces/policy.types';
 import { PolicyService } from '../services/policy/policy.service';
 import { NotificationService } from '../services/ui/notification.service';
 
@@ -36,39 +38,58 @@ export class PolicyTypeDataSource extends DataSource<PolicyTypeSchema> {
 
     policyTypes: PolicyTypeSchema[] = [];
 
-    private policyTypeSubject = new BehaviorSubject<PolicyTypeSchema[]>([]);
+    policyTypeSubject = new BehaviorSubject<PolicyTypeSchema[]>([]);
+
+    private loadingSubject = new BehaviorSubject<boolean>(false);
 
     public rowCount = 1; // hide footer during intial load
 
-    constructor(private policySvc: PolicyService,
+    constructor(public policySvc: PolicyService,
         private notificationService: NotificationService) {
         super();
     }
 
     public getPolicyTypes() {
         this.policyTypes = [] as PolicyTypeSchema[];
-        this.policySvc.getPolicyTypes().subscribe(policyType => {
-            if (policyType.policytype_ids.length != 0) {
-                policyType.policytype_ids.forEach(policyTypeId => {
-                    var policyTypeSchema = {} as PolicyTypeSchema
-                    if (policyTypeId === "") {
-                        policyTypeSchema.id = '';
-                        policyTypeSchema.name = '';
-                        policyTypeSchema.schemaObject = '{}';
-                        this.policyTypes.push(policyTypeSchema);
-                    }
-                    else {
-                        this.policySvc.getPolicyType(policyTypeId).subscribe(policyType => {
-                            policyTypeSchema.id = policyTypeId;
-                            policyTypeSchema.schemaObject = policyType.policy_schema;
-                            policyTypeSchema.name = policyType.policy_schema.title;
+        this.policySvc.getPolicyTypes()
+            .pipe(
+                catchError((httpError: HttpErrorResponse) => {
+                    this.notificationService.error('Failed to get policy types: ' + httpError.error);
+                    return of([]);
+                }),
+                finalize(() => this.loadingSubject.next(false))
+            )
+            .subscribe((policyType: PolicyTypes) => {
+                this.rowCount = policyType.policytype_ids.length;
+                if (policyType.policytype_ids.length != 0) {
+                    policyType.policytype_ids.forEach(policyTypeId => {
+                        var policyTypeSchema = {} as PolicyTypeSchema
+                        if (policyTypeId === "") {
+                            policyTypeSchema.id = '';
+                            policyTypeSchema.name = '';
+                            policyTypeSchema.schemaObject = '{}';
                             this.policyTypes.push(policyTypeSchema);
-                        })
-                    }
-                    this.policyTypeSubject.next(this.policyTypes);
-                })
-            }
-        })
+                        }
+                        else {
+                            this.policySvc.getPolicyType(policyTypeId)
+                                .pipe(
+                                    catchError((httpError: HttpErrorResponse) => {
+                                        this.notificationService.error('Failed to get policy type: ' + httpError.error);
+                                        return of([]);
+                                    }),
+                                    finalize(() => this.loadingSubject.next(false))
+                                )
+                                .subscribe((policyType: PolicyType) => {
+                                    policyTypeSchema.id = policyTypeId;
+                                    policyTypeSchema.schemaObject = policyType.policy_schema;
+                                    policyTypeSchema.name = policyType.policy_schema.title;
+                                    this.policyTypes.push(policyTypeSchema);
+                                })
+                        }
+                        this.policyTypeSubject.next(this.policyTypes);
+                    })
+                }
+            })
     }
 
     connect(collectionViewer: CollectionViewer): Observable<PolicyTypeSchema[]> {