87cfce0db594efa62167720e52f0a420fc998aed
[Sone.git] / src / test / kotlin / net / pterodactylus / sone / template / SoneAccessorTest.kt
1 package net.pterodactylus.sone.template
2
3 import net.pterodactylus.sone.core.Core
4 import net.pterodactylus.sone.data.Album
5 import net.pterodactylus.sone.data.Image
6 import net.pterodactylus.sone.data.Profile
7 import net.pterodactylus.sone.data.Sone
8 import net.pterodactylus.sone.data.Sone.SoneStatus
9 import net.pterodactylus.sone.data.Sone.SoneStatus.downloading
10 import net.pterodactylus.sone.data.Sone.SoneStatus.idle
11 import net.pterodactylus.sone.data.Sone.SoneStatus.inserting
12 import net.pterodactylus.sone.data.Sone.SoneStatus.unknown
13 import net.pterodactylus.sone.freenet.L10nText
14 import net.pterodactylus.sone.freenet.wot.Identity
15 import net.pterodactylus.sone.freenet.wot.OwnIdentity
16 import net.pterodactylus.sone.freenet.wot.Trust
17 import net.pterodactylus.sone.test.mock
18 import net.pterodactylus.sone.test.whenever
19 import net.pterodactylus.sone.text.TimeText
20 import net.pterodactylus.sone.text.TimeTextConverter
21 import net.pterodactylus.util.template.TemplateContext
22 import org.hamcrest.Matcher
23 import org.hamcrest.MatcherAssert.assertThat
24 import org.hamcrest.Matchers.contains
25 import org.hamcrest.Matchers.equalTo
26 import org.junit.Before
27 import org.junit.Test
28
29 /**
30  * Unit test for [SoneAccessor].
31  */
32 class SoneAccessorTest {
33
34         private val core = mock<Core>()
35         private val timeTextConverter = mock<TimeTextConverter>()
36         private val accessor = SoneAccessor(core, timeTextConverter)
37         private val templateContext = mock<TemplateContext>()
38         private val currentSone = mock<Sone>()
39         private val currentIdentity = mock<OwnIdentity>()
40         private val sone = mock<Sone>()
41         private val remoteIdentity = mock<Identity>()
42
43         @Before
44         fun setupSone() {
45                 whenever(sone.id).thenReturn("sone-id")
46                 whenever(sone.name).thenReturn("sone-name")
47                 whenever(sone.profile).thenReturn(Profile(sone))
48                 whenever(sone.identity).thenReturn(remoteIdentity)
49                 whenever(currentSone.identity).thenReturn(currentIdentity)
50         }
51
52         @Before
53         fun setupTemplateContext() {
54                 whenever(templateContext["currentSone"]).thenReturn(currentSone)
55         }
56
57         private fun assertAccessorReturnValue(member: String, expected: Any?) {
58                 assertThat(accessor.get(templateContext, sone, member), equalTo(expected))
59         }
60
61         @Suppress("UNCHECKED_CAST")
62         private fun <T : Any> assertAccessorReturnValueMatches(member: String, matcher: Matcher<in T>) {
63                 assertThat(accessor.get(templateContext, sone, member) as T, matcher)
64         }
65
66         @Test
67         fun `accessor returns nice name of a sone`() {
68                 assertAccessorReturnValue("niceName", "sone-name")
69         }
70
71         @Test
72         fun `accessor returns that given sone is not a friend of the current sone if there is no current sone`() {
73                 whenever(templateContext["currentSone"]).thenReturn(null)
74                 assertAccessorReturnValue("friend", false)
75         }
76
77         @Test
78         fun `accessor returns that given sone is not a friend of the current sone if the given sone is not a friend`() {
79                 assertAccessorReturnValue("friend", false)
80         }
81
82         @Test
83         fun `accessor returns that given sone is a friend of the current sone if the given sone is a friend`() {
84                 whenever(currentSone.hasFriend("sone-id")).thenReturn(true)
85                 assertAccessorReturnValue("friend", true)
86         }
87
88         @Test
89         fun `accessor returns that the given sone is not the current sone if there is no current sone`() {
90                 whenever(templateContext["currentSone"]).thenReturn(null)
91                 assertAccessorReturnValue("current", false)
92         }
93
94         @Test
95         fun `accessor returns that the given sone is not the current sone if it is not`() {
96                 assertAccessorReturnValue("current", false)
97         }
98
99         @Test
100         fun `accessor returns that the given sone is the current sone if it is `() {
101                 whenever(templateContext["currentSone"]).thenReturn(sone)
102                 assertAccessorReturnValue("current", true)
103         }
104
105         @Test
106         fun `accessor returns that a sone was not modified if the sone was not modified`() {
107                 assertAccessorReturnValue("modified", false)
108         }
109
110         @Test
111         fun `accessor returns that a sone was modified if the sone was modified`() {
112                 whenever(core.isModifiedSone(sone)).thenReturn(true)
113                 assertAccessorReturnValue("modified", true)
114         }
115
116         @Test
117         fun `accessor returns the sone’s status`() {
118                 val soneStatus = mock<SoneStatus>()
119                 whenever(sone.status).thenReturn(soneStatus)
120                 assertAccessorReturnValue("status", soneStatus)
121         }
122
123         @Test
124         fun `accessor returns that the sone’s status is unknown if it is unknown`() {
125                 whenever(sone.status).thenReturn(unknown)
126                 assertAccessorReturnValue("unknown", true)
127         }
128
129         @Test
130         fun `accessor returns that the sone’s status is not unknown if it is not unknown`() {
131                 whenever(sone.status).thenReturn(mock())
132                 assertAccessorReturnValue("unknown", false)
133         }
134
135         @Test
136         fun `accessor returns that the sone’s status is idle if it is idle`() {
137                 whenever(sone.status).thenReturn(idle)
138                 assertAccessorReturnValue("idle", true)
139         }
140
141         @Test
142         fun `accessor returns that the sone’s status is not idle if it is not idle`() {
143                 whenever(sone.status).thenReturn(mock())
144                 assertAccessorReturnValue("idle", false)
145         }
146
147         @Test
148         fun `accessor returns that the sone’s status is inserting if it is inserting`() {
149                 whenever(sone.status).thenReturn(inserting)
150                 assertAccessorReturnValue("inserting", true)
151         }
152
153         @Test
154         fun `accessor returns that the sone’s status is not inserting if it is not inserting`() {
155                 whenever(sone.status).thenReturn(mock())
156                 assertAccessorReturnValue("inserting", false)
157         }
158
159         @Test
160         fun `accessor returns that the sone’s status is downloading if it is downloading`() {
161                 whenever(sone.status).thenReturn(downloading)
162                 assertAccessorReturnValue("downloading", true)
163         }
164
165         @Test
166         fun `accessor returns that the sone’s status is not downloading if it is not downloading`() {
167                 whenever(sone.status).thenReturn(mock())
168                 assertAccessorReturnValue("downloading", false)
169         }
170
171         @Test
172         fun `accessor returns that the sone is new if it is not known`() {
173                 assertAccessorReturnValue("new", true)
174         }
175
176         @Test
177         fun `accessor returns that the sone is not new if it is known`() {
178                 whenever(sone.isKnown).thenReturn(true)
179                 assertAccessorReturnValue("new", false)
180         }
181
182         @Test
183         fun `accessor returns that the sone is not locked if it is not locked`() {
184                 assertAccessorReturnValue("locked", false)
185         }
186
187         @Test
188         fun `accessor returns that the sone is locked if it is locked`() {
189                 whenever(core.isLocked(sone)).thenReturn(true)
190                 assertAccessorReturnValue("locked", true)
191         }
192
193         @Test
194         fun `accessor returns l10n text for last update time`() {
195                 whenever(sone.time).thenReturn(12345)
196                 whenever(timeTextConverter.getTimeText(12345L)).thenReturn(TimeText(L10nText("l10n.key", listOf(3L)), 23456))
197                 assertAccessorReturnValue("lastUpdatedText", L10nText("l10n.key", listOf(3L)))
198         }
199
200         @Test
201         fun `accessor returns null trust if there is no current sone`() {
202                 whenever(templateContext["currentSone"]).thenReturn(null)
203                 assertAccessorReturnValue("trust", null)
204         }
205
206         @Test
207         fun `accessor returns trust with null values if there is no trust from the current sone`() {
208                 assertAccessorReturnValue("trust", Trust(null, null, null))
209         }
210
211         @Test
212         fun `accessor returns trust if there is trust from the current sone`() {
213                 val trust = mock<Trust>()
214                 whenever(remoteIdentity.getTrust(currentIdentity)).thenReturn(trust)
215                 assertAccessorReturnValue("trust", trust)
216         }
217
218         @Test
219         fun `accessor returns all images in the correct order`() {
220                 val images = listOf(mock<Image>(), mock(), mock(), mock(), mock())
221                 val firstAlbum = createAlbum(listOf(), listOf(images[0], images[3]))
222                 val secondAlbum = createAlbum(listOf(), listOf(images[1], images[4], images[2]))
223                 val rootAlbum = createAlbum(listOf(firstAlbum, secondAlbum), listOf())
224                 whenever(sone.rootAlbum).thenReturn(rootAlbum)
225                 assertAccessorReturnValueMatches("allImages", contains(images[0], images[3], images[1], images[4], images[2]))
226         }
227
228         private fun createAlbum(albums: List<Album>, images: List<Image>) =
229                         mock<Album>().apply {
230                                 whenever(this.albums).thenReturn(albums)
231                                 whenever(this.images).thenReturn(images)
232                         }
233
234         @Test
235         fun `accessor returns all albums in the correct order`() {
236                 val albums = listOf(mock<Album>(), mock(), mock(), mock(), mock())
237                 val rootAlbum = createAlbum(albums, listOf())
238                 whenever(sone.rootAlbum).thenReturn(rootAlbum)
239                 assertAccessorReturnValueMatches("albums", contains(*albums.toTypedArray()))
240         }
241
242         @Test
243         fun `reflection accessor is used for other members`() {
244             assertAccessorReturnValue("hashCode", sone.hashCode())
245         }
246
247 }