Add test for DI constructability of GetStatusAjaxPage
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Thu, 25 Jan 2018 18:10:57 +0000 (19:10 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Thu, 25 Jan 2018 18:30:52 +0000 (19:30 +0100)
src/main/kotlin/net/pterodactylus/sone/web/ajax/GetStatusAjaxPage.kt
src/test/kotlin/net/pterodactylus/sone/web/ajax/GetStatusAjaxPageTest.kt

index 8aa4887..0017a32 100644 (file)
@@ -17,14 +17,18 @@ import net.pterodactylus.sone.web.WebInterface
 import net.pterodactylus.sone.web.page.FreenetRequest
 import java.text.SimpleDateFormat
 import java.util.TimeZone
 import net.pterodactylus.sone.web.page.FreenetRequest
 import java.text.SimpleDateFormat
 import java.util.TimeZone
+import javax.inject.Inject
 
 /**
  * The “get status” AJAX handler returns all information that is necessary to
  * update the web interface in real-time.
  */
 
 /**
  * The “get status” AJAX handler returns all information that is necessary to
  * update the web interface in real-time.
  */
-class GetStatusAjaxPage(webInterface: WebInterface, private val elementLoader: ElementLoader, private val timeTextConverter: TimeTextConverter, private val l10nFilter: L10nFilter, timeZone: TimeZone = TimeZone.getDefault()):
+class GetStatusAjaxPage(webInterface: WebInterface, private val elementLoader: ElementLoader, private val timeTextConverter: TimeTextConverter, private val l10nFilter: L10nFilter, timeZone: TimeZone):
                JsonPage("getStatus.ajax", webInterface) {
 
                JsonPage("getStatus.ajax", webInterface) {
 
+       @Inject constructor(webInterface: WebInterface, elementLoader: ElementLoader, timeTextConverter: TimeTextConverter, l10nFilter: L10nFilter):
+                       this(webInterface, elementLoader, timeTextConverter, l10nFilter, TimeZone.getDefault())
+
        private val dateFormatter = SimpleDateFormat("MMM d, yyyy, HH:mm:ss").apply {
                this.timeZone = timeZone
        }
        private val dateFormatter = SimpleDateFormat("MMM d, yyyy, HH:mm:ss").apply {
                this.timeZone = timeZone
        }
index 9576c7c..f270d71 100644 (file)
@@ -1,17 +1,21 @@
 package net.pterodactylus.sone.web.ajax
 
 import com.fasterxml.jackson.databind.JsonNode
 package net.pterodactylus.sone.web.ajax
 
 import com.fasterxml.jackson.databind.JsonNode
+import net.pterodactylus.sone.core.ElementLoader
 import net.pterodactylus.sone.data.Sone
 import net.pterodactylus.sone.data.Sone.SoneStatus.downloading
 import net.pterodactylus.sone.data.Sone.SoneStatus.inserting
 import net.pterodactylus.sone.freenet.L10nFilter
 import net.pterodactylus.sone.freenet.L10nText
 import net.pterodactylus.sone.test.deepMock
 import net.pterodactylus.sone.data.Sone
 import net.pterodactylus.sone.data.Sone.SoneStatus.downloading
 import net.pterodactylus.sone.data.Sone.SoneStatus.inserting
 import net.pterodactylus.sone.freenet.L10nFilter
 import net.pterodactylus.sone.freenet.L10nText
 import net.pterodactylus.sone.test.deepMock
+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.jsonArray
 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.jsonArray
+import net.pterodactylus.sone.web.baseInjector
 import net.pterodactylus.util.notify.Notification
 import org.hamcrest.MatcherAssert.assertThat
 import org.hamcrest.Matchers.allOf
 import net.pterodactylus.util.notify.Notification
 import org.hamcrest.MatcherAssert.assertThat
 import org.hamcrest.Matchers.allOf
@@ -19,6 +23,7 @@ import org.hamcrest.Matchers.containsInAnyOrder
 import org.hamcrest.Matchers.emptyIterable
 import org.hamcrest.Matchers.equalTo
 import org.hamcrest.Matchers.hasEntry
 import org.hamcrest.Matchers.emptyIterable
 import org.hamcrest.Matchers.equalTo
 import org.hamcrest.Matchers.hasEntry
+import org.hamcrest.Matchers.notNullValue
 import org.junit.Before
 import org.junit.Test
 import org.mockito.ArgumentMatchers.any
 import org.junit.Before
 import org.junit.Test
 import org.mockito.ArgumentMatchers.any
@@ -134,6 +139,15 @@ class GetStatusAjaxPageTest: JsonPageTest("getStatus.ajax", requiresLogin = fals
                ))
        }
 
                ))
        }
 
+       @Test
+       fun `page can be created by dependency injection`() {
+           assertThat(baseInjector.createChildInjector(
+                           ElementLoader::class.isProvidedByMock(),
+                           TimeTextConverter::class.isProvidedByMock(),
+                           L10nFilter::class.isProvidedByMock()
+           ).getInstance<GetStatusAjaxPage>(), notNullValue())
+       }
+
        private fun JsonNode.toMap() = fields().asSequence().map { it.key!! to if (it.value.isNull) null else it.value.asText()!! }.toMap()
 
 }
        private fun JsonNode.toMap() = fields().asSequence().map { it.key!! to if (it.value.isNull) null else it.value.asText()!! }.toMap()
 
 }