1 package net.pterodactylus.sone.template
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
30 * Unit test for [SoneAccessor].
32 class SoneAccessorTest {
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>()
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)
53 fun setupTemplateContext() {
54 whenever(templateContext["currentSone"]).thenReturn(currentSone)
57 private fun assertAccessorReturnValue(member: String, expected: Any?) {
58 assertThat(accessor.get(templateContext, sone, member), equalTo(expected))
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)
67 fun `accessor returns nice name of a sone`() {
68 assertAccessorReturnValue("niceName", "sone-name")
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)
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)
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)
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)
95 fun `accessor returns that the given sone is not the current sone if it is not`() {
96 assertAccessorReturnValue("current", false)
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)
106 fun `accessor returns that a sone was not modified if the sone was not modified`() {
107 assertAccessorReturnValue("modified", false)
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)
117 fun `accessor returns the sone’s status`() {
118 val soneStatus = mock<SoneStatus>()
119 whenever(sone.status).thenReturn(soneStatus)
120 assertAccessorReturnValue("status", soneStatus)
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)
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)
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)
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)
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)
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)
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)
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)
172 fun `accessor returns that the sone is new if it is not known`() {
173 assertAccessorReturnValue("new", true)
177 fun `accessor returns that the sone is not new if it is known`() {
178 whenever(sone.isKnown).thenReturn(true)
179 assertAccessorReturnValue("new", false)
183 fun `accessor returns that the sone is not locked if it is not locked`() {
184 assertAccessorReturnValue("locked", false)
188 fun `accessor returns that the sone is locked if it is locked`() {
189 whenever(core.isLocked(sone)).thenReturn(true)
190 assertAccessorReturnValue("locked", true)
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)))
201 fun `accessor returns null trust if there is no current sone`() {
202 whenever(templateContext["currentSone"]).thenReturn(null)
203 assertAccessorReturnValue("trust", null)
207 fun `accessor returns trust with null values if there is no trust from the current sone`() {
208 assertAccessorReturnValue("trust", Trust(null, null, null))
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)
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]))
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)
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()))
243 fun `reflection accessor is used for other members`() {
244 assertAccessorReturnValue("hashCode", sone.hashCode())