1 package net.pterodactylus.sone.core
3 import net.pterodactylus.sone.core.FreenetInterface.InsertToken
4 import net.pterodactylus.sone.core.FreenetInterface.InsertTokenSupplier
5 import net.pterodactylus.sone.data.Image
6 import net.pterodactylus.sone.data.TemporaryImage
7 import net.pterodactylus.sone.test.getInstance
8 import net.pterodactylus.sone.test.mock
9 import net.pterodactylus.sone.test.whenever
10 import net.pterodactylus.sone.web.baseInjector
11 import org.hamcrest.MatcherAssert.assertThat
12 import org.hamcrest.Matchers.notNullValue
14 import org.mockito.ArgumentMatchers.any
15 import org.mockito.ArgumentMatchers.eq
16 import org.mockito.Mockito.doThrow
17 import org.mockito.Mockito.never
18 import org.mockito.Mockito.verify
21 * Unit test for [ImageInserter].
23 class ImageInserterTest {
25 private val temporaryImage = mock<TemporaryImage>().apply { whenever(id).thenReturn("image-id") }
26 private val image = mock<Image>().apply { whenever(id).thenReturn("image-id") }
27 private val freenetInterface = mock<FreenetInterface>()
28 private val insertToken = mock<InsertToken>()
29 private val insertTokenSupplier: InsertTokenSupplier = mock<InsertTokenSupplier>().apply { whenever(apply(any())).thenReturn(insertToken) }
30 private val imageInserter = ImageInserter(freenetInterface, insertTokenSupplier)
33 fun `inserter inserts image`() {
34 imageInserter.insertImage(temporaryImage, image)
35 verify(freenetInterface).insertImage(eq(temporaryImage), eq(image), any(InsertToken::class.java))
39 fun `exception when inserting image is ignored`() {
40 doThrow(SoneException::class.java).`when`(freenetInterface).insertImage(eq(temporaryImage), eq(image), any(InsertToken::class.java))
41 imageInserter.insertImage(temporaryImage, image)
42 verify(freenetInterface).insertImage(eq(temporaryImage), eq(image), any(InsertToken::class.java))
46 fun `cancelling image insert that is not running does nothing`() {
47 imageInserter.cancelImageInsert(image)
48 verify(insertToken, never()).cancel()
52 fun `cancelling image cancels the insert token`() {
53 imageInserter.insertImage(temporaryImage, image)
54 imageInserter.cancelImageInsert(image)
55 verify(insertToken).cancel()
59 fun `image inserter can be created by dependency injection`() {
60 assertThat(baseInjector.getInstance<ImageInserter>(), notNullValue())