Merge branch 'release-0.9.6'
[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                         verify(core).touchConfiguration();
65                 }
66         }
67
68         private Album createAlbum(String albumId) {
69                 Album newAlbum = mock(Album.class, RETURNS_DEEP_STUBS);
70                 when(newAlbum.getId()).thenReturn(albumId);
71                 Modifier albumModifier = mock(Modifier.class, RETURNS_SELF);
72                 when(newAlbum.modify()).thenReturn(albumModifier);
73                 when(albumModifier.update()).thenReturn(newAlbum);
74                 return newAlbum;
75         }
76
77         @Test
78         public void rootAlbumIsUsedIfNoParentIsSpecified() throws Exception {
79                 request("", POST);
80                 Album parentAlbum = createAlbum("root-id");
81                 when(currentSone.getRootAlbum()).thenReturn(parentAlbum);
82                 Album newAlbum = createAlbum("album-id");
83                 when(core.createAlbum(currentSone, parentAlbum)).thenReturn(newAlbum);
84                 addHttpRequestParameter("name", "new name");
85                 addHttpRequestParameter("description", "new description");
86                 expectedException.expect(redirectsTo("imageBrowser.html?album=album-id"));
87                 page.processTemplate(freenetRequest, templateContext);
88         }
89
90         @Test
91         @Dirty("that exception can never happen")
92         public void emptyAlbumTitleRedirectsToErrorPage() throws Exception {
93                 request("", POST);
94                 Album parentAlbum = createAlbum("root-id");
95                 when(currentSone.getRootAlbum()).thenReturn(parentAlbum);
96                 Album newAlbum = createAlbum("album-id");
97                 when(core.createAlbum(currentSone, parentAlbum)).thenReturn(newAlbum);
98                 when(newAlbum.modify().update()).thenThrow(AlbumTitleMustNotBeEmpty.class);
99                 addHttpRequestParameter("name", "new name");
100                 addHttpRequestParameter("description", "new description");
101                 expectedException.expect(redirectsTo("emptyAlbumTitle.html"));
102                 page.processTemplate(freenetRequest, templateContext);
103         }
104
105 }