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.HashMap;
15 import java.util.HashSet;
16 import java.util.List;
20 import javax.annotation.Nonnull;
22 import net.pterodactylus.sone.core.Core;
23 import net.pterodactylus.sone.core.Preferences;
24 import net.pterodactylus.sone.core.UpdateChecker;
25 import net.pterodactylus.sone.data.Album;
26 import net.pterodactylus.sone.data.Image;
27 import net.pterodactylus.sone.data.Post;
28 import net.pterodactylus.sone.data.Sone;
29 import net.pterodactylus.sone.data.SoneOptions.DefaultSoneOptions;
30 import net.pterodactylus.sone.freenet.wot.OwnIdentity;
31 import net.pterodactylus.sone.web.page.FreenetRequest;
32 import net.pterodactylus.util.notify.Notification;
33 import net.pterodactylus.util.template.Template;
34 import net.pterodactylus.util.template.TemplateContext;
35 import net.pterodactylus.util.web.Method;
37 import freenet.clients.http.ToadletContext;
38 import freenet.support.api.HTTPRequest;
40 import com.google.common.base.Optional;
41 import com.google.common.eventbus.EventBus;
42 import org.junit.Before;
43 import org.junit.Rule;
44 import org.junit.rules.ExpectedException;
45 import org.mockito.invocation.InvocationOnMock;
46 import org.mockito.stubbing.Answer;
49 * Base class for web page tests.
51 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
53 public abstract class WebPageTest {
56 public final ExpectedException expectedException = ExpectedException.none();
58 protected final Template template = new Template();
59 protected final WebInterface webInterface = mock(WebInterface.class, RETURNS_DEEP_STUBS);
60 protected final EventBus eventBus = mock(EventBus.class);
61 protected final Core core = webInterface.getCore();
63 protected final Sone currentSone = mock(Sone.class);
65 protected final TemplateContext templateContext = new TemplateContext();
66 protected final HTTPRequest httpRequest = mock(HTTPRequest.class);
67 protected final Map<String, String> requestParameters = new HashMap<>();
68 protected final Map<String, String> requestHeaders = new HashMap<>();
69 protected final FreenetRequest freenetRequest = mock(FreenetRequest.class);
70 protected final ToadletContext toadletContext = mock(ToadletContext.class);
72 private final Set<OwnIdentity> ownIdentities = new HashSet<>();
73 private final List<Sone> localSones = new ArrayList<>();
76 public final void setupFreenetRequest() {
77 when(freenetRequest.getToadletContext()).thenReturn(toadletContext);
78 when(freenetRequest.getHttpRequest()).thenReturn(httpRequest);
79 when(httpRequest.getPartAsStringFailsafe(anyString(), anyInt())).thenAnswer(new Answer<String>() {
81 public String answer(InvocationOnMock invocation) throws Throwable {
82 String parameter = invocation.getArgument(0);
83 int maxLength = invocation.getArgument(1);
84 return requestParameters.containsKey(parameter) ? requestParameters.get(parameter).substring(0, Math.min(maxLength, requestParameters.get(parameter).length())) : "";
87 when(httpRequest.getParam(anyString())).thenAnswer(new Answer<String>() {
89 public String answer(InvocationOnMock invocation) throws Throwable {
90 String parameter = invocation.getArgument(0);
91 return requestParameters.containsKey(parameter) ? requestParameters.get(parameter) : "";
94 when(httpRequest.getParam(anyString(), anyString())).thenAnswer(new Answer<String>() {
96 public String answer(InvocationOnMock invocation) throws Throwable {
97 String parameter = invocation.getArgument(0);
98 return requestParameters.containsKey(parameter) ? requestParameters.get(parameter) : invocation.<String>getArgument(1);
101 when(httpRequest.isPartSet(anyString())).thenAnswer(new Answer<Boolean>() {
103 public Boolean answer(InvocationOnMock invocation) throws Throwable {
104 return requestParameters.containsKey(invocation.<String>getArgument(0));
107 when(httpRequest.getParts()).thenAnswer(new Answer<String[]>() {
109 public String[] answer(InvocationOnMock invocation) throws Throwable {
110 return requestParameters.keySet().toArray(new String[requestParameters.size()]);
113 when(httpRequest.getHeader(anyString())).thenAnswer(new Answer<String>() {
115 public String answer(InvocationOnMock invocation) throws Throwable {
116 return requestHeaders.get(invocation.<String>getArgument(0).toLowerCase());
122 public final void setupCore() {
123 UpdateChecker updateChecker = mock(UpdateChecker.class);
124 when(core.getUpdateChecker()).thenReturn(updateChecker);
125 when(core.getPreferences()).thenReturn(new Preferences(eventBus));
126 when(core.getLocalSone(anyString())).thenReturn(null);
127 when(core.getLocalSones()).thenReturn(localSones);
128 when(core.getSone(anyString())).thenReturn(Optional.<Sone>absent());
129 when(core.getPost(anyString())).thenReturn(Optional.<Post>absent());
130 when(core.getAlbum(anyString())).thenReturn(null);
131 when(core.getImage(anyString())).thenReturn(null);
132 when(core.getImage(anyString(), anyBoolean())).thenReturn(null);
136 public final void setupIdentityManager() {
137 when(core.getIdentityManager().getAllOwnIdentities()).thenReturn(ownIdentities);
141 public final void setupWebInterface() {
142 when(webInterface.getCurrentSone(toadletContext)).thenReturn(currentSone);
143 when(webInterface.getCurrentSone(eq(toadletContext), anyBoolean())).thenReturn(currentSone);
144 when(webInterface.getNotification(anyString())).thenReturn(Optional.<Notification>absent());
145 when(webInterface.getNotifications(currentSone)).thenReturn(new ArrayList<Notification>());
149 public void setupSone() {
150 when(currentSone.getOptions()).thenReturn(new DefaultSoneOptions());
153 protected void unsetCurrentSone() {
154 when(webInterface.getCurrentSone(toadletContext)).thenReturn(null);
155 when(webInterface.getCurrentSone(eq(toadletContext), anyBoolean())).thenReturn(null);
158 protected void request(String uri, Method method) {
160 when(freenetRequest.getUri()).thenReturn(new URI(uri));
161 } catch (URISyntaxException e) {
162 throw new RuntimeException(e);
164 when(freenetRequest.getMethod()).thenReturn(method);
167 protected void addHttpRequestHeader(@Nonnull String name, String value) {
168 requestHeaders.put(name.toLowerCase(), value);
171 protected void addHttpRequestParameter(String name, final String value) {
172 requestParameters.put(name, value);
175 protected void addPost(String postId, Post post) {
176 when(core.getPost(postId)).thenReturn(Optional.fromNullable(post));
179 protected void addSone(String soneId, Sone sone) {
180 when(core.getSone(eq(soneId))).thenReturn(Optional.fromNullable(sone));
183 protected void addLocalSone(String soneId, Sone sone) {
184 when(core.getLocalSone(eq(soneId))).thenReturn(sone);
185 localSones.add(sone);
188 protected void addOwnIdentity(OwnIdentity ownIdentity) {
189 ownIdentities.add(ownIdentity);
192 protected void addAlbum(String albumId, Album album) {
193 when(core.getAlbum(eq(albumId))).thenReturn(album);
196 protected void addImage(String imageId, Image image) {
197 when(core.getImage(eq(imageId))).thenReturn(image);
198 when(core.getImage(eq(imageId), anyBoolean())).thenReturn(image);
201 protected void addNotification(String notificationId, Notification notification) {
202 when(webInterface.getNotification(eq(notificationId))).thenReturn(Optional.of(notification));