Add test for bookmark page
[Sone.git] / src / test / java / net / pterodactylus / sone / web / WebPageTest.java
1 package net.pterodactylus.sone.web;
2
3 import static org.mockito.ArgumentMatchers.anyBoolean;
4 import static org.mockito.ArgumentMatchers.anyInt;
5 import static org.mockito.ArgumentMatchers.eq;
6 import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
7 import static org.mockito.Mockito.mock;
8 import static org.mockito.Mockito.when;
9
10 import java.net.URI;
11 import java.net.URISyntaxException;
12 import java.util.ArrayList;
13
14 import net.pterodactylus.sone.core.Core;
15 import net.pterodactylus.sone.core.UpdateChecker;
16 import net.pterodactylus.sone.data.Post;
17 import net.pterodactylus.sone.data.Sone;
18 import net.pterodactylus.sone.web.page.FreenetRequest;
19 import net.pterodactylus.util.notify.Notification;
20 import net.pterodactylus.util.template.Template;
21 import net.pterodactylus.util.template.TemplateContext;
22 import net.pterodactylus.util.web.Method;
23
24 import freenet.clients.http.ToadletContext;
25 import freenet.support.api.HTTPRequest;
26
27 import com.google.common.base.Optional;
28 import org.junit.Before;
29 import org.junit.Rule;
30 import org.junit.rules.ExpectedException;
31 import org.mockito.invocation.InvocationOnMock;
32 import org.mockito.stubbing.Answer;
33
34 /**
35  * Base class for web page tests.
36  *
37  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
38  */
39 public abstract class WebPageTest {
40
41         @Rule
42         public final ExpectedException expectedException = ExpectedException.none();
43
44         protected final Template template = new Template();
45         protected final WebInterface webInterface = mock(WebInterface.class, RETURNS_DEEP_STUBS);
46         protected final Core core = webInterface.getCore();
47
48         protected final Sone currentSone = mock(Sone.class);
49
50         protected final TemplateContext templateContext = new TemplateContext();
51         protected final HTTPRequest httpRequest = mock(HTTPRequest.class);
52         protected final FreenetRequest freenetRequest = mock(FreenetRequest.class);
53         protected final ToadletContext toadletContext = mock(ToadletContext.class);
54
55
56         @Before
57         public final void setupFreenetRequest() {
58                 when(freenetRequest.getToadletContext()).thenReturn(toadletContext);
59                 when(freenetRequest.getHttpRequest()).thenReturn(httpRequest);
60         }
61
62         @Before
63         public final void setupCore() {
64                 UpdateChecker updateChecker = mock(UpdateChecker.class);
65                 when(webInterface.getCore().getUpdateChecker()).thenReturn(updateChecker);
66         }
67
68         @Before
69         public final void setupWebInterface() {
70                 when(webInterface.getCurrentSone(toadletContext)).thenReturn(currentSone);
71                 when(webInterface.getCurrentSone(eq(toadletContext), anyBoolean())).thenReturn(currentSone);
72                 when(webInterface.getNotifications(currentSone)).thenReturn(new ArrayList<Notification>());
73         }
74
75         protected void request(String uri, Method method) {
76                 try {
77                         when(freenetRequest.getUri()).thenReturn(new URI(uri));
78                 } catch (URISyntaxException e) {
79                         throw new RuntimeException(e);
80                 }
81                 when(freenetRequest.getMethod()).thenReturn(method);
82         }
83
84         protected void addHttpRequestParameter(String name, final String value) {
85                 when(httpRequest.getPartAsStringFailsafe(eq(name), anyInt())).thenAnswer(new Answer<String>() {
86                         @Override
87                         public String answer(InvocationOnMock invocation) throws Throwable {
88                                 int maxLength = invocation.getArgument(1);
89                                 return value.substring(0, Math.min(maxLength, value.length()));
90                         }
91                 });
92         }
93
94         protected void addPost(String postId, Post post) {
95                 when(core.getPost(postId)).thenReturn(Optional.fromNullable(post));
96         }
97
98 }