Merge branch 'release-0.9.6'
[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 import java.util.HashSet;
15 import java.util.List;
16 import java.util.Set;
17
18 import net.pterodactylus.sone.core.Core;
19 import net.pterodactylus.sone.core.UpdateChecker;
20 import net.pterodactylus.sone.data.Post;
21 import net.pterodactylus.sone.data.Sone;
22 import net.pterodactylus.sone.freenet.wot.OwnIdentity;
23 import net.pterodactylus.sone.web.page.FreenetRequest;
24 import net.pterodactylus.util.notify.Notification;
25 import net.pterodactylus.util.template.Template;
26 import net.pterodactylus.util.template.TemplateContext;
27 import net.pterodactylus.util.web.Method;
28
29 import freenet.clients.http.ToadletContext;
30 import freenet.support.api.HTTPRequest;
31
32 import com.google.common.base.Optional;
33 import org.junit.Before;
34 import org.junit.Rule;
35 import org.junit.rules.ExpectedException;
36 import org.mockito.invocation.InvocationOnMock;
37 import org.mockito.stubbing.Answer;
38
39 /**
40  * Base class for web page tests.
41  *
42  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
43  */
44 public abstract class WebPageTest {
45
46         @Rule
47         public final ExpectedException expectedException = ExpectedException.none();
48
49         protected final Template template = new Template();
50         protected final WebInterface webInterface = mock(WebInterface.class, RETURNS_DEEP_STUBS);
51         protected final Core core = webInterface.getCore();
52
53         protected final Sone currentSone = mock(Sone.class);
54
55         protected final TemplateContext templateContext = new TemplateContext();
56         protected final HTTPRequest httpRequest = mock(HTTPRequest.class);
57         protected final FreenetRequest freenetRequest = mock(FreenetRequest.class);
58         protected final ToadletContext toadletContext = mock(ToadletContext.class);
59
60         private final Set<OwnIdentity> ownIdentities = new HashSet<>();
61         private final List<Sone> localSones = new ArrayList<>();
62
63         @Before
64         public final void setupFreenetRequest() {
65                 when(freenetRequest.getToadletContext()).thenReturn(toadletContext);
66                 when(freenetRequest.getHttpRequest()).thenReturn(httpRequest);
67                 when(httpRequest.getPartAsStringFailsafe(anyString(), anyInt())).thenAnswer(new Answer<String>() {
68                         @Override
69                         public String answer(InvocationOnMock invocation) throws Throwable {
70                                 return "";
71                         }
72                 });
73         }
74
75         @Before
76         public final void setupCore() {
77                 UpdateChecker updateChecker = mock(UpdateChecker.class);
78                 when(core.getUpdateChecker()).thenReturn(updateChecker);
79                 when(core.getLocalSone(anyString())).thenReturn(null);
80                 when(core.getLocalSones()).thenReturn(localSones);
81                 when(core.getSone(anyString())).thenReturn(Optional.<Sone>absent());
82                 when(core.getPost(anyString())).thenReturn(Optional.<Post>absent());
83         }
84
85         @Before
86         public final void setupIdentityManager() {
87                 when(core.getIdentityManager().getAllOwnIdentities()).thenReturn(ownIdentities);
88         }
89
90         @Before
91         public final void setupWebInterface() {
92                 when(webInterface.getCurrentSone(toadletContext)).thenReturn(currentSone);
93                 when(webInterface.getCurrentSone(eq(toadletContext), anyBoolean())).thenReturn(currentSone);
94                 when(webInterface.getNotifications(currentSone)).thenReturn(new ArrayList<Notification>());
95         }
96
97         protected void unsetCurrentSone() {
98                 when(webInterface.getCurrentSone(toadletContext)).thenReturn(null);
99                 when(webInterface.getCurrentSone(eq(toadletContext), anyBoolean())).thenReturn(null);
100         }
101
102         protected void request(String uri, Method method) {
103                 try {
104                         when(freenetRequest.getUri()).thenReturn(new URI(uri));
105                 } catch (URISyntaxException e) {
106                         throw new RuntimeException(e);
107                 }
108                 when(freenetRequest.getMethod()).thenReturn(method);
109         }
110
111         protected void addHttpRequestParameter(String name, final String value) {
112                 when(httpRequest.getPartAsStringFailsafe(eq(name), anyInt())).thenAnswer(new Answer<String>() {
113                         @Override
114                         public String answer(InvocationOnMock invocation) throws Throwable {
115                                 int maxLength = invocation.getArgument(1);
116                                 return value.substring(0, Math.min(maxLength, value.length()));
117                         }
118                 });
119         }
120
121         protected void addPost(String postId, Post post) {
122                 when(core.getPost(postId)).thenReturn(Optional.fromNullable(post));
123         }
124
125         protected void addSone(String soneId, Sone sone) {
126                 when(core.getSone(eq(soneId))).thenReturn(Optional.fromNullable(sone));
127         }
128
129         protected void addLocalSone(String soneId, Sone sone) {
130                 when(core.getLocalSone(eq(soneId))).thenReturn(sone);
131                 localSones.add(sone);
132         }
133
134         protected void addOwnIdentity(OwnIdentity ownIdentity) {
135                 ownIdentities.add(ownIdentity);
136         }
137
138 }