forked from lukas-krecan/JsonUnit
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
lukas-krecan#567 Ease assertions on content of JSON files
- Loading branch information
Showing
4 changed files
with
129 additions
and
0 deletions.
There are no files selected for viewing
82 changes: 82 additions & 0 deletions
82
json-unit-assertj/src/main/java/net/javacrumbs/jsonunit/assertj/JsonFileAssert.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,82 @@ | ||
package net.javacrumbs.jsonunit.assertj; | ||
|
||
import net.javacrumbs.jsonunit.assertj.JsonAssert.ConfigurableJsonAssert; | ||
import org.assertj.core.api.AbstractFileAssert; | ||
import org.assertj.core.internal.Files; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.io.UncheckedIOException; | ||
import java.nio.charset.Charset; | ||
|
||
import static java.nio.file.Files.readAllBytes; | ||
import static net.javacrumbs.jsonunit.assertj.JsonAssertions.assertThatJson; | ||
import static org.assertj.core.internal.Files.instance; | ||
|
||
/** | ||
* Wrapper on {@link ConfigurableJsonAssert} to retrieve JSON from a file. | ||
*/ | ||
public class JsonFileAssert extends AbstractFileAssert<JsonFileAssert> { | ||
private final Files files = instance(); | ||
|
||
/** | ||
* Assert json properties in a file from its path. | ||
* | ||
* <pre>{@code | ||
* assertThatJsonFile("path/to/file.json").isFile() | ||
* .hasContent().inPath("test1").isEqualTo(2); | ||
* }</pre> | ||
*/ | ||
@NotNull | ||
public static JsonFileAssert assertThatJsonFile(@NotNull String path) { | ||
return new JsonFileAssert(new File(path)); | ||
} | ||
|
||
/** | ||
* Assert json properties in a file. | ||
* | ||
* <pre>{@code | ||
* assertThatJsonFile(jsonFile).isFile() | ||
* .hasContent().inPath("test1").isEqualTo(2); | ||
* }</pre> | ||
*/ | ||
@NotNull | ||
public static JsonFileAssert assertThatJsonFile(@NotNull File file) { | ||
return new JsonFileAssert(file); | ||
} | ||
|
||
private JsonFileAssert(File actual) { | ||
super(actual, JsonFileAssert.class); | ||
} | ||
|
||
/** | ||
* Convert file content to assertable JSON. | ||
* | ||
* @return ConfigurableJsonAssert to perform assertions on json content | ||
* @throws IllegalArgumentException when file content is not valid JSON | ||
*/ | ||
public ConfigurableJsonAssert hasContent() { | ||
return hasContent(Charset.defaultCharset()); | ||
} | ||
|
||
/** | ||
* Convert file content to assertable JSON with provided charset. | ||
* | ||
* @param charset The charset of the JSON file | ||
* @return ConfigurableJsonAssert to perform assertions on json content | ||
* @throws IllegalArgumentException when file content is not valid JSON | ||
*/ | ||
public ConfigurableJsonAssert hasContent(Charset charset) { | ||
files.assertCanRead(info, actual); | ||
return assertThatJson(readFile(charset)); | ||
} | ||
|
||
private String readFile(Charset charset) { | ||
try { | ||
return new String(readAllBytes(actual.toPath()), charset); | ||
} catch (IOException e) { | ||
throw new UncheckedIOException(String.format("Failed to read %s content with %s charset", actual, charset), e); | ||
} | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
json-unit-assertj/src/test/kotlin/net/javacrumbs/jsonunit/assertj/JsonFileAssertTest.kt
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,42 @@ | ||
package net.javacrumbs.jsonunit.assertj | ||
|
||
import net.javacrumbs.jsonunit.assertj.JsonFileAssert.assertThatJsonFile | ||
import org.assertj.core.api.Assertions.assertThatThrownBy | ||
import org.junit.jupiter.api.Test | ||
import java.io.File | ||
import java.nio.charset.Charset | ||
|
||
class JsonFileAssertTest { | ||
|
||
@Test | ||
fun `assertJsonFile should allow JSON assertions on JSON file when path is provided`() { | ||
assertThatJsonFile("src/test/resources/test.json") | ||
.hasContent().node("key").isEqualTo("value") | ||
} | ||
|
||
@Test | ||
fun `assertJsonFile should allow JSON assertions on JSON file when file is provided`() { | ||
assertThatJsonFile(File("src/test/resources/test.json")) | ||
.hasContent().node("key").isEqualTo("value") | ||
} | ||
|
||
@Test | ||
fun `assertJsonFile should allow defining charset for the JSON file`() { | ||
assertThatJsonFile(File("src/test/resources/test.json")) | ||
.hasContent(Charset.forName("UTF-8")).node("key").isEqualTo("value") | ||
} | ||
|
||
@Test | ||
fun `assertJsonFile should throw an IllegalArgumentException when JSON file is invalid`() { | ||
assertThatThrownBy { | ||
assertThatJsonFile("src/test/resources/invalid.json").hasContent() | ||
}.isInstanceOf(IllegalArgumentException::class.java) | ||
} | ||
|
||
@Test | ||
fun `assertJsonFile should throw an AssertionError when JSON file is not found`() { | ||
assertThatThrownBy { | ||
assertThatJsonFile("src/test/resources/not_found.json").hasContent() | ||
}.isInstanceOf(AssertionError::class.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,2 @@ | ||
{ | ||
"key": |
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,3 @@ | ||
{ | ||
"key": "value" | ||
} |