Skip to content

Commit

Permalink
Added HTTP API Router unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
u-hubar committed Aug 18, 2023
1 parent 9c2c608 commit 2336220
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 1 deletion.
1 change: 1 addition & 0 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ module.exports = {
{
files: ['*-mock.js', '*.test.js'],
rules: {
'no-empty-function': 'off',
'no-unused-vars': 'off',
},
},
Expand Down
2 changes: 1 addition & 1 deletion src/controllers/http-api/http-api-router.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { HTTP_API_ROUTES } from '../../constants/constants.js';

class HttpApiRouter {
constructor(ctx) {
this.config = ctx.config;
this.httpClientModuleManager = ctx.httpClientModuleManager;

this.apiRoutes = HTTP_API_ROUTES;
Expand All @@ -19,6 +18,7 @@ class HttpApiRouter {
const versionedController = `${stringUtil.toCamelCase(
operation,
)}HttpApiController${stringUtil.capitalize(version)}`;

this[versionedController] = ctx[versionedController];
}
}
Expand Down
80 changes: 80 additions & 0 deletions test/unit/api/http-api-router.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import { beforeEach, describe, it } from 'mocha';
import { expect } from 'chai';
import sinon from 'sinon';
import HttpApiRouter from '../../../src/controllers/http-api/http-api-router.js';
import JsonSchemaServiceMock from '../mock/json-schema-service-mock.js';
import HttpClientModuleManagerMock from '../mock/http-client-module-manager-mock.js';
import { HTTP_API_ROUTES } from '../../../src/constants/constants.js';

describe('HTTP API Router test', async () => {
let httpApiRouter;
const controllerMocks = {};

beforeEach(() => {
// Mock Controllers
Object.keys(HTTP_API_ROUTES).forEach((version) => {
Object.keys(HTTP_API_ROUTES[version]).forEach((operation) => {
const versionedController = `${operation}HttpApiController${
version.charAt(1).toUpperCase() + version.slice(2)
}`;
controllerMocks[versionedController] = { handleRequest: sinon.stub() };
});
});

// Mock context
const ctx = {
httpClientModuleManager: new HttpClientModuleManagerMock(),
jsonSchemaService: new JsonSchemaServiceMock(),
...controllerMocks,
};

// Initialize HttpApiRouter with mocks
httpApiRouter = new HttpApiRouter(ctx);
});

it('Router has all defined routes', async () => {
// Extract unique HTTP methods present across all versions
const httpMethods = new Set();
Object.values(HTTP_API_ROUTES).forEach((routes) => {
Object.values(routes).forEach((route) => {
httpMethods.add(route.method);
});
});

// Create spies for each extracted HTTP method on each router instance and httpClientModuleManager
const spies = {};
Object.keys(HTTP_API_ROUTES).forEach((version) => {
spies[version] = {};
Array.from(httpMethods).forEach((method) => {
spies[version][method] = sinon.spy(httpApiRouter.routers[version], method);
});
});
const httpClientModuleManagerUseSpy = sinon.spy(
httpApiRouter.httpClientModuleManager,
'use',
);

// Initialize the routes
await httpApiRouter.initialize();

// Validate each route
Object.entries(HTTP_API_ROUTES).forEach(([version, routes]) => {
expect(httpClientModuleManagerUseSpy.calledWith(`/${version}`)).to.equal(true);

Object.values(routes).forEach((routeDetails) => {
const { method, path } = routeDetails;
expect(spies[version][method].calledWith(path)).to.equal(true);
});
});
expect(httpClientModuleManagerUseSpy.calledWith('/latest')).to.equal(true);
expect(httpClientModuleManagerUseSpy.calledWith('/')).to.equal(true);

// Restore all spies
Object.values(spies).forEach((versionSpies) => {
Object.values(versionSpies).forEach((spy) => {
spy.restore();
});
});
httpClientModuleManagerUseSpy.restore();
});
});
25 changes: 25 additions & 0 deletions test/unit/mock/http-client-module-manager-mock.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import express from 'express';

class HttpClientModuleManagerMock {
createRouterInstance() {
return express.Router();
}

initializeBeforeMiddlewares() {}

async listen() {}

use(path, callback) {}

get() {}

post() {}

selectMiddlewares(options) {
return [];
}

initializeAfterMiddlewares() {}
}

export default HttpClientModuleManagerMock;
3 changes: 3 additions & 0 deletions test/unit/mock/json-schema-service-mock.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class JsonSchemaServiceMock {}

export default JsonSchemaServiceMock;

0 comments on commit 2336220

Please sign in to comment.