Skip to content

Commit

Permalink
If we dont match the regex for substitution return an error.
Browse files Browse the repository at this point in the history
Additional tests.
  • Loading branch information
credmond-git committed Apr 26, 2023
1 parent 9a37c9a commit 4dd466d
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.github.gestalt.config.entity;

import org.github.gestalt.config.post.process.transform.TransformerPostProcessor;

import java.time.format.DateTimeFormatter;

/**
Expand Down Expand Up @@ -37,8 +39,7 @@ public class GestaltConfig {

// the regex used to parse string substitutions.
// Must have a named capture group transform, key, and default, where the key is required and the transform and default are optional.
private String substitutionRegex =
"((?<transform>\\w+):)?(?<key>[\\w ,_.+;\"'`~!@#$%^&*()\\[\\]<>]+)(:=(?<default>[\\w ,_.+;:\"'`~!@#$%^&*()\\[\\]<>]+))?";
private String substitutionRegex = TransformerPostProcessor.defaultSubstitutionRegex;

/**
* Treat all warnings as errors.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1115,6 +1115,25 @@ public String description() {
}
}

/**
* Transform doesnt match the regex
*/
public static class TransformDoesntMatchRegex extends ValidationError {
private final String path;
private final String value;

public TransformDoesntMatchRegex(String path, String value) {
super(ValidationLevel.ERROR);
this.path = path;
this.value = value;
}

@Override
public String description() {
return "Transform doesnt match the expected format with value " + value + " on path " + path;
}
}

/**
* Not a valid SubstitutionNode
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@
*/
public class TransformerPostProcessor implements PostProcessor {

private static final String regexPattern =
"((?<transform>\\w+):)?(?<key>[\\w ,_.+;\"'`~!@#$%^&*()\\[\\]<>]+)(:=(?<default>[\\w ,_.+;:\"'`~!@#$%^&*()\\[\\]<>]+))?";
public static final String defaultSubstitutionRegex =
"^((?<transform>\\w+):)?(?<key>[\\w ,_.+;\"'`~!@#$%^&*()\\[\\]<>]+)(:=(?<default>[\\w ,_.+;:\"'`~!@#$%^&*()\\[\\]<>]+))?$";
private Pattern pattern;

private final Map<String, Transformer> transformers;
Expand All @@ -56,7 +56,7 @@ public TransformerPostProcessor() {
});

this.orderedDefaultTransformers = buildOrderedConfigPriorities(transformersList, false);
this.pattern = Pattern.compile(regexPattern);
this.pattern = Pattern.compile(defaultSubstitutionRegex);
}

/**
Expand All @@ -74,7 +74,7 @@ public TransformerPostProcessor(List<Transformer> transformers) {
}

this.substitutionTreeBuilder = new SubstitutionTreeBuilder("${", "}");
this.pattern = Pattern.compile(regexPattern);
this.pattern = Pattern.compile(defaultSubstitutionRegex);
}

@Override
Expand All @@ -85,7 +85,7 @@ public void applyConfig(PostProcessorConfig config) {
config.getConfig().getSubstitutionClosingToken());

this.maxRecursionDepth = config.getConfig().getMaxSubstitutionNestedDepth();
this.pattern = Pattern.compile(regexPattern);
this.pattern = Pattern.compile(defaultSubstitutionRegex);
}

@Override
Expand Down Expand Up @@ -207,7 +207,7 @@ private ValidateOf<String> transformString(String path, String input) {
if (foundMatch) {
return ValidateOf.valid(newLeafValue.toString());
} else {
return ValidateOf.valid(input);
return ValidateOf.inValid(new ValidationError.TransformDoesntMatchRegex(path, input));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,45 @@ void processTextWithMultipleDefaults() {
Assertions.assertEquals("hello world it is sunny today", validateNode.results().getValue().get());
}

@Test
void processTextWithMultipleDefaultsButHasValues() {

Map<String, String> customMap = new HashMap<>();
customMap.put("place", "world");
customMap.put("weather", "sunny");
CustomMapTransformer transformer = new CustomMapTransformer(customMap);

TransformerPostProcessor transformerPostProcessor = new TransformerPostProcessor(Collections.singletonList(transformer));
LeafNode node = new LeafNode("hello ${map:place:=earth} it is ${weather:=overcast} today");
ValidateOf<ConfigNode> validateNode = transformerPostProcessor.process("location", node);

Assertions.assertFalse(validateNode.hasErrors());
Assertions.assertTrue(validateNode.hasResults());
Assertions.assertTrue(validateNode.results().getValue().isPresent());
Assertions.assertEquals("hello world it is sunny today", validateNode.results().getValue().get());
}

@Test
void processInvalidFormat() {
// not sure about this test, this isnt "intended" behaviour. It just happens to happen.

Map<String, String> customMap = new HashMap<>();
customMap.put("place", "world");
customMap.put("weather", "sunny");
CustomMapTransformer transformer = new CustomMapTransformer(customMap);

TransformerPostProcessor transformerPostProcessor = new TransformerPostProcessor(Collections.singletonList(transformer));
LeafNode node = new LeafNode("${map:place:world}");
ValidateOf<ConfigNode> validateNode = transformerPostProcessor.process("location", node);

Assertions.assertTrue(validateNode.hasErrors());
Assertions.assertEquals(1, validateNode.getErrors().size());
Assertions.assertEquals("Transform doesnt match the expected format with value map:place:world on path location",
validateNode.getErrors().get(0).description());
Assertions.assertEquals(ValidationLevel.ERROR, validateNode.getErrors().get(0).level());

}

@Test
void processNoValue() {
Map<String, String> customMap = new HashMap<>();
Expand Down
4 changes: 2 additions & 2 deletions gestalt-core/src/test/resources/defaultPPEnv.properties
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ db.hosts[1].url=jdbc:postgresql://localhost:5432/mydb2
db.hosts[2].user=credmond
db.hosts[2].url=jdbc:postgresql://localhost:5432/mydb3
db.ConnectionTimeout=6000
db.idleTimeout=${envVar:DB_IDLETIMEOUT}
db.maxLifetime=60000.0
db.idleTimeout=${envVar:DB_IDLETIMEOUT:=900}
db.maxLifetime=${envVar:NO_RESULTS_FOUND:=60000.0}
http.Pool.maxTotal=100
http.Pool.maxPerRoute=10
http.Pool.validateAfterInactivity=6000
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ db.hosts[1].url=jdbc:postgresql://localhost:5432/mydb2
db.hosts[2].user=credmond
db.hosts[2].url=jdbc:postgresql://localhost:5432/mydb3
db.ConnectionTimeout=6000
db.idleTimeout=${envVar:DB_IDLETIMEOUT}
db.maxLifetime=60000.0
db.idleTimeout=${envVar:DB_IDLETIMEOUT:=900}
db.maxLifetime=${envVar:NO_RESULTS_FOUND:=60000.0}
http.Pool.maxTotal=100
http.Pool.maxPerRoute=10
http.Pool.validateAfterInactivity=6000
Expand Down
4 changes: 2 additions & 2 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[versions]
java = "11"
javaLatest = "19"
kotlin = "1.8.20"
kotlin = "1.8.21"
kotlinDokka = "1.8.10"
kodeinDI = "7.20.1"
koinDI= "3.4.0"
Expand All @@ -23,7 +23,7 @@ junit5 = "5.9.2"
assertJ = "3.24.2"
mockito = "5.2.0"
mockk = "1.13.5"
koTestAssertions = "5.5.5"
koTestAssertions = "5.6.1"
awsMock = "2.11.0"

errorprone = "2.18.0"
Expand Down

0 comments on commit 4dd466d

Please sign in to comment.