1 package net.pterodactylus.sone.web.ajax
3 import com.fasterxml.jackson.databind.JsonNode
4 import net.pterodactylus.sone.data.Post
5 import net.pterodactylus.sone.data.PostReply
6 import net.pterodactylus.sone.freenet.L10nFilter
7 import net.pterodactylus.sone.freenet.L10nText
8 import net.pterodactylus.sone.test.get
9 import net.pterodactylus.sone.test.mock
10 import net.pterodactylus.sone.test.whenever
11 import net.pterodactylus.sone.text.TimeText
12 import net.pterodactylus.sone.text.TimeTextConverter
13 import net.pterodactylus.sone.utils.jsonObject
14 import org.hamcrest.MatcherAssert.assertThat
15 import org.hamcrest.Matchers.containsInAnyOrder
16 import org.hamcrest.Matchers.emptyIterable
17 import org.hamcrest.Matchers.equalTo
18 import org.junit.Before
20 import org.mockito.ArgumentMatchers.any
21 import org.mockito.ArgumentMatchers.anyLong
24 * Unit test for [GetTimesAjaxPage].
26 class GetTimesAjaxPageTest : JsonPageTest("getTimes.ajax", needsFormPassword = false, requiresLogin = false) {
28 private val timeTextConverter = mock<TimeTextConverter>()
29 private val l10nFilter = mock<L10nFilter>()
30 override val page: JsonPage by lazy { GetTimesAjaxPage(webInterface, timeTextConverter, l10nFilter) }
31 private val posts = listOf(createPost(1), createPost(2))
32 private val replies = listOf(createReply(1), createReply(2))
34 private fun createPost(index: Int): Post {
35 return mock<Post>().apply {
36 whenever(id).thenReturn("post$index")
37 whenever(time).thenReturn(index.toLong() * 1000)
41 private fun createReply(index: Int): PostReply {
42 return mock<PostReply>().apply {
43 whenever(id).thenReturn("reply$index")
44 whenever(time).thenReturn(index.toLong() * 1000)
50 whenever(timeTextConverter.getTimeText(anyLong())).then { TimeText(L10nText(it.get<Long>(0).toString()), it.get<Long>(0) * 2) }
51 whenever(l10nFilter.format(any(), any(), any())).then { it.get<L10nText>(1).text }
55 fun `request without any parameters responds with empty post and reply times`() {
56 assertThat(json.isSuccess, equalTo(true))
57 assertThat(json["postTimes"].toList(), emptyIterable())
58 assertThat(json["replyTimes"].toList(), emptyIterable())
62 fun `request with single post parameter responds with post times and empty reply times`() {
64 addRequestParameter("posts", "post1")
65 assertThat(json.isSuccess, equalTo(true))
66 assertThat(json["postTimes"].fields().asSequence().map { it.key to it.value }.toList(), containsInAnyOrder<Pair<String, JsonNode>>(
67 "post1" to jsonObject("timeText" to "1000", "refreshTime" to 2L, "tooltip" to "Jan 1, 1970, 01:00:01")
69 assertThat(json["replyTimes"].toList(), emptyIterable())
73 fun `request with single reply parameter responds with reply times and empty post times`() {
75 addRequestParameter("replies", "reply1")
76 assertThat(json.isSuccess, equalTo(true))
77 assertThat(json["postTimes"].toList(), emptyIterable())
78 assertThat(json["replyTimes"].fields().asSequence().map { it.key to it.value }.toList(), containsInAnyOrder<Pair<String, JsonNode>>(
79 "reply1" to jsonObject("timeText" to "1000", "refreshTime" to 2L, "tooltip" to "Jan 1, 1970, 01:00:01")
84 fun `request with multiple post parameter responds with post times and empty reply times`() {
87 addRequestParameter("posts", "post1,post2,post3")
88 assertThat(json.isSuccess, equalTo(true))
89 assertThat(json["postTimes"].fields().asSequence().map { it.key to it.value }.toList(), containsInAnyOrder<Pair<String, JsonNode>>(
90 "post1" to jsonObject("timeText" to "1000", "refreshTime" to 2L, "tooltip" to "Jan 1, 1970, 01:00:01"),
91 "post2" to jsonObject("timeText" to "2000", "refreshTime" to 4L, "tooltip" to "Jan 1, 1970, 01:00:02")
93 assertThat(json["replyTimes"].toList(), emptyIterable())
97 fun `request with multiple reply parameters responds with reply times and empty post times`() {
100 addRequestParameter("replies", "reply1,reply2,reply3")
101 assertThat(json.isSuccess, equalTo(true))
102 assertThat(json["postTimes"].toList(), emptyIterable())
103 assertThat(json["replyTimes"].fields().asSequence().map { it.key to it.value }.toList(), containsInAnyOrder<Pair<String, JsonNode>>(
104 "reply1" to jsonObject("timeText" to "1000", "refreshTime" to 2L, "tooltip" to "Jan 1, 1970, 01:00:01"),
105 "reply2" to jsonObject("timeText" to "2000", "refreshTime" to 4L, "tooltip" to "Jan 1, 1970, 01:00:02")