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> requestHeaders = new HashMap<>();
68 protected final FreenetRequest freenetRequest = mock(FreenetRequest.class);
69 protected final ToadletContext toadletContext = mock(ToadletContext.class);
71 private final Set<OwnIdentity> ownIdentities = new HashSet<>();
72 private final List<Sone> localSones = new ArrayList<>();
75 public final void setupFreenetRequest() {
76 when(freenetRequest.getToadletContext()).thenReturn(toadletContext);
77 when(freenetRequest.getHttpRequest()).thenReturn(httpRequest);
78 when(httpRequest.getPartAsStringFailsafe(anyString(), anyInt())).thenAnswer(new Answer<String>() {
80 public String answer(InvocationOnMock invocation) throws Throwable {
84 when(httpRequest.getParam(anyString())).thenReturn("");
85 when(httpRequest.getParam(anyString(), anyString())).thenReturn("");
86 when(httpRequest.getHeader(anyString())).thenAnswer(new Answer<String>() {
88 public String answer(InvocationOnMock invocation) throws Throwable {
89 return requestHeaders.get(invocation.<String>getArgument(0).toLowerCase());
95 public final void setupCore() {
96 UpdateChecker updateChecker = mock(UpdateChecker.class);
97 when(core.getUpdateChecker()).thenReturn(updateChecker);
98 when(core.getPreferences()).thenReturn(new Preferences(eventBus));
99 when(core.getLocalSone(anyString())).thenReturn(null);
100 when(core.getLocalSones()).thenReturn(localSones);
101 when(core.getSone(anyString())).thenReturn(Optional.<Sone>absent());
102 when(core.getPost(anyString())).thenReturn(Optional.<Post>absent());
103 when(core.getAlbum(anyString())).thenReturn(null);
104 when(core.getImage(anyString())).thenReturn(null);
105 when(core.getImage(anyString(), anyBoolean())).thenReturn(null);
109 public final void setupIdentityManager() {
110 when(core.getIdentityManager().getAllOwnIdentities()).thenReturn(ownIdentities);
114 public final void setupWebInterface() {
115 when(webInterface.getCurrentSone(toadletContext)).thenReturn(currentSone);
116 when(webInterface.getCurrentSone(eq(toadletContext), anyBoolean())).thenReturn(currentSone);
117 when(webInterface.getNotification(anyString())).thenReturn(Optional.<Notification>absent());
118 when(webInterface.getNotifications(currentSone)).thenReturn(new ArrayList<Notification>());
122 public void setupSone() {
123 when(currentSone.getOptions()).thenReturn(new DefaultSoneOptions());
126 protected void unsetCurrentSone() {
127 when(webInterface.getCurrentSone(toadletContext)).thenReturn(null);
128 when(webInterface.getCurrentSone(eq(toadletContext), anyBoolean())).thenReturn(null);
131 protected void request(String uri, Method method) {
133 when(freenetRequest.getUri()).thenReturn(new URI(uri));
134 } catch (URISyntaxException e) {
135 throw new RuntimeException(e);
137 when(freenetRequest.getMethod()).thenReturn(method);
140 protected void addHttpRequestHeader(@Nonnull String name, String value) {
141 requestHeaders.put(name.toLowerCase(), value);
144 protected void addHttpRequestParameter(String name, final String value) {
145 when(httpRequest.getPartAsStringFailsafe(eq(name), anyInt())).thenAnswer(new Answer<String>() {
147 public String answer(InvocationOnMock invocation) throws Throwable {
148 int maxLength = invocation.getArgument(1);
149 return value.substring(0, Math.min(maxLength, value.length()));
152 when(httpRequest.getParam(eq(name))).thenReturn(value);
153 when(httpRequest.getParam(eq(name), anyString())).thenReturn(value);
154 when(httpRequest.isPartSet(eq(name))).thenReturn(value != null && !value.isEmpty());
157 protected void addPost(String postId, Post post) {
158 when(core.getPost(postId)).thenReturn(Optional.fromNullable(post));
161 protected void addSone(String soneId, Sone sone) {
162 when(core.getSone(eq(soneId))).thenReturn(Optional.fromNullable(sone));
165 protected void addLocalSone(String soneId, Sone sone) {
166 when(core.getLocalSone(eq(soneId))).thenReturn(sone);
167 localSones.add(sone);
170 protected void addOwnIdentity(OwnIdentity ownIdentity) {
171 ownIdentities.add(ownIdentity);
174 protected void addAlbum(String albumId, Album album) {
175 when(core.getAlbum(eq(albumId))).thenReturn(album);
178 protected void addImage(String imageId, Image image) {
179 when(core.getImage(eq(imageId))).thenReturn(image);
180 when(core.getImage(eq(imageId), anyBoolean())).thenReturn(image);
183 protected void addNotification(String notificationId, Notification notification) {
184 when(webInterface.getNotification(eq(notificationId))).thenReturn(Optional.of(notification));