X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;ds=sidebyside;f=src%2Ftest%2Fkotlin%2Fnet%2Fpterodactylus%2Fsone%2Fweb%2Fnotification%2FSoneInsertHandlerTest.kt;fp=src%2Ftest%2Fkotlin%2Fnet%2Fpterodactylus%2Fsone%2Fweb%2Fnotification%2FSoneInsertHandlerTest.kt;h=6a11a90a60ca4201f27c3f328be6b10a74a22d36;hb=a1fa131a44204b2b42de3b5d467373cd113dab28;hp=0000000000000000000000000000000000000000;hpb=d673c44ae56fcaf8368bcf3488f417e3b947158e;p=Sone.git diff --git a/src/test/kotlin/net/pterodactylus/sone/web/notification/SoneInsertHandlerTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/notification/SoneInsertHandlerTest.kt new file mode 100644 index 0000000..6a11a90 --- /dev/null +++ b/src/test/kotlin/net/pterodactylus/sone/web/notification/SoneInsertHandlerTest.kt @@ -0,0 +1,112 @@ +/** + * Sone - SoneInsertHandlerTest.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 . + */ + +package net.pterodactylus.sone.web.notification + +import net.pterodactylus.sone.core.event.* +import net.pterodactylus.sone.test.* +import net.pterodactylus.util.notify.* +import net.pterodactylus.util.template.* +import org.hamcrest.MatcherAssert.* +import org.hamcrest.Matchers.* +import kotlin.test.* + +/** + * Unit test for [SoneInsertHandler]. + */ +class SoneInsertHandlerTest { + + private val localSone = createLocalSone() + private val notification1 = TemplateNotification(Template()) + private val notification2 = TemplateNotification(Template()) + private val soneInsertHandlerTester = NotificationHandlerTester { + SoneInsertHandler(it) { sone -> + if (sone == localSone) notification1 else notification2 + } + } + + @Test + fun `handler adds notification to manager when sone insert starts`() { + localSone.options.isSoneInsertNotificationEnabled = true + soneInsertHandlerTester.sendEvent(SoneInsertingEvent(localSone)) + assertThat(soneInsertHandlerTester.notifications, hasItem(notification1)) + } + + @Test + fun `handler sets sone status in notification when sone insert starts`() { + localSone.options.isSoneInsertNotificationEnabled = true + soneInsertHandlerTester.sendEvent(SoneInsertingEvent(localSone)) + assertThat(notification1.get("soneStatus"), equalTo("inserting")) + } + + @Test + fun `handler does not add notification to manager if option is disabled`() { + localSone.options.isSoneInsertNotificationEnabled = false + soneInsertHandlerTester.sendEvent(SoneInsertingEvent(localSone)) + assertThat(soneInsertHandlerTester.notifications, not(hasItem(notification1))) + } + + @Test + fun `handler adds notification to manager when sone insert finishes`() { + localSone.options.isSoneInsertNotificationEnabled = true + soneInsertHandlerTester.sendEvent(SoneInsertedEvent(localSone, 123456, "")) + assertThat(soneInsertHandlerTester.notifications, hasItem(notification1)) + } + + @Test + fun `handler sets sone status in notification when sone insert finishes`() { + localSone.options.isSoneInsertNotificationEnabled = true + soneInsertHandlerTester.sendEvent(SoneInsertedEvent(localSone, 123456, "")) + assertThat(notification1.get("soneStatus"), equalTo("inserted")) + } + + @Test + fun `handler sets insert duration in notification when sone insert finishes`() { + localSone.options.isSoneInsertNotificationEnabled = true + soneInsertHandlerTester.sendEvent(SoneInsertedEvent(localSone, 123456, "")) + assertThat(notification1.get("insertDuration"), equalTo(123L)) + } + + @Test + fun `handler does not add notification for finished insert to manager if option is disabled`() { + localSone.options.isSoneInsertNotificationEnabled = false + soneInsertHandlerTester.sendEvent(SoneInsertedEvent(localSone, 123456, "")) + assertThat(soneInsertHandlerTester.notifications, not(hasItem(notification1))) + } + + @Test + fun `handler adds notification to manager when sone insert aborts`() { + localSone.options.isSoneInsertNotificationEnabled = true + soneInsertHandlerTester.sendEvent(SoneInsertAbortedEvent(localSone, Exception())) + assertThat(soneInsertHandlerTester.notifications, hasItem(notification1)) + } + + @Test + fun `handler sets sone status in notification when sone insert aborts`() { + localSone.options.isSoneInsertNotificationEnabled = true + soneInsertHandlerTester.sendEvent(SoneInsertAbortedEvent(localSone, Exception())) + assertThat(notification1.get("soneStatus"), equalTo("insert-aborted")) + } + + @Test + fun `handler does not add notification for aborted insert to manager if option is disabled`() { + localSone.options.isSoneInsertNotificationEnabled = false + soneInsertHandlerTester.sendEvent(SoneInsertAbortedEvent(localSone, Exception())) + assertThat(soneInsertHandlerTester.notifications, not(hasItem(notification1))) + } + +}