getPredicate() {
- return actual -> StringUtils.equals(StringUtils.removeNewLines(expectedLine), StringUtils.removeNewLines(actual));
+ val pattern = Pattern.compile(expectedLine);
+ return actual -> StringUtils.equals(StringUtils.removeNewLines(expectedLine), StringUtils.removeNewLines(actual)) || pattern.matcher(actual).matches();
}
/**
diff --git a/enforcer/file/common/src/main/java/com/optum/sourcehawk/enforcer/file/common/Sha256ChecksumEquals.java b/enforcer/file/common/src/main/java/com/optum/sourcehawk/enforcer/file/common/Sha256ChecksumEquals.java
index c559c9c..cc493b0 100644
--- a/enforcer/file/common/src/main/java/com/optum/sourcehawk/enforcer/file/common/Sha256ChecksumEquals.java
+++ b/enforcer/file/common/src/main/java/com/optum/sourcehawk/enforcer/file/common/Sha256ChecksumEquals.java
@@ -13,6 +13,7 @@
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
+import java.util.regex.Pattern;
/**
* An enforcer which is responsible for enforcing that SHA-256 checksum of a file's contents match
@@ -36,7 +37,8 @@ public class Sha256ChecksumEquals extends AbstractFileEnforcer {
@Override
public EnforcerResult enforceInternal(@NonNull final InputStream actualFileInputStream) throws IOException {
val actualChecksum = checksum(actualFileInputStream);
- if (expectedChecksum.equals(actualChecksum)) {
+ val pattern = Pattern.compile(expectedChecksum);
+ if (expectedChecksum.equals(actualChecksum) || pattern.matcher(actualChecksum).matches()) {
return EnforcerResult.passed();
}
return EnforcerResult.failed(DEFAULT_MESSAGE);
diff --git a/enforcer/file/common/src/main/java/com/optum/sourcehawk/enforcer/file/json/JsonValueEquals.java b/enforcer/file/common/src/main/java/com/optum/sourcehawk/enforcer/file/json/JsonValueEquals.java
index 082d6b7..22d4672 100644
--- a/enforcer/file/common/src/main/java/com/optum/sourcehawk/enforcer/file/json/JsonValueEquals.java
+++ b/enforcer/file/common/src/main/java/com/optum/sourcehawk/enforcer/file/json/JsonValueEquals.java
@@ -26,6 +26,7 @@
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
+import java.util.regex.Pattern;
import java.util.stream.Collectors;
/**
@@ -48,7 +49,7 @@ public class JsonValueEquals extends AbstractFileEnforcer implements FileResolve
/**
* Key: The JsonPointer expression to retrieve the value
- *
+ *
* Value: The expected value which the query should evaluate to
*/
private final Map expectations;
@@ -57,14 +58,16 @@ public class JsonValueEquals extends AbstractFileEnforcer implements FileResolve
* Create with a single path query and expected value
*
* @param jsonPointerExpression the JSON pointer expression
- * @param expectedValue the expected value
+ * @param expectedValue the expected value
* @return the enforcer
*/
public static JsonValueEquals equals(final String jsonPointerExpression, final Object expectedValue) {
return JsonValueEquals.equals(Collections.singletonMap(jsonPointerExpression, expectedValue));
}
- /** {@inheritDoc} */
+ /**
+ * {@inheritDoc}
+ */
@Override
public EnforcerResult enforceInternal(@NonNull final InputStream actualFileInputStream) {
final JsonNode jsonNode;
@@ -85,9 +88,9 @@ public EnforcerResult enforceInternal(@NonNull final InputStream actualFileInput
/**
* Enforce individual json path queries with expected value
*
- * @param jsonNode the JSON node
- * @param jsonPointerExpression the JSON pointer expression
- * @param expectedValue the expected value
+ * @param jsonNode the JSON node
+ * @param jsonPointerExpression the JSON pointer expression
+ * @param expectedValue the expected value
* @return The message to be added, otherwise {@link Optional#empty()}
*/
private static Optional enforce(final JsonNode jsonNode, final String jsonPointerExpression, final Object expectedValue) {
@@ -99,13 +102,15 @@ private static Optional enforce(final JsonNode jsonNode, final String js
if (jsonNodeValueEquals(actualJsonNode, expectedValue)) {
return Optional.empty();
}
- return Optional.of(String.format(NOT_EQUAL_MESSAGE_TEMPLATE, jsonPointerExpression, actualJsonNode.asText(), expectedValue));
+ return Optional.of(String.format(NOT_EQUAL_MESSAGE_TEMPLATE, jsonPointerExpression, actualJsonNode.asText(), expectedValue));
} catch (final Exception e) {
return Optional.of(String.format(QUERY_ERROR_TEMPLATE, jsonPointerExpression, e.getMessage()));
}
}
- /** {@inheritDoc} */
+ /**
+ * {@inheritDoc}
+ */
@Override
public ResolverResult resolve(final @NonNull InputStream actualFileInputStream, final @NonNull Writer outputFileWriter) throws IOException {
final JsonNode rootJsonNode;
@@ -126,9 +131,9 @@ public ResolverResult resolve(final @NonNull InputStream actualFileInputStream,
/**
* Resolve an individual json path query with the expected value
*
- * @param rootJsonNode the root JSON node
+ * @param rootJsonNode the root JSON node
* @param jsonPointerExpression the JSON pointer expression
- * @param expectedValue the expected value
+ * @param expectedValue the expected value
* @return the resolver result
*/
private static ResolverResult resolve(final JsonNode rootJsonNode, final String jsonPointerExpression, final Object expectedValue) {
@@ -162,8 +167,8 @@ private static ResolverResult resolve(final JsonNode rootJsonNode, final String
* Update the actual node with the expected value
*
* @param parentObjectNode the parent object node
- * @param childNodeName the child node name
- * @param expectedValue the expected value
+ * @param childNodeName the child node name
+ * @param expectedValue the expected value
*/
private static void updateObjectNodeValue(final ObjectNode parentObjectNode, final String childNodeName, final Object expectedValue) {
if (expectedValue instanceof Boolean) {
@@ -188,8 +193,8 @@ private static void updateObjectNodeValue(final ObjectNode parentObjectNode, fin
*
* @param parentArrayNode the parent object node
* @param actualNodeIndex the actual node index in the array
- * @param expectedValue the expected value
- * @param missing true if node does not currently exist, false otherwise
+ * @param expectedValue the expected value
+ * @param missing true if node does not currently exist, false otherwise
*/
@SuppressWarnings("squid:S3776")
private static void updateArrayNodeValue(final ArrayNode parentArrayNode, final int actualNodeIndex, final Object expectedValue, final boolean missing) {
@@ -241,15 +246,20 @@ private static void updateArrayNodeValue(final ArrayNode parentArrayNode, final
/**
* Determine if the {@code jsonNode} value equals that of the {@code expectedValue}
*
- * @param jsonNode the JSON node to retrieve the value from
+ * @param jsonNode the JSON node to retrieve the value from
* @param expectedValue the expected value
* @return true if they are equal, false otherwise
*/
private static boolean jsonNodeValueEquals(final JsonNode jsonNode, final Object expectedValue) {
- return (expectedValue instanceof CharSequence && StringUtils.equals((CharSequence) expectedValue, jsonNode.textValue()))
+ return (expectedValue instanceof CharSequence && equalsOrMatches(jsonNode, (CharSequence) expectedValue))
|| (expectedValue instanceof Number && expectedValue == jsonNode.numberValue())
|| (expectedValue instanceof Boolean && (boolean) expectedValue == jsonNode.booleanValue())
- || Objects.equals(expectedValue.toString(), jsonNode.asText());
+ || Objects.equals(expectedValue.toString(), jsonNode.asText())
+ || Pattern.compile(jsonNode.asText()).matcher(expectedValue.toString()).matches();
}
+ private static boolean equalsOrMatches(JsonNode jsonNode, CharSequence expectedValue) {
+ return StringUtils.equals(expectedValue, jsonNode.textValue())
+ || Pattern.compile(expectedValue.toString()).matcher(jsonNode.textValue()).matches();
+ }
}
diff --git a/enforcer/file/common/src/test/groovy/com/optum/sourcehawk/enforcer/file/common/ContainsLineAtSpec.groovy b/enforcer/file/common/src/test/groovy/com/optum/sourcehawk/enforcer/file/common/ContainsLineAtSpec.groovy
index 0859117..ff83b09 100644
--- a/enforcer/file/common/src/test/groovy/com/optum/sourcehawk/enforcer/file/common/ContainsLineAtSpec.groovy
+++ b/enforcer/file/common/src/test/groovy/com/optum/sourcehawk/enforcer/file/common/ContainsLineAtSpec.groovy
@@ -41,8 +41,31 @@ class ContainsLineAtSpec extends Specification {
where:
expectedLine | expectedLineNumber
+ '^(?:(?!include).)*$' | 5
'^ Here is a special character: $' | 5
'Perhaps I should include a double " and a single \' as well...' | 7
+ '.*I should include a double " and a single \' as well.*' | 7
+ }
+
+ @Unroll
+ def "enforce - NOT #expectedLine - #expectedLineNumber (passed)"() {
+ given:
+ ContainsLineAt containsLineAt = ContainsLineAt.containsAt(expectedLine, expectedLineNumber)
+ InputStream fileInputStream = IoUtil.getResourceAsStream('/file.txt')
+
+ when:
+ EnforcerResult result = containsLineAt.enforce(fileInputStream)
+
+ then:
+ result
+ !result.passed
+ result.messages
+
+ where:
+ expectedLine | expectedLineNumber
+ '![\\^ Here is a special character: \\$]' | 5
+ '^(?:(?!special character).)*$' | 5
+ '![Perhaps I should include a double " and a single \' as well...]' | 7
}
@Unroll
@@ -58,7 +81,7 @@ class ContainsLineAtSpec extends Specification {
result
!result.passed
result.messages
- result.messages[0] == "File does not contain the line [$expectedLine] at line number [$expectedLineNumber]"
+ result.messages[0] == "File contains line [$expectedLine] at line number [$expectedLineNumber] failed"
where:
expectedLine | expectedLineNumber
diff --git a/enforcer/file/common/src/test/groovy/com/optum/sourcehawk/enforcer/file/common/ContainsLineSpec.groovy b/enforcer/file/common/src/test/groovy/com/optum/sourcehawk/enforcer/file/common/ContainsLineSpec.groovy
index 4687b15..e8eaade 100644
--- a/enforcer/file/common/src/test/groovy/com/optum/sourcehawk/enforcer/file/common/ContainsLineSpec.groovy
+++ b/enforcer/file/common/src/test/groovy/com/optum/sourcehawk/enforcer/file/common/ContainsLineSpec.groovy
@@ -55,11 +55,11 @@ class ContainsLineSpec extends Specification {
result
!result.passed
result.messages
- result.messages[0] == "File does not contain the line [$expectedLine]"
+ result.messages[0] == "File contains line [$expectedLine] failed"
where:
expectedLine << [
- 'Here is a special character: $',
+ 'Here is a special character: \\$',
'Perhaps I should include a double " and a single \' as well'
]
}
diff --git a/enforcer/file/common/src/test/groovy/com/optum/sourcehawk/enforcer/file/common/ContainsSpec.groovy b/enforcer/file/common/src/test/groovy/com/optum/sourcehawk/enforcer/file/common/ContainsSpec.groovy
index 0befcc3..ef4b4cd 100644
--- a/enforcer/file/common/src/test/groovy/com/optum/sourcehawk/enforcer/file/common/ContainsSpec.groovy
+++ b/enforcer/file/common/src/test/groovy/com/optum/sourcehawk/enforcer/file/common/ContainsSpec.groovy
@@ -42,6 +42,27 @@ class ContainsSpec extends Specification {
]
}
+ @Unroll
+ def "enforce - NOT #expectedSubstring (passed)"() {
+ given:
+ Contains contains = Contains.substring(expectedSubstring)
+ InputStream fileInputStream = IoUtil.getResourceAsStream('/file.txt')
+
+ when:
+ EnforcerResult result = contains.enforce(fileInputStream)
+
+ then:
+ result
+ !result.passed
+ result.messages
+
+ where:
+ expectedSubstring << [
+ '![character]',
+ '![I should include]'
+ ]
+ }
+
@Unroll
def "enforce - #expectedSubstring (failed)"() {
given:
@@ -55,7 +76,7 @@ class ContainsSpec extends Specification {
result
!result.passed
result.messages
- result.messages[0] == "File does not contain the sub string [$expectedSubstring]"
+ result.messages[0] == "File contain the sub string [$expectedSubstring] failed"
where:
expectedSubstring << [
diff --git a/enforcer/file/common/src/test/groovy/com/optum/sourcehawk/enforcer/file/common/Sha256ChecksumEqualsSpec.groovy b/enforcer/file/common/src/test/groovy/com/optum/sourcehawk/enforcer/file/common/Sha256ChecksumEqualsSpec.groovy
index e74e4d2..9229f82 100644
--- a/enforcer/file/common/src/test/groovy/com/optum/sourcehawk/enforcer/file/common/Sha256ChecksumEqualsSpec.groovy
+++ b/enforcer/file/common/src/test/groovy/com/optum/sourcehawk/enforcer/file/common/Sha256ChecksumEqualsSpec.groovy
@@ -35,6 +35,36 @@ class Sha256ChecksumEqualsSpec extends Specification {
!enforcerResult.messages
}
+ def "enforce regex (passed))"() {
+ given:
+ String expectedChecksum = "a6179a.*"
+ Sha256ChecksumEquals sha256ChecksumEquals = Sha256ChecksumEquals.equals(expectedChecksum)
+ InputStream fileInputStream = IoUtil.getResourceAsStream("/checksum.txt")
+
+ when:
+ EnforcerResult enforcerResult = sha256ChecksumEquals.enforce(fileInputStream)
+
+ then:
+ enforcerResult
+ enforcerResult.passed
+ !enforcerResult.messages
+ }
+
+ def "enforce regex negate (passed))"() {
+ given:
+ String expectedChecksum = "^(?:(?!abc123).)*\$"
+ Sha256ChecksumEquals sha256ChecksumEquals = Sha256ChecksumEquals.equals(expectedChecksum)
+ InputStream fileInputStream = IoUtil.getResourceAsStream("/checksum.txt")
+
+ when:
+ EnforcerResult enforcerResult = sha256ChecksumEquals.enforce(fileInputStream)
+
+ then:
+ enforcerResult
+ enforcerResult.passed
+ !enforcerResult.messages
+ }
+
def "enforce (failed))"() {
given:
String expectedChecksum = "123"
diff --git a/enforcer/file/common/src/test/groovy/com/optum/sourcehawk/enforcer/file/json/JsonValueEqualsSpec.groovy b/enforcer/file/common/src/test/groovy/com/optum/sourcehawk/enforcer/file/json/JsonValueEqualsSpec.groovy
index f0fdb71..56fa02b 100644
--- a/enforcer/file/common/src/test/groovy/com/optum/sourcehawk/enforcer/file/json/JsonValueEqualsSpec.groovy
+++ b/enforcer/file/common/src/test/groovy/com/optum/sourcehawk/enforcer/file/json/JsonValueEqualsSpec.groovy
@@ -47,6 +47,8 @@ class JsonValueEqualsSpec extends Specification {
'/make' | 'Raleigh'
'/size/value' | 60
'/components/0' | 'handlebars'
+ '/components/0' | 'handle.*'
+ '/components/0' | '^(?:(?!pedals).)*$'
}
@Unroll
diff --git a/enforcer/file/common/src/test/groovy/com/optum/sourcehawk/enforcer/file/yaml/YamlValueEqualsSpec.groovy b/enforcer/file/common/src/test/groovy/com/optum/sourcehawk/enforcer/file/yaml/YamlValueEqualsSpec.groovy
index b4ac3ca..fc2ba25 100644
--- a/enforcer/file/common/src/test/groovy/com/optum/sourcehawk/enforcer/file/yaml/YamlValueEqualsSpec.groovy
+++ b/enforcer/file/common/src/test/groovy/com/optum/sourcehawk/enforcer/file/yaml/YamlValueEqualsSpec.groovy
@@ -41,6 +41,8 @@ class YamlValueEqualsSpec extends Specification {
'/make' | 'Raleigh'
'/size/value' | 60
'/components/0' | 'handlebars'
+ '/components/0' | 'handle.*'
+ '/components/0' | '^(?:(?!pedals).)*$'
}
def "enforce - map (passed)"() {
diff --git a/enforcer/file/core/pom.xml b/enforcer/file/core/pom.xml
index 06ba9db..34788a8 100644
--- a/enforcer/file/core/pom.xml
+++ b/enforcer/file/core/pom.xml
@@ -8,7 +8,7 @@
com.optum.sourcehawk
sourcehawk-enforcer-file
- 0.7.2-SNAPSHOT
+ 0.8.0-SNAPSHOT
../pom.xml
diff --git a/enforcer/file/docker/pom.xml b/enforcer/file/docker/pom.xml
index 779cfaf..23a5cf5 100644
--- a/enforcer/file/docker/pom.xml
+++ b/enforcer/file/docker/pom.xml
@@ -8,7 +8,7 @@
com.optum.sourcehawk
sourcehawk-enforcer-file
- 0.7.2-SNAPSHOT
+ 0.8.0-SNAPSHOT
../pom.xml
diff --git a/enforcer/file/maven/pom.xml b/enforcer/file/maven/pom.xml
index 6f91426..57cc542 100644
--- a/enforcer/file/maven/pom.xml
+++ b/enforcer/file/maven/pom.xml
@@ -8,7 +8,7 @@
com.optum.sourcehawk
sourcehawk-enforcer-file
- 0.7.2-SNAPSHOT
+ 0.8.0-SNAPSHOT
../pom.xml
diff --git a/enforcer/file/pom.xml b/enforcer/file/pom.xml
index de3f2ef..96395d3 100644
--- a/enforcer/file/pom.xml
+++ b/enforcer/file/pom.xml
@@ -8,7 +8,7 @@
com.optum.sourcehawk
sourcehawk-enforcer
- 0.7.2-SNAPSHOT
+ 0.8.0-SNAPSHOT
../pom.xml
diff --git a/enforcer/file/registry/pom.xml b/enforcer/file/registry/pom.xml
index e7f10e3..5deb7de 100644
--- a/enforcer/file/registry/pom.xml
+++ b/enforcer/file/registry/pom.xml
@@ -8,7 +8,7 @@
com.optum.sourcehawk
sourcehawk-enforcer-file
- 0.7.2-SNAPSHOT
+ 0.8.0-SNAPSHOT
../pom.xml
diff --git a/enforcer/pom.xml b/enforcer/pom.xml
index c6954e5..e5aad34 100644
--- a/enforcer/pom.xml
+++ b/enforcer/pom.xml
@@ -8,7 +8,7 @@
com.optum.sourcehawk
sourcehawk
- 0.7.2-SNAPSHOT
+ 0.8.0-SNAPSHOT
../pom.xml
diff --git a/exec/pom.xml b/exec/pom.xml
index 5f12f1d..bff4a8a 100644
--- a/exec/pom.xml
+++ b/exec/pom.xml
@@ -8,7 +8,7 @@
com.optum.sourcehawk
sourcehawk
- 0.7.2-SNAPSHOT
+ 0.8.0-SNAPSHOT
../pom.xml
diff --git a/pom.xml b/pom.xml
index 7617347..b717094 100644
--- a/pom.xml
+++ b/pom.xml
@@ -11,7 +11,7 @@
sourcehawk
- 0.7.2-SNAPSHOT
+ 0.8.0-SNAPSHOT
pom
Sourcehawk
@@ -22,7 +22,8 @@
0.97
- 1.7.32
+ 1.7.36
+ 1.18.24
e1
@@ -34,9 +35,8 @@
brianwyka
- Brian Wyka}
- brian.wyka@optum.com
- Optum
+ Brian Wyka
+ brian.wyka@gmail.com
Project Lead
@@ -44,8 +44,7 @@
ctoestreich
Christian Oestreich
- christian.oestreich@optum.com
- Optum
+ acetrike@yahoo.com
Project Lead
@@ -95,7 +94,7 @@
com.fasterxml.jackson
jackson-bom
- 2.12.5
+ 2.12.7.20221012
import
pom