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 private fun <T : Any> assertAccessorReturnValueMatches(member: String, matcher: Matcher<in T>) {
62 assertThat(accessor.get(templateContext, sone, member) as T, matcher)
66 fun `accessor returns nice name of a sone`() {
67 assertAccessorReturnValue("niceName", "sone-name")
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)
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)
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)
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)
94 fun `accessor returns that the given sone is not the current sone if it is not`() {
95 assertAccessorReturnValue("current", false)
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)
105 fun `accessor returns that a sone was not modified if the sone was not modified`() {
106 assertAccessorReturnValue("modified", false)
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)
116 fun `accessor returns the sone’s status`() {
117 val soneStatus = mock<SoneStatus>()
118 whenever(sone.status).thenReturn(soneStatus)
119 assertAccessorReturnValue("status", soneStatus)
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)
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)
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)
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)
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)
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)
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)
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)
171 fun `accessor returns that the sone is new if it is not known`() {
172 assertAccessorReturnValue("new", true)
176 fun `accessor returns that the sone is not new if it is known`() {
177 whenever(sone.isKnown).thenReturn(true)
178 assertAccessorReturnValue("new", false)
182 fun `accessor returns that the sone is not locked if it is not locked`() {
183 assertAccessorReturnValue("locked", false)
187 fun `accessor returns that the sone is locked if it is locked`() {
188 whenever(core.isLocked(sone)).thenReturn(true)
189 assertAccessorReturnValue("locked", true)
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)))
200 fun `accessor returns null trust if there is no current sone`() {
201 whenever(templateContext["currentSone"]).thenReturn(null)
202 assertAccessorReturnValue("trust", null)
206 fun `accessor returns trust with null values if there is no trust from the current sone`() {
207 assertAccessorReturnValue("trust", Trust(null, null, null))
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)
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]))
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)
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()))
242 fun `reflection accessor is used for other members`() {
243 assertAccessorReturnValue("hashCode", sone.hashCode())