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.data.Post
7 import net.pterodactylus.sone.data.PostReply
8 import net.pterodactylus.sone.data.Sone
9 import net.pterodactylus.sone.data.Sone.SoneStatus
10 import net.pterodactylus.sone.data.Sone.SoneStatus.idle
11 import net.pterodactylus.sone.test.asOptional
12 import net.pterodactylus.sone.test.deepMock
13 import net.pterodactylus.sone.test.mock
14 import net.pterodactylus.sone.test.whenever
15 import net.pterodactylus.sone.web.WebInterface
16 import net.pterodactylus.sone.web.page.FreenetRequest
17 import net.pterodactylus.util.notify.Notification
18 import org.junit.Before
19 import org.mockito.ArgumentMatchers.anyString
22 * Base class for tests for any [JsonPage] implementations.
24 open class JsonPageTest {
26 protected val webInterface = mock<WebInterface>()
27 protected val core = mock<Core>()
28 protected open lateinit var page: JsonPage
29 protected val json by lazy { page.createJsonObject(freenetRequest)!! }
31 protected val toadletContext = mock<ToadletContext>()
32 protected val freenetRequest = mock<FreenetRequest>()
33 protected val httpRequest = mock<HTTPRequest>()
34 protected val currentSone = deepMock<Sone>()
36 private val requestParameters = mutableMapOf<String, String>()
37 private val localSones = mutableMapOf<String, Sone>()
38 private val remoteSones = mutableMapOf<String, Sone>()
39 private val newPosts = mutableMapOf<String, Post>()
40 private val newReplies = mutableMapOf<String, PostReply>()
41 private val notifications = mutableListOf<Notification>()
44 fun setupWebInterface() {
45 whenever(webInterface.getCurrentSoneCreatingSession(toadletContext)).thenReturn(currentSone)
46 whenever(webInterface.getCurrentSoneWithoutCreatingSession(toadletContext)).thenReturn(currentSone)
47 whenever(webInterface.core).thenReturn(core)
48 whenever(webInterface.getNotifications(currentSone)).thenAnswer { notifications }
49 whenever(webInterface.getNewPosts(currentSone)).thenAnswer { newPosts.values }
50 whenever(webInterface.getNewReplies(currentSone)).thenAnswer { newReplies.values }
55 whenever(core.getSone(anyString())).thenAnswer { (localSones + remoteSones)[it.getArgument(0)].asOptional() }
59 fun setupCurrentSone() {
60 currentSone.mock("soneId", "Sone_Id", true, 1000, idle)
64 fun setupFreenetRequest() {
65 whenever(freenetRequest.toadletContext).thenReturn(toadletContext)
66 whenever(freenetRequest.httpRequest).thenReturn(httpRequest)
70 fun setupHttpRequest() {
71 whenever(httpRequest.getParam(anyString())).thenAnswer { requestParameters[it.getArgument(0)] ?: "" }
74 protected fun Sone.mock(id: String, name: String, local: Boolean = false, time: Long, status: SoneStatus = idle) = apply {
75 whenever(this.id).thenReturn(id)
76 whenever(this.name).thenReturn(name)
77 whenever(isLocal).thenReturn(local)
78 whenever(this.time).thenReturn(time)
79 whenever(this.status).thenReturn(status)
82 protected fun unsetCurrentSone() {
83 whenever(webInterface.getCurrentSoneWithoutCreatingSession(toadletContext)).thenReturn(null)
84 whenever(webInterface.getCurrentSoneCreatingSession(toadletContext)).thenReturn(null)
87 protected fun addRequestParameter(key: String, value: String) {
88 requestParameters += key to value
91 protected fun addNotification(vararg notifications: Notification) {
92 this.notifications += notifications
95 protected fun addSone(sone: Sone) {
96 remoteSones += sone.id to sone
99 protected fun addNewPost(id: String, soneId: String, time: Long, recipientId: String? = null) {
100 newPosts[id] = mock<Post>().apply {
101 whenever(this.id).thenReturn(id)
102 val sone = mock<Sone>().apply { whenever(this.id).thenReturn(soneId) }
103 whenever(this.sone).thenReturn(sone)
104 whenever(this.time).thenReturn(time)
105 whenever(this.recipientId).thenReturn(recipientId.asOptional())
109 protected fun addNewReply(id: String, soneId: String, postId: String, postSoneId: String) {
110 newReplies[id] = mock<PostReply>().apply {
111 whenever(this.id).thenReturn(id)
112 val sone = mock<Sone>().apply { whenever(this.id).thenReturn(soneId) }
113 whenever(this.sone).thenReturn(sone)
114 val postSone = mock<Sone>().apply { whenever(this.id).thenReturn(postSoneId) }
115 val post = mock<Post>().apply {
116 whenever(this.sone).thenReturn(postSone)
118 whenever(this.post).thenReturn(post.asOptional())
119 whenever(this.postId).thenReturn(postId)