🐛 Fix NPE during Sone removal
[Sone.git] / src / test / kotlin / net / pterodactylus / sone / web / notification / LocalPostHandlerTest.kt
index d435101..70aa709 100644 (file)
@@ -1,5 +1,5 @@
 /**
- * Sone - NewLocalPostHandlerTest.kt - Copyright © 2019 David ‘Bombe’ Roden
+ * Sone - LocalPostHandlerTest.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
@@ -22,11 +22,11 @@ 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.createPost
 import net.pterodactylus.util.notify.*
 import net.pterodactylus.util.template.*
 import org.hamcrest.MatcherAssert.*
 import org.hamcrest.Matchers.*
-import java.io.*
 import kotlin.test.*
 
 /**
@@ -55,17 +55,21 @@ class LocalPostHandlerTest {
        }
 
        @Test
-       fun `handler adds notification to manager`() {
+       fun `handler does not add notification to manager for post by remote sone`() {
                eventBus.post(NewPostFoundEvent(remotePost))
+               assertThat(notificationManager.notifications, not(hasItem<Notification>(notification)))
+       }
+
+       @Test
+       fun `handler adds notification to manager`() {
+               eventBus.post(NewPostFoundEvent(localPost))
                assertThat(notificationManager.notifications, contains<Notification>(notification))
        }
 
        @Test
        fun `handler does not add notification during first start`() {
-               notificationManager.addNotification(object : AbstractNotification("first-start-notification") {
-                       override fun render(writer: Writer?) = Unit
-               })
-               eventBus.post(NewPostFoundEvent(remotePost))
+               notificationManager.firstStart()
+               eventBus.post(NewPostFoundEvent(localPost))
                assertThat(notificationManager.notifications, not(hasItem<Notification>(notification)))
        }
 
@@ -86,6 +90,15 @@ class LocalPostHandlerTest {
        }
 
        @Test
+       // this scenario can happen when sones are removed.
+       fun `handler removes post from notification if sone is missing`() {
+               val post = createPost(sone = null)
+               notification.add(post)
+               eventBus.post(PostRemovedEvent(post))
+               assertThat(notification.elements, emptyIterable())
+       }
+
+       @Test
        fun `handler removes post from notification when post is marked as known`() {
                notification.add(localPost)
                notificationManager.addNotification(notification)