From: Kai Moritz Date: Fri, 26 Jun 2020 14:21:49 +0000 (+0200) Subject: Added vorgang.service.ts and accompanying spec X-Git-Url: http://juplo.de/gitweb/?a=commitdiff_plain;h=ebd9d8841452da2adb2d0c00357af646a97afcd8;p=examples%2Fangular-tour-of-heroes Added vorgang.service.ts and accompanying spec --- diff --git a/src/app/vorgang.service.spec.ts b/src/app/vorgang.service.spec.ts new file mode 100644 index 0000000..72b694d --- /dev/null +++ b/src/app/vorgang.service.spec.ts @@ -0,0 +1,51 @@ +import { TestBed, getTestBed } from '@angular/core/testing'; +import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing'; + +import { VorgangService } from './vorgang.service'; +import { Vorgang } from './vorgang'; + +describe('VorgangService', () => { + let service: VorgangService; + let httpMock: HttpTestingController; + + beforeEach(() => { + TestBed.configureTestingModule({ + imports: [ HttpClientTestingModule ], + providers: [ VorgangService ] + }); + service = TestBed.inject(VorgangService); + httpMock = TestBed.inject(HttpTestingController); + }); + + afterEach(() => { + // After every test, assert that there are no more pending requests. + httpMock.verify(); + }); + + it('should be created', () => { + expect(service).toBeTruthy(); + }); + + describe('#getUrlSave', () => { + it ('should reate valid url for save', () => { + const expectedUrl = 'http://localhost:1991/las/VBID/save'; + + expect(service.getUrlSave('VBID')).toBe(expectedUrl); + }); + }); + + describe('#create', () => { + it('should return Vorgang', () => { + const expectedVorgang = require('../mock/vorgang.json'); + + service.create({ type: null, vbId: 'greetings', vorgangId: '1' , zustand: 'Hello world!'}).subscribe( + vorgang => expect(vorgang).toEqual(expectedVorgang), + fail + ); + + const req = httpMock.expectOne(service.getUrlSave(expectedVorgang.vbId)); + expect(req.request.method).toBe('POST'); + req.flush(expectedVorgang); + }); + }); +}); diff --git a/src/app/vorgang.service.ts b/src/app/vorgang.service.ts new file mode 100644 index 0000000..dff07d2 --- /dev/null +++ b/src/app/vorgang.service.ts @@ -0,0 +1,67 @@ +import { Injectable } from '@angular/core'; +import { Observable, of, EMPTY } from 'rxjs'; +import { Vorgang } from './vorgang'; +import { HttpClient, HttpHeaders } from '@angular/common/http'; +import { catchError, tap } from 'rxjs/operators'; + + +@Injectable({ + providedIn: 'root' +}) +export class VorgangService { + + gpsUrl = 'http://localhost:1991/las/'; // URL to web api + httpOptions = { + headers: new HttpHeaders({ + 'Content-Type': 'text/plain', + 'Accept': 'application/json' + }) + }; + + constructor(private http: HttpClient) { } + + /** POST: Einen neuen Vorgang erzeugen */ + create(vorgang: Vorgang): Observable { + return this + .http + .post( + this.getUrlSave(vorgang.vbId), + vorgang.zustand, + this.httpOptions) + .pipe( + tap((result: Vorgang) => this.log(`Result for ${JSON.stringify(vorgang)}: ${JSON.stringify(result)}`)), + catchError(this.handleError('create'))); + } + + getUrlSave(vbId: string): string { + return this.gpsUrl + vbId + '/save'; + } + + /** + * Handle Http operation that failed. + * Let the app continue. + * @param operation - name of the operation that failed + * @param result - optional value to return as the observable result + */ + private handleError(operation = 'operation', result?: T) { + return (error: any): Observable => { + + // TODO: send the error to remote logging infrastructure + console.error(error); // log to console instead + + // TODO: better job of transforming error for user consumption + this.log(`${operation} failed: ${error.message}`); + + // Let the app keep running by returning an empty result. + if (result === undefined) { + return EMPTY as Observable; + } else { + return of(result as T); + } + }; + } + + private log(message: string) { + console.log(message); + } +} diff --git a/src/app/vorgang.ts b/src/app/vorgang.ts new file mode 100644 index 0000000..ab6095f --- /dev/null +++ b/src/app/vorgang.ts @@ -0,0 +1,11 @@ +export enum Type { + SUCCESS = 'SUCCESS', + FAILURE = 'FAILURE' +} + +export interface Vorgang { + type?: Type; + vbId: string; + vorgangId: string; + zustand: any; +} diff --git a/src/mock/vorgang.json b/src/mock/vorgang.json new file mode 100644 index 0000000..052717a --- /dev/null +++ b/src/mock/vorgang.json @@ -0,0 +1,9 @@ +{ + "type": "SUCCESS", + "vbId": "greetings", + "vorgangId": 1, + "zustand": { + "dlz": "1|vbId=greetings|1, rand=8: Hallo Welt!", + "produkt": "1|vbId=greetings|1, rand=1: Hallo Welt!" + } +}