3c735c2fd002d1f22bc993150db7bcf4133d7ed2
[Sone.git] / src / test / kotlin / net / pterodactylus / sone / template / ParserFilterTest.kt
1 package net.pterodactylus.sone.template
2
3 import com.google.common.base.Optional.of
4 import net.pterodactylus.sone.core.Core
5 import net.pterodactylus.sone.data.Sone
6 import net.pterodactylus.sone.test.mock
7 import net.pterodactylus.sone.text.SoneTextParser
8 import net.pterodactylus.sone.text.SoneTextParserContext
9 import net.pterodactylus.util.template.TemplateContext
10 import org.hamcrest.MatcherAssert.assertThat
11 import org.hamcrest.Matchers.`is`
12 import org.hamcrest.Matchers.emptyIterable
13 import org.junit.Test
14 import org.mockito.ArgumentCaptor.forClass
15 import org.mockito.Mockito.`when`
16 import org.mockito.Mockito.eq
17 import org.mockito.Mockito.verify
18
19 /**
20  * Unit test for [ParserFilter].
21  */
22 class ParserFilterTest {
23
24         companion object {
25                 private const val SONE_IDENTITY = "nwa8lHa271k2QvJ8aa0Ov7IHAV-DFOCFgmDt3X6BpCI"
26         }
27
28         private val core = mock<Core>()
29         private val sone = setupSone(SONE_IDENTITY)
30         private val soneTextParser = mock<SoneTextParser>()
31         private val templateContext = TemplateContext()
32         private val parameters = mutableMapOf<String, Any?>()
33         private val filter = ParserFilter(core, soneTextParser)
34
35         private fun setupSone(identity: String): Sone {
36                 val sone = mock<Sone>()
37                 `when`(sone.id).thenReturn(identity)
38                 `when`(core.getSone(identity)).thenReturn(of(sone))
39                 return sone
40         }
41
42         @Test
43         fun `parsing null returns an empty iterable`() {
44                 assertThat(filter.format(templateContext, null, mutableMapOf()) as Iterable<*>, emptyIterable())
45         }
46
47         @Test
48         fun `given sone is used to create parser context`() {
49                 setupSoneAndVerifyItIsUsedInContext(sone, sone)
50         }
51
52         @Test
53         fun `sone with given sone ID is used to create parser context`() {
54                 setupSoneAndVerifyItIsUsedInContext(SONE_IDENTITY, sone)
55         }
56
57         private fun setupSoneAndVerifyItIsUsedInContext(soneOrSoneId: Any, sone: Sone) {
58                 parameters.put("sone", soneOrSoneId)
59                 filter.format(templateContext, "text", parameters)
60                 val context = forClass(SoneTextParserContext::class.java)
61                 verify(soneTextParser).parse(eq<String>("text"), context.capture())
62                 assertThat(context.value.postingSone, `is`(sone))
63         }
64
65 }