From: David ‘Bombe’ Roden Date: Sat, 3 Mar 2018 17:40:37 +0000 (+0100) Subject: Use UpdatedSoneProcessor in SoneDownloader X-Git-Tag: v79^2~127 X-Git-Url: https://git.pterodactylus.net/?p=Sone.git;a=commitdiff_plain;h=e82bcd0c57096e722421a263fd2e95f88974b65b Use UpdatedSoneProcessor in SoneDownloader --- diff --git a/src/main/java/net/pterodactylus/sone/core/SoneDownloaderImpl.java b/src/main/java/net/pterodactylus/sone/core/SoneDownloaderImpl.java index 8b257dd..91bf13b 100644 --- a/src/main/java/net/pterodactylus/sone/core/SoneDownloaderImpl.java +++ b/src/main/java/net/pterodactylus/sone/core/SoneDownloaderImpl.java @@ -55,7 +55,7 @@ public class SoneDownloaderImpl extends AbstractService implements SoneDownloade private static final int MAX_PROTOCOL_VERSION = 0; /** The core. */ - private final Core core; + private final UpdatedSoneProcessor updatedSoneProcessor; private final SoneParser soneParser; /** The Freenet interface. */ @@ -64,19 +64,10 @@ public class SoneDownloaderImpl extends AbstractService implements SoneDownloade /** The sones to update. */ private final Set sones = new HashSet(); - /** - * Creates a new Sone downloader. - * - * @param core - * The core - * @param freenetInterface - * The Freenet interface - * @param soneParser - */ @Inject - SoneDownloaderImpl(Core core, FreenetInterface freenetInterface, SoneParser soneParser) { + SoneDownloaderImpl(UpdatedSoneProcessor updatedSoneProcessor, FreenetInterface freenetInterface, SoneParser soneParser) { super("Sone Downloader", false); - this.core = core; + this.updatedSoneProcessor = updatedSoneProcessor; this.freenetInterface = freenetInterface; this.soneParser = soneParser; } @@ -170,7 +161,7 @@ public class SoneDownloaderImpl extends AbstractService implements SoneDownloade if (parsedSone != null) { if (!fetchOnly) { parsedSone.setStatus((parsedSone.getTime() == 0) ? SoneStatus.unknown : SoneStatus.idle); - core.updateSone(parsedSone); + updatedSoneProcessor.updateSone(parsedSone); addSone(parsedSone); } } diff --git a/src/test/java/net/pterodactylus/sone/core/SoneDownloaderTest.java b/src/test/java/net/pterodactylus/sone/core/SoneDownloaderTest.java index 568d76c..284d9b2 100644 --- a/src/test/java/net/pterodactylus/sone/core/SoneDownloaderTest.java +++ b/src/test/java/net/pterodactylus/sone/core/SoneDownloaderTest.java @@ -38,38 +38,30 @@ import freenet.support.api.Bucket; import org.junit.Before; import org.junit.Test; import org.mockito.ArgumentCaptor; -import org.mockito.invocation.InvocationOnMock; -import org.mockito.stubbing.Answer; /** * Unit test for {@link SoneDownloaderImpl} and its subclasses. */ public class SoneDownloaderTest { - 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); + private final UpdatedSoneProcessor updatedSoneProcessor = mock(UpdatedSoneProcessor.class); + private final SoneDownloaderImpl soneDownloader = new SoneDownloaderImpl(updatedSoneProcessor, freenetInterface, soneParser); + private final InsertableClientSSK clientSSK = createRandom(new DummyRandomSource(), "WoT"); + private final FreenetURI requestUri = clientSSK.getURI().setKeyType("USK").setDocName("Sone"); + private final FreenetURI finalRequestUri = requestUri.setMetaString(new String[] { "sone.xml" }); + private final Sone sone = mock(Sone.class); + private final Sone parsedSone = 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() { - @Override - public FreenetURI answer(InvocationOnMock invocation) - throws Throwable { - return requestUri; - } - }); + when(sone.getRequestUri()).thenReturn(requestUri); when(sone.getTime()).thenReturn(currentTimeMillis() - DAYS.toMillis(1)); } @@ -104,13 +96,11 @@ public class SoneDownloaderTest { @Test public void notBeingAbleToFetchAnUnknownSoneDoesNotUpdateCore() { - FreenetURI finalRequestUri = requestUri.sskForUSK() - .setMetaString(new String[] { "sone.xml" }); setupSoneAsUnknown(); soneDownloader.fetchSoneAsSskAction(sone).run(); - verify(freenetInterface).fetchUri(finalRequestUri); + verify(freenetInterface).fetchUri(finalRequestUri.sskForUSK()); verifyThatSoneStatusWasChangedToDownloadingAndBackTo(unknown); - verify(core, never()).updateSone(any(Sone.class)); + verify(updatedSoneProcessor, never()).updateSone(any(Sone.class)); } private void verifyThatSoneStatusWasChangedToDownloadingAndBackTo(SoneStatus soneStatus) { @@ -122,80 +112,53 @@ public class SoneDownloaderTest { @Test public void notBeingAbleToFetchAKnownSoneDoesNotUpdateCore() { - FreenetURI finalRequestUri = requestUri.sskForUSK() - .setMetaString(new String[] { "sone.xml" }); soneDownloader.fetchSoneAsSskAction(sone).run(); - verify(freenetInterface).fetchUri(finalRequestUri); + verify(freenetInterface).fetchUri(finalRequestUri.sskForUSK()); verifyThatSoneStatusWasChangedToDownloadingAndBackTo(idle); - verify(core, never()).updateSone(any(Sone.class)); + verify(updatedSoneProcessor, 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.fetchSoneAsSskAction(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); + public void exceptionWhileFetchingSoneDoesNotProcessUpdatedSone() { + when(freenetInterface.fetchUri(any(FreenetURI.class))).thenThrow(NullPointerException.class); try { soneDownloader.fetchSoneAsSskAction(sone).run(); } finally { - verify(freenetInterface).fetchUri(finalRequestUri); - verifyThatSoneStatusWasChangedToDownloadingAndBackTo(idle); - verify(core, never()).updateSone(any(Sone.class)); + verify(updatedSoneProcessor, 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.fetchSoneAsSskAction(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.fetchSoneAsSskAction(sone).run(); - verify(core, never()).updateSone(any(Sone.class)); + 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 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)); + public void fetchingACompleteSoneNotifiesTheUpdatedSoneProcessor() throws IOException, SoneException { + setupParsedSone(); + soneDownloader.fetchSone(sone, sone.getRequestUri(), false); + verify(updatedSoneProcessor).updateSone(parsedSone); verifyThatSoneStatusWasChangedToDownloadingAndBackTo(idle); } - private Fetched createFetchResult(FreenetURI uri, InputStream inputStream) throws IOException { + 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); - return new Fetched(uri, fetchResult); + Fetched fetched = new Fetched(finalRequestUri, fetchResult); + when(freenetInterface.fetchUri(eq(finalRequestUri))).thenReturn(fetched); + when(soneParser.parseSone(sone, inputStream)).thenReturn(parsedSone); } @Test public void soneDownloaderCanBeCreatedByDependencyInjection() { assertThat(getBaseInjector().createChildInjector( + GuiceKt.supply(UpdatedSoneProcessor.class).byInstance(mock(UpdatedSoneProcessor.class)), GuiceKt.supply(SoneParser.class).byInstance(mock(SoneParser.class)) ).getInstance(SoneDownloader.class), notNullValue()); }