diff --git a/app/controllers/test/supplementary.controller.js b/app/controllers/test/supplementary.controller.js index dd009cc414..ce00a5aceb 100644 --- a/app/controllers/test/supplementary.controller.js +++ b/app/controllers/test/supplementary.controller.js @@ -1,13 +1,13 @@ 'use strict' -const TestSupplementaryService = require('../../services/test/supplementary.service.js') +const SupplementaryService = require('../../services/test/supplementary.service.js') -class TestSupplementaryController { +class SupplementaryController { static async index (_request, h) { - const result = await TestSupplementaryService.go() + const result = await SupplementaryService.go() return h.response(result).code(200) } } -module.exports = TestSupplementaryController +module.exports = SupplementaryController diff --git a/app/routes/test.routes.js b/app/routes/test.routes.js index f4d486fca7..63b769583d 100644 --- a/app/routes/test.routes.js +++ b/app/routes/test.routes.js @@ -1,12 +1,12 @@ 'use strict' -const TestSupplementaryController = require('../controllers/test/supplementary.controller') +const SupplementaryController = require('../controllers/test/supplementary.controller') const routes = [ { method: 'GET', path: '/test/supplementary', - handler: TestSupplementaryController.index, + handler: SupplementaryController.index, options: { description: 'Test endpoint which returns all selected charge versions' } diff --git a/app/services/test/supplementary.service.js b/app/services/test/supplementary.service.js index 0b06276d09..937cd1c666 100644 --- a/app/services/test/supplementary.service.js +++ b/app/services/test/supplementary.service.js @@ -1,24 +1,35 @@ 'use strict' /** - * @module TestSupplementaryService + * @module SupplementaryService */ +const { db } = require('../../../db/db') + /** * Returns charge versions selected for supplementary billing * At present this returns a set response until further development */ -class TestSupplementaryService { + +// Format into the response and return the data +class SupplementaryService { static async go () { + const chargeVersions = await this._fetchChargeVersions() const response = { - chargeVersions: [ - { id: '986d5b14-8686-429e-9ae7-1164c1300f8d', licenceRef: 'AT/SROC/SUPB/01' }, - { id: 'ca0e4a77-bb13-4eef-a1a1-2ccf9e302cc4', licenceRef: 'AT/SROC/SUPB/03' } - ] + chargeVersions } return response } + + static async _fetchChargeVersions () { + const chargeVersions = db.table('water.charge_versions') + .where('scheme', 'sroc') + .select('chargeVersionId') + .select('licenceRef') + + return chargeVersions + } } -module.exports = TestSupplementaryService +module.exports = SupplementaryService diff --git a/test/controllers/test/supplementary.controller.test.js b/test/controllers/test/supplementary.controller.test.js index 9530b03634..9627fabb12 100644 --- a/test/controllers/test/supplementary.controller.test.js +++ b/test/controllers/test/supplementary.controller.test.js @@ -8,10 +8,13 @@ const Sinon = require('sinon') const { describe, it, beforeEach, after } = exports.lab = Lab.script() const { expect } = Code +// Things we need to stub +const SupplementaryService = require('../../../app/services/test/supplementary.service.js') + // For running our service const { init } = require('../../../app/server') -describe('Test supplementary controller', () => { +describe('Supplementary controller', () => { let server beforeEach(async () => { @@ -22,29 +25,23 @@ describe('Test supplementary controller', () => { Sinon.restore() }) - describe('Returning selected charge versions: GET /test/supplementary', () => { - let response - + describe('GET /test/supplementary', () => { const options = { method: 'GET', url: '/test/supplementary' } + let response + beforeEach(async () => { - response = await server.inject(options) - }) + Sinon.stub(SupplementaryService, 'go').resolves({ chargeVersions: [] }) - it('returns a 200 status code', async () => { - expect(response.statusCode).to.equal(200) + response = await server.inject(options) }) - it('returns a JSON response', async () => { - const jsonBody = JSON.parse(response.payload) - expect(jsonBody).to.equal({ - chargeVersions: [ - { id: '986d5b14-8686-429e-9ae7-1164c1300f8d', licenceRef: 'AT/SROC/SUPB/01' }, - { id: 'ca0e4a77-bb13-4eef-a1a1-2ccf9e302cc4', licenceRef: 'AT/SROC/SUPB/03' } - ] + describe('when the request is valid', () => { + it('returns success status 200', async () => { + expect(response.statusCode).to.equal(200) }) }) }) diff --git a/test/services/test/supplementary.service.test.js b/test/services/test/supplementary.service.test.js index c136146312..34248fb71b 100644 --- a/test/services/test/supplementary.service.test.js +++ b/test/services/test/supplementary.service.test.js @@ -4,20 +4,49 @@ const Lab = require('@hapi/lab') const Code = require('@hapi/code') -const { describe, it } = exports.lab = Lab.script() +const { describe, it, beforeEach } = exports.lab = Lab.script() const { expect } = Code +// Test helpers +const ChargeVersionHelper = require('../../support/helpers/charge_version.helper') +const DatabaseHelper = require('../../support/helpers/database.helper') + // Thing under test -const TestSupplementaryService = require('../../../app/services/test/supplementary.service.js') - -describe('Test supplementary service', () => { - it('returns a set response', async () => { - const result = await TestSupplementaryService.go() - expect(result).to.equal({ - chargeVersions: [ - { id: '986d5b14-8686-429e-9ae7-1164c1300f8d', licenceRef: 'AT/SROC/SUPB/01' }, - { id: 'ca0e4a77-bb13-4eef-a1a1-2ccf9e302cc4', licenceRef: 'AT/SROC/SUPB/03' } - ] +const SupplementaryService = require('../../../app/services/test/supplementary.service.js') + +describe('Supplementary service', () => { + const testData = { + sroc: { licence_ref: '01/123', scheme: 'sroc' }, + alcs: { licence_ref: '01/456', scheme: 'alcs' } + } + let testRecords + + beforeEach(async () => { + await DatabaseHelper.clean() + }) + + describe('when there are charge versions to be included in supplimentery billing', () => { + beforeEach(async () => { + testRecords = await ChargeVersionHelper.add([testData.sroc, testData.alcs]) + }) + + it('returns only those that match', async () => { + const result = await SupplementaryService.go() + + expect(result.chargeVersions.length).to.equal(1) + expect(result.chargeVersions[0].charge_version_id).to.equal(testRecords[0].charge_version_id) + }) + }) + + describe('when there are no charge versions to be included in supplimentery billing', () => { + beforeEach(async () => { + testRecords = await ChargeVersionHelper.add(testData.alcs) + }) + + it('returns no matches', async () => { + const result = await SupplementaryService.go() + + expect(result.chargeVersions.length).to.equal(0) }) }) }) diff --git a/test/support/helpers/charge_version.helper.js b/test/support/helpers/charge_version.helper.js new file mode 100644 index 0000000000..3fdd2cc5bf --- /dev/null +++ b/test/support/helpers/charge_version.helper.js @@ -0,0 +1,19 @@ +'use strict' + +/** + * @module ChargeVersionHelper + */ + +const { db } = require('../../../db/db') + +class ChargeVersionHelper { + static async add (data) { + const result = await db.table('water.charge_versions') + .insert(data) + .returning('charge_version_id') + + return result + } +} + +module.exports = ChargeVersionHelper