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