Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A few changes to modernize and make more flexible #87

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bom/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<parent>
<groupId>com.optum.sourcehawk</groupId>
<artifactId>sourcehawk</artifactId>
<version>0.7.2-SNAPSHOT</version>
<version>0.8.0-SNAPSHOT</version>
</parent>

<artifactId>sourcehawk-bom</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion cli/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<parent>
<artifactId>sourcehawk</artifactId>
<groupId>com.optum.sourcehawk</groupId>
<version>0.7.2-SNAPSHOT</version>
<version>0.8.0-SNAPSHOT</version>
</parent>

<artifactId>sourcehawk-cli</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<parent>
<groupId>com.optum.sourcehawk</groupId>
<artifactId>sourcehawk</artifactId>
<version>0.7.2-SNAPSHOT</version>
<version>0.8.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion distributions/debian/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<parent>
<groupId>com.optum.sourcehawk</groupId>
<artifactId>sourcehawk-dist</artifactId>
<version>0.7.2-SNAPSHOT</version>
<version>0.8.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion distributions/docker/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<parent>
<groupId>com.optum.sourcehawk</groupId>
<artifactId>sourcehawk-dist</artifactId>
<version>0.7.2-SNAPSHOT</version>
<version>0.8.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion distributions/linux/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<parent>
<groupId>com.optum.sourcehawk</groupId>
<artifactId>sourcehawk-dist</artifactId>
<version>0.7.2-SNAPSHOT</version>
<version>0.8.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion distributions/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<parent>
<groupId>com.optum.sourcehawk</groupId>
<artifactId>sourcehawk</artifactId>
<version>0.7.2-SNAPSHOT</version>
<version>0.8.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion distributions/rpm/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<parent>
<artifactId>sourcehawk-dist</artifactId>
<groupId>com.optum.sourcehawk</groupId>
<version>0.7.2-SNAPSHOT</version>
<version>0.8.0-SNAPSHOT</version>
</parent>

<artifactId>sourcehawk-dist-rpm</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion enforcer/core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<parent>
<groupId>com.optum.sourcehawk</groupId>
<artifactId>sourcehawk-enforcer</artifactId>
<version>0.7.2-SNAPSHOT</version>
<version>0.8.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion enforcer/file/aot/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<parent>
<groupId>com.optum.sourcehawk</groupId>
<artifactId>sourcehawk-enforcer-file</artifactId>
<version>0.7.2-SNAPSHOT</version>
<version>0.8.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion enforcer/file/common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<parent>
<groupId>com.optum.sourcehawk</groupId>
<artifactId>sourcehawk-enforcer-file</artifactId>
<version>0.7.2-SNAPSHOT</version>
<version>0.8.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import java.io.IOException;
import java.io.InputStream;
import java.util.regex.Pattern;

/**
* An enforcer which is responsible for enforcing that file contains a string
Expand All @@ -21,7 +22,7 @@
@AllArgsConstructor(staticName = "substring")
public class Contains extends AbstractFileEnforcer {

private static final String MESSAGE_TEMPLATE = "File does not contain the sub string [%s]";
private static final String MESSAGE_TEMPLATE = "File contain the sub string [%s] failed";

/**
* The substring that is expected to be found in the file
Expand All @@ -32,8 +33,8 @@ public class Contains extends AbstractFileEnforcer {
/** {@inheritDoc} */
@Override
public EnforcerResult enforceInternal(@NonNull final InputStream actualFileInputStream) throws IOException {
val actualFileContent = toString(actualFileInputStream);
if (!actualFileContent.contains(expectedSubstring)) {
val p = Pattern.compile(expectedSubstring);
if (!p.matcher(toString(actualFileInputStream)).find()) {
return EnforcerResult.failed(String.format(MESSAGE_TEMPLATE, expectedSubstring));
}
return EnforcerResult.passed();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.regex.Pattern;

/**
* An enforcer which is responsible for enforcing that file contains an entire line
Expand All @@ -24,14 +25,16 @@
@AllArgsConstructor(staticName = "contains")
public class ContainsLine extends AbstractFileEnforcer {

private static final String MESSAGE_TEMPLATE = "File does not contain the line [%s]";
private static final String MESSAGE_TEMPLATE = "File contains line [%s] failed";

/**
* The line that is expected to be found in the file
*/
protected final String expectedLine;

/** {@inheritDoc} */
/**
* {@inheritDoc}
*/
@Override
public EnforcerResult enforceInternal(@NonNull final InputStream actualFileInputStream) throws IOException {
try (val bufferedFileReader = new BufferedReader(new InputStreamReader((actualFileInputStream)))) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.util.Collections;
import java.util.Optional;
import java.util.function.Predicate;
import java.util.regex.Pattern;

/**
* An enforcer which is responsible for enforcing that file contains an entire line at a specific line number.
Expand All @@ -30,7 +31,7 @@
@AllArgsConstructor(staticName = "containsAt")
public class ContainsLineAt extends AbstractFileEnforcer implements FileResolver {

private static final String MESSAGE_TEMPLATE = "File does not contain the line [%s] at line number [%d]";
private static final String MESSAGE_TEMPLATE = "File contains line [%s] at line number [%d] failed";
private static final String UPDATE_MESSAGE_TEMPLATE = "File line number [%d] has been updated to value [%s]";

/**
Expand All @@ -51,7 +52,8 @@ public EnforcerResult enforceInternal(@NonNull final InputStream actualFileInput
}

private Predicate<String> 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();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -48,7 +49,7 @@ public class JsonValueEquals extends AbstractFileEnforcer implements FileResolve

/**
* Key: The JsonPointer expression to retrieve the value
*
* <p>
* Value: The expected value which the query should evaluate to
*/
private final Map<String, Object> expectations;
Expand All @@ -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;
Expand All @@ -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<String> enforce(final JsonNode jsonNode, final String jsonPointerExpression, final Object expectedValue) {
Expand All @@ -99,13 +102,15 @@ private static Optional<String> 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;
Expand All @@ -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) {
Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'
]
}
Expand Down
Loading