X-Git-Url: https://git.pterodactylus.net/?p=Sone.git;a=blobdiff_plain;f=src%2Ftest%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fcore%2FSoneDownloaderTest.java;fp=src%2Ftest%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fcore%2FSoneDownloaderTest.java;h=284d9b2ad98fe9bf7ec43b7c3425935b578aa7b3;hp=90439743ee9ee784fb3902e5af05440ca3d5e17d;hb=03cec6a6772c2d836d94864adddaf544cbe9d72f;hpb=6f1f26e3998cfef155b0cf59152827accea70d30 diff --git a/src/test/java/net/pterodactylus/sone/core/SoneDownloaderTest.java b/src/test/java/net/pterodactylus/sone/core/SoneDownloaderTest.java index 9043974..284d9b2 100644 --- a/src/test/java/net/pterodactylus/sone/core/SoneDownloaderTest.java +++ b/src/test/java/net/pterodactylus/sone/core/SoneDownloaderTest.java @@ -6,8 +6,10 @@ import static java.util.concurrent.TimeUnit.DAYS; import static net.pterodactylus.sone.data.Sone.SoneStatus.downloading; import static net.pterodactylus.sone.data.Sone.SoneStatus.idle; import static net.pterodactylus.sone.data.Sone.SoneStatus.unknown; +import static net.pterodactylus.sone.web.AllPagesTestKt.getBaseInjector; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.notNullValue; import static org.mockito.ArgumentCaptor.forClass; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.eq; @@ -20,10 +22,10 @@ import static org.mockito.Mockito.when; import java.io.IOException; import java.io.InputStream; -import net.pterodactylus.sone.core.FreenetInterface.Fetched; import net.pterodactylus.sone.data.Sone; import net.pterodactylus.sone.data.Sone.SoneStatus; import net.pterodactylus.sone.freenet.wot.Identity; +import net.pterodactylus.sone.test.GuiceKt; import freenet.client.ClientMetadata; import freenet.client.FetchResult; @@ -36,40 +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. - * - * @author David ‘Bombe’ Roden */ 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.fetchSoneAction(sone).run(); - verify(freenetInterface).fetchUri(finalRequestUri); + soneDownloader.fetchSoneAsSskAction(sone).run(); + 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,75 +112,55 @@ public class SoneDownloaderTest { @Test public void notBeingAbleToFetchAKnownSoneDoesNotUpdateCore() { - FreenetURI finalRequestUri = requestUri.sskForUSK() - .setMetaString(new String[] { "sone.xml" }); - soneDownloader.fetchSoneAction(sone).run(); - verify(freenetInterface).fetchUri(finalRequestUri); + soneDownloader.fetchSoneAsSskAction(sone).run(); + 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.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); + public void exceptionWhileFetchingSoneDoesNotProcessUpdatedSone() { + when(freenetInterface.fetchUri(any(FreenetURI.class))).thenThrow(NullPointerException.class); try { - soneDownloader.fetchSoneAction(sone).run(); + 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.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)); + 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()); } }