From: David ‘Bombe’ Roden Date: Sat, 19 Nov 2016 08:32:34 +0000 (+0100) Subject: Add unit test for JSON return object X-Git-Tag: 0.9.7^2~419 X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=2a58b5e97b774664376de1d6a676aa0f38d30b18;p=Sone.git Add unit test for JSON return object --- 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 index 0000000..4acb8aa --- /dev/null +++ b/src/test/kotlin/net/pterodactylus/sone/web/ajax/JsonReturnObjectTest.kt @@ -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(TextNode("bar"))) + } + + @Test + fun `json object returns int nodes for int properties`() { + jsonReturnObject.put("foo", 123) + assertThat(jsonReturnObject["foo"], equalTo(IntNode(123))) + } + + @Test + fun `json object returns boolean nodes for boolean properties`() { + jsonReturnObject.put("foo", true) + assertThat(jsonReturnObject["foo"], equalTo(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(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(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(ObjectNode(JsonNodeFactory.instance).apply { + put("success", true) + put("text", "text") + put("int", 123) + put("boolean", true) + put("object", objectNode) + })) + } + +}