1 package net.pterodactylus.sone.web;
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;
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;
18 import org.junit.Test;
21 * Unit test for {@link CreateAlbumPage}.
23 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
25 public class CreateAlbumPageTest extends WebPageTest {
27 private final CreateAlbumPage page = new CreateAlbumPage(template, webInterface);
30 public void pageReturnsCorrectPath() {
31 assertThat(page.getPath(), is("createAlbum.html"));
35 public void getRequestShowsTemplate() throws Exception {
36 page.processTemplate(freenetRequest, templateContext);
40 public void missingNameResultsInAttributeSetInTemplateContext() throws Exception {
42 page.processTemplate(freenetRequest, templateContext);
43 assertThat(templateContext.get("nameMissing"), is((Object) true));
47 public void titleAndDescriptionAreSetCorrectlyOnTheAlbum() throws Exception {
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"));
58 page.processTemplate(freenetRequest, templateContext);
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();
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);
78 public void rootAlbumIsUsedIfNoParentIsSpecified() throws Exception {
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);
91 @Dirty("that exception can never happen")
92 public void emptyAlbumTitleRedirectsToErrorPage() throws Exception {
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);