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;
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;
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;
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}.
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() {
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");
}
@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);