1 package net.pterodactylus.rhynodge.filters
3 import com.spotify.hamcrest.jackson.JsonMatchers.jsonArray
4 import com.spotify.hamcrest.jackson.JsonMatchers.jsonDouble
5 import com.spotify.hamcrest.jackson.JsonMatchers.jsonNull
6 import com.spotify.hamcrest.jackson.JsonMatchers.jsonObject
7 import com.spotify.hamcrest.jackson.JsonMatchers.jsonText
8 import net.pterodactylus.rhynodge.Filter
9 import net.pterodactylus.rhynodge.states.AbstractState
10 import net.pterodactylus.rhynodge.states.FailedState
11 import net.pterodactylus.rhynodge.states.HttpState
12 import net.pterodactylus.rhynodge.states.JsonState
13 import net.pterodactylus.rhynodge.states.StringState
14 import org.hamcrest.MatcherAssert.assertThat
15 import org.hamcrest.Matchers.contains
16 import org.hamcrest.Matchers.instanceOf
17 import org.junit.Assert
18 import org.junit.Assert.assertThrows
21 class JsonFilterTest {
24 fun `json filter is a filter implementation`() {
25 assertThat(JsonFilter(), instanceOf(Filter::class.java))
29 fun `http state can be turned into json state`() {
30 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())
31 val jsonState = filter.filter(httpState) as JsonState
32 val jsonNode = jsonState.jsonNode
34 jsonNode, jsonObject()
35 .where("state", jsonText("foo"))
45 "object", jsonObject()
46 .where("foo", jsonText("bar"))
47 .where("baz", jsonNull())
48 .where("quux", jsonDouble(1.5))
54 fun `json filter returns failed state when json can not be parsed`() {
55 val httpState = HttpState("", 200, "application/json", "<json>this is not json</json>".encodeToByteArray())
56 assertThat(filter.filter(httpState), instanceOf(FailedState::class.java))
60 fun `json filter throws exception if given state is not an http state`() {
61 assertThrows(IllegalArgumentException::class.java) { filter.filter(StringState("foo")) }
65 fun `json filter returns a failed state when a failed state is given`() {
66 val newState = filter.filter(FailedState())
67 assertThat(newState, instanceOf(FailedState::class.java))
70 val filter = JsonFilter()