Refactor test, move mocks and verifiers to their respective classes.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Wed, 18 Dec 2013 23:46:18 +0000 (00:46 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Fri, 28 Feb 2014 21:26:08 +0000 (22:26 +0100)
src/test/java/net/pterodactylus/sone/Verifiers.java
src/test/java/net/pterodactylus/sone/data/Mocks.java
src/test/java/net/pterodactylus/sone/web/ajax/BookmarkAjaxPageTest.java

index 32a359f..c505bcf 100644 (file)
@@ -32,6 +32,8 @@ import net.pterodactylus.sone.data.PostReply;
 import net.pterodactylus.sone.data.Profile.Field;
 import net.pterodactylus.sone.data.Sone;
 import net.pterodactylus.sone.freenet.fcp.Command.Response;
+import net.pterodactylus.sone.web.ajax.JsonErrorReturnObject;
+import net.pterodactylus.sone.web.ajax.JsonReturnObject;
 
 import freenet.node.FSParseException;
 import freenet.support.SimpleFieldSet;
@@ -129,4 +131,15 @@ public class Verifiers {
                }
        }
 
+       public static void verifySuccessfulJsonResponse(JsonReturnObject jsonReturnObject) {
+               assertThat(jsonReturnObject, notNullValue());
+               assertThat(jsonReturnObject.isSuccess(), is(true));
+       }
+
+       public static void verifyJsonError(JsonReturnObject jsonReturnObject, String error) {
+               assertThat(jsonReturnObject, notNullValue());
+               assertThat(jsonReturnObject.isSuccess(), is(false));
+               assertThat(((JsonErrorReturnObject) jsonReturnObject).getError(), is(error));
+       }
+
 }
index f1b0a09..ff52581 100644 (file)
@@ -30,6 +30,8 @@ import static org.mockito.Mockito.doAnswer;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.when;
 
+import java.net.URI;
+import java.net.URISyntaxException;
 import java.util.Collection;
 import java.util.List;
 import java.util.Map;
@@ -40,6 +42,11 @@ import net.pterodactylus.sone.data.impl.DefaultPostBuilder;
 import net.pterodactylus.sone.data.impl.DefaultPostReplyBuilder;
 import net.pterodactylus.sone.database.Database;
 import net.pterodactylus.sone.database.PostReplyBuilder;
+import net.pterodactylus.sone.web.WebInterface;
+import net.pterodactylus.sone.web.page.FreenetRequest;
+
+import freenet.clients.http.HTTPRequestImpl;
+import freenet.support.api.HTTPRequest;
 
 import com.google.common.base.Function;
 import com.google.common.base.Optional;
@@ -67,10 +74,12 @@ public class Mocks {
        private final SetMultimap<PostReply, Sone> postReplyLikingSones = HashMultimap.create();
        public final Database database;
        public final Core core;
+       public final WebInterface webInterface;
 
        public Mocks() {
                database = mockDatabase();
                core = mockCore(database);
+               webInterface = mockWebInterface(core);
                when(database.getSone()).thenReturn(new Function<String, Optional<Sone>>() {
                        @Override
                        public Optional<Sone> apply(String soneId) {
@@ -112,6 +121,12 @@ public class Mocks {
                return database;
        }
 
+       private static WebInterface mockWebInterface(Core core) {
+               WebInterface webInterface = mock(WebInterface.class);
+               when(webInterface.getCore()).thenReturn(core);
+               return webInterface;
+       }
+
        public SoneMocker mockSone(String id) {
                return new SoneMocker(id);
        }
@@ -124,6 +139,13 @@ public class Mocks {
                return new PostReplyMocker(replyId, sone);
        }
 
+       public FreenetRequest mockRequest(String path) throws URISyntaxException {
+               HTTPRequest httpRequest = new HTTPRequestImpl(new URI(path), "GET");
+               FreenetRequest request = mock(FreenetRequest.class);
+               when(request.getHttpRequest()).thenReturn(httpRequest);
+               return request;
+       }
+
        public class SoneMocker {
 
                private final Sone mockedSone = mock(Sone.class);
index 1db89d0..a4b6a90 100644 (file)
@@ -4,25 +4,21 @@
 
 package net.pterodactylus.sone.web.ajax;
 
-import static org.hamcrest.CoreMatchers.is;
-import static org.hamcrest.CoreMatchers.notNullValue;
-import static org.junit.Assert.assertThat;
+import static net.pterodactylus.sone.Verifiers.verifyJsonError;
+import static net.pterodactylus.sone.Verifiers.verifySuccessfulJsonResponse;
 import static org.mockito.Matchers.anyString;
-import static org.mockito.Mockito.mock;
+import static org.mockito.Matchers.eq;
 import static org.mockito.Mockito.never;
 import static org.mockito.Mockito.times;
 import static org.mockito.Mockito.verify;
-import static org.mockito.Mockito.when;
 
-import java.net.URI;
 import java.net.URISyntaxException;
 
 import net.pterodactylus.sone.core.Core;
+import net.pterodactylus.sone.data.Mocks;
 import net.pterodactylus.sone.web.WebInterface;
 import net.pterodactylus.sone.web.page.FreenetRequest;
 
-import freenet.clients.http.HTTPRequestImpl;
-import freenet.support.api.HTTPRequest;
 import org.junit.Test;
 
 /**
@@ -32,50 +28,28 @@ import org.junit.Test;
  */
 public class BookmarkAjaxPageTest {
 
+       private final Mocks mocks = new Mocks();
+       private final Core core = mocks.core;
+       private final WebInterface webInterface = mocks.webInterface;
+       private final BookmarkAjaxPage bookmarkAjaxPage = new BookmarkAjaxPage(webInterface);
+
        @Test
        public void testBookmarkingExistingPost() throws URISyntaxException {
-               /* create mocks. */
-               Core core = mock(Core.class);
-               WebInterface webInterface = mock(WebInterface.class);
-               when(webInterface.getCore()).thenReturn(core);
-               HTTPRequest httpRequest = new HTTPRequestImpl(new URI("/ajax/bookmark.ajax?post=abc"), "GET");
-               FreenetRequest request = mock(FreenetRequest.class);
-               when(request.getHttpRequest()).thenReturn(httpRequest);
-
-               /* create JSON page. */
-               BookmarkAjaxPage bookmarkAjaxPage = new BookmarkAjaxPage(webInterface);
-               JsonReturnObject jsonReturnObject = bookmarkAjaxPage.createJsonObject(request);
-
-               /* verify response. */
-               assertThat(jsonReturnObject, notNullValue());
-               assertThat(jsonReturnObject.isSuccess(), is(true));
-
-               /* verify behaviour. */
-               verify(core, times(1)).bookmarkPost(anyString());
-               verify(core).bookmarkPost("abc");
+               JsonReturnObject jsonReturnObject = performRequest("/ajax/bookmark.ajax?post=abc", bookmarkAjaxPage);
+               verifySuccessfulJsonResponse(jsonReturnObject);
+               verify(core, times(1)).bookmarkPost(eq("abc"));
        }
 
        @Test
        public void testBookmarkingMissingPost() throws URISyntaxException {
-               /* create mocks. */
-               Core core = mock(Core.class);
-               WebInterface webInterface = mock(WebInterface.class);
-               when(webInterface.getCore()).thenReturn(core);
-               HTTPRequest httpRequest = new HTTPRequestImpl(new URI("/ajax/bookmark.ajax"), "GET");
-               FreenetRequest request = mock(FreenetRequest.class);
-               when(request.getHttpRequest()).thenReturn(httpRequest);
-
-               /* create JSON page. */
-               BookmarkAjaxPage bookmarkAjaxPage = new BookmarkAjaxPage(webInterface);
-               JsonReturnObject jsonReturnObject = bookmarkAjaxPage.createJsonObject(request);
-
-               /* verify response. */
-               assertThat(jsonReturnObject, notNullValue());
-               assertThat(jsonReturnObject.isSuccess(), is(false));
-               assertThat(((JsonErrorReturnObject) jsonReturnObject).getError(), is("invalid-post-id"));
-
-               /* verify behaviour. */
+               JsonReturnObject jsonReturnObject = performRequest("/ajax/bookmark.ajax", bookmarkAjaxPage);
+               verifyJsonError(jsonReturnObject, "invalid-post-id");
                verify(core, never()).bookmarkPost(anyString());
        }
 
+       private JsonReturnObject performRequest(String path, BookmarkAjaxPage bookmarkAjaxPage) throws URISyntaxException {
+               FreenetRequest request = mocks.mockRequest(path);
+               return bookmarkAjaxPage.createJsonObject(request);
+       }
+
 }