From 295be8b3c0d0d26f3abcde2ad60eff345fc7592d Mon Sep 17 00:00:00 2001 From: helkv Date: Mon, 5 Jun 2023 10:17:54 +0200 Subject: [PATCH 1/5] ItemService Test: Mock PropertiesService properties --- src/app/core/services/impl/item.service.spec.ts | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) diff --git a/src/app/core/services/impl/item.service.spec.ts b/src/app/core/services/impl/item.service.spec.ts index 2ddd2d4..74e1b0c 100644 --- a/src/app/core/services/impl/item.service.spec.ts +++ b/src/app/core/services/impl/item.service.spec.ts @@ -4,17 +4,9 @@ import { ItemService } from './item.service'; import { PropertiesService } from '../properties.service'; import { HttpClientTestingModule } from '@angular/common/http/testing'; -import * as props from '../../../../assets/properties.json'; - -const mocked_props = props; - -export class PropsFactory { - public properties: any = mocked_props; -} - describe('ItemService', () => { let service: ItemService; - let props: PropertiesService; + const mockedIngeRestUri = "testURL"; beforeEach(() => { TestBed.configureTestingModule({ @@ -25,12 +17,11 @@ describe('ItemService', () => { ItemService, { provide: PropertiesService, - useClass: PropsFactory + useValue: {properties: {"inge_rest_uri": mockedIngeRestUri}} } ] }); service = TestBed.inject(ItemService); - props = TestBed.inject(PropertiesService); }); it('should be created', () => { From 6a5634dc588968d514d4cde5bfb566ae12bf1717 Mon Sep 17 00:00:00 2001 From: helkv Date: Mon, 5 Jun 2023 10:27:41 +0200 Subject: [PATCH 2/5] Small fix for ItemService.getItem() --- src/app/core/services/impl/item.service.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/app/core/services/impl/item.service.ts b/src/app/core/services/impl/item.service.ts index 7413442..114427d 100644 --- a/src/app/core/services/impl/item.service.ts +++ b/src/app/core/services/impl/item.service.ts @@ -47,7 +47,7 @@ export class ItemService extends IngeCrudService { * @returns an item */ getItem(itemId: string, token?: string): Observable { - const path = this.createItem + '/' + itemId; + const path = this.contextPath + '/' + itemId; return this.get(path, token); } From 1b53f544a4a7c26df96405f6b8b21cfc85480c86 Mon Sep 17 00:00:00 2001 From: helkv Date: Mon, 5 Jun 2023 10:32:06 +0200 Subject: [PATCH 3/5] Add ItemService.getItem() Test --- .../core/services/impl/item.service.spec.ts | 21 +++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/src/app/core/services/impl/item.service.spec.ts b/src/app/core/services/impl/item.service.spec.ts index 74e1b0c..7c803ef 100644 --- a/src/app/core/services/impl/item.service.spec.ts +++ b/src/app/core/services/impl/item.service.spec.ts @@ -2,10 +2,11 @@ import { TestBed } from '@angular/core/testing'; import { ItemService } from './item.service'; import { PropertiesService } from '../properties.service'; -import { HttpClientTestingModule } from '@angular/common/http/testing'; +import {HttpClientTestingModule, HttpTestingController} from '@angular/common/http/testing'; describe('ItemService', () => { - let service: ItemService; + let itemService: ItemService; + let controller: HttpTestingController; const mockedIngeRestUri = "testURL"; beforeEach(() => { @@ -21,10 +22,22 @@ describe('ItemService', () => { } ] }); - service = TestBed.inject(ItemService); + itemService = TestBed.inject(ItemService); + controller = TestBed.inject(HttpTestingController); }); it('should be created', () => { - expect(service).toBeTruthy(); + expect(itemService).toBeTruthy(); + }); + + it('getItem request', () => { + const itemId = '1'; + const expectedRequestUrl = mockedIngeRestUri + '/items/' + itemId; + + itemService.getItem(itemId).subscribe(); + + const request = controller.expectOne(expectedRequestUrl); + expect(request.request.method).toEqual('GET'); + controller.verify(); }); }); From b3bb6b2834bf0b8ee57d385d1d7cbfbe32162829 Mon Sep 17 00:00:00 2001 From: helkv Date: Mon, 5 Jun 2023 11:36:28 +0200 Subject: [PATCH 4/5] Add ItemService.createItem() Test --- .../core/services/impl/item.service.spec.ts | 20 ++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/src/app/core/services/impl/item.service.spec.ts b/src/app/core/services/impl/item.service.spec.ts index 7c803ef..b958ea2 100644 --- a/src/app/core/services/impl/item.service.spec.ts +++ b/src/app/core/services/impl/item.service.spec.ts @@ -3,6 +3,7 @@ import { TestBed } from '@angular/core/testing'; import { ItemService } from './item.service'; import { PropertiesService } from '../properties.service'; import {HttpClientTestingModule, HttpTestingController} from '@angular/common/http/testing'; +import {ItemVersionVO} from "../../model/model"; describe('ItemService', () => { let itemService: ItemService; @@ -26,11 +27,24 @@ describe('ItemService', () => { controller = TestBed.inject(HttpTestingController); }); - it('should be created', () => { - expect(itemService).toBeTruthy(); + it('#createItem should send a correct HTTP-Request', () => { + const item = { + message : 'testMessage' + }; + const token = 'token'; + const expectedRequestUrl = mockedIngeRestUri + '/items'; + + itemService.createItem(item, token).subscribe(); + + const request = controller.expectOne(expectedRequestUrl); + expect(request.request.method).toEqual('POST'); + expect(request.request.headers.get('Authorization')).toEqual('token'); + expect(request.request.headers.get('Content-Type')).toEqual('application/json'); + expect(request.request.body).toEqual('{"message":"testMessage"}'); + controller.verify(); }); - it('getItem request', () => { + it('#getItem should send a correct HTTP-Request', () => { const itemId = '1'; const expectedRequestUrl = mockedIngeRestUri + '/items/' + itemId; From bfc456bdb157ede17b515c727a416ab26cac81b6 Mon Sep 17 00:00:00 2001 From: helkv Date: Mon, 5 Jun 2023 16:26:27 +0200 Subject: [PATCH 5/5] ItemService Test: Add error test case for getItem() --- .../core/services/impl/item.service.spec.ts | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/app/core/services/impl/item.service.spec.ts b/src/app/core/services/impl/item.service.spec.ts index b958ea2..f0ece30 100644 --- a/src/app/core/services/impl/item.service.spec.ts +++ b/src/app/core/services/impl/item.service.spec.ts @@ -54,4 +54,29 @@ describe('ItemService', () => { expect(request.request.method).toEqual('GET'); controller.verify(); }); + + it('#getItem should throw an error, if the HTTP Request returns an error response', () => { + const itemId = '1'; + const expectedRequestUrl = mockedIngeRestUri + '/items/' + itemId; + const mockedErrorResponse = { status: 500, statusText: 'Internal Server Error' }; + let actualError: Error | undefined; + + itemService.getItem(itemId).subscribe({ + next: () => fail('next must not be called'), + error: (error) => actualError = error, + complete: () => fail('complete must not be called') + }); + + const request = controller.expectOne(expectedRequestUrl); + expect(request.request.method).toEqual('GET'); + request.flush('', { status: mockedErrorResponse.status, statusText: mockedErrorResponse.statusText}) + if (!actualError) { + throw new Error('Error needs to be defined'); + } + expect(actualError.name).toBe('Error'); + const actualErrorMessage = JSON.parse(actualError.message); + expect(actualErrorMessage.status).toBe(mockedErrorResponse.status); + expect(actualErrorMessage.statusText).toBe(mockedErrorResponse.statusText); + controller.verify(); + }); });