Add unit test for mark as known ajax page
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Wed, 27 Sep 2017 17:15:12 +0000 (19:15 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Wed, 27 Sep 2017 17:15:12 +0000 (19:15 +0200)
src/test/kotlin/net/pterodactylus/sone/web/ajax/MarkAsKnownAjaxPageTest.kt [new file with mode: 0644]

diff --git a/src/test/kotlin/net/pterodactylus/sone/web/ajax/MarkAsKnownAjaxPageTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/ajax/MarkAsKnownAjaxPageTest.kt
new file mode 100644 (file)
index 0000000..b2db4c1
--- /dev/null
@@ -0,0 +1,73 @@
+package net.pterodactylus.sone.web.ajax
+
+import net.pterodactylus.sone.data.Post
+import net.pterodactylus.sone.data.PostReply
+import net.pterodactylus.sone.data.Sone
+import net.pterodactylus.sone.test.mock
+import net.pterodactylus.sone.test.whenever
+import org.hamcrest.MatcherAssert.assertThat
+import org.hamcrest.Matchers.equalTo
+import org.junit.Test
+import org.mockito.Mockito.any
+import org.mockito.Mockito.never
+import org.mockito.Mockito.verify
+
+/**
+ * Unit test for [MarkAsKnownAjaxPage].
+ */
+class MarkAsKnownAjaxPageTest : JsonPageTest("markAsKnown.ajax", requiresLogin = false, pageSupplier = ::MarkAsKnownAjaxPage) {
+
+       @Test
+       fun `request without type results in invalid-type`() {
+               assertThat(json.isSuccess, equalTo(false))
+               assertThat(json.error, equalTo("invalid-type"))
+       }
+
+       @Test
+       fun `request with unknown sone returns successfully`() {
+               addRequestParameter("type", "sone")
+               addRequestParameter("sone", "invalid")
+               assertThat(json.isSuccess, equalTo(true))
+               verify(core, never()).markSoneKnown(any())
+       }
+
+       @Test
+       fun `request with multiple valid sones marks sones as known and returns successfully`() {
+               addRequestParameter("type", "sone")
+               addRequestParameter("id", "sone-id1 sone-id2")
+               val sone1 = mock<Sone>().apply { whenever(id).thenReturn("sone-id1") }
+               val sone2 = mock<Sone>().apply { whenever(id).thenReturn("sone-id2") }
+               addSone(sone1)
+               addSone(sone2)
+               assertThat(json.isSuccess, equalTo(true))
+               verify(core).markSoneKnown(sone1)
+               verify(core).markSoneKnown(sone2)
+       }
+
+       @Test
+       fun `request with multiple valid posts marks posts as known and returns successfully`() {
+               addRequestParameter("type", "post")
+               addRequestParameter("id", "post1 post2 post 3")
+               val post1 = mock<Post>()
+               val post2 = mock<Post>()
+               addPost(post1, "post1")
+               addPost(post2, "post2")
+               assertThat(json.isSuccess, equalTo(true))
+               verify(core).markPostKnown(post1)
+               verify(core).markPostKnown(post2)
+       }
+
+       @Test
+       fun `request with multiple valid replies marks replies as known and returns successfully`() {
+               addRequestParameter("type", "reply")
+               addRequestParameter("id", "reply1 reply2 reply3")
+               val reply1 = mock<PostReply>()
+               val reply2 = mock<PostReply>()
+               addReply(reply1, "reply1")
+               addReply(reply2, "reply2")
+               assertThat(json.isSuccess, equalTo(true))
+               verify(core).markReplyKnown(reply1)
+               verify(core).markReplyKnown(reply2)
+       }
+
+}