🚧 Add JSON filter
[rhynodge.git] / src / test / java / net / pterodactylus / rhynodge / filters / JsonFilterTest.kt
1 package net.pterodactylus.rhynodge.filters
2
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
19 import org.junit.Test
20
21 class JsonFilterTest {
22
23         @Test
24         fun `json filter is a filter implementation`() {
25                 assertThat(JsonFilter(), instanceOf(Filter::class.java))
26         }
27
28         @Test
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
33                 assertThat(
34                         jsonNode, jsonObject()
35                                 .where("state", jsonText("foo"))
36                                 .where(
37                                         "list", jsonArray(
38                                                 contains(
39                                                         jsonText("value1"),
40                                                         jsonText("value2")
41                                                 )
42                                         )
43                                 )
44                                 .where(
45                                         "object", jsonObject()
46                                                 .where("foo", jsonText("bar"))
47                                                 .where("baz", jsonNull())
48                                                 .where("quux", jsonDouble(1.5))
49                                 )
50                 )
51         }
52
53         @Test
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))
57         }
58
59         @Test
60         fun `json filter throws exception if given state is not an http state`() {
61                 assertThrows(IllegalArgumentException::class.java) { filter.filter(StringState("foo")) }
62         }
63
64         @Test
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))
68         }
69
70         val filter = JsonFilter()
71 }