Clean up some imports
[Sone.git] / src / test / kotlin / net / pterodactylus / sone / web / pages / BookmarksPageTest.kt
1 package net.pterodactylus.sone.web.pages
2
3 import net.pterodactylus.sone.data.Post
4 import net.pterodactylus.sone.test.mock
5 import net.pterodactylus.sone.test.whenever
6 import net.pterodactylus.sone.utils.Pagination
7 import org.hamcrest.MatcherAssert.assertThat
8 import org.hamcrest.Matchers.contains
9 import org.hamcrest.Matchers.equalTo
10 import org.junit.Before
11 import org.junit.Test
12
13 /**
14  * Unit test for [BookmarksPage].
15  */
16 class BookmarksPageTest: WebPageTest() {
17
18         private val page = BookmarksPage(template, webInterface)
19         private val post1 = createLoadedPost(1000)
20         private val post2 = createLoadedPost(3000)
21         private val post3 = createLoadedPost(2000)
22
23         private fun createLoadedPost(time: Long) = mock<Post>().apply {
24                 whenever(isLoaded).thenReturn(true)
25                 whenever(this.time).thenReturn(time)
26         }
27
28         @Before
29         fun setupBookmarkedPostsAndPagination() {
30                 whenever(core.bookmarkedPosts).thenReturn(setOf(post1, post2, post3))
31                 core.preferences.postsPerPage = 5
32         }
33
34         @Test
35         fun `page returns correct path`() {
36                 assertThat(page.path, equalTo("bookmarks.html"))
37         }
38
39         @Test
40         @Suppress("UNCHECKED_CAST")
41         fun `page sets correct posts in template context`() {
42                 page.processTemplate(freenetRequest, templateContext)
43                 assertThat(templateContext["posts"] as Collection<Post>, contains(post2, post3, post1))
44                 assertThat((templateContext["pagination"] as Pagination<Post>).items, contains(post2, post3, post1))
45                 assertThat(templateContext["postsNotLoaded"], equalTo<Any>(false))
46         }
47
48         @Test
49         @Suppress("UNCHECKED_CAST")
50         fun `page does not put unloaded posts in template context but sets a flag`() {
51                 whenever(post3.isLoaded).thenReturn(false)
52                 page.processTemplate(freenetRequest, templateContext)
53                 assertThat(templateContext["posts"] as Collection<Post>, contains(post2, post1))
54                 assertThat((templateContext["pagination"] as Pagination<Post>).items, contains(post2, post1))
55                 assertThat(templateContext["postsNotLoaded"], equalTo<Any>(true))
56         }
57
58 }