|
| 1 | +/* |
| 2 | + * Copyright (c) 2025, VRAI Labs and/or its affiliates. All rights reserved. |
| 3 | + * |
| 4 | + * This software is licensed under the Apache License, Version 2.0 (the |
| 5 | + * "License") as published by the Apache Software Foundation. |
| 6 | + * |
| 7 | + * You may not use this file except in compliance with the License. You may |
| 8 | + * obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | + * License for the specific language governing permissions and limitations |
| 14 | + * under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package io.supertokens.storage.postgresql.test; |
| 18 | + |
| 19 | +import com.google.gson.JsonObject; |
| 20 | +import io.supertokens.storage.postgresql.Start; |
| 21 | +import org.junit.Test; |
| 22 | + |
| 23 | +import java.lang.reflect.Field; |
| 24 | +import java.util.Map; |
| 25 | + |
| 26 | +import static org.junit.Assert.*; |
| 27 | + |
| 28 | +public class EnvConfigTest { |
| 29 | + |
| 30 | + private static void setEnv(String key, String value) { |
| 31 | + try { |
| 32 | + Map<String, String> env = System.getenv(); |
| 33 | + Class<?> cl = env.getClass(); |
| 34 | + Field field = cl.getDeclaredField("m"); |
| 35 | + field.setAccessible(true); |
| 36 | + @SuppressWarnings("unchecked") |
| 37 | + Map<String, String> writableEnv = (Map<String, String>) field.get(env); |
| 38 | + writableEnv.put(key, value); |
| 39 | + } catch (Exception e) { |
| 40 | + throw new IllegalStateException("Failed to set environment variable", e); |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + private static void removeEnv(String key) { |
| 45 | + try { |
| 46 | + Map<String, String> env = System.getenv(); |
| 47 | + Class<?> cl = env.getClass(); |
| 48 | + Field field = cl.getDeclaredField("m"); |
| 49 | + field.setAccessible(true); |
| 50 | + @SuppressWarnings("unchecked") |
| 51 | + Map<String, String> writableEnv = (Map<String, String>) field.get(env); |
| 52 | + writableEnv.remove(key); |
| 53 | + } catch (Exception e) { |
| 54 | + throw new IllegalStateException("Failed to remove environment variable", e); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + public void testUpdateConfigJsonFromEnvHandlesBoxedTypes() { |
| 60 | + // Regression test: Integer fields like postgresql_minimum_idle_connections |
| 61 | + // were silently dropped because updateConfigJsonFromEnv only checked |
| 62 | + // primitive types (int.class) not boxed types (Integer.class). |
| 63 | + Start start = new Start(); |
| 64 | + try { |
| 65 | + setEnv("POSTGRESQL_MINIMUM_IDLE_CONNECTIONS", "5"); |
| 66 | + |
| 67 | + JsonObject configJson = new JsonObject(); |
| 68 | + start.updateConfigJsonFromEnv(configJson); |
| 69 | + |
| 70 | + assertTrue("POSTGRESQL_MINIMUM_IDLE_CONNECTIONS env var should be loaded into configJson", |
| 71 | + configJson.has("postgresql_minimum_idle_connections")); |
| 72 | + assertEquals(5, configJson.get("postgresql_minimum_idle_connections").getAsInt()); |
| 73 | + } finally { |
| 74 | + removeEnv("POSTGRESQL_MINIMUM_IDLE_CONNECTIONS"); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + public void testUpdateConfigJsonFromEnvHandlesBoxedTypeWithZero() { |
| 80 | + // Tests that Integer value of 0 is correctly loaded (not confused with null/unset) |
| 81 | + Start start = new Start(); |
| 82 | + try { |
| 83 | + setEnv("POSTGRESQL_MINIMUM_IDLE_CONNECTIONS", "0"); |
| 84 | + |
| 85 | + JsonObject configJson = new JsonObject(); |
| 86 | + start.updateConfigJsonFromEnv(configJson); |
| 87 | + |
| 88 | + assertTrue("POSTGRESQL_MINIMUM_IDLE_CONNECTIONS env var with value 0 should be loaded", |
| 89 | + configJson.has("postgresql_minimum_idle_connections")); |
| 90 | + assertEquals(0, configJson.get("postgresql_minimum_idle_connections").getAsInt()); |
| 91 | + } finally { |
| 92 | + removeEnv("POSTGRESQL_MINIMUM_IDLE_CONNECTIONS"); |
| 93 | + } |
| 94 | + } |
| 95 | +} |
0 commit comments