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();
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);
77 public void rootAlbumIsUsedIfNoParentIsSpecified() throws Exception {
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);
90 @Dirty("that exception can never happen")
91 public void emptyAlbumTitleRedirectsToErrorPage() throws Exception {
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);