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
22 import java.util.TimeZone.getTimeZone
25 * Unit test for [GetTimesAjaxPage].
27 class GetTimesAjaxPageTest : JsonPageTest("getTimes.ajax", needsFormPassword = false, requiresLogin = false) {
29 private val timeTextConverter = mock<TimeTextConverter>()
30 private val l10nFilter = mock<L10nFilter>()
31 override val page: JsonPage by lazy { GetTimesAjaxPage(webInterface, timeTextConverter, l10nFilter, getTimeZone("UTC")) }
32 private val testPosts = listOf(createPost(1), createPost(2))
33 private val testReplies = listOf(createReply(1), createReply(2))
35 private fun createPost(index: Int): Post {
36 return mock<Post>().apply {
37 whenever(id).thenReturn("post$index")
38 whenever(time).thenReturn(index.toLong() * 1000)
42 private fun createReply(index: Int): PostReply {
43 return mock<PostReply>().apply {
44 whenever(id).thenReturn("reply$index")
45 whenever(time).thenReturn(index.toLong() * 1000)
51 whenever(timeTextConverter.getTimeText(anyLong())).then { TimeText(L10nText(it.get<Long>(0).toString()), it.get<Long>(0) * 2) }
52 whenever(l10nFilter.format(any(), any(), any())).then { it.get<L10nText>(1).text }
56 fun `request without any parameters responds with empty post and reply times`() {
57 assertThatJsonIsSuccessful()
58 assertThat(json["postTimes"]?.toList(), emptyIterable())
59 assertThat(json["replyTimes"]?.toList(), emptyIterable())
63 fun `request with single post parameter responds with post times and empty reply times`() {
65 addRequestParameter("posts", "post1")
66 assertThatJsonIsSuccessful()
67 assertThat(json["postTimes"]!!.fields().asSequence().map { it.key to it.value }.toList(), containsInAnyOrder<Pair<String, JsonNode>>(
68 "post1" to jsonObject("timeText" to "1000", "refreshTime" to 2L, "tooltip" to "Jan 1, 1970, 00:00:01")
70 assertThat(json["replyTimes"]?.toList(), emptyIterable())
74 fun `request with single reply parameter responds with reply times and empty post times`() {
75 addReply(testReplies[0])
76 addRequestParameter("replies", "reply1")
77 assertThatJsonIsSuccessful()
78 assertThat(json["postTimes"]?.toList(), emptyIterable())
79 assertThat(json["replyTimes"]!!.fields().asSequence().map { it.key to it.value }.toList(), containsInAnyOrder<Pair<String, JsonNode>>(
80 "reply1" to jsonObject("timeText" to "1000", "refreshTime" to 2L, "tooltip" to "Jan 1, 1970, 00:00:01")
85 fun `request with multiple post parameter responds with post times and empty reply times`() {
88 addRequestParameter("posts", "post1,post2,post3")
89 assertThatJsonIsSuccessful()
90 assertThat(json["postTimes"]!!.fields().asSequence().map { it.key to it.value }.toList(), containsInAnyOrder<Pair<String, JsonNode>>(
91 "post1" to jsonObject("timeText" to "1000", "refreshTime" to 2L, "tooltip" to "Jan 1, 1970, 00:00:01"),
92 "post2" to jsonObject("timeText" to "2000", "refreshTime" to 4L, "tooltip" to "Jan 1, 1970, 00:00:02")
94 assertThat(json["replyTimes"]?.toList(), emptyIterable())
98 fun `request with multiple reply parameters responds with reply times and empty post times`() {
99 addReply(testReplies[0])
100 addReply(testReplies[1])
101 addRequestParameter("replies", "reply1,reply2,reply3")
102 assertThatJsonIsSuccessful()
103 assertThat(json["postTimes"]?.toList(), emptyIterable())
104 assertThat(json["replyTimes"]!!.fields().asSequence().map { it.key to it.value }.toList(), containsInAnyOrder<Pair<String, JsonNode>>(
105 "reply1" to jsonObject("timeText" to "1000", "refreshTime" to 2L, "tooltip" to "Jan 1, 1970, 00:00:01"),
106 "reply2" to jsonObject("timeText" to "2000", "refreshTime" to 4L, "tooltip" to "Jan 1, 1970, 00:00:02")