Fix tests by parsing the created JSON reply
[Sone.git] / src / test / kotlin / net / pterodactylus / sone / template / ReplyAccessorTest.kt
1 package net.pterodactylus.sone.template
2
3 import net.pterodactylus.sone.core.Core
4 import net.pterodactylus.sone.data.PostReply
5 import net.pterodactylus.sone.data.Sone
6 import net.pterodactylus.sone.test.mock
7 import net.pterodactylus.sone.test.whenever
8 import net.pterodactylus.util.template.TemplateContext
9 import org.hamcrest.MatcherAssert.assertThat
10 import org.hamcrest.Matchers.equalTo
11 import org.junit.Before
12 import org.junit.Test
13
14 /**
15  * Unit test for [ReplyAccessor].
16  */
17 class ReplyAccessorTest {
18
19         private val core = mock<Core>()
20         private val accessor = ReplyAccessor(core)
21         private val templateContext = mock<TemplateContext>()
22         private val reply = mock<PostReply>()
23         private val currentSone = mock<Sone>()
24
25         @Before
26         fun setupReply() {
27                 whenever(reply.id).thenReturn("reply-id")
28         }
29
30         @Before
31         fun setupTemplateContext() {
32                 whenever(templateContext.get("currentSone")).thenReturn(currentSone)
33         }
34
35         @Test
36         fun `returns the likes correctly`() {
37                 val sones = setOf(mock<Sone>(), mock<Sone>(), mock<Sone>())
38                 whenever(core.getLikes(reply)).thenReturn(sones)
39                 assertThat(accessor.get(templateContext, reply, "likes"), equalTo<Any>(sones))
40         }
41
42         @Test
43         fun `returns that the reply is not liked if the current sone is null`() {
44                 whenever(templateContext.get("currentSone")).thenReturn(null)
45                 assertThat(accessor.get(templateContext, reply, "liked"), equalTo<Any>(false))
46         }
47
48         @Test
49         fun `returns that the reply is not liked if the current sone does not like the reply`() {
50                 assertThat(accessor.get(templateContext, reply, "liked"), equalTo<Any>(false))
51         }
52
53         @Test
54         fun `returns that the reply is liked if the current sone does like the reply`() {
55                 whenever(currentSone.isLikedReplyId("reply-id")).thenReturn(true)
56                 assertThat(accessor.get(templateContext, reply, "liked"), equalTo<Any>(true))
57         }
58
59         @Test
60         fun `returns that the reply is new if the reply is not known`() {
61                 assertThat(accessor.get(templateContext, reply, "new"), equalTo<Any>(true))
62         }
63
64         @Test
65         fun `returns that the reply is not new if the reply is known`() {
66                 whenever(reply.isKnown).thenReturn(true)
67                 assertThat(accessor.get(templateContext, reply, "new"), equalTo<Any>(false))
68         }
69
70         @Test
71         fun `return that a reply is not loaded if its sone is null`() {
72                 assertThat(accessor.get(templateContext, reply, "loaded"), equalTo<Any>(false))
73         }
74
75         @Test
76         fun `return that a reply is loaded if its sone is not null`() {
77                 whenever(reply.sone).thenReturn(mock<Sone>())
78                 assertThat(accessor.get(templateContext, reply, "loaded"), equalTo<Any>(true))
79         }
80
81         @Test
82         fun `reflection accessor is used for all other members`() {
83                 assertThat(accessor.get(templateContext, reply, "hashCode"), equalTo<Any>(reply.hashCode()))
84         }
85
86 }