From 8dbd99ec310a150de00e38231a3739a83f106707 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Thu, 19 Dec 2013 00:46:18 +0100 Subject: [PATCH] Refactor test, move mocks and verifiers to their respective classes. --- .../java/net/pterodactylus/sone/Verifiers.java | 13 +++++ .../java/net/pterodactylus/sone/data/Mocks.java | 22 ++++++++ .../sone/web/ajax/BookmarkAjaxPageTest.java | 64 +++++++--------------- 3 files changed, 54 insertions(+), 45 deletions(-) diff --git a/src/test/java/net/pterodactylus/sone/Verifiers.java b/src/test/java/net/pterodactylus/sone/Verifiers.java index 32a359f..c505bcf 100644 --- a/src/test/java/net/pterodactylus/sone/Verifiers.java +++ b/src/test/java/net/pterodactylus/sone/Verifiers.java @@ -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)); + } + } diff --git a/src/test/java/net/pterodactylus/sone/data/Mocks.java b/src/test/java/net/pterodactylus/sone/data/Mocks.java index f1b0a09..ff52581 100644 --- a/src/test/java/net/pterodactylus/sone/data/Mocks.java +++ b/src/test/java/net/pterodactylus/sone/data/Mocks.java @@ -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 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>() { @Override public Optional 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); diff --git a/src/test/java/net/pterodactylus/sone/web/ajax/BookmarkAjaxPageTest.java b/src/test/java/net/pterodactylus/sone/web/ajax/BookmarkAjaxPageTest.java index 1db89d0..a4b6a90 100644 --- a/src/test/java/net/pterodactylus/sone/web/ajax/BookmarkAjaxPageTest.java +++ b/src/test/java/net/pterodactylus/sone/web/ajax/BookmarkAjaxPageTest.java @@ -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); + } + } -- 2.7.4