-
-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
191 additions
and
1 deletion.
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
50 changes: 50 additions & 0 deletions
50
src/main/java/me/itzg/helpers/assertcmd/JsonPathEquals.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,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
139
src/test/java/me/itzg/helpers/assertcmd/JsonPathEqualsTest.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,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"); | ||
} | ||
} |