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