Don’t allow null for any attribute of a LinkPart
[Sone.git] / src / test / java / net / pterodactylus / sone / web / UploadImagePageTest.java
1 package net.pterodactylus.sone.web;
2
3 import static net.pterodactylus.sone.web.WebTestUtils.redirectsTo;
4 import static org.mockito.Matchers.any;
5 import static org.mockito.Matchers.anyInt;
6 import static org.mockito.Matchers.eq;
7 import static org.mockito.Mockito.mock;
8 import static org.mockito.Mockito.when;
9
10 import java.net.URI;
11
12 import net.pterodactylus.sone.core.Core;
13 import net.pterodactylus.sone.core.UpdateChecker;
14 import net.pterodactylus.sone.data.Album;
15 import net.pterodactylus.sone.data.Sone;
16 import net.pterodactylus.sone.web.page.FreenetRequest;
17 import net.pterodactylus.util.template.Template;
18 import net.pterodactylus.util.template.TemplateContext;
19 import net.pterodactylus.util.web.Method;
20
21 import freenet.clients.http.ToadletContext;
22 import freenet.support.api.HTTPRequest;
23
24 import org.junit.Before;
25 import org.junit.Rule;
26 import org.junit.Test;
27 import org.junit.rules.ExpectedException;
28
29 /**
30  * Unit test for {@link UploadImagePageTest}.
31  *
32  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
33  */
34 public class UploadImagePageTest {
35
36         @Rule
37         public final ExpectedException expectedException = ExpectedException.none();
38
39         private final Template template = new Template();
40         private final WebInterface webInterface = mock(WebInterface.class);
41         private final UploadImagePage uploadImagePage = new UploadImagePage(template, webInterface);
42
43         private final TemplateContext templateContext = new TemplateContext();
44         private final HTTPRequest httpRequest = mock(HTTPRequest.class);
45         private final ToadletContext toadletContext = mock(ToadletContext.class);
46         private final Core core = mock(Core.class);
47         private final Sone currentSone = mock(Sone.class);
48         private final Album parentAlbum = mock(Album.class);
49
50         @Before
51         public void setupWebInterface() {
52                 UpdateChecker updateChecker = mock(UpdateChecker.class);
53                 when(core.getUpdateChecker()).thenReturn(updateChecker);
54                 when(webInterface.getCore()).thenReturn(core);
55                 when(webInterface.getCurrentSone(any(ToadletContext.class))).thenReturn(currentSone);
56         }
57
58         @Before
59         public void setupParentAlbum() {
60                 when(core.getAlbum("parent-id")).thenReturn(parentAlbum);
61                 when(parentAlbum.getSone()).thenReturn(currentSone);
62         }
63
64         @Test
65         public void uploadingAnImageWithoutTitleRedirectsToEmptyImageTitlePage() throws Exception {
66                 FreenetRequest request = new FreenetRequest(new URI(""), Method.POST, httpRequest, toadletContext);
67                 when(httpRequest.getPartAsStringFailsafe(eq("parent"), anyInt())).thenReturn("parent-id");
68                 when(httpRequest.getPartAsStringFailsafe(eq("title"), anyInt())).thenReturn("  ");
69                 expectedException.expect(redirectsTo("emptyImageTitle.html"));
70                 uploadImagePage.processTemplate(request, templateContext);
71         }
72
73 }