Add test for DI constructability of GetTimesAjaxPage
[Sone.git] / src / test / kotlin / net / pterodactylus / sone / web / ajax / GetTimesAjaxPageTest.kt
index af33eea..62a34d1 100644 (file)
@@ -6,19 +6,23 @@ import net.pterodactylus.sone.data.PostReply
 import net.pterodactylus.sone.freenet.L10nFilter
 import net.pterodactylus.sone.freenet.L10nText
 import net.pterodactylus.sone.test.get
+import net.pterodactylus.sone.test.getInstance
+import net.pterodactylus.sone.test.isProvidedByMock
 import net.pterodactylus.sone.test.mock
 import net.pterodactylus.sone.test.whenever
 import net.pterodactylus.sone.text.TimeText
 import net.pterodactylus.sone.text.TimeTextConverter
 import net.pterodactylus.sone.utils.jsonObject
+import net.pterodactylus.sone.web.baseInjector
 import org.hamcrest.MatcherAssert.assertThat
 import org.hamcrest.Matchers.containsInAnyOrder
 import org.hamcrest.Matchers.emptyIterable
-import org.hamcrest.Matchers.equalTo
+import org.hamcrest.Matchers.notNullValue
 import org.junit.Before
 import org.junit.Test
 import org.mockito.ArgumentMatchers.any
 import org.mockito.ArgumentMatchers.anyLong
+import java.util.TimeZone
 import java.util.TimeZone.getTimeZone
 
 /**
@@ -29,8 +33,8 @@ class GetTimesAjaxPageTest : JsonPageTest("getTimes.ajax", needsFormPassword = f
        private val timeTextConverter = mock<TimeTextConverter>()
        private val l10nFilter = mock<L10nFilter>()
        override val page: JsonPage by lazy { GetTimesAjaxPage(webInterface, timeTextConverter, l10nFilter, getTimeZone("UTC")) }
-       private val posts = listOf(createPost(1), createPost(2))
-       private val replies = listOf(createReply(1), createReply(2))
+       private val testPosts = listOf(createPost(1), createPost(2))
+       private val testReplies = listOf(createReply(1), createReply(2))
 
        private fun createPost(index: Int): Post {
                return mock<Post>().apply {
@@ -54,57 +58,66 @@ class GetTimesAjaxPageTest : JsonPageTest("getTimes.ajax", needsFormPassword = f
 
        @Test
        fun `request without any parameters responds with empty post and reply times`() {
-               assertThat(json.isSuccess, equalTo(true))
-               assertThat(json["postTimes"].toList(), emptyIterable())
-               assertThat(json["replyTimes"].toList(), emptyIterable())
+               assertThatJsonIsSuccessful()
+               assertThat(json["postTimes"]?.toList(), emptyIterable())
+               assertThat(json["replyTimes"]?.toList(), emptyIterable())
        }
 
        @Test
        fun `request with single post parameter responds with post times and empty reply times`() {
-               addPost(posts[0])
+               addPost(testPosts[0])
                addRequestParameter("posts", "post1")
-               assertThat(json.isSuccess, equalTo(true))
-               assertThat(json["postTimes"].fields().asSequence().map { it.key to it.value }.toList(), containsInAnyOrder<Pair<String, JsonNode>>(
+               assertThatJsonIsSuccessful()
+               assertThat(json["postTimes"]!!.fields().asSequence().map { it.key to it.value }.toList(), containsInAnyOrder<Pair<String, JsonNode>>(
                                "post1" to jsonObject("timeText" to "1000", "refreshTime" to 2L, "tooltip" to "Jan 1, 1970, 00:00:01")
                ))
-               assertThat(json["replyTimes"].toList(), emptyIterable())
+               assertThat(json["replyTimes"]?.toList(), emptyIterable())
        }
 
        @Test
        fun `request with single reply parameter responds with reply times and empty post times`() {
-               addReply(replies[0])
+               addReply(testReplies[0])
                addRequestParameter("replies", "reply1")
-               assertThat(json.isSuccess, equalTo(true))
-               assertThat(json["postTimes"].toList(), emptyIterable())
-               assertThat(json["replyTimes"].fields().asSequence().map { it.key to it.value }.toList(), containsInAnyOrder<Pair<String, JsonNode>>(
+               assertThatJsonIsSuccessful()
+               assertThat(json["postTimes"]?.toList(), emptyIterable())
+               assertThat(json["replyTimes"]!!.fields().asSequence().map { it.key to it.value }.toList(), containsInAnyOrder<Pair<String, JsonNode>>(
                                "reply1" to jsonObject("timeText" to "1000", "refreshTime" to 2L, "tooltip" to "Jan 1, 1970, 00:00:01")
                ))
        }
 
        @Test
        fun `request with multiple post parameter responds with post times and empty reply times`() {
-               addPost(posts[0])
-               addPost(posts[1])
+               addPost(testPosts[0])
+               addPost(testPosts[1])
                addRequestParameter("posts", "post1,post2,post3")
-               assertThat(json.isSuccess, equalTo(true))
-               assertThat(json["postTimes"].fields().asSequence().map { it.key to it.value }.toList(), containsInAnyOrder<Pair<String, JsonNode>>(
+               assertThatJsonIsSuccessful()
+               assertThat(json["postTimes"]!!.fields().asSequence().map { it.key to it.value }.toList(), containsInAnyOrder<Pair<String, JsonNode>>(
                                "post1" to jsonObject("timeText" to "1000", "refreshTime" to 2L, "tooltip" to "Jan 1, 1970, 00:00:01"),
                                "post2" to jsonObject("timeText" to "2000", "refreshTime" to 4L, "tooltip" to "Jan 1, 1970, 00:00:02")
                ))
-               assertThat(json["replyTimes"].toList(), emptyIterable())
+               assertThat(json["replyTimes"]?.toList(), emptyIterable())
        }
 
        @Test
        fun `request with multiple reply parameters responds with reply times and empty post times`() {
-               addReply(replies[0])
-               addReply(replies[1])
+               addReply(testReplies[0])
+               addReply(testReplies[1])
                addRequestParameter("replies", "reply1,reply2,reply3")
-               assertThat(json.isSuccess, equalTo(true))
-               assertThat(json["postTimes"].toList(), emptyIterable())
-               assertThat(json["replyTimes"].fields().asSequence().map { it.key to it.value }.toList(), containsInAnyOrder<Pair<String, JsonNode>>(
+               assertThatJsonIsSuccessful()
+               assertThat(json["postTimes"]?.toList(), emptyIterable())
+               assertThat(json["replyTimes"]!!.fields().asSequence().map { it.key to it.value }.toList(), containsInAnyOrder<Pair<String, JsonNode>>(
                                "reply1" to jsonObject("timeText" to "1000", "refreshTime" to 2L, "tooltip" to "Jan 1, 1970, 00:00:01"),
                                "reply2" to jsonObject("timeText" to "2000", "refreshTime" to 4L, "tooltip" to "Jan 1, 1970, 00:00:02")
                ))
        }
 
+       @Test
+       fun `page can be created by dependency injection`() {
+               assertThat(baseInjector.createChildInjector(
+                               TimeTextConverter::class.isProvidedByMock(),
+                               L10nFilter::class.isProvidedByMock(),
+                               TimeZone::class.isProvidedByMock()
+               ).getInstance<GetTimesAjaxPage>(), notNullValue())
+       }
+
 }