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