1 package net.pterodactylus.sone.web.ajax
3 import com.fasterxml.jackson.databind.JsonNode
4 import net.pterodactylus.sone.core.LinkedElement
5 import net.pterodactylus.sone.template.LinkedElementRenderFilter
6 import net.pterodactylus.sone.test.mock
7 import net.pterodactylus.sone.test.whenever
8 import net.pterodactylus.sone.utils.jsonArray
9 import net.pterodactylus.util.template.TemplateContext
10 import org.hamcrest.MatcherAssert.assertThat
11 import org.hamcrest.Matchers
12 import org.hamcrest.Matchers.equalTo
14 import org.mockito.ArgumentMatchers
17 * Unit test for [GetLinkedElementAjaxPage].
19 class GetLinkedElementAjaxPageTest: JsonPageTest("getLinkedElement.ajax", requiresLogin = false, needsFormPassword = false) {
21 private val linkedElementRenderFilter = mock<LinkedElementRenderFilter>()
22 override var page: JsonPage = GetLinkedElementAjaxPage(webInterface, elementLoader, linkedElementRenderFilter)
25 fun `only loaded linked elements are returned`() {
26 addRequestParameter("elements", jsonArray("KSK@foo.png", "KSK@foo.jpg", "KSK@foo.html").toString())
27 addLinkedElement("KSK@foo.png", true, false)
28 addLinkedElement("KSK@foo.jpg", false, false)
29 addLinkedElement("KSK@foo.html", false, true)
30 whenever(linkedElementRenderFilter.format(ArgumentMatchers.any<TemplateContext>(), ArgumentMatchers.any(), ArgumentMatchers.any())).thenAnswer { invocation ->
31 when (invocation.getArgument<LinkedElement>(1).link) {
32 "KSK@foo.jpg" -> "jpeg-image"
33 "KSK@foo.html" -> "html-page"
37 assertThat(json.get("linkedElements")!!.elements().asSequence().map { it.toMap() }.toList(), Matchers.containsInAnyOrder(
38 mapOf<String, String?>("link" to "KSK@foo.jpg", "html" to "jpeg-image"),
39 mapOf("link" to "KSK@foo.html", "html" to "html-page")
43 private fun JsonNode.toMap() = fields().asSequence().map { it.key!! to if (it.value.isNull) null else it.value.asText()!! }.toMap()