From 108ca51ec6cf3dc90917ce763beeb16f03d2cd25 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Sun, 27 Nov 2016 01:54:19 +0100 Subject: [PATCH] Add unit test for image browser page --- .../net/pterodactylus/sone/web/WebPageTest.java | 19 +++- .../pterodactylus/sone/web/ImageBrowserPageTest.kt | 104 +++++++++++++++++++++ 2 files changed, 121 insertions(+), 2 deletions(-) create mode 100644 src/test/kotlin/net/pterodactylus/sone/web/ImageBrowserPageTest.kt diff --git a/src/test/java/net/pterodactylus/sone/web/WebPageTest.java b/src/test/java/net/pterodactylus/sone/web/WebPageTest.java index 4aef917..b8b7fa6 100644 --- a/src/test/java/net/pterodactylus/sone/web/WebPageTest.java +++ b/src/test/java/net/pterodactylus/sone/web/WebPageTest.java @@ -15,6 +15,7 @@ import java.io.PipedOutputStream; import java.net.URI; import java.net.URISyntaxException; import java.util.ArrayList; +import java.util.Collection; import java.util.HashMap; import java.util.HashSet; import java.util.List; @@ -49,6 +50,7 @@ import com.google.common.io.ByteStreams; import org.junit.Before; import org.junit.Rule; import org.junit.rules.ExpectedException; +import org.mockito.ArgumentMatchers; import org.mockito.invocation.InvocationOnMock; import org.mockito.stubbing.Answer; @@ -80,6 +82,7 @@ public abstract class WebPageTest { protected final ToadletContext toadletContext = mock(ToadletContext.class); private final Set ownIdentities = new HashSet<>(); + private final Map sones = new HashMap<>(); private final List localSones = new ArrayList<>(); protected WebPageTest() { @@ -109,7 +112,7 @@ public abstract class WebPageTest { return requestParameters.containsKey(parameter) ? requestParameters.get(parameter) : ""; } }); - when(httpRequest.getParam(anyString(), anyString())).thenAnswer(new Answer() { + when(httpRequest.getParam(anyString(), ArgumentMatchers.any())).thenAnswer(new Answer() { @Override public String answer(InvocationOnMock invocation) throws Throwable { String parameter = invocation.getArgument(0); @@ -144,6 +147,18 @@ public abstract class WebPageTest { when(core.getLocalSone(anyString())).thenReturn(null); when(core.getLocalSones()).thenReturn(localSones); when(core.getSone(anyString())).thenReturn(Optional.absent()); + when(core.getSones()).thenAnswer(new Answer>() { + @Override + public Collection answer(InvocationOnMock invocation) throws Throwable { + return sones.values(); + } + }); + when(core.getSone(anyString())).thenAnswer(new Answer>() { + @Override + public Optional answer(InvocationOnMock invocation) throws Throwable { + return Optional.fromNullable(sones.get(invocation.getArgument(0))); + } + }); when(core.getPost(anyString())).thenReturn(Optional.absent()); when(core.getAlbum(anyString())).thenReturn(null); when(core.getImage(anyString())).thenReturn(null); @@ -196,7 +211,7 @@ public abstract class WebPageTest { } protected void addSone(String soneId, Sone sone) { - when(core.getSone(eq(soneId))).thenReturn(Optional.fromNullable(sone)); + sones.put(soneId, sone); } protected void addLocalSone(String soneId, Sone sone) { diff --git a/src/test/kotlin/net/pterodactylus/sone/web/ImageBrowserPageTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/ImageBrowserPageTest.kt new file mode 100644 index 0000000..426eaf4 --- /dev/null +++ b/src/test/kotlin/net/pterodactylus/sone/web/ImageBrowserPageTest.kt @@ -0,0 +1,104 @@ +package net.pterodactylus.sone.web + +import net.pterodactylus.sone.data.Album +import net.pterodactylus.sone.data.Image +import net.pterodactylus.sone.data.Sone +import net.pterodactylus.sone.test.mock +import net.pterodactylus.sone.test.whenever +import net.pterodactylus.util.web.Method.GET +import org.hamcrest.MatcherAssert.assertThat +import org.hamcrest.Matchers.contains +import org.hamcrest.Matchers.equalTo +import org.junit.Test + +/** + * Unit test for [ImageBrowserPage]. + */ +class ImageBrowserPageTest : WebPageTest() { + + private val page = ImageBrowserPage(template, webInterface) + + @Test + fun `get request with album sets album and page in template context`() { + request("", GET) + val album = mock() + addAlbum("album-id", album) + addHttpRequestParameter("album", "album-id") + addHttpRequestParameter("page", "5") + page.handleRequest(freenetRequest, templateContext) + assertThat(templateContext["albumRequested"], equalTo(true)) + assertThat(templateContext["album"], equalTo(album)) + assertThat(templateContext["page"], equalTo("5")) + } + + @Test + fun `get request with image sets image in template context`() { + request("", GET) + val image = mock() + addImage("image-id", image) + addHttpRequestParameter("image", "image-id") + page.handleRequest(freenetRequest, templateContext) + assertThat(templateContext["imageRequested"], equalTo(true)) + assertThat(templateContext["image"], equalTo(image)) + } + + @Test + fun `get request with sone sets sone in template context`() { + request("", GET) + val sone = mock() + addSone("sone-id", sone) + addHttpRequestParameter("sone", "sone-id") + page.handleRequest(freenetRequest, templateContext) + assertThat(templateContext["soneRequested"], equalTo(true)) + assertThat(templateContext["sone"], equalTo(sone)) + } + + @Test + fun `get request with mode of gallery sets albums and page in template context`() { + request("", GET) + val firstSone = createSone("first album", "second album") + addSone("sone1", firstSone) + val secondSone = createSone("third album", "fourth album") + addSone("sone2", secondSone) + addHttpRequestParameter("mode", "gallery") + page.handleRequest(freenetRequest, templateContext) + assertThat(templateContext["galleryRequested"], equalTo(true)) + @Suppress("UNCHECKED_CAST") + assertThat(templateContext["albums"] as Iterable, contains( + firstSone.rootAlbum.albums[0], + secondSone.rootAlbum.albums[1], + firstSone.rootAlbum.albums[1], + secondSone.rootAlbum.albums[0] + )) + } + + private fun createSone(firstAlbumTitle: String, secondAlbumTitle: String): Sone { + return mock().apply { + val rootAlbum = mock() + val firstAlbum = mock() + val firstImage = mock().run { whenever(isInserted).thenReturn(true); this } + whenever(firstAlbum.images).thenReturn(listOf(firstImage)) + val secondAlbum = mock() + val secondImage = mock().run { whenever(isInserted).thenReturn(true); this } + whenever(secondAlbum.images).thenReturn(listOf(secondImage)) + whenever(firstAlbum.title).thenReturn(firstAlbumTitle) + whenever(secondAlbum.title).thenReturn(secondAlbumTitle) + whenever(rootAlbum.albums).thenReturn(listOf(firstAlbum, secondAlbum)) + whenever(this.rootAlbum).thenReturn(rootAlbum) + } + } + + @Test + fun `requesting nothing will show the albums of the current sone`() { + request("", GET) + page.handleRequest(freenetRequest, templateContext) + assertThat(templateContext["soneRequested"], equalTo(true)) + assertThat(templateContext["sone"], equalTo(currentSone)) + } + + @Test + fun `page is link-excepted`() { + assertThat(page.isLinkExcepted(null), equalTo(true)) + } + +} -- 2.7.4