diff --git a/src/service/ApplicationService/index.spec.ts b/src/service/ApplicationService/index.spec.ts index c0a1e8c79..79cac56bd 100644 --- a/src/service/ApplicationService/index.spec.ts +++ b/src/service/ApplicationService/index.spec.ts @@ -4,9 +4,72 @@ import ApplicationService from '.'; describe('ApplicationService', () => { let service: ApplicationService; + const mockApplication = { id: 123, name: 'TestApp' } as Application; beforeEach(() => { service = new ApplicationService(); + global.fetch = jest.fn(); + }); + + afterEach(() => { + jest.clearAllMocks(); + }); + + it('should return the application when the fetch is successful', async () => { + (fetch as jest.Mock).mockResolvedValueOnce({ + ok: true, + json: jest.fn().mockResolvedValueOnce(mockApplication), + }); + + const result = await service.findOneByName('TestApp'); + + expect(fetch).toHaveBeenCalledWith('/applications/findByName/TestApp', expect.objectContaining({ + method: 'GET', + headers: expect.any(Object), + })); + expect(result).toEqual(mockApplication); + }); + + it('should throw an error when the response is not ok', async () => { + (fetch as jest.Mock).mockResolvedValueOnce({ + ok: false, + status: 404, + }); + + await expect(service.findOneByName('NonExistentApp')).rejects.toThrow('HTTP error status: 404'); + expect(fetch).toHaveBeenCalledWith('/applications/findByName/NonExistentApp', expect.objectContaining({ + method: 'GET', + headers: expect.any(Object), + })); + }); + + it('should throw an error when fetch fails', async () => { + (fetch as jest.Mock).mockRejectedValueOnce(new Error('Network error')); + + await expect(service.findOneByName('TestApp')).rejects.toThrow('Error while requesting a single entity: Error: Network error'); + expect(fetch).toHaveBeenCalledWith('/applications/findByName/TestApp', expect.objectContaining({ + method: 'GET', + headers: expect.any(Object), + })); + }); + + it('should return the application when the fetch is successful', async () => { + const mockApplication = { id: 123, name: 'TestApp' } as Application; + + (fetch as jest.Mock).mockResolvedValueOnce({ + ok: true, + json: jest.fn().mockResolvedValueOnce(mockApplication), + }); + + const result = await service.findOneByName('TestApp'); + + expect(fetch).toHaveBeenCalledWith('/applications/findByName/TestApp', expect.objectContaining({ + method: 'GET', + headers: expect.any(Object), + })); + + expect(result).toEqual(mockApplication); + expect(result.name).toBe('TestApp'); }); it('is defined', () => { diff --git a/src/service/ApplicationService/index.ts b/src/service/ApplicationService/index.ts index dc88bd36c..cb3753c6a 100644 --- a/src/service/ApplicationService/index.ts +++ b/src/service/ApplicationService/index.ts @@ -1,4 +1,5 @@ import Application from '../../model/Application'; +import { getBearerTokenHeader } from '../../security/getBearerTokenHeader'; import GenericEntityService, { GenericEntityServiceOpts } from '../GenericEntityService'; export class ApplicationService extends GenericEntityService { @@ -9,6 +10,26 @@ export class ApplicationService extends GenericEntityServ super(opts); } + async findOneByName(name: string, fetchOpts?: RequestInit): Promise { + try { + const response = await fetch(`${this.basePath}/findByName/${name}`, { + method: 'GET', + headers: { + ...getBearerTokenHeader(this.keycloak) + }, + ...fetchOpts + }); + + if (!response.ok) { + throw new Error(`HTTP error status: ${response.status}`); + } + + return await response.json(); + } catch (error) { + throw new Error(`Error while requesting a single entity: ${error}`); + } + } + } export default ApplicationService;