-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2909 from entur/otp2_ds5_resolve_environment_vari…
…ables Otp2 ds5 resolve environment variables
- Loading branch information
Showing
4 changed files
with
209 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
src/main/java/org/opentripplanner/util/EnvironmentVariableReplacer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package org.opentripplanner.util; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
/** | ||
* Replaces environment variable placeholders specified on the format ${variable} in a text | ||
* with the current system environment variable values. | ||
*/ | ||
public class EnvironmentVariableReplacer { | ||
/** | ||
* A pattern matching a placeholder like '${VAR_NAME}'. The placeholder must start with | ||
* '${' and end with '}'. The environment variable name must consist of only alphanumerical | ||
* characters(a-z, A-Z, 0-9) and underscore '_'. | ||
*/ | ||
private static Pattern PATTERN = Pattern.compile("\\$\\{(\\w+)}"); | ||
|
||
|
||
/** | ||
* Search for {@link #PATTERN}s and replace each placeholder with the value of the | ||
* corresponding environment variable. | ||
* | ||
* @param source is used only to generate human friendly error message in case the text | ||
* contain a placeholder whitch can not be found. | ||
* @throws IllegalArgumentException if a placeholder exist in the {@code text}, but the | ||
* environment variable do not exist. | ||
*/ | ||
public static String insertEnvironmentVariables(String text, String source) { | ||
Map<String, String> environmentVariables = new HashMap<>(); | ||
Matcher matcher = PATTERN.matcher(text); | ||
|
||
while (matcher.find()) { | ||
String envVar = matcher.group(0); | ||
String nameOnly = matcher.group(1); | ||
if (!environmentVariables.containsKey(nameOnly)) { | ||
String value = System.getenv(nameOnly); | ||
if (value != null) { | ||
environmentVariables.put(envVar, value); | ||
} | ||
else { | ||
throw new IllegalArgumentException( | ||
"Environment variable name '" + nameOnly + "' in config '" | ||
+ source + "' not found in the system environment variables." | ||
); | ||
} | ||
} | ||
} | ||
for (Map.Entry<String, String> entry : environmentVariables.entrySet()) { | ||
text = text.replace(entry.getKey(), entry.getValue()); | ||
} | ||
return text; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
src/test/java/org/opentripplanner/util/EnvironmentVariableReplacerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package org.opentripplanner.util; | ||
|
||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.opentripplanner.standalone.config.ConfigLoader; | ||
|
||
import java.util.Map; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
public class EnvironmentVariableReplacerTest { | ||
|
||
|
||
private String envName = "<not set>"; | ||
private String envValue = "<not set>"; | ||
|
||
|
||
/** | ||
* To test the system environment variable substitution we must have a name/value pair that | ||
* exist in the system environment variables. So to prepare for the test, we look up a random | ||
* environment variable and use it later to construct the test samples, and the expected | ||
* results. | ||
* | ||
* We search for a environment variable name containing only alphanumeric characters and a | ||
* value with less than 30 characters. We do this to make it easier for humans to see what is | ||
* going on, if a test fails. This constraint is just to make the text involved more | ||
* readable. | ||
*/ | ||
@Before | ||
public void setup() { | ||
Map.Entry<String, String> envVar = System.getenv().entrySet() | ||
.stream() | ||
.filter(e -> | ||
e.getKey().matches("\\w+") && e.getValue().length() < 30 | ||
) | ||
.findFirst() | ||
.orElse(null); | ||
|
||
if(envVar == null) { | ||
throw new IllegalStateException("No environment variables for testing found."); | ||
} | ||
envName = envVar.getKey(); | ||
envValue = envVar.getValue(); | ||
} | ||
|
||
|
||
/** | ||
* Test replacing environment variables in a text. The {@link EnvironmentVariableReplacer} | ||
* should replace placeholders like '${ENV_NAME}' with the value of the system environment | ||
* variable. | ||
*/ | ||
@Test | ||
public void insertEnvironmentVariables() { | ||
// Given: a text and a expected result | ||
String text = "Env.var: ${" + envName + "}."; | ||
String expectedResult = "Env.var: " + envValue + "."; | ||
|
||
// When: | ||
String result = EnvironmentVariableReplacer.insertEnvironmentVariables(text, "test"); | ||
|
||
// Then: | ||
assertEquals(expectedResult, result); | ||
} | ||
|
||
@Test | ||
public void verifyThatAEnvVariableMayExistMoreThanOnce() { | ||
// Given: a text and a expected result | ||
String text = "Env. var1: ${" + envName + "} and var2: ${" + envName + "}."; | ||
String expectedResult = "Env. var1: " + envValue + " and var2: " + envValue + "."; | ||
|
||
// When: | ||
String result = EnvironmentVariableReplacer.insertEnvironmentVariables(text, "test"); | ||
|
||
// Then: | ||
assertEquals(expectedResult, result); | ||
} | ||
|
||
|
||
/** | ||
* Test replacing environment variable fails for unknown environment variable. | ||
*/ | ||
@Test(expected = IllegalArgumentException.class) | ||
public void testMissingEnvironmentVariable() { | ||
ConfigLoader.fromString( | ||
"None existing env.var: '${none_existing_env_variable}'.", "test" | ||
); | ||
} | ||
} |