Skip to content

Commit

Permalink
Add JsonTest
Browse files Browse the repository at this point in the history
  • Loading branch information
Stephan202 authored and rickie committed Jan 10, 2024
1 parent 5d9c0a8 commit 9a0ad2b
Showing 1 changed file with 62 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package tech.picnic.errorprone.documentation;

import static java.nio.charset.StandardCharsets.UTF_8;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.google.auto.value.AutoValue;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.file.Files;
import java.nio.file.Path;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

final class JsonTest {
private static final TestObject TEST_OBJECT = new AutoValue_JsonTest_TestObject("foo", 42);
private static final String TEST_JSON = "{\"string\":\"foo\",\"number\":42}";

@Test
void write(@TempDir Path directory) {
Path file = directory.resolve("test.json");

Json.write(file, TEST_OBJECT);

assertThat(file).content(UTF_8).isEqualTo(TEST_JSON);
}

@Test
void writeFailure(@TempDir Path directory) {
assertThatThrownBy(() -> Json.write(directory, TEST_OBJECT))
.isInstanceOf(UncheckedIOException.class)
.hasMessageContaining("Failure writing to '%s'", directory)
.hasCauseInstanceOf(FileNotFoundException.class);
}

@Test
void read(@TempDir Path directory) throws IOException {
Path file = directory.resolve("test.json");

Files.writeString(file, TEST_JSON, UTF_8);

assertThat(Json.read(file, TestObject.class)).isEqualTo(TEST_OBJECT);
}

@Test
void readFailure(@TempDir Path directory) {
assertThatThrownBy(() -> Json.read(directory, TestObject.class))
.isInstanceOf(UncheckedIOException.class)
.hasMessageContaining("Failure reading from '%s'", directory)
.hasCauseInstanceOf(FileNotFoundException.class);
}

@AutoValue
@JsonDeserialize(as = AutoValue_JsonTest_TestObject.class)
abstract static class TestObject {
abstract String string();

abstract int number();
}
}

0 comments on commit 9a0ad2b

Please sign in to comment.