Add test for creating replies via AJAX.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Wed, 19 Feb 2014 20:22:08 +0000 (21:22 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Fri, 28 Feb 2014 21:26:09 +0000 (22:26 +0100)
src/test/java/net/pterodactylus/sone/web/ajax/CreateReplyAjaxPageTest.java [new file with mode: 0644]

diff --git a/src/test/java/net/pterodactylus/sone/web/ajax/CreateReplyAjaxPageTest.java b/src/test/java/net/pterodactylus/sone/web/ajax/CreateReplyAjaxPageTest.java
new file mode 100644 (file)
index 0000000..0438947
--- /dev/null
@@ -0,0 +1,104 @@
+package net.pterodactylus.sone.web.ajax;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.is;
+import static org.hamcrest.Matchers.notNullValue;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+import net.pterodactylus.sone.data.Mocks;
+import net.pterodactylus.sone.data.Sone;
+import net.pterodactylus.sone.web.page.FreenetRequest;
+
+import freenet.support.api.HTTPRequest;
+
+import org.junit.Test;
+
+/**
+ * Unit test for {@link CreateReplyAjaxPage}.
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+public class CreateReplyAjaxPageTest {
+
+       private final Mocks mocks = new Mocks();
+       private final CreateReplyAjaxPage createReplyAjaxPage = new CreateReplyAjaxPage(mocks.webInterface);
+       private final FreenetRequest freenetRequest = mocks.mockRequest("createReply.ajax");
+
+       @Test
+       public void canCreateReplyWithAllOptionalValues() {
+               Sone sender = mocks.mockSone("SenderId").local().create();
+               Sone remote = mocks.mockSone("Remote").create();
+               mocks.mockPost(remote, "PostId").create();
+               HTTPRequest httpRequest = createHttpRequest(freenetRequest);
+               setReplyText(httpRequest, "Reply Text");
+               when(httpRequest.getParam("sender")).thenReturn("SenderId");
+               JsonReturnObject jsonReturnObject = createReplyAjaxPage.createJsonObject(freenetRequest);
+               verifySuccess(sender, jsonReturnObject);
+       }
+
+       private void verifySuccess(Sone sender, JsonReturnObject jsonReturnObject) {
+               assertThat(jsonReturnObject.isSuccess(), is(true));
+               assertThat(jsonReturnObject.get("reply").textValue(), notNullValue());
+               assertThat(jsonReturnObject.get("sone").textValue(), is(sender.getId()));
+       }
+
+       private void setReplyText(HTTPRequest httpRequest, String replyText) {
+               when(httpRequest.getParam("text")).thenReturn(replyText);
+       }
+
+       private HTTPRequest createHttpRequest(FreenetRequest freenetRequest) {
+               HTTPRequest httpRequest = mock(HTTPRequest.class);
+               when(httpRequest.getParam("post")).thenReturn("PostId");
+               when(freenetRequest.getHttpRequest()).thenReturn(httpRequest);
+               return httpRequest;
+       }
+
+       @Test
+       public void canCreateReplyWithDefaultSender() {
+               Sone sender = mocks.mockSone("SenderId").current().local().create();
+               Sone remote = mocks.mockSone("Remote").create();
+               mocks.mockPost(remote, "PostId").create();
+               HTTPRequest httpRequest = createHttpRequest(freenetRequest);
+               setReplyText(httpRequest, "Reply Text");
+               JsonReturnObject jsonReturnObject = createReplyAjaxPage.createJsonObject(freenetRequest);
+               verifySuccess(sender, jsonReturnObject);
+       }
+
+       @Test
+       public void canNotCreateReplyWithoutSender() {
+               Sone remote = mocks.mockSone("Remote").create();
+               mocks.mockPost(remote, "PostId").create();
+               HTTPRequest httpRequest = createHttpRequest(freenetRequest);
+               setReplyText(httpRequest, "Reply Text");
+               JsonReturnObject jsonReturnObject = createReplyAjaxPage.createJsonObject(freenetRequest);
+               verifyFailure(jsonReturnObject, "invalid-sone-id");
+       }
+
+       private void verifyFailure(JsonReturnObject jsonReturnObject, String errorId) {
+               assertThat(jsonReturnObject.isSuccess(), is(false));
+               assertThat(((JsonErrorReturnObject) jsonReturnObject).getError(), is(errorId));
+       }
+
+       @Test
+       public void canNotCreateReplyWithoutPost() {
+               mocks.mockSone("SenderId").local().create();
+               HTTPRequest httpRequest = createHttpRequest(freenetRequest);
+               setReplyText(httpRequest, "Reply Text");
+               when(httpRequest.getParam("sender")).thenReturn("SenderId");
+               JsonReturnObject jsonReturnObject = createReplyAjaxPage.createJsonObject(freenetRequest);
+               verifyFailure(jsonReturnObject, "invalid-post-id");
+       }
+
+       @Test(expected = IllegalStateException.class)
+       public void canNotCreateReplyWithoutText() {
+               mocks.mockSone("SenderId").local().create();
+               Sone remote = mocks.mockSone("Remote").create();
+               mocks.mockPost(remote, "PostId").create();
+               HTTPRequest httpRequest = createHttpRequest(freenetRequest);
+               setReplyText(httpRequest, "");
+               when(httpRequest.getParam("sender")).thenReturn("SenderId");
+               createReplyAjaxPage.createJsonObject(freenetRequest);
+       }
+
+}