1 package net.pterodactylus.sone.utils
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
12 * Unit test for JSON utilities.
17 fun `object node is created correctly`() {
18 val objectNode = jsonObject {
21 assertThat(objectNode, instanceOf(ObjectNode::class.java))
22 assertThat(objectNode.toString(), equalTo("{\"foo\":\"bar\"}"))
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())
40 fun `array node is created correctly`() {
41 val arrayNode = listOf(
42 jsonObject { put("foo", "bar") },
43 jsonObject { put("baz", "quo") }
45 assertThat(arrayNode, instanceOf(ArrayNode::class.java))
46 assertThat(arrayNode.toString(), equalTo("[{\"foo\":\"bar\"},{\"baz\":\"quo\"}]"))
50 fun `array is created correctly for strings`() {
51 val arrayNode = jsonArray("foo", "bar", "baz")
52 assertThat(arrayNode.toString(), equalTo("[\"foo\",\"bar\",\"baz\"]"))