From 402543aa5755faa3c7cfc8a9ae55e16a00cf1d1e Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Thu, 13 Oct 2016 22:12:04 +0200 Subject: [PATCH] Add test for create album page --- .../sone/web/CreateAlbumPageTest.java | 104 +++++++++++++++++++++ 1 file changed, 104 insertions(+) create mode 100644 src/test/java/net/pterodactylus/sone/web/CreateAlbumPageTest.java diff --git a/src/test/java/net/pterodactylus/sone/web/CreateAlbumPageTest.java b/src/test/java/net/pterodactylus/sone/web/CreateAlbumPageTest.java new file mode 100644 index 0000000..79cedca --- /dev/null +++ b/src/test/java/net/pterodactylus/sone/web/CreateAlbumPageTest.java @@ -0,0 +1,104 @@ +package net.pterodactylus.sone.web; + +import static net.pterodactylus.sone.web.WebTestUtils.redirectsTo; +import static net.pterodactylus.util.web.Method.POST; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.is; +import static org.mockito.Answers.RETURNS_DEEP_STUBS; +import static org.mockito.Answers.RETURNS_SELF; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import net.pterodactylus.sone.data.Album; +import net.pterodactylus.sone.data.Album.Modifier; +import net.pterodactylus.sone.data.Album.Modifier.AlbumTitleMustNotBeEmpty; +import net.pterodactylus.sone.test.Dirty; + +import org.junit.Test; + +/** + * Unit test for {@link CreateAlbumPage}. + * + * @author David ‘Bombe’ Roden + */ +public class CreateAlbumPageTest extends WebPageTest { + + private final CreateAlbumPage page = new CreateAlbumPage(template, webInterface); + + @Test + public void pageReturnsCorrectPath() { + assertThat(page.getPath(), is("createAlbum.html")); + } + + @Test + public void getRequestShowsTemplate() throws Exception { + page.processTemplate(freenetRequest, templateContext); + } + + @Test + public void missingNameResultsInAttributeSetInTemplateContext() throws Exception { + request("", POST); + page.processTemplate(freenetRequest, templateContext); + assertThat(templateContext.get("nameMissing"), is((Object) true)); + } + + @Test + public void titleAndDescriptionAreSetCorrectlyOnTheAlbum() throws Exception { + request("", POST); + Album parentAlbum = createAlbum("parent-id"); + when(core.getAlbum("parent-id")).thenReturn(parentAlbum); + Album newAlbum = createAlbum("album-id"); + when(core.createAlbum(currentSone, parentAlbum)).thenReturn(newAlbum); + addHttpRequestParameter("name", "new name"); + addHttpRequestParameter("description", "new description"); + addHttpRequestParameter("parent", "parent-id"); + expectedException.expect(redirectsTo("imageBrowser.html?album=album-id")); + try { + page.processTemplate(freenetRequest, templateContext); + } finally { + verify(newAlbum).modify(); + verify(newAlbum.modify()).setTitle("new name"); + verify(newAlbum.modify()).setDescription("new description"); + verify(newAlbum.modify()).update(); + } + } + + private Album createAlbum(String albumId) { + Album newAlbum = mock(Album.class, RETURNS_DEEP_STUBS); + when(newAlbum.getId()).thenReturn(albumId); + Modifier albumModifier = mock(Modifier.class, RETURNS_SELF); + when(newAlbum.modify()).thenReturn(albumModifier); + when(albumModifier.update()).thenReturn(newAlbum); + return newAlbum; + } + + @Test + public void rootAlbumIsUsedIfNoParentIsSpecified() throws Exception { + request("", POST); + Album parentAlbum = createAlbum("root-id"); + when(currentSone.getRootAlbum()).thenReturn(parentAlbum); + Album newAlbum = createAlbum("album-id"); + when(core.createAlbum(currentSone, parentAlbum)).thenReturn(newAlbum); + addHttpRequestParameter("name", "new name"); + addHttpRequestParameter("description", "new description"); + expectedException.expect(redirectsTo("imageBrowser.html?album=album-id")); + page.processTemplate(freenetRequest, templateContext); + } + + @Test + @Dirty("that exception can never happen") + public void emptyAlbumTitleRedirectsToErrorPage() throws Exception { + request("", POST); + Album parentAlbum = createAlbum("root-id"); + when(currentSone.getRootAlbum()).thenReturn(parentAlbum); + Album newAlbum = createAlbum("album-id"); + when(core.createAlbum(currentSone, parentAlbum)).thenReturn(newAlbum); + when(newAlbum.modify().update()).thenThrow(AlbumTitleMustNotBeEmpty.class); + addHttpRequestParameter("name", "new name"); + addHttpRequestParameter("description", "new description"); + expectedException.expect(redirectsTo("emptyAlbumTitle.html")); + page.processTemplate(freenetRequest, templateContext); + } + +} -- 2.7.4