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.getNotification(anyString())).thenReturn(Optional.<Notification>absent());
107 when(webInterface.getNotifications(currentSone)).thenReturn(new ArrayList<Notification>());
111 public void setupSone() {
112 when(currentSone.getOptions()).thenReturn(new DefaultSoneOptions());
115 protected void unsetCurrentSone() {
116 when(webInterface.getCurrentSone(toadletContext)).thenReturn(null);
117 when(webInterface.getCurrentSone(eq(toadletContext), anyBoolean())).thenReturn(null);
120 protected void request(String uri, Method method) {
122 when(freenetRequest.getUri()).thenReturn(new URI(uri));
123 } catch (URISyntaxException e) {
124 throw new RuntimeException(e);
126 when(freenetRequest.getMethod()).thenReturn(method);
129 protected void addHttpRequestParameter(String name, final String value) {
130 when(httpRequest.getPartAsStringFailsafe(eq(name), anyInt())).thenAnswer(new Answer<String>() {
132 public String answer(InvocationOnMock invocation) throws Throwable {
133 int maxLength = invocation.getArgument(1);
134 return value.substring(0, Math.min(maxLength, value.length()));
137 when(httpRequest.getParam(eq(name))).thenReturn(value);
138 when(httpRequest.getParam(eq(name), anyString())).thenReturn(value);
139 when(httpRequest.isPartSet(eq(name))).thenReturn(value != null && !value.isEmpty());
142 protected void addPost(String postId, Post post) {
143 when(core.getPost(postId)).thenReturn(Optional.fromNullable(post));
146 protected void addSone(String soneId, Sone sone) {
147 when(core.getSone(eq(soneId))).thenReturn(Optional.fromNullable(sone));
150 protected void addLocalSone(String soneId, Sone sone) {
151 when(core.getLocalSone(eq(soneId))).thenReturn(sone);
152 localSones.add(sone);
155 protected void addOwnIdentity(OwnIdentity ownIdentity) {
156 ownIdentities.add(ownIdentity);
159 protected void addAlbum(String albumId, Album album) {
160 when(core.getAlbum(eq(albumId))).thenReturn(album);
163 protected void addImage(String imageId, Image image) {
164 when(core.getImage(eq(imageId))).thenReturn(image);
165 when(core.getImage(eq(imageId), anyBoolean())).thenReturn(image);
168 protected void addNotification(String notificationId, Notification notification) {
169 when(webInterface.getNotification(eq(notificationId))).thenReturn(Optional.of(notification));