From d58ef344b43543fdcfca13c07df87e194f004376 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Thu, 5 Oct 2017 22:25:20 +0200 Subject: [PATCH] Replace JSON return object with Kotlin version --- .../sone/web/ajax/JsonReturnObject.java | 150 --------------------- .../sone/web/ajax/JsonReturnObject.kt | 46 +++++++ .../sone/web/ajax/CreatePostAjaxPageTest.kt | 18 +-- .../sone/web/ajax/CreateReplyAjaxPageTest.kt | 12 +- .../sone/web/ajax/DistrustAjaxPageTest.kt | 2 +- .../sone/web/ajax/EditAlbumAjaxPageTest.kt | 14 +- .../sone/web/ajax/EditImageAjaxPageTest.kt | 14 +- .../sone/web/ajax/GetLikesAjaxPageTest.kt | 8 +- .../sone/web/ajax/GetLinkedElementAjaxPageTest.kt | 2 +- .../sone/web/ajax/GetNotificationsAjaxPageTest.kt | 20 +-- .../sone/web/ajax/GetPostAjaxPageTest.kt | 10 +- .../sone/web/ajax/GetReplyAjaxPageTest.kt | 10 +- .../sone/web/ajax/GetStatusAjaxPageTest.kt | 18 +-- .../sone/web/ajax/GetTimesAjaxPageTest.kt | 20 +-- .../sone/web/ajax/GetTranslationAjaxPageTest.kt | 2 +- .../sone/web/ajax/TrustAjaxPageTest.kt | 2 +- 16 files changed, 122 insertions(+), 226 deletions(-) delete mode 100644 src/main/java/net/pterodactylus/sone/web/ajax/JsonReturnObject.java create mode 100644 src/main/kotlin/net/pterodactylus/sone/web/ajax/JsonReturnObject.kt diff --git a/src/main/java/net/pterodactylus/sone/web/ajax/JsonReturnObject.java b/src/main/java/net/pterodactylus/sone/web/ajax/JsonReturnObject.java deleted file mode 100644 index 45bbb4f..0000000 --- a/src/main/java/net/pterodactylus/sone/web/ajax/JsonReturnObject.java +++ /dev/null @@ -1,150 +0,0 @@ -/* - * © 2013 xplosion interactive - */ - -package net.pterodactylus.sone.web.ajax; - -import java.util.Map; - -import com.fasterxml.jackson.annotation.JsonAnyGetter; -import com.fasterxml.jackson.annotation.JsonProperty; -import com.fasterxml.jackson.databind.JsonNode; -import com.fasterxml.jackson.databind.node.BooleanNode; -import com.fasterxml.jackson.databind.node.IntNode; -import com.fasterxml.jackson.databind.node.TextNode; -import com.google.common.annotations.VisibleForTesting; -import com.google.common.collect.Maps; - -/** - * JSON return object for AJAX requests. - * - * @author David Roden - */ -public class JsonReturnObject { - - /** Whether the request was successful. */ - @JsonProperty - private final boolean success; - - /** The returned values. */ - private final Map content = Maps.newHashMap(); - - /** - * Creates a new JSON return object. - * - * @param success - * {@code true} if the request was successful, {@code false} otherwise - */ - public JsonReturnObject(boolean success) { - this.success = success; - } - - // - // ACCESSORS - // - - /** - * Returns whether the request was successful. - * - * @return {@code true} if the request was successful, {@code false} otherwise - */ - @VisibleForTesting - public boolean isSuccess() { - return success; - } - - /** - * Returns the value stored under the given key. - * - * @param key - * The key of the value to retrieve - * @return The value of the key, or {@code null} if there is no value for the - * given key - */ - @VisibleForTesting - public JsonNode get(String key) { - return content.get(key); - } - - /** - * Returns the content of this object for serialization. - * - * @return The content of this object - */ - @JsonAnyGetter - public Map getContent() { - return content; - } - - // - // ACTIONS - // - - /** - * Stores the given value under the given key. - * - * @param key - * The key under which to store the value - * @param value - * The value to store - * @return This JSON return object - */ - public JsonReturnObject put(String key, boolean value) { - return put(key, BooleanNode.valueOf(value)); - } - - /** - * Stores the given value under the given key. - * - * @param key - * The key under which to store the value - * @param value - * The value to store - * @return This JSON return object - */ - public JsonReturnObject put(String key, int value) { - return put(key, new IntNode(value)); - } - - /** - * Stores the given value under the given key. - * - * @param key - * The key under which to store the value - * @param value - * The value to store - * @return This JSON return object - */ - public JsonReturnObject put(String key, String value) { - return put(key, TextNode.valueOf(value)); - } - - /** - * Stores the given value under the given key. - * - * @param key - * The key under which to store the value - * @param value - * The value to store - * @return This JSON return object - */ - public JsonReturnObject put(String key, JsonNode value) { - content.put(key, value); - return this; - } - - @Override - public int hashCode() { - return Boolean.valueOf(success).hashCode() ^ content.hashCode(); - } - - @Override - public boolean equals(Object object) { - if ((object == null) || (object.getClass() != getClass())) { - return false; - } - JsonReturnObject other = (JsonReturnObject) object; - return (success == other.success) && content.equals(other.content); - } - -} diff --git a/src/main/kotlin/net/pterodactylus/sone/web/ajax/JsonReturnObject.kt b/src/main/kotlin/net/pterodactylus/sone/web/ajax/JsonReturnObject.kt new file mode 100644 index 0000000..3d18d05 --- /dev/null +++ b/src/main/kotlin/net/pterodactylus/sone/web/ajax/JsonReturnObject.kt @@ -0,0 +1,46 @@ +package net.pterodactylus.sone.web.ajax + +import com.fasterxml.jackson.annotation.JsonAnyGetter +import com.fasterxml.jackson.databind.JsonNode +import com.fasterxml.jackson.databind.node.BooleanNode +import com.fasterxml.jackson.databind.node.IntNode +import com.fasterxml.jackson.databind.node.TextNode + +/** + * JSON return object for AJAX requests. + */ +open class JsonReturnObject(val isSuccess: Boolean) { + + private val values = mutableMapOf() + + val content: Map + @JsonAnyGetter get() = values + + operator fun get(key: String) = values[key] + + fun put(key: String, value: String?) = apply { + values[key] = TextNode.valueOf(value) + } + + fun put(key: String, value: Int) = apply { + values[key] = IntNode.valueOf(value) + } + + fun put(key: String, value: Boolean) = apply { + values[key] = BooleanNode.valueOf(value) + } + + fun put(key: String, value: JsonNode) = apply { + values[key] = value + } + + override fun hashCode(): Int { + return isSuccess.hashCode() xor content.hashCode() + } + + override fun equals(other: Any?) = + (other as? JsonReturnObject)?.let { + it.isSuccess == isSuccess && it.content == content + } ?: false + +} diff --git a/src/test/kotlin/net/pterodactylus/sone/web/ajax/CreatePostAjaxPageTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/ajax/CreatePostAjaxPageTest.kt index 47fee5b..8d7e521 100644 --- a/src/test/kotlin/net/pterodactylus/sone/web/ajax/CreatePostAjaxPageTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/web/ajax/CreatePostAjaxPageTest.kt @@ -42,8 +42,8 @@ class CreatePostAjaxPageTest : JsonPageTest("createPost.ajax", pageSupplier = :: val post = createPost() whenever(core.createPost(currentSone, Optional.absent(), "test")).thenReturn(post) assertThat(json.isSuccess, equalTo(true)) - assertThat(json["postId"].asText(), equalTo("id")) - assertThat(json["sone"].asText(), equalTo(currentSone.id)) + assertThat(json["postId"]?.asText(), equalTo("id")) + assertThat(json["sone"]?.asText(), equalTo(currentSone.id)) assertThat(json["recipient"], nullValue()) } @@ -54,8 +54,8 @@ class CreatePostAjaxPageTest : JsonPageTest("createPost.ajax", pageSupplier = :: val post = createPost() whenever(core.createPost(currentSone, Optional.absent(), "test")).thenReturn(post) assertThat(json.isSuccess, equalTo(true)) - assertThat(json["postId"].asText(), equalTo("id")) - assertThat(json["sone"].asText(), equalTo(currentSone.id)) + assertThat(json["postId"]?.asText(), equalTo("id")) + assertThat(json["sone"]?.asText(), equalTo(currentSone.id)) assertThat(json["recipient"], nullValue()) } @@ -68,9 +68,9 @@ class CreatePostAjaxPageTest : JsonPageTest("createPost.ajax", pageSupplier = :: val post = createPost("valid") whenever(core.createPost(currentSone, Optional.of(recipient), "test")).thenReturn(post) assertThat(json.isSuccess, equalTo(true)) - assertThat(json["postId"].asText(), equalTo("id")) - assertThat(json["sone"].asText(), equalTo(currentSone.id)) - assertThat(json["recipient"].asText(), equalTo("valid")) + assertThat(json["postId"]?.asText(), equalTo("id")) + assertThat(json["sone"]?.asText(), equalTo(currentSone.id)) + assertThat(json["recipient"]?.asText(), equalTo("valid")) } @Test @@ -80,8 +80,8 @@ class CreatePostAjaxPageTest : JsonPageTest("createPost.ajax", pageSupplier = :: val post = createPost() whenever(core.createPost(currentSone, Optional.absent(), "Link KSK@foo is filtered")).thenReturn(post) assertThat(json.isSuccess, equalTo(true)) - assertThat(json["postId"].asText(), equalTo("id")) - assertThat(json["sone"].asText(), equalTo(currentSone.id)) + assertThat(json["postId"]?.asText(), equalTo("id")) + assertThat(json["sone"]?.asText(), equalTo(currentSone.id)) assertThat(json["recipient"], nullValue()) } diff --git a/src/test/kotlin/net/pterodactylus/sone/web/ajax/CreateReplyAjaxPageTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/ajax/CreateReplyAjaxPageTest.kt index b192f84..6931b69 100644 --- a/src/test/kotlin/net/pterodactylus/sone/web/ajax/CreateReplyAjaxPageTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/web/ajax/CreateReplyAjaxPageTest.kt @@ -28,8 +28,8 @@ class CreateReplyAjaxPageTest : JsonPageTest("createReply.ajax", pageSupplier = whenever(core.createReply(currentSone, post, "")).thenReturn(reply) addRequestParameter("post", "post-id") assertThat(json.isSuccess, equalTo(true)) - assertThat(json["reply"].asText(), equalTo("reply-id")) - assertThat(json["sone"].asText(), equalTo("soneId")) + assertThat(json["reply"]?.asText(), equalTo("reply-id")) + assertThat(json["sone"]?.asText(), equalTo("soneId")) } @Test @@ -42,8 +42,8 @@ class CreateReplyAjaxPageTest : JsonPageTest("createReply.ajax", pageSupplier = addRequestParameter("text", "Text with http://127.0.0.1:8888/KSK@foo.bar link") addRequestHeader("Host", "127.0.0.1:8888") assertThat(json.isSuccess, equalTo(true)) - assertThat(json["reply"].asText(), equalTo("reply-id")) - assertThat(json["sone"].asText(), equalTo("soneId")) + assertThat(json["reply"]?.asText(), equalTo("reply-id")) + assertThat(json["sone"]?.asText(), equalTo("soneId")) } @Test @@ -58,8 +58,8 @@ class CreateReplyAjaxPageTest : JsonPageTest("createReply.ajax", pageSupplier = addRequestParameter("text", "Text") addRequestParameter("sender", "local-sone") assertThat(json.isSuccess, equalTo(true)) - assertThat(json["reply"].asText(), equalTo("reply-id")) - assertThat(json["sone"].asText(), equalTo("local-sone")) + assertThat(json["reply"]?.asText(), equalTo("reply-id")) + assertThat(json["sone"]?.asText(), equalTo("local-sone")) } } diff --git a/src/test/kotlin/net/pterodactylus/sone/web/ajax/DistrustAjaxPageTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/ajax/DistrustAjaxPageTest.kt index 770d068..dbe2ed5 100644 --- a/src/test/kotlin/net/pterodactylus/sone/web/ajax/DistrustAjaxPageTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/web/ajax/DistrustAjaxPageTest.kt @@ -41,7 +41,7 @@ class DistrustAjaxPageTest : JsonPageTest("distrustSone.ajax", pageSupplier = :: addSone(sone, "sone-id") addRequestParameter("sone", "sone-id") assertThat(json.isSuccess, equalTo(true)) - assertThat(json["trustValue"].asInt(), equalTo(-33)) + assertThat(json["trustValue"]?.asInt(), equalTo(-33)) } } diff --git a/src/test/kotlin/net/pterodactylus/sone/web/ajax/EditAlbumAjaxPageTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/ajax/EditAlbumAjaxPageTest.kt index 2d18366..d540344 100644 --- a/src/test/kotlin/net/pterodactylus/sone/web/ajax/EditAlbumAjaxPageTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/web/ajax/EditAlbumAjaxPageTest.kt @@ -46,8 +46,8 @@ class EditAlbumAjaxPageTest : JsonPageTest("editAlbum.ajax", pageSupplier = ::Ed addRequestParameter("album", "album-id") addRequestParameter("moveLeft", "true") assertThat(json.isSuccess, equalTo(true)) - assertThat(json["sourceAlbumId"].asText(), equalTo("album-id")) - assertThat(json["destinationAlbumId"].asText(), equalTo("swapped")) + assertThat(json["sourceAlbumId"]?.asText(), equalTo("album-id")) + assertThat(json["destinationAlbumId"]?.asText(), equalTo("swapped")) } @Test @@ -61,8 +61,8 @@ class EditAlbumAjaxPageTest : JsonPageTest("editAlbum.ajax", pageSupplier = ::Ed addRequestParameter("album", "album-id") addRequestParameter("moveRight", "true") assertThat(json.isSuccess, equalTo(true)) - assertThat(json["sourceAlbumId"].asText(), equalTo("album-id")) - assertThat(json["destinationAlbumId"].asText(), equalTo("swapped")) + assertThat(json["sourceAlbumId"]?.asText(), equalTo("album-id")) + assertThat(json["destinationAlbumId"]?.asText(), equalTo("swapped")) } @Test @@ -85,9 +85,9 @@ class EditAlbumAjaxPageTest : JsonPageTest("editAlbum.ajax", pageSupplier = ::Ed addRequestParameter("description", "foo http://127.0.0.1:8888/KSK@foo.html link") addRequestHeader("Host", "127.0.0.1:8888") assertThat(json.isSuccess, equalTo(true)) - assertThat(json["albumId"].asText(), equalTo("album-id")) - assertThat(json["title"].asText(), equalTo("new title")) - assertThat(json["description"].asText(), equalTo("foo KSK@foo.html link")) + assertThat(json["albumId"]?.asText(), equalTo("album-id")) + assertThat(json["title"]?.asText(), equalTo("new title")) + assertThat(json["description"]?.asText(), equalTo("foo KSK@foo.html link")) } } diff --git a/src/test/kotlin/net/pterodactylus/sone/web/ajax/EditImageAjaxPageTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/ajax/EditImageAjaxPageTest.kt index 6a3d89c..126fa83 100644 --- a/src/test/kotlin/net/pterodactylus/sone/web/ajax/EditImageAjaxPageTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/web/ajax/EditImageAjaxPageTest.kt @@ -57,8 +57,8 @@ class EditImageAjaxPageTest : JsonPageTest("editImage.ajax") { addRequestParameter("image", "image-id") addRequestParameter("moveLeft", "true") assertThat(json.isSuccess, equalTo(true)) - assertThat(json["sourceImageId"].asText(), equalTo("image-id")) - assertThat(json["destinationImageId"].asText(), equalTo("swapped")) + assertThat(json["sourceImageId"]?.asText(), equalTo("image-id")) + assertThat(json["destinationImageId"]?.asText(), equalTo("swapped")) verify(core).touchConfiguration() } @@ -75,8 +75,8 @@ class EditImageAjaxPageTest : JsonPageTest("editImage.ajax") { addRequestParameter("image", "image-id") addRequestParameter("moveRight", "true") assertThat(json.isSuccess, equalTo(true)) - assertThat(json["sourceImageId"].asText(), equalTo("image-id")) - assertThat(json["destinationImageId"].asText(), equalTo("swapped")) + assertThat(json["sourceImageId"]?.asText(), equalTo("image-id")) + assertThat(json["destinationImageId"]?.asText(), equalTo("swapped")) verify(core).touchConfiguration() } @@ -107,9 +107,9 @@ class EditImageAjaxPageTest : JsonPageTest("editImage.ajax") { addRequestParameter("description", "some http://127.0.0.1:8888/KSK@foo link") addRequestHeader("Host", "127.0.0.1:8888") assertThat(json.isSuccess, equalTo(true)) - assertThat(json["title"].asText(), equalTo("some title")) - assertThat(json["description"].asText(), equalTo("some KSK@foo link")) - assertThat(json["parsedDescription"].asText(), equalTo("rendered description")) + assertThat(json["title"]?.asText(), equalTo("some title")) + assertThat(json["description"]?.asText(), equalTo("some KSK@foo link")) + assertThat(json["parsedDescription"]?.asText(), equalTo("rendered description")) verify(core).touchConfiguration() val parameterCaptor = argumentCaptor>() verify(parserFilter).format(any(), any(), parameterCaptor.capture()) diff --git a/src/test/kotlin/net/pterodactylus/sone/web/ajax/GetLikesAjaxPageTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/ajax/GetLikesAjaxPageTest.kt index 777d958..1b1255e 100644 --- a/src/test/kotlin/net/pterodactylus/sone/web/ajax/GetLikesAjaxPageTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/web/ajax/GetLikesAjaxPageTest.kt @@ -44,8 +44,8 @@ class GetLikesAjaxPageTest : JsonPageTest("getLikes.ajax", needsFormPassword = f addRequestParameter("type", "post") addRequestParameter("post", "post-id") assertThat(json.isSuccess, equalTo(true)) - assertThat(json["likes"].asInt(), equalTo(3)) - assertThat(json["sones"].toList().map { it["id"].asText() to it["name"].asText() }, contains( + assertThat(json["likes"]?.asInt(), equalTo(3)) + assertThat(json["sones"]!!.toList().map { it["id"].asText() to it["name"].asText() }, contains( "S1" to "F1 M1 L1", "S2" to "F2 M2 L2", "S3" to "F3 M3 L3" @@ -75,8 +75,8 @@ class GetLikesAjaxPageTest : JsonPageTest("getLikes.ajax", needsFormPassword = f addRequestParameter("type", "reply") addRequestParameter("reply", "reply-id") assertThat(json.isSuccess, equalTo(true)) - assertThat(json["likes"].asInt(), equalTo(3)) - assertThat(json["sones"].toList().map { it["id"].asText() to it["name"].asText() }, contains( + assertThat(json["likes"]?.asInt(), equalTo(3)) + assertThat(json["sones"]!!.toList().map { it["id"].asText() to it["name"].asText() }, contains( "S1" to "F1 M1 L1", "S2" to "F2 M2 L2", "S3" to "F3 M3 L3" diff --git a/src/test/kotlin/net/pterodactylus/sone/web/ajax/GetLinkedElementAjaxPageTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/ajax/GetLinkedElementAjaxPageTest.kt index 92ec1d4..f49f9f3 100644 --- a/src/test/kotlin/net/pterodactylus/sone/web/ajax/GetLinkedElementAjaxPageTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/web/ajax/GetLinkedElementAjaxPageTest.kt @@ -34,7 +34,7 @@ class GetLinkedElementAjaxPageTest: JsonPageTest("getLinkedElement.ajax", requir else -> null } } - assertThat(json.get("linkedElements").elements().asSequence().map { it.toMap() }.toList(), Matchers.containsInAnyOrder( + assertThat(json.get("linkedElements")!!.elements().asSequence().map { it.toMap() }.toList(), Matchers.containsInAnyOrder( mapOf("link" to "KSK@foo.jpg", "html" to "jpeg-image"), mapOf("link" to "KSK@foo.html", "html" to "html-page") )) diff --git a/src/test/kotlin/net/pterodactylus/sone/web/ajax/GetNotificationsAjaxPageTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/ajax/GetNotificationsAjaxPageTest.kt index 9084260..f2206e3 100644 --- a/src/test/kotlin/net/pterodactylus/sone/web/ajax/GetNotificationsAjaxPageTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/web/ajax/GetNotificationsAjaxPageTest.kt @@ -44,15 +44,15 @@ class GetNotificationsAjaxPageTest : JsonPageTest("getNotifications.ajax", requi fun `notification hash is calculated correctly`() { testNotifications.forEach { addNotification(it) } assertThat(json.isSuccess, equalTo(true)) - assertThat(json["notificationHash"].asInt(), equalTo(listOf(1, 0, 2).map(testNotifications::get).hashCode())) + assertThat(json["notificationHash"]?.asInt(), equalTo(listOf(1, 0, 2).map(testNotifications::get).hashCode())) } @Test fun `options are included correctly`() { assertThat(json.isSuccess, equalTo(true)) - assertThat(json["options"]["ShowNotification/NewSones"].asBoolean(), equalTo(true)) - assertThat(json["options"]["ShowNotification/NewPosts"].asBoolean(), equalTo(true)) - assertThat(json["options"]["ShowNotification/NewReplies"].asBoolean(), equalTo(true)) + assertThat(json["options"]!!["ShowNotification/NewSones"].asBoolean(), equalTo(true)) + assertThat(json["options"]!!["ShowNotification/NewPosts"].asBoolean(), equalTo(true)) + assertThat(json["options"]!!["ShowNotification/NewReplies"].asBoolean(), equalTo(true)) } @Test @@ -61,23 +61,23 @@ class GetNotificationsAjaxPageTest : JsonPageTest("getNotifications.ajax", requi currentSone.options.isShowNewPostNotifications = false currentSone.options.isShowNewReplyNotifications = false assertThat(json.isSuccess, equalTo(true)) - assertThat(json["options"]["ShowNotification/NewSones"].asBoolean(), equalTo(false)) - assertThat(json["options"]["ShowNotification/NewPosts"].asBoolean(), equalTo(false)) - assertThat(json["options"]["ShowNotification/NewReplies"].asBoolean(), equalTo(false)) + assertThat(json["options"]!!["ShowNotification/NewSones"].asBoolean(), equalTo(false)) + assertThat(json["options"]!!["ShowNotification/NewPosts"].asBoolean(), equalTo(false)) + assertThat(json["options"]!!["ShowNotification/NewReplies"].asBoolean(), equalTo(false)) } @Test fun `options are not included if user is not logged in`() { unsetCurrentSone() assertThat(json.isSuccess, equalTo(true)) - assertThat(json["options"].toList(), empty()) + assertThat(json["options"]?.toList(), empty()) } @Test fun `notifications are rendered correctly`() { testNotifications.forEach { addNotification(it) } assertThat(json.isSuccess, equalTo(true)) - assertThat(json["notifications"].toList().map { node -> listOf("id", "text", "createdTime", "lastUpdatedTime", "dismissable").map { it to node.get(it).asText() }.toMap() }, containsInAnyOrder( + assertThat(json["notifications"]!!.toList().map { node -> listOf("id", "text", "createdTime", "lastUpdatedTime", "dismissable").map { it to node.get(it).asText() }.toMap() }, containsInAnyOrder( mapOf("id" to "n1", "createdTime" to "2000", "lastUpdatedTime" to "5000", "dismissable" to "true", "text" to "t1"), mapOf("id" to "n2", "createdTime" to "1000", "lastUpdatedTime" to "6000", "dismissable" to "false", "text" to "t2"), mapOf("id" to "n3", "createdTime" to "3000", "lastUpdatedTime" to "7000", "dismissable" to "true", "text" to "t3") @@ -100,7 +100,7 @@ class GetNotificationsAjaxPageTest : JsonPageTest("getNotifications.ajax", requi testNotifications.forEach { addNotification(it) } addNotification(templateNotification) assertThat(json.isSuccess, equalTo(true)) - assertThat(json["notifications"].last()["text"].asText(), equalTo("t4")) + assertThat(json["notifications"]!!.last()["text"].asText(), equalTo("t4")) val templateContext = argumentCaptor() verify(templateNotification).render(templateContext.capture(), any()) assertThat(templateContext.value["core"], equalTo(core)) diff --git a/src/test/kotlin/net/pterodactylus/sone/web/ajax/GetPostAjaxPageTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/ajax/GetPostAjaxPageTest.kt index 768a186..e729b24 100644 --- a/src/test/kotlin/net/pterodactylus/sone/web/ajax/GetPostAjaxPageTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/web/ajax/GetPostAjaxPageTest.kt @@ -39,11 +39,11 @@ class GetPostAjaxPageTest : JsonPageTest("getPost.ajax", needsFormPassword = fal addPost(post) addRequestParameter("post", "post-id") assertThat(json.isSuccess, equalTo(true)) - assertThat(json["post"]["id"].asText(), equalTo("post-id")) - assertThat(json["post"]["time"].asLong(), equalTo(1000L)) - assertThat(json["post"]["sone"].asText(), equalTo("sone-id")) - assertThat(json["post"]["recipient"].asText(), equalTo("recipient-id")) - assertThat(json["post"]["html"].asText(), equalTo(listOf( + assertThat(json["post"]!!["id"].asText(), equalTo("post-id")) + assertThat(json["post"]!!["time"].asLong(), equalTo(1000L)) + assertThat(json["post"]!!["sone"].asText(), equalTo("sone-id")) + assertThat(json["post"]!!["recipient"].asText(), equalTo("recipient-id")) + assertThat(json["post"]!!["html"].asText(), equalTo(listOf( core.toString(), freenetRequest.toString(), "post text", diff --git a/src/test/kotlin/net/pterodactylus/sone/web/ajax/GetReplyAjaxPageTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/ajax/GetReplyAjaxPageTest.kt index 8fe16e2..17792e2 100644 --- a/src/test/kotlin/net/pterodactylus/sone/web/ajax/GetReplyAjaxPageTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/web/ajax/GetReplyAjaxPageTest.kt @@ -38,11 +38,11 @@ class GetReplyAjaxPageTest : JsonPageTest("getReply.ajax", needsFormPassword = f addReply(reply) addRequestParameter("reply", "reply-id") assertThat(json.isSuccess, equalTo(true)) - assertThat(json["reply"]["id"].asText(), equalTo("reply-id")) - assertThat(json["reply"]["soneId"].asText(), equalTo("sone-id")) - assertThat(json["reply"]["postId"].asText(), equalTo("post-id")) - assertThat(json["reply"]["time"].asLong(), equalTo(1000L)) - assertThat(json["reply"]["html"].asText(), equalTo(listOf( + assertThat(json["reply"]!!["id"].asText(), equalTo("reply-id")) + assertThat(json["reply"]!!["soneId"].asText(), equalTo("sone-id")) + assertThat(json["reply"]!!["postId"].asText(), equalTo("post-id")) + assertThat(json["reply"]!!["time"].asLong(), equalTo(1000L)) + assertThat(json["reply"]!!["html"].asText(), equalTo(listOf( core.toString(), freenetRequest.toString(), "reply text", diff --git a/src/test/kotlin/net/pterodactylus/sone/web/ajax/GetStatusAjaxPageTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/ajax/GetStatusAjaxPageTest.kt index 0d8cb54..9576c7c 100644 --- a/src/test/kotlin/net/pterodactylus/sone/web/ajax/GetStatusAjaxPageTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/web/ajax/GetStatusAjaxPageTest.kt @@ -42,18 +42,18 @@ class GetStatusAjaxPageTest: JsonPageTest("getStatus.ajax", requiresLogin = fals @Test fun `page returns correct attribute “loggedIn” if sone is logged in`() { - assertThat(json.get("loggedIn").asText(), equalTo("true")) + assertThat(json.get("loggedIn")?.asText(), equalTo("true")) } @Test fun `page returns correct attribute “loggedIn” if sone is not logged in`() { unsetCurrentSone() - assertThat(json.get("loggedIn").asText(), equalTo("false")) + assertThat(json.get("loggedIn")?.asText(), equalTo("false")) } @Test fun `page returns options for sone if sone is logged in`() { - assertThat(json.get("options").toMap(), allOf( + assertThat(json.get("options")?.toMap(), allOf( hasEntry("ShowNotification/NewSones", "true"), hasEntry("ShowNotification/NewPosts", "true"), hasEntry("ShowNotification/NewReplies", "true") @@ -74,7 +74,7 @@ class GetStatusAjaxPageTest: JsonPageTest("getStatus.ajax", requiresLogin = fals @Test fun `page returns a sones object with the current sone if not other sones parameter is given`() { - assertThat(json.get("sones").elements().asSequence().map { it.toMap() }.toList(), containsInAnyOrder( + assertThat(json.get("sones")!!.elements().asSequence().map { it.toMap() }.toList(), containsInAnyOrder( mapOf("id" to "soneId", "name" to "Sone_Id", "local" to "true", "status" to "idle", "modified" to "false", "locked" to "false", "lastUpdatedUnknown" to "false", "lastUpdated" to "Jan 1, 1970, 00:00:01", "lastUpdatedText" to "1000") )) } @@ -84,7 +84,7 @@ class GetStatusAjaxPageTest: JsonPageTest("getStatus.ajax", requiresLogin = fals addSone(deepMock().mock("sone1", "Sone 1", false, 2000, downloading)) addSone(deepMock().mock("sone3", "Sone 3", true, 3000, inserting)) addRequestParameter("soneIds", "sone1,sone2,sone3") - assertThat(json.get("sones").elements().asSequence().map { it.toMap() }.toList(), containsInAnyOrder( + assertThat(json.get("sones")!!.elements().asSequence().map { it.toMap() }.toList(), containsInAnyOrder( mapOf("id" to "soneId", "name" to "Sone_Id", "local" to "true", "status" to "idle", "modified" to "false", "locked" to "false", "lastUpdatedUnknown" to "false", "lastUpdated" to "Jan 1, 1970, 00:00:01", "lastUpdatedText" to "1000"), mapOf("id" to "sone1", "name" to "Sone 1", "local" to "false", "status" to "downloading", "modified" to "false", "locked" to "false", "lastUpdatedUnknown" to "false", "lastUpdated" to "Jan 1, 1970, 00:00:02", "lastUpdatedText" to "2000"), mapOf("id" to "sone3", "name" to "Sone 3", "local" to "true", "status" to "inserting", "modified" to "false", "locked" to "false", "lastUpdatedUnknown" to "false", "lastUpdated" to "Jan 1, 1970, 00:00:03", "lastUpdatedText" to "3000") @@ -98,14 +98,14 @@ class GetStatusAjaxPageTest: JsonPageTest("getStatus.ajax", requiresLogin = fals mock().apply { whenever(this.createdTime).thenReturn(1000) } ) notifications.forEachIndexed { index, notification -> addNotification(notification, "notification$index")} - assertThat(json.get("notificationHash").asInt(), equalTo(notifications.sortedBy { it.createdTime }.hashCode())) + assertThat(json.get("notificationHash")?.asInt(), equalTo(notifications.sortedBy { it.createdTime }.hashCode())) } @Test fun `page returns new posts`() { addNewPost("post1", "sone1", 1000) addNewPost("post2", "sone2", 2000, "sone1") - assertThat(json.get("newPosts").elements().asSequence().map { it.toMap() }.toList(), containsInAnyOrder( + assertThat(json.get("newPosts")!!.elements().asSequence().map { it.toMap() }.toList(), containsInAnyOrder( mapOf("id" to "post1", "sone" to "sone1", "time" to "1000", "recipient" to null), mapOf("id" to "post2", "sone" to "sone2", "time" to "2000", "recipient" to "sone1") )) @@ -115,7 +115,7 @@ class GetStatusAjaxPageTest: JsonPageTest("getStatus.ajax", requiresLogin = fals fun `page returns new replies`() { addNewReply("reply1", "sone1", "post1", "sone11") addNewReply("reply2", "sone2", "post2", "sone22") - assertThat(json.get("newReplies").elements().asSequence().map { it.toMap() }.toList(), containsInAnyOrder( + assertThat(json.get("newReplies")!!.elements().asSequence().map { it.toMap() }.toList(), containsInAnyOrder( mapOf("id" to "reply1", "sone" to "sone1", "post" to "post1", "postSone" to "sone11"), mapOf("id" to "reply2", "sone" to "sone2", "post" to "post2", "postSone" to "sone22") )) @@ -127,7 +127,7 @@ class GetStatusAjaxPageTest: JsonPageTest("getStatus.ajax", requiresLogin = fals addLinkedElement("KSK@test.html", loading = true, failed = false) addLinkedElement("KSK@test.jpeg", loading = false, failed = true) addRequestParameter("elements", jsonArray("KSK@test.png", "KSK@test.html", "KSK@test.jpeg").toString()) - assertThat(json.get("linkedElements").elements().asSequence().map { it.toMap() }.toList(), containsInAnyOrder( + assertThat(json.get("linkedElements")!!.elements().asSequence().map { it.toMap() }.toList(), containsInAnyOrder( mapOf("link" to "KSK@test.png", "loading" to "false", "failed" to "false"), mapOf("link" to "KSK@test.html", "loading" to "true", "failed" to "false"), mapOf("link" to "KSK@test.jpeg", "loading" to "false", "failed" to "true") diff --git a/src/test/kotlin/net/pterodactylus/sone/web/ajax/GetTimesAjaxPageTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/ajax/GetTimesAjaxPageTest.kt index 5bffe09..fa86867 100644 --- a/src/test/kotlin/net/pterodactylus/sone/web/ajax/GetTimesAjaxPageTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/web/ajax/GetTimesAjaxPageTest.kt @@ -55,8 +55,8 @@ class GetTimesAjaxPageTest : JsonPageTest("getTimes.ajax", needsFormPassword = f @Test fun `request without any parameters responds with empty post and reply times`() { assertThat(json.isSuccess, equalTo(true)) - assertThat(json["postTimes"].toList(), emptyIterable()) - assertThat(json["replyTimes"].toList(), emptyIterable()) + assertThat(json["postTimes"]?.toList(), emptyIterable()) + assertThat(json["replyTimes"]?.toList(), emptyIterable()) } @Test @@ -64,10 +64,10 @@ class GetTimesAjaxPageTest : JsonPageTest("getTimes.ajax", needsFormPassword = f addPost(testPosts[0]) addRequestParameter("posts", "post1") assertThat(json.isSuccess, equalTo(true)) - assertThat(json["postTimes"].fields().asSequence().map { it.key to it.value }.toList(), containsInAnyOrder>( + assertThat(json["postTimes"]!!.fields().asSequence().map { it.key to it.value }.toList(), containsInAnyOrder>( "post1" to jsonObject("timeText" to "1000", "refreshTime" to 2L, "tooltip" to "Jan 1, 1970, 00:00:01") )) - assertThat(json["replyTimes"].toList(), emptyIterable()) + assertThat(json["replyTimes"]?.toList(), emptyIterable()) } @Test @@ -75,8 +75,8 @@ class GetTimesAjaxPageTest : JsonPageTest("getTimes.ajax", needsFormPassword = f addReply(testReplies[0]) addRequestParameter("replies", "reply1") assertThat(json.isSuccess, equalTo(true)) - assertThat(json["postTimes"].toList(), emptyIterable()) - assertThat(json["replyTimes"].fields().asSequence().map { it.key to it.value }.toList(), containsInAnyOrder>( + assertThat(json["postTimes"]?.toList(), emptyIterable()) + assertThat(json["replyTimes"]!!.fields().asSequence().map { it.key to it.value }.toList(), containsInAnyOrder>( "reply1" to jsonObject("timeText" to "1000", "refreshTime" to 2L, "tooltip" to "Jan 1, 1970, 00:00:01") )) } @@ -87,11 +87,11 @@ class GetTimesAjaxPageTest : JsonPageTest("getTimes.ajax", needsFormPassword = f addPost(testPosts[1]) addRequestParameter("posts", "post1,post2,post3") assertThat(json.isSuccess, equalTo(true)) - assertThat(json["postTimes"].fields().asSequence().map { it.key to it.value }.toList(), containsInAnyOrder>( + assertThat(json["postTimes"]!!.fields().asSequence().map { it.key to it.value }.toList(), containsInAnyOrder>( "post1" to jsonObject("timeText" to "1000", "refreshTime" to 2L, "tooltip" to "Jan 1, 1970, 00:00:01"), "post2" to jsonObject("timeText" to "2000", "refreshTime" to 4L, "tooltip" to "Jan 1, 1970, 00:00:02") )) - assertThat(json["replyTimes"].toList(), emptyIterable()) + assertThat(json["replyTimes"]?.toList(), emptyIterable()) } @Test @@ -100,8 +100,8 @@ class GetTimesAjaxPageTest : JsonPageTest("getTimes.ajax", needsFormPassword = f addReply(testReplies[1]) addRequestParameter("replies", "reply1,reply2,reply3") assertThat(json.isSuccess, equalTo(true)) - assertThat(json["postTimes"].toList(), emptyIterable()) - assertThat(json["replyTimes"].fields().asSequence().map { it.key to it.value }.toList(), containsInAnyOrder>( + assertThat(json["postTimes"]?.toList(), emptyIterable()) + assertThat(json["replyTimes"]!!.fields().asSequence().map { it.key to it.value }.toList(), containsInAnyOrder>( "reply1" to jsonObject("timeText" to "1000", "refreshTime" to 2L, "tooltip" to "Jan 1, 1970, 00:00:01"), "reply2" to jsonObject("timeText" to "2000", "refreshTime" to 4L, "tooltip" to "Jan 1, 1970, 00:00:02") )) diff --git a/src/test/kotlin/net/pterodactylus/sone/web/ajax/GetTranslationAjaxPageTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/ajax/GetTranslationAjaxPageTest.kt index 30a1a72..87ea2c6 100644 --- a/src/test/kotlin/net/pterodactylus/sone/web/ajax/GetTranslationAjaxPageTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/web/ajax/GetTranslationAjaxPageTest.kt @@ -14,7 +14,7 @@ class GetTranslationAjaxPageTest : JsonPageTest("getTranslation.ajax", requiresL addTranslation("foo", "bar") addRequestParameter("key", "foo") assertThat(json.isSuccess, equalTo(true)) - assertThat(json["value"].asText(), equalTo("bar")) + assertThat(json["value"]?.asText(), equalTo("bar")) } } diff --git a/src/test/kotlin/net/pterodactylus/sone/web/ajax/TrustAjaxPageTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/ajax/TrustAjaxPageTest.kt index b62d40b..7fc726a 100644 --- a/src/test/kotlin/net/pterodactylus/sone/web/ajax/TrustAjaxPageTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/web/ajax/TrustAjaxPageTest.kt @@ -34,7 +34,7 @@ class TrustAjaxPageTest : JsonPageTest("trustSone.ajax", requiresLogin = true, n addRequestParameter("sone", "sone-id") core.preferences.positiveTrust = 31 assertThat(json.isSuccess, equalTo(true)) - assertThat(json["trustValue"].asInt(), equalTo(31)) + assertThat(json["trustValue"]?.asInt(), equalTo(31)) } } -- 2.7.4