package net.pterodactylus.sone.web import com.google.inject.Guice.* import freenet.client.* import freenet.clients.http.* import freenet.support.api.* import net.pterodactylus.sone.core.* import net.pterodactylus.sone.data.* import net.pterodactylus.sone.database.* import net.pterodactylus.sone.freenet.* import net.pterodactylus.sone.freenet.wot.* import net.pterodactylus.sone.main.* import net.pterodactylus.sone.template.* import net.pterodactylus.sone.test.* import net.pterodactylus.sone.text.* import net.pterodactylus.sone.web.notification.* import net.pterodactylus.sone.web.page.* import net.pterodactylus.util.notify.* import net.pterodactylus.util.template.* import net.pterodactylus.util.web.* import org.hamcrest.MatcherAssert.* import org.hamcrest.Matchers.* import java.util.* import kotlin.test.* class WebInterfaceModuleTest { private val webInterfaceModule = WebInterfaceModule() private val loaders = mock() private val translation = object : Translation { override val currentLocale = Locale.ENGLISH override fun translate(key: String) = if (key == "View.Sone.Text.UnknownDate") "unknown" else key } private val additionalModules = arrayOf( Core::class.isProvidedByMock(), SoneProvider::class.isProvidedByMock(), Translation::class.isProvidedBy(translation), SoneTextParser::class.isProvidedByMock(), ElementLoader::class.isProvidedByMock(), Loaders::class.isProvidedBy(loaders), HighLevelSimpleClient::class.isProvidedByMock(), SessionManager::class.isProvidedByMock() ) private val injector = createInjector(webInterfaceModule, *additionalModules)!! private val templateContext by lazy { injector.getInstance().createTemplateContext()!! } @Test fun `template context factory creates template with reflection accessor for objects`() { verifyAccessor() } @Test fun `template context factory creates template with collection accessor for collections`() { verifyAccessor, CollectionAccessor>() } @Test fun `template context contains sone accessor for sones`() { verifyAccessor() } @Test fun `template context contains post accessor for posts`() { verifyAccessor() } @Test fun `template context contains reply accessor for replies`() { verifyAccessor, ReplyAccessor>() } @Test fun `template context contains album accessor for albums`() { verifyAccessor() } @Test fun `template context contains image accessor for images`() { verifyAccessor() } @Test fun `template context contains identity accessor for identities`() { verifyAccessor() } @Test fun `template context contains trust accessor for trusts`() { verifyAccessor() } @Test fun `template context contains http request accessor for http requests`() { verifyAccessor() } @Test fun `template context contains profile accessor for profiles`() { verifyAccessor() } private inline fun verifyAccessor() { assertThat(templateContext.getAccessor(O::class.java), instanceOf(A::class.java)) } @Test fun `template context contains date filter`() { verifyFilter("date") } @Test fun `template context contains html filter`() { verifyFilter("html") } @Test fun `template context contains replace filter`() { verifyFilter("replace") } @Test fun `template context contains store filter`() { verifyFilter("store") } @Test fun `template context contains l10n filter`() { verifyFilter("l10n") } @Test fun `template context contains substring filter`() { verifyFilter("substring") } @Test fun `template context contains xml filter`() { verifyFilter("xml") } @Test fun `template context contains change filter`() { verifyFilter("change") } @Test fun `template context contains match filter`() { verifyFilter("match") } @Test fun `template context contains css filter`() { verifyFilter("css") } @Test fun `template context contains js filter`() { verifyFilter("js") } @Test fun `template context contains parser filter`() { verifyFilter("parse") } @Test fun `template context contains shorten filter`() { verifyFilter("shorten") } @Test fun `template context contains render filter`() { verifyFilter("render") } @Test fun `template context contains linked elements filter`() { verifyFilter("linked-elements") } @Test fun `template context contains linked elements render filter`() { verifyFilter("render-linked-element") } @Test fun `template context contains reparse filter`() { verifyFilter("reparse") } @Test fun `template context contains unknown date filter`() { verifyFilter("unknown") } @Test fun `unknown date filter uses correct l10n key`() { assertThat(getFilter("unknown")!!.format(null, 0L, emptyMap()), equalTo("unknown")) } @Test fun `template context contains format filter`() { verifyFilter("format") } @Test fun `template context contains duration format filter`() { verifyFilter("duration") } @Test fun `template context contains collection sort filter`() { verifyFilter("sort") } @Test fun `template context contains image link filter`() { verifyFilter("image-link") } @Test fun `template context contains reply group filter`() { verifyFilter("replyGroup") } @Test fun `template context contains contains filter`() { verifyFilter("in") } @Test fun `template context unique elements filter`() { verifyFilter("unique") } @Test fun `template context mod filter`() { verifyFilter("mod") } @Test fun `template context pagination filter`() { verifyFilter("paginate") } @Test fun `template context histogram renderer`() { verifyFilter("render-histogram") } private inline fun verifyFilter(name: String) { assertThat(getFilter(name), instanceOf(F::class.java)) } private fun getFilter(name: String): Filter? = templateContext.getFilter(name) @Test fun `template context factory is created as singleton`() { injector.verifySingletonInstance() } @Test fun `template from classpath is returned`() { val template = Template() templateContext["testTemplate"] = template assertThat(templateContext.getTemplate("testTemplate"), sameInstance(template)) } @Test fun `template from loaders’ provider is returned`() { val template = Template() whenever(loaders.templateProvider).thenReturn(TemplateProvider { _, templateName -> template.takeIf { templateName == "testTemplate" } }) assertThat(templateContext.getTemplate("testTemplate"), sameInstance(template)) } @Test fun `page toadlet factory is created with correct prefix`() { val page = mock>() assertThat(injector.getInstance().createPageToadlet(page).path(), startsWith("/Sone/")) } @Test fun `notification manager is created as singleton`() { injector.verifySingletonInstance() } }