Add test for CreatePostPage
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Thu, 20 Oct 2016 09:05:19 +0000 (11:05 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Thu, 20 Oct 2016 09:05:19 +0000 (11:05 +0200)
src/test/java/net/pterodactylus/sone/web/CreatePostPageTest.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/CreatePostPageTest.java b/src/test/java/net/pterodactylus/sone/web/CreatePostPageTest.java
new file mode 100644 (file)
index 0000000..2a91086
--- /dev/null
@@ -0,0 +1,89 @@
+package net.pterodactylus.sone.web;
+
+import static net.pterodactylus.util.web.Method.POST;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.is;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+
+import net.pterodactylus.sone.data.Sone;
+
+import com.google.common.base.Optional;
+import org.junit.Test;
+
+/**
+ * Unit test for {@link CreatePostPage}.
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+public class CreatePostPageTest extends WebPageTest {
+
+       private final CreatePostPage page = new CreatePostPage(template, webInterface);
+
+       @Test
+       public void pageReturnsCorrectPath() {
+               assertThat(page.getPath(), is("createPost.html"));
+       }
+
+       @Test
+       public void returnPageIsSetInTemplateContext() throws Exception {
+               addHttpRequestParameter("returnPage", "returnPage.html");
+               page.processTemplate(freenetRequest, templateContext);
+               assertThat(templateContext.get("returnPage"), is((Object) "returnPage.html"));
+       }
+
+       @Test
+       public void postIsCreatedCorrectly() throws Exception {
+               addHttpRequestParameter("returnPage", "returnPage.html");
+               addHttpRequestParameter("text", "post text");
+               request("", POST);
+               expectedException.expect(WebTestUtils.redirectsTo("returnPage.html"));
+               try {
+                       page.processTemplate(freenetRequest, templateContext);
+               } finally {
+                       verify(core).createPost(currentSone, Optional.<Sone>absent(), "post text");
+               }
+       }
+
+       @Test
+       public void creatingAnEmptyPostIsDenied() throws Exception {
+               addHttpRequestParameter("returnPage", "returnPage.html");
+               addHttpRequestParameter("text", "   ");
+               request("", POST);
+               page.processTemplate(freenetRequest, templateContext);
+               assertThat(templateContext.get("errorTextEmpty"), is((Object) true));
+       }
+
+       @Test
+       public void aSenderCanBeSelected() throws Exception {
+               addHttpRequestParameter("returnPage", "returnPage.html");
+               addHttpRequestParameter("text", "post text");
+               addHttpRequestParameter("sender", "sender-id");
+               Sone sender = mock(Sone.class);
+               addLocalSone("sender-id", sender);
+               request("", POST);
+               expectedException.expect(WebTestUtils.redirectsTo("returnPage.html"));
+               try {
+                       page.processTemplate(freenetRequest, templateContext);
+               } finally {
+                       verify(core).createPost(sender, Optional.<Sone>absent(), "post text");
+               }
+       }
+
+       @Test
+       public void aRecipientCanBeSelected() throws Exception {
+               addHttpRequestParameter("returnPage", "returnPage.html");
+               addHttpRequestParameter("text", "post text");
+               addHttpRequestParameter("recipient", "recipient-id");
+               Sone recipient = mock(Sone.class);
+               addSone("recipient-id", recipient);
+               request("", POST);
+               expectedException.expect(WebTestUtils.redirectsTo("returnPage.html"));
+               try {
+                       page.processTemplate(freenetRequest, templateContext);
+               } finally {
+                       verify(core).createPost(currentSone, Optional.of(recipient), "post text");
+               }
+       }
+
+}
index f464eac..2717174 100644 (file)
@@ -70,6 +70,8 @@ public abstract class WebPageTest {
        public final void setupCore() {
                UpdateChecker updateChecker = mock(UpdateChecker.class);
                when(core.getUpdateChecker()).thenReturn(updateChecker);
+               when(core.getLocalSone(anyString())).thenReturn(null);
+               when(core.getSone(anyString())).thenReturn(Optional.<Sone>absent());
        }
 
        @Before
@@ -102,4 +104,12 @@ public abstract class WebPageTest {
                when(core.getPost(postId)).thenReturn(Optional.fromNullable(post));
        }
 
+       protected void addSone(String soneId, Sone sone) {
+               when(core.getSone(eq(soneId))).thenReturn(Optional.fromNullable(sone));
+       }
+
+       protected void addLocalSone(String soneId, Sone sone) {
+               when(core.getLocalSone(eq(soneId))).thenReturn(sone);
+       }
+
 }