X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;ds=sidebyside;f=src%2Ftest%2Fkotlin%2Fnet%2Fpterodactylus%2Fsone%2Fweb%2Fajax%2FJsonPageTest.kt;h=d5314c5ae27862b9992ffea8a9fb45711a8f33bd;hb=c3407a682f9f81b27d0354e59fe9e3d3d6dffcd6;hp=f3558fa94fe229eedef53000be34205916bbf7f5;hpb=e5224bcc64cbbe5b0396984554d76dc3a5960e80;p=Sone.git diff --git a/src/test/kotlin/net/pterodactylus/sone/web/ajax/JsonPageTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/ajax/JsonPageTest.kt index f3558fa..d5314c5 100644 --- a/src/test/kotlin/net/pterodactylus/sone/web/ajax/JsonPageTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/web/ajax/JsonPageTest.kt @@ -1,11 +1,15 @@ package net.pterodactylus.sone.web.ajax +import com.google.common.eventbus.EventBus import freenet.clients.http.ToadletContext import freenet.support.SimpleReadOnlyArrayBucket import freenet.support.api.HTTPRequest import net.pterodactylus.sone.core.Core import net.pterodactylus.sone.core.ElementLoader import net.pterodactylus.sone.core.LinkedElement +import net.pterodactylus.sone.core.Preferences +import net.pterodactylus.sone.data.Album +import net.pterodactylus.sone.data.Image import net.pterodactylus.sone.data.Post import net.pterodactylus.sone.data.PostReply import net.pterodactylus.sone.data.Profile @@ -25,6 +29,7 @@ import org.hamcrest.MatcherAssert.assertThat import org.hamcrest.Matchers.equalTo import org.junit.Before import org.junit.Test +import org.mockito.ArgumentMatchers.any import org.mockito.ArgumentMatchers.anyBoolean import org.mockito.ArgumentMatchers.anyInt import org.mockito.ArgumentMatchers.anyString @@ -44,9 +49,11 @@ abstract class JsonPageTest( protected val webInterface = mock() protected val core = mock() + protected val eventBus = mock() + protected val preferences = Preferences(eventBus) protected val elementLoader = mock() protected open val page: JsonPage by lazy { pageSupplier(webInterface) } - protected val json by lazy { page.createJsonObject(freenetRequest)!! } + protected val json by lazy { page.createJsonObject(freenetRequest) } protected val toadletContext = mock() protected val freenetRequest = mock() @@ -60,11 +67,15 @@ abstract class JsonPageTest( private val localSones = mutableMapOf() private val remoteSones = mutableMapOf() private val posts = mutableMapOf() + private val postLikes = mutableMapOf>() private val newPosts = mutableMapOf() private val replies = mutableMapOf() + private val replyLikes = mutableMapOf>() private val newReplies = mutableMapOf() private val linkedElements = mutableMapOf() private val notifications = mutableMapOf() + private val albums = mutableMapOf() + private val images = mutableMapOf() @Before fun setupWebInterface() { @@ -80,10 +91,16 @@ abstract class JsonPageTest( @Before fun setupCore() { + whenever(core.preferences).thenReturn(preferences) whenever(core.getSone(anyString())).thenAnswer { (localSones + remoteSones)[it.getArgument(0)].asOptional() } whenever(core.getLocalSone(anyString())).thenAnswer { localSones[it[0]] } whenever(core.getPost(anyString())).thenAnswer { (posts + newPosts)[it[0]].asOptional() } + whenever(core.getLikes(any())).then { postLikes[it[0]] ?: emptySet() } + whenever(core.getLikes(any())).then { replyLikes[it[0]] ?: emptySet() } whenever(core.getPostReply(anyString())).then { replies[it[0]].asOptional() } + whenever(core.getAlbum(anyString())).then { albums[it[0]] } + whenever(core.getImage(anyString())).then { images[it[0]] } + whenever(core.getImage(anyString(), anyBoolean())).then { images[it[0]] } } @Before @@ -158,16 +175,24 @@ abstract class JsonPageTest( notifications[notificationId ?: notification.id] = notification } - protected fun addSone(sone: Sone) { - remoteSones += sone.id to sone + protected fun addSone(sone: Sone, soneId: String? = null) { + remoteSones += (soneId ?: sone.id) to sone } protected fun addLocalSone(id: String, sone: Sone) { localSones += id to sone } - protected fun addPost(id: String, post: Post) { - posts[id] = post + protected fun addPost(post: Post, id: String? = null) { + posts[id ?: post.id] = post + } + + protected fun addLikes(post: Post, vararg sones: Sone) { + postLikes[post] = setOf(*sones) + } + + protected fun addLikes(reply: PostReply, vararg sones: Sone) { + replyLikes[reply] = setOf(*sones) } protected fun addNewPost(id: String, soneId: String, time: Long, recipientId: String? = null) = @@ -179,8 +204,8 @@ abstract class JsonPageTest( whenever(this.recipientId).thenReturn(recipientId.asOptional()) }.also { newPosts[id] = it } - protected fun addReply(id: String, reply: PostReply) { - replies[id] = reply + protected fun addReply(reply: PostReply, id: String? = null) { + replies[id ?: reply.id] = reply } protected fun addNewReply(id: String, soneId: String, postId: String, postSoneId: String) { @@ -201,6 +226,14 @@ abstract class JsonPageTest( linkedElements[link] = LinkedElement(link, failed, loading) } + protected fun addAlbum(album: Album, albumId: String? = null) { + albums[albumId ?: album.id] = album + } + + protected fun addImage(image: Image, imageId: String? = null) { + images[imageId ?: image.id] = image + } + @Test fun `page returns correct path`() { assertThat(page.path, equalTo(expectedPath))