diff --git a/Server/.nycrc b/Server/.nycrc index da9bccbfc..d6406bf7e 100644 --- a/Server/.nycrc +++ b/Server/.nycrc @@ -1,6 +1,6 @@ { "all": true, - "include": ["**/*.js"], + "include": ["controllers/*.js"], "exclude": ["**/*.test.js"], "reporter": ["html", "text", "lcov"], "sourceMap": false, diff --git a/Server/controllers/queueController.js b/Server/controllers/queueController.js index 341c4b76b..3151816be 100644 --- a/Server/controllers/queueController.js +++ b/Server/controllers/queueController.js @@ -1,13 +1,15 @@ const { handleError } = require("./controllerUtils"); - +const { errorMessages, successMessages } = require("../utils/messages"); const SERVICE_NAME = "JobQueueController"; const getMetrics = async (req, res, next) => { try { const metrics = await req.jobQueue.getMetrics(); - res - .status(200) - .json({ success: true, msg: "Metrics retrieved", data: metrics }); + res.status(200).json({ + success: true, + msg: successMessages.QUEUE_GET_METRICS, + data: metrics, + }); } catch (error) { next(handleError(error, SERVICE_NAME, "getMetrics")); return; @@ -17,7 +19,11 @@ const getMetrics = async (req, res, next) => { const getJobs = async (req, res, next) => { try { const jobs = await req.jobQueue.getJobStats(); - return res.status(200).json({ jobs }); + return res.status(200).json({ + success: true, + msg: successMessages.QUEUE_GET_METRICS, + data: jobs, + }); } catch (error) { next(handleError(error, SERVICE_NAME, "getJobs")); return; @@ -27,7 +33,10 @@ const getJobs = async (req, res, next) => { const addJob = async (req, res, next) => { try { await req.jobQueue.addJob(Math.random().toString(36).substring(7)); - return res.send("Added job"); + return res.status(200).json({ + success: true, + msg: successMessages.QUEUE_ADD_JOB, + }); } catch (error) { next(handleError(error, SERVICE_NAME, "addJob")); return; @@ -37,7 +46,9 @@ const addJob = async (req, res, next) => { const obliterateQueue = async (req, res, next) => { try { await req.jobQueue.obliterate(); - return res.status(200).send("Obliterated queue"); + return res + .status(200) + .json({ success: true, msg: successMessages.QUEUE_OBLITERATE }); } catch (error) { next(handleError(error, SERVICE_NAME, "obliterateQueue")); return; diff --git a/Server/tests/controllers/queueController.test.js b/Server/tests/controllers/queueController.test.js new file mode 100644 index 000000000..476905b54 --- /dev/null +++ b/Server/tests/controllers/queueController.test.js @@ -0,0 +1,168 @@ +const { afterEach } = require("node:test"); +const { + getMetrics, + getJobs, + addJob, + obliterateQueue, +} = require("../../controllers/queueController"); +const SERVICE_NAME = "JobQueueController"; + +const { errorMessages, successMessages } = require("../../utils/messages"); +const sinon = require("sinon"); + +describe("Queue Controller - getMetrics", () => { + beforeEach(() => { + req = { + headers: {}, + params: {}, + body: {}, + db: {}, + jobQueue: { + getMetrics: sinon.stub(), + }, + }; + res = { + status: sinon.stub().returnsThis(), + json: sinon.stub(), + }; + next = sinon.stub(); + handleError = sinon.stub(); + }); + afterEach(() => { + sinon.restore(); + }); + it("should throw an error if getMetrics throws an error", async () => { + req.jobQueue.getMetrics.throws(new Error("getMetrics error")); + await getMetrics(req, res, next); + expect(next.firstCall.args[0]).to.be.an("error"); + expect(next.firstCall.args[0].message).to.equal("getMetrics error"); + }); + + it("should return a success message and data if getMetrics is successful", async () => { + const data = { data: "metrics" }; + req.jobQueue.getMetrics.returns(data); + await getMetrics(req, res, next); + expect(res.status.firstCall.args[0]).to.equal(200); + expect(res.json.firstCall.args[0]).to.deep.equal({ + success: true, + msg: successMessages.QUEUE_GET_METRICS, + data, + }); + }); +}); + +describe("Queue Controller - getJobs", () => { + beforeEach(() => { + req = { + headers: {}, + params: {}, + body: {}, + db: {}, + jobQueue: { + getJobStats: sinon.stub(), + }, + }; + res = { + status: sinon.stub().returnsThis(), + json: sinon.stub(), + }; + next = sinon.stub(); + handleError = sinon.stub(); + }); + afterEach(() => { + sinon.restore(); + }); + it("should reject with an error if getJobs throws an error", async () => { + req.jobQueue.getJobStats.throws(new Error("getJobs error")); + await getJobs(req, res, next); + expect(next.firstCall.args[0]).to.be.an("error"); + expect(next.firstCall.args[0].message).to.equal("getJobs error"); + }); + it("should return a success message and data if getJobs is successful", async () => { + const data = { data: "jobs" }; + req.jobQueue.getJobStats.returns(data); + await getJobs(req, res, next); + expect(res.status.firstCall.args[0]).to.equal(200); + expect(res.json.firstCall.args[0]).to.deep.equal({ + success: true, + msg: successMessages.QUEUE_GET_METRICS, + data, + }); + }); +}); + +describe("Queue Controller - addJob", () => { + beforeEach(() => { + req = { + headers: {}, + params: {}, + body: {}, + db: {}, + jobQueue: { + addJob: sinon.stub(), + }, + }; + res = { + status: sinon.stub().returnsThis(), + json: sinon.stub(), + }; + next = sinon.stub(); + handleError = sinon.stub(); + }); + afterEach(() => { + sinon.restore(); + }); + it("should reject with an error if addJob throws an error", async () => { + req.jobQueue.addJob.throws(new Error("addJob error")); + await addJob(req, res, next); + expect(next.firstCall.args[0]).to.be.an("error"); + expect(next.firstCall.args[0].message).to.equal("addJob error"); + }); + it("should return a success message if addJob is successful", async () => { + req.jobQueue.addJob.resolves(); + await addJob(req, res, next); + expect(res.status.firstCall.args[0]).to.equal(200); + expect(res.json.firstCall.args[0]).to.deep.equal({ + success: true, + msg: successMessages.QUEUE_ADD_JOB, + }); + }); +}); + +describe("Queue Controller - obliterateQueue", () => { + beforeEach(() => { + req = { + headers: {}, + params: {}, + body: {}, + db: {}, + jobQueue: { + obliterate: sinon.stub(), + }, + }; + res = { + status: sinon.stub().returnsThis(), + json: sinon.stub(), + }; + next = sinon.stub(); + handleError = sinon.stub(); + }); + afterEach(() => { + sinon.restore(); + }); + it("should reject with an error if obliterateQueue throws an error", async () => { + req.jobQueue.obliterate.throws(new Error("obliterateQueue error")); + await obliterateQueue(req, res, next); + expect(next.firstCall.args[0]).to.be.an("error"); + expect(next.firstCall.args[0].message).to.equal("obliterateQueue error"); + }); + it("should return a success message if obliterateQueue is successful", async () => { + req.jobQueue.obliterate.resolves(); + await obliterateQueue(req, res, next); + expect(res.status.firstCall.args[0]).to.equal(200); + expect(res.json.firstCall.args[0]).to.deep.equal({ + success: true, + msg: successMessages.QUEUE_OBLITERATE, + }); + }); +}); diff --git a/Server/utils/messages.js b/Server/utils/messages.js index 16b2f7883..d685e7326 100644 --- a/Server/utils/messages.js +++ b/Server/utils/messages.js @@ -86,6 +86,12 @@ const successMessages = { MONITOR_CERTIFICATE: "Got monitor certificate successfully", MONITOR_DEMO_ADDED: "Successfully added demo monitors", + // Queue Controller + QUEUE_GET_METRICS: "Got metrics successfully", + QUEUE_GET_METRICS: "Got job stats successfully", + QUEUE_ADD_JOB: "Job added successfully", + QUEUE_OBLITERATE: "Queue obliterated", + //Job Queue JOB_QUEUE_DELETE_JOB: "Job removed successfully", JOB_QUEUE_OBLITERATE: "Queue OBLITERATED!!!",