🔥 Remove “create” flag from session provider interface
[Sone.git] / src / test / kotlin / net / pterodactylus / sone / core / ImageInserterTest.kt
1 package net.pterodactylus.sone.core
2
3 import net.pterodactylus.sone.core.FreenetInterface.InsertToken
4 import net.pterodactylus.sone.core.FreenetInterface.InsertTokenSupplier
5 import net.pterodactylus.sone.data.TemporaryImage
6 import net.pterodactylus.sone.data.impl.*
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
13 import org.junit.Test
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
19
20 /**
21  * Unit test for [ImageInserter].
22  */
23 class ImageInserterTest {
24
25         private val temporaryImage = mock<TemporaryImage>().apply { whenever(id).thenReturn("image-id") }
26         private val image = ImageImpl("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)
31
32         @Test
33         fun `inserter inserts image`() {
34                 imageInserter.insertImage(temporaryImage, image)
35                 verify(freenetInterface).insertImage(eq(temporaryImage), eq(image), any(InsertToken::class.java))
36         }
37
38         @Test
39         fun `exception when inserting image is ignored`() {
40                 doThrow(SoneException::class.java).whenever(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))
43         }
44
45         @Test
46         fun `cancelling image insert that is not running does nothing`() {
47                 imageInserter.cancelImageInsert(image)
48                 verify(insertToken, never()).cancel()
49         }
50
51         @Test
52         fun `cancelling image cancels the insert token`() {
53                 imageInserter.insertImage(temporaryImage, image)
54                 imageInserter.cancelImageInsert(image)
55                 verify(insertToken).cancel()
56         }
57
58         @Test
59         fun `image inserter can be created by dependency injection`() {
60             assertThat(baseInjector.getInstance<ImageInserter>(), notNullValue())
61         }
62
63 }