Add some tests and fix names of tests
[Sone.git] / src / test / kotlin / net / pterodactylus / sone / web / KnownSonesPageTest.kt
1 package net.pterodactylus.sone.web
2
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
16 import org.junit.Test
17
18 /**
19  * Unit test for [KnownSonesPage].
20  */
21 class KnownSonesPageTest : WebPageTest() {
22
23         private val page = KnownSonesPage(template, webInterface)
24
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)
30         )
31
32         @Before
33         fun setupSones() {
34                 addSone("sone1", sones[0])
35                 addSone("sone2", sones[1])
36                 addSone("sone3", sones[2])
37                 addSone("sone4", sones[3])
38         }
39
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))
50                 }
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)
55         }
56
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()
61                 ))
62         }
63
64         @Test
65         fun `default known sones are sorted newest first`() {
66                 page.handleRequest(freenetRequest, templateContext)
67                 verifySonesAreInOrder(3, 2, 1, 0)
68         }
69
70         @Test
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)
75         }
76
77         @Test
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)
82         }
83
84         @Test
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)
90         }
91
92         @Test
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)
97         }
98
99         @Test
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)
105         }
106
107         @Test
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)
113         }
114
115         @Test
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)
120         }
121
122         @Test
123         fun `known sones can be filtered by local sones`() {
124                 addHttpRequestParameter("filter", "own")
125                 page.handleRequest(freenetRequest, templateContext)
126                 verifySonesAreInOrder(2, 0)
127         }
128
129         @Test
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)
134         }
135
136         @Test
137         fun `known sones can be filtered by new sones`() {
138                 addHttpRequestParameter("filter", "new")
139                 page.handleRequest(freenetRequest, templateContext)
140                 verifySonesAreInOrder(1, 0)
141         }
142
143         @Test
144         fun `known sones can be filtered by known sones`() {
145                 addHttpRequestParameter("filter", "not-new")
146                 page.handleRequest(freenetRequest, templateContext)
147                 verifySonesAreInOrder(3, 2)
148         }
149
150         @Test
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)
156         }
157
158         @Test
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)
164         }
165
166         @Test
167         fun `known sones can not be filtered by followed sones if there is no current sone`() {
168                 addHttpRequestParameter("filter", "followed")
169                 unsetCurrentSone()
170                 page.handleRequest(freenetRequest, templateContext)
171                 verifySonesAreInOrder(3, 2, 1, 0)
172         }
173
174         @Test
175         fun `known sones can not be filtered by not-followed sones if there is no current sone`() {
176                 addHttpRequestParameter("filter", "not-followed")
177                 unsetCurrentSone()
178                 page.handleRequest(freenetRequest, templateContext)
179                 verifySonesAreInOrder(3, 2, 1, 0)
180         }
181
182 }