+ public void addingASoneTwiceWillAlsoDeregisterItsKey() {
+ soneDownloader.addSone(sone);
+ soneDownloader.addSone(sone);
+ verify(freenetInterface, times(2)).registerActiveUsk(eq(
+ sone.getRequestUri()), any(USKCallback.class));
+ verify(freenetInterface).unregisterUsk(sone);
+ }
+
+
+ @Test
+ public void stoppingTheSoneDownloaderUnregistersTheSone() {
+ soneDownloader.addSone(sone);
+ soneDownloader.stop();
+ verify(freenetInterface).unregisterUsk(sone);
+ }
+
+ @Test
+ public void notBeingAbleToFetchAnUnknownSoneDoesNotUpdateCore() {
+ FreenetURI finalRequestUri = requestUri.sskForUSK()
+ .setMetaString(new String[] { "sone.xml" });
+ setupSoneAsUnknown();
+ soneDownloader.fetchSoneAction(sone).run();
+ verify(freenetInterface).fetchUri(finalRequestUri);
+ verifyThatSoneStatusWasChangedToDownloadingAndBackTo(unknown);
+ verify(core, 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() {
+ FreenetURI finalRequestUri = requestUri.sskForUSK()
+ .setMetaString(new String[] { "sone.xml" });
+ soneDownloader.fetchSoneAction(sone).run();
+ verify(freenetInterface).fetchUri(finalRequestUri);
+ verifyThatSoneStatusWasChangedToDownloadingAndBackTo(idle);
+ verify(core, never()).updateSone(any(Sone.class));
+ }
+
+ @Test(expected = NullPointerException.class)
+ public void exceptionWhileFetchingAnUnknownSoneDoesNotUpdateCore() {
+ FreenetURI finalRequestUri = requestUri.sskForUSK()
+ .setMetaString(new String[] { "sone.xml" });
+ setupSoneAsUnknown();
+ when(freenetInterface.fetchUri(finalRequestUri)).thenThrow(NullPointerException.class);
+ try {
+ soneDownloader.fetchSoneAction(sone).run();
+ } finally {
+ verify(freenetInterface).fetchUri(finalRequestUri);
+ verifyThatSoneStatusWasChangedToDownloadingAndBackTo(unknown);
+ verify(core, never()).updateSone(any(Sone.class));
+ }
+ }
+
+ @Test(expected = NullPointerException.class)
+ public void exceptionWhileFetchingAKnownSoneDoesNotUpdateCore() {
+ FreenetURI finalRequestUri = requestUri.sskForUSK()
+ .setMetaString(new String[] { "sone.xml" });
+ when(freenetInterface.fetchUri(finalRequestUri)).thenThrow( NullPointerException.class);
+ try {
+ soneDownloader.fetchSoneAction(sone).run();
+ } finally {
+ verify(freenetInterface).fetchUri(finalRequestUri);
+ verifyThatSoneStatusWasChangedToDownloadingAndBackTo(idle);
+ verify(core, never()).updateSone(any(Sone.class));
+ }
+ }
+
+ @Test
+ public void fetchingSoneWithInvalidXmlWillNotUpdateTheCore() throws IOException {
+ final Fetched fetchResult = createFetchResult(requestUri, getClass().getResourceAsStream("sone-parser-not-xml.xml"));
+ when(freenetInterface.fetchUri(requestUri)).thenReturn(fetchResult);
+ soneDownloader.fetchSoneAction(sone).run();
+ verify(core, never()).updateSone(any(Sone.class));
+ }
+
+ @Test
+ public void exceptionWhileFetchingSoneWillNotUpdateTheCore() throws IOException {
+ final Fetched fetchResult = createFetchResult(requestUri, getClass().getResourceAsStream("sone-parser-no-payload.xml"));
+ when(core.soneBuilder()).thenReturn(null);
+ when(freenetInterface.fetchUri(requestUri)).thenReturn(fetchResult);
+ soneDownloader.fetchSoneAction(sone).run();
+ verify(core, never()).updateSone(any(Sone.class));
+ }
+
+ @Test
+ public void onlyFetchingASoneWillNotUpdateTheCore() throws IOException {
+ final Fetched fetchResult = createFetchResult(requestUri, getClass().getResourceAsStream("sone-parser-no-payload.xml"));
+ when(freenetInterface.fetchUri(requestUri)).thenReturn(fetchResult);
+ soneDownloader.fetchSone(sone, sone.getRequestUri(), true);
+ verify(core, never()).updateSone(any(Sone.class));
+ verifyThatSoneStatusWasChangedToDownloadingAndBackTo(idle);
+ }
+
+ private Fetched createFetchResult(FreenetURI uri, InputStream inputStream) throws IOException {
+ ClientMetadata clientMetadata = new ClientMetadata("application/xml");
+ Bucket bucket = mock(Bucket.class);
+ when(bucket.getInputStream()).thenReturn(inputStream);
+ FetchResult fetchResult = new FetchResult(clientMetadata, bucket);
+ return new Fetched(uri, fetchResult);