♻️ Move startup notification into handler
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Wed, 11 Dec 2019 18:46:11 +0000 (19:46 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Wed, 11 Dec 2019 18:47:37 +0000 (19:47 +0100)
src/main/java/net/pterodactylus/sone/web/WebInterface.java
src/main/kotlin/net/pterodactylus/sone/web/notification/NotificationHandler.kt
src/main/kotlin/net/pterodactylus/sone/web/notification/NotificationHandlerModule.kt
src/main/kotlin/net/pterodactylus/sone/web/notification/StartupHandler.kt
src/test/kotlin/net/pterodactylus/sone/web/notification/NotificationHandlerModuleTest.kt

index cefbfa9..8e21ea5 100644 (file)
@@ -411,19 +411,6 @@ public class WebInterface implements SessionProvider {
                registerToadlets();
 
                /* notification templates. */
-               Template startupNotificationTemplate = loaders.loadTemplate("/templates/notify/startupNotification.html");
-
-               final TemplateNotification startupNotification = new TemplateNotification("startup-notification", startupNotificationTemplate);
-               notificationManager.addNotification(startupNotification);
-
-               ticker.schedule(new Runnable() {
-
-                       @Override
-                       public void run() {
-                               startupNotification.dismiss();
-                       }
-               }, 2, TimeUnit.MINUTES);
-
                Template wotMissingNotificationTemplate = loaders.loadTemplate("/templates/notify/wotMissingNotification.html");
                final TemplateNotification wotMissingNotification = new TemplateNotification("wot-missing-notification", wotMissingNotificationTemplate);
                ticker.scheduleAtFixedRate(new Runnable() {
index dc60079..41e67bd 100644 (file)
@@ -33,5 +33,6 @@ class NotificationHandler @Inject constructor(
                newVersionHandler: NewVersionHandler,
                imageInsertHandler: ImageInsertHandler,
                firstStartHandler: FirstStartHandler,
-               configNotReadHandler: ConfigNotReadHandler
+               configNotReadHandler: ConfigNotReadHandler,
+               startupHandler: StartupHandler
 )
index e94be5b..539f001 100644 (file)
@@ -47,6 +47,7 @@ class NotificationHandlerModule : AbstractModule() {
                bind<ImageInsertHandler>().asSingleton()
                bind<FirstStartHandler>().asSingleton()
                bind<ConfigNotReadHandler>().asSingleton()
+               bind<StartupHandler>().asSingleton()
        }
 
        @Provides
@@ -121,6 +122,12 @@ class NotificationHandlerModule : AbstractModule() {
        fun getConfigNotReadNotification(loaders: Loaders) =
                        TemplateNotification("config-not-read-notification", loaders.loadTemplate("/templates/notify/configNotReadNotification.html"))
 
+       @Provides
+       @Singleton
+       @Named("startup")
+       fun getStartupNotification(loaders: Loaders) =
+                       TemplateNotification("startup-notification", loaders.loadTemplate("/templates/notify/startupNotification.html"))
+
        private inline fun <reified T> bind(): AnnotatedBindingBuilder<T> = bind(T::class.java)
        private fun ScopedBindingBuilder.asSingleton() = `in`(Singleton::class.java)
 
index 392aa5c..c3b69f0 100644 (file)
@@ -22,11 +22,15 @@ import net.pterodactylus.sone.core.event.*
 import net.pterodactylus.util.notify.*
 import java.util.concurrent.*
 import java.util.concurrent.TimeUnit.*
+import javax.inject.*
 
 /**
  * Handler for the [Startup] event notification.
  */
-class StartupHandler(private val notificationManager: NotificationManager, private val notification: TemplateNotification, val ticker: ScheduledExecutorService) {
+class StartupHandler @Inject constructor(
+               private val notificationManager: NotificationManager,
+               @Named("startup") private val notification: TemplateNotification,
+               private val ticker: ScheduledExecutorService) {
 
        @Subscribe
        fun startup(startup: Startup) {
index a061230..477e6e3 100644 (file)
@@ -370,4 +370,31 @@ class NotificationHandlerModuleTest {
                injector.verifySingletonInstance<ConfigNotReadHandler>()
        }
 
+       @Test
+       fun `startup notification can be created`() {
+               injector.verifySingletonInstance<TemplateNotification>(named("startup"))
+       }
+
+       @Test
+       fun `startup notification has correct ID`() {
+               assertThat(injector.getInstance<TemplateNotification>(named("startup")).id, equalTo("startup-notification"))
+       }
+
+       @Test
+       fun `startup notification is dismissable`() {
+               assertThat(injector.getInstance<TemplateNotification>(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<TemplateNotification>(named("startup"))
+               assertThat(notification.render(), equalTo("1"))
+       }
+
+       @Test
+       fun `startup handler is created as singleton`() {
+               injector.verifySingletonInstance<StartupHandler>()
+       }
+
 }