Add unit test for edit profile 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.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.HashMap;
15 import java.util.HashSet;
16 import java.util.List;
17 import java.util.Map;
18 import java.util.Set;
19
20 import javax.annotation.Nonnull;
21
22 import net.pterodactylus.sone.core.Core;
23 import net.pterodactylus.sone.core.Preferences;
24 import net.pterodactylus.sone.core.UpdateChecker;
25 import net.pterodactylus.sone.data.Album;
26 import net.pterodactylus.sone.data.Image;
27 import net.pterodactylus.sone.data.Post;
28 import net.pterodactylus.sone.data.Sone;
29 import net.pterodactylus.sone.data.SoneOptions.DefaultSoneOptions;
30 import net.pterodactylus.sone.freenet.wot.OwnIdentity;
31 import net.pterodactylus.sone.web.page.FreenetRequest;
32 import net.pterodactylus.util.notify.Notification;
33 import net.pterodactylus.util.template.Template;
34 import net.pterodactylus.util.template.TemplateContext;
35 import net.pterodactylus.util.web.Method;
36
37 import freenet.clients.http.ToadletContext;
38 import freenet.support.api.HTTPRequest;
39
40 import com.google.common.base.Optional;
41 import com.google.common.eventbus.EventBus;
42 import org.junit.Before;
43 import org.junit.Rule;
44 import org.junit.rules.ExpectedException;
45 import org.mockito.invocation.InvocationOnMock;
46 import org.mockito.stubbing.Answer;
47
48 /**
49  * Base class for web page tests.
50  *
51  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
52  */
53 public abstract class WebPageTest {
54
55         @Rule
56         public final ExpectedException expectedException = ExpectedException.none();
57
58         protected final Template template = new Template();
59         protected final WebInterface webInterface = mock(WebInterface.class, RETURNS_DEEP_STUBS);
60         protected final EventBus eventBus = mock(EventBus.class);
61         protected final Core core = webInterface.getCore();
62
63         protected final Sone currentSone = mock(Sone.class);
64
65         protected final TemplateContext templateContext = new TemplateContext();
66         protected final HTTPRequest httpRequest = mock(HTTPRequest.class);
67         protected final Map<String, String> requestParameters = new HashMap<>();
68         protected final Map<String, String> requestHeaders = new HashMap<>();
69         protected final FreenetRequest freenetRequest = mock(FreenetRequest.class);
70         protected final ToadletContext toadletContext = mock(ToadletContext.class);
71
72         private final Set<OwnIdentity> ownIdentities = new HashSet<>();
73         private final List<Sone> localSones = new ArrayList<>();
74
75         @Before
76         public final void setupFreenetRequest() {
77                 when(freenetRequest.getToadletContext()).thenReturn(toadletContext);
78                 when(freenetRequest.getHttpRequest()).thenReturn(httpRequest);
79                 when(httpRequest.getPartAsStringFailsafe(anyString(), anyInt())).thenAnswer(new Answer<String>() {
80                         @Override
81                         public String answer(InvocationOnMock invocation) throws Throwable {
82                                 String parameter = invocation.getArgument(0);
83                                 int maxLength = invocation.getArgument(1);
84                                 return requestParameters.containsKey(parameter) ? requestParameters.get(parameter).substring(0, Math.min(maxLength, requestParameters.get(parameter).length())) : "";
85                         }
86                 });
87                 when(httpRequest.getParam(anyString())).thenAnswer(new Answer<String>() {
88                         @Override
89                         public String answer(InvocationOnMock invocation) throws Throwable {
90                                 String parameter = invocation.getArgument(0);
91                                 return requestParameters.containsKey(parameter) ? requestParameters.get(parameter) : "";
92                         }
93                 });
94                 when(httpRequest.getParam(anyString(), anyString())).thenAnswer(new Answer<String>() {
95                         @Override
96                         public String answer(InvocationOnMock invocation) throws Throwable {
97                                 String parameter = invocation.getArgument(0);
98                                 return requestParameters.containsKey(parameter) ? requestParameters.get(parameter) : invocation.<String>getArgument(1);
99                         }
100                 });
101                 when(httpRequest.isPartSet(anyString())).thenAnswer(new Answer<Boolean>() {
102                         @Override
103                         public Boolean answer(InvocationOnMock invocation) throws Throwable {
104                                 return requestParameters.containsKey(invocation.<String>getArgument(0));
105                         }
106                 });
107                 when(httpRequest.getParts()).thenAnswer(new Answer<String[]>() {
108                         @Override
109                         public String[] answer(InvocationOnMock invocation) throws Throwable {
110                                 return requestParameters.keySet().toArray(new String[requestParameters.size()]);
111                         }
112                 });
113                 when(httpRequest.getHeader(anyString())).thenAnswer(new Answer<String>() {
114                         @Override
115                         public String answer(InvocationOnMock invocation) throws Throwable {
116                                 return requestHeaders.get(invocation.<String>getArgument(0).toLowerCase());
117                         }
118                 });
119         }
120
121         @Before
122         public final void setupCore() {
123                 UpdateChecker updateChecker = mock(UpdateChecker.class);
124                 when(core.getUpdateChecker()).thenReturn(updateChecker);
125                 when(core.getPreferences()).thenReturn(new Preferences(eventBus));
126                 when(core.getLocalSone(anyString())).thenReturn(null);
127                 when(core.getLocalSones()).thenReturn(localSones);
128                 when(core.getSone(anyString())).thenReturn(Optional.<Sone>absent());
129                 when(core.getPost(anyString())).thenReturn(Optional.<Post>absent());
130                 when(core.getAlbum(anyString())).thenReturn(null);
131                 when(core.getImage(anyString())).thenReturn(null);
132                 when(core.getImage(anyString(), anyBoolean())).thenReturn(null);
133         }
134
135         @Before
136         public final void setupIdentityManager() {
137                 when(core.getIdentityManager().getAllOwnIdentities()).thenReturn(ownIdentities);
138         }
139
140         @Before
141         public final void setupWebInterface() {
142                 when(webInterface.getCurrentSone(toadletContext)).thenReturn(currentSone);
143                 when(webInterface.getCurrentSone(eq(toadletContext), anyBoolean())).thenReturn(currentSone);
144                 when(webInterface.getNotification(anyString())).thenReturn(Optional.<Notification>absent());
145                 when(webInterface.getNotifications(currentSone)).thenReturn(new ArrayList<Notification>());
146         }
147
148         @Before
149         public void setupSone() {
150                 when(currentSone.getOptions()).thenReturn(new DefaultSoneOptions());
151         }
152
153         protected void unsetCurrentSone() {
154                 when(webInterface.getCurrentSone(toadletContext)).thenReturn(null);
155                 when(webInterface.getCurrentSone(eq(toadletContext), anyBoolean())).thenReturn(null);
156         }
157
158         protected void request(String uri, Method method) {
159                 try {
160                         when(freenetRequest.getUri()).thenReturn(new URI(uri));
161                 } catch (URISyntaxException e) {
162                         throw new RuntimeException(e);
163                 }
164                 when(freenetRequest.getMethod()).thenReturn(method);
165         }
166
167         protected void addHttpRequestHeader(@Nonnull String name, String value) {
168                 requestHeaders.put(name.toLowerCase(), value);
169         }
170
171         protected void addHttpRequestParameter(String name, final String value) {
172                 requestParameters.put(name, value);
173         }
174
175         protected void addPost(String postId, Post post) {
176                 when(core.getPost(postId)).thenReturn(Optional.fromNullable(post));
177         }
178
179         protected void addSone(String soneId, Sone sone) {
180                 when(core.getSone(eq(soneId))).thenReturn(Optional.fromNullable(sone));
181         }
182
183         protected void addLocalSone(String soneId, Sone sone) {
184                 when(core.getLocalSone(eq(soneId))).thenReturn(sone);
185                 localSones.add(sone);
186         }
187
188         protected void addOwnIdentity(OwnIdentity ownIdentity) {
189                 ownIdentities.add(ownIdentity);
190         }
191
192         protected void addAlbum(String albumId, Album album) {
193                 when(core.getAlbum(eq(albumId))).thenReturn(album);
194         }
195
196         protected void addImage(String imageId, Image image) {
197                 when(core.getImage(eq(imageId))).thenReturn(image);
198                 when(core.getImage(eq(imageId), anyBoolean())).thenReturn(image);
199         }
200
201         protected void addNotification(String notificationId, Notification notification) {
202                 when(webInterface.getNotification(eq(notificationId))).thenReturn(Optional.of(notification));
203         }
204
205 }