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