diff --git a/modules/ui/src/app/services/test-run-mqtt.service.spec.ts b/modules/ui/src/app/services/test-run-mqtt.service.spec.ts index 19bda437a..6f5c7e325 100644 --- a/modules/ui/src/app/services/test-run-mqtt.service.spec.ts +++ b/modules/ui/src/app/services/test-run-mqtt.service.spec.ts @@ -9,17 +9,23 @@ import { MOCK_ADAPTERS } from '../mocks/settings.mock'; import { Topic } from '../model/topic'; import { MOCK_INTERNET } from '../mocks/topic.mock'; import { MOCK_PROGRESS_DATA_IN_PROGRESS } from '../mocks/testrun.mock'; +import { TestRunService } from './test-run.service'; describe('TestRunMqttService', () => { let service: TestRunMqttService; let mockService: SpyObj; + let testRunServiceMock: SpyObj; beforeEach(() => { mockService = jasmine.createSpyObj(['observe']); + testRunServiceMock = jasmine.createSpyObj(['changeReportURL']); TestBed.configureTestingModule({ imports: [MqttModule.forRoot(MQTT_SERVICE_OPTIONS)], - providers: [{ provide: MqttService, useValue: mockService }], + providers: [ + { provide: MqttService, useValue: mockService }, + { provide: TestRunService, useValue: testRunServiceMock }, + ], }); service = TestBed.inject(TestRunMqttService); }); @@ -75,6 +81,7 @@ describe('TestRunMqttService', () => { mockService.observe.and.returnValue( of(getResponse(MOCK_PROGRESS_DATA_IN_PROGRESS)) ); + testRunServiceMock.changeReportURL.and.returnValue(''); }); it('should subscribe the topic', done => { diff --git a/modules/ui/src/app/services/test-run-mqtt.service.ts b/modules/ui/src/app/services/test-run-mqtt.service.ts index d5e805da6..d53622d28 100644 --- a/modules/ui/src/app/services/test-run-mqtt.service.ts +++ b/modules/ui/src/app/services/test-run-mqtt.service.ts @@ -5,12 +5,16 @@ import { map } from 'rxjs/operators'; import { Adapters } from '../model/setting'; import { TestrunStatus } from '../model/testrun-status'; import { InternetConnection, Topic } from '../model/topic'; +import { TestRunService } from './test-run.service'; @Injectable({ providedIn: 'root', }) export class TestRunMqttService { - constructor(private mqttService: MqttService) {} + constructor( + private mqttService: MqttService, + private testrunService: TestRunService + ) {} getNetworkAdapters(): Observable { return this.topic(Topic.NetworkAdapters); @@ -21,7 +25,12 @@ export class TestRunMqttService { } getStatus(): Observable { - return this.topic(Topic.Status); + return this.topic(Topic.Status).pipe( + map(result => { + result.report = this.testrunService.changeReportURL(result.report); + return result; + }) + ); } private topic(topicName: string): Observable { diff --git a/modules/ui/src/app/services/test-run.service.spec.ts b/modules/ui/src/app/services/test-run.service.spec.ts index bcd3ec592..113f72c8f 100644 --- a/modules/ui/src/app/services/test-run.service.spec.ts +++ b/modules/ui/src/app/services/test-run.service.spec.ts @@ -154,6 +154,8 @@ describe('TestRunService', () => { }); describe('fetchSystemStatus', () => { + const systemStatusUrl = 'http://localhost:8000/system/status'; + it('should get system status data with no changes', () => { const result = { ...MOCK_PROGRESS_DATA_IN_PROGRESS }; @@ -161,12 +163,22 @@ describe('TestRunService', () => { expect(res).toEqual(result); }); - const req = httpTestingController.expectOne( - 'http://localhost:8000/system/status' - ); + const req = httpTestingController.expectOne(systemStatusUrl); expect(req.request.method).toBe('GET'); req.flush(result); }); + + it('should get system status as empty object if error happens', () => { + const mockError = { error: 'someError' } as ErrorEvent; + + service.fetchSystemStatus().subscribe(res => { + expect(res).toEqual({} as TestrunStatus); + }); + + const req = httpTestingController.expectOne(systemStatusUrl); + + req.error(mockError); + }); }); it('stopTestrun should have necessary request data', () => { diff --git a/modules/ui/src/app/services/test-run.service.ts b/modules/ui/src/app/services/test-run.service.ts index 2e66234c7..c517f7004 100644 --- a/modules/ui/src/app/services/test-run.service.ts +++ b/modules/ui/src/app/services/test-run.service.ts @@ -53,6 +53,14 @@ export class TestRunService { constructor(private http: HttpClient) {} + changeReportURL(url: string): string { + if (!url) { + return ''; + } + // replace url part before '/report' from static to dynamic API URL + return url.replace(/^.*(?=\/report)/, `${API_URL}`); + } + fetchDevices(): Observable { return this.http.get(`${API_URL}/devices`); } @@ -72,7 +80,15 @@ export class TestRunService { } fetchSystemStatus() { - return this.http.get(`${API_URL}/system/status`); + return this.http.get(`${API_URL}/system/status`).pipe( + map(result => { + result.report = this.changeReportURL(result.report); + return result; + }), + catchError(() => { + return of({} as TestrunStatus); + }) + ); } stopTestrun(): Observable { @@ -134,7 +150,14 @@ export class TestRunService { } getHistory(): Observable { - return this.http.get(`${API_URL}/reports`); + return this.http.get(`${API_URL}/reports`).pipe( + map(result => { + result.forEach( + item => (item.report = this.changeReportURL(item.report)) + ); + return result; + }) + ); } public getResultClass(result: string): StatusResultClassName {