Use UpdatedSoneProcessor in SoneDownloader
[Sone.git] / src / test / java / net / pterodactylus / sone / core / SoneDownloaderTest.java
index 568d76c..284d9b2 100644 (file)
@@ -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<FreenetURI>() {
-                       @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());
        }