Add unit test for JSON return object
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sat, 19 Nov 2016 08:32:34 +0000 (09:32 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sun, 20 Nov 2016 08:23:32 +0000 (09:23 +0100)
src/test/kotlin/net/pterodactylus/sone/web/ajax/JsonReturnObjectTest.kt [new file with mode: 0644]

diff --git a/src/test/kotlin/net/pterodactylus/sone/web/ajax/JsonReturnObjectTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/ajax/JsonReturnObjectTest.kt
new file mode 100644 (file)
index 0000000..4acb8aa
--- /dev/null
@@ -0,0 +1,90 @@
+package net.pterodactylus.sone.web.ajax
+
+import com.fasterxml.jackson.databind.JsonNode
+import com.fasterxml.jackson.databind.ObjectMapper
+import com.fasterxml.jackson.databind.node.BooleanNode
+import com.fasterxml.jackson.databind.node.IntNode
+import com.fasterxml.jackson.databind.node.JsonNodeFactory
+import com.fasterxml.jackson.databind.node.ObjectNode
+import com.fasterxml.jackson.databind.node.TextNode
+import org.hamcrest.MatcherAssert.assertThat
+import org.hamcrest.Matchers.equalTo
+import org.junit.Test
+
+/**
+ * Unit test for [JsonReturnObject].
+ */
+class JsonReturnObjectTest {
+
+       private val jsonReturnObject = JsonReturnObject(true)
+       private val objectMapper = ObjectMapper()
+
+       @Test
+       fun `json object retains success status if true`() {
+               assertThat(JsonReturnObject(true).isSuccess, equalTo(true))
+       }
+
+       @Test
+       fun `json object retains success status if false`() {
+               assertThat(JsonReturnObject(false).isSuccess, equalTo(false))
+       }
+
+       @Test
+       fun `json object returns text nodes for string properties`() {
+               jsonReturnObject.put("foo", "bar")
+               assertThat(jsonReturnObject["foo"], equalTo<Any>(TextNode("bar")))
+       }
+
+       @Test
+       fun `json object returns int nodes for int properties`() {
+               jsonReturnObject.put("foo", 123)
+               assertThat(jsonReturnObject["foo"], equalTo<Any>(IntNode(123)))
+       }
+
+       @Test
+       fun `json object returns boolean nodes for boolean properties`() {
+               jsonReturnObject.put("foo", true)
+               assertThat(jsonReturnObject["foo"], equalTo<Any>(BooleanNode.TRUE))
+       }
+
+       @Test
+       fun `json object returns json node for json properties`() {
+               val objectNode = ObjectNode(JsonNodeFactory.instance)
+               jsonReturnObject.put("foo", objectNode)
+               assertThat(jsonReturnObject["foo"], equalTo<Any>(objectNode))
+       }
+
+       @Test
+       fun `json object returns all properties`() {
+               val objectNode = ObjectNode(JsonNodeFactory.instance)
+               jsonReturnObject.put("text", "text")
+               jsonReturnObject.put("int", 123)
+               jsonReturnObject.put("boolean", true)
+               jsonReturnObject.put("object", objectNode)
+               assertThat(jsonReturnObject.content, equalTo<Any>(mapOf(
+                               "text" to TextNode("text"),
+                               "int" to IntNode(123),
+                               "boolean" to BooleanNode.TRUE,
+                               "object" to objectNode
+               )))
+       }
+
+       @Test
+       fun `json object is serialized correctly`() {
+               val objectNode = ObjectNode(JsonNodeFactory.instance)
+               jsonReturnObject.put("text", "text")
+               jsonReturnObject.put("int", 123)
+               jsonReturnObject.put("boolean", true)
+               jsonReturnObject.put("object", objectNode)
+               val json = objectMapper.writeValueAsString(jsonReturnObject)
+               val parsedJson = objectMapper.readTree(json)
+               assertThat(parsedJson, equalTo<JsonNode>(ObjectNode(JsonNodeFactory.instance).apply {
+                       put("success", true)
+                       put("text", "text")
+                       put("int", 123)
+                       put("boolean", true)
+                       put("object", objectNode)
+               }))
+       }
+
+}