|
| 1 | +/******************************************************************************** |
| 2 | + * Copyright (c) 2025 Contributors to the Eclipse Foundation |
| 3 | + * |
| 4 | + * See the NOTICE file(s) distributed with this work for additional |
| 5 | + * information regarding copyright ownership. |
| 6 | + * |
| 7 | + * This program and the accompanying materials are made available under the |
| 8 | + * terms of the Eclipse Public License v. 2.0 which is available at |
| 9 | + * http://www.eclipse.org/legal/epl-2.0, or the W3C Software Notice and |
| 10 | + * Document License (2015-05-13) which is available at |
| 11 | + * https://www.w3.org/Consortium/Legal/2015/copyright-software-and-document. |
| 12 | + * |
| 13 | + * SPDX-License-Identifier: EPL-2.0 OR W3C-20150513 |
| 14 | + ********************************************************************************/ |
| 15 | + |
| 16 | +import { suite, test } from "@testdeck/mocha"; |
| 17 | +import { should, expect, use as chaiUse } from "chai"; |
| 18 | +import { buildConfig, buildConfigFromFile, defaultConfiguration, Configuration } from "../src/configuration"; |
| 19 | +import Ajv, { ValidateFunction } from "ajv"; |
| 20 | +import ConfigSchema from "../src/generated/wot-servient-schema.conf"; |
| 21 | +import chaiAsPromised from "chai-as-promised"; |
| 22 | +import { writeFileSync, unlinkSync } from "fs"; |
| 23 | +import { join } from "path"; |
| 24 | +import { ValidationError } from "ajv"; |
| 25 | + |
| 26 | +should(); |
| 27 | +chaiUse(chaiAsPromised); |
| 28 | + |
| 29 | +@suite("Configuration management") |
| 30 | +class ConfigurationTest { |
| 31 | + private static validator: ValidateFunction<Configuration>; |
| 32 | + private testFilePath!: string; |
| 33 | + |
| 34 | + static before() { |
| 35 | + const ajv = new Ajv({ strict: true, allErrors: true }); |
| 36 | + ConfigurationTest.validator = ajv.compile(ConfigSchema) as ValidateFunction<Configuration>; |
| 37 | + } |
| 38 | + |
| 39 | + before() { |
| 40 | + this.testFilePath = join(__dirname, "./resources", "test-config-" + Date.now() + ".json"); |
| 41 | + } |
| 42 | + |
| 43 | + after() { |
| 44 | + try { |
| 45 | + unlinkSync(this.testFilePath); |
| 46 | + } catch { |
| 47 | + // File may not exist |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + @test async "should use default configuration when none provided"() { |
| 52 | + const result = await buildConfig({}, defaultConfiguration, {}, ConfigurationTest.validator); |
| 53 | + |
| 54 | + expect(result).to.have.property("http"); |
| 55 | + expect(result.http.port).to.equal(8080); |
| 56 | + expect(result.coap.port).to.equal(5683); |
| 57 | + expect(result.logLevel).to.equal("warn"); |
| 58 | + } |
| 59 | + |
| 60 | + @test async "should handle credentials in config"() { |
| 61 | + const config = { ...defaultConfiguration, credentials: { THING_ID_1: { username: "user", password: "pass" } } }; |
| 62 | + const result = await buildConfig({}, config, {}, ConfigurationTest.validator); |
| 63 | + |
| 64 | + expect(result.credentials).to.have.property("THING_ID_1"); |
| 65 | + } |
| 66 | + |
| 67 | + @test async "should merge environment variables with defaults"() { |
| 68 | + const env = { HTTP_PORT: "9000" }; |
| 69 | + const result = await buildConfig({}, defaultConfiguration, env, ConfigurationTest.validator); |
| 70 | + |
| 71 | + expect(result.http.port).to.equal(9000); |
| 72 | + } |
| 73 | + |
| 74 | + @test async "should apply config parameters"() { |
| 75 | + const options = { configParams: { http: { port: 8888 } } }; |
| 76 | + const result = await buildConfig(options, defaultConfiguration, {}, ConfigurationTest.validator); |
| 77 | + |
| 78 | + expect(result.http.port).to.equal(8888); |
| 79 | + } |
| 80 | + |
| 81 | + @test async "should merge environment variables and config parameters"() { |
| 82 | + const env = { HTTP_PORT: "9000" }; |
| 83 | + const options = { configParams: { coap: { port: 6000 } } }; |
| 84 | + const result = await buildConfig(options, defaultConfiguration, env, ConfigurationTest.validator); |
| 85 | + |
| 86 | + expect(result.http.port).to.equal(9000); |
| 87 | + expect(result.coap.port).to.equal(6000); |
| 88 | + } |
| 89 | + |
| 90 | + @test "should validate merged configuration"() { |
| 91 | + const options = { configParams: { http: { port: "invalid" } } }; |
| 92 | + |
| 93 | + expect(buildConfig(options, defaultConfiguration, {}, ConfigurationTest.validator)).to.eventually.throw( |
| 94 | + ValidationError |
| 95 | + ); |
| 96 | + } |
| 97 | + |
| 98 | + @test async "should apply default values to provided config"() { |
| 99 | + const customConfig = { http: { port: 8888 } }; |
| 100 | + const result = await buildConfig({}, customConfig, {}, ConfigurationTest.validator); |
| 101 | + |
| 102 | + expect(result.http.port).to.equal(8888); |
| 103 | + expect(result.coap.port).to.equal(defaultConfiguration.coap.port); |
| 104 | + expect(result.logLevel).to.equal(defaultConfiguration.logLevel); |
| 105 | + } |
| 106 | + |
| 107 | + @test async "should read and build config from file"() { |
| 108 | + const config = { http: { port: 7777 } }; |
| 109 | + writeFileSync(this.testFilePath, JSON.stringify(config)); |
| 110 | + |
| 111 | + const result = await buildConfigFromFile({}, this.testFilePath, {}, ConfigurationTest.validator); |
| 112 | + |
| 113 | + expect(result.http.port).to.equal(7777); |
| 114 | + } |
| 115 | + |
| 116 | + @test async "should merge file config with environment variables"() { |
| 117 | + const config = { http: { port: 7777 } }; |
| 118 | + writeFileSync(this.testFilePath, JSON.stringify(config)); |
| 119 | + |
| 120 | + const env = { COAP_PORT: "6000" }; |
| 121 | + const result = await buildConfigFromFile({}, this.testFilePath, env, ConfigurationTest.validator); |
| 122 | + |
| 123 | + expect(result.http.port).to.equal(7777); |
| 124 | + expect(result.coap.port).to.equal(6000); |
| 125 | + } |
| 126 | + |
| 127 | + @test async "should handle configFile option"() { |
| 128 | + const config = { http: { port: 5555 } }; |
| 129 | + writeFileSync(this.testFilePath, JSON.stringify(config)); |
| 130 | + |
| 131 | + const options = { configFile: this.testFilePath }; |
| 132 | + const result = await buildConfigFromFile(options, this.testFilePath, {}, ConfigurationTest.validator); |
| 133 | + |
| 134 | + expect(result.http.port).to.equal(5555); |
| 135 | + } |
| 136 | + |
| 137 | + @test "should throw error for invalid config file"() { |
| 138 | + writeFileSync(this.testFilePath, "{ invalid json }"); |
| 139 | + |
| 140 | + expect(buildConfigFromFile({}, this.testFilePath, {}, ConfigurationTest.validator)).to.eventually.throw(); |
| 141 | + } |
| 142 | + |
| 143 | + @test async "should convert string env variables to appropriate types"() { |
| 144 | + const env = { HTTP_PORT: "8080", SERVIENT_CLIENTONLY: "true", COAP_PORT: "5683" }; |
| 145 | + const result = await buildConfig({}, defaultConfiguration, env, ConfigurationTest.validator); |
| 146 | + |
| 147 | + expect(result.http.port).to.equal(8080); |
| 148 | + expect(result.servient.clientOnly).to.equal(true); |
| 149 | + expect(result.coap.port).to.equal(5683); |
| 150 | + } |
| 151 | +} |
0 commit comments