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.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
27 * Unit test for [SoneAccessor].
29 class SoneAccessorTest {
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>()
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)
49 fun setupTemplateContext() {
50 whenever(templateContext["currentSone"]).thenReturn(currentSone)
53 private fun assertAccessorReturnValue(member: String, expected: Any?) {
54 assertThat(accessor.get(templateContext, sone, member), equalTo(expected))
57 private fun <T : Any> assertAccessorReturnValueMatches(member: String, matcher: Matcher<in T>) {
58 assertThat(accessor.get(templateContext, sone, member) as T, matcher)
62 fun `accessor returns nice name of a sone`() {
63 assertAccessorReturnValue("niceName", "sone-name")
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)
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)
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)
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)
90 fun `accessor returns that the given sone is not the current sone if it is not`() {
91 assertAccessorReturnValue("current", false)
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)
101 fun `accessor returns that a sone was not modified if the sone was not modified`() {
102 assertAccessorReturnValue("modified", false)
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)
112 fun `accessor returns the sone’s status`() {
113 val soneStatus = mock<SoneStatus>()
114 whenever(sone.status).thenReturn(soneStatus)
115 assertAccessorReturnValue("status", soneStatus)
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)
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)
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)
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)
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)
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)
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)
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)
167 fun `accessor returns that the sone is new if it is not known`() {
168 assertAccessorReturnValue("new", true)
172 fun `accessor returns that the sone is not new if it is known`() {
173 whenever(sone.isKnown).thenReturn(true)
174 assertAccessorReturnValue("new", false)
178 fun `accessor returns that the sone is not locked if it is not locked`() {
179 assertAccessorReturnValue("locked", false)
183 fun `accessor returns that the sone is locked if it is locked`() {
184 whenever(core.isLocked(sone)).thenReturn(true)
185 assertAccessorReturnValue("locked", true)
189 fun `accessor returns null trust if there is no current sone`() {
190 whenever(templateContext["currentSone"]).thenReturn(null)
191 assertAccessorReturnValue("trust", null)
195 fun `accessor returns trust with null values if there is no trust from the current sone`() {
196 assertAccessorReturnValue("trust", Trust(null, null, null))
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)
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]))
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)
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()))
231 fun `reflection accessor is used for other members`() {
232 assertAccessorReturnValue("hashCode", sone.hashCode())