From 3b0bcc44b06a0c74b1376be843c7f63b9a71d965 Mon Sep 17 00:00:00 2001 From: Braun Patrik Date: Thu, 5 May 2016 17:51:51 +0200 Subject: [PATCH] implementing network fail tests, refactoring services --- frontend/app/admin/admin.service.ts | 15 ++- frontend/app/gallery/gallery.service.ts | 10 +- .../gallery/search/autocomplete.service.ts | 7 +- .../app/model/network/network.service.spec.ts | 100 ++++++++++++++++-- frontend/app/model/network/network.service.ts | 3 +- 5 files changed, 107 insertions(+), 28 deletions(-) diff --git a/frontend/app/admin/admin.service.ts b/frontend/app/admin/admin.service.ts index dae29ff0..b969686f 100644 --- a/frontend/app/admin/admin.service.ts +++ b/frontend/app/admin/admin.service.ts @@ -7,28 +7,27 @@ import {Message} from "../../../common/entities/Message"; import {User} from "../../../common/entities/User"; @Injectable() -export class AdminService extends NetworkService{ - +export class AdminService { - constructor(_http:Http){ - super(_http); + + constructor(private _networkService:NetworkService){ } public createUser(user:User): Promise>{ - return this.putJson("/user",{newUser:user}); + return this._networkService.putJson("/user",{newUser:user}); } public getUsers():Promise>>{ - return this.getJson("/user/list"); + return this._networkService.getJson("/user/list"); } public deleteUser(user:User) { - return this.deleteJson("/user/"+user.id); + return this._networkService.deleteJson("/user/"+user.id); } public updateRole(user:User) { - return this.postJson("/user/"+user.id+"/role",{newRole:user.role}); + return this._networkService.postJson("/user/"+user.id+"/role",{newRole:user.role}); } } diff --git a/frontend/app/gallery/gallery.service.ts b/frontend/app/gallery/gallery.service.ts index 652a9da7..971f38ee 100644 --- a/frontend/app/gallery/gallery.service.ts +++ b/frontend/app/gallery/gallery.service.ts @@ -7,15 +7,15 @@ import {Message} from "../../../common/entities/Message"; import {Directory} from "../../../common/entities/Directory"; @Injectable() -export class GalleryService extends NetworkService{ - +export class GalleryService{ - constructor(_http:Http){ - super(_http); + + + constructor(private _networkService:NetworkService){ } public getDirectory(directoryName:string): Promise>{ - return this.getJson("/gallery/content/"+directoryName); + return this._networkService.getJson("/gallery/content/"+directoryName); } diff --git a/frontend/app/gallery/search/autocomplete.service.ts b/frontend/app/gallery/search/autocomplete.service.ts index e2f41a33..004198c8 100644 --- a/frontend/app/gallery/search/autocomplete.service.ts +++ b/frontend/app/gallery/search/autocomplete.service.ts @@ -7,15 +7,14 @@ import {AutoCompleteItem} from "../../../../common/entities/AutoCompleteItem"; import {Message} from "../../../../common/entities/Message"; @Injectable() -export class AutoCompleteService extends NetworkService { +export class AutoCompleteService { - constructor(_http:Http) { - super(_http); + constructor(private _networkService:NetworkService){ } public autoComplete(text:string): Promise >> { - return this.getJson("/gallery/autocomplete/"+text); + return this._networkService.getJson("/gallery/autocomplete/"+text); } diff --git a/frontend/app/model/network/network.service.spec.ts b/frontend/app/model/network/network.service.spec.ts index eccc8e59..de553327 100644 --- a/frontend/app/model/network/network.service.spec.ts +++ b/frontend/app/model/network/network.service.spec.ts @@ -1,7 +1,7 @@ /// import {it, inject, beforeEachProviders, beforeEach, afterEach} from "@angular/core/testing"; -import {BaseRequestOptions, Http, Response, ResponseOptions} from "@angular/http"; +import {BaseRequestOptions, Http, Response, ResponseOptions, ResponseType, BaseResponseOptions} from "@angular/http"; import {MockBackend, MockConnection} from "@angular/http/testing"; import {provide} from "@angular/core"; import "rxjs/Rx"; @@ -9,8 +9,7 @@ import {NetworkService} from "./network.service"; import {Message} from "../../../../common/entities/Message"; -describe('NetworkService', () => { - +describe('NetworkService Success tests', () => { let connection:MockConnection = null; let testUrl = "/test/url"; @@ -44,10 +43,9 @@ describe('NetworkService', () => { afterEach(() => { - expect(connection.request.url).toBe("/api"+testUrl); + expect(connection.request.url).toBe("/api" + testUrl); }); - it('should call GET', inject([NetworkService], (networkService) => { networkService.getJson(testUrl).then((res:Message) => { @@ -56,16 +54,20 @@ describe('NetworkService', () => { })); - it('should call POST', inject([NetworkService, MockBackend], (networkService) => { networkService.postJson(testUrl, testData).then((res:Message) => { expect(res.result).toBe(testResponse); }); expect(connection.request.text()).toBe(JSON.stringify(testData)); - })); + networkService.postJson(testUrl).then((res:Message) => { + expect(res.result).toBe(testResponse); + }); + expect(connection.request.text()).toBe(JSON.stringify({})); + })); + it('should call PUT', inject([NetworkService, MockBackend], (networkService) => { networkService.putJson(testUrl, testData).then((res:Message) => { @@ -74,8 +76,13 @@ describe('NetworkService', () => { expect(connection.request.text()).toBe(JSON.stringify(testData)); - })); + networkService.putJson(testUrl).then((res:Message) => { + expect(res.result).toBe(testResponse); + }); + expect(connection.request.text()).toBe(JSON.stringify({})); + + })); it('should call DELETE', inject([NetworkService, MockBackend], (networkService) => { @@ -83,6 +90,79 @@ describe('NetworkService', () => { expect(res.result).toBe(testResponse); }); })); - - }); + + +describe('NetworkService Fail tests', () => { + let connection:MockConnection = null; + + let testUrl = "/test/url"; + let testData = {data: "testData"}; + let testError = "testError"; + + beforeEachProviders(() => [ + MockBackend, + BaseRequestOptions, + provide(Http, { + useFactory: (backend, options) => { + return new Http(backend, options); + }, deps: [MockBackend, BaseRequestOptions] + }), + NetworkService + ]); + + beforeEach(inject([MockBackend], (backend) => { + + backend.connections.subscribe((c) => { + connection = c; + connection.mockError({name :"errorName",message:testError}); + + }); + })); + + afterEach(() => { + + expect(connection.request.url).toBe("/api" + testUrl); + }); + + it('should call GET with error', inject([NetworkService], (networkService) => { + + networkService.getJson(testUrl).then((res:Message) => { + expect(res).toBe(null); + }).catch((err) => { + expect(err).toBe(testError); + }); + + })); + + it('should call POST with error', inject([NetworkService, MockBackend], (networkService) => { + + networkService.postJson(testUrl, testData).then((res:Message) => { + expect(res).toBe(null); + }).catch((err) => { + expect(err).toBe(testError); + }); + expect(connection.request.text()).toBe(JSON.stringify(testData)); + })); + + it('should call PUT with error', inject([NetworkService, MockBackend], (networkService) => { + + networkService.putJson(testUrl, testData).then((res:Message) => { + expect(res).toBe(null); + }).catch((err) => { + expect(err).toBe(testError); + }); + + expect(connection.request.text()).toBe(JSON.stringify(testData)); + + })); + + it('should call DELETE with error', inject([NetworkService, MockBackend], (networkService) => { + + networkService.deleteJson(testUrl).then((res:Message) => { + expect(res).toBe(null); + }).catch((err) => { + expect(err).toBe(testError); + }); + })); +}); diff --git a/frontend/app/model/network/network.service.ts b/frontend/app/model/network/network.service.ts index 78245e38..ffa7dcfe 100644 --- a/frontend/app/model/network/network.service.ts +++ b/frontend/app/model/network/network.service.ts @@ -38,6 +38,7 @@ export class NetworkService{ public putJson(url:string, data:any = {}): Promise{ return this.callJson("put",url,data); } + public getJson(url:string): Promise{ return this.callJson("get",url); } @@ -48,7 +49,7 @@ export class NetworkService{ } private static handleError (error: any) { - // in a real world app, we may send the error to some remote logging infrastructure + // TODO: in a real world app, we may send the error to some remote logging infrastructure // instead of just logging it to the console console.error(error); return Promise.reject(error.message || error.json().error || 'Server error');