Add test for bookmark page
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Tue, 11 Oct 2016 19:41:35 +0000 (21:41 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Tue, 11 Oct 2016 19:41:35 +0000 (21:41 +0200)
src/test/java/net/pterodactylus/sone/web/BookmarkPageTest.java [new file with mode: 0644]
src/test/java/net/pterodactylus/sone/web/WebPageTest.java

diff --git a/src/test/java/net/pterodactylus/sone/web/BookmarkPageTest.java b/src/test/java/net/pterodactylus/sone/web/BookmarkPageTest.java
new file mode 100644 (file)
index 0000000..ddba2c5
--- /dev/null
@@ -0,0 +1,67 @@
+package net.pterodactylus.sone.web;
+
+import static net.pterodactylus.sone.web.WebTestUtils.redirectsTo;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.is;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.verify;
+
+import net.pterodactylus.sone.data.Post;
+import net.pterodactylus.util.web.Method;
+
+import org.junit.Test;
+
+/**
+ * Unit test for {@link BookmarkPage}.
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+public class BookmarkPageTest extends WebPageTest {
+
+       private final BookmarkPage page = new BookmarkPage(template, webInterface);
+
+       @Test
+       public void pathIsSetCorrectly() {
+               assertThat(page.getPath(), is("bookmark.html"));
+       }
+
+       @Test
+       public void getRequestDoesNotBookmarkAnythingAndDoesNotRedirect() throws Exception {
+               page.processTemplate(freenetRequest, templateContext);
+               verify(core, never()).bookmarkPost(any(Post.class));
+       }
+
+       @Test
+       public void postIsBookmarkedCorrectly() throws Exception {
+               setupRequest();
+               Post post = mock(Post.class);
+               addPost("post-id", post);
+               expectedException.expect(redirectsTo("return-page.html"));
+               try {
+                       page.processTemplate(freenetRequest, templateContext);
+               } finally {
+                       verify(core).bookmarkPost(post);
+               }
+       }
+
+       private void setupRequest() {
+               request("", Method.POST);
+               addHttpRequestParameter("post", "post-id");
+               addHttpRequestParameter("returnPage", "return-page.html");
+       }
+
+       @Test
+       public void nonExistentPostIsNotBookmarked() throws Exception {
+               setupRequest();
+               addPost("post-id", null);
+               expectedException.expect(redirectsTo("return-page.html"));
+               try {
+                       page.processTemplate(freenetRequest, templateContext);
+               } finally {
+                       verify(core, never()).bookmarkPost(any(Post.class));
+               }
+       }
+
+}
index c135d07..1a6fb59 100644 (file)
@@ -1,6 +1,7 @@
 package net.pterodactylus.sone.web;
 
 import static org.mockito.ArgumentMatchers.anyBoolean;
+import static org.mockito.ArgumentMatchers.anyInt;
 import static org.mockito.ArgumentMatchers.eq;
 import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
 import static org.mockito.Mockito.mock;
@@ -12,6 +13,7 @@ import java.util.ArrayList;
 
 import net.pterodactylus.sone.core.Core;
 import net.pterodactylus.sone.core.UpdateChecker;
+import net.pterodactylus.sone.data.Post;
 import net.pterodactylus.sone.data.Sone;
 import net.pterodactylus.sone.web.page.FreenetRequest;
 import net.pterodactylus.util.notify.Notification;
@@ -22,9 +24,12 @@ import net.pterodactylus.util.web.Method;
 import freenet.clients.http.ToadletContext;
 import freenet.support.api.HTTPRequest;
 
+import com.google.common.base.Optional;
 import org.junit.Before;
 import org.junit.Rule;
 import org.junit.rules.ExpectedException;
+import org.mockito.invocation.InvocationOnMock;
+import org.mockito.stubbing.Answer;
 
 /**
  * Base class for web page tests.
@@ -76,4 +81,18 @@ public abstract class WebPageTest {
                when(freenetRequest.getMethod()).thenReturn(method);
        }
 
+       protected void addHttpRequestParameter(String name, final String value) {
+               when(httpRequest.getPartAsStringFailsafe(eq(name), anyInt())).thenAnswer(new Answer<String>() {
+                       @Override
+                       public String answer(InvocationOnMock invocation) throws Throwable {
+                               int maxLength = invocation.getArgument(1);
+                               return value.substring(0, Math.min(maxLength, value.length()));
+                       }
+               });
+       }
+
+       protected void addPost(String postId, Post post) {
+               when(core.getPost(postId)).thenReturn(Optional.fromNullable(post));
+       }
+
 }