Use time text converter to display “last updated” text
[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         private fun <T : Any> assertAccessorReturnValueMatches(member: String, matcher: Matcher<in T>) {
62                 assertThat(accessor.get(templateContext, sone, member) as T, matcher)
63         }
64
65         @Test
66         fun `accessor returns nice name of a sone`() {
67                 assertAccessorReturnValue("niceName", "sone-name")
68         }
69
70         @Test
71         fun `accessor returns that given sone is not a friend of the current sone if there is no current sone`() {
72                 whenever(templateContext["currentSone"]).thenReturn(null)
73                 assertAccessorReturnValue("friend", false)
74         }
75
76         @Test
77         fun `accessor returns that given sone is not a friend of the current sone if the given sone is not a friend`() {
78                 assertAccessorReturnValue("friend", false)
79         }
80
81         @Test
82         fun `accessor returns that given sone is a friend of the current sone if the given sone is a friend`() {
83                 whenever(currentSone.hasFriend("sone-id")).thenReturn(true)
84                 assertAccessorReturnValue("friend", true)
85         }
86
87         @Test
88         fun `accessor returns that the given sone is not the current sone if there is no current sone`() {
89                 whenever(templateContext["currentSone"]).thenReturn(null)
90                 assertAccessorReturnValue("current", false)
91         }
92
93         @Test
94         fun `accessor returns that the given sone is not the current sone if it is not`() {
95                 assertAccessorReturnValue("current", false)
96         }
97
98         @Test
99         fun `accessor returns that the given sone is the current sone if it is `() {
100                 whenever(templateContext["currentSone"]).thenReturn(sone)
101                 assertAccessorReturnValue("current", true)
102         }
103
104         @Test
105         fun `accessor returns that a sone was not modified if the sone was not modified`() {
106                 assertAccessorReturnValue("modified", false)
107         }
108
109         @Test
110         fun `accessor returns that a sone was modified if the sone was modified`() {
111                 whenever(core.isModifiedSone(sone)).thenReturn(true)
112                 assertAccessorReturnValue("modified", true)
113         }
114
115         @Test
116         fun `accessor returns the sone’s status`() {
117                 val soneStatus = mock<SoneStatus>()
118                 whenever(sone.status).thenReturn(soneStatus)
119                 assertAccessorReturnValue("status", soneStatus)
120         }
121
122         @Test
123         fun `accessor returns that the sone’s status is unknown if it is unknown`() {
124                 whenever(sone.status).thenReturn(unknown)
125                 assertAccessorReturnValue("unknown", true)
126         }
127
128         @Test
129         fun `accessor returns that the sone’s status is not unknown if it is not unknown`() {
130                 whenever(sone.status).thenReturn(mock<SoneStatus>())
131                 assertAccessorReturnValue("unknown", false)
132         }
133
134         @Test
135         fun `accessor returns that the sone’s status is idle if it is idle`() {
136                 whenever(sone.status).thenReturn(idle)
137                 assertAccessorReturnValue("idle", true)
138         }
139
140         @Test
141         fun `accessor returns that the sone’s status is not idle if it is not idle`() {
142                 whenever(sone.status).thenReturn(mock<SoneStatus>())
143                 assertAccessorReturnValue("idle", false)
144         }
145
146         @Test
147         fun `accessor returns that the sone’s status is inserting if it is inserting`() {
148                 whenever(sone.status).thenReturn(inserting)
149                 assertAccessorReturnValue("inserting", true)
150         }
151
152         @Test
153         fun `accessor returns that the sone’s status is not inserting if it is not inserting`() {
154                 whenever(sone.status).thenReturn(mock<SoneStatus>())
155                 assertAccessorReturnValue("inserting", false)
156         }
157
158         @Test
159         fun `accessor returns that the sone’s status is downloading if it is downloading`() {
160                 whenever(sone.status).thenReturn(downloading)
161                 assertAccessorReturnValue("downloading", true)
162         }
163
164         @Test
165         fun `accessor returns that the sone’s status is not downloading if it is not downloading`() {
166                 whenever(sone.status).thenReturn(mock<SoneStatus>())
167                 assertAccessorReturnValue("downloading", false)
168         }
169
170         @Test
171         fun `accessor returns that the sone is new if it is not known`() {
172                 assertAccessorReturnValue("new", true)
173         }
174
175         @Test
176         fun `accessor returns that the sone is not new if it is known`() {
177                 whenever(sone.isKnown).thenReturn(true)
178                 assertAccessorReturnValue("new", false)
179         }
180
181         @Test
182         fun `accessor returns that the sone is not locked if it is not locked`() {
183                 assertAccessorReturnValue("locked", false)
184         }
185
186         @Test
187         fun `accessor returns that the sone is locked if it is locked`() {
188                 whenever(core.isLocked(sone)).thenReturn(true)
189                 assertAccessorReturnValue("locked", true)
190         }
191
192         @Test
193         fun `accessor returns l10n text for last update time`() {
194                 whenever(sone.time).thenReturn(12345)
195                 whenever(timeTextConverter.getTimeText(12345L)).thenReturn(TimeText(L10nText("l10n.key", listOf(3L)), 23456))
196                 assertAccessorReturnValue("lastUpdatedText", L10nText("l10n.key", listOf(3L)))
197         }
198
199         @Test
200         fun `accessor returns null trust if there is no current sone`() {
201                 whenever(templateContext["currentSone"]).thenReturn(null)
202                 assertAccessorReturnValue("trust", null)
203         }
204
205         @Test
206         fun `accessor returns trust with null values if there is no trust from the current sone`() {
207                 assertAccessorReturnValue("trust", Trust(null, null, null))
208         }
209
210         @Test
211         fun `accessor returns trust if there is trust from the current sone`() {
212                 val trust = mock<Trust>()
213                 whenever(remoteIdentity.getTrust(currentIdentity)).thenReturn(trust)
214                 assertAccessorReturnValue("trust", trust)
215         }
216
217         @Test
218         fun `accessor returns all images in the correct order`() {
219                 val images = listOf(mock<Image>(), mock<Image>(), mock<Image>(), mock<Image>(), mock<Image>())
220                 val firstAlbum = createAlbum(listOf(), listOf(images[0], images[3]))
221                 val secondAlbum = createAlbum(listOf(), listOf(images[1], images[4], images[2]))
222                 val rootAlbum = createAlbum(listOf(firstAlbum, secondAlbum), listOf())
223                 whenever(sone.rootAlbum).thenReturn(rootAlbum)
224                 assertAccessorReturnValueMatches("allImages", contains(images[0], images[3], images[1], images[4], images[2]))
225         }
226
227         private fun createAlbum(albums: List<Album>, images: List<Image>) =
228                         mock<Album>().apply {
229                                 whenever(this.albums).thenReturn(albums)
230                                 whenever(this.images).thenReturn(images)
231                         }
232
233         @Test
234         fun `accessor returns all albums in the correct order`() {
235                 val albums = listOf(mock<Album>(), mock<Album>(), mock<Album>(), mock<Album>(), mock<Album>())
236                 val rootAlbum = createAlbum(albums, listOf())
237                 whenever(sone.rootAlbum).thenReturn(rootAlbum)
238                 assertAccessorReturnValueMatches("albums", contains(*albums.toTypedArray()))
239         }
240
241         @Test
242         fun `reflection accessor is used for other members`() {
243             assertAccessorReturnValue("hashCode", sone.hashCode())
244         }
245
246 }