🚧 Add handler for marking replies as known during first start
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Thu, 9 Jan 2020 21:18:13 +0000 (22:18 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Thu, 9 Jan 2020 21:18:13 +0000 (22:18 +0100)
src/main/java/net/pterodactylus/sone/web/WebInterface.java
src/main/kotlin/net/pterodactylus/sone/web/notification/MarkPostReplyKnownDuringFirstStartHandler.kt [new file with mode: 0644]
src/main/kotlin/net/pterodactylus/sone/web/notification/NotificationHandler.kt
src/main/kotlin/net/pterodactylus/sone/web/notification/NotificationHandlerModule.kt
src/test/kotlin/net/pterodactylus/sone/web/notification/MarkPostReplyKnownDuringFirstStartHandlerTest.kt [new file with mode: 0644]
src/test/kotlin/net/pterodactylus/sone/web/notification/NotificationHandlerModuleTest.kt
src/test/kotlin/net/pterodactylus/sone/web/notification/NotificationHandlerTester.kt [new file with mode: 0644]

index 5bb4e3a..ea357d2 100644 (file)
@@ -532,8 +532,6 @@ public class WebInterface implements SessionProvider {
                }
                if (!hasFirstStartNotification()) {
                        notificationManager.addNotification(isLocal ? localReplyNotification : newReplyNotification);
-               } else {
-                       getCore().markReplyKnown(reply);
                }
        }
 
diff --git a/src/main/kotlin/net/pterodactylus/sone/web/notification/MarkPostReplyKnownDuringFirstStartHandler.kt b/src/main/kotlin/net/pterodactylus/sone/web/notification/MarkPostReplyKnownDuringFirstStartHandler.kt
new file mode 100644 (file)
index 0000000..6a7f083
--- /dev/null
@@ -0,0 +1,43 @@
+/**
+ * Sone - MarkPostReplyKnownDuringFirstStartHandlerTest.kt - Copyright © 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 <http://www.gnu.org/licenses/>.
+ */
+
+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.notify.*
+import net.pterodactylus.sone.utils.*
+import net.pterodactylus.util.notify.*
+import java.util.function.*
+import javax.inject.*
+
+/**
+ * Handler that marks post replies [as known][net.pterodactylus.sone.core.Core.markReplyKnown]
+ * while the [first start notification][net.pterodactylus.util.notify.NotificationManager.hasFirstStartNotification]
+ * is shown.
+ */
+class MarkPostReplyKnownDuringFirstStartHandler @Inject constructor(private val notificationManager: NotificationManager, private val markAsKnown: Consumer<PostReply>) {
+
+       @Subscribe
+       fun newPostReply(event: NewPostReplyFoundEvent) {
+               if (notificationManager.hasFirstStartNotification()) {
+                       markAsKnown(event.postReply)
+               }
+       }
+
+}
index aba74ae..d0e8bf2 100644 (file)
@@ -28,6 +28,7 @@ import javax.inject.*
 @Suppress("UNUSED_PARAMETER")
 class NotificationHandler @Inject constructor(
                markPostKnownDuringFirstStartHandler: MarkPostKnownDuringFirstStartHandler,
+               markPostReplyKnownDuringFirstStartHandler: MarkPostReplyKnownDuringFirstStartHandler,
                newSoneHandler: NewSoneHandler,
                newRemotePostHandler: NewRemotePostHandler,
                soneLockedOnStartupHandler: SoneLockedOnStartupHandler,
index cfe5db3..2b147fc 100644 (file)
@@ -40,6 +40,7 @@ class NotificationHandlerModule : AbstractModule() {
        override fun configure() {
                bind(NotificationHandler::class.java).`in`(Singleton::class.java)
                bind<MarkPostKnownDuringFirstStartHandler>().asSingleton()
+               bind<MarkPostReplyKnownDuringFirstStartHandler>().asSingleton()
                bind<SoneLockedOnStartupHandler>().asSingleton()
                bind<NewSoneHandler>().asSingleton()
                bind<NewRemotePostHandler>().asSingleton()
@@ -59,6 +60,9 @@ class NotificationHandlerModule : AbstractModule() {
        fun getMarkPostKnownHandler(core: Core): Consumer<Post> = Consumer { core.markPostKnown(it) }
 
        @Provides
+       fun getMarkPostReplyKnownHandler(core: Core): Consumer<PostReply> = Consumer { core.markReplyKnown(it) }
+
+       @Provides
        @Singleton
        @Named("soneLockedOnStartup")
        fun getSoneLockedOnStartupNotification(loaders: Loaders) =
diff --git a/src/test/kotlin/net/pterodactylus/sone/web/notification/MarkPostReplyKnownDuringFirstStartHandlerTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/notification/MarkPostReplyKnownDuringFirstStartHandlerTest.kt
new file mode 100644 (file)
index 0000000..3cb463f
--- /dev/null
@@ -0,0 +1,50 @@
+/**
+ * Sone - MarkPostReplyKnownDuringFirstStartHandlerTest.kt - Copyright © 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 <http://www.gnu.org/licenses/>.
+ */
+
+package net.pterodactylus.sone.web.notification
+
+import net.pterodactylus.sone.core.event.*
+import net.pterodactylus.sone.data.*
+import net.pterodactylus.sone.test.*
+import org.hamcrest.MatcherAssert.*
+import org.hamcrest.Matchers.*
+import java.util.function.*
+import kotlin.test.*
+
+/**
+ * Unit test for [MarkPostReplyKnownDuringFirstStartHandler].
+ */
+class MarkPostReplyKnownDuringFirstStartHandlerTest {
+
+       private val markedAsKnown = mutableListOf<PostReply>()
+       private val notificationTester = NotificationHandlerTester { MarkPostReplyKnownDuringFirstStartHandler(it, Consumer { markedAsKnown += it }) }
+       private val postReply = emptyPostReply()
+
+       @Test
+       fun `post reply is marked as known on new reply during first start`() {
+               notificationTester.firstStart()
+               notificationTester.sendEvent(NewPostReplyFoundEvent(postReply))
+               assertThat(markedAsKnown, contains(postReply))
+       }
+
+       @Test
+       fun `post reply is not marked as known on new reply if not during first start`() {
+               notificationTester.sendEvent(NewPostReplyFoundEvent(postReply))
+               assertThat(markedAsKnown, not(hasItem(postReply)))
+       }
+
+}
index 00b0a19..a4ed7af 100644 (file)
@@ -83,6 +83,20 @@ class NotificationHandlerModuleTest {
        }
 
        @Test
+       fun `mark-post-reply-known-during-first-start handler is created as singleton`() {
+               injector.verifySingletonInstance<MarkPostReplyKnownDuringFirstStartHandler>()
+       }
+
+       @Test
+       fun `mark-post-reply-known-during-first-start handler is created with correct action`() {
+               notificationManager.firstStart()
+               val handler = injector.getInstance<MarkPostReplyKnownDuringFirstStartHandler>()
+               val postReply = mock<PostReply>()
+               handler.newPostReply(NewPostReplyFoundEvent(postReply))
+               verify(core).markReplyKnown(postReply)
+       }
+
+       @Test
        fun `sone-locked-on-startup handler is created as singleton`() {
                injector.verifySingletonInstance<SoneLockedOnStartupHandler>()
        }
diff --git a/src/test/kotlin/net/pterodactylus/sone/web/notification/NotificationHandlerTester.kt b/src/test/kotlin/net/pterodactylus/sone/web/notification/NotificationHandlerTester.kt
new file mode 100644 (file)
index 0000000..4d5627f
--- /dev/null
@@ -0,0 +1,58 @@
+/**
+ * Sone - NotificationHandlerTester.kt - Copyright © 2019 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 <http://www.gnu.org/licenses/>.
+ */
+
+package net.pterodactylus.sone.web.notification
+
+import com.google.common.eventbus.*
+import net.pterodactylus.util.notify.*
+
+/**
+ * Helper for testing event handlers that deal with notifications. It contains
+ * a notification manager and an [event bus][EventBus] and automatically
+ * registers the created handler on the event bus.
+ *
+ * ```
+ * val notification = SomeNotification()
+ * val notificationTester = NotificationTester { SomeHandler(it, notification) }
+ *
+ * fun test() {
+ *     notificationTester.sendEvent(SomeEvent())
+ *     assertThat(notificationTester.elements, hasItem(notification))
+ * }
+ * ```
+ */
+@Suppress("UnstableApiUsage")
+class NotificationHandlerTester(createHandler: (NotificationManager) -> Any) {
+
+       private val eventBus = EventBus()
+       private val notificationManager = NotificationManager()
+
+       /** Returns all notifications of the notification manager. */
+       val notifications: Set<Notification>
+               get() = notificationManager.notifications
+
+       init {
+               eventBus.register(createHandler(notificationManager))
+       }
+
+       /** Sends an event to the event bus. */
+       fun sendEvent(event: Any) = eventBus.post(event)
+
+       /** Sets the first-start notification on the notification manager. */
+       fun firstStart() = notificationManager.firstStart()
+
+}