+ private final Core core = mock(Core.class);
+ private final FreenetInterface freenetInterface = mock(FreenetInterface.class);
+ private final SoneParser soneParser = mock(SoneParser.class);
+ private final SoneDownloaderImpl soneDownloader = new SoneDownloaderImpl(core, freenetInterface, soneParser);
+ private FreenetURI requestUri = mock(FreenetURI.class);
+ private Sone sone = mock(Sone.class);
+
+ @Before
+ public void setupSone() {
+ Sone sone = SoneDownloaderTest.this.sone;
+ Identity identity = mock(Identity.class);
+ InsertableClientSSK clientSSK = createRandom(new DummyRandomSource(), "WoT");
+ when(identity.getRequestUri()).thenReturn(clientSSK.getURI().toString());
+ when(identity.getId()).thenReturn("identity");
+ when(sone.getId()).thenReturn("identity");
+ when(sone.getIdentity()).thenReturn(identity);
+ requestUri = clientSSK.getURI().setKeyType("USK").setDocName("Sone");
+ when(sone.getRequestUri()).thenAnswer(new Answer<FreenetURI>() {
+ @Override
+ public FreenetURI answer(InvocationOnMock invocation)
+ throws Throwable {
+ return requestUri;
+ }
+ });
+ when(sone.getTime()).thenReturn(currentTimeMillis() - DAYS.toMillis(1));
+ }
+
+ private void setupSoneAsUnknown() {
+ when(sone.getTime()).thenReturn(0L);
+ }
+
+ @Test
+ public void addingASoneWillRegisterItsKey() {
+ soneDownloader.addSone(sone);
+ verify(freenetInterface).registerActiveUsk(eq(sone.getRequestUri()), any(
+ USKCallback.class));
+ verify(freenetInterface, never()).unregisterUsk(sone);
+ }
+
+ @Test
+ 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));
+ }
+ }
+