X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;ds=sidebyside;f=src%2Ftest%2Fjava%2Fnet%2Fpterodactylus%2Frhynodge%2Ffilters%2FJsonFilterTest.kt;fp=src%2Ftest%2Fjava%2Fnet%2Fpterodactylus%2Frhynodge%2Ffilters%2FJsonFilterTest.kt;h=a37990afcfaf3c2e083101da08f304906fe596cd;hb=3dc17a61f9a05eff6ed71a67a4e2e898435a3c57;hp=0000000000000000000000000000000000000000;hpb=6cc668aa117e70ac390e5a669d5e6ceb8822b3a7;p=rhynodge.git diff --git a/src/test/java/net/pterodactylus/rhynodge/filters/JsonFilterTest.kt b/src/test/java/net/pterodactylus/rhynodge/filters/JsonFilterTest.kt new file mode 100644 index 0000000..a37990a --- /dev/null +++ b/src/test/java/net/pterodactylus/rhynodge/filters/JsonFilterTest.kt @@ -0,0 +1,71 @@ +package net.pterodactylus.rhynodge.filters + +import com.spotify.hamcrest.jackson.JsonMatchers.jsonArray +import com.spotify.hamcrest.jackson.JsonMatchers.jsonDouble +import com.spotify.hamcrest.jackson.JsonMatchers.jsonNull +import com.spotify.hamcrest.jackson.JsonMatchers.jsonObject +import com.spotify.hamcrest.jackson.JsonMatchers.jsonText +import net.pterodactylus.rhynodge.Filter +import net.pterodactylus.rhynodge.states.AbstractState +import net.pterodactylus.rhynodge.states.FailedState +import net.pterodactylus.rhynodge.states.HttpState +import net.pterodactylus.rhynodge.states.JsonState +import net.pterodactylus.rhynodge.states.StringState +import org.hamcrest.MatcherAssert.assertThat +import org.hamcrest.Matchers.contains +import org.hamcrest.Matchers.instanceOf +import org.junit.Assert +import org.junit.Assert.assertThrows +import org.junit.Test + +class JsonFilterTest { + + @Test + fun `json filter is a filter implementation`() { + assertThat(JsonFilter(), instanceOf(Filter::class.java)) + } + + @Test + fun `http state can be turned into json state`() { + val httpState = HttpState("", 200, "application/json", "{\n \"state\": \"foo\",\n \"list\": [\n \"value1\",\n \"value2\"\n ],\n \"object\": {\n \"foo\": \"bar\",\n \"baz\": null,\n \"quux\": 1.5\n }\n}".encodeToByteArray()) + val jsonState = filter.filter(httpState) as JsonState + val jsonNode = jsonState.jsonNode + assertThat( + jsonNode, jsonObject() + .where("state", jsonText("foo")) + .where( + "list", jsonArray( + contains( + jsonText("value1"), + jsonText("value2") + ) + ) + ) + .where( + "object", jsonObject() + .where("foo", jsonText("bar")) + .where("baz", jsonNull()) + .where("quux", jsonDouble(1.5)) + ) + ) + } + + @Test + fun `json filter returns failed state when json can not be parsed`() { + val httpState = HttpState("", 200, "application/json", "this is not json".encodeToByteArray()) + assertThat(filter.filter(httpState), instanceOf(FailedState::class.java)) + } + + @Test + fun `json filter throws exception if given state is not an http state`() { + assertThrows(IllegalArgumentException::class.java) { filter.filter(StringState("foo")) } + } + + @Test + fun `json filter returns a failed state when a failed state is given`() { + val newState = filter.filter(FailedState()) + assertThat(newState, instanceOf(FailedState::class.java)) + } + + val filter = JsonFilter() +}