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;
14 import java.util.HashSet;
15 import java.util.List;
18 import net.pterodactylus.sone.core.Core;
19 import net.pterodactylus.sone.core.Preferences;
20 import net.pterodactylus.sone.core.UpdateChecker;
21 import net.pterodactylus.sone.data.Album;
22 import net.pterodactylus.sone.data.Image;
23 import net.pterodactylus.sone.data.Post;
24 import net.pterodactylus.sone.data.Sone;
25 import net.pterodactylus.sone.data.SoneOptions.DefaultSoneOptions;
26 import net.pterodactylus.sone.freenet.wot.OwnIdentity;
27 import net.pterodactylus.sone.web.page.FreenetRequest;
28 import net.pterodactylus.util.notify.Notification;
29 import net.pterodactylus.util.template.Template;
30 import net.pterodactylus.util.template.TemplateContext;
31 import net.pterodactylus.util.web.Method;
33 import freenet.clients.http.ToadletContext;
34 import freenet.support.api.HTTPRequest;
36 import com.google.common.base.Optional;
37 import com.google.common.eventbus.EventBus;
38 import org.junit.Before;
39 import org.junit.Rule;
40 import org.junit.rules.ExpectedException;
41 import org.mockito.invocation.InvocationOnMock;
42 import org.mockito.stubbing.Answer;
45 * Base class for web page tests.
47 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
49 public abstract class WebPageTest {
52 public final ExpectedException expectedException = ExpectedException.none();
54 protected final Template template = new Template();
55 protected final WebInterface webInterface = mock(WebInterface.class, RETURNS_DEEP_STUBS);
56 protected final EventBus eventBus = mock(EventBus.class);
57 protected final Core core = webInterface.getCore();
59 protected final Sone currentSone = mock(Sone.class);
61 protected final TemplateContext templateContext = new TemplateContext();
62 protected final HTTPRequest httpRequest = mock(HTTPRequest.class);
63 protected final FreenetRequest freenetRequest = mock(FreenetRequest.class);
64 protected final ToadletContext toadletContext = mock(ToadletContext.class);
66 private final Set<OwnIdentity> ownIdentities = new HashSet<>();
67 private final List<Sone> localSones = new ArrayList<>();
70 public final void setupFreenetRequest() {
71 when(freenetRequest.getToadletContext()).thenReturn(toadletContext);
72 when(freenetRequest.getHttpRequest()).thenReturn(httpRequest);
73 when(httpRequest.getPartAsStringFailsafe(anyString(), anyInt())).thenAnswer(new Answer<String>() {
75 public String answer(InvocationOnMock invocation) throws Throwable {
79 when(httpRequest.getParam(anyString())).thenReturn("");
80 when(httpRequest.getParam(anyString(), anyString())).thenReturn("");
84 public final void setupCore() {
85 UpdateChecker updateChecker = mock(UpdateChecker.class);
86 when(core.getUpdateChecker()).thenReturn(updateChecker);
87 when(core.getPreferences()).thenReturn(new Preferences(eventBus));
88 when(core.getLocalSone(anyString())).thenReturn(null);
89 when(core.getLocalSones()).thenReturn(localSones);
90 when(core.getSone(anyString())).thenReturn(Optional.<Sone>absent());
91 when(core.getPost(anyString())).thenReturn(Optional.<Post>absent());
92 when(core.getAlbum(anyString())).thenReturn(null);
93 when(core.getImage(anyString())).thenReturn(null);
94 when(core.getImage(anyString(), anyBoolean())).thenReturn(null);
98 public final void setupIdentityManager() {
99 when(core.getIdentityManager().getAllOwnIdentities()).thenReturn(ownIdentities);
103 public final void setupWebInterface() {
104 when(webInterface.getCurrentSone(toadletContext)).thenReturn(currentSone);
105 when(webInterface.getCurrentSone(eq(toadletContext), anyBoolean())).thenReturn(currentSone);
106 when(webInterface.getNotifications(currentSone)).thenReturn(new ArrayList<Notification>());
110 public void setupSone() {
111 when(currentSone.getOptions()).thenReturn(new DefaultSoneOptions());
114 protected void unsetCurrentSone() {
115 when(webInterface.getCurrentSone(toadletContext)).thenReturn(null);
116 when(webInterface.getCurrentSone(eq(toadletContext), anyBoolean())).thenReturn(null);
119 protected void request(String uri, Method method) {
121 when(freenetRequest.getUri()).thenReturn(new URI(uri));
122 } catch (URISyntaxException e) {
123 throw new RuntimeException(e);
125 when(freenetRequest.getMethod()).thenReturn(method);
128 protected void addHttpRequestParameter(String name, final String value) {
129 when(httpRequest.getPartAsStringFailsafe(eq(name), anyInt())).thenAnswer(new Answer<String>() {
131 public String answer(InvocationOnMock invocation) throws Throwable {
132 int maxLength = invocation.getArgument(1);
133 return value.substring(0, Math.min(maxLength, value.length()));
136 when(httpRequest.getParam(eq(name))).thenReturn(value);
137 when(httpRequest.getParam(eq(name), anyString())).thenReturn(value);
138 when(httpRequest.isPartSet(eq(name))).thenReturn(value != null && !value.isEmpty());
141 protected void addPost(String postId, Post post) {
142 when(core.getPost(postId)).thenReturn(Optional.fromNullable(post));
145 protected void addSone(String soneId, Sone sone) {
146 when(core.getSone(eq(soneId))).thenReturn(Optional.fromNullable(sone));
149 protected void addLocalSone(String soneId, Sone sone) {
150 when(core.getLocalSone(eq(soneId))).thenReturn(sone);
151 localSones.add(sone);
154 protected void addOwnIdentity(OwnIdentity ownIdentity) {
155 ownIdentities.add(ownIdentity);
158 protected void addAlbum(String albumId, Album album) {
159 when(core.getAlbum(eq(albumId))).thenReturn(album);
162 protected void addImage(String imageId, Image image) {
163 when(core.getImage(eq(imageId))).thenReturn(image);
164 when(core.getImage(eq(imageId), anyBoolean())).thenReturn(image);