1 package net.pterodactylus.sone.web;
3 import static org.mockito.ArgumentMatchers.anyBoolean;
4 import static org.mockito.ArgumentMatchers.anyInt;
5 import static org.mockito.ArgumentMatchers.eq;
6 import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
7 import static org.mockito.Mockito.mock;
8 import static org.mockito.Mockito.when;
11 import java.net.URISyntaxException;
12 import java.util.ArrayList;
14 import net.pterodactylus.sone.core.Core;
15 import net.pterodactylus.sone.core.UpdateChecker;
16 import net.pterodactylus.sone.data.Post;
17 import net.pterodactylus.sone.data.Sone;
18 import net.pterodactylus.sone.web.page.FreenetRequest;
19 import net.pterodactylus.util.notify.Notification;
20 import net.pterodactylus.util.template.Template;
21 import net.pterodactylus.util.template.TemplateContext;
22 import net.pterodactylus.util.web.Method;
24 import freenet.clients.http.ToadletContext;
25 import freenet.support.api.HTTPRequest;
27 import com.google.common.base.Optional;
28 import org.junit.Before;
29 import org.junit.Rule;
30 import org.junit.rules.ExpectedException;
31 import org.mockito.invocation.InvocationOnMock;
32 import org.mockito.stubbing.Answer;
35 * Base class for web page tests.
37 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
39 public abstract class WebPageTest {
42 public final ExpectedException expectedException = ExpectedException.none();
44 protected final Template template = new Template();
45 protected final WebInterface webInterface = mock(WebInterface.class, RETURNS_DEEP_STUBS);
46 protected final Core core = webInterface.getCore();
48 protected final Sone currentSone = mock(Sone.class);
50 protected final TemplateContext templateContext = new TemplateContext();
51 protected final HTTPRequest httpRequest = mock(HTTPRequest.class);
52 protected final FreenetRequest freenetRequest = mock(FreenetRequest.class);
53 protected final ToadletContext toadletContext = mock(ToadletContext.class);
57 public final void setupFreenetRequest() {
58 when(freenetRequest.getToadletContext()).thenReturn(toadletContext);
59 when(freenetRequest.getHttpRequest()).thenReturn(httpRequest);
63 public final void setupCore() {
64 UpdateChecker updateChecker = mock(UpdateChecker.class);
65 when(webInterface.getCore().getUpdateChecker()).thenReturn(updateChecker);
69 public final void setupWebInterface() {
70 when(webInterface.getCurrentSone(toadletContext)).thenReturn(currentSone);
71 when(webInterface.getCurrentSone(eq(toadletContext), anyBoolean())).thenReturn(currentSone);
72 when(webInterface.getNotifications(currentSone)).thenReturn(new ArrayList<Notification>());
75 protected void request(String uri, Method method) {
77 when(freenetRequest.getUri()).thenReturn(new URI(uri));
78 } catch (URISyntaxException e) {
79 throw new RuntimeException(e);
81 when(freenetRequest.getMethod()).thenReturn(method);
84 protected void addHttpRequestParameter(String name, final String value) {
85 when(httpRequest.getPartAsStringFailsafe(eq(name), anyInt())).thenAnswer(new Answer<String>() {
87 public String answer(InvocationOnMock invocation) throws Throwable {
88 int maxLength = invocation.getArgument(1);
89 return value.substring(0, Math.min(maxLength, value.length()));
94 protected void addPost(String postId, Post post) {
95 when(core.getPost(postId)).thenReturn(Optional.fromNullable(post));