X-Git-Url: https://git.pterodactylus.net/?p=Sone.git;a=blobdiff_plain;f=src%2Ftest%2Fkotlin%2Fnet%2Fpterodactylus%2Fsone%2Fweb%2Fajax%2FGetStatusAjaxPageTest.kt;h=f270d7192672b9162234b0285359570d75cc4739;hp=1b0df131c2e4031e3c7360e7d905375b55c27be8;hb=9021ef622e01f71f067cab0583ac849b98646e16;hpb=0b033de14127df341b053fbe71ac8aebf8e318b8 diff --git a/src/test/kotlin/net/pterodactylus/sone/web/ajax/GetStatusAjaxPageTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/ajax/GetStatusAjaxPageTest.kt index 1b0df13..f270d71 100644 --- a/src/test/kotlin/net/pterodactylus/sone/web/ajax/GetStatusAjaxPageTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/web/ajax/GetStatusAjaxPageTest.kt @@ -1,17 +1,21 @@ 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.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.web.baseInjector import net.pterodactylus.util.notify.Notification import org.hamcrest.MatcherAssert.assertThat import org.hamcrest.Matchers.allOf @@ -19,19 +23,21 @@ import org.hamcrest.Matchers.containsInAnyOrder 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.mockito.ArgumentMatchers.anyLong +import java.util.TimeZone /** * Unit test for [GetStatusAjaxPage]. */ -class GetStatusAjaxPageTest: JsonPageTest() { +class GetStatusAjaxPageTest: JsonPageTest("getStatus.ajax", requiresLogin = false, needsFormPassword = false) { private val timeTextConverter = mock() private val l10nFilter = mock() - override var page: JsonPage = GetStatusAjaxPage(webInterface, elementLoader, timeTextConverter, l10nFilter) + override var page: JsonPage = GetStatusAjaxPage(webInterface, elementLoader, timeTextConverter, l10nFilter, TimeZone.getTimeZone("UTC")) @Before fun setupTimeTextConverter() { @@ -40,37 +46,22 @@ class GetStatusAjaxPageTest: JsonPageTest() { } @Test - fun `page returns correct path`() { - assertThat(page.path, equalTo("getStatus.ajax")) - } - - @Test - fun `page does not require form password`() { - assertThat(page.needsFormPassword(), equalTo(false)) - } - - @Test - fun `page does not require login`() { - assertThat(page.requiresLogin(), equalTo(false)) - } - - @Test fun `page returns correct attribute “loggedIn” if sone is logged in`() { - assertThat(json.get("loggedIn").asText(), equalTo("true")) + assertThat(json.get("loggedIn")?.asText(), equalTo("true")) } @Test fun `page returns correct attribute “loggedIn” if sone is not logged in`() { unsetCurrentSone() - assertThat(json.get("loggedIn").asText(), equalTo("false")) + assertThat(json.get("loggedIn")?.asText(), equalTo("false")) } @Test fun `page returns options for sone if sone is logged in`() { - assertThat(json.get("options").toMap(), allOf( - hasEntry("ShowNotification/NewSones", "false"), - hasEntry("ShowNotification/NewPosts", "false"), - hasEntry("ShowNotification/NewReplies", "false") + assertThat(json.get("options")?.toMap(), allOf( + hasEntry("ShowNotification/NewSones", "true"), + hasEntry("ShowNotification/NewPosts", "true"), + hasEntry("ShowNotification/NewReplies", "true") )) } @@ -88,8 +79,8 @@ class GetStatusAjaxPageTest: JsonPageTest() { @Test fun `page returns a sones object with the current sone if not other sones parameter is given`() { - assertThat(json.get("sones").elements().asSequence().map { it.toMap() }.toList(), containsInAnyOrder( - mapOf("id" to "soneId", "name" to "Sone_Id", "local" to "true", "status" to "idle", "modified" to "false", "locked" to "false", "lastUpdatedUnknown" to "false", "lastUpdated" to "Jan 1, 1970, 01:00:01", "lastUpdatedText" to "1000") + assertThat(json.get("sones")!!.elements().asSequence().map { it.toMap() }.toList(), containsInAnyOrder( + mapOf("id" to "soneId", "name" to "Sone_Id", "local" to "true", "status" to "idle", "modified" to "false", "locked" to "false", "lastUpdatedUnknown" to "false", "lastUpdated" to "Jan 1, 1970, 00:00:01", "lastUpdatedText" to "1000") )) } @@ -98,10 +89,10 @@ class GetStatusAjaxPageTest: JsonPageTest() { addSone(deepMock().mock("sone1", "Sone 1", false, 2000, downloading)) addSone(deepMock().mock("sone3", "Sone 3", true, 3000, inserting)) addRequestParameter("soneIds", "sone1,sone2,sone3") - assertThat(json.get("sones").elements().asSequence().map { it.toMap() }.toList(), containsInAnyOrder( - mapOf("id" to "soneId", "name" to "Sone_Id", "local" to "true", "status" to "idle", "modified" to "false", "locked" to "false", "lastUpdatedUnknown" to "false", "lastUpdated" to "Jan 1, 1970, 01:00:01", "lastUpdatedText" to "1000"), - mapOf("id" to "sone1", "name" to "Sone 1", "local" to "false", "status" to "downloading", "modified" to "false", "locked" to "false", "lastUpdatedUnknown" to "false", "lastUpdated" to "Jan 1, 1970, 01:00:02", "lastUpdatedText" to "2000"), - mapOf("id" to "sone3", "name" to "Sone 3", "local" to "true", "status" to "inserting", "modified" to "false", "locked" to "false", "lastUpdatedUnknown" to "false", "lastUpdated" to "Jan 1, 1970, 01:00:03", "lastUpdatedText" to "3000") + assertThat(json.get("sones")!!.elements().asSequence().map { it.toMap() }.toList(), containsInAnyOrder( + mapOf("id" to "soneId", "name" to "Sone_Id", "local" to "true", "status" to "idle", "modified" to "false", "locked" to "false", "lastUpdatedUnknown" to "false", "lastUpdated" to "Jan 1, 1970, 00:00:01", "lastUpdatedText" to "1000"), + mapOf("id" to "sone1", "name" to "Sone 1", "local" to "false", "status" to "downloading", "modified" to "false", "locked" to "false", "lastUpdatedUnknown" to "false", "lastUpdated" to "Jan 1, 1970, 00:00:02", "lastUpdatedText" to "2000"), + mapOf("id" to "sone3", "name" to "Sone 3", "local" to "true", "status" to "inserting", "modified" to "false", "locked" to "false", "lastUpdatedUnknown" to "false", "lastUpdated" to "Jan 1, 1970, 00:00:03", "lastUpdatedText" to "3000") )) } @@ -111,15 +102,15 @@ class GetStatusAjaxPageTest: JsonPageTest() { mock().apply { whenever(this.createdTime).thenReturn(2000) }, mock().apply { whenever(this.createdTime).thenReturn(1000) } ) - addNotification(*notifications.toTypedArray()) - assertThat(json.get("notificationHash").asInt(), equalTo(notifications.sortedBy { it.createdTime }.hashCode())) + notifications.forEachIndexed { index, notification -> addNotification(notification, "notification$index")} + assertThat(json.get("notificationHash")?.asInt(), equalTo(notifications.sortedBy { it.createdTime }.hashCode())) } @Test fun `page returns new posts`() { addNewPost("post1", "sone1", 1000) addNewPost("post2", "sone2", 2000, "sone1") - assertThat(json.get("newPosts").elements().asSequence().map { it.toMap() }.toList(), containsInAnyOrder( + assertThat(json.get("newPosts")!!.elements().asSequence().map { it.toMap() }.toList(), containsInAnyOrder( mapOf("id" to "post1", "sone" to "sone1", "time" to "1000", "recipient" to null), mapOf("id" to "post2", "sone" to "sone2", "time" to "2000", "recipient" to "sone1") )) @@ -129,7 +120,7 @@ class GetStatusAjaxPageTest: JsonPageTest() { fun `page returns new replies`() { addNewReply("reply1", "sone1", "post1", "sone11") addNewReply("reply2", "sone2", "post2", "sone22") - assertThat(json.get("newReplies").elements().asSequence().map { it.toMap() }.toList(), containsInAnyOrder( + assertThat(json.get("newReplies")!!.elements().asSequence().map { it.toMap() }.toList(), containsInAnyOrder( mapOf("id" to "reply1", "sone" to "sone1", "post" to "post1", "postSone" to "sone11"), mapOf("id" to "reply2", "sone" to "sone2", "post" to "post2", "postSone" to "sone22") )) @@ -141,13 +132,22 @@ class GetStatusAjaxPageTest: JsonPageTest() { addLinkedElement("KSK@test.html", loading = true, failed = false) addLinkedElement("KSK@test.jpeg", loading = false, failed = true) addRequestParameter("elements", jsonArray("KSK@test.png", "KSK@test.html", "KSK@test.jpeg").toString()) - assertThat(json.get("linkedElements").elements().asSequence().map { it.toMap() }.toList(), containsInAnyOrder( + assertThat(json.get("linkedElements")!!.elements().asSequence().map { it.toMap() }.toList(), containsInAnyOrder( mapOf("link" to "KSK@test.png", "loading" to "false", "failed" to "false"), mapOf("link" to "KSK@test.html", "loading" to "true", "failed" to "false"), mapOf("link" to "KSK@test.jpeg", "loading" to "false", "failed" to "true") )) } + @Test + fun `page can be created by dependency injection`() { + assertThat(baseInjector.createChildInjector( + ElementLoader::class.isProvidedByMock(), + TimeTextConverter::class.isProvidedByMock(), + L10nFilter::class.isProvidedByMock() + ).getInstance(), notNullValue()) + } + private fun JsonNode.toMap() = fields().asSequence().map { it.key!! to if (it.value.isNull) null else it.value.asText()!! }.toMap() }