Increase test coverage.
[Sone.git] / src / test / java / net / pterodactylus / sone / core / UpdateCheckerTest.java
1 package net.pterodactylus.sone.core;
2
3 import static java.lang.Long.MAX_VALUE;
4 import static net.pterodactylus.sone.main.SonePlugin.VERSION;
5 import static org.hamcrest.MatcherAssert.assertThat;
6 import static org.hamcrest.Matchers.instanceOf;
7 import static org.hamcrest.Matchers.is;
8 import static org.mockito.ArgumentCaptor.forClass;
9 import static org.mockito.Matchers.any;
10 import static org.mockito.Matchers.argThat;
11 import static org.mockito.Mockito.mock;
12 import static org.mockito.Mockito.never;
13 import static org.mockito.Mockito.times;
14 import static org.mockito.Mockito.verify;
15 import static org.mockito.Mockito.when;
16
17 import java.io.IOException;
18 import java.io.InputStream;
19
20 import net.pterodactylus.sone.core.FreenetInterface.Callback;
21 import net.pterodactylus.sone.core.FreenetInterface.Fetched;
22 import net.pterodactylus.sone.core.event.UpdateFoundEvent;
23 import net.pterodactylus.sone.freenet.StringBucket;
24 import net.pterodactylus.util.version.Version;
25
26 import freenet.client.ClientMetadata;
27 import freenet.client.FetchResult;
28 import freenet.keys.FreenetURI;
29 import freenet.support.api.Bucket;
30
31 import com.google.common.eventbus.EventBus;
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.mockito.ArgumentCaptor;
35 import org.mockito.invocation.InvocationOnMock;
36 import org.mockito.stubbing.Answer;
37
38 /**
39  * Unit test for {@link UpdateChecker}.
40  *
41  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
42  */
43 public class UpdateCheckerTest {
44
45         private final EventBus eventBus = mock(EventBus.class);
46         private final FreenetInterface freenetInterface = mock(FreenetInterface.class);
47         private final UpdateChecker updateChecker = new UpdateChecker(eventBus, freenetInterface);
48
49         @Before
50         public void startUpdateChecker() {
51                 updateChecker.start();
52         }
53
54         @Test
55         public void newUpdateCheckerDoesNotHaveALatestVersion() {
56                 assertThat(updateChecker.hasLatestVersion(), is(false));
57                 assertThat(updateChecker.getLatestVersion(), is(VERSION));
58         }
59
60         @Test
61         public void startingAnUpdateCheckerRegisterAUsk() {
62                 verify(freenetInterface).registerUsk(any(FreenetURI.class), any(Callback.class));
63         }
64
65         @Test
66         public void stoppingAnUpdateCheckerUnregistersAUsk() {
67                 updateChecker.stop();
68                 verify(freenetInterface).unregisterUsk(any(FreenetURI.class));
69         }
70
71         @Test
72         public void callbackDoesNotDownloadIfNewEditionIsNotFound() {
73                 setupCallbackWithEdition(MAX_VALUE, false, false);
74                 verify(freenetInterface, never()).fetchUri(any(FreenetURI.class));
75                 verify(eventBus, never()).post(argThat(instanceOf(UpdateFoundEvent.class)));
76         }
77
78         private void setupCallbackWithEdition(long edition, boolean newKnownGood, boolean newSlot) {
79                 ArgumentCaptor<FreenetURI> uri = forClass(FreenetURI.class);
80                 ArgumentCaptor<Callback> callback = forClass(Callback.class);
81                 verify(freenetInterface).registerUsk(uri.capture(), callback.capture());
82                 callback.getValue().editionFound(uri.getValue(), edition, newKnownGood, newSlot);
83         }
84
85         @Test
86         public void callbackStartsIfNewEditionIsFound() {
87                 setupFetchResult(createFutureFetchResult());
88                 setupCallbackWithEdition(MAX_VALUE, true, false);
89                 verifyAFreenetUriIsFetched();
90                 ArgumentCaptor<UpdateFoundEvent> updateFoundEvent = forClass(UpdateFoundEvent.class);
91                 verify(eventBus, times(1)).post(updateFoundEvent.capture());
92                 assertThat(updateFoundEvent.getValue().version(), is(new Version(99, 0, 0)));
93                 assertThat(updateFoundEvent.getValue().releaseTime(), is(11865368297000L));
94                 assertThat(updateChecker.getLatestVersion(), is(new Version(99, 0, 0)));
95                 assertThat(updateChecker.getLatestVersionDate(), is(11865368297000L));
96         }
97
98         private FetchResult createFutureFetchResult() {
99                 ClientMetadata clientMetadata = new ClientMetadata("application/xml");
100                 Bucket fetched = new StringBucket("# MapConfigurationBackendVersion=1\n" +
101                                 "CurrentVersion/Version: 99.0.0\n" +
102                                 "CurrentVersion/ReleaseTime: 11865368297000");
103                 return new FetchResult(clientMetadata, fetched);
104         }
105
106         @Test
107         public void callbackDoesNotStartIfNoNewEditionIsFound() {
108                 setupFetchResult(createPastFetchResult());
109                 setupCallbackWithEdition(updateChecker.getLatestEdition(), true, false);
110                 verifyAFreenetUriIsFetched();
111                 verifyNoUpdateFoundEventIsFired();
112         }
113
114         private void setupFetchResult(final FetchResult pastFetchResult) {
115                 when(freenetInterface.fetchUri(any(FreenetURI.class))).thenAnswer(new Answer<Fetched>() {
116                         @Override
117                         public Fetched answer(InvocationOnMock invocation) throws Throwable {
118                                 FreenetURI freenetUri = (FreenetURI) invocation.getArguments()[0];
119                                 return new Fetched(freenetUri, pastFetchResult);
120                         }
121                 });
122         }
123
124         private FetchResult createPastFetchResult() {
125                 ClientMetadata clientMetadata = new ClientMetadata("application/xml");
126                 Bucket fetched = new StringBucket("# MapConfigurationBackendVersion=1\n" +
127                                 "CurrentVersion/Version: 0.2\n" +
128                                 "CurrentVersion/ReleaseTime: 1289417883000");
129                 return new FetchResult(clientMetadata, fetched);
130         }
131
132         @Test
133         public void invalidUpdateFileDoesNotStartCallback() {
134                 setupFetchResult(createInvalidFetchResult());
135                 setupCallbackWithEdition(MAX_VALUE, true, false);
136                 verifyAFreenetUriIsFetched();
137                 verifyNoUpdateFoundEventIsFired();
138         }
139
140         private FetchResult createInvalidFetchResult() {
141                 ClientMetadata clientMetadata = new ClientMetadata("text/plain");
142                 Bucket fetched = new StringBucket("Some other data.");
143                 return new FetchResult(clientMetadata, fetched);
144         }
145
146         @Test
147         public void nonExistingPropertiesWillNotCauseUpdateToBeFound() {
148                 setupCallbackWithEdition(MAX_VALUE, true, false);
149                 verifyAFreenetUriIsFetched();
150                 verifyNoUpdateFoundEventIsFired();
151         }
152
153         private void verifyNoUpdateFoundEventIsFired() {
154                 verify(eventBus, never()).post(any(UpdateFoundEvent.class));
155         }
156
157         private void verifyAFreenetUriIsFetched() {
158                 verify(freenetInterface).fetchUri(any(FreenetURI.class));
159         }
160
161         @Test
162         public void brokenBucketDoesNotCauseUpdateToBeFound() {
163                 setupFetchResult(createBrokenBucketFetchResult());
164                 setupCallbackWithEdition(MAX_VALUE, true, false);
165                 verifyAFreenetUriIsFetched();
166                 verifyNoUpdateFoundEventIsFired();
167         }
168
169         private FetchResult createBrokenBucketFetchResult() {
170                 ClientMetadata clientMetadata = new ClientMetadata("text/plain");
171                 Bucket fetched = new StringBucket("Some other data.") {
172                         @Override
173                         public InputStream getInputStream() {
174                                 try {
175                                         return when(mock(InputStream.class).read()).thenThrow(IOException.class).getMock();
176                                 } catch (IOException ioe1) {
177                                         /* won’t throw here. */
178                                         return null;
179                                 }
180                         }
181                 };
182                 return new FetchResult(clientMetadata, fetched);
183         }
184
185         @Test
186         public void invalidTimeDoesNotCauseAnUpdateToBeFound() {
187                 setupFetchResult(createInvalidTimeFetchResult());
188                 setupCallbackWithEdition(MAX_VALUE, true, false);
189                 verifyAFreenetUriIsFetched();
190                 verifyNoUpdateFoundEventIsFired();
191         }
192
193         private FetchResult createInvalidTimeFetchResult() {
194                 ClientMetadata clientMetadata = new ClientMetadata("application/xml");
195                 Bucket fetched = new StringBucket("# MapConfigurationBackendVersion=1\n" +
196                                 "CurrentVersion/Version: 0.2\n" +
197                                 "CurrentVersion/ReleaseTime: invalid");
198                 return new FetchResult(clientMetadata, fetched);
199         }
200
201 }