✅ Fix tests failing with non-English locales
[Sone.git] / src / test / kotlin / net / pterodactylus / sone / web / ajax / GetTimesAjaxPageTest.kt
1 package net.pterodactylus.sone.web.ajax
2
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.OverrideLocale
9 import net.pterodactylus.sone.test.get
10 import net.pterodactylus.sone.test.getInstance
11 import net.pterodactylus.sone.test.isProvidedByMock
12 import net.pterodactylus.sone.test.mock
13 import net.pterodactylus.sone.test.whenever
14 import net.pterodactylus.sone.text.TimeText
15 import net.pterodactylus.sone.text.TimeTextConverter
16 import net.pterodactylus.sone.utils.jsonObject
17 import net.pterodactylus.sone.web.baseInjector
18 import org.hamcrest.MatcherAssert.assertThat
19 import org.hamcrest.Matchers.containsInAnyOrder
20 import org.hamcrest.Matchers.emptyIterable
21 import org.hamcrest.Matchers.notNullValue
22 import org.junit.Before
23 import org.junit.Rule
24 import org.junit.Test
25 import org.mockito.ArgumentMatchers.any
26 import org.mockito.ArgumentMatchers.anyLong
27 import java.util.Locale.ENGLISH
28 import java.util.TimeZone
29 import java.util.TimeZone.getTimeZone
30
31 /**
32  * Unit test for [GetTimesAjaxPage].
33  */
34 class GetTimesAjaxPageTest : JsonPageTest("getTimes.ajax", needsFormPassword = false, requiresLogin = false) {
35
36         @get:Rule
37         val overrideLocale = OverrideLocale(ENGLISH)
38
39         private val timeTextConverter = mock<TimeTextConverter>()
40         private val l10nFilter = mock<L10nFilter>()
41         override val page: JsonPage by lazy { GetTimesAjaxPage(webInterface, timeTextConverter, l10nFilter, getTimeZone("UTC")) }
42         private val testPosts = listOf(createPost(1), createPost(2))
43         private val testReplies = listOf(createReply(1), createReply(2))
44
45         private fun createPost(index: Int): Post {
46                 return mock<Post>().apply {
47                         whenever(id).thenReturn("post$index")
48                         whenever(time).thenReturn(index.toLong() * 1000)
49                 }
50         }
51
52         private fun createReply(index: Int): PostReply {
53                 return mock<PostReply>().apply {
54                         whenever(id).thenReturn("reply$index")
55                         whenever(time).thenReturn(index.toLong() * 1000)
56                 }
57         }
58
59         @Before
60         fun setupMocks() {
61                 whenever(timeTextConverter.getTimeText(anyLong())).then { TimeText(L10nText(it.get<Long>(0).toString()), it.get<Long>(0) * 2) }
62                 whenever(l10nFilter.format(any(), any(), any())).then { it.get<L10nText>(1).text }
63         }
64
65         @Test
66         fun `request without any parameters responds with empty post and reply times`() {
67                 assertThatJsonIsSuccessful()
68                 assertThat(json["postTimes"]?.toList(), emptyIterable())
69                 assertThat(json["replyTimes"]?.toList(), emptyIterable())
70         }
71
72         @Test
73         fun `request with single post parameter responds with post times and empty reply times`() {
74                 addPost(testPosts[0])
75                 addRequestParameter("posts", "post1")
76                 assertThatJsonIsSuccessful()
77                 assertThat(json["postTimes"]!!.fields().asSequence().map { it.key to it.value }.toList(), containsInAnyOrder<Pair<String, JsonNode>>(
78                                 "post1" to jsonObject("timeText" to "1000", "refreshTime" to 2L, "tooltip" to "Jan 1, 1970, 00:00:01")
79                 ))
80                 assertThat(json["replyTimes"]?.toList(), emptyIterable())
81         }
82
83         @Test
84         fun `request with single reply parameter responds with reply times and empty post times`() {
85                 addReply(testReplies[0])
86                 addRequestParameter("replies", "reply1")
87                 assertThatJsonIsSuccessful()
88                 assertThat(json["postTimes"]?.toList(), emptyIterable())
89                 assertThat(json["replyTimes"]!!.fields().asSequence().map { it.key to it.value }.toList(), containsInAnyOrder<Pair<String, JsonNode>>(
90                                 "reply1" to jsonObject("timeText" to "1000", "refreshTime" to 2L, "tooltip" to "Jan 1, 1970, 00:00:01")
91                 ))
92         }
93
94         @Test
95         fun `request with multiple post parameter responds with post times and empty reply times`() {
96                 addPost(testPosts[0])
97                 addPost(testPosts[1])
98                 addRequestParameter("posts", "post1,post2,post3")
99                 assertThatJsonIsSuccessful()
100                 assertThat(json["postTimes"]!!.fields().asSequence().map { it.key to it.value }.toList(), containsInAnyOrder<Pair<String, JsonNode>>(
101                                 "post1" to jsonObject("timeText" to "1000", "refreshTime" to 2L, "tooltip" to "Jan 1, 1970, 00:00:01"),
102                                 "post2" to jsonObject("timeText" to "2000", "refreshTime" to 4L, "tooltip" to "Jan 1, 1970, 00:00:02")
103                 ))
104                 assertThat(json["replyTimes"]?.toList(), emptyIterable())
105         }
106
107         @Test
108         fun `request with multiple reply parameters responds with reply times and empty post times`() {
109                 addReply(testReplies[0])
110                 addReply(testReplies[1])
111                 addRequestParameter("replies", "reply1,reply2,reply3")
112                 assertThatJsonIsSuccessful()
113                 assertThat(json["postTimes"]?.toList(), emptyIterable())
114                 assertThat(json["replyTimes"]!!.fields().asSequence().map { it.key to it.value }.toList(), containsInAnyOrder<Pair<String, JsonNode>>(
115                                 "reply1" to jsonObject("timeText" to "1000", "refreshTime" to 2L, "tooltip" to "Jan 1, 1970, 00:00:01"),
116                                 "reply2" to jsonObject("timeText" to "2000", "refreshTime" to 4L, "tooltip" to "Jan 1, 1970, 00:00:02")
117                 ))
118         }
119
120         @Test
121         fun `page can be created by dependency injection`() {
122                 assertThat(baseInjector.createChildInjector(
123                                 TimeTextConverter::class.isProvidedByMock(),
124                                 L10nFilter::class.isProvidedByMock(),
125                                 TimeZone::class.isProvidedByMock()
126                 ).getInstance<GetTimesAjaxPage>(), notNullValue())
127         }
128
129 }