X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Ftest%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fcore%2FFreenetInterfaceTest.java;h=d2a11217cbb5f0ef880a2c8f6203770d2a30cf3c;hb=f67d0cbb1bc328c0072fe5f795ffb31cf1b987bb;hp=9e8852b0035c9458019004d5ad905386f1b477ee;hpb=8677d84408d2495fe6c3e114e64accd28d6576ec;p=Sone.git diff --git a/src/test/java/net/pterodactylus/sone/core/FreenetInterfaceTest.java b/src/test/java/net/pterodactylus/sone/core/FreenetInterfaceTest.java index 9e8852b..d2a1121 100644 --- a/src/test/java/net/pterodactylus/sone/core/FreenetInterfaceTest.java +++ b/src/test/java/net/pterodactylus/sone/core/FreenetInterfaceTest.java @@ -1,5 +1,7 @@ package net.pterodactylus.sone.core; +import static freenet.client.InsertException.CANCELLED; +import static freenet.client.InsertException.INTERNAL_ERROR; import static freenet.keys.InsertableClientSSK.createRandom; import static freenet.node.RequestStarter.INTERACTIVE_PRIORITY_CLASS; import static freenet.node.RequestStarter.PREFETCH_PRIORITY_CLASS; @@ -21,6 +23,7 @@ import static org.mockito.Mockito.doAnswer; import static org.mockito.Mockito.doNothing; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.never; +import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; import static org.mockito.Mockito.withSettings; @@ -34,6 +37,10 @@ import net.pterodactylus.sone.TestUtil; import net.pterodactylus.sone.core.FreenetInterface.Callback; import net.pterodactylus.sone.core.FreenetInterface.Fetched; import net.pterodactylus.sone.core.FreenetInterface.InsertToken; +import net.pterodactylus.sone.core.FreenetInterface.InsertTokenSupplier; +import net.pterodactylus.sone.core.event.ImageInsertAbortedEvent; +import net.pterodactylus.sone.core.event.ImageInsertFailedEvent; +import net.pterodactylus.sone.core.event.ImageInsertFinishedEvent; import net.pterodactylus.sone.core.event.ImageInsertStartedEvent; import net.pterodactylus.sone.data.Image; import net.pterodactylus.sone.data.ImageImpl; @@ -86,6 +93,9 @@ public class FreenetInterfaceTest { private final Sone sone = mock(Sone.class); private final ArgumentCaptor callbackCaptor = forClass(USKCallback.class); private final SoneDownloader soneDownloader = mock(SoneDownloader.class); + private final SoneUpdater soneUpdater = mock(SoneUpdater.class); + private final Image image = mock(Image.class); + private InsertToken insertToken; @Before public void setupFreenetInterface() { @@ -94,6 +104,7 @@ public class FreenetInterfaceTest { setFinalField(node, "random", randomSource); setFinalField(nodeClientCore, "uskManager", uskManager); freenetInterface = new FreenetInterface(eventBus, node); + insertToken = freenetInterface.new InsertToken(image); } @Before @@ -280,32 +291,9 @@ public class FreenetInterfaceTest { @Test public void callbackForRegisteredSoneWithHigherEditionTriggersDownload() throws InterruptedException { - freenetInterface.registerUsk(sone, soneDownloader); - final CountDownLatch downloadTriggered = new CountDownLatch(1); - doAnswer(new Answer() { - @Override - public Void answer(InvocationOnMock invocation) throws Throwable { - downloadTriggered.countDown(); - return null; - } - }).when(soneDownloader).fetchSone(sone); + freenetInterface.registerUsk(sone, soneUpdater); callbackCaptor.getValue().onFoundEdition(1, null, null, null, false, (short) 0, null, false, false); - assertThat(downloadTriggered.await(1, SECONDS), is(true)); - } - - @Test - public void callbackForRegisteredSoneWithTheSameEditionDoesNotTriggerDownload() throws InterruptedException { - freenetInterface.registerUsk(sone, soneDownloader); - final CountDownLatch downloadTriggered = new CountDownLatch(1); - doAnswer(new Answer() { - @Override - public Void answer(InvocationOnMock invocation) throws Throwable { - downloadTriggered.countDown(); - return null; - } - }).when(soneDownloader).fetchSone(sone); - callbackCaptor.getValue().onFoundEdition(0, null, null, null, false, (short) 0, null, false, false); - assertThat(downloadTriggered.await(1, SECONDS), is(false)); + verify(soneUpdater).updateSone(1); } @Test @@ -337,4 +325,69 @@ public class FreenetInterfaceTest { assertThat(fetched.getFetchResult(), is(fetchResult)); } + @Test + public void cancellingAnInsertWillFireImageInsertAbortedEvent() { + ClientPutter clientPutter = mock(ClientPutter.class); + insertToken.setClientPutter(clientPutter); + ArgumentCaptor imageInsertStartedEvent = forClass(ImageInsertStartedEvent.class); + verify(eventBus).post(imageInsertStartedEvent.capture()); + assertThat(imageInsertStartedEvent.getValue().image(), is(image)); + insertToken.cancel(); + ArgumentCaptor imageInsertAbortedEvent = forClass(ImageInsertAbortedEvent.class); + verify(eventBus, times(2)).post(imageInsertAbortedEvent.capture()); + assertThat(imageInsertAbortedEvent.getValue().image(), is(image)); + } + + @Test + public void failureWithoutExceptionSendsFailedEvent() { + insertToken.onFailure(null, null, null); + ArgumentCaptor imageInsertFailedEvent = forClass(ImageInsertFailedEvent.class); + verify(eventBus).post(imageInsertFailedEvent.capture()); + assertThat(imageInsertFailedEvent.getValue().image(), is(image)); + assertThat(imageInsertFailedEvent.getValue().cause(), nullValue()); + } + + @Test + public void failureSendsFailedEventWithException() { + InsertException insertException = new InsertException(INTERNAL_ERROR, "Internal error", null); + insertToken.onFailure(insertException, null, null); + ArgumentCaptor imageInsertFailedEvent = forClass(ImageInsertFailedEvent.class); + verify(eventBus).post(imageInsertFailedEvent.capture()); + assertThat(imageInsertFailedEvent.getValue().image(), is(image)); + assertThat(imageInsertFailedEvent.getValue().cause(), is((Throwable) insertException)); + } + + @Test + public void failureBecauseCancelledByUserSendsAbortedEvent() { + InsertException insertException = new InsertException(CANCELLED, null); + insertToken.onFailure(insertException, null, null); + ArgumentCaptor imageInsertAbortedEvent = forClass(ImageInsertAbortedEvent.class); + verify(eventBus).post(imageInsertAbortedEvent.capture()); + assertThat(imageInsertAbortedEvent.getValue().image(), is(image)); + } + + @Test + public void ignoredMethodsDoNotThrowExceptions() { + insertToken.onMajorProgress(null); + insertToken.onFetchable(null, null); + insertToken.onGeneratedMetadata(null, null, null); + } + + @Test + public void generatedUriIsPostedOnSuccess() { + FreenetURI generatedUri = mock(FreenetURI.class); + insertToken.onGeneratedURI(generatedUri, null, null); + insertToken.onSuccess(null, null); + ArgumentCaptor imageInsertFinishedEvent = forClass(ImageInsertFinishedEvent.class); + verify(eventBus).post(imageInsertFinishedEvent.capture()); + assertThat(imageInsertFinishedEvent.getValue().image(), is(image)); + assertThat(imageInsertFinishedEvent.getValue().resultingUri(), is(generatedUri)); + } + + @Test + public void insertTokenSupplierSuppliesInsertTokens() { + InsertTokenSupplier insertTokenSupplier = freenetInterface.new InsertTokenSupplier(); + assertThat(insertTokenSupplier.apply(image), notNullValue()); + } + }