Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion modules/ui/src/app/services/test-run-mqtt.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<MqttService>;
let testRunServiceMock: SpyObj<TestRunService>;

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);
});
Expand Down Expand Up @@ -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 => {
Expand Down
13 changes: 11 additions & 2 deletions modules/ui/src/app/services/test-run-mqtt.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Adapters> {
return this.topic<Adapters>(Topic.NetworkAdapters);
Expand All @@ -21,7 +25,12 @@ export class TestRunMqttService {
}

getStatus(): Observable<TestrunStatus> {
return this.topic<TestrunStatus>(Topic.Status);
return this.topic<TestrunStatus>(Topic.Status).pipe(
map(result => {
result.report = this.testrunService.changeReportURL(result.report);
return result;
})
);
}

private topic<Type>(topicName: string): Observable<Type> {
Expand Down
18 changes: 15 additions & 3 deletions modules/ui/src/app/services/test-run.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,19 +154,31 @@ 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 };

service.fetchSystemStatus().subscribe(res => {
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', () => {
Expand Down
27 changes: 25 additions & 2 deletions modules/ui/src/app/services/test-run.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Device[]> {
return this.http.get<Device[]>(`${API_URL}/devices`);
}
Expand All @@ -72,7 +80,15 @@ export class TestRunService {
}

fetchSystemStatus() {
return this.http.get<TestrunStatus>(`${API_URL}/system/status`);
return this.http.get<TestrunStatus>(`${API_URL}/system/status`).pipe(
map(result => {
result.report = this.changeReportURL(result.report);
return result;
}),
catchError(() => {
return of({} as TestrunStatus);
})
);
}

stopTestrun(): Observable<boolean> {
Expand Down Expand Up @@ -134,7 +150,14 @@ export class TestRunService {
}

getHistory(): Observable<TestrunStatus[] | null> {
return this.http.get<TestrunStatus[]>(`${API_URL}/reports`);
return this.http.get<TestrunStatus[]>(`${API_URL}/reports`).pipe(
map(result => {
result.forEach(
item => (item.report = this.changeReportURL(item.report))
);
return result;
})
);
}

public getResultClass(result: string): StatusResultClassName {
Expand Down