Add test for PostPart
[Sone.git] / src / test / java / net / pterodactylus / sone / core / ImageInserterTest.java
1 package net.pterodactylus.sone.core;
2
3 import static org.mockito.Matchers.any;
4 import static org.mockito.Matchers.eq;
5 import static org.mockito.Mockito.doThrow;
6 import static org.mockito.Mockito.mock;
7 import static org.mockito.Mockito.never;
8 import static org.mockito.Mockito.verify;
9 import static org.mockito.Mockito.when;
10
11 import net.pterodactylus.sone.core.FreenetInterface.InsertToken;
12 import net.pterodactylus.sone.data.Image;
13 import net.pterodactylus.sone.data.TemporaryImage;
14
15 import com.google.common.base.Function;
16 import org.junit.Test;
17
18 /**
19  * Unit test for {@link ImageInserter}.
20  *
21  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
22  */
23 public class ImageInserterTest {
24
25         private final TemporaryImage temporaryImage = when(mock(TemporaryImage.class).getId()).thenReturn("image-id").getMock();
26         private final Image image = when(mock(Image.class).getId()).thenReturn("image-id").getMock();
27         private final FreenetInterface freenetInterface = mock(FreenetInterface.class);
28         private final InsertToken insertToken = mock(InsertToken.class);
29         private final Function<Image, InsertToken> insertTokenSupplier = when(mock(Function.class).apply(any(Image.class))).thenReturn(insertToken).getMock();
30         private final ImageInserter imageInserter = new ImageInserter(freenetInterface, insertTokenSupplier);
31
32         @Test
33         public void inserterInsertsImage() throws SoneException {
34                 imageInserter.insertImage(temporaryImage, image);
35                 verify(freenetInterface).insertImage(eq(temporaryImage), eq(image), any(InsertToken.class));
36         }
37
38         @Test
39         public void exceptionWhenInsertingImageIsIgnored() throws SoneException {
40                 doThrow(SoneException.class).when(freenetInterface).insertImage(eq(temporaryImage), eq(image), any(InsertToken.class));
41                 imageInserter.insertImage(temporaryImage, image);
42                 verify(freenetInterface).insertImage(eq(temporaryImage), eq(image), any(InsertToken.class));
43         }
44
45         @Test
46         public void cancellingImageInsertThatIsNotRunningDoesNothing() {
47                 imageInserter.cancelImageInsert(image);
48                 verify(insertToken, never()).cancel();
49         }
50
51         @Test
52         public void cancellingImage() {
53                 imageInserter.insertImage(temporaryImage, image);
54                 imageInserter.cancelImageInsert(image);
55                 verify(insertToken).cancel();
56         }
57
58 }