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 <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
*/
-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.<Notification>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.<PostReply>absent());
expectedException.expect(redirectsTo("noPermission.html"));
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;
*
* @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
*/
-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.<Notification>emptyList());
}
@Test
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 <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
*/
-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);
@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);
}
}
--- /dev/null
+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 <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+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<Notification>());
+ }
+
+ 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);
+ }
+
+}