|
| 1 | +/* |
| 2 | + * Copyright (c) 2026 dexpace and Omar Aljarrah |
| 3 | + * |
| 4 | + * Licensed under the MIT License. See LICENSE in the project root. |
| 5 | + * SPDX-License-Identifier: MIT |
| 6 | + */ |
| 7 | + |
| 8 | +package org.dexpace.sdk.core.http.pipeline.steps |
| 9 | + |
| 10 | +import org.dexpace.sdk.core.config.ConfigurationBuilder |
| 11 | +import kotlin.test.Test |
| 12 | +import kotlin.test.assertEquals |
| 13 | + |
| 14 | +class HttpLogLevelTest { |
| 15 | + /** Builds a [org.dexpace.sdk.core.config.Configuration] whose env layer returns [entries] and nothing else. */ |
| 16 | + private fun configWithEnv(vararg entries: Pair<String, String>) = |
| 17 | + ConfigurationBuilder() |
| 18 | + .envSource { name -> entries.firstOrNull { it.first == name }?.second } |
| 19 | + .propsSource { null } |
| 20 | + .build() |
| 21 | + |
| 22 | + // ----- Known value resolves (case-insensitive) ----- |
| 23 | + |
| 24 | + @Test |
| 25 | + fun `known key resolves to the matching level`() { |
| 26 | + val cfg = configWithEnv("MY_PRODUCT_LOG_LEVEL" to "HEADERS") |
| 27 | + assertEquals( |
| 28 | + HttpLogLevel.HEADERS, |
| 29 | + HttpLogLevel.fromConfiguration("MY_PRODUCT_LOG_LEVEL", cfg, HttpLogLevel.NONE), |
| 30 | + ) |
| 31 | + } |
| 32 | + |
| 33 | + @Test |
| 34 | + fun `value resolves case-insensitively`() { |
| 35 | + val cfg = configWithEnv("MY_PRODUCT_LOG_LEVEL" to "body_and_headers") |
| 36 | + assertEquals( |
| 37 | + HttpLogLevel.BODY_AND_HEADERS, |
| 38 | + HttpLogLevel.fromConfiguration("MY_PRODUCT_LOG_LEVEL", cfg, HttpLogLevel.NONE), |
| 39 | + ) |
| 40 | + } |
| 41 | + |
| 42 | + @Test |
| 43 | + fun `value resolves with surrounding whitespace trimmed`() { |
| 44 | + val cfg = configWithEnv("MY_PRODUCT_LOG_LEVEL" to " Headers ") |
| 45 | + assertEquals( |
| 46 | + HttpLogLevel.HEADERS, |
| 47 | + HttpLogLevel.fromConfiguration("MY_PRODUCT_LOG_LEVEL", cfg, HttpLogLevel.NONE), |
| 48 | + ) |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + fun `each level name round-trips`() { |
| 53 | + HttpLogLevel.entries.forEach { level -> |
| 54 | + val cfg = configWithEnv("LL" to level.name) |
| 55 | + assertEquals(level, HttpLogLevel.fromConfiguration("LL", cfg, HttpLogLevel.NONE)) |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + // ----- Resolution honors the full Configuration layering, not just env ----- |
| 60 | + |
| 61 | + @Test |
| 62 | + fun `value resolves from the system-property layer when the env is unset`() { |
| 63 | + // Env unset; the property is looked up under the normalized name (MY_PRODUCT_LOG_LEVEL |
| 64 | + // -> my.product.log.level). The resolved value must still feed the level parsing. |
| 65 | + val cfg = |
| 66 | + ConfigurationBuilder() |
| 67 | + .envSource { null } |
| 68 | + .propsSource { name -> if (name == "my.product.log.level") "HEADERS" else null } |
| 69 | + .build() |
| 70 | + assertEquals( |
| 71 | + HttpLogLevel.HEADERS, |
| 72 | + HttpLogLevel.fromConfiguration("MY_PRODUCT_LOG_LEVEL", cfg, HttpLogLevel.NONE), |
| 73 | + ) |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + fun `explicit override wins over a conflicting env value`() { |
| 78 | + // Override is the top layer; it must take precedence over the env entry for the same key. |
| 79 | + val cfg = |
| 80 | + ConfigurationBuilder() |
| 81 | + .put("MY_PRODUCT_LOG_LEVEL", "BODY_AND_HEADERS") |
| 82 | + .envSource { name -> if (name == "MY_PRODUCT_LOG_LEVEL") "HEADERS" else null } |
| 83 | + .propsSource { null } |
| 84 | + .build() |
| 85 | + assertEquals( |
| 86 | + HttpLogLevel.BODY_AND_HEADERS, |
| 87 | + HttpLogLevel.fromConfiguration("MY_PRODUCT_LOG_LEVEL", cfg, HttpLogLevel.NONE), |
| 88 | + ) |
| 89 | + } |
| 90 | + |
| 91 | + // ----- Unset key falls back to the supplied default ----- |
| 92 | + |
| 93 | + @Test |
| 94 | + fun `unset key returns the supplied default`() { |
| 95 | + val cfg = configWithEnv() // nothing set |
| 96 | + assertEquals( |
| 97 | + HttpLogLevel.HEADERS, |
| 98 | + HttpLogLevel.fromConfiguration("MY_PRODUCT_LOG_LEVEL", cfg, HttpLogLevel.HEADERS), |
| 99 | + ) |
| 100 | + } |
| 101 | + |
| 102 | + @Test |
| 103 | + fun `empty env value returns the supplied default`() { |
| 104 | + // An empty env var (EXAMPLE=) is treated as absent by Configuration, so the default applies. |
| 105 | + val cfg = configWithEnv("MY_PRODUCT_LOG_LEVEL" to "") |
| 106 | + assertEquals( |
| 107 | + HttpLogLevel.BODY_AND_HEADERS, |
| 108 | + HttpLogLevel.fromConfiguration("MY_PRODUCT_LOG_LEVEL", cfg, HttpLogLevel.BODY_AND_HEADERS), |
| 109 | + ) |
| 110 | + } |
| 111 | + |
| 112 | + @Test |
| 113 | + fun `whitespace-only value returns the supplied default`() { |
| 114 | + // Configuration only treats an exactly-empty env string as absent, so a whitespace-only |
| 115 | + // value is returned as-is; fromConfiguration's own trim collapses it to "" and falls to the default. |
| 116 | + val cfg = configWithEnv("MY_PRODUCT_LOG_LEVEL" to " ") |
| 117 | + assertEquals( |
| 118 | + HttpLogLevel.HEADERS, |
| 119 | + HttpLogLevel.fromConfiguration("MY_PRODUCT_LOG_LEVEL", cfg, HttpLogLevel.HEADERS), |
| 120 | + ) |
| 121 | + } |
| 122 | + |
| 123 | + // ----- Unrecognized value falls back to the supplied default ----- |
| 124 | + |
| 125 | + @Test |
| 126 | + fun `unrecognized value returns the supplied default`() { |
| 127 | + val cfg = configWithEnv("MY_PRODUCT_LOG_LEVEL" to "VERBOSE") |
| 128 | + assertEquals( |
| 129 | + HttpLogLevel.NONE, |
| 130 | + HttpLogLevel.fromConfiguration("MY_PRODUCT_LOG_LEVEL", cfg, HttpLogLevel.NONE), |
| 131 | + ) |
| 132 | + } |
| 133 | + |
| 134 | + // ----- Default of the default-defaulted overload ----- |
| 135 | + |
| 136 | + @Test |
| 137 | + fun `default parameter is NONE when omitted`() { |
| 138 | + val cfg = configWithEnv() // nothing set |
| 139 | + assertEquals(HttpLogLevel.NONE, HttpLogLevel.fromConfiguration("MY_PRODUCT_LOG_LEVEL", cfg)) |
| 140 | + } |
| 141 | +} |
0 commit comments