72e1343fd59503a503946b57e8fab0205181e0fb
[Sone.git] / src / test / kotlin / net / pterodactylus / sone / template / PostAccessorTest.kt
1 package net.pterodactylus.sone.template
2
3 import net.pterodactylus.sone.core.*
4 import net.pterodactylus.sone.data.*
5 import net.pterodactylus.sone.test.*
6 import net.pterodactylus.util.template.*
7 import org.hamcrest.MatcherAssert.*
8 import org.hamcrest.Matchers.*
9 import org.junit.*
10
11 /**
12  * Unit test for [PostAccessor].
13  */
14 class PostAccessorTest {
15
16         private val core = mock<Core>()
17         private val accessor = PostAccessor(core)
18         private val post = mock<Post>()
19         private val now = System.currentTimeMillis()
20
21         @Before
22         fun setupPost() {
23                 whenever(post.id).thenReturn("post-id")
24         }
25
26         @Test
27         fun `accessor returns the correct replies`() {
28                 val replies = listOf(
29                                 createPostReply(2000),
30                                 createPostReply(-1000),
31                                 createPostReply(-2000),
32                                 createPostReply(-3000),
33                                 createPostReply(-4000)
34                 )
35                 whenever(core.getReplies("post-id")).thenReturn(replies)
36                 val repliesForPost = accessor[null, post, "replies"] as Collection<PostReply>
37                 assertThat(repliesForPost, contains(
38                                 replies[1],
39                                 replies[2],
40                                 replies[3],
41                                 replies[4]
42                 ))
43         }
44
45         private fun createPostReply(timeOffset: Long) = mock<PostReply>().apply {
46                 whenever(time).thenReturn(now + timeOffset)
47         }
48
49         @Test
50         fun `accessor returns the liking sones`() {
51                 val sones = setOf<Sone>()
52                 whenever(core.getLikes(post)).thenReturn(sones)
53                 val likingSones = accessor[null, post, "likes"] as Set<Sone>
54                 assertThat(likingSones, equalTo(sones))
55         }
56
57         @Test
58         fun `accessor returns whether the current sone liked a post`() {
59                 val sone = mock<Sone>()
60                 whenever(sone.isLikedPostId("post-id")).thenReturn(true)
61                 val templateContext = TemplateContext()
62                 templateContext["currentSone"] = sone
63                 assertThat(accessor[templateContext, post, "liked"], equalTo<Any>(true))
64         }
65
66         @Test
67         fun `accessor returns false if post is not liked`() {
68                 val sone = mock<Sone>()
69                 val templateContext = TemplateContext()
70                 templateContext["currentSone"] = sone
71                 assertThat(accessor[templateContext, post, "liked"], equalTo<Any>(false))
72         }
73
74         @Test
75         fun `accessor returns false if there is no current sone`() {
76                 val templateContext = TemplateContext()
77                 assertThat(accessor[templateContext, post, "liked"], equalTo<Any>(false))
78         }
79
80         @Test
81         fun `accessor returns that not known post is new`() {
82                 assertThat(accessor[null, post, "new"], equalTo<Any>(true))
83         }
84
85         @Test
86         fun `accessor returns that known post is not new`() {
87                 whenever(post.isKnown).thenReturn(true)
88                 assertThat(accessor[null, post, "new"], equalTo<Any>(false))
89         }
90
91         @Test
92         fun `accessor returns if post is bookmarked`() {
93                 whenever(core.isBookmarked(post)).thenReturn(true)
94                 assertThat(accessor[null, post, "bookmarked"], equalTo<Any>(true))
95         }
96
97         @Test
98         fun `reply sone for remote post without replies is current sone`() {
99                 val post = mockPostFrom(remoteSone)
100                 assertThat(accessor[templateContext, post, "replySone"], equalTo<Any>(currentSone))
101         }
102
103         @Test
104         fun `reply sone for remote post with remote replies is current sone`() {
105                 val post = mockPostFrom(remoteSone)
106                 val replies = listOf(mockReplyFrom(remoteSone), mockReplyFrom(remoteSone))
107                 whenever(core.getReplies("post-id")).thenReturn(replies)
108                 assertThat(accessor[templateContext, post, "replySone"], equalTo<Any>(currentSone))
109         }
110
111         @Test
112         fun `reply sone for remote post with remote and one local replies is sone of local reply`() {
113                 val post = mockPostFrom(remoteSone)
114                 val localSone = mockLocalSone()
115                 val replies = listOf(mockReplyFrom(remoteSone), mockReplyFrom(localSone))
116                 whenever(core.getReplies("post-id")).thenReturn(replies)
117                 assertThat(accessor[templateContext, post, "replySone"], equalTo<Any>(localSone))
118         }
119
120         @Test
121         fun `reply sone for remote post with remote and several local replies is sone of latest local reply`() {
122                 val post = mockPostFrom(remoteSone)
123                 val localSone1 = mockLocalSone()
124                 val localSone2 = mockLocalSone()
125                 val replies = listOf(mockReplyFrom(remoteSone), mockReplyFrom(localSone1), mockReplyFrom(localSone2))
126                 whenever(core.getReplies("post-id")).thenReturn(replies)
127                 assertThat(accessor[templateContext, post, "replySone"], equalTo<Any>(localSone2))
128         }
129
130         @Test
131         fun `reply sone for local post without replies is post sone`() {
132                 val localSone = mockLocalSone()
133                 val post = mockPostFrom(localSone)
134                 assertThat(accessor[templateContext, post, "replySone"], equalTo<Any>(localSone))
135         }
136
137         @Test
138         fun `reply sone for local post with remote replies is local sone`() {
139                 val localSone = mockLocalSone()
140                 val post = mockPostFrom(localSone)
141                 val replies = listOf(mockReplyFrom(remoteSone), mockReplyFrom(remoteSone))
142                 whenever(core.getReplies("post-id")).thenReturn(replies)
143                 assertThat(accessor[templateContext, post, "replySone"], equalTo<Any>(localSone))
144         }
145
146         @Test
147         fun `reply sone for local post with remote and one local replies is local reply sone`() {
148                 val localSone1 = mockLocalSone()
149                 val post = mockPostFrom(localSone1)
150                 val localSone2 = mockLocalSone()
151                 val replies = listOf(mockReplyFrom(remoteSone), mockReplyFrom(localSone2))
152                 whenever(core.getReplies("post-id")).thenReturn(replies)
153                 assertThat(accessor[templateContext, post, "replySone"], equalTo<Any>(localSone2))
154         }
155
156         @Test
157         fun `reply sone for local post with remote and several local replies is latest local reply sone`() {
158                 val localSone1 = mockLocalSone()
159                 val post = mockPostFrom(localSone1)
160                 val localSone2 = mockLocalSone()
161                 val localSone3 = mockLocalSone()
162                 val replies = listOf(mockReplyFrom(remoteSone), mockReplyFrom(localSone2), mockReplyFrom(localSone3))
163                 whenever(core.getReplies("post-id")).thenReturn(replies)
164                 assertThat(accessor[templateContext, post, "replySone"], equalTo<Any>(localSone3))
165         }
166
167         @Test
168         fun `accessor returns other properties`() {
169                 assertThat(accessor[null, post, "hashCode"], equalTo<Any>(post.hashCode()))
170         }
171
172 }
173
174 private val currentSone = mock<Sone>()
175 private val remoteSone = mock<Sone>()
176 private fun mockLocalSone() = mock<Sone>().apply { whenever(isLocal).thenReturn(true) }
177
178 private val templateContext = TemplateContext().apply {
179         this["currentSone"] = currentSone
180 }
181
182 private fun mockPostFrom(sone: Sone) = mock<Post>().apply {
183         whenever(id).thenReturn("post-id")
184         whenever(this.sone).thenReturn(sone)
185 }
186
187 private fun mockReplyFrom(sone: Sone) = mock<PostReply>().apply { whenever(this.sone).thenReturn(sone) }