Add test for DI constructability of ImageInserter
[Sone.git] / src / test / java / net / pterodactylus / sone / core / SoneRescuerTest.java
1 package net.pterodactylus.sone.core;
2
3 import static org.hamcrest.MatcherAssert.assertThat;
4 import static org.hamcrest.Matchers.is;
5 import static org.mockito.ArgumentMatchers.any;
6 import static org.mockito.ArgumentMatchers.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;
12
13 import net.pterodactylus.sone.data.Sone;
14
15 import freenet.keys.FreenetURI;
16
17 import org.junit.Before;
18 import org.junit.Test;
19 import org.mockito.invocation.InvocationOnMock;
20 import org.mockito.stubbing.Answer;
21
22 /**
23  * Unit test for {@link SoneRescuer}.
24  *
25  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
26  */
27 public class SoneRescuerTest {
28
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;
35
36         @Before
37         public void setupSone() {
38                 FreenetURI soneUri = mock(FreenetURI.class);
39                 when(soneUri.getEdition()).thenReturn(CURRENT_EDITION);
40                 when(sone.getRequestUri()).thenReturn(soneUri);
41         }
42
43         @Before
44         public void setupSoneRescuer() {
45                 soneRescuer = new SoneRescuer(core, soneDownloader, sone);
46         }
47
48         @Test
49         public void newSoneRescuerIsNotFetchingAnything() {
50                 assertThat(soneRescuer.isFetching(), is(false));
51         }
52
53         @Test
54         public void newSoneRescuerStartsAtCurrentEditionOfSone() {
55                 assertThat(soneRescuer.getCurrentEdition(), is(CURRENT_EDITION));
56         }
57
58         @Test
59         public void newSoneRescuerHasANextEditionToGet() {
60                 assertThat(soneRescuer.hasNextEdition(), is(true));
61         }
62
63         @Test
64         public void soneRescuerDoesNotHaveANextEditionIfCurrentEditionIsZero() {
65                 when(sone.getRequestUri().getEdition()).thenReturn(0L);
66                 soneRescuer = new SoneRescuer(core, soneDownloader, sone);
67                 assertThat(soneRescuer.hasNextEdition(), is(false));
68         }
69
70         @Test
71         public void nextEditionIsOneSmallerThanTheCurrentEdition() {
72                 assertThat(soneRescuer.getNextEdition(), is(CURRENT_EDITION - 1));
73         }
74
75         @Test
76         public void currentEditionCanBeSet() {
77                 soneRescuer.setEdition(SOME_OTHER_EDITION);
78                 assertThat(soneRescuer.getCurrentEdition(), is(SOME_OTHER_EDITION));
79         }
80
81         @Test
82         public void lastFetchOfANewSoneRescuerWasSuccessful() {
83                 assertThat(soneRescuer.isLastFetchSuccessful(), is(true));
84         }
85
86         @Test
87         public void mainLoopStopsWhenItShould() {
88                 soneRescuer.stop();
89                 soneRescuer.serviceRun();
90         }
91
92         @Test
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));
102         }
103
104         @Test
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));
113         }
114
115         private void returnUriOnInsert(final Sone fetchedSone) {
116                 FreenetURI keyWithMetaStrings = setupFreenetUri();
117                 doAnswer(new Answer<Sone>() {
118                         @Override
119                         public Sone answer(InvocationOnMock invocation) throws Throwable {
120                                 soneRescuer.stop();
121                                 return fetchedSone;
122                         }
123                 }).when(soneDownloader).fetchSone(eq(sone), eq(keyWithMetaStrings), eq(true));
124         }
125
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;
134         }
135
136 }