🎨 Move new-sone template creation into module
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sun, 8 Dec 2019 18:26:52 +0000 (19:26 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Wed, 11 Dec 2019 15:59:04 +0000 (16:59 +0100)
src/main/kotlin/net/pterodactylus/sone/web/notification/NewSoneHandler.kt
src/main/kotlin/net/pterodactylus/sone/web/notification/NotificationHandlerModule.kt
src/test/kotlin/net/pterodactylus/sone/web/notification/NewSoneHandlerTest.kt
src/test/kotlin/net/pterodactylus/sone/web/notification/NotificationHandlerModuleTest.kt

index 89150cc..a420f14 100644 (file)
@@ -22,14 +22,11 @@ import net.pterodactylus.sone.core.event.*
 import net.pterodactylus.sone.data.*
 import net.pterodactylus.sone.notify.*
 import net.pterodactylus.util.notify.*
-import net.pterodactylus.util.template.*
 
 /**
  * Notification handler for “new Sone discovered” events.
  */
-class NewSoneHandler(private val notificationManager: NotificationManager, template: Template) {
-
-       private val notification = ListNotification<Sone>("new-sone-notification", "sones", template, dismissable = false)
+class NewSoneHandler(private val notificationManager: NotificationManager, private val notification: ListNotification<Sone>) {
 
        @Subscribe
        fun newSoneFound(newSoneFoundEvent: NewSoneFoundEvent) {
index 02bc3f9..b843cca 100644 (file)
@@ -50,4 +50,14 @@ class NotificationHandlerModule : AbstractModule() {
        fun getSoneLockedOnStartupNotification(loaders: Loaders) =
                        ListNotification<Sone>("sone-locked-on-startup", "sones", loaders.loadTemplate("/templates/notify/soneLockedOnStartupNotification.html"))
 
+       @Provides
+       @Singleton
+       fun getNewSoneHandler(notificationManager: NotificationManager, @Named("newSone") notification: ListNotification<Sone>) =
+                       NewSoneHandler(notificationManager, notification)
+
+       @Provides
+       @Named("newSone")
+       fun getNewSoneNotification(loaders: Loaders) =
+                       ListNotification<Sone>("new-sone-notification", "sones", loaders.loadTemplate("/templates/notify/newSoneNotification.html"), dismissable = false)
+
 }
index 951de28..ff0947d 100644 (file)
@@ -19,11 +19,11 @@ package net.pterodactylus.sone.web.notification
 
 import com.google.common.eventbus.*
 import net.pterodactylus.sone.core.event.*
+import net.pterodactylus.sone.data.*
 import net.pterodactylus.sone.data.impl.*
 import net.pterodactylus.sone.notify.*
-import net.pterodactylus.sone.test.*
-import net.pterodactylus.sone.utils.*
 import net.pterodactylus.util.notify.*
+import net.pterodactylus.util.template.*
 import org.hamcrest.MatcherAssert.*
 import org.hamcrest.Matchers.*
 import java.io.*
@@ -34,7 +34,8 @@ class NewSoneHandlerTest {
        @Suppress("UnstableApiUsage")
        private val eventBus = EventBus()
        private val notificationManager = NotificationManager()
-       private val handler = NewSoneHandler(notificationManager, "<% sones>".asTemplate())
+       private val notification = ListNotification<Sone>("", "", Template())
+       private val handler = NewSoneHandler(notificationManager, notification)
 
        init {
                eventBus.register(handler)
@@ -43,29 +44,13 @@ class NewSoneHandlerTest {
        @Test
        fun `handler adds notification if new sone event is fired`() {
                eventBus.post(NewSoneFoundEvent(sone))
-               val notification = notificationManager.notifications.single() as ListNotification<*>
-               assertThat(notification.id, equalTo("new-sone-notification"))
+               assertThat(notificationManager.notifications, contains<Notification>(notification))
        }
 
        @Test
        fun `handler adds sone to notification`() {
                eventBus.post(NewSoneFoundEvent(sone))
-               val notification = notificationManager.notifications.single() as ListNotification<*>
-               assertThat(notification.elements, contains<Any>(sone))
-       }
-
-       @Test
-       fun `handler sets correct key for sones`() {
-               eventBus.post(NewSoneFoundEvent(sone))
-               val notification = notificationManager.notifications.single() as ListNotification<*>
-               assertThat(notification.render(), equalTo(listOf(sone).toString()))
-       }
-
-       @Test
-       fun `handler creates non-dismissable notification`() {
-               eventBus.post(NewSoneFoundEvent(sone))
-               val notification = notificationManager.notifications.single() as ListNotification<*>
-               assertThat(notification, matches("is not dismissable") { !it.isDismissable })
+               assertThat(notification.elements, contains(sone))
        }
 
        @Test
@@ -74,26 +59,23 @@ class NewSoneHandlerTest {
                        override fun render(writer: Writer) = Unit
                })
                eventBus.post(NewSoneFoundEvent(sone))
-               val notification = notificationManager.notifications.single()
-               assertThat(notification.id, equalTo("first-start-notification"))
+               assertThat(notificationManager.notifications.single().id, equalTo("first-start-notification"))
        }
 
        @Test
        fun `handler removes sone from notification if sone is marked as known`() {
-               eventBus.post(NewSoneFoundEvent(sone))
-               val notification = notificationManager.notifications.single() as ListNotification<*>
+               notification.add(sone)
                eventBus.post(MarkSoneKnownEvent(sone))
                assertThat(notification.elements, emptyIterable())
        }
 
        @Test
        fun `handler removes sone from notification if sone is removed`() {
-               eventBus.post(NewSoneFoundEvent(sone))
-               val notification = notificationManager.notifications.single() as ListNotification<*>
+               notification.add(sone)
                eventBus.post(SoneRemovedEvent(sone))
                assertThat(notification.elements, emptyIterable())
        }
 
 }
 
-private val sone = IdOnlySone("sone-id")
+private val sone: Sone = IdOnlySone("sone-id")
index 665e69d..76926fb 100644 (file)
@@ -113,4 +113,33 @@ class NotificationHandlerModuleTest {
                assertThat(injector.getInstance<ListNotification<Sone>>(named("soneLockedOnStartup")).isDismissable, equalTo(true))
        }
 
+       @Test
+       fun `new-sone handler can be created`() {
+               assertThat(injector.getInstance<NewSoneHandler>(), notNullValue())
+       }
+
+       @Test
+       fun `new-sone handler is created as singleton`() {
+               injector.verifySingletonInstance<NewSoneHandler>()
+       }
+
+       @Test
+       fun `new-sone notification has correct ID`() {
+               assertThat(injector.getInstance<ListNotification<Sone>>(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<ListNotification<Sone>>(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<ListNotification<Sone>>(named("newSone")).isDismissable, equalTo(false))
+       }
+
 }