Add unit test for mark as known page and slightly improve the logic
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Mon, 28 Nov 2016 20:47:35 +0000 (21:47 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Mon, 28 Nov 2016 20:47:35 +0000 (21:47 +0100)
src/main/java/net/pterodactylus/sone/web/MarkAsKnownPage.java
src/test/kotlin/net/pterodactylus/sone/web/MarkAsKnownPageTest.kt [new file with mode: 0644]

index dc1841b..57ea00f 100644 (file)
@@ -77,7 +77,7 @@ public class MarkAsKnownPage extends SoneTemplatePage {
                                        continue;
                                }
                                webInterface.getCore().markReplyKnown(reply.get());
-                       } else if (type.equals("sone")) {
+                       } else {
                                Optional<Sone> sone = webInterface.getCore().getSone(id);
                                if (!sone.isPresent()) {
                                        continue;
diff --git a/src/test/kotlin/net/pterodactylus/sone/web/MarkAsKnownPageTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/MarkAsKnownPageTest.kt
new file mode 100644 (file)
index 0000000..e586e67
--- /dev/null
@@ -0,0 +1,76 @@
+package net.pterodactylus.sone.web
+
+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.web.WebTestUtils.redirectsTo
+import org.junit.Test
+import org.mockito.Mockito.verify
+
+/**
+ * Unit test for [MarkAsKnownPage].
+ */
+class MarkAsKnownPageTest : WebPageTest() {
+
+       private val page = MarkAsKnownPage(template, webInterface)
+
+       @Test
+       fun `posts can be marked as known`() {
+               addHttpRequestParameter("returnPage", "return.html")
+               addHttpRequestParameter("type", "post")
+               addHttpRequestParameter("id", "post1 post2 post3")
+               val posts = listOf(mock<Post>(), mock<Post>())
+               addPost("post1", posts[0])
+               addPost("post3", posts[1])
+               expectedException.expect(redirectsTo("return.html"))
+               try {
+                       page.handleRequest(freenetRequest, templateContext)
+               } finally {
+                       verify(core).markPostKnown(posts[0])
+                       verify(core).markPostKnown(posts[1])
+               }
+       }
+
+       @Test
+       fun `replies can be marked as known`() {
+               addHttpRequestParameter("returnPage", "return.html")
+               addHttpRequestParameter("type", "reply")
+               addHttpRequestParameter("id", "reply1 reply2 reply3")
+               val replies = listOf(mock<PostReply>(), mock<PostReply>())
+               addPostReply("reply1", replies[0])
+               addPostReply("reply3", replies[1])
+               expectedException.expect(redirectsTo("return.html"))
+               try {
+                       page.handleRequest(freenetRequest, templateContext)
+               } finally {
+                       verify(core).markReplyKnown(replies[0])
+                       verify(core).markReplyKnown(replies[1])
+               }
+       }
+
+       @Test
+       fun `sones can be marked as known`() {
+               addHttpRequestParameter("returnPage", "return.html")
+               addHttpRequestParameter("type", "sone")
+               addHttpRequestParameter("id", "sone1 sone2 sone3")
+               val sones = listOf(mock<Sone>(), mock<Sone>())
+               addSone("sone1", sones[0])
+               addSone("sone3", sones[1])
+               expectedException.expect(redirectsTo("return.html"))
+               try {
+                       page.handleRequest(freenetRequest, templateContext)
+               } finally {
+                       verify(core).markSoneKnown(sones[0])
+                       verify(core).markSoneKnown(sones[1])
+               }
+       }
+
+       @Test
+       fun `different type redirects to invalid page`() {
+               addHttpRequestParameter("type", "foo")
+               expectedException.expect(redirectsTo("invalid.html"))
+               page.handleRequest(freenetRequest, templateContext)
+       }
+
+}