|
| 1 | +/* |
| 2 | + * Copyright © 2024 DataSQRL (contact@datasqrl.com) |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * 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, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.datasqrl; |
| 17 | + |
| 18 | +import static org.assertj.core.api.Assertions.assertThat; |
| 19 | +import static org.assertj.core.api.Assertions.assertThatThrownBy; |
| 20 | + |
| 21 | +import java.util.Map; |
| 22 | +import java.util.Set; |
| 23 | +import org.junit.jupiter.params.ParameterizedTest; |
| 24 | +import org.junit.jupiter.params.provider.CsvSource; |
| 25 | + |
| 26 | +class EnvironmentVariablesUtilsTest { |
| 27 | + |
| 28 | + @ParameterizedTest |
| 29 | + @CsvSource({ |
| 30 | + "'Hello, ${USER}', 'Hello, John'", // Positive case: USER variable exists |
| 31 | + "'Path: ${PATH}', 'Path: /usr/bin'", // Positive case: PATH variable exists |
| 32 | + "'No match here', 'No match here'", // Case with no placeholders |
| 33 | + "'Partial ${USER_HOME', 'Partial ${USER_HOME'" // Case to ensure partial match is not replaced |
| 34 | + }) |
| 35 | + void givenEnvVariables_whenReplaceWithEnv_thenReplaceCorrectly(String command, String expected) { |
| 36 | + Map<String, String> envVariables = |
| 37 | + Map.of( |
| 38 | + "USER", "John", |
| 39 | + "PATH", "/usr/bin"); |
| 40 | + String result = EnvironmentVariablesUtils.replaceWithEnv(command, envVariables); |
| 41 | + assertThat(result).isEqualTo(expected); |
| 42 | + } |
| 43 | + |
| 44 | + @ParameterizedTest |
| 45 | + @CsvSource({ |
| 46 | + "'Hello, ${USER}'", // USER variable missing |
| 47 | + "'Path: ${UNKNOWN}'", // UNKNOWN variable missing |
| 48 | + "'Combined ${VAR1} and ${VAR2}'" // Multiple missing variables |
| 49 | + }) |
| 50 | + void givenMissingEnvVariables_whenReplaceWithEnv_thenThrowException(String command) { |
| 51 | + Map<String, String> envVariables = Map.of(); // Empty map to simulate missing variables |
| 52 | + assertThatThrownBy(() -> EnvironmentVariablesUtils.replaceWithEnv(command, envVariables)) |
| 53 | + .isInstanceOf(IllegalStateException.class) |
| 54 | + .hasMessageContaining("Missing environment variable"); |
| 55 | + } |
| 56 | + |
| 57 | + @ParameterizedTest |
| 58 | + @CsvSource({ |
| 59 | + "'${USER} is different from ${USER_NAME}'", |
| 60 | + "'${NAME} is different from ${USER_NAME}'" |
| 61 | + }) |
| 62 | + void givenSimilarEnvVariableNames_whenReplaceWithEnv_thenPartialMatchDoesNotOccur( |
| 63 | + String command) { |
| 64 | + Map<String, String> envVariables = |
| 65 | + Map.of( |
| 66 | + "USER", "John", |
| 67 | + "NAME", "exists"); |
| 68 | + assertThatThrownBy(() -> EnvironmentVariablesUtils.replaceWithEnv(command, envVariables)) |
| 69 | + .isInstanceOf(IllegalStateException.class) |
| 70 | + .hasMessageContaining("Missing environment variable: USER_NAME"); |
| 71 | + } |
| 72 | + |
| 73 | + @ParameterizedTest |
| 74 | + @CsvSource({"'Run with ${VAR1} and ${VAR2}'", "'Execute ${USER} ${HOME}'", "'Simple command'"}) |
| 75 | + void givenAllEnvVariablesInScript_whenValidateEnvironmentVariables_thenReturnEmptySet( |
| 76 | + String script) { |
| 77 | + Map<String, String> envVariables = |
| 78 | + Map.of( |
| 79 | + "VAR1", "1", |
| 80 | + "VAR2", "2", |
| 81 | + "USER", "admin", |
| 82 | + "HOME", "/home/admin"); |
| 83 | + Set<String> missingVars = |
| 84 | + EnvironmentVariablesUtils.validateEnvironmentVariables(envVariables, script); |
| 85 | + assertThat(missingVars).isEmpty(); |
| 86 | + } |
| 87 | + |
| 88 | + @ParameterizedTest |
| 89 | + @CsvSource({ |
| 90 | + "'Run with ${VAR1} and ${VAR2}', 'VAR2'", |
| 91 | + "'Execute ${USER} ${HOME} ${PATH}', 'PATH'", |
| 92 | + "'${MISSING_VAR}', 'MISSING_VAR'" |
| 93 | + }) |
| 94 | + void givenSomeEnvVariablesInScript_whenValidateEnvironmentVariables_thenReturnMissingVariables( |
| 95 | + String script, String expectedMissing) { |
| 96 | + Map<String, String> envVariables = |
| 97 | + Map.of( |
| 98 | + "VAR1", "1", |
| 99 | + "USER", "admin", |
| 100 | + "HOME", "/home/admin"); |
| 101 | + Set<String> missingVars = |
| 102 | + EnvironmentVariablesUtils.validateEnvironmentVariables(envVariables, script); |
| 103 | + assertThat(missingVars).containsExactlyInAnyOrder(expectedMissing.split(",")); |
| 104 | + } |
| 105 | + |
| 106 | + @ParameterizedTest |
| 107 | + @CsvSource({ |
| 108 | + "'${USER} is different from ${USER_NAME}'", |
| 109 | + "'${NAME} is different from ${USER_NAME}'" |
| 110 | + }) |
| 111 | + void givenSimilarVariableNames_whenValidateEnvironmentVariables_thenNoPartialMatch( |
| 112 | + String script) { |
| 113 | + Map<String, String> envVariables = |
| 114 | + Map.of( |
| 115 | + "USER", "admin", |
| 116 | + "NAME", "admin"); |
| 117 | + Set<String> missingVars = |
| 118 | + EnvironmentVariablesUtils.validateEnvironmentVariables(envVariables, script); |
| 119 | + assertThat(missingVars) |
| 120 | + .containsExactly("USER_NAME"); // Ensure only USERNAME is reported missing |
| 121 | + } |
| 122 | +} |
0 commit comments