From e760e04a9ba946b45b65c68455eedcc2694f8fae Mon Sep 17 00:00:00 2001 From: Michael Courtin Date: Fri, 3 Dec 2021 11:59:18 +0100 Subject: [PATCH] fix: shutdown hook configuration is using wrong config key In the api-server constructor the evaluation for the activation / deactivation of the shutdown hook is based on the wrong configuration property so that it always is falling back to the default value and pre-configured value is not taken. Closes: #1619 Signed-off-by: Michael Courtin --- .../src/main/typescript/api-server.ts | 6 +- .../unit/config/api-server-config.test.ts | 84 +++++++++++++++++++ 2 files changed, 88 insertions(+), 2 deletions(-) create mode 100644 packages/cactus-cmd-api-server/src/test/typescript/unit/config/api-server-config.test.ts diff --git a/packages/cactus-cmd-api-server/src/main/typescript/api-server.ts b/packages/cactus-cmd-api-server/src/main/typescript/api-server.ts index 4c0e87f508..75dcda5cb3 100644 --- a/packages/cactus-cmd-api-server/src/main/typescript/api-server.ts +++ b/packages/cactus-cmd-api-server/src/main/typescript/api-server.ts @@ -109,8 +109,10 @@ export class ApiServer { throw new Error(`ApiServer#ctor options.config was falsy`); } - this.enableShutdownHook = Bools.isBooleanStrict(options.enableShutdownHook) - ? (options.enableShutdownHook as boolean) + this.enableShutdownHook = Bools.isBooleanStrict( + options.config.enableShutdownHook, + ) + ? (options.config.enableShutdownHook as boolean) : true; if (this.enableShutdownHook) { diff --git a/packages/cactus-cmd-api-server/src/test/typescript/unit/config/api-server-config.test.ts b/packages/cactus-cmd-api-server/src/test/typescript/unit/config/api-server-config.test.ts new file mode 100644 index 0000000000..9aa1c0abe5 --- /dev/null +++ b/packages/cactus-cmd-api-server/src/test/typescript/unit/config/api-server-config.test.ts @@ -0,0 +1,84 @@ +import "jest-extended"; +import { + ApiServer, + // AuthorizationProtocol, + ConfigService, +} from "../../../../main/typescript/public-api"; + +describe("api-server shutdown-hook configuration tests", () => { + // create a config service as base for the following UTs + const configService = new ConfigService(); + + it("enables the shutdown hook based on schema-default", async () => { + const expectedResult = true; + const apiServerOptions = await configService.newExampleConfig(); + const config = await configService.newExampleConfigConvict( + apiServerOptions, + ); + + const apiServer = new ApiServer({ + config: config.getProperties(), + }); + + // check apiServerOptions + expect(apiServerOptions).not.toBeUndefined(); + expect(apiServerOptions.enableShutdownHook).toBe(expectedResult); + + // check apiServer + expect(apiServer).toBeTruthy(); + const result = apiServer["enableShutdownHook"]; + expect(result).toBe(expectedResult); + }); + + it("disables the shutdown hook based on the config value set to false", async () => { + const expectedResult = false; + const apiServerOptions = await configService.newExampleConfig(); + + // disable shutdown hook + apiServerOptions.enableShutdownHook = false; + + const config = await configService.newExampleConfigConvict( + apiServerOptions, + true, + ); + + const apiServer = new ApiServer({ + config: config.getProperties(), + }); + + // check apiServerOptions + expect(apiServerOptions).not.toBeUndefined(); + expect(apiServerOptions.enableShutdownHook).toBe(expectedResult); + + // check apiServer + expect(apiServer).toBeTruthy(); + const result = apiServer["enableShutdownHook"]; + expect(result).toBe(expectedResult); + }); + + it("enables the shutdown hook based on the config value set to true", async () => { + const expectedResult = true; + const apiServerOptions = await configService.newExampleConfig(); + + // disable shutdown hook + apiServerOptions.enableShutdownHook = true; + + const config = await configService.newExampleConfigConvict( + apiServerOptions, + true, + ); + + const apiServer = new ApiServer({ + config: config.getProperties(), + }); + + // check apiServerOptions + expect(apiServerOptions).not.toBeUndefined(); + expect(apiServerOptions.enableShutdownHook).toBe(expectedResult); + + // check apiServer + expect(apiServer).toBeTruthy(); + const result = apiServer["enableShutdownHook"]; + expect(result).toBe(expectedResult); + }); +});