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.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
31 * Unit test for [SoneAccessor].
33 class SoneAccessorTest {
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>()
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)
54 fun setupTemplateContext() {
55 whenever(templateContext["currentSone"]).thenReturn(currentSone)
58 private fun assertAccessorReturnValue(member: String, expected: Any?) {
59 assertThat(accessor.get(templateContext, sone, member), equalTo(expected))
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)
68 fun `accessor returns nice name of a sone`() {
69 assertAccessorReturnValue("niceName", "sone-name")
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)
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)
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)
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)
96 fun `accessor returns that the given sone is not the current sone if it is not`() {
97 assertAccessorReturnValue("current", false)
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)
107 fun `accessor returns that a sone was not modified if the sone was not modified`() {
108 assertAccessorReturnValue("modified", false)
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)
118 fun `accessor returns the sone’s status`() {
119 val soneStatus = mock<SoneStatus>()
120 whenever(sone.status).thenReturn(soneStatus)
121 assertAccessorReturnValue("status", soneStatus)
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)
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)
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)
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)
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)
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)
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)
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)
173 fun `accessor returns that the sone is new if it is not known`() {
174 assertAccessorReturnValue("new", true)
178 fun `accessor returns that the sone is not new if it is known`() {
179 whenever(sone.isKnown).thenReturn(true)
180 assertAccessorReturnValue("new", false)
184 fun `accessor returns that the sone is not locked if it is not locked`() {
185 assertAccessorReturnValue("locked", false)
189 fun `accessor returns that the sone is locked if it is locked`() {
190 whenever(core.isLocked(sone)).thenReturn(true)
191 assertAccessorReturnValue("locked", true)
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)))
202 fun `accessor returns null trust if there is no current sone`() {
203 whenever(templateContext["currentSone"]).thenReturn(null)
204 assertAccessorReturnValue("trust", null)
208 fun `accessor returns trust with null values if there is no trust from the current sone`() {
209 assertAccessorReturnValue("trust", Trust(null, null, null))
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)
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]))
229 private fun createAlbum(albums: List<Album>, images: List<Image>) =
230 AlbumImpl(sone).also {
231 albums.forEach(it::addAlbum)
232 images.forEach(it::addImage)
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()))
244 fun `reflection accessor is used for other members`() {
245 assertAccessorReturnValue("hashCode", sone.hashCode())