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;
11 import java.io.ByteArrayOutputStream;
12 import java.io.IOException;
13 import java.io.PipedInputStream;
14 import java.io.PipedOutputStream;
16 import java.net.URISyntaxException;
17 import java.util.ArrayList;
18 import java.util.HashMap;
19 import java.util.HashSet;
20 import java.util.List;
24 import javax.annotation.Nonnull;
26 import net.pterodactylus.sone.core.Core;
27 import net.pterodactylus.sone.core.Preferences;
28 import net.pterodactylus.sone.core.UpdateChecker;
29 import net.pterodactylus.sone.data.Album;
30 import net.pterodactylus.sone.data.Image;
31 import net.pterodactylus.sone.data.Post;
32 import net.pterodactylus.sone.data.Sone;
33 import net.pterodactylus.sone.data.SoneOptions.DefaultSoneOptions;
34 import net.pterodactylus.sone.data.TemporaryImage;
35 import net.pterodactylus.sone.freenet.wot.OwnIdentity;
36 import net.pterodactylus.sone.web.page.FreenetRequest;
37 import net.pterodactylus.util.notify.Notification;
38 import net.pterodactylus.util.template.Template;
39 import net.pterodactylus.util.template.TemplateContext;
40 import net.pterodactylus.util.web.Method;
41 import net.pterodactylus.util.web.Response;
43 import freenet.clients.http.ToadletContext;
44 import freenet.support.api.HTTPRequest;
46 import com.google.common.base.Optional;
47 import com.google.common.eventbus.EventBus;
48 import com.google.common.io.ByteStreams;
49 import org.junit.Before;
50 import org.junit.Rule;
51 import org.junit.rules.ExpectedException;
52 import org.mockito.invocation.InvocationOnMock;
53 import org.mockito.stubbing.Answer;
56 * Base class for web page tests.
58 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
60 public abstract class WebPageTest {
63 public final ExpectedException expectedException = ExpectedException.none();
65 protected final Template template = new Template();
66 protected final WebInterface webInterface = mock(WebInterface.class, RETURNS_DEEP_STUBS);
67 protected final EventBus eventBus = mock(EventBus.class);
68 protected final Core core = webInterface.getCore();
70 protected final Sone currentSone = mock(Sone.class);
72 protected final TemplateContext templateContext = new TemplateContext();
73 protected final HTTPRequest httpRequest = mock(HTTPRequest.class);
74 protected final Map<String, String> requestParameters = new HashMap<>();
75 protected final Map<String, String> requestHeaders = new HashMap<>();
76 protected final FreenetRequest freenetRequest = mock(FreenetRequest.class);
77 private final PipedOutputStream responseOutputStream = new PipedOutputStream();
78 private final PipedInputStream responseInputStream;
79 protected final Response response = new Response(responseOutputStream);
80 protected final ToadletContext toadletContext = mock(ToadletContext.class);
82 private final Set<OwnIdentity> ownIdentities = new HashSet<>();
83 private final List<Sone> localSones = new ArrayList<>();
85 protected WebPageTest() {
87 responseInputStream = new PipedInputStream(responseOutputStream);
88 } catch (IOException e) {
89 throw new RuntimeException(e);
94 public final void setupFreenetRequest() {
95 when(freenetRequest.getToadletContext()).thenReturn(toadletContext);
96 when(freenetRequest.getHttpRequest()).thenReturn(httpRequest);
97 when(httpRequest.getPartAsStringFailsafe(anyString(), anyInt())).thenAnswer(new Answer<String>() {
99 public String answer(InvocationOnMock invocation) throws Throwable {
100 String parameter = invocation.getArgument(0);
101 int maxLength = invocation.getArgument(1);
102 return requestParameters.containsKey(parameter) ? requestParameters.get(parameter).substring(0, Math.min(maxLength, requestParameters.get(parameter).length())) : "";
105 when(httpRequest.getParam(anyString())).thenAnswer(new Answer<String>() {
107 public String answer(InvocationOnMock invocation) throws Throwable {
108 String parameter = invocation.getArgument(0);
109 return requestParameters.containsKey(parameter) ? requestParameters.get(parameter) : "";
112 when(httpRequest.getParam(anyString(), anyString())).thenAnswer(new Answer<String>() {
114 public String answer(InvocationOnMock invocation) throws Throwable {
115 String parameter = invocation.getArgument(0);
116 return requestParameters.containsKey(parameter) ? requestParameters.get(parameter) : invocation.<String>getArgument(1);
119 when(httpRequest.isPartSet(anyString())).thenAnswer(new Answer<Boolean>() {
121 public Boolean answer(InvocationOnMock invocation) throws Throwable {
122 return requestParameters.containsKey(invocation.<String>getArgument(0));
125 when(httpRequest.getParts()).thenAnswer(new Answer<String[]>() {
127 public String[] answer(InvocationOnMock invocation) throws Throwable {
128 return requestParameters.keySet().toArray(new String[requestParameters.size()]);
131 when(httpRequest.getHeader(anyString())).thenAnswer(new Answer<String>() {
133 public String answer(InvocationOnMock invocation) throws Throwable {
134 return requestHeaders.get(invocation.<String>getArgument(0).toLowerCase());
140 public final void setupCore() {
141 UpdateChecker updateChecker = mock(UpdateChecker.class);
142 when(core.getUpdateChecker()).thenReturn(updateChecker);
143 when(core.getPreferences()).thenReturn(new Preferences(eventBus));
144 when(core.getLocalSone(anyString())).thenReturn(null);
145 when(core.getLocalSones()).thenReturn(localSones);
146 when(core.getSone(anyString())).thenReturn(Optional.<Sone>absent());
147 when(core.getPost(anyString())).thenReturn(Optional.<Post>absent());
148 when(core.getAlbum(anyString())).thenReturn(null);
149 when(core.getImage(anyString())).thenReturn(null);
150 when(core.getImage(anyString(), anyBoolean())).thenReturn(null);
151 when(core.getTemporaryImage(anyString())).thenReturn(null);
155 public final void setupIdentityManager() {
156 when(core.getIdentityManager().getAllOwnIdentities()).thenReturn(ownIdentities);
160 public final void setupWebInterface() {
161 when(webInterface.getCurrentSone(toadletContext)).thenReturn(currentSone);
162 when(webInterface.getCurrentSone(eq(toadletContext), anyBoolean())).thenReturn(currentSone);
163 when(webInterface.getNotification(anyString())).thenReturn(Optional.<Notification>absent());
164 when(webInterface.getNotifications(currentSone)).thenReturn(new ArrayList<Notification>());
168 public void setupSone() {
169 when(currentSone.getOptions()).thenReturn(new DefaultSoneOptions());
172 protected void unsetCurrentSone() {
173 when(webInterface.getCurrentSone(toadletContext)).thenReturn(null);
174 when(webInterface.getCurrentSone(eq(toadletContext), anyBoolean())).thenReturn(null);
177 protected void request(String uri, Method method) {
179 when(freenetRequest.getUri()).thenReturn(new URI(uri));
180 } catch (URISyntaxException e) {
181 throw new RuntimeException(e);
183 when(freenetRequest.getMethod()).thenReturn(method);
186 protected void addHttpRequestHeader(@Nonnull String name, String value) {
187 requestHeaders.put(name.toLowerCase(), value);
190 protected void addHttpRequestParameter(String name, final String value) {
191 requestParameters.put(name, value);
194 protected void addPost(String postId, Post post) {
195 when(core.getPost(postId)).thenReturn(Optional.fromNullable(post));
198 protected void addSone(String soneId, Sone sone) {
199 when(core.getSone(eq(soneId))).thenReturn(Optional.fromNullable(sone));
202 protected void addLocalSone(String soneId, Sone sone) {
203 when(core.getLocalSone(eq(soneId))).thenReturn(sone);
204 localSones.add(sone);
207 protected void addOwnIdentity(OwnIdentity ownIdentity) {
208 ownIdentities.add(ownIdentity);
211 protected void addAlbum(String albumId, Album album) {
212 when(core.getAlbum(eq(albumId))).thenReturn(album);
215 protected void addImage(String imageId, Image image) {
216 when(core.getImage(eq(imageId))).thenReturn(image);
217 when(core.getImage(eq(imageId), anyBoolean())).thenReturn(image);
220 protected void addTemporaryImage(String imageId, TemporaryImage temporaryImage) {
221 when(core.getTemporaryImage(eq(imageId))).thenReturn(temporaryImage);
224 protected byte[] getResponseBytes() throws IOException {
225 response.getContent().close();
226 try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
227 ByteStreams.copy(responseInputStream, outputStream);
228 return outputStream.toByteArray();
232 protected void addNotification(String notificationId, Notification notification) {
233 when(webInterface.getNotification(eq(notificationId))).thenReturn(Optional.of(notification));