Let status page give out information about loaded elements
[Sone.git] / src / test / kotlin / net / pterodactylus / sone / web / ajax / JsonPageTest.kt
1 package net.pterodactylus.sone.web.ajax
2
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
22
23 /**
24  * Base class for tests for any [JsonPage] implementations.
25  */
26 open class JsonPageTest {
27
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)!! }
33
34         protected val toadletContext = mock<ToadletContext>()
35         protected val freenetRequest = mock<FreenetRequest>()
36         protected val httpRequest = mock<HTTPRequest>()
37         protected val currentSone = deepMock<Sone>()
38
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 loadedElements = mutableMapOf<String, LinkedElement>()
45         private val notifications = mutableListOf<Notification>()
46
47         @Before
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 }
55         }
56
57         @Before
58         fun setupCore() {
59                 whenever(core.getSone(anyString())).thenAnswer { (localSones + remoteSones)[it.getArgument(0)].asOptional() }
60         }
61
62         @Before
63         fun setupElementLoader() {
64                 whenever(elementLoader.loadElement(anyString())).thenAnswer {
65                         loadedElements[it.getArgument(0)] ?: LinkedElement(it.getArgument(0), loading = true)
66                 }
67         }
68
69         @Before
70         fun setupCurrentSone() {
71                 currentSone.mock("soneId", "Sone_Id", true, 1000, idle)
72         }
73
74         @Before
75         fun setupFreenetRequest() {
76                 whenever(freenetRequest.toadletContext).thenReturn(toadletContext)
77                 whenever(freenetRequest.httpRequest).thenReturn(httpRequest)
78         }
79
80         @Before
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) }
84         }
85
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)
92         }
93
94         protected fun unsetCurrentSone() {
95                 whenever(webInterface.getCurrentSoneWithoutCreatingSession(toadletContext)).thenReturn(null)
96                 whenever(webInterface.getCurrentSoneCreatingSession(toadletContext)).thenReturn(null)
97         }
98
99         protected fun addRequestParameter(key: String, value: String) {
100                 requestParameters += key to value
101         }
102
103         protected fun addNotification(vararg notifications: Notification) {
104                 this.notifications += notifications
105         }
106
107         protected fun addSone(sone: Sone) {
108                 remoteSones += sone.id to sone
109         }
110
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())
118                 }
119         }
120
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)
129                         }
130                         whenever(this.post).thenReturn(post.asOptional())
131                         whenever(this.postId).thenReturn(postId)
132                 }
133         }
134
135         protected fun addLoadedElement(link: String, loading: Boolean, failed: Boolean) {
136                 loadedElements[link] = LinkedElement(link, failed, loading)
137         }
138
139 }