Add test for creating a post via AJAX.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Wed, 19 Feb 2014 20:12:44 +0000 (21:12 +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/CreatePostAjaxPageTest.java [new file with mode: 0644]

diff --git a/src/test/java/net/pterodactylus/sone/web/ajax/CreatePostAjaxPageTest.java b/src/test/java/net/pterodactylus/sone/web/ajax/CreatePostAjaxPageTest.java
new file mode 100644 (file)
index 0000000..904f5b1
--- /dev/null
@@ -0,0 +1,101 @@
+package net.pterodactylus.sone.web.ajax;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.is;
+import static org.hamcrest.Matchers.nullValue;
+import static org.mockito.Matchers.any;
+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.clients.http.ToadletContext;
+import freenet.support.api.HTTPRequest;
+
+import org.junit.Test;
+
+/**
+ * Unit test for {@link CreatePostAjaxPage}.
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+public class CreatePostAjaxPageTest {
+
+       private final Mocks mocks = new Mocks();
+       private final CreatePostAjaxPage createPostAjaxPage = new CreatePostAjaxPage(mocks.webInterface);
+
+       @Test
+       public void missingSoneResultsInAnError() {
+               JsonReturnObject jsonReturnObject = createPostAjaxPage.createJsonObject(mocks.mockRequest(""));
+               verifyError(jsonReturnObject, "auth-required");
+       }
+
+       private void verifyError(JsonReturnObject jsonReturnObject, String error) {
+               assertThat(jsonReturnObject.isSuccess(), is(false));
+               assertThat(((JsonErrorReturnObject) jsonReturnObject).getError(), is(error));
+       }
+
+       @Test
+       public void missingTextResultsInError() {
+               configureLocalSone();
+               FreenetRequest request = mocks.mockRequest("");
+               JsonReturnObject jsonReturnObject = createPostAjaxPage.createJsonObject(request);
+               verifyError(jsonReturnObject, "text-required");
+       }
+
+       @Test
+       public void emptyTextResultsInError() {
+               configureLocalSone();
+       }
+
+       private void configureText(FreenetRequest request, String text) {
+               HTTPRequest httpRequest = request.getHttpRequest();
+               when(httpRequest.getParam("text")).thenReturn(text);
+       }
+
+       private void configureLocalSone() {
+               Sone sone = mocks.mockSone("Sone").local().create();
+               when(mocks.webInterface.getCurrentSone(any(ToadletContext.class))).thenReturn(sone);
+       }
+
+       @Test
+       public void postIsCreated() {
+               configureLocalSone();
+               FreenetRequest request = mocks.mockRequest("");
+               configureText(request, "Test text.");
+               JsonReturnObject jsonReturnObject = createPostAjaxPage.createJsonObject(request);
+               assertThat(jsonReturnObject.isSuccess(), is(true));
+               assertThat(mocks.database.getPost(jsonReturnObject.get("postId").asText()).isPresent(), is(true));
+       }
+
+       @Test
+       public void postWithMissingRecipientIsCreatedWithoutRecipient() {
+               configureLocalSone();
+               FreenetRequest request = mocks.mockRequest("");
+               configureText(request, "Test text.");
+               when(request.getHttpRequest().getParam("recipient")).thenReturn("Recipient");
+               JsonReturnObject jsonReturnObject = createPostAjaxPage.createJsonObject(request);
+               assertThat(jsonReturnObject.isSuccess(), is(true));
+               assertThat(mocks.database.getPost(jsonReturnObject.get("postId").asText()).isPresent(), is(true));
+               assertThat(jsonReturnObject.get("recipient").textValue(), nullValue());
+       }
+
+       @Test
+       public void postWithRecipientIsCreatedWithRecipient() {
+               configureLocalSone();
+               FreenetRequest request = mocks.mockRequest("");
+               configureRecipient(request);
+               configureText(request, "Test text.");
+               JsonReturnObject jsonReturnObject = createPostAjaxPage.createJsonObject(request);
+               assertThat(jsonReturnObject.isSuccess(), is(true));
+               assertThat(mocks.database.getPost(jsonReturnObject.get("postId").asText()).isPresent(), is(true));
+               assertThat(jsonReturnObject.get("recipient").textValue(), is("Recipient"));
+       }
+
+       private void configureRecipient(FreenetRequest request) {
+               mocks.mockSone("Recipient").create();
+               when(request.getHttpRequest().getParam("recipient")).thenReturn("Recipient");
+       }
+
+}