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