From: David ‘Bombe’ Roden Date: Fri, 1 Aug 2014 00:20:17 +0000 (+0200) Subject: Add unit test for image inserter. X-Git-Tag: 0.9-rc1^2~3^2~184 X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=746a59d03f62acf17083065011c6048c4d2ddd6f;p=Sone.git Add unit test for image inserter. --- diff --git a/src/test/java/net/pterodactylus/sone/core/ImageInserterTest.java b/src/test/java/net/pterodactylus/sone/core/ImageInserterTest.java new file mode 100644 index 0000000..0912391 --- /dev/null +++ b/src/test/java/net/pterodactylus/sone/core/ImageInserterTest.java @@ -0,0 +1,58 @@ +package net.pterodactylus.sone.core; + +import static org.mockito.Matchers.any; +import static org.mockito.Matchers.eq; +import static org.mockito.Mockito.doThrow; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import net.pterodactylus.sone.core.FreenetInterface.InsertToken; +import net.pterodactylus.sone.data.Image; +import net.pterodactylus.sone.data.TemporaryImage; + +import com.google.common.base.Function; +import org.junit.Test; + +/** + * Unit test for {@link ImageInserter}. + * + * @author David ‘Bombe’ Roden + */ +public class ImageInserterTest { + + private final TemporaryImage temporaryImage = when(mock(TemporaryImage.class).getId()).thenReturn("image-id").getMock(); + private final Image image = when(mock(Image.class).getId()).thenReturn("image-id").getMock(); + private final FreenetInterface freenetInterface = mock(FreenetInterface.class); + private final InsertToken insertToken = mock(InsertToken.class); + private final Function insertTokenSupplier = when(mock(Function.class).apply(any(Image.class))).thenReturn(insertToken).getMock(); + private final ImageInserter imageInserter = new ImageInserter(freenetInterface, insertTokenSupplier); + + @Test + public void inserterInsertsImage() throws SoneException { + imageInserter.insertImage(temporaryImage, image); + verify(freenetInterface).insertImage(eq(temporaryImage), eq(image), any(InsertToken.class)); + } + + @Test + public void exceptionWhenInsertingImageIsIgnored() throws SoneException { + doThrow(SoneException.class).when(freenetInterface).insertImage(eq(temporaryImage), eq(image), any(InsertToken.class)); + imageInserter.insertImage(temporaryImage, image); + verify(freenetInterface).insertImage(eq(temporaryImage), eq(image), any(InsertToken.class)); + } + + @Test + public void cancellingImageInsertThatIsNotRunningDoesNothing() { + imageInserter.cancelImageInsert(image); + verify(insertToken, never()).cancel(); + } + + @Test + public void cancellingImage() { + imageInserter.insertImage(temporaryImage, image); + imageInserter.cancelImageInsert(image); + verify(insertToken).cancel(); + } + +}