Replace reply-specific interfaces with Kotlin versions
[Sone.git] / src / test / kotlin / net / pterodactylus / sone / web / ajax / GetLikesAjaxPageTest.kt
1 package net.pterodactylus.sone.web.ajax
2
3 import net.pterodactylus.sone.data.Post
4 import net.pterodactylus.sone.data.PostReply
5 import net.pterodactylus.sone.data.Profile
6 import net.pterodactylus.sone.data.Sone
7 import net.pterodactylus.sone.test.mock
8 import net.pterodactylus.sone.test.whenever
9 import org.hamcrest.MatcherAssert.assertThat
10 import org.hamcrest.Matchers.contains
11 import org.hamcrest.Matchers.equalTo
12 import org.junit.Test
13
14 /**
15  * Unit test for [GetLikesAjaxPage].
16  */
17 class GetLikesAjaxPageTest : JsonPageTest("getLikes.ajax", needsFormPassword = false, pageSupplier = ::GetLikesAjaxPage) {
18
19         @Test
20         fun `request without parameters results in failing request`() {
21                 assertThat(json.isSuccess, equalTo(false))
22         }
23
24         @Test
25         fun `request with invalid post id results in invalid-post-id`() {
26                 addRequestParameter("type", "post")
27                 addRequestParameter("post", "invalid")
28                 assertThatJsonFailed("invalid-post-id")
29         }
30
31         @Test
32         fun `request with missing post id results in invalid-post-id`() {
33                 addRequestParameter("type", "post")
34                 assertThatJsonFailed("invalid-post-id")
35         }
36
37         @Test
38         fun `request with valid post id results in likes for post`() {
39                 val post = mock<Post>().apply { whenever(id).thenReturn("post-id") }
40                 addPost(post)
41                 addLikes(post, createSone(2), createSone(1), createSone(3))
42                 addRequestParameter("type", "post")
43                 addRequestParameter("post", "post-id")
44                 assertThatJsonIsSuccessful()
45                 assertThat(json["likes"]?.asInt(), equalTo(3))
46                 assertThat(json["sones"]!!.toList().map { it["id"].asText() to it["name"].asText() }, contains(
47                                 "S1" to "F1 M1 L1",
48                                 "S2" to "F2 M2 L2",
49                                 "S3" to "F3 M3 L3"
50                 ))
51         }
52
53         @Test
54         fun `request with invalid reply id results in invalid-reply-id`() {
55                 addRequestParameter("type", "reply")
56                 addRequestParameter("reply", "invalid")
57                 assertThatJsonFailed("invalid-reply-id")
58         }
59
60         @Test
61         fun `request with missing reply id results in invalid-reply-id`() {
62                 addRequestParameter("type", "reply")
63                 assertThatJsonFailed("invalid-reply-id")
64         }
65
66         @Test
67         fun `request with valid reply id results in likes for reply`() {
68                 val reply = mock<PostReply>().apply { whenever(id).thenReturn("reply-id") }
69                 addReply(reply)
70                 addLikes(reply, createSone(2), createSone(1), createSone(3))
71                 addRequestParameter("type", "reply")
72                 addRequestParameter("reply", "reply-id")
73                 assertThatJsonIsSuccessful()
74                 assertThat(json["likes"]?.asInt(), equalTo(3))
75                 assertThat(json["sones"]!!.toList().map { it["id"].asText() to it["name"].asText() }, contains(
76                                 "S1" to "F1 M1 L1",
77                                 "S2" to "F2 M2 L2",
78                                 "S3" to "F3 M3 L3"
79                 ))
80         }
81
82         @Test
83         fun `request with invalid type results in invalid-type`() {
84                 addRequestParameter("type", "invalid")
85                 addRequestParameter("invalid", "foo")
86                 assertThatJsonFailed("invalid-type")
87         }
88
89 }
90
91 private fun createSone(index: Int) = mock<Sone>().apply {
92         whenever(id).thenReturn("S$index")
93         whenever(profile).thenReturn(Profile(this).apply {
94                 firstName = "F$index"
95                 middleName = "M$index"
96                 lastName = "L$index"
97         })
98 }