Add test for CreatePostPage
[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(core.getUpdateChecker()).thenReturn(updateChecker);
73                 when(core.getLocalSone(anyString())).thenReturn(null);
74                 when(core.getSone(anyString())).thenReturn(Optional.<Sone>absent());
75         }
76
77         @Before
78         public final void setupWebInterface() {
79                 when(webInterface.getCurrentSone(toadletContext)).thenReturn(currentSone);
80                 when(webInterface.getCurrentSone(eq(toadletContext), anyBoolean())).thenReturn(currentSone);
81                 when(webInterface.getNotifications(currentSone)).thenReturn(new ArrayList<Notification>());
82         }
83
84         protected void request(String uri, Method method) {
85                 try {
86                         when(freenetRequest.getUri()).thenReturn(new URI(uri));
87                 } catch (URISyntaxException e) {
88                         throw new RuntimeException(e);
89                 }
90                 when(freenetRequest.getMethod()).thenReturn(method);
91         }
92
93         protected void addHttpRequestParameter(String name, final String value) {
94                 when(httpRequest.getPartAsStringFailsafe(eq(name), anyInt())).thenAnswer(new Answer<String>() {
95                         @Override
96                         public String answer(InvocationOnMock invocation) throws Throwable {
97                                 int maxLength = invocation.getArgument(1);
98                                 return value.substring(0, Math.min(maxLength, value.length()));
99                         }
100                 });
101         }
102
103         protected void addPost(String postId, Post post) {
104                 when(core.getPost(postId)).thenReturn(Optional.fromNullable(post));
105         }
106
107         protected void addSone(String soneId, Sone sone) {
108                 when(core.getSone(eq(soneId))).thenReturn(Optional.fromNullable(sone));
109         }
110
111         protected void addLocalSone(String soneId, Sone sone) {
112                 when(core.getLocalSone(eq(soneId))).thenReturn(sone);
113         }
114
115 }