/**
* Sone - NotificationHandlerModuleTest.kt - Copyright © 2019–2020 David ‘Bombe’ Roden
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
package net.pterodactylus.sone.web.notification
import com.google.inject.*
import com.google.inject.Guice.*
import com.google.inject.name.Names.*
import net.pterodactylus.sone.core.*
import net.pterodactylus.sone.core.event.*
import net.pterodactylus.sone.data.*
import net.pterodactylus.sone.data.Post.*
import net.pterodactylus.sone.data.impl.*
import net.pterodactylus.sone.database.*
import net.pterodactylus.sone.freenet.wot.*
import net.pterodactylus.sone.main.*
import net.pterodactylus.sone.notify.*
import net.pterodactylus.sone.test.*
import net.pterodactylus.sone.text.*
import net.pterodactylus.sone.utils.*
import net.pterodactylus.util.notify.*
import org.hamcrest.MatcherAssert.*
import org.hamcrest.Matchers.*
import org.mockito.*
import org.mockito.Mockito.*
import java.util.concurrent.*
import java.util.concurrent.TimeUnit.*
import java.util.function.*
import kotlin.test.*
/**
* Unit test for [NotificationHandlerModule].
*/
class NotificationHandlerModuleTest {
private val core = mock()
private val webOfTrustConnector = mock()
private val ticker = mock()
private val notificationManager = NotificationManager()
private val loaders = TestLoaders()
private val injector: Injector = createInjector(
Core::class.isProvidedBy(core),
NotificationManager::class.isProvidedBy(notificationManager),
Loaders::class.isProvidedBy(loaders),
WebOfTrustConnector::class.isProvidedBy(webOfTrustConnector),
ScheduledExecutorService::class.withNameIsProvidedBy(ticker, "notification"),
SoneTextParser::class.isProvidedByMock(),
PostReplyProvider::class.isProvidedByMock(),
NotificationHandlerModule()
)
@Test
fun `notification handler is created as singleton`() {
injector.verifySingletonInstance()
}
@Test
fun `mark-post-known-during-first-start handler is created as singleton`() {
injector.verifySingletonInstance()
}
@Test
fun `mark-post-known-during-first-start handler is created with correct action`() {
notificationManager.firstStart()
val handler = injector.getInstance()
val post = mock()
handler.newPostFound(NewPostFoundEvent(post))
verify(core).markPostKnown(post)
}
@Test
fun `mark-post-reply-known-during-first-start handler is created as singleton`() {
injector.verifySingletonInstance()
}
@Test
fun `mark-post-reply-known-during-first-start handler is created with correct action`() {
notificationManager.firstStart()
val handler = injector.getInstance()
val postReply = mock()
handler.newPostReply(NewPostReplyFoundEvent(postReply))
verify(core).markReplyKnown(postReply)
}
@Test
fun `sone-locked-on-startup handler is created as singleton`() {
injector.verifySingletonInstance()
}
@Test
fun `module can create sone-locked-on-startup notification with correct id`() {
val notification = injector.getInstance>(named("soneLockedOnStartup"))
assertThat(notification.id, equalTo("sone-locked-on-startup"))
}
@Test
fun `sone-locked-on-startup notification is created as singleton`() {
injector.verifySingletonInstance>(named("soneLockedOnStartup"))
}
@Test
fun `module can create sone-locked-on-startup notification with correct template and key`() {
loaders.templates += "/templates/notify/soneLockedOnStartupNotification.html" to "<% sones>".asTemplate()
val notification = injector.getInstance>(named("soneLockedOnStartup"))
val sone1 = IdOnlySone("sone1")
val sone2 = IdOnlySone("sone2")
notification.add(sone1)
notification.add(sone2)
assertThat(notification.render(), equalTo(listOf(sone1, sone2).toString()))
}
@Test
fun `sone-locked-on-startup notification is dismissable`() {
assertThat(injector.getInstance>(named("soneLockedOnStartup")).isDismissable, equalTo(true))
}
@Test
fun `new-sone handler is created as singleton`() {
injector.verifySingletonInstance()
}
@Test
fun `new-sone notification has correct ID`() {
assertThat(injector.getInstance>(named("newSone")).id, equalTo("new-sone-notification"))
}
@Test
fun `new-sone notification has correct key and template`() {
loaders.templates += "/templates/notify/newSoneNotification.html" to "<% sones>".asTemplate()
val notification = injector.getInstance>(named("newSone"))
val sones = listOf(IdOnlySone("sone1"), IdOnlySone("sone2"))
sones.forEach(notification::add)
assertThat(notification.render(), equalTo(sones.toString()))
}
@Test
fun `new-sone notification is not dismissable`() {
assertThat(injector.getInstance>(named("newSone")).isDismissable, equalTo(false))
}
@Test
fun `new-remote-post handler is created as singleton`() {
injector.verifySingletonInstance()
}
@Test
fun `new-remote-post notification is created as singleton`() {
injector.verifySingletonInstance>(named("newRemotePost"))
}
@Test
fun `new-remote-post notification has correct ID`() {
assertThat(injector.getInstance>(named("newRemotePost")).id, equalTo("new-post-notification"))
}
@Test
fun `new-remote-post notification is not dismissable`() {
assertThat(injector.getInstance>(named("newRemotePost")).isDismissable, equalTo(false))
}
@Test
fun `new-remote-post notification has correct key and template`() {
loaders.templates += "/templates/notify/newPostNotification.html" to "<% posts>".asTemplate()
val notification = injector.getInstance>(named("newRemotePost"))
val posts = listOf(EmptyPost("post1"), EmptyPost("post2"))
posts.forEach(notification::add)
assertThat(notification.render(), equalTo(posts.toString()))
}
@Test
fun `remote-post handler is created as singleton`() {
injector.verifySingletonInstance()
}
@Test
fun `new-remote-post-reply notification is created as singleton`() {
injector.verifySingletonInstance>(named("newRemotePostReply"))
}
@Test
fun `new-remote-post-reply notification has correct ID`() {
assertThat(injector.getInstance>(named("newRemotePostReply")).id, equalTo("new-reply-notification"))
}
@Test
fun `new-remote-post-reply notification is not dismissable`() {
assertThat(injector.getInstance>(named("newRemotePostReply")).isDismissable, equalTo(false))
}
@Test
fun `new-remote-post-reply notification has correct key and template`() {
loaders.templates += "/templates/notify/newReplyNotification.html" to "<% replies>".asTemplate()
val notification = injector.getInstance>(named("newRemotePostReply"))
val postReplies = listOf(createPostReply(), createPostReply())
postReplies.forEach(notification::add)
assertThat(notification.render(), equalTo(postReplies.toString()))
}
@Test
fun `sone-locked notification is created as singleton`() {
injector.verifySingletonInstance>(named("soneLocked"))
}
@Test
fun `sone-locked notification is dismissable`() {
assertThat(injector.getInstance>(named("soneLocked")).isDismissable, equalTo(true))
}
@Test
fun `sone-locked notification has correct ID`() {
assertThat(injector.getInstance>(named("soneLocked")).id, equalTo("sones-locked-notification"))
}
@Test
fun `sone-locked notification has correct key and template`() {
loaders.templates += "/templates/notify/lockedSonesNotification.html" to "<% sones>".asTemplate()
val notification = injector.getInstance>(named("soneLocked"))
val sones = listOf(IdOnlySone("sone1"), IdOnlySone("sone2"))
sones.forEach(notification::add)
assertThat(notification.render(), equalTo(sones.toString()))
}
@Test
fun `sone-locked handler is created as singleton`() {
injector.verifySingletonInstance()
}
@Test
fun `local-post notification is not dismissable`() {
assertThat(injector.getInstance>(named("localPost")).isDismissable, equalTo(false))
}
@Test
fun `local-post notification has correct ID`() {
assertThat(injector.getInstance>(named("localPost")).id, equalTo("local-post-notification"))
}
@Test
fun `local-post notification has correct key and template`() {
loaders.templates += "/templates/notify/newPostNotification.html" to "<% posts>".asTemplate()
val notification = injector.getInstance>(named("localPost"))
val posts = listOf(EmptyPost("post1"), EmptyPost("post2"))
posts.forEach(notification::add)
assertThat(notification.render(), equalTo(posts.toString()))
}
@Test
fun `local-post notification is created as singleton`() {
injector.verifySingletonInstance>(named("localPost"))
}
@Test
fun `local-post handler is created as singleton`() {
injector.verifySingletonInstance()
}
@Test
fun `local-reply notification is not dismissable`() {
assertThat(injector.getInstance>(named("localReply")).isDismissable, equalTo(false))
}
@Test
fun `local-reply notification has correct ID`() {
assertThat(injector.getInstance>(named("localReply")).id, equalTo("local-reply-notification"))
}
@Test
fun `local-reply notification has correct key and template`() {
loaders.templates += "/templates/notify/newReplyNotification.html" to "<% replies>".asTemplate()
val notification = injector.getInstance>(named("localReply"))
val replies = listOf(createPostReply("reply1"), createPostReply("reply2"))
replies.forEach(notification::add)
assertThat(notification.render(), equalTo(replies.toString()))
}
@Test
fun `local-reply notification is created as singleton`() {
injector.verifySingletonInstance>(named("localReply"))
}
@Test
fun `local-reply handler is created as singleton`() {
injector.verifySingletonInstance()
}
@Test
fun `new-version notification is created as singleton`() {
injector.verifySingletonInstance(named("newVersion"))
}
@Test
fun `new-version notification has correct ID`() {
assertThat(injector.getInstance(named("newVersion")).id, equalTo("new-version-notification"))
}
@Test
fun `new-version notification is dismissable`() {
assertThat(injector.getInstance(named("newVersion")).isDismissable, equalTo(true))
}
@Test
fun `new-version notification loads correct template`() {
loaders.templates += "/templates/notify/newVersionNotification.html" to "1".asTemplate()
val notification = injector.getInstance(named("newVersion"))
assertThat(notification.render(), equalTo("1"))
}
@Test
fun `new-version handler is created as singleton`() {
injector.verifySingletonInstance()
}
@Test
fun `inserting-image notification is created as singleton`() {
injector.verifySingletonInstance>(named("imageInserting"))
}
@Test
fun `inserting-image notification has correct ID`() {
assertThat(injector.getInstance>(named("imageInserting")).id, equalTo("inserting-images-notification"))
}
@Test
fun `inserting-image notification is dismissable`() {
assertThat(injector.getInstance>(named("imageInserting")).isDismissable, equalTo(true))
}
@Test
fun `inserting-image notification loads correct template`() {
loaders.templates += "/templates/notify/inserting-images-notification.html" to "<% images>".asTemplate()
val notification = injector.getInstance>(named("imageInserting"))
val images = listOf(ImageImpl(), ImageImpl()).onEach(notification::add)
assertThat(notification.render(), equalTo(images.toString()))
}
@Test
fun `inserting-image-failed notification is created as singleton`() {
injector.verifySingletonInstance>(named("imageFailed"))
}
@Test
fun `inserting-image-failed notification has correct ID`() {
assertThat(injector.getInstance>(named("imageFailed")).id, equalTo("image-insert-failed-notification"))
}
@Test
fun `inserting-image-failed notification is dismissable`() {
assertThat(injector.getInstance>(named("imageFailed")).isDismissable, equalTo(true))
}
@Test
fun `inserting-image-failed notification loads correct template`() {
loaders.templates += "/templates/notify/image-insert-failed-notification.html" to "<% images>".asTemplate()
val notification = injector.getInstance>(named("imageFailed"))
val images = listOf(ImageImpl(), ImageImpl()).onEach(notification::add)
assertThat(notification.render(), equalTo(images.toString()))
}
@Test
fun `inserted-image notification is created as singleton`() {
injector.verifySingletonInstance>(named("imageInserted"))
}
@Test
fun `inserted-image notification has correct ID`() {
assertThat(injector.getInstance>(named("imageInserted")).id, equalTo("inserted-images-notification"))
}
@Test
fun `inserted-image notification is dismissable`() {
assertThat(injector.getInstance>(named("imageInserted")).isDismissable, equalTo(true))
}
@Test
fun `inserted-image notification loads correct template`() {
loaders.templates += "/templates/notify/inserted-images-notification.html" to "<% images>".asTemplate()
val notification = injector.getInstance>(named("imageInserted"))
val images = listOf(ImageImpl(), ImageImpl()).onEach(notification::add)
assertThat(notification.render(), equalTo(images.toString()))
}
@Test
fun `image insert handler is created as singleton`() {
injector.verifySingletonInstance()
}
@Test
fun `first-start notification is created as singleton`() {
injector.verifySingletonInstance(named("firstStart"))
}
@Test
fun `first-start notification has correct ID`() {
assertThat(injector.getInstance(named("firstStart")).id, equalTo("first-start-notification"))
}
@Test
fun `first-start notification is dismissable`() {
assertThat(injector.getInstance(named("firstStart")).isDismissable, equalTo(true))
}
@Test
fun `first-start notification loads correct template`() {
loaders.templates += "/templates/notify/firstStartNotification.html" to "1".asTemplate()
val notification = injector.getInstance(named("firstStart"))
assertThat(notification.render(), equalTo("1"))
}
@Test
fun `first-start handler is created as singleton`() {
injector.verifySingletonInstance()
}
@Test
fun `config-not-read notification is created as singleton`() {
injector.verifySingletonInstance(named("configNotRead"))
}
@Test
fun `config-not-read notification has correct ID `() {
assertThat(injector.getInstance(named("configNotRead")).id, equalTo("config-not-read-notification"))
}
@Test
fun `config-not-read notification is dismissable`() {
assertThat(injector.getInstance(named("configNotRead")).isDismissable, equalTo(true))
}
@Test
fun `config-not-read notification loads correct template`() {
loaders.templates += "/templates/notify/configNotReadNotification.html" to "1".asTemplate()
val notification = injector.getInstance(named("configNotRead"))
assertThat(notification.render(), equalTo("1"))
}
@Test
fun `config-not-read handler is created as singleton`() {
injector.verifySingletonInstance()
}
@Test
fun `startup notification can be created`() {
injector.verifySingletonInstance(named("startup"))
}
@Test
fun `startup notification has correct ID`() {
assertThat(injector.getInstance(named("startup")).id, equalTo("startup-notification"))
}
@Test
fun `startup notification is dismissable`() {
assertThat(injector.getInstance(named("startup")).isDismissable, equalTo(true))
}
@Test
fun `startup notification loads correct template`() {
loaders.templates += "/templates/notify/startupNotification.html" to "1".asTemplate()
val notification = injector.getInstance(named("startup"))
assertThat(notification.render(), equalTo("1"))
}
@Test
fun `startup handler is created as singleton`() {
injector.verifySingletonInstance()
}
@Test
fun `web-of-trust notification is created as singleton`() {
injector.verifySingletonInstance(named("webOfTrust"))
}
@Test
fun `web-of-trust notification has correct ID`() {
assertThat(injector.getInstance(named("webOfTrust")).id, equalTo("wot-missing-notification"))
}
@Test
fun `web-of-trust notification is dismissable`() {
assertThat(injector.getInstance(named("webOfTrust")).isDismissable, equalTo(true))
}
@Test
fun `web-of-trust notification loads correct template`() {
loaders.templates += "/templates/notify/wotMissingNotification.html" to "1".asTemplate()
val notification = injector.getInstance(named("webOfTrust"))
assertThat(notification.render(), equalTo("1"))
}
@Test
fun `web-of-trust handler is created as singleton`() {
injector.verifySingletonInstance(named("webOfTrust"))
}
@Test
fun `web-of-trust reacher is created as singleton`() {
injector.verifySingletonInstance(named("webOfTrustReacher"))
}
@Test
fun `web-of-trust reacher access the wot connector`() {
injector.getInstance(named("webOfTrustReacher")).run()
verify(webOfTrustConnector).ping()
}
@Test
fun `web-of-trust reschedule is created as singleton`() {
injector.verifySingletonInstance>(named("webOfTrustReschedule"))
}
@Test
fun `web-of-trust reschedule schedules at the correct delay`() {
val webOfTrustPinger = injector.getInstance()
injector.getInstance>(named("webOfTrustReschedule"))(webOfTrustPinger)
verify(ticker).schedule(ArgumentMatchers.eq(webOfTrustPinger), ArgumentMatchers.eq(15L), ArgumentMatchers.eq(SECONDS))
}
@Test
fun `sone mention detector is created as singleton`() {
assertThat(injector.getInstance(), notNullValue())
}
@Test
fun `sone-mentioned notification is created as singleton`() {
injector.verifySingletonInstance>(named("soneMentioned"))
}
@Test
fun `sone-mentioned notification has correct ID`() {
assertThat(injector.getInstance>(named("soneMentioned")).id, equalTo("mention-notification"))
}
@Test
fun `sone-mentioned notification is not dismissable`() {
assertThat(injector.getInstance>(named("soneMentioned")).isDismissable, equalTo(false))
}
@Test
fun `sone-mentioned notification loads correct template`() {
loaders.templates += "/templates/notify/mentionNotification.html" to "<% posts>".asTemplate()
val notification = injector.getInstance>(named("soneMentioned"))
val posts = listOf(EmptyPost("1"), EmptyPost("2")).onEach(notification::add)
assertThat(notification.render(), equalTo(posts.toString()))
}
@Test
fun `sone-mentioned handler is created as singleton`() {
injector.verifySingletonInstance()
}
@Test
fun `sone insert notification supplier is created as singleton`() {
injector.verifySingletonInstance()
}
@Test
fun `sone insert notification template is loaded correctly`() {
loaders.templates += "/templates/notify/soneInsertNotification.html" to "foo".asTemplate()
injector.getInstance()
.invoke(createRemoteSone())
.render()
.let { assertThat(it, equalTo("foo")) }
}
@Test
fun `sone notification supplier returns different notifications for different sones`() {
val supplier = injector.getInstance()
listOf(createRemoteSone(), createRemoteSone(), createRemoteSone())
.map(supplier)
.distinct()
.let { assertThat(it, hasSize(3)) }
}
@Test
fun `sone notification supplier caches notifications for a sone`() {
val supplier = injector.getInstance()
val sone = createRemoteSone()
listOf(sone, sone, sone)
.map(supplier)
.distinct()
.let { assertThat(it, hasSize(1)) }
}
@Test
fun `sone notification supplier sets sone in notification template`() {
val supplier = injector.getInstance()
val sone = createRemoteSone()
val templateNotification = supplier(sone)
assertThat(templateNotification["insertSone"], sameInstance(sone))
}
@Test
fun `sone insert handler is created as singleton`() {
injector.verifySingletonInstance()
}
}