+ @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);
+ }
+