1 package net.pterodactylus.sone.web.ajax
3 import freenet.clients.http.ToadletContext
4 import freenet.support.api.HTTPRequest
5 import net.pterodactylus.sone.core.Core
6 import net.pterodactylus.sone.core.ElementLoader
7 import net.pterodactylus.sone.core.LinkedElement
8 import net.pterodactylus.sone.data.Post
9 import net.pterodactylus.sone.data.PostReply
10 import net.pterodactylus.sone.data.Sone
11 import net.pterodactylus.sone.data.Sone.SoneStatus
12 import net.pterodactylus.sone.data.Sone.SoneStatus.idle
13 import net.pterodactylus.sone.test.asOptional
14 import net.pterodactylus.sone.test.deepMock
15 import net.pterodactylus.sone.test.mock
16 import net.pterodactylus.sone.test.whenever
17 import net.pterodactylus.sone.web.WebInterface
18 import net.pterodactylus.sone.web.page.FreenetRequest
19 import net.pterodactylus.util.notify.Notification
20 import org.junit.Before
21 import org.mockito.ArgumentMatchers.anyString
24 * Base class for tests for any [JsonPage] implementations.
26 open class JsonPageTest {
28 protected val webInterface = mock<WebInterface>()
29 protected val core = mock<Core>()
30 protected val elementLoader = mock<ElementLoader>()
31 protected open lateinit var page: JsonPage
32 protected val json by lazy { page.createJsonObject(freenetRequest)!! }
34 protected val toadletContext = mock<ToadletContext>()
35 protected val freenetRequest = mock<FreenetRequest>()
36 protected val httpRequest = mock<HTTPRequest>()
37 protected val currentSone = deepMock<Sone>()
39 private val requestParameters = mutableMapOf<String, String>()
40 private val localSones = mutableMapOf<String, Sone>()
41 private val remoteSones = mutableMapOf<String, Sone>()
42 private val newPosts = mutableMapOf<String, Post>()
43 private val newReplies = mutableMapOf<String, PostReply>()
44 private val linkedElements = mutableMapOf<String, LinkedElement>()
45 private val notifications = mutableListOf<Notification>()
48 fun setupWebInterface() {
49 whenever(webInterface.getCurrentSoneCreatingSession(toadletContext)).thenReturn(currentSone)
50 whenever(webInterface.getCurrentSoneWithoutCreatingSession(toadletContext)).thenReturn(currentSone)
51 whenever(webInterface.core).thenReturn(core)
52 whenever(webInterface.getNotifications(currentSone)).thenAnswer { notifications }
53 whenever(webInterface.getNewPosts(currentSone)).thenAnswer { newPosts.values }
54 whenever(webInterface.getNewReplies(currentSone)).thenAnswer { newReplies.values }
59 whenever(core.getSone(anyString())).thenAnswer { (localSones + remoteSones)[it.getArgument(0)].asOptional() }
63 fun setupElementLoader() {
64 whenever(elementLoader.loadElement(anyString())).thenAnswer {
65 linkedElements[it.getArgument(0)] ?: LinkedElement(it.getArgument(0), loading = true)
70 fun setupCurrentSone() {
71 currentSone.mock("soneId", "Sone_Id", true, 1000, idle)
75 fun setupFreenetRequest() {
76 whenever(freenetRequest.toadletContext).thenReturn(toadletContext)
77 whenever(freenetRequest.httpRequest).thenReturn(httpRequest)
81 fun setupHttpRequest() {
82 whenever(httpRequest.getParam(anyString())).thenAnswer { requestParameters[it.getArgument(0)] ?: "" }
83 whenever(httpRequest.getParam(anyString(), anyString())).thenAnswer { requestParameters[it.getArgument(0)] ?: it.getArgument(1) }
86 protected fun Sone.mock(id: String, name: String, local: Boolean = false, time: Long, status: SoneStatus = idle) = apply {
87 whenever(this.id).thenReturn(id)
88 whenever(this.name).thenReturn(name)
89 whenever(isLocal).thenReturn(local)
90 whenever(this.time).thenReturn(time)
91 whenever(this.status).thenReturn(status)
94 protected fun unsetCurrentSone() {
95 whenever(webInterface.getCurrentSoneWithoutCreatingSession(toadletContext)).thenReturn(null)
96 whenever(webInterface.getCurrentSoneCreatingSession(toadletContext)).thenReturn(null)
99 protected fun addRequestParameter(key: String, value: String) {
100 requestParameters += key to value
103 protected fun addNotification(vararg notifications: Notification) {
104 this.notifications += notifications
107 protected fun addSone(sone: Sone) {
108 remoteSones += sone.id to sone
111 protected fun addNewPost(id: String, soneId: String, time: Long, recipientId: String? = null) {
112 newPosts[id] = mock<Post>().apply {
113 whenever(this.id).thenReturn(id)
114 val sone = mock<Sone>().apply { whenever(this.id).thenReturn(soneId) }
115 whenever(this.sone).thenReturn(sone)
116 whenever(this.time).thenReturn(time)
117 whenever(this.recipientId).thenReturn(recipientId.asOptional())
121 protected fun addNewReply(id: String, soneId: String, postId: String, postSoneId: String) {
122 newReplies[id] = mock<PostReply>().apply {
123 whenever(this.id).thenReturn(id)
124 val sone = mock<Sone>().apply { whenever(this.id).thenReturn(soneId) }
125 whenever(this.sone).thenReturn(sone)
126 val postSone = mock<Sone>().apply { whenever(this.id).thenReturn(postSoneId) }
127 val post = mock<Post>().apply {
128 whenever(this.sone).thenReturn(postSone)
130 whenever(this.post).thenReturn(post.asOptional())
131 whenever(this.postId).thenReturn(postId)
135 protected fun addLinkedElement(link: String, loading: Boolean, failed: Boolean) {
136 linkedElements[link] = LinkedElement(link, failed, loading)