X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Ftest%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fcore%2FSoneDownloaderTest.java;h=284d9b2ad98fe9bf7ec43b7c3425935b578aa7b3;hb=6940f7d3df4f6164bfc69a9393a0f5796627b880;hp=be697b3c6abd335f774655694e0cf56f48229dea;hpb=534abb5d5dba7f0377856d2d067ae48538226680;p=Sone.git
diff --git a/src/test/java/net/pterodactylus/sone/core/SoneDownloaderTest.java b/src/test/java/net/pterodactylus/sone/core/SoneDownloaderTest.java
index be697b3..284d9b2 100644
--- a/src/test/java/net/pterodactylus/sone/core/SoneDownloaderTest.java
+++ b/src/test/java/net/pterodactylus/sone/core/SoneDownloaderTest.java
@@ -22,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;
@@ -38,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));
}
@@ -106,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) {
@@ -124,80 +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().getInstance(SoneDownloader.class), notNullValue());
+ assertThat(getBaseInjector().createChildInjector(
+ GuiceKt.supply(UpdatedSoneProcessor.class).byInstance(mock(UpdatedSoneProcessor.class)),
+ GuiceKt.supply(SoneParser.class).byInstance(mock(SoneParser.class))
+ ).getInstance(SoneDownloader.class), notNullValue());
}
}