Add tests for the callback.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Tue, 29 Jul 2014 15:29:53 +0000 (17:29 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Tue, 29 Jul 2014 15:29:53 +0000 (17:29 +0200)
src/test/java/net/pterodactylus/sone/core/FreenetInterfaceTest.java

index 5abf2c1..644c9fe 100644 (file)
@@ -1,8 +1,10 @@
 package net.pterodactylus.sone.core;
 
 import static freenet.keys.InsertableClientSSK.createRandom;
+import static freenet.node.RequestStarter.INTERACTIVE_PRIORITY_CLASS;
 import static java.lang.System.currentTimeMillis;
 import static java.util.concurrent.TimeUnit.DAYS;
+import static java.util.concurrent.TimeUnit.SECONDS;
 import static net.pterodactylus.sone.Matchers.delivers;
 import static org.hamcrest.MatcherAssert.assertThat;
 import static org.hamcrest.Matchers.is;
@@ -13,6 +15,8 @@ import static org.mockito.Matchers.any;
 import static org.mockito.Matchers.anyBoolean;
 import static org.mockito.Matchers.anyShort;
 import static org.mockito.Matchers.eq;
+import static org.mockito.Mockito.doAnswer;
+import static org.mockito.Mockito.doNothing;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.never;
 import static org.mockito.Mockito.verify;
@@ -24,6 +28,7 @@ import java.lang.reflect.Field;
 import java.lang.reflect.Modifier;
 import java.net.MalformedURLException;
 import java.util.HashMap;
+import java.util.concurrent.CountDownLatch;
 
 import net.pterodactylus.sone.core.FreenetInterface.Callback;
 import net.pterodactylus.sone.core.FreenetInterface.Fetched;
@@ -60,6 +65,8 @@ import com.google.common.eventbus.EventBus;
 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 FreenetInterface}.
@@ -76,6 +83,8 @@ public class FreenetInterfaceTest {
        private final USKManager uskManager = mock(USKManager.class);
        private FreenetInterface freenetInterface;
        private final Sone sone = mock(Sone.class);
+       private final ArgumentCaptor<USKCallback> callbackCaptor = forClass(USKCallback.class);
+       private final SoneDownloader soneDownloader = mock(SoneDownloader.class);
 
        @Before
        public void setupFreenetInterface() {
@@ -93,6 +102,11 @@ public class FreenetInterfaceTest {
                when(sone.getRequestUri()).thenReturn(insertSsk.getURI().uskForSSK());
        }
 
+       @Before
+       public void setupCallbackCaptorAndUskManager() {
+               doNothing().when(uskManager).subscribe(any(USK.class), callbackCaptor.capture(), anyBoolean(), any(RequestClient.class));
+       }
+
        @Test
        public void canFetchUri() throws MalformedURLException, FetchException {
                FreenetURI freenetUri = new FreenetURI("KSK@GPLv3.txt");
@@ -284,6 +298,43 @@ public class FreenetInterfaceTest {
        }
 
        @Test
+       public void callbackPrioritiesAreInteractive() {
+               freenetInterface.registerUsk(sone, null);
+               assertThat(callbackCaptor.getValue().getPollingPriorityNormal(), is(INTERACTIVE_PRIORITY_CLASS));
+               assertThat(callbackCaptor.getValue().getPollingPriorityProgress(), is(INTERACTIVE_PRIORITY_CLASS));
+       }
+
+       @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);
+               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));
+       }
+
+       @Test
        public void fetchedRetainsUriAndFetchResult() {
                FreenetURI freenetUri = mock(FreenetURI.class);
                FetchResult fetchResult = mock(FetchResult.class);