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() }