}
}
- /**
- * Registers the USK for the given Sone and notifies the given
- * {@link SoneDownloader} if an update was found.
- *
- * @param sone
- * The Sone to watch
- * @param soneDownloader
- * The Sone download to notify on updates
- */
- public void registerUsk(final Sone sone, final SoneDownloader soneDownloader) {
+ public void registerUsk(final Sone sone, final SoneUpdater soneUpdater) {
try {
logger.log(Level.FINE, String.format("Registering Sone “%s” for USK updates at %s…", sone, sone.getRequestUri().setMetaString(new String[] { "sone.xml" })));
USKCallback uskCallback = new USKCallback() {
@SuppressWarnings("synthetic-access")
public void onFoundEdition(long edition, USK key, ObjectContainer objectContainer, ClientContext clientContext, boolean metadata, short codec, byte[] data, boolean newKnownGood, boolean newSlotToo) {
logger.log(Level.FINE, String.format("Found USK update for Sone “%s” at %s, new known good: %s, new slot too: %s.", sone, key, newKnownGood, newSlotToo));
- if (edition > sone.getLatestEdition()) {
- sone.setLatestEdition(edition);
- new Thread(soneDownloader.fetchSoneAction(sone), "Sone Downloader").start();
- }
+ soneUpdater.updateSone(sone, edition);
}
@Override
private final Sone sone = mock(Sone.class);
private final ArgumentCaptor<USKCallback> callbackCaptor = forClass(USKCallback.class);
private final SoneDownloader soneDownloader = mock(SoneDownloader.class);
+ private final SoneUpdater soneUpdater = mock(SoneUpdater.class);
private final Image image = mock(Image.class);
private InsertToken insertToken;
@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);
+ freenetInterface.registerUsk(sone, soneUpdater);
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));
+ verify(soneUpdater).updateSone(sone, 1);
}
@Test
private final Core core = mock(Core.class);
private final FreenetInterface freenetInterface = mock(FreenetInterface.class);
private final SoneDownloaderImpl soneDownloader = new SoneDownloaderImpl(core, freenetInterface);
+ private final SoneUpdater soneUpdater = mock(SoneUpdater.class);
private final FreenetURI requestUri = mock(FreenetURI.class);
private final Sone sone = mock(Sone.class);
private final PostBuilder postBuilder = mock(PostBuilder.class);
@Test
public void addingASoneWillRegisterItsKey() {
soneDownloader.addSone(sone);
- verify(freenetInterface).registerUsk(sone, soneDownloader);
+ verify(freenetInterface).registerUsk(eq(sone), any(SoneUpdater.class));
verify(freenetInterface, never()).unregisterUsk(sone);
}
public void addingASoneTwiceWillAlsoDeregisterItsKey() {
soneDownloader.addSone(sone);
soneDownloader.addSone(sone);
- verify(freenetInterface, times(2)).registerUsk(sone, soneDownloader);
+ verify(freenetInterface, times(2)).registerUsk(eq(sone), any(SoneUpdater.class));
verify(freenetInterface).unregisterUsk(sone);
}
@Test
public void notBeingAbleToFetchAnUnknownSoneDoesNotUpdateCore() {
- soneDownloader.fetchSone(sone);
+ soneDownloader.fetchSoneAction(sone).run();
verify(freenetInterface).fetchUri(requestUri);
verifyThatSoneStatusWasChangedToDownloadingAndBackTo(unknown);
verify(core, never()).updateSone(any(Sone.class));
@Test
public void notBeingAbleToFetchAKnownSoneDoesNotUpdateCore() {
when(sone.getTime()).thenReturn(1000L);
- soneDownloader.fetchSone(sone);
+ soneDownloader.fetchSoneAction(sone).run();
verify(freenetInterface).fetchUri(requestUri);
verifyThatSoneStatusWasChangedToDownloadingAndBackTo(idle);
verify(core, never()).updateSone(any(Sone.class));
public void exceptionWhileFetchingAnUnknownSoneDoesNotUpdateCore() {
when(freenetInterface.fetchUri(requestUri)).thenThrow(NullPointerException.class);
try {
- soneDownloader.fetchSone(sone);
+ soneDownloader.fetchSoneAction(sone).run();
} finally {
verify(freenetInterface).fetchUri(requestUri);
verifyThatSoneStatusWasChangedToDownloadingAndBackTo(unknown);
when(sone.getTime()).thenReturn(1000L);
when(freenetInterface.fetchUri(requestUri)).thenThrow(NullPointerException.class);
try {
- soneDownloader.fetchSone(sone);
+ soneDownloader.fetchSoneAction(sone).run();
} finally {
verify(freenetInterface).fetchUri(requestUri);
verifyThatSoneStatusWasChangedToDownloadingAndBackTo(idle);
public void successfulFetchingOfSoneWithUskRequestUriUpdatesTheCoreWithASone() throws IOException {
final Fetched fetchResult = createFetchResult(requestUri, getClass().getResourceAsStream("sone-parser-no-payload.xml"));
when(freenetInterface.fetchUri(requestUri)).thenReturn(fetchResult);
- soneDownloader.fetchSone(sone);
+ soneDownloader.fetchSoneAction(sone).run();
verifyThatParsedSoneHasTheSameIdAsTheOriginalSone();
}
when(requestUri.getKeyType()).thenReturn("SSK");
final Fetched fetchResult = createFetchResult(requestUri, getClass().getResourceAsStream("sone-parser-no-payload.xml"));
when(freenetInterface.fetchUri(requestUri)).thenReturn(fetchResult);
- soneDownloader.fetchSone(sone);
+ soneDownloader.fetchSoneAction(sone).run();
verifyThatParsedSoneHasTheSameIdAsTheOriginalSone();
}
when(requestUri.getKeyType()).thenReturn("SSK");
final Fetched fetchResult = createFetchResult(requestUri, getClass().getResourceAsStream("sone-parser-with-zero-time.xml"));
when(freenetInterface.fetchUri(requestUri)).thenReturn(fetchResult);
- soneDownloader.fetchSone(sone);
+ soneDownloader.fetchSoneAction(sone).run();
verifyThatParsedSoneHasTheSameIdAsTheOriginalSone();
}
public void fetchingSoneWithInvalidXmlWillNotUpdateTheCore() throws IOException {
final Fetched fetchResult = createFetchResult(requestUri, getClass().getResourceAsStream("sone-parser-not-xml.xml"));
when(freenetInterface.fetchUri(requestUri)).thenReturn(fetchResult);
- soneDownloader.fetchSone(sone);
+ soneDownloader.fetchSoneAction(sone).run();
verify(core, never()).updateSone(any(Sone.class));
}
final Fetched fetchResult = createFetchResult(requestUri, getClass().getResourceAsStream("sone-parser-no-payload.xml"));
when(sone.getId()).thenThrow(NullPointerException.class);
when(freenetInterface.fetchUri(requestUri)).thenReturn(fetchResult);
- soneDownloader.fetchSone(sone);
+ soneDownloader.fetchSoneAction(sone).run();
verify(core, never()).updateSone(any(Sone.class));
}