Setup core and web interface separately
[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.eq;
5 import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
6 import static org.mockito.Mockito.mock;
7 import static org.mockito.Mockito.when;
8
9 import java.net.URI;
10 import java.net.URISyntaxException;
11 import java.util.ArrayList;
12
13 import net.pterodactylus.sone.core.Core;
14 import net.pterodactylus.sone.core.UpdateChecker;
15 import net.pterodactylus.sone.data.Sone;
16 import net.pterodactylus.sone.web.page.FreenetRequest;
17 import net.pterodactylus.util.notify.Notification;
18 import net.pterodactylus.util.template.Template;
19 import net.pterodactylus.util.template.TemplateContext;
20 import net.pterodactylus.util.web.Method;
21
22 import freenet.clients.http.ToadletContext;
23 import freenet.support.api.HTTPRequest;
24
25 import org.junit.Before;
26 import org.junit.Rule;
27 import org.junit.rules.ExpectedException;
28
29 /**
30  * Base class for web page tests.
31  *
32  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
33  */
34 public abstract class WebPageTest {
35
36         @Rule
37         public final ExpectedException expectedException = ExpectedException.none();
38
39         protected final Template template = new Template();
40         protected final WebInterface webInterface = mock(WebInterface.class, RETURNS_DEEP_STUBS);
41         protected final Core core = webInterface.getCore();
42
43         protected final Sone currentSone = mock(Sone.class);
44
45         protected final TemplateContext templateContext = new TemplateContext();
46         protected final HTTPRequest httpRequest = mock(HTTPRequest.class);
47         protected final FreenetRequest freenetRequest = mock(FreenetRequest.class);
48         protected final ToadletContext toadletContext = mock(ToadletContext.class);
49
50
51         @Before
52         public final void setupFreenetRequest() {
53                 when(freenetRequest.getToadletContext()).thenReturn(toadletContext);
54                 when(freenetRequest.getHttpRequest()).thenReturn(httpRequest);
55         }
56
57         @Before
58         public final void setupCore() {
59                 UpdateChecker updateChecker = mock(UpdateChecker.class);
60                 when(webInterface.getCore().getUpdateChecker()).thenReturn(updateChecker);
61         }
62
63         @Before
64         public final void setupWebInterface() {
65                 when(webInterface.getCurrentSone(toadletContext)).thenReturn(currentSone);
66                 when(webInterface.getCurrentSone(eq(toadletContext), anyBoolean())).thenReturn(currentSone);
67                 when(webInterface.getNotifications(currentSone)).thenReturn(new ArrayList<Notification>());
68         }
69
70         protected void request(String uri, Method method) {
71                 try {
72                         when(freenetRequest.getUri()).thenReturn(new URI(uri));
73                 } catch (URISyntaxException e) {
74                         throw new RuntimeException(e);
75                 }
76                 when(freenetRequest.getMethod()).thenReturn(method);
77         }
78
79 }