Fix Policy-type Datasource Test cases
[portal/nonrtric-controlpanel.git] / webapp-frontend / src / app / policy-control / policy-type.datasource.ts
index 983fc96..2e10f55 100644 (file)
  * ========================LICENSE_END===================================
  */
 
-import { CollectionViewer, DataSource } from '@angular/cdk/collections';
 import { HttpErrorResponse } from '@angular/common/http';
-import { MatSort } from '@angular/material';
-import { Observable } from 'rxjs/Observable';
+import { CollectionViewer, DataSource } from '@angular/cdk/collections';
+import { Injectable } from '@angular/core';
 import { BehaviorSubject } from 'rxjs/BehaviorSubject';
-import { merge } from 'rxjs';
 import { of } from 'rxjs/observable/of';
+import { Observable } from 'rxjs/Observable';
 import { catchError, finalize, map } from 'rxjs/operators';
-import { PolicyType } 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';
 
-export class PolicyTypeDataSource extends DataSource<PolicyType> {
+@Injectable({
+    providedIn: 'root'
+})
 
-    private policyTypeSubject = new BehaviorSubject<PolicyType[]>([]);
+export class PolicyTypeDataSource extends DataSource<PolicyTypeSchema> {
 
-    private loadingSubject = new BehaviorSubject<boolean>(false);
+    policyTypes: PolicyTypeSchema[] = [];
 
-    public loading$ = this.loadingSubject.asObservable();
+    policyTypeSubject = new BehaviorSubject<PolicyTypeSchema[]>([]);
+
+    private loadingSubject = new BehaviorSubject<boolean>(false);
 
     public rowCount = 1; // hide footer during intial load
 
-    constructor(private policySvc: PolicyService,
-        private sort: MatSort,
+    constructor(public policySvc: PolicyService,
         private notificationService: NotificationService) {
         super();
     }
 
-    loadTable() {
-        this.loadingSubject.next(true);
+    public getPolicyTypes() {
+        this.policyTypes = [] as PolicyTypeSchema[];
         this.policySvc.getPolicyTypes()
             .pipe(
-                catchError((her: HttpErrorResponse) => {
-                    this.notificationService.error('Failed to get policy types: ' + her.statusText + ', ' + her.error);
+                catchError((httpError: HttpErrorResponse) => {
+                    this.notificationService.error('Failed to get policy types: ' + httpError.error);
                     return of([]);
                 }),
                 finalize(() => this.loadingSubject.next(false))
             )
-            .subscribe((types: PolicyType[]) => {
-                this.rowCount = types.length;
-                for (let i = 0; i < types.length; i++) {
-                    const policyType = types[i];
-                    try {
-                        policyType.schemaObject = JSON.parse(policyType.schema);
-                    } catch (jsonError) {
-                        console.error('Could not parse schema: ' + policyType.schema);
-                        policyType.schemaObject = { description: 'Incorrect schema: ' + jsonError };
-                    }
+            .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);
+                        }
+                        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);
+                    })
                 }
-                this.policyTypeSubject.next(types);
-            });
+            })
     }
 
-    connect(collectionViewer: CollectionViewer): Observable<PolicyType[]> {
-        const dataMutations = [
-            this.policyTypeSubject.asObservable(),
-            this.sort.sortChange
-        ];
-        return merge(...dataMutations).pipe(map(() => {
-            return this.getSortedData([...this.policyTypeSubject.getValue()]);
-        }));
+    connect(collectionViewer: CollectionViewer): Observable<PolicyTypeSchema[]> {
+        return of(this.policyTypeSubject.getValue());
     }
 
     disconnect(collectionViewer: CollectionViewer): void {
         this.policyTypeSubject.complete();
-        this.loadingSubject.complete();
-    }
-
-    private getSortedData(data: PolicyType[]) {
-        if (!this.sort.active || this.sort.direction === '') {
-            return data;
-        }
-
-        return data.sort((a, b) => {
-            const isAsc = this.sort.direction === 'asc';
-            switch (this.sort.active) {
-                case 'name': return compare(a.name, b.name, isAsc);
-                default: return 0;
-            }
-        });
     }
 }
-
-function compare(a: any, b: any, isAsc: boolean) {
-    return (a < b ? -1 : 1) * (isAsc ? 1 : -1);
-}