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 java.lang.System.currentTimeMillis;
-import static java.util.concurrent.TimeUnit.DAYS;
-import static java.util.concurrent.TimeUnit.SECONDS;
+import static freenet.node.RequestStarter.PREFETCH_PRIORITY_CLASS;
import static net.pterodactylus.sone.Matchers.delivers;
+import static net.pterodactylus.sone.TestUtil.setFinalField;
import static org.hamcrest.MatcherAssert.assertThat;
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.Mockito.doAnswer;
+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;
import java.io.IOException;
-import java.lang.reflect.Field;
-import java.lang.reflect.Modifier;
import java.net.MalformedURLException;
import java.util.HashMap;
-import java.util.concurrent.CountDownLatch;
+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;
+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;
import net.pterodactylus.sone.data.Sone;
import net.pterodactylus.sone.data.TemporaryImage;
-import net.pterodactylus.sone.freenet.StringBucket;
+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;
import freenet.client.HighLevelSimpleClient;
import freenet.client.InsertBlock;
import freenet.client.InsertContext;
import freenet.client.InsertException;
+import freenet.client.InsertException.InsertExceptionMode;
+import freenet.client.Metadata;
+import freenet.client.async.ClientContext;
+import freenet.client.async.ClientGetCallback;
+import freenet.client.async.ClientGetter;
import freenet.client.async.ClientPutter;
+import freenet.client.async.SnoopMetadata;
import freenet.client.async.USKCallback;
import freenet.client.async.USKManager;
import freenet.crypt.DummyRandomSource;
import freenet.node.RequestClient;
import freenet.support.Base64;
import freenet.support.api.Bucket;
+import freenet.support.io.ArrayBucket;
+import freenet.support.io.ResumeFailedException;
import com.google.common.eventbus.EventBus;
import org.junit.Before;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
-import org.mockito.invocation.InvocationOnMock;
-import org.mockito.stubbing.Answer;
+import org.mockito.ArgumentMatchers;
/**
* Unit test for {@link FreenetInterface}.
private FreenetInterface freenetInterface;
private final Sone sone = mock(Sone.class);
private final ArgumentCaptor<USKCallback> callbackCaptor = forClass(USKCallback.class);
- private final SoneDownloader soneDownloader = mock(SoneDownloader.class);
+ private final Image image = mock(Image.class);
+ private InsertToken insertToken;
+ private final Bucket bucket = mock(Bucket.class);
+ private final ArgumentCaptor<ClientGetCallback> 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);
+ private final ClientGetter clientGetter = mock(ClientGetter.class);
+
+ public FreenetInterfaceTest() throws MalformedURLException {
+ }
+
+ @Before
+ public void setupHighLevelSimpleClient() throws Exception {
+ when(highLevelSimpleClient.getFetchContext()).thenReturn(mock(FetchContext.class));
+ when(highLevelSimpleClient.fetch(eq(uri), anyLong(), any(ClientGetCallback.class), any(FetchContext.class), anyShort())).thenReturn( clientGetter);
+ }
@Before
public void setupFreenetInterface() {
setFinalField(node, "clientCore", nodeClientCore);
setFinalField(node, "random", randomSource);
setFinalField(nodeClientCore, "uskManager", uskManager);
+ setFinalField(nodeClientCore, "clientContext", mock(ClientContext.class));
freenetInterface = new FreenetInterface(eventBus, node);
+ insertToken = freenetInterface.new InsertToken(image);
+ insertToken.setBucket(bucket);
}
@Before
FreenetURI freenetUri = new FreenetURI("KSK@GPLv2.txt");
FreenetURI newFreenetUri = new FreenetURI("KSK@GPLv3.txt");
FetchResult fetchResult = createFetchResult();
- FetchException fetchException = new FetchException(FetchException.PERMANENT_REDIRECT, newFreenetUri);
+ FetchException fetchException = new FetchException(FetchExceptionMode.PERMANENT_REDIRECT, newFreenetUri);
when(highLevelSimpleClient.fetch(freenetUri)).thenThrow(fetchException);
when(highLevelSimpleClient.fetch(newFreenetUri)).thenReturn(fetchResult);
Fetched fetched = freenetInterface.fetchUri(freenetUri);
@Test
public void fetchReturnsNullOnFetchExceptions() throws MalformedURLException, FetchException {
FreenetURI freenetUri = new FreenetURI("KSK@GPLv2.txt");
- FetchException fetchException = new FetchException(FetchException.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());
private FetchResult createFetchResult() {
ClientMetadata clientMetadata = new ClientMetadata("text/plain");
- Bucket bucket = new StringBucket("Some Data.");
+ Bucket bucket = new ArrayBucket("Some Data.".getBytes());
return new FetchResult(clientMetadata, bucket);
}
- private void setFinalField(Object object, String fieldName, Object value) {
- try {
- Field clientCoreField = object.getClass().getField(fieldName);
- clientCoreField.setAccessible(true);
- Field modifiersField = Field.class.getDeclaredField("modifiers");
- modifiersField.setAccessible(true);
- modifiersField.setInt(clientCoreField, clientCoreField.getModifiers() & ~Modifier.FINAL);
- clientCoreField.set(object, value);
- } catch (NoSuchFieldException e) {
- throw new RuntimeException(e);
- } catch (IllegalAccessException e) {
- throw new RuntimeException(e);
- }
- }
-
@Test
public void insertingAnImage() throws SoneException, InsertException, IOException {
TemporaryImage temporaryImage = new TemporaryImage("image-id");
when(highLevelSimpleClient.getInsertContext(anyBoolean())).thenReturn(insertContext);
ClientPutter clientPutter = mock(ClientPutter.class);
ArgumentCaptor<InsertBlock> insertBlockCaptor = forClass(InsertBlock.class);
- when(highLevelSimpleClient.insert(insertBlockCaptor.capture(), eq(false), eq((String) null), eq(false), eq(insertContext), eq(insertToken), anyShort())).thenReturn(clientPutter);
+ when(highLevelSimpleClient.insert(insertBlockCaptor.capture(), eq((String) null), eq(false), eq(insertContext), eq(insertToken), anyShort())).thenReturn(clientPutter);
freenetInterface.insertImage(temporaryImage, image, insertToken);
assertThat(insertBlockCaptor.getValue().getData().getInputStream(), delivers(new byte[] { 1, 2, 3, 4 }));
- assertThat(this.<ClientPutter>getPrivateField(insertToken, "clientPutter"), is(clientPutter));
+ assertThat(TestUtil.<ClientPutter>getPrivateField(insertToken, "clientPutter"), is(clientPutter));
verify(eventBus).post(any(ImageInsertStartedEvent.class));
}
- private <T> T getPrivateField(Object object, String fieldName) {
- try {
- Field field = object.getClass().getDeclaredField(fieldName);
- field.setAccessible(true);
- return (T) field.get(object);
- } catch (NoSuchFieldException e) {
- throw new RuntimeException(e);
- } catch (IllegalAccessException e) {
- throw new RuntimeException(e);
- }
- }
-
@Test(expected = SoneInsertException.class)
public void insertExceptionCausesASoneException() throws InsertException, SoneException, IOException {
TemporaryImage temporaryImage = new TemporaryImage("image-id");
InsertContext insertContext = mock(InsertContext.class);
when(highLevelSimpleClient.getInsertContext(anyBoolean())).thenReturn(insertContext);
ArgumentCaptor<InsertBlock> insertBlockCaptor = forClass(InsertBlock.class);
- when(highLevelSimpleClient.insert(insertBlockCaptor.capture(), eq(false), eq((String) null), eq(false), eq(insertContext), eq(insertToken), anyShort())).thenThrow(InsertException.class);
+ when(highLevelSimpleClient.insert(insertBlockCaptor.capture(), eq((String) null), eq(false), eq(insertContext), eq(insertToken), anyShort())).thenThrow(InsertException.class);
freenetInterface.insertImage(temporaryImage, image, insertToken);
}
@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.<FreenetURI>any(), ArgumentMatchers.<HashMap<String, Object>>any(), ArgumentMatchers.<String>any())).thenThrow(InsertException.class);
freenetInterface.insertDirectory(null, null, null);
}
@Test
public void soneWithWrongRequestUriWillNotBeSubscribed() throws MalformedURLException {
when(sone.getRequestUri()).thenReturn(new FreenetURI("KSK@GPLv3.txt"));
- freenetInterface.registerUsk(sone, null);
+ freenetInterface.registerUsk(new FreenetURI("KSK@GPLv3.txt"), null);
verify(uskManager, never()).subscribe(any(USK.class), any(USKCallback.class), anyBoolean(), any(RequestClient.class));
}
@Test
- public void registeringAUskForARecentlyModifiedSone() throws MalformedURLException {
- when(sone.getTime()).thenReturn(currentTimeMillis() - DAYS.toMillis(1));
- freenetInterface.registerUsk(sone, null);
- verify(uskManager).subscribe(any(USK.class), any(USKCallback.class), eq(true), eq((RequestClient) highLevelSimpleClient));
- }
-
- @Test
- public void registeringAUskForAnOldSone() throws MalformedURLException {
- when(sone.getTime()).thenReturn(currentTimeMillis() - DAYS.toMillis(365));
- freenetInterface.registerUsk(sone, null);
- verify(uskManager).subscribe(any(USK.class), any(USKCallback.class), eq(false), eq((RequestClient) highLevelSimpleClient));
- }
-
- @Test
public void registeringAUsk() {
FreenetURI freenetUri = createRandom(randomSource, "test-0").getURI().uskForSSK();
Callback callback = mock(Callback.class);
}
@Test
+ public void registeringAnActiveUskWillSubscribeToItCorrectly() {
+ FreenetURI freenetUri = createRandom(randomSource, "test-0").getURI().uskForSSK();
+ final USKCallback uskCallback = mock(USKCallback.class);
+ freenetInterface.registerActiveUsk(freenetUri, uskCallback);
+ verify(uskManager).subscribe(any(USK.class), eq(uskCallback), eq(true), any(RequestClient.class));
+ }
+
+ @Test
+ public void registeringAnInactiveUskWillSubscribeToItCorrectly() {
+ FreenetURI freenetUri = createRandom(randomSource, "test-0").getURI().uskForSSK();
+ final USKCallback uskCallback = mock(USKCallback.class);
+ freenetInterface.registerPassiveUsk(freenetUri, uskCallback);
+ verify(uskManager).subscribe(any(USK.class), eq(uskCallback), eq(false), any(RequestClient.class));
+ }
+
+ @Test
+ public void registeringAnActiveNonUskWillNotSubscribeToAUsk()
+ throws MalformedURLException {
+ FreenetURI freenetUri = createRandom(randomSource, "test-0").getURI();
+ freenetInterface.registerActiveUsk(freenetUri, null);
+ verify(uskManager, never()).subscribe(any(USK.class),
+ any(USKCallback.class), anyBoolean(),
+ eq((RequestClient) highLevelSimpleClient));
+ }
+
+ @Test
+ public void registeringAnInactiveNonUskWillNotSubscribeToAUsk()
+ throws MalformedURLException {
+ FreenetURI freenetUri = createRandom(randomSource, "test-0").getURI();
+ freenetInterface.registerPassiveUsk(freenetUri, null);
+ verify(uskManager, never()).subscribe(any(USK.class),
+ any(USKCallback.class), anyBoolean(),
+ eq((RequestClient) highLevelSimpleClient));
+ }
+
+ @Test
public void unregisteringANotRegisteredUskDoesNothing() {
FreenetURI freenetURI = createRandom(randomSource, "test-0").getURI().uskForSSK();
freenetInterface.unregisterUsk(freenetURI);
}
@Test
- public void unregisteringARegisteredSoneUnregistersTheSone() {
- freenetInterface.registerUsk(sone, null);
+ public void unregisteringARegisteredSoneUnregistersTheSone()
+ throws MalformedURLException {
+ freenetInterface.registerActiveUsk(sone.getRequestUri(), mock(USKCallback.class));
freenetInterface.unregisterUsk(sone);
verify(uskManager).unsubscribe(any(USK.class), any(USKCallback.class));
}
@Test
public void unregisteringASoneWithAWrongRequestKeyWillNotUnsubscribe() throws MalformedURLException {
- freenetInterface.registerUsk(sone, null);
when(sone.getRequestUri()).thenReturn(new FreenetURI("KSK@GPLv3.txt"));
+ freenetInterface.registerUsk(sone.getRequestUri(), null);
freenetInterface.unregisterUsk(sone);
verify(uskManager, never()).unsubscribe(any(USK.class), any(USKCallback.class));
}
@Test
- public void callbackPrioritiesAreInteractive() {
- freenetInterface.registerUsk(sone, null);
- assertThat(callbackCaptor.getValue().getPollingPriorityNormal(), is(INTERACTIVE_PRIORITY_CLASS));
+ public void callbackForNormalUskUsesDifferentPriorities() {
+ Callback callback = mock(Callback.class);
+ FreenetURI soneUri = createRandom(randomSource, "test-0").getURI().uskForSSK();
+ freenetInterface.registerUsk(soneUri, callback);
+ assertThat(callbackCaptor.getValue().getPollingPriorityNormal(), is(PREFETCH_PRIORITY_CLASS));
assertThat(callbackCaptor.getValue().getPollingPriorityProgress(), is(INTERACTIVE_PRIORITY_CLASS));
}
@Test
- public void callbackForRegisteredSoneWithHigherEditionTriggersDownload() throws InterruptedException {
- freenetInterface.registerUsk(sone, soneDownloader);
- final CountDownLatch downloadTriggered = new CountDownLatch(1);
- doAnswer(new Answer<Void>() {
- @Override
- public Void answer(InvocationOnMock invocation) throws Throwable {
- downloadTriggered.countDown();
- return null;
- }
- }).when(soneDownloader).fetchSone(sone);
- 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<Void>() {
- @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));
+ public void callbackForNormalUskForwardsImportantParameters() throws MalformedURLException {
+ Callback callback = mock(Callback.class);
+ FreenetURI uri = createRandom(randomSource, "test-0").getURI().uskForSSK();
+ freenetInterface.registerUsk(uri, callback);
+ USK key = mock(USK.class);
+ when(key.getURI()).thenReturn(uri);
+ callbackCaptor.getValue().onFoundEdition(3, key, null, false, (short) 0, null, true, true);
+ verify(callback).editionFound(eq(uri), eq(3L), eq(true), eq(true));
}
@Test
assertThat(fetched.getFetchResult(), is(fetchResult));
}
+ @Test
+ public void cancellingAnInsertWillFireImageInsertAbortedEvent() {
+ ClientPutter clientPutter = mock(ClientPutter.class);
+ insertToken.setClientPutter(clientPutter);
+ ArgumentCaptor<ImageInsertStartedEvent> imageInsertStartedEvent = forClass(ImageInsertStartedEvent.class);
+ verify(eventBus).post(imageInsertStartedEvent.capture());
+ assertThat(imageInsertStartedEvent.getValue().image(), is(image));
+ insertToken.cancel();
+ ArgumentCaptor<ImageInsertAbortedEvent> imageInsertAbortedEvent = forClass(ImageInsertAbortedEvent.class);
+ verify(eventBus, times(2)).post(imageInsertAbortedEvent.capture());
+ verify(bucket).free();
+ assertThat(imageInsertAbortedEvent.getValue().image(), is(image));
+ }
+
+ @Test
+ public void failureWithoutExceptionSendsFailedEvent() {
+ insertToken.onFailure(null, null);
+ ArgumentCaptor<ImageInsertFailedEvent> imageInsertFailedEvent = forClass(ImageInsertFailedEvent.class);
+ verify(eventBus).post(imageInsertFailedEvent.capture());
+ verify(bucket).free();
+ assertThat(imageInsertFailedEvent.getValue().image(), is(image));
+ assertThat(imageInsertFailedEvent.getValue().cause(), nullValue());
+ }
+
+ @Test
+ public void failureSendsFailedEventWithException() {
+ InsertException insertException = new InsertException(InsertExceptionMode.INTERNAL_ERROR, "Internal error", null);
+ insertToken.onFailure(insertException, null);
+ ArgumentCaptor<ImageInsertFailedEvent> imageInsertFailedEvent = forClass(ImageInsertFailedEvent.class);
+ verify(eventBus).post(imageInsertFailedEvent.capture());
+ verify(bucket).free();
+ assertThat(imageInsertFailedEvent.getValue().image(), is(image));
+ assertThat(imageInsertFailedEvent.getValue().cause(), is((Throwable) insertException));
+ }
+
+ @Test
+ public void failureBecauseCancelledByUserSendsAbortedEvent() {
+ InsertException insertException = new InsertException(InsertExceptionMode.CANCELLED, null);
+ insertToken.onFailure(insertException, null);
+ ArgumentCaptor<ImageInsertAbortedEvent> imageInsertAbortedEvent = forClass(ImageInsertAbortedEvent.class);
+ verify(eventBus).post(imageInsertAbortedEvent.capture());
+ verify(bucket).free();
+ assertThat(imageInsertAbortedEvent.getValue().image(), is(image));
+ }
+
+ @Test
+ public void ignoredMethodsDoNotThrowExceptions() throws ResumeFailedException {
+ insertToken.onResume(null);
+ insertToken.onFetchable(null);
+ insertToken.onGeneratedMetadata(null, null);
+ }
+
+ @Test
+ public void generatedUriIsPostedOnSuccess() {
+ FreenetURI generatedUri = mock(FreenetURI.class);
+ insertToken.onGeneratedURI(generatedUri, null);
+ insertToken.onSuccess(null);
+ ArgumentCaptor<ImageInsertFinishedEvent> imageInsertFinishedEvent = forClass(ImageInsertFinishedEvent.class);
+ verify(eventBus).post(imageInsertFinishedEvent.capture());
+ verify(bucket).free();
+ 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());
+ }
+
+ @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 backgroundFetchRegistersSnoopAndRestartsTheRequest() throws Exception {
+ freenetInterface.startFetch(uri, backgroundFetchCallback);
+ verify(clientGetter).setMetaSnoop(any(SnoopMetadata.class));
+ verify(clientGetter).restart(eq(uri), anyBoolean(), any(ClientContext.class));
+ }
+
+ @Test
+ public void requestIsNotCancelledForImageMimeType() {
+ verifySnoopCancelsRequestForMimeType("image/png", false);
+ verify(backgroundFetchCallback, never()).failed(uri);
+ }
+
+ @Test
+ public void requestIsCancelledForNullMimeType() {
+ verifySnoopCancelsRequestForMimeType(null, true);
+ verify(backgroundFetchCallback, never()).shouldCancel(eq(uri), ArgumentMatchers.<String>any(), anyLong());
+ verify(backgroundFetchCallback).failed(uri);
+ }
+
+ @Test
+ public void requestIsCancelledForVideoMimeType() {
+ verifySnoopCancelsRequestForMimeType("video/mkv", true);
+ verify(backgroundFetchCallback).failed(uri);
+ }
+
+ @Test
+ public void requestIsCancelledForAudioMimeType() {
+ verifySnoopCancelsRequestForMimeType("audio/mpeg", true);
+ verify(backgroundFetchCallback).failed(uri);
+ }
+
+ @Test
+ public void requestIsCancelledForTextMimeType() {
+ verifySnoopCancelsRequestForMimeType("text/plain", true);
+ verify(backgroundFetchCallback).failed(uri);
+ }
+
+ private void verifySnoopCancelsRequestForMimeType(String mimeType, boolean cancel) {
+ when(backgroundFetchCallback.shouldCancel(eq(uri), eq(mimeType), anyLong())).thenReturn(cancel);
+ freenetInterface.startFetch(uri, backgroundFetchCallback);
+ ArgumentCaptor<SnoopMetadata> snoopMetadata = forClass(SnoopMetadata.class);
+ verify(clientGetter).setMetaSnoop(snoopMetadata.capture());
+ Metadata metadata = mock(Metadata.class);
+ when(metadata.getMIMEType()).thenReturn(mimeType);
+ assertThat(snoopMetadata.getValue().snoopMetadata(metadata, mock(ClientContext.class)), is(cancel));
+ }
+
+ @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);
+ }
+
}