package net.pterodactylus.sone.data
-import net.pterodactylus.sone.test.emptyPostReply
+import net.pterodactylus.sone.test.createPostReply
import org.hamcrest.MatcherAssert.assertThat
import org.hamcrest.Matchers.equalTo
import org.hamcrest.Matchers.greaterThan
@Test
fun `newestReplyFirst comparator returns less-than 0 is first reply is newer than second`() {
- val newerReply = emptyPostReply(time = 2000)
- val olderReply = emptyPostReply(time = 1000)
+ val newerReply = createPostReply(time = 2000)
+ val olderReply = createPostReply(time = 1000)
assertThat(newestReplyFirst.compare(newerReply, olderReply), lessThan(0))
}
@Test
fun `newestReplyFirst comparator returns greater-than 0 is first reply is older than second`() {
- val newerReply = emptyPostReply(time = 2000)
- val olderReply = emptyPostReply(time = 1000)
+ val newerReply = createPostReply(time = 2000)
+ val olderReply = createPostReply(time = 1000)
assertThat(newestReplyFirst.compare(olderReply, newerReply), greaterThan(0))
}
@Test
fun `newestReplyFirst comparator returns 0 is first and second reply have same age`() {
- val reply1 = emptyPostReply(time = 1000)
- val reply2 = emptyPostReply(time = 1000)
+ val reply1 = createPostReply(time = 1000)
+ val reply2 = createPostReply(time = 1000)
assertThat(newestReplyFirst.compare(reply1, reply2), equalTo(0))
}
@Test
fun `noFutureReply filter recognizes reply from the future`() {
- val futureReply = emptyPostReply(time = System.currentTimeMillis() + DAYS.toMillis(1))
+ val futureReply = createPostReply(time = System.currentTimeMillis() + DAYS.toMillis(1))
assertThat(noFutureReply(futureReply), equalTo(false))
}
@Test
fun `noFutureReply filter recognizes reply from the present`() {
- val futureReply = emptyPostReply(time = System.currentTimeMillis())
+ val futureReply = createPostReply(time = System.currentTimeMillis())
assertThat(noFutureReply(futureReply), equalTo(true))
}
fun `post count comparator compares replies if posts are not different`() {
val sone1 = object : IdOnlySone("1") {
override fun getPosts() = listOf(createPost(), createPost())
- override fun getReplies() = setOf(emptyPostReply(), emptyPostReply())
+ override fun getReplies() = setOf(createPostReply(), createPostReply())
}
val sone2 = object : IdOnlySone("2") {
override fun getPosts() = listOf(createPost(), createPost())
- override fun getReplies() = setOf(emptyPostReply(), emptyPostReply(), emptyPostReply())
+ override fun getReplies() = setOf(createPostReply(), createPostReply(), createPostReply())
}
assertThat(postCountComparator.compare(sone1, sone2), greaterThan(0))
}
fun `post count comparator sorts sone with same amount of posts and replies as equal`() {
val sone1 = object : IdOnlySone("1") {
override fun getPosts() = listOf(createPost(), createPost())
- override fun getReplies() = setOf(emptyPostReply(), emptyPostReply())
+ override fun getReplies() = setOf(createPostReply(), createPostReply())
}
val sone2 = object : IdOnlySone("2") {
override fun getPosts() = listOf(createPost(), createPost())
- override fun getReplies() = setOf(emptyPostReply(), emptyPostReply())
+ override fun getReplies() = setOf(createPostReply(), createPostReply())
}
assertThat(postCountComparator.compare(sone1, sone2), equalTo(0))
}
}
}
-fun emptyPostReply(text: String = "", post: Post? = createPost(), sone: Sone = remoteSone1, known: Boolean = false, time: Long = 1) = object : PostReply {
+fun createPostReply(text: String = "", post: Post? = createPost(), sone: Sone = remoteSone1, known: Boolean = false, time: Long = 1) = object : PostReply {
override val id = "reply-id"
override fun getSone() = sone
override fun getPostId() = post!!.id
@Test
fun `detector does not emit event for reply that contains no sones`() {
- val reply = emptyPostReply()
+ val reply = createPostReply()
eventBus.post(NewPostReplyFoundEvent(reply))
assertThat(capturedFoundEvents, emptyIterable())
}
@Test
fun `detector does not emit event for reply that contains two links to remote sones`() {
- val reply = emptyPostReply("text mentions sone://${remoteSone1.id} and sone://${remoteSone2.id}.")
+ val reply = createPostReply("text mentions sone://${remoteSone1.id} and sone://${remoteSone2.id}.")
eventBus.post(NewPostReplyFoundEvent(reply))
assertThat(capturedFoundEvents, emptyIterable())
}
@Test
fun `detector emits event on reply that contains links to a remote and a local sone`() {
val post = createPost()
- val reply = emptyPostReply("text mentions sone://${remoteSone1.id} and sone://${localSone1.id}.", post)
+ val reply = createPostReply("text mentions sone://${remoteSone1.id} and sone://${localSone1.id}.", post)
eventBus.post(NewPostReplyFoundEvent(reply))
assertThat(capturedFoundEvents, contains(MentionOfLocalSoneFoundEvent(post)))
}
@Test
fun `detector emits one event on reply that contains two links to the same local sone`() {
val post = createPost()
- val reply = emptyPostReply("text mentions sone://${localSone1.id} and sone://${localSone1.id}.", post)
+ val reply = createPostReply("text mentions sone://${localSone1.id} and sone://${localSone1.id}.", post)
eventBus.post(NewPostReplyFoundEvent(reply))
assertThat(capturedFoundEvents, contains(MentionOfLocalSoneFoundEvent(post)))
}
@Test
fun `detector emits one event on reply that contains two links to local sones`() {
val post = createPost()
- val reply = emptyPostReply("text mentions sone://${localSone1.id} and sone://${localSone2.id}.", post)
+ val reply = createPostReply("text mentions sone://${localSone1.id} and sone://${localSone2.id}.", post)
eventBus.post(NewPostReplyFoundEvent(reply))
assertThat(capturedFoundEvents, contains(MentionOfLocalSoneFoundEvent(post)))
}
@Test
fun `detector does not emit event for reply by local sone`() {
- val reply = emptyPostReply("text mentions sone://${localSone1.id} and sone://${localSone2.id}.", sone = localSone1)
+ val reply = createPostReply("text mentions sone://${localSone1.id} and sone://${localSone2.id}.", sone = localSone1)
eventBus.post(NewPostReplyFoundEvent(reply))
assertThat(capturedFoundEvents, emptyIterable())
}
@Test
fun `detector does not emit event for reply without post`() {
- val reply = emptyPostReply("text mentions sone://${localSone1.id} and sone://${localSone2.id}.", post = null)
+ val reply = createPostReply("text mentions sone://${localSone1.id} and sone://${localSone2.id}.", post = null)
eventBus.post(NewPostReplyFoundEvent(reply))
assertThat(caughtExceptions, emptyIterable())
assertThat(capturedFoundEvents, emptyIterable())
@Test
fun `detector does emit removed event when reply with mention is removed and no more mentions in that post exist`() {
val post = createPost()
- val reply = emptyPostReply("sone://${localSone1.id}", post)
+ val reply = createPostReply("sone://${localSone1.id}", post)
postReplyProvider.postReplies[post.id] = listOf(reply)
eventBus.post(NewPostReplyFoundEvent(reply))
eventBus.post(PostReplyRemovedEvent(reply))
@Test
fun `detector does not emit removed event when reply with mention is removed and post mentions local sone`() {
val post = createPost("sone://${localSone1.id}")
- val reply = emptyPostReply("sone://${localSone1.id}", post)
+ val reply = createPostReply("sone://${localSone1.id}", post)
eventBus.post(NewPostReplyFoundEvent(reply))
eventBus.post(PostReplyRemovedEvent(reply))
assertThat(capturedRemovedEvents, emptyIterable())
@Test
fun `detector does emit removed event when reply with mention is removed and post mentions local sone but is known`() {
val post = createPost("sone://${localSone1.id}", known = true)
- val reply = emptyPostReply("sone://${localSone1.id}", post)
+ val reply = createPostReply("sone://${localSone1.id}", post)
eventBus.post(NewPostReplyFoundEvent(reply))
eventBus.post(PostReplyRemovedEvent(reply))
assertThat(capturedRemovedEvents, contains(MentionOfLocalSoneRemovedEvent(post)))
@Test
fun `detector does not emit removed event when reply with mention is removed and post has other replies with mentions`() {
val post = createPost()
- val reply1 = emptyPostReply("sone://${localSone1.id}", post)
- val reply2 = emptyPostReply("sone://${localSone1.id}", post)
+ val reply1 = createPostReply("sone://${localSone1.id}", post)
+ val reply2 = createPostReply("sone://${localSone1.id}", post)
postReplyProvider.postReplies[post.id] = listOf(reply1, reply2)
eventBus.post(NewPostReplyFoundEvent(reply1))
eventBus.post(PostReplyRemovedEvent(reply1))
@Test
fun `detector does emit removed event when reply with mention is removed and post has other replies with mentions which are known`() {
val post = createPost()
- val reply1 = emptyPostReply("sone://${localSone1.id}", post)
- val reply2 = emptyPostReply("sone://${localSone1.id}", post, known = true)
+ val reply1 = createPostReply("sone://${localSone1.id}", post)
+ val reply2 = createPostReply("sone://${localSone1.id}", post, known = true)
postReplyProvider.postReplies[post.id] = listOf(reply1, reply2)
eventBus.post(NewPostReplyFoundEvent(reply1))
eventBus.post(PostReplyRemovedEvent(reply1))
}
-private val localReply = emptyPostReply(sone = localSone1)
-private val remoteReply = emptyPostReply()
+private val localReply = createPostReply(sone = localSone1)
+private val remoteReply = createPostReply()
private val markedAsKnown = mutableListOf<PostReply>()
private val notificationTester = NotificationHandlerTester { MarkPostReplyKnownDuringFirstStartHandler(it, Consumer { markedAsKnown += it }) }
- private val postReply = emptyPostReply()
+ private val postReply = createPostReply()
@Test
fun `post reply is marked as known on new reply during first start`() {
fun `new-remote-post-reply notification has correct key and template`() {
loaders.templates += "/templates/notify/newReplyNotification.html" to "<% replies>".asTemplate()
val notification = injector.getInstance<ListNotification<PostReply>>(named("newRemotePostReply"))
- val postReplies = listOf(emptyPostReply(), emptyPostReply())
+ val postReplies = listOf(createPostReply(), createPostReply())
postReplies.forEach(notification::add)
assertThat(notification.render(), equalTo(postReplies.toString()))
}
fun `local-reply notification has correct key and template`() {
loaders.templates += "/templates/notify/newReplyNotification.html" to "<% replies>".asTemplate()
val notification = injector.getInstance<ListNotification<PostReply>>(named("localReply"))
- val replies = listOf(emptyPostReply("reply1"), emptyPostReply("reply2"))
+ val replies = listOf(createPostReply("reply1"), createPostReply("reply2"))
replies.forEach(notification::add)
assertThat(notification.render(), equalTo(replies.toString()))
}
private val notification = ListNotification<PostReply>("", "", Template())
private val notificationHandlerTester = NotificationHandlerTester { RemotePostReplyHandler(it, notification) }
- private val postReply = emptyPostReply()
+ private val postReply = createPostReply()
@Test
fun `reply is added to notification on new reply`() {
@Test
fun `reply is not added to notification on new local reply`() {
- val postReply = emptyPostReply(sone = localSone1)
+ val postReply = createPostReply(sone = localSone1)
notificationHandlerTester.sendEvent(NewPostReplyFoundEvent(postReply))
assertThat(notification.elements, not(hasItem<PostReply>(postReply)))
}
@Test
fun `notification is not added to manager on new local reply`() {
- val postReply = emptyPostReply(sone = localSone1)
+ val postReply = createPostReply(sone = localSone1)
notificationHandlerTester.sendEvent(NewPostReplyFoundEvent(postReply))
assertThat(notificationHandlerTester.notifications, not(hasItem<Notification>(notification)))
}