Replace get status AJAX page with Kotlin version
[Sone.git] / src / test / kotlin / net / pterodactylus / sone / web / ajax / GetStatusAjaxPageTest.kt
1 package net.pterodactylus.sone.web.ajax
2
3 import com.fasterxml.jackson.databind.JsonNode
4 import net.pterodactylus.sone.data.Sone
5 import net.pterodactylus.sone.data.Sone.SoneStatus.downloading
6 import net.pterodactylus.sone.data.Sone.SoneStatus.inserting
7 import net.pterodactylus.sone.freenet.L10nFilter
8 import net.pterodactylus.sone.freenet.L10nText
9 import net.pterodactylus.sone.test.deepMock
10 import net.pterodactylus.sone.test.mock
11 import net.pterodactylus.sone.test.whenever
12 import net.pterodactylus.sone.text.TimeText
13 import net.pterodactylus.sone.text.TimeTextConverter
14 import net.pterodactylus.util.notify.Notification
15 import org.hamcrest.MatcherAssert.assertThat
16 import org.hamcrest.Matchers.allOf
17 import org.hamcrest.Matchers.containsInAnyOrder
18 import org.hamcrest.Matchers.emptyIterable
19 import org.hamcrest.Matchers.equalTo
20 import org.hamcrest.Matchers.hasEntry
21 import org.junit.Before
22 import org.junit.Test
23 import org.mockito.ArgumentMatchers.any
24 import org.mockito.ArgumentMatchers.anyLong
25
26 /**
27  * Unit test for [GetStatusAjaxPage].
28  */
29 class GetStatusAjaxPageTest: JsonPageTest() {
30
31         private val timeTextConverter = mock<TimeTextConverter>()
32         private val l10nFilter = mock<L10nFilter>()
33         override var page: JsonPage = GetStatusAjaxPage(webInterface, timeTextConverter, l10nFilter)
34
35         @Before
36         fun setupTimeTextConverter() {
37                 whenever(timeTextConverter.getTimeText(anyLong())).thenAnswer { TimeText(L10nText(it.getArgument<Long>(0).toString()), it.getArgument(0)) }
38                 whenever(l10nFilter.format(any(), any(), any())).thenAnswer { it.getArgument<L10nText>(1).text }
39         }
40
41         @Test
42         fun `page returns correct path`() {
43                 assertThat(page.path, equalTo("getStatus.ajax"))
44         }
45
46         @Test
47         fun `page does not require form password`() {
48                 assertThat(page.needsFormPassword(), equalTo(false))
49         }
50
51         @Test
52         fun `page does not require login`() {
53                 assertThat(page.requiresLogin(), equalTo(false))
54         }
55
56         @Test
57         fun `page returns correct attribute “loggedIn” if sone is logged in`() {
58                 assertThat(json.get("loggedIn").asText(), equalTo("true"))
59         }
60
61         @Test
62         fun `page returns correct attribute “loggedIn” if sone is not logged in`() {
63                 unsetCurrentSone()
64                 assertThat(json.get("loggedIn").asText(), equalTo("false"))
65         }
66
67         @Test
68         fun `page returns options for sone if sone is logged in`() {
69                 assertThat(json.get("options").toMap(), allOf(
70                                 hasEntry("ShowNotification/NewSones", "false"),
71                                 hasEntry("ShowNotification/NewPosts", "false"),
72                                 hasEntry("ShowNotification/NewReplies", "false")
73                 ))
74         }
75
76         @Test
77         fun `page returns empty options if sone is not logged in`() {
78                 unsetCurrentSone()
79                 assertThat(json.get("options"), emptyIterable())
80         }
81
82         @Test
83         fun `page returns empty sones object if no sone is logged in and no sones parameter is given`() {
84                 unsetCurrentSone()
85                 assertThat(json.get("sones"), emptyIterable())
86         }
87
88         @Test
89         fun `page returns a sones object with the current sone if not other sones parameter is given`() {
90                 assertThat(json.get("sones").elements().asSequence().map { it.toMap() }.toList(), containsInAnyOrder(
91                                 mapOf<String, String?>("id" to "soneId", "name" to "Sone_Id", "local" to "true", "status" to "idle", "modified" to "false", "locked" to "false", "lastUpdatedUnknown" to "false", "lastUpdated" to "Jan 1, 1970, 01:00:01", "lastUpdatedText" to "1000")
92                 ))
93         }
94
95         @Test
96         fun `page returns some sones objects with the current sone and some sones given as sones parameter`() {
97                 addSone(deepMock<Sone>().mock("sone1", "Sone 1", false, 2000, downloading))
98                 addSone(deepMock<Sone>().mock("sone3", "Sone 3", true, 3000, inserting))
99                 addRequestParameter("soneIds", "sone1,sone2,sone3")
100                 assertThat(json.get("sones").elements().asSequence().map { it.toMap() }.toList(), containsInAnyOrder(
101                                 mapOf<String, String?>("id" to "soneId", "name" to "Sone_Id", "local" to "true", "status" to "idle", "modified" to "false", "locked" to "false", "lastUpdatedUnknown" to "false", "lastUpdated" to "Jan 1, 1970, 01:00:01", "lastUpdatedText" to "1000"),
102                                 mapOf("id" to "sone1", "name" to "Sone 1", "local" to "false", "status" to "downloading", "modified" to "false", "locked" to "false", "lastUpdatedUnknown" to "false", "lastUpdated" to "Jan 1, 1970, 01:00:02", "lastUpdatedText" to "2000"),
103                                 mapOf("id" to "sone3", "name" to "Sone 3", "local" to "true", "status" to "inserting", "modified" to "false", "locked" to "false", "lastUpdatedUnknown" to "false", "lastUpdated" to "Jan 1, 1970, 01:00:03", "lastUpdatedText" to "3000")
104                 ))
105         }
106
107         @Test
108         fun `page returns correct notifications hash`() {
109                 val notifications = listOf(
110                                 mock<Notification>().apply { whenever(this.createdTime).thenReturn(2000) },
111                                 mock<Notification>().apply { whenever(this.createdTime).thenReturn(1000) }
112                 )
113                 addNotification(*notifications.toTypedArray())
114                 assertThat(json.get("notificationHash").asInt(), equalTo(notifications.sortedBy { it.createdTime }.hashCode()))
115         }
116
117         @Test
118         fun `page returns new posts`() {
119                 addNewPost("post1", "sone1", 1000)
120                 addNewPost("post2", "sone2", 2000, "sone1")
121                 assertThat(json.get("newPosts").elements().asSequence().map { it.toMap() }.toList(), containsInAnyOrder(
122                                 mapOf("id" to "post1", "sone" to "sone1", "time" to "1000", "recipient" to null),
123                                 mapOf("id" to "post2", "sone" to "sone2", "time" to "2000", "recipient" to "sone1")
124                 ))
125         }
126
127         @Test
128         fun `page returns new replies`() {
129                 addNewReply("reply1", "sone1", "post1", "sone11")
130                 addNewReply("reply2", "sone2", "post2", "sone22")
131                 assertThat(json.get("newReplies").elements().asSequence().map { it.toMap() }.toList(), containsInAnyOrder(
132                                 mapOf<String, String?>("id" to "reply1", "sone" to "sone1", "post" to "post1", "postSone" to "sone11"),
133                                 mapOf("id" to "reply2", "sone" to "sone2", "post" to "post2", "postSone" to "sone22")
134                 ))
135         }
136
137         private fun JsonNode.toMap() = fields().asSequence().map { it.key!! to if (it.value.isNull) null else it.value.asText()!! }.toMap()
138
139 }