1 package net.pterodactylus.sone.core;
3 import static org.hamcrest.MatcherAssert.assertThat;
4 import static org.hamcrest.Matchers.is;
5 import static org.mockito.Matchers.any;
6 import static org.mockito.Matchers.eq;
7 import static org.mockito.Mockito.doAnswer;
8 import static org.mockito.Mockito.mock;
9 import static org.mockito.Mockito.never;
10 import static org.mockito.Mockito.verify;
11 import static org.mockito.Mockito.when;
13 import net.pterodactylus.sone.data.Sone;
15 import freenet.keys.FreenetURI;
17 import org.junit.Before;
18 import org.junit.Test;
19 import org.mockito.invocation.InvocationOnMock;
20 import org.mockito.stubbing.Answer;
23 * Unit test for {@link SoneRescuer}.
25 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
27 public class SoneRescuerTest {
29 private static final long CURRENT_EDITION = 12L;
30 private static final long SOME_OTHER_EDITION = 15L;
31 private final Core core = mock(Core.class);
32 private final SoneDownloader soneDownloader = mock(SoneDownloader.class);
33 private final Sone sone = mock(Sone.class);
34 private SoneRescuer soneRescuer;
37 public void setupSone() {
38 FreenetURI soneUri = mock(FreenetURI.class);
39 when(soneUri.getEdition()).thenReturn(CURRENT_EDITION);
40 when(sone.getRequestUri()).thenReturn(soneUri);
44 public void setupSoneRescuer() {
45 soneRescuer = new SoneRescuer(core, soneDownloader, sone);
49 public void newSoneRescuerIsNotFetchingAnything() {
50 assertThat(soneRescuer.isFetching(), is(false));
54 public void newSoneRescuerStartsAtCurrentEditionOfSone() {
55 assertThat(soneRescuer.getCurrentEdition(), is(CURRENT_EDITION));
59 public void newSoneRescuerHasANextEditionToGet() {
60 assertThat(soneRescuer.hasNextEdition(), is(true));
64 public void soneRescuerDoesNotHaveANextEditionIfCurrentEditionIsZero() {
65 when(sone.getRequestUri().getEdition()).thenReturn(0L);
66 soneRescuer = new SoneRescuer(core, soneDownloader, sone);
67 assertThat(soneRescuer.hasNextEdition(), is(false));
71 public void nextEditionIsOneSmallerThanTheCurrentEdition() {
72 assertThat(soneRescuer.getNextEdition(), is(CURRENT_EDITION - 1));
76 public void currentEditionCanBeSet() {
77 soneRescuer.setEdition(SOME_OTHER_EDITION);
78 assertThat(soneRescuer.getCurrentEdition(), is(SOME_OTHER_EDITION));
82 public void lastFetchOfANewSoneRescuerWasSuccessful() {
83 assertThat(soneRescuer.isLastFetchSuccessful(), is(true));
87 public void mainLoopStopsWhenItShould() {
89 soneRescuer.serviceRun();
93 public void successfulInsert() {
94 final Sone fetchedSone = mock(Sone.class);
95 returnUriOnInsert(fetchedSone);
96 soneRescuer.startNextFetch();
97 soneRescuer.serviceRun();
98 verify(core).lockSone(eq(sone));
99 verify(core).updateSone(eq(fetchedSone), eq(true));
100 assertThat(soneRescuer.isLastFetchSuccessful(), is(true));
101 assertThat(soneRescuer.isFetching(), is(false));
105 public void nonSuccessfulInsertIsRecognized() {
106 returnUriOnInsert(null);
107 soneRescuer.startNextFetch();
108 soneRescuer.serviceRun();
109 verify(core).lockSone(eq(sone));
110 verify(core, never()).updateSone(any(Sone.class), eq(true));
111 assertThat(soneRescuer.isLastFetchSuccessful(), is(false));
112 assertThat(soneRescuer.isFetching(), is(false));
115 private void returnUriOnInsert(final Sone fetchedSone) {
116 FreenetURI keyWithMetaStrings = setupFreenetUri();
117 doAnswer(new Answer<Sone>() {
119 public Sone answer(InvocationOnMock invocation) throws Throwable {
123 }).when(soneDownloader).fetchSone(eq(sone), eq(keyWithMetaStrings), eq(true));
126 private FreenetURI setupFreenetUri() {
127 FreenetURI sskKey = mock(FreenetURI.class);
128 FreenetURI keyWithDocName = mock(FreenetURI.class);
129 FreenetURI keyWithMetaStrings = mock(FreenetURI.class);
130 when(keyWithDocName.setMetaString(eq(new String[] { "sone.xml" }))).thenReturn(keyWithMetaStrings);
131 when(sskKey.setDocName(eq("Sone-" + CURRENT_EDITION))).thenReturn(keyWithDocName);
132 when(sone.getRequestUri().setKeyType(eq("SSK"))).thenReturn(sskKey);
133 return keyWithMetaStrings;