From: David ‘Bombe’ Roden Date: Tue, 11 Oct 2016 19:08:56 +0000 (+0200) Subject: Use common base class for web page tests X-Git-Tag: 0.9.6^2~27 X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=f9ddb56aac0ca9530fdcb22a5d59c1d32c9b658a;p=Sone.git Use common base class for web page tests --- diff --git a/src/test/java/net/pterodactylus/sone/web/DeleteReplyPageTest.java b/src/test/java/net/pterodactylus/sone/web/DeleteReplyPageTest.java index 91c781b..88c87a1 100644 --- a/src/test/java/net/pterodactylus/sone/web/DeleteReplyPageTest.java +++ b/src/test/java/net/pterodactylus/sone/web/DeleteReplyPageTest.java @@ -1,61 +1,28 @@ package net.pterodactylus.sone.web; import static net.pterodactylus.sone.web.WebTestUtils.redirectsTo; -import static org.mockito.ArgumentMatchers.any; +import static net.pterodactylus.util.web.Method.POST; import static org.mockito.ArgumentMatchers.anyInt; import static org.mockito.ArgumentMatchers.eq; -import static org.mockito.Mockito.RETURNS_DEEP_STUBS; -import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; -import java.util.Collections; - import net.pterodactylus.sone.data.PostReply; -import net.pterodactylus.sone.data.Sone; -import net.pterodactylus.sone.web.page.FreenetRequest; -import net.pterodactylus.util.notify.Notification; -import net.pterodactylus.util.template.Template; -import net.pterodactylus.util.template.TemplateContext; -import net.pterodactylus.util.web.Method; - -import freenet.support.api.HTTPRequest; import com.google.common.base.Optional; -import org.junit.Before; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; /** * Unit test for {@link DeleteReplyPage}. * * @author David ‘Bombe’ Roden */ -public class DeleteReplyPageTest { +public class DeleteReplyPageTest extends WebPageTest { - @Rule - public final ExpectedException expectedException = ExpectedException.none(); - - private final Template template = new Template(); - private final WebInterface webInterface = mock(WebInterface.class, RETURNS_DEEP_STUBS); private final DeleteReplyPage page = new DeleteReplyPage(template, webInterface); - private final TemplateContext templateContext = new TemplateContext(); - private final FreenetRequest freenetRequest = mock(FreenetRequest.class); - private final HTTPRequest httpRequest = mock(HTTPRequest.class); - - @Before - public void setupWebInterface() { - when(webInterface.getNotifications(any(Sone.class))).thenReturn(Collections.emptyList()); - } - - @Before - public void setupHttpRequest() { - when(freenetRequest.getHttpRequest()).thenReturn(httpRequest); - } @Test public void tryingToDeleteAReplyWithAnInvalidIdResultsInNoPermissionPage() throws Exception { - when(freenetRequest.getMethod()).thenReturn(Method.POST); + request("", POST); when(httpRequest.getPartAsStringFailsafe(eq("reply"), anyInt())).thenReturn("id"); when(webInterface.getCore().getPostReply("id")).thenReturn(Optional.absent()); expectedException.expect(redirectsTo("noPermission.html")); diff --git a/src/test/java/net/pterodactylus/sone/web/NewPageTest.java b/src/test/java/net/pterodactylus/sone/web/NewPageTest.java index 81d576e..e110969 100644 --- a/src/test/java/net/pterodactylus/sone/web/NewPageTest.java +++ b/src/test/java/net/pterodactylus/sone/web/NewPageTest.java @@ -3,24 +3,13 @@ package net.pterodactylus.sone.web; import static java.util.Arrays.asList; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.containsInAnyOrder; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.ArgumentMatchers.anyBoolean; -import static org.mockito.Mockito.RETURNS_DEEP_STUBS; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; -import java.util.Collections; import java.util.List; import net.pterodactylus.sone.data.Post; import net.pterodactylus.sone.data.PostReply; -import net.pterodactylus.sone.data.Sone; -import net.pterodactylus.sone.web.page.FreenetRequest; -import net.pterodactylus.util.notify.Notification; -import net.pterodactylus.util.template.Template; -import net.pterodactylus.util.template.TemplateContext; - -import freenet.clients.http.ToadletContext; import com.google.common.base.Optional; import org.junit.Before; @@ -31,25 +20,13 @@ import org.junit.Test; * * @author David ‘Bombe’ Roden */ -public class NewPageTest { +public class NewPageTest extends WebPageTest { - private final Template template = mock(Template.class); - private final WebInterface webInterface = mock(WebInterface.class, RETURNS_DEEP_STUBS); private final NewPage newPage = new NewPage(template, webInterface); - private final Sone currentSone = mock(Sone.class); - private final TemplateContext templateContext = new TemplateContext(); - private final FreenetRequest freenetRequest = mock(FreenetRequest.class, RETURNS_DEEP_STUBS); - - @Before - public void setupFreenetRequest() { - when(freenetRequest.getToadletContext()).thenReturn(mock(ToadletContext.class)); - } @Before - public void setupWebInterface() { + public void setupNumberOfPostsPerPage() { when(webInterface.getCore().getPreferences().getPostsPerPage()).thenReturn(5); - when(webInterface.getCurrentSone(any(ToadletContext.class), anyBoolean())).thenReturn(currentSone); - when(webInterface.getNotifications(any(Sone.class))).thenReturn(Collections.emptyList()); } @Test diff --git a/src/test/java/net/pterodactylus/sone/web/UploadImagePageTest.java b/src/test/java/net/pterodactylus/sone/web/UploadImagePageTest.java index 3bdf588..3b8258e 100644 --- a/src/test/java/net/pterodactylus/sone/web/UploadImagePageTest.java +++ b/src/test/java/net/pterodactylus/sone/web/UploadImagePageTest.java @@ -1,61 +1,29 @@ package net.pterodactylus.sone.web; import static net.pterodactylus.sone.web.WebTestUtils.redirectsTo; -import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyInt; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; -import java.net.URI; - -import net.pterodactylus.sone.core.Core; -import net.pterodactylus.sone.core.UpdateChecker; import net.pterodactylus.sone.data.Album; -import net.pterodactylus.sone.data.Sone; -import net.pterodactylus.sone.web.page.FreenetRequest; -import net.pterodactylus.util.template.Template; -import net.pterodactylus.util.template.TemplateContext; import net.pterodactylus.util.web.Method; -import freenet.clients.http.ToadletContext; -import freenet.support.api.HTTPRequest; - import org.junit.Before; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; /** * Unit test for {@link UploadImagePageTest}. * * @author David ‘Bombe’ Roden */ -public class UploadImagePageTest { - - @Rule - public final ExpectedException expectedException = ExpectedException.none(); +public class UploadImagePageTest extends WebPageTest { - private final Template template = new Template(); - private final WebInterface webInterface = mock(WebInterface.class); private final UploadImagePage uploadImagePage = new UploadImagePage(template, webInterface); - private final TemplateContext templateContext = new TemplateContext(); - private final HTTPRequest httpRequest = mock(HTTPRequest.class); - private final ToadletContext toadletContext = mock(ToadletContext.class); - private final Core core = mock(Core.class); - private final Sone currentSone = mock(Sone.class); private final Album parentAlbum = mock(Album.class); @Before - public void setupWebInterface() { - UpdateChecker updateChecker = mock(UpdateChecker.class); - when(core.getUpdateChecker()).thenReturn(updateChecker); - when(webInterface.getCore()).thenReturn(core); - when(webInterface.getCurrentSone(any(ToadletContext.class))).thenReturn(currentSone); - } - - @Before public void setupParentAlbum() { when(core.getAlbum("parent-id")).thenReturn(parentAlbum); when(parentAlbum.getSone()).thenReturn(currentSone); @@ -63,11 +31,11 @@ public class UploadImagePageTest { @Test public void uploadingAnImageWithoutTitleRedirectsToEmptyImageTitlePage() throws Exception { - FreenetRequest request = new FreenetRequest(new URI(""), Method.POST, httpRequest, toadletContext); + request("", Method.POST); when(httpRequest.getPartAsStringFailsafe(eq("parent"), anyInt())).thenReturn("parent-id"); when(httpRequest.getPartAsStringFailsafe(eq("title"), anyInt())).thenReturn(" "); expectedException.expect(redirectsTo("emptyImageTitle.html")); - uploadImagePage.processTemplate(request, templateContext); + uploadImagePage.processTemplate(freenetRequest, templateContext); } } diff --git a/src/test/java/net/pterodactylus/sone/web/WebPageTest.java b/src/test/java/net/pterodactylus/sone/web/WebPageTest.java new file mode 100644 index 0000000..3cbc75e --- /dev/null +++ b/src/test/java/net/pterodactylus/sone/web/WebPageTest.java @@ -0,0 +1,75 @@ +package net.pterodactylus.sone.web; + +import static org.mockito.ArgumentMatchers.anyBoolean; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.RETURNS_DEEP_STUBS; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import java.net.URI; +import java.net.URISyntaxException; +import java.util.ArrayList; + +import net.pterodactylus.sone.core.Core; +import net.pterodactylus.sone.core.UpdateChecker; +import net.pterodactylus.sone.data.Sone; +import net.pterodactylus.sone.web.page.FreenetRequest; +import net.pterodactylus.util.notify.Notification; +import net.pterodactylus.util.template.Template; +import net.pterodactylus.util.template.TemplateContext; +import net.pterodactylus.util.web.Method; + +import freenet.clients.http.ToadletContext; +import freenet.support.api.HTTPRequest; + +import org.junit.Before; +import org.junit.Rule; +import org.junit.rules.ExpectedException; + +/** + * Base class for web page tests. + * + * @author David ‘Bombe’ Roden + */ +public abstract class WebPageTest { + + @Rule + public final ExpectedException expectedException = ExpectedException.none(); + + protected final Template template = new Template(); + protected final WebInterface webInterface = mock(WebInterface.class, RETURNS_DEEP_STUBS); + protected final Core core = webInterface.getCore(); + + protected final Sone currentSone = mock(Sone.class); + + protected final TemplateContext templateContext = new TemplateContext(); + protected final HTTPRequest httpRequest = mock(HTTPRequest.class); + protected final FreenetRequest freenetRequest = mock(FreenetRequest.class); + protected final ToadletContext toadletContext = mock(ToadletContext.class); + + + @Before + public final void setupFreenetRequest() { + when(freenetRequest.getToadletContext()).thenReturn(toadletContext); + when(freenetRequest.getHttpRequest()).thenReturn(httpRequest); + } + + @Before + public final void setupWebInterface() { + UpdateChecker updateChecker = mock(UpdateChecker.class); + when(webInterface.getCore().getUpdateChecker()).thenReturn(updateChecker); + when(webInterface.getCurrentSone(toadletContext)).thenReturn(currentSone); + when(webInterface.getCurrentSone(eq(toadletContext), anyBoolean())).thenReturn(currentSone); + when(webInterface.getNotifications(currentSone)).thenReturn(new ArrayList()); + } + + protected void request(String uri, Method method) { + try { + when(freenetRequest.getUri()).thenReturn(new URI(uri)); + } catch (URISyntaxException e) { + throw new RuntimeException(e); + } + when(freenetRequest.getMethod()).thenReturn(method); + } + +}