1 package net.pterodactylus.sone.core;
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;
11 import net.pterodactylus.sone.core.FreenetInterface.InsertToken;
12 import net.pterodactylus.sone.data.Image;
13 import net.pterodactylus.sone.data.TemporaryImage;
15 import com.google.common.base.Function;
16 import org.junit.Test;
19 * Unit test for {@link ImageInserter}.
21 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
23 public class ImageInserterTest {
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);
33 public void inserterInsertsImage() throws SoneException {
34 imageInserter.insertImage(temporaryImage, image);
35 verify(freenetInterface).insertImage(eq(temporaryImage), eq(image), any(InsertToken.class));
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));
46 public void cancellingImageInsertThatIsNotRunningDoesNothing() {
47 imageInserter.cancelImageInsert(image);
48 verify(insertToken, never()).cancel();
52 public void cancellingImage() {
53 imageInserter.insertImage(temporaryImage, image);
54 imageInserter.cancelImageInsert(image);
55 verify(insertToken).cancel();