1 package net.pterodactylus.sone.web
3 import net.pterodactylus.sone.data.Album
4 import net.pterodactylus.sone.data.Image
5 import net.pterodactylus.sone.data.Post
6 import net.pterodactylus.sone.data.PostReply
7 import net.pterodactylus.sone.data.Profile
8 import net.pterodactylus.sone.data.Sone
9 import net.pterodactylus.sone.freenet.wot.Identity
10 import net.pterodactylus.sone.freenet.wot.OwnIdentity
11 import net.pterodactylus.sone.test.mock
12 import net.pterodactylus.sone.test.whenever
13 import org.hamcrest.MatcherAssert.assertThat
14 import org.hamcrest.Matchers.contains
15 import org.junit.Before
19 * Unit test for [KnownSonesPage].
21 class KnownSonesPageTest : WebPageTest() {
23 private val page = KnownSonesPage(template, webInterface)
25 private val sones = listOf(
26 createSone(1000, 4, 7, 2, "sone2", true, true),
27 createSone(2000, 3, 2, 3, "Sone1", false, true),
28 createSone(3000, 3, 8, 1, "Sone3", true, false),
29 createSone(4000, 1, 6, 0, "sone0", false, false)
34 addSone("sone1", sones[0])
35 addSone("sone2", sones[1])
36 addSone("sone3", sones[2])
37 addSone("sone4", sones[3])
40 private fun createSone(time: Long, posts: Int, replies: Int, images: Int, name: String, local: Boolean, new: Boolean) = mock<Sone>().apply {
41 whenever(identity).thenReturn(if (local) mock<OwnIdentity>() else mock<Identity>())
42 whenever(isKnown).thenReturn(!new)
43 whenever(this.time).thenReturn(time)
44 whenever(this.posts).thenReturn((0..(posts - 1)).map { mock<Post>() })
45 whenever(this.replies).thenReturn((0..(replies - 1)).map { mock<PostReply>() }.toSet())
46 val album = mock<Album>()
47 whenever(album.images).thenReturn(((0..(images - 1)).map { mock<Image>() }))
48 val rootAlbum = mock<Album>().apply {
49 whenever(albums).thenReturn(listOf(album))
51 whenever(this.rootAlbum).thenReturn(rootAlbum)
52 whenever(this.profile).thenReturn(mock<Profile>())
53 whenever(id).thenReturn(name.toLowerCase())
54 whenever(this.name).thenReturn(name)
57 private fun verifySonesAreInOrder(vararg indices: Int) {
58 @Suppress("UNCHECKED_CAST")
59 assertThat(templateContext["knownSones"] as Iterable<Sone>, contains(
60 *indices.map { sones[it] }.toTypedArray()
65 fun `default known sones are sorted newest first`() {
66 page.handleRequest(freenetRequest, templateContext)
67 verifySonesAreInOrder(3, 2, 1, 0)
71 fun `known sones can be sorted by oldest first`() {
72 addHttpRequestParameter("order", "asc")
73 page.handleRequest(freenetRequest, templateContext)
74 verifySonesAreInOrder(0, 1, 2, 3)
78 fun `known sones can be sorted by posts, most posts first`() {
79 addHttpRequestParameter("sort", "posts")
80 page.handleRequest(freenetRequest, templateContext)
81 verifySonesAreInOrder(0, 2, 1, 3)
85 fun `known sones can be sorted by posts, least posts first`() {
86 addHttpRequestParameter("sort", "posts")
87 addHttpRequestParameter("order", "asc")
88 page.handleRequest(freenetRequest, templateContext)
89 verifySonesAreInOrder(3, 1, 2, 0)
93 fun `known sones can be sorted by images, most images first`() {
94 addHttpRequestParameter("sort", "images")
95 page.handleRequest(freenetRequest, templateContext)
96 verifySonesAreInOrder(1, 0, 2, 3)
100 fun `known sones can be sorted by images, least images first`() {
101 addHttpRequestParameter("sort", "images")
102 addHttpRequestParameter("order", "asc")
103 page.handleRequest(freenetRequest, templateContext)
104 verifySonesAreInOrder(3, 2, 0, 1)
108 fun `known sones can be sorted by nice name, ascending`() {
109 addHttpRequestParameter("sort", "name")
110 addHttpRequestParameter("order", "asc")
111 page.handleRequest(freenetRequest, templateContext)
112 verifySonesAreInOrder(3, 1, 0, 2)
116 fun `known sones can be sorted by nice name, descending`() {
117 addHttpRequestParameter("sort", "name")
118 page.handleRequest(freenetRequest, templateContext)
119 verifySonesAreInOrder(2, 0, 1, 3)
123 fun `known sones can be filtered by local sones`() {
124 addHttpRequestParameter("filter", "own")
125 page.handleRequest(freenetRequest, templateContext)
126 verifySonesAreInOrder(2, 0)
130 fun `known sones can be filtered by non-local sones`() {
131 addHttpRequestParameter("filter", "not-own")
132 page.handleRequest(freenetRequest, templateContext)
133 verifySonesAreInOrder(3, 1)
137 fun `known sones can be filtered by new sones`() {
138 addHttpRequestParameter("filter", "new")
139 page.handleRequest(freenetRequest, templateContext)
140 verifySonesAreInOrder(1, 0)
144 fun `known sones can be filtered by known sones`() {
145 addHttpRequestParameter("filter", "not-new")
146 page.handleRequest(freenetRequest, templateContext)
147 verifySonesAreInOrder(3, 2)
151 fun `known sones can be filtered by followed sones`() {
152 addHttpRequestParameter("filter", "followed")
153 listOf("sone1", "sone3").forEach { whenever(currentSone.hasFriend(it)).thenReturn(true) }
154 page.handleRequest(freenetRequest, templateContext)
155 verifySonesAreInOrder(2, 1)
159 fun `known sones can be filtered by not-followed sones`() {
160 addHttpRequestParameter("filter", "not-followed")
161 listOf("sone1", "sone3").forEach { whenever(currentSone.hasFriend(it)).thenReturn(true) }
162 page.handleRequest(freenetRequest, templateContext)
163 verifySonesAreInOrder(3, 0)
167 fun `known sones can not be filtered by followed sones if there is no current sone`() {
168 addHttpRequestParameter("filter", "followed")
170 page.handleRequest(freenetRequest, templateContext)
171 verifySonesAreInOrder(3, 2, 1, 0)
175 fun `known sones can not be filtered by not-followed sones if there is no current sone`() {
176 addHttpRequestParameter("filter", "not-followed")
178 page.handleRequest(freenetRequest, templateContext)
179 verifySonesAreInOrder(3, 2, 1, 0)