X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Ftest%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fcore%2FFreenetInterfaceTest.java;h=04b5b67d01d080483a6a2fae853f897d45697922;hb=c635cc7615b8cea5f0a557143889c3ab7dcd0394;hp=091c93f9705b24a22b3c141eca9f49d8169c7144;hpb=f229fe41f708d2b275c20ceb9aba5993761218a3;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 091c93f..04b5b67 100644 --- a/src/test/java/net/pterodactylus/sone/core/FreenetInterfaceTest.java +++ b/src/test/java/net/pterodactylus/sone/core/FreenetInterfaceTest.java @@ -1,5 +1,6 @@ package net.pterodactylus.sone.core; +import static freenet.client.FetchException.FetchExceptionMode.ALL_DATA_NOT_FOUND; import static freenet.keys.InsertableClientSSK.createRandom; import static freenet.node.RequestStarter.INTERACTIVE_PRIORITY_CLASS; import static freenet.node.RequestStarter.PREFETCH_PRIORITY_CLASS; @@ -10,15 +11,17 @@ import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.notNullValue; import static org.hamcrest.Matchers.nullValue; import static org.mockito.ArgumentCaptor.forClass; -import static org.mockito.Matchers.any; -import static org.mockito.Matchers.anyBoolean; -import static org.mockito.Matchers.anyShort; -import static org.mockito.Matchers.eq; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyBoolean; +import static org.mockito.ArgumentMatchers.anyLong; +import static org.mockito.ArgumentMatchers.anyShort; +import static org.mockito.ArgumentMatchers.eq; 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.verifyNoMoreInteractions; import static org.mockito.Mockito.when; import static org.mockito.Mockito.withSettings; @@ -27,6 +30,7 @@ import java.net.MalformedURLException; import java.util.HashMap; import net.pterodactylus.sone.TestUtil; +import net.pterodactylus.sone.core.FreenetInterface.BackgroundFetchCallback; import net.pterodactylus.sone.core.FreenetInterface.Callback; import net.pterodactylus.sone.core.FreenetInterface.Fetched; import net.pterodactylus.sone.core.FreenetInterface.InsertToken; @@ -36,11 +40,12 @@ 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.impl.ImageImpl; import net.pterodactylus.sone.data.Sone; import net.pterodactylus.sone.data.TemporaryImage; +import net.pterodactylus.sone.data.impl.ImageImpl; import freenet.client.ClientMetadata; +import freenet.client.FetchContext; import freenet.client.FetchException; import freenet.client.FetchException.FetchExceptionMode; import freenet.client.FetchResult; @@ -49,6 +54,8 @@ import freenet.client.InsertBlock; import freenet.client.InsertContext; import freenet.client.InsertException; import freenet.client.InsertException.InsertExceptionMode; +import freenet.client.async.ClientGetCallback; +import freenet.client.async.ClientGetter; import freenet.client.async.ClientPutter; import freenet.client.async.USKCallback; import freenet.client.async.USKManager; @@ -69,6 +76,7 @@ import com.google.common.eventbus.EventBus; import org.junit.Before; import org.junit.Test; import org.mockito.ArgumentCaptor; +import org.mockito.ArgumentMatchers; /** * Unit test for {@link FreenetInterface}. @@ -89,6 +97,18 @@ public class FreenetInterfaceTest { private final Image image = mock(Image.class); private InsertToken insertToken; private final Bucket bucket = mock(Bucket.class); + private final ArgumentCaptor clientGetCallback = forClass(ClientGetCallback.class); + private final FreenetURI uri = new FreenetURI("KSK@pgl.png"); + private final FetchResult fetchResult = mock(FetchResult.class); + private final BackgroundFetchCallback backgroundFetchCallback = mock(BackgroundFetchCallback.class); + + public FreenetInterfaceTest() throws MalformedURLException { + } + + @Before + public void setupHighLevelSimpleClient() { + when(highLevelSimpleClient.getFetchContext()).thenReturn(mock(FetchContext.class)); + } @Before public void setupFreenetInterface() { @@ -140,7 +160,7 @@ public class FreenetInterfaceTest { @Test public void fetchReturnsNullOnFetchExceptions() throws MalformedURLException, FetchException { FreenetURI freenetUri = new FreenetURI("KSK@GPLv2.txt"); - FetchException fetchException = new FetchException(FetchExceptionMode.ALL_DATA_NOT_FOUND); + FetchException fetchException = new FetchException(ALL_DATA_NOT_FOUND); when(highLevelSimpleClient.fetch(freenetUri)).thenThrow(fetchException); Fetched fetched = freenetInterface.fetchUri(freenetUri); assertThat(fetched, nullValue()); @@ -198,7 +218,7 @@ public class FreenetInterfaceTest { @Test(expected = SoneException.class) public void insertExceptionIsForwardedAsSoneException() throws InsertException, SoneException { - when(highLevelSimpleClient.insertManifest(any(FreenetURI.class), any(HashMap.class), any(String.class))).thenThrow(InsertException.class); + when(highLevelSimpleClient.insertManifest(ArgumentMatchers.any(), ArgumentMatchers.>any(), ArgumentMatchers.any())).thenThrow(InsertException.class); freenetInterface.insertDirectory(null, null, null); } @@ -245,7 +265,7 @@ public class FreenetInterfaceTest { public void registeringAnActiveNonUskWillNotSubscribeToAUsk() throws MalformedURLException { FreenetURI freenetUri = createRandom(randomSource, "test-0").getURI(); - freenetInterface.registerActiveUsk(freenetUri, null); + freenetInterface.registerActiveUsk(freenetUri, null); verify(uskManager, never()).subscribe(any(USK.class), any(USKCallback.class), anyBoolean(), eq((RequestClient) highLevelSimpleClient)); @@ -255,7 +275,7 @@ public class FreenetInterfaceTest { public void registeringAnInactiveNonUskWillNotSubscribeToAUsk() throws MalformedURLException { FreenetURI freenetUri = createRandom(randomSource, "test-0").getURI(); - freenetInterface.registerPassiveUsk(freenetUri, null); + freenetInterface.registerPassiveUsk(freenetUri, null); verify(uskManager, never()).subscribe(any(USK.class), any(USKCallback.class), anyBoolean(), eq((RequestClient) highLevelSimpleClient)); @@ -398,4 +418,43 @@ public class FreenetInterfaceTest { assertThat(insertTokenSupplier.apply(image), notNullValue()); } + @Test + public void backgroundFetchCanBeStarted() throws Exception { + freenetInterface.startFetch(uri, backgroundFetchCallback); + verify(highLevelSimpleClient).fetch(eq(uri), anyLong(), any(ClientGetCallback.class), any(FetchContext.class), anyShort()); + } + + @Test + public void callbackOfBackgroundFetchIsNotifiedOnSuccess() throws Exception { + freenetInterface.startFetch(uri, backgroundFetchCallback); + verify(highLevelSimpleClient).fetch(eq(uri), anyLong(), clientGetCallback.capture(), any(FetchContext.class), anyShort()); + when(fetchResult.getMimeType()).thenReturn("image/png"); + when(fetchResult.asByteArray()).thenReturn(new byte[] { 1, 2, 3, 4, 5 }); + clientGetCallback.getValue().onSuccess(fetchResult, mock(ClientGetter.class)); + verify(backgroundFetchCallback).loaded(uri, "image/png", new byte[] { 1, 2, 3, 4, 5 }); + verifyNoMoreInteractions(backgroundFetchCallback); + } + + @Test + public void callbackOfBackgroundFetchIsNotifiedOnFailure() throws Exception { + freenetInterface.startFetch(uri, backgroundFetchCallback); + verify(highLevelSimpleClient).fetch(eq(uri), anyLong(), clientGetCallback.capture(), any(FetchContext.class), anyShort()); + when(fetchResult.getMimeType()).thenReturn("image/png"); + when(fetchResult.asByteArray()).thenReturn(new byte[] { 1, 2, 3, 4, 5 }); + clientGetCallback.getValue().onFailure(new FetchException(ALL_DATA_NOT_FOUND), mock(ClientGetter.class)); + verify(backgroundFetchCallback).failed(uri); + verifyNoMoreInteractions(backgroundFetchCallback); + } + + @Test + public void callbackOfBackgroundFetchIsNotifiedAsFailureIfBucketCanNotBeLoaded() throws Exception { + freenetInterface.startFetch(uri, backgroundFetchCallback); + verify(highLevelSimpleClient).fetch(eq(uri), anyLong(), clientGetCallback.capture(), any(FetchContext.class), anyShort()); + when(fetchResult.getMimeType()).thenReturn("image/png"); + when(fetchResult.asByteArray()).thenThrow(IOException.class); + clientGetCallback.getValue().onSuccess(fetchResult, mock(ClientGetter.class)); + verify(backgroundFetchCallback).failed(uri); + verifyNoMoreInteractions(backgroundFetchCallback); + } + }