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.anyString;
6 import static org.mockito.ArgumentMatchers.eq;
7 import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
8 import static org.mockito.Mockito.mock;
9 import static org.mockito.Mockito.when;
12 import java.net.URISyntaxException;
13 import java.util.ArrayList;
15 import net.pterodactylus.sone.core.Core;
16 import net.pterodactylus.sone.core.UpdateChecker;
17 import net.pterodactylus.sone.data.Post;
18 import net.pterodactylus.sone.data.Sone;
19 import net.pterodactylus.sone.web.page.FreenetRequest;
20 import net.pterodactylus.util.notify.Notification;
21 import net.pterodactylus.util.template.Template;
22 import net.pterodactylus.util.template.TemplateContext;
23 import net.pterodactylus.util.web.Method;
25 import freenet.clients.http.ToadletContext;
26 import freenet.support.api.HTTPRequest;
28 import com.google.common.base.Optional;
29 import org.junit.Before;
30 import org.junit.Rule;
31 import org.junit.rules.ExpectedException;
32 import org.mockito.invocation.InvocationOnMock;
33 import org.mockito.stubbing.Answer;
36 * Base class for web page tests.
38 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
40 public abstract class WebPageTest {
43 public final ExpectedException expectedException = ExpectedException.none();
45 protected final Template template = new Template();
46 protected final WebInterface webInterface = mock(WebInterface.class, RETURNS_DEEP_STUBS);
47 protected final Core core = webInterface.getCore();
49 protected final Sone currentSone = mock(Sone.class);
51 protected final TemplateContext templateContext = new TemplateContext();
52 protected final HTTPRequest httpRequest = mock(HTTPRequest.class);
53 protected final FreenetRequest freenetRequest = mock(FreenetRequest.class);
54 protected final ToadletContext toadletContext = mock(ToadletContext.class);
58 public final void setupFreenetRequest() {
59 when(freenetRequest.getToadletContext()).thenReturn(toadletContext);
60 when(freenetRequest.getHttpRequest()).thenReturn(httpRequest);
61 when(httpRequest.getPartAsStringFailsafe(anyString(), anyInt())).thenAnswer(new Answer<String>() {
63 public String answer(InvocationOnMock invocation) throws Throwable {
70 public final void setupCore() {
71 UpdateChecker updateChecker = mock(UpdateChecker.class);
72 when(webInterface.getCore().getUpdateChecker()).thenReturn(updateChecker);
76 public final void setupWebInterface() {
77 when(webInterface.getCurrentSone(toadletContext)).thenReturn(currentSone);
78 when(webInterface.getCurrentSone(eq(toadletContext), anyBoolean())).thenReturn(currentSone);
79 when(webInterface.getNotifications(currentSone)).thenReturn(new ArrayList<Notification>());
82 protected void request(String uri, Method method) {
84 when(freenetRequest.getUri()).thenReturn(new URI(uri));
85 } catch (URISyntaxException e) {
86 throw new RuntimeException(e);
88 when(freenetRequest.getMethod()).thenReturn(method);
91 protected void addHttpRequestParameter(String name, final String value) {
92 when(httpRequest.getPartAsStringFailsafe(eq(name), anyInt())).thenAnswer(new Answer<String>() {
94 public String answer(InvocationOnMock invocation) throws Throwable {
95 int maxLength = invocation.getArgument(1);
96 return value.substring(0, Math.min(maxLength, value.length()));
101 protected void addPost(String postId, Post post) {
102 when(core.getPost(postId)).thenReturn(Optional.fromNullable(post));