1 package net.pterodactylus.sone.web;
3 import static org.mockito.Matchers.any;
4 import static org.mockito.Matchers.anyInt;
5 import static org.mockito.Matchers.eq;
6 import static org.mockito.Mockito.mock;
7 import static org.mockito.Mockito.when;
11 import net.pterodactylus.sone.core.Core;
12 import net.pterodactylus.sone.core.UpdateChecker;
13 import net.pterodactylus.sone.data.Album;
14 import net.pterodactylus.sone.data.Sone;
15 import net.pterodactylus.sone.web.page.FreenetRequest;
16 import net.pterodactylus.sone.web.page.FreenetTemplatePage.RedirectException;
17 import net.pterodactylus.util.template.Template;
18 import net.pterodactylus.util.template.TemplateContext;
19 import net.pterodactylus.util.web.Method;
21 import freenet.clients.http.ToadletContext;
22 import freenet.support.api.HTTPRequest;
24 import org.hamcrest.Description;
25 import org.hamcrest.Matcher;
26 import org.hamcrest.TypeSafeDiagnosingMatcher;
27 import org.junit.Before;
28 import org.junit.Rule;
29 import org.junit.Test;
30 import org.junit.rules.ExpectedException;
33 * Unit test for {@link UploadImagePageTest}.
35 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
37 public class UploadImagePageTest {
40 public final ExpectedException expectedException = ExpectedException.none();
42 private final Template template = new Template();
43 private final WebInterface webInterface = mock(WebInterface.class);
44 private final UploadImagePage uploadImagePage = new UploadImagePage(template, webInterface);
46 private final TemplateContext templateContext = new TemplateContext();
47 private final HTTPRequest httpRequest = mock(HTTPRequest.class);
48 private final ToadletContext toadletContext = mock(ToadletContext.class);
49 private final Core core = mock(Core.class);
50 private final Sone currentSone = mock(Sone.class);
51 private final Album parentAlbum = mock(Album.class);
54 public void setupWebInterface() {
55 UpdateChecker updateChecker = mock(UpdateChecker.class);
56 when(core.getUpdateChecker()).thenReturn(updateChecker);
57 when(webInterface.getCore()).thenReturn(core);
58 when(webInterface.getCurrentSone(any(ToadletContext.class))).thenReturn(currentSone);
62 public void setupParentAlbum() {
63 when(core.getAlbum("parent-id")).thenReturn(parentAlbum);
64 when(parentAlbum.getSone()).thenReturn(currentSone);
68 public void uploadingAnImageWithoutTitleRedirectsToEmptyImageTitlePage() throws Exception {
69 FreenetRequest request = new FreenetRequest(new URI(""), Method.POST, httpRequest, toadletContext);
70 when(httpRequest.getPartAsStringFailsafe(eq("parent"), anyInt())).thenReturn("parent-id");
71 when(httpRequest.getPartAsStringFailsafe(eq("title"), anyInt())).thenReturn(" ");
72 expectedException.expect(redirectsTo("emptyImageTitle.html"));
73 uploadImagePage.processTemplate(request, templateContext);
76 private Matcher<RedirectException> redirectsTo(final String page) {
77 return new TypeSafeDiagnosingMatcher<RedirectException>() {
79 protected boolean matchesSafely(RedirectException exception, Description mismatchDescription) {
80 if (!exception.getTarget().equals(page)) {
81 mismatchDescription.appendText("target is ").appendValue(exception.getTarget());
88 public void describeTo(Description description) {
89 description.appendText("target is ").appendValue(page);