Replace Sone provider interface with Kotlin version
[Sone.git] / src / test / kotlin / net / pterodactylus / sone / template / RenderFilterTest.kt
index 2d7e6a5..0920280 100644 (file)
@@ -1,15 +1,14 @@
 package net.pterodactylus.sone.template
 
-import com.google.common.base.Optional
 import net.pterodactylus.sone.core.Core
 import net.pterodactylus.sone.data.Post
 import net.pterodactylus.sone.data.Profile
 import net.pterodactylus.sone.data.Sone
+import net.pterodactylus.sone.test.mock
 import net.pterodactylus.sone.text.FreemailPart
 import net.pterodactylus.sone.text.FreenetLinkPart
 import net.pterodactylus.sone.text.LinkPart
 import net.pterodactylus.sone.text.Part
-import net.pterodactylus.sone.text.PartContainer
 import net.pterodactylus.sone.text.PlainTextPart
 import net.pterodactylus.sone.text.PostPart
 import net.pterodactylus.sone.text.SonePart
@@ -22,10 +21,8 @@ import org.hamcrest.Matchers.containsInAnyOrder
 import org.jsoup.Jsoup
 import org.jsoup.nodes.Attribute
 import org.jsoup.nodes.Element
-import org.jsoup.nodes.TextNode
 import org.junit.Test
 import org.mockito.Mockito.`when`
-import org.mockito.Mockito.mock
 import java.net.URLEncoder
 
 /**
@@ -40,7 +37,7 @@ class RenderFilterTest {
                private const val POST_ID = "37a06250-6775-4b94-86ff-257ba690953c"
        }
 
-       private val core = mock(Core::class.java)
+       private val core = mock<Core>()
        private val templateContextFactory = TemplateContextFactory()
        private val templateContext: TemplateContext
        private val sone = setupSone(SONE_IDENTITY, "Sone", "First")
@@ -54,78 +51,14 @@ class RenderFilterTest {
        private val filter = RenderFilter(core, templateContextFactory)
 
        @Test
-       fun plainTextIsRenderedCorrectly() {
+       fun `plain text part is rendered correctly`() {
                assertThat(renderParts(PlainTextPart("plain text")), `is`("plain text"))
        }
 
        private fun renderParts(vararg part: Part) = filter.format(templateContext, listOf(*part), parameters) as String
 
        @Test
-       fun plainTextPartIsShortenedIfLengthExceedsMaxLength() {
-               setLengthAndCutOffLength(15, 10)
-               assertThat(renderParts(PlainTextPart("This is a long text.")), `is`("This is a &hellip;"))
-       }
-
-       @Test
-       fun plainTextPartIsNotShortenedIfLengthDoesNotExceedMaxLength() {
-               setLengthAndCutOffLength(20, 10)
-               assertThat(renderParts(PlainTextPart("This is a long text.")), `is`("This is a &hellip;"))
-       }
-
-       @Test
-       fun shortPartsAreNotShortened() {
-               setLengthAndCutOffLength(15, 10)
-               assertThat(renderParts(PlainTextPart("This.")), `is`("This."))
-       }
-
-       @Test
-       fun multiplePlainTextPartsAreShortened() {
-               setLengthAndCutOffLength(15, 10)
-               assertThat(renderParts(PlainTextPart("This "), PlainTextPart("is a long text.")), `is`("This is a &hellip;"))
-       }
-
-       @Test
-       fun partsAfterLengthHasBeenReachedAreIgnored() {
-               setLengthAndCutOffLength(15, 10)
-               assertThat(renderParts(PlainTextPart("This is a long text."), PlainTextPart(" And even more.")), `is`("This is a &hellip;"))
-       }
-
-       @Test
-       fun linkPartsAreNotShortened() {
-               setLengthAndCutOffLength(15, 10)
-               val linkNode = Jsoup.parseBodyFragment(renderParts(FreenetLinkPart("KSK@gpl.txt", "This is a long text.", false))).body().child(0)
-               verifyLink(linkNode, "/KSK@gpl.txt", "freenet", "KSK@gpl.txt", "This is a long text.")
-       }
-
-       @Test
-       fun additionalLinkPartsAreIgnored() {
-               setLengthAndCutOffLength(15, 10)
-               assertThat(renderParts(PlainTextPart("This is a long text."), FreenetLinkPart("KSK@gpl.txt", "This is a long text.", false)), `is`("This is a &hellip;"))
-       }
-
-       private fun setLengthAndCutOffLength(length: Int, cutOffLength: Int) {
-               parameters.put("length", length)
-               parameters.put("cut-off-length", cutOffLength)
-       }
-
-       @Test
-       fun sonePartsAreAddedButTheirLengthIsIgnored() {
-               setLengthAndCutOffLength(15, 10)
-               val body = Jsoup.parseBodyFragment(renderParts(SonePart(sone), PlainTextPart("This is a long text."))).body()
-               val linkNode = body.childNode(0) as Element
-               println(linkNode)
-               verifyLink(linkNode, "viewSone.html?sone=$SONE_IDENTITY", "in-sone", "First", "First")
-               assertThat((body.childNode(1) as TextNode).text(), `is`("This is a …"))
-       }
-
-       @Test
-       fun additionalSonePartsAreIgnored() {
-               setLengthAndCutOffLength(15, 10)
-               assertThat(renderParts(PlainTextPart("This is a long text."), SonePart(sone)), `is`("This is a &hellip;"))
-       }
-
-       @Test
-       fun freenetLinkIsRenderedCorrectly() {
+       fun `freenet link is rendered correctly`() {
                val linkNode = renderParts(FreenetLinkPart("KSK@gpl.txt", "gpl.txt", false)).toLinkNode()
                verifyLink(linkNode, "/KSK@gpl.txt", "freenet", "KSK@gpl.txt", "gpl.txt")
        }
@@ -141,7 +74,7 @@ class RenderFilterTest {
        }
 
        @Test
-       fun trustedFreenetLinkIsRenderedWithCorrectCssClass() {
+       fun `trusted freenet link is rendered with correct css class`() {
                val linkNode = renderParts(FreenetLinkPart("KSK@gpl.txt", "gpl.txt", true)).toLinkNode()
                verifyLink(linkNode, "/KSK@gpl.txt", "freenet-trusted", "KSK@gpl.txt", "gpl.txt")
        }
@@ -149,44 +82,44 @@ class RenderFilterTest {
        private fun String.toLinkNode() = Jsoup.parseBodyFragment(this).body().child(0)
 
        @Test
-       fun internetLinkIsRenderedCorrectly() {
+       fun `internet link is rendered correctly`() {
                val linkNode = renderParts(LinkPart("http://test.com/test.html", "test.com/test.html")).toLinkNode()
                verifyLink(linkNode, "/external-link/?_CHECKED_HTTP_=${URLEncoder.encode("http://test.com/test.html", "UTF-8")}", "internet",
                                "http://test.com/test.html", "test.com/test.html")
        }
 
        @Test
-       fun sonePartsAreRenderedCorrectly() {
+       fun `sone parts are rendered correctly`() {
                val linkNode = renderParts(SonePart(sone)).toLinkNode()
                verifyLink(linkNode, "viewSone.html?sone=" + SONE_IDENTITY, "in-sone", "First", "First")
        }
 
        private fun setupSone(identity: String, name: String?, firstName: String): Sone {
-               val sone = mock<Sone>(Sone::class.java)
+               val sone = mock<Sone>()
                `when`(sone.id).thenReturn(identity)
                `when`(sone.profile).thenReturn(Profile(sone))
                `when`(sone.name).thenReturn(name)
                sone.profile.firstName = firstName
-               `when`(core.getSone(identity)).thenReturn(Optional.of<Sone>(sone))
+               `when`(core.getSone(identity)).thenReturn(sone)
                return sone
        }
 
        @Test
-       fun sonePartsWithUnknownSoneIsRenderedAsLinkToWebOfTrust() {
+       fun `sone part with unknown sone is rendered as link to web of trust`() {
                val sone = setupSone(SONE_IDENTITY, null, "First")
                val linkNode = renderParts(SonePart(sone)).toLinkNode()
                verifyLink(linkNode, "/WebOfTrust/ShowIdentity?id=$SONE_IDENTITY", "in-sone", SONE_IDENTITY, SONE_IDENTITY)
        }
 
        @Test
-       fun postPartIsCutOffCorrectlyWhenThereAreSpaces() {
+       fun `post part is cut off correctly when there are spaces`() {
                val post = setupPost(sone, "1234 678901 345 789012 45678 01.")
                val linkNode = renderParts(PostPart(post)).toLinkNode()
                verifyLink(linkNode, "viewPost.html?post=$POST_ID", "in-sone", "First", "1234 678901 345…")
        }
 
        private fun setupPost(sone: Sone, value: String): Post {
-               val post = mock<Post>(Post::class.java)
+               val post = mock<Post>()
                `when`(post.id).thenReturn(POST_ID)
                `when`(post.sone).thenReturn(sone)
                `when`(post.text).thenReturn(value)
@@ -194,29 +127,27 @@ class RenderFilterTest {
        }
 
        @Test
-       fun postPartIsCutOffCorrectlyWhenThereAreNoSpaces() {
+       fun `post part is cut off correctly when there are no spaces`() {
                val post = setupPost(sone, "1234567890123456789012345678901.")
                val linkNode = renderParts(PostPart(post)).toLinkNode()
                verifyLink(linkNode, "viewPost.html?post=$POST_ID", "in-sone", "First", "12345678901234567890…")
        }
 
        @Test
-       fun postPartShorterThan21CharsIsNotCutOff() {
+       fun `post part shorter than 21 chars is not cut off`() {
                val post = setupPost(sone, "12345678901234567890")
                val linkNode = renderParts(PostPart(post)).toLinkNode()
                verifyLink(linkNode, "viewPost.html?post=$POST_ID", "in-sone", "First", "12345678901234567890")
        }
 
        @Test
-       fun multiplePartsAreRenderedCorrectly() {
-               val parts = PartContainer()
-               parts.add(PlainTextPart("te"))
-               parts.add(PlainTextPart("xt"))
-               assertThat(renderParts(parts), `is`("text"))
+       fun `multiple parts are rendered correctly`() {
+               val parts = arrayOf(PlainTextPart("te"), PlainTextPart("xt"))
+               assertThat(renderParts(*parts), `is`("text"))
        }
 
        @Test
-       fun freemailAddressIsDisplayedCorrectly() {
+       fun `freemail address is displayed correctly`() {
                val linkNode = renderParts(FreemailPart("sone", FREEMAIL_ID, SONE_IDENTITY)).toLinkNode()
                verifyLink(linkNode, "/Freemail/NewMessage?to=$SONE_IDENTITY", "in-sone", "First\n$SONE_FREEMAIL", "sone@First.freemail")
        }