Add unit test for image browser page
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sun, 27 Nov 2016 00:54:19 +0000 (01:54 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sun, 27 Nov 2016 00:54:19 +0000 (01:54 +0100)
src/test/java/net/pterodactylus/sone/web/WebPageTest.java
src/test/kotlin/net/pterodactylus/sone/web/ImageBrowserPageTest.kt [new file with mode: 0644]

index 4aef917..b8b7fa6 100644 (file)
@@ -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<OwnIdentity> ownIdentities = new HashSet<>();
+       private final Map<String, Sone> sones = new HashMap<>();
        private final List<Sone> 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<String>() {
+               when(httpRequest.getParam(anyString(), ArgumentMatchers.<String>any())).thenAnswer(new Answer<String>() {
                        @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.<Sone>absent());
+               when(core.getSones()).thenAnswer(new Answer<Collection<Sone>>() {
+                       @Override
+                       public Collection<Sone> answer(InvocationOnMock invocation) throws Throwable {
+                               return sones.values();
+                       }
+               });
+               when(core.getSone(anyString())).thenAnswer(new Answer<Optional<Sone>>() {
+                       @Override
+                       public Optional<Sone> answer(InvocationOnMock invocation) throws Throwable {
+                               return Optional.fromNullable(sones.get(invocation.getArgument(0)));
+                       }
+               });
                when(core.getPost(anyString())).thenReturn(Optional.<Post>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 (file)
index 0000000..426eaf4
--- /dev/null
@@ -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<Album>()
+               addAlbum("album-id", album)
+               addHttpRequestParameter("album", "album-id")
+               addHttpRequestParameter("page", "5")
+               page.handleRequest(freenetRequest, templateContext)
+               assertThat(templateContext["albumRequested"], equalTo<Any>(true))
+               assertThat(templateContext["album"], equalTo<Any>(album))
+               assertThat(templateContext["page"], equalTo<Any>("5"))
+       }
+
+       @Test
+       fun `get request with image sets image in template context`() {
+               request("", GET)
+               val image = mock<Image>()
+               addImage("image-id", image)
+               addHttpRequestParameter("image", "image-id")
+               page.handleRequest(freenetRequest, templateContext)
+               assertThat(templateContext["imageRequested"], equalTo<Any>(true))
+               assertThat(templateContext["image"], equalTo<Any>(image))
+       }
+
+       @Test
+       fun `get request with sone sets sone in template context`() {
+               request("", GET)
+               val sone = mock<Sone>()
+               addSone("sone-id", sone)
+               addHttpRequestParameter("sone", "sone-id")
+               page.handleRequest(freenetRequest, templateContext)
+               assertThat(templateContext["soneRequested"], equalTo<Any>(true))
+               assertThat(templateContext["sone"], equalTo<Any>(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<Any>(true))
+               @Suppress("UNCHECKED_CAST")
+               assertThat(templateContext["albums"] as Iterable<Album>, 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<Sone>().apply {
+                       val rootAlbum = mock<Album>()
+                       val firstAlbum = mock<Album>()
+                       val firstImage = mock<Image>().run { whenever(isInserted).thenReturn(true); this }
+                       whenever(firstAlbum.images).thenReturn(listOf(firstImage))
+                       val secondAlbum = mock<Album>()
+                       val secondImage = mock<Image>().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<Any>(true))
+               assertThat(templateContext["sone"], equalTo<Any>(currentSone))
+       }
+
+       @Test
+       fun `page is link-excepted`() {
+           assertThat(page.isLinkExcepted(null), equalTo(true))
+       }
+
+}