Skip to content

Commit

Permalink
feat(assert): added jsonPathEquals
Browse files Browse the repository at this point in the history
  • Loading branch information
itzg committed Jan 25, 2022
1 parent 216a86e commit 87881b9
Show file tree
Hide file tree
Showing 3 changed files with 191 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/main/java/me/itzg/helpers/assertcmd/AssertCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

@Command(name = "assert", description = "Provides assertion operators for verifying container setup",
subcommands = {
FileExists.class
FileExists.class,
JsonPathEquals.class,
}
)
public class AssertCommand {
Expand Down
50 changes: 50 additions & 0 deletions src/main/java/me/itzg/helpers/assertcmd/JsonPathEquals.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package me.itzg.helpers.assertcmd;

import com.jayway.jsonpath.DocumentContext;
import com.jayway.jsonpath.JsonPath;
import com.jayway.jsonpath.PathNotFoundException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Objects;
import java.util.concurrent.Callable;
import picocli.CommandLine.Command;
import picocli.CommandLine.ExitCode;
import picocli.CommandLine.Option;

@Command(name = "jsonPathEquals")
public class JsonPathEquals implements Callable<Integer> {
@Option(names = "--file", required = true)
Path file;

@Option(names = "--path", required = true)
String jsonPath;

@Option(names = "--expect", required = true)
String expectedValue;

@Override
public Integer call() throws Exception {
if (!Files.exists(file)) {
System.err.printf("The file %s does not exist%n", file);
return ExitCode.SOFTWARE;
}

final DocumentContext doc = JsonPath.parse(file.toFile());

final String result;
try {
result = doc.read(jsonPath, String.class);
} catch (PathNotFoundException e) {
System.err.printf("The path %s in %s does not exist%n",
jsonPath, file);
return ExitCode.SOFTWARE;
}
if (!Objects.equals(result, expectedValue)) {
System.err.printf("Expected '%s' at the path %s in %s, but was '%s'%n",
expectedValue, jsonPath, file, result);
return ExitCode.SOFTWARE;
}

return ExitCode.OK;
}
}
139 changes: 139 additions & 0 deletions src/test/java/me/itzg/helpers/assertcmd/JsonPathEqualsTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
package me.itzg.helpers.assertcmd;

import static com.github.stefanbirkner.systemlambda.SystemLambda.tapSystemErrNormalized;
import static org.assertj.core.api.Assertions.assertThat;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Collections;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import picocli.CommandLine;

class JsonPathEqualsTest {

@Test
void passesForString(@TempDir Path tempDir) throws IOException {
final Path opsJson = Files.write(tempDir.resolve("ops.json"),
Collections.singletonList(
"[ { \"uuid\": \"1-2-3-4\", \"name\": \"itzg\", \"level\": 4 } ]"
)
);

final int exitCode = new CommandLine(new JsonPathEquals())
.execute(
"--file=" + opsJson,
"--path=$[0].name",
"--expect=itzg"
);

assertThat(exitCode).isEqualTo(0);
}

@Test
void passesForNumber(@TempDir Path tempDir) throws IOException {
final Path opsJson = Files.write(tempDir.resolve("ops.json"),
Collections.singletonList(
"[ { \"uuid\": \"1-2-3-4\", \"name\": \"itzg\", \"level\": 4 } ]"
)
);

final int exitCode = new CommandLine(new JsonPathEquals())
.execute(
"--file=" + opsJson,
"--path=$[0].level",
"--expect=4"
);

assertThat(exitCode).isEqualTo(0);
}

@Test
void failsForMismatch(@TempDir Path tempDir) throws Exception {
final Path opsJson = Files.write(tempDir.resolve("ops.json"),
Collections.singletonList(
"[ { \"uuid\": null, \"name\": \"itzg\", \"level\": 4 } ]"
)
);

final String errOut = tapSystemErrNormalized(() -> {
final int exitCode = new CommandLine(new JsonPathEquals())
.execute(
"--file=" + opsJson,
"--path=$[0].name",
"--expect=notg"
);

assertThat(exitCode).isEqualTo(1);
});

assertThat(errOut).isEqualTo("Expected 'notg' at the path $[0].name in " + opsJson
+ ", but was 'itzg'\n");
}

@Test
void failsForMissingField(@TempDir Path tempDir) throws Exception {
final Path opsJson = Files.write(tempDir.resolve("ops.json"),
Collections.singletonList(
"[ { \"uuid\": null, \"name\": \"itzg\", \"level\": 4 } ]"
)
);

final String errOut = tapSystemErrNormalized(() -> {
final int exitCode = new CommandLine(new JsonPathEquals())
.execute(
"--file=" + opsJson,
"--path=$[0].doesNotExist",
"--expect=NA"
);

assertThat(exitCode).isEqualTo(1);
});

assertThat(errOut).isEqualTo("The path $[0].doesNotExist in " + opsJson
+ " does not exist\n");
}

@Test
void failsForNullField(@TempDir Path tempDir) throws Exception {
final Path opsJson = Files.write(tempDir.resolve("ops.json"),
Collections.singletonList(
"[ { \"uuid\": null, \"name\": \"itzg\", \"level\": 4 } ]"
)
);

final String errOut = tapSystemErrNormalized(() -> {
final int exitCode = new CommandLine(new JsonPathEquals())
.execute(
"--file=" + opsJson,
"--path=$[0].uuid",
"--expect=1-2-3-4"
);

assertThat(exitCode).isEqualTo(1);
});

assertThat(errOut).isEqualTo("Expected '1-2-3-4' at the path $[0].uuid in " + opsJson
+ ", but was 'null'\n");
}

@Test
void failsForMissingFile(@TempDir Path tempDir) throws Exception {
final Path missingPath = tempDir.resolve("ops.json");

final String errOut = tapSystemErrNormalized(() -> {

final int exitCode = new CommandLine(new JsonPathEquals())
.execute(
"--file=" + missingPath,
"--path=$[0].uuid",
"--expect=1-2-3-4"
);

assertThat(exitCode).isEqualTo(1);
});

assertThat(errOut).isEqualTo("The file " + missingPath + " does not exist\n");
}
}

0 comments on commit 87881b9

Please sign in to comment.