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';
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'};
+ const 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']);
TestBed.configureTestingModule({
imports: [ToastrModule.forRoot()],
providers: [
- { provide: EIService, useValue: eiServiceSpy },
- NotificationService
+ { provide: EIService, useValue: eiServiceSpy }
]
});
});
* ========================LICENSE_END===================================
*/
-import { HttpErrorResponse } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { MatTableDataSource } from '@angular/material';
import { BehaviorSubject } from 'rxjs/BehaviorSubject';
-import { of } from 'rxjs/observable/of';
-import { catchError, finalize, map } from 'rxjs/operators';
import { EIJob } from '../interfaces/ei.types';
import { EIService } from '../services/ei/ei.service';
-import { NotificationService } from '../services/ui/notification.service';
@Injectable({
providedIn: 'root'
public rowCount = 1; // hide footer during intial load
constructor(
- private eiSvc: EIService,
- private notificationService: NotificationService) {
+ private eiSvc: EIService) {
super();
}
getJobs() {
this.loadingSubject.next(true);
this.eiSvc.getProducerIds()
- .pipe(
- catchError((her: HttpErrorResponse) => {
- this.notificationService.error('Failed to get EI jobs: ' + her.error);
- return of([]);
- }),
- finalize(() => this.loadingSubject.next(false))
- )
.subscribe((producerIds: string[]) => {
producerIds.forEach(id => {
this.getJobsForProducer(id);
import { BehaviorSubject, of } from 'rxjs';
import { EIService } from '../services/ei/ei.service';
-import { NotificationService } from '../services/ui/notification.service';
import { ToastrModule } from 'ngx-toastr';
import { EIProducer, OperationalState, ProducerRegistrationInfo, ProducerStatus } from '../interfaces/ei.types';
import { EIProducerDataSource } from './ei-producer.datasource';
TestBed.configureTestingModule({
imports: [ToastrModule.forRoot()],
providers: [
- { provide: EIService, useValue: eiServiceSpy },
- NotificationService
+ { provide: EIService, useValue: eiServiceSpy }
]
});
});
* ========================LICENSE_END===================================
*/
-import { HttpErrorResponse } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { MatTableDataSource } from '@angular/material';
import { Observable } from 'rxjs/Observable';
import { BehaviorSubject } from 'rxjs/BehaviorSubject';
import { of } from 'rxjs/observable/of';
-import { catchError, finalize, tap } from 'rxjs/operators';
import { EIProducer } from '../interfaces/ei.types';
import { EIService } from '../services/ei/ei.service';
-import { NotificationService } from '../services/ui/notification.service';
@Injectable({
providedIn: 'root'
public rowCount = 1; // hide footer during intial load
constructor(
- private eiSvc: EIService,
- private notificationService: NotificationService) {
+ private eiSvc: EIService) {
super();
}
this.loadingSubject.next(true);
let producers: Array<EIProducer> = [];
this.eiSvc.getProducerIds()
- .pipe(
- catchError((her: HttpErrorResponse) => {
- this.notificationService.error('Failed to get producers: ' + her.error);
- return of([]);
- }),
- finalize(() => this.loadingSubject.next(false))
- )
.subscribe((prodIds: string[]) => {
console.log("ProducerIds: " + prodIds);
prodIds.forEach(id => {
--- /dev/null
+/*-
+ * ========================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 {
+ HttpClientTestingModule,
+ HttpTestingController,
+} from '@angular/common/http/testing';
+import { HTTP_INTERCEPTORS } from '@angular/common/http';
+import { HttpRequestInterceptor } from './interceptor';
+import { NotificationService } from './services/ui/notification.service';
+import { EIService } from './services/ei/ei.service';
+import { of } from 'rxjs/observable/of';
+
+describe(`HttpRequestInterceptor`, () => {
+ let service: EIService;
+ const notificationServiceSpy = jasmine.createSpyObj('NotificationService', [ 'error' ]);
+ let httpMock: HttpTestingController;
+
+ beforeEach(() => {
+
+ TestBed.configureTestingModule({
+ imports: [HttpClientTestingModule],
+ providers: [
+ {
+ provide: NotificationService, useValue: notificationServiceSpy
+ },
+ {
+ provide: HTTP_INTERCEPTORS,
+ useClass: HttpRequestInterceptor,
+ multi: true,
+ },
+ EIService
+ ]
+ });
+
+ httpMock = TestBed.get(HttpTestingController);
+ service = TestBed.get(EIService);
+ });
+
+ it('should create', () => {
+ const interceptors = TestBed.get(HTTP_INTERCEPTORS);
+ let found = false;
+ interceptors.forEach(interceptor => {
+ if (interceptor instanceof HttpRequestInterceptor) {
+ found = true;
+ };
+ });
+ expect(found).toBeTruthy();
+ })
+
+ it('should pass through when ok', () => {
+ let response: [ 'roducer1' ];
+
+ spyOn(service, 'getProducerIds').and.returnValue(of(response));
+ service.getProducerIds().subscribe(() => {
+ ids => expect(ids).toEqual(response);
+ });
+
+ httpMock.verify();
+ });
+
+ it('should notify when error', () => {
+ let response: any;
+ let errResponse: any;
+
+ service.getProducerIds().subscribe(res => response = res, err => errResponse = err);
+
+ const data = 'Invalid request parameters';
+ const mockErrorResponse = { status: 400, statusText: 'Bad Request' };
+ httpMock.expectOne(`/ei-producer/v1/eiproducers`).flush(data, mockErrorResponse);
+
+ httpMock.verify();
+
+ expect(notificationServiceSpy.error).toHaveBeenCalledWith(containsString('Bad Request'));
+ });
+
+ function containsString(msg: string) {
+ return {
+ asymmetricMatch: function(compareTo: string) {
+ return compareTo.includes(msg);
+ },
+
+ jasmineToString: function() {
+ return '<containsString: ' + msg + '>';
+ }
+ };
+ }
+});
+
* ========================LICENSE_END===================================
*/
-import { Injectable, Injector } from '@angular/core';
-import { HttpEvent, HttpHandler, HttpInterceptor, HttpRequest, HttpResponse } from '@angular/common/http';
-import { Observable, of } from 'rxjs';
+import { Injectable } from '@angular/core';
+import { HttpErrorResponse, HttpEvent, HttpHandler, HttpInterceptor, HttpRequest } from '@angular/common/http';
+import { Observable } from 'rxjs';
+import { catchError } from 'rxjs/operators';
+import { throwError } from 'rxjs';
+import { NotificationService } from './services/ui/notification.service';
@Injectable()
export class HttpRequestInterceptor implements HttpInterceptor {
- constructor(private injector: Injector) {}
+ constructor(private notificationService: NotificationService) {}
- intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
+ intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
console.log('Interceptor Invoked' + request.url);
- return next.handle(request);
+ return next.handle(request).pipe(
+ catchError((error: HttpErrorResponse) => {
+ console.error("Error from error interceptor", error);
+
+ // show dialog for error message
+ this.notificationService.error(error.message);
+ return throwError(error);
+ })
+ ) as Observable<HttpEvent<any>>;
}
}
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';
TestBed.configureTestingModule({
imports: [ToastrModule.forRoot()],
providers: [
- { provide: PolicyService, useValue: policyServiceSpy },
- NotificationService
+ { provide: PolicyService, useValue: policyServiceSpy }
]
});
});
* ========================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 { PolicyType, PolicyTypes, PolicyTypeSchema } from '../interfaces/policy.types';
import { PolicyService } from '../services/policy/policy.service';
-import { NotificationService } from '../services/ui/notification.service';
@Injectable({
providedIn: 'root'
policyTypeSubject = new BehaviorSubject<PolicyTypeSchema[]>([]);
- private loadingSubject = new BehaviorSubject<boolean>(false);
-
public rowCount = 1; // hide footer during intial load
- constructor(public policySvc: PolicyService,
- private notificationService: NotificationService) {
+ constructor(public policySvc: PolicyService) {
super();
}
public getPolicyTypes() {
this.policyTypes = [] as PolicyTypeSchema[];
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) {
}
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;