Skip to content

Commit

Permalink
lukas-krecan#567 Ease assertions on content of JSON files
Browse files Browse the repository at this point in the history
  • Loading branch information
fstaudt committed Oct 23, 2022
1 parent 0cd489e commit ca77e3e
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 0 deletions.
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);
}
}
}
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)
}
}
2 changes: 2 additions & 0 deletions json-unit-assertj/src/test/resources/invalid.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{
"key":
3 changes: 3 additions & 0 deletions json-unit-assertj/src/test/resources/test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"key": "value"
}

0 comments on commit ca77e3e

Please sign in to comment.