1 package net.pterodactylus.sone.web.ajax
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
23 import org.mockito.ArgumentMatchers.any
24 import org.mockito.ArgumentMatchers.anyLong
27 * Unit test for [GetStatusAjaxPage].
29 class GetStatusAjaxPageTest: JsonPageTest() {
31 private val timeTextConverter = mock<TimeTextConverter>()
32 private val l10nFilter = mock<L10nFilter>()
33 override var page: JsonPage = GetStatusAjaxPage(webInterface, timeTextConverter, l10nFilter)
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 }
42 fun `page returns correct path`() {
43 assertThat(page.path, equalTo("getStatus.ajax"))
47 fun `page does not require form password`() {
48 assertThat(page.needsFormPassword(), equalTo(false))
52 fun `page does not require login`() {
53 assertThat(page.requiresLogin(), equalTo(false))
57 fun `page returns correct attribute “loggedIn” if sone is logged in`() {
58 assertThat(json.get("loggedIn").asText(), equalTo("true"))
62 fun `page returns correct attribute “loggedIn” if sone is not logged in`() {
64 assertThat(json.get("loggedIn").asText(), equalTo("false"))
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")
77 fun `page returns empty options if sone is not logged in`() {
79 assertThat(json.get("options"), emptyIterable())
83 fun `page returns empty sones object if no sone is logged in and no sones parameter is given`() {
85 assertThat(json.get("sones"), emptyIterable())
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")
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")
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) }
113 addNotification(*notifications.toTypedArray())
114 assertThat(json.get("notificationHash").asInt(), equalTo(notifications.sortedBy { it.createdTime }.hashCode()))
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")
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")
137 private fun JsonNode.toMap() = fields().asSequence().map { it.key!! to if (it.value.isNull) null else it.value.asText()!! }.toMap()