Add test for CreatePostPage
[Sone.git] / src / test / java / net / pterodactylus / sone / web / CreateAlbumPageTest.java
1 package net.pterodactylus.sone.web;
2
3 import static net.pterodactylus.sone.web.WebTestUtils.redirectsTo;
4 import static net.pterodactylus.util.web.Method.POST;
5 import static org.hamcrest.MatcherAssert.assertThat;
6 import static org.hamcrest.Matchers.is;
7 import static org.mockito.Answers.RETURNS_DEEP_STUBS;
8 import static org.mockito.Answers.RETURNS_SELF;
9 import static org.mockito.Mockito.mock;
10 import static org.mockito.Mockito.verify;
11 import static org.mockito.Mockito.when;
12
13 import net.pterodactylus.sone.data.Album;
14 import net.pterodactylus.sone.data.Album.Modifier;
15 import net.pterodactylus.sone.data.Album.Modifier.AlbumTitleMustNotBeEmpty;
16 import net.pterodactylus.sone.test.Dirty;
17
18 import org.junit.Test;
19
20 /**
21  * Unit test for {@link CreateAlbumPage}.
22  *
23  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
24  */
25 public class CreateAlbumPageTest extends WebPageTest {
26
27         private final CreateAlbumPage page = new CreateAlbumPage(template, webInterface);
28
29         @Test
30         public void pageReturnsCorrectPath() {
31                 assertThat(page.getPath(), is("createAlbum.html"));
32         }
33
34         @Test
35         public void getRequestShowsTemplate() throws Exception {
36                 page.processTemplate(freenetRequest, templateContext);
37         }
38
39         @Test
40         public void missingNameResultsInAttributeSetInTemplateContext() throws Exception {
41                 request("", POST);
42                 page.processTemplate(freenetRequest, templateContext);
43                 assertThat(templateContext.get("nameMissing"), is((Object) true));
44         }
45
46         @Test
47         public void titleAndDescriptionAreSetCorrectlyOnTheAlbum() throws Exception {
48                 request("", POST);
49                 Album parentAlbum = createAlbum("parent-id");
50                 when(core.getAlbum("parent-id")).thenReturn(parentAlbum);
51                 Album newAlbum = createAlbum("album-id");
52                 when(core.createAlbum(currentSone, parentAlbum)).thenReturn(newAlbum);
53                 addHttpRequestParameter("name", "new name");
54                 addHttpRequestParameter("description", "new description");
55                 addHttpRequestParameter("parent", "parent-id");
56                 expectedException.expect(redirectsTo("imageBrowser.html?album=album-id"));
57                 try {
58                         page.processTemplate(freenetRequest, templateContext);
59                 } finally {
60                         verify(newAlbum).modify();
61                         verify(newAlbum.modify()).setTitle("new name");
62                         verify(newAlbum.modify()).setDescription("new description");
63                         verify(newAlbum.modify()).update();
64                 }
65         }
66
67         private Album createAlbum(String albumId) {
68                 Album newAlbum = mock(Album.class, RETURNS_DEEP_STUBS);
69                 when(newAlbum.getId()).thenReturn(albumId);
70                 Modifier albumModifier = mock(Modifier.class, RETURNS_SELF);
71                 when(newAlbum.modify()).thenReturn(albumModifier);
72                 when(albumModifier.update()).thenReturn(newAlbum);
73                 return newAlbum;
74         }
75
76         @Test
77         public void rootAlbumIsUsedIfNoParentIsSpecified() throws Exception {
78                 request("", POST);
79                 Album parentAlbum = createAlbum("root-id");
80                 when(currentSone.getRootAlbum()).thenReturn(parentAlbum);
81                 Album newAlbum = createAlbum("album-id");
82                 when(core.createAlbum(currentSone, parentAlbum)).thenReturn(newAlbum);
83                 addHttpRequestParameter("name", "new name");
84                 addHttpRequestParameter("description", "new description");
85                 expectedException.expect(redirectsTo("imageBrowser.html?album=album-id"));
86                 page.processTemplate(freenetRequest, templateContext);
87         }
88
89         @Test
90         @Dirty("that exception can never happen")
91         public void emptyAlbumTitleRedirectsToErrorPage() throws Exception {
92                 request("", POST);
93                 Album parentAlbum = createAlbum("root-id");
94                 when(currentSone.getRootAlbum()).thenReturn(parentAlbum);
95                 Album newAlbum = createAlbum("album-id");
96                 when(core.createAlbum(currentSone, parentAlbum)).thenReturn(newAlbum);
97                 when(newAlbum.modify().update()).thenThrow(AlbumTitleMustNotBeEmpty.class);
98                 addHttpRequestParameter("name", "new name");
99                 addHttpRequestParameter("description", "new description");
100                 expectedException.expect(redirectsTo("emptyAlbumTitle.html"));
101                 page.processTemplate(freenetRequest, templateContext);
102         }
103
104 }