Use logging method that’s present in Java 1.7
[Sone.git] / src / test / kotlin / net / pterodactylus / sone / utils / JsonTest.kt
1 package net.pterodactylus.sone.utils
2
3 import com.fasterxml.jackson.databind.node.ArrayNode
4 import com.fasterxml.jackson.databind.node.ObjectNode
5 import org.hamcrest.MatcherAssert.assertThat
6 import org.hamcrest.Matchers.equalTo
7 import org.hamcrest.Matchers.instanceOf
8 import org.hamcrest.Matchers.nullValue
9 import org.junit.Test
10
11 /**
12  * Unit test for JSON utilities.
13  */
14 class JsonTest {
15
16         @Test
17         fun `object node is created correctly`() {
18                 val objectNode = jsonObject {
19                         put("foo", "bar")
20                 }
21                 assertThat(objectNode, instanceOf(ObjectNode::class.java))
22                 assertThat(objectNode.toString(), equalTo("{\"foo\":\"bar\"}"))
23         }
24
25         @Test
26         fun `object node is created with correctly-typed properties`() {
27             val objectNode = jsonObject("string" to "foo", "int" to 1, "long" to 2L, "boolean" to true, "other" to Any())
28                 assertThat(objectNode["string"].isTextual, equalTo(true))
29                 assertThat(objectNode["string"].asText(), equalTo("foo"))
30                 assertThat(objectNode["int"].isInt, equalTo(true))
31                 assertThat(objectNode["int"].asInt(), equalTo(1))
32                 assertThat(objectNode["long"].isLong, equalTo(true))
33                 assertThat(objectNode["long"].asLong(), equalTo(2L))
34                 assertThat(objectNode["boolean"].isBoolean, equalTo(true))
35                 assertThat(objectNode["boolean"].asBoolean(), equalTo(true))
36                 assertThat(objectNode["other"], nullValue())
37         }
38
39         @Test
40         fun `array node is created correctly`() {
41                 val arrayNode = listOf(
42                                 jsonObject { put("foo", "bar") },
43                                 jsonObject { put("baz", "quo") }
44                 ).toArray()
45                 assertThat(arrayNode, instanceOf(ArrayNode::class.java))
46                 assertThat(arrayNode.toString(), equalTo("[{\"foo\":\"bar\"},{\"baz\":\"quo\"}]"))
47         }
48
49         @Test
50         fun `array is created correctly for strings`() {
51             val arrayNode = jsonArray("foo", "bar", "baz")
52                 assertThat(arrayNode.toString(), equalTo("[\"foo\",\"bar\",\"baz\"]"))
53         }
54
55 }