- public void fetchSoneWithUriDownloadsSoneWithUri() {
- SoneDownloader soneDownloader = mock(SoneDownloader.class);
- Sone sone = mock(Sone.class);
- FreenetURI soneUri = mock(FreenetURI.class);
- when(sone.getRequestUri()).thenReturn(soneUri);
- FetchSoneWithUri fetchSoneWithUri = soneDownloader.new FetchSoneWithUri(sone);
- fetchSoneWithUri.run();
- verify(soneDownloader).fetchSone(eq(sone), eq(soneUri));
+ public void notBeingAbleToFetchAnUnknownSoneDoesNotUpdateCore() {
+ setupSoneAsUnknown();
+ soneDownloader.fetchSoneAsSskAction(sone).run();
+ verify(freenetInterface).fetchUri(finalRequestUri.sskForUSK());
+ verifyThatSoneStatusWasChangedToDownloadingAndBackTo(unknown);
+ verify(updatedSoneProcessor, never()).updateSone(any(Sone.class));
+ }
+
+ private void verifyThatSoneStatusWasChangedToDownloadingAndBackTo(SoneStatus soneStatus) {
+ ArgumentCaptor<SoneStatus> soneStatuses = forClass(SoneStatus.class);
+ verify(sone, times(2)).setStatus(soneStatuses.capture());
+ assertThat(soneStatuses.getAllValues().get(0), is(downloading));
+ assertThat(soneStatuses.getAllValues().get(1), is(soneStatus));
+ }
+
+ @Test
+ public void notBeingAbleToFetchAKnownSoneDoesNotUpdateCore() {
+ soneDownloader.fetchSoneAsSskAction(sone).run();
+ verify(freenetInterface).fetchUri(finalRequestUri.sskForUSK());
+ verifyThatSoneStatusWasChangedToDownloadingAndBackTo(idle);
+ verify(updatedSoneProcessor, never()).updateSone(any(Sone.class));
+ }
+
+ @Test(expected = NullPointerException.class)
+ public void exceptionWhileFetchingSoneDoesNotProcessUpdatedSone() {
+ when(freenetInterface.fetchUri(any(FreenetURI.class))).thenThrow(NullPointerException.class);
+ try {
+ soneDownloader.fetchSoneAsSskAction(sone).run();
+ } finally {
+ verify(updatedSoneProcessor, never()).updateSone(any(Sone.class));
+ }
+ }
+
+ @Test
+ public void onlyFetchingASoneWillNotUpdateTheCore() throws IOException, SoneException {
+ setupParsedSone();
+ soneDownloader.fetchSone(sone, sone.getRequestUri(), true);
+ verify(updatedSoneProcessor, never()).updateSone(any(Sone.class));
+ verifyThatSoneStatusWasChangedToDownloadingAndBackTo(idle);
+ }
+
+ @Test
+ public void fetchingACompleteSoneNotifiesTheUpdatedSoneProcessor() throws IOException, SoneException {
+ setupParsedSone();
+ soneDownloader.fetchSone(sone, sone.getRequestUri(), false);
+ verify(updatedSoneProcessor).updateSone(parsedSone);
+ verifyThatSoneStatusWasChangedToDownloadingAndBackTo(idle);
+ }
+
+ private void setupParsedSone() throws IOException, SoneException {
+ InputStream inputStream = mock(InputStream.class);
+ ClientMetadata clientMetadata = new ClientMetadata("application/xml");
+ Bucket bucket = mock(Bucket.class);
+ when(bucket.getInputStream()).thenReturn(inputStream);
+ FetchResult fetchResult = new FetchResult(clientMetadata, bucket);
+ Fetched fetched = new Fetched(finalRequestUri, fetchResult);
+ when(freenetInterface.fetchUri(eq(finalRequestUri))).thenReturn(fetched);
+ when(soneParser.parseSone(sone, inputStream)).thenReturn(parsedSone);