|
16 | 16 |
|
17 | 17 | package com.cloudant.sync.internal.replication; |
18 | 18 |
|
| 19 | +import static org.junit.Assume.assumeNoException; |
| 20 | +import static org.mockito.Matchers.eq; |
19 | 21 | import static org.mockito.Mockito.any; |
20 | 22 | import static org.mockito.Mockito.anyInt; |
21 | 23 | import static org.mockito.Mockito.anyString; |
|
26 | 28 | import static org.mockito.Mockito.when; |
27 | 29 |
|
28 | 30 | import com.cloudant.common.RequireRunningCouchDB; |
| 31 | +import com.cloudant.sync.event.Subscribe; |
| 32 | +import com.cloudant.sync.internal.documentstore.DocumentRevsList; |
29 | 33 | import com.cloudant.sync.internal.mazha.ChangesResult; |
30 | 34 | import com.cloudant.sync.internal.mazha.DocumentRevs; |
31 | 35 | import com.cloudant.sync.internal.mazha.OkOpenRevision; |
32 | 36 | import com.cloudant.sync.internal.mazha.OpenRevision; |
33 | | -import com.cloudant.sync.internal.documentstore.DocumentRevsList; |
34 | | -import com.cloudant.sync.event.Subscribe; |
35 | 37 | import com.cloudant.sync.internal.util.JSONUtils; |
36 | 38 | import com.cloudant.sync.replication.PullFilter; |
37 | 39 | import com.cloudant.sync.util.TestUtils; |
@@ -231,6 +233,107 @@ public Object answer(InvocationOnMock invocation) throws Throwable { |
231 | 233 | verify(mockListener,never()).error(any(ReplicationStrategyErrored.class)); |
232 | 234 | } |
233 | 235 |
|
| 236 | + @Test |
| 237 | + public void testSetCheckpointWhenEmpty() throws Exception { |
| 238 | + CouchDB mockRemoteDb = mock(CouchDB.class); |
| 239 | + when(mockRemoteDb.changes((PullFilter) null, null, 1000)).then(new Answer<Object>() { |
| 240 | + @Override |
| 241 | + public Object answer(InvocationOnMock invocation) throws Throwable { |
| 242 | + FileReader fr = new FileReader(TestUtils.loadFixture |
| 243 | + ("fixture/empty_changes.json")); |
| 244 | + return JSONUtils.fromJson(fr, ChangesResult.class); |
| 245 | + } |
| 246 | + }); |
| 247 | + when(mockRemoteDb.exists()).thenReturn(true); |
| 248 | + |
| 249 | + StrategyListener mockListener = mock(StrategyListener.class); |
| 250 | + PullStrategy pullStrategy = super.getPullStrategy(); |
| 251 | + pullStrategy.sourceDb = mockRemoteDb; |
| 252 | + pullStrategy.getEventBus().register(mockListener); |
| 253 | + pullStrategy.run(); |
| 254 | + |
| 255 | + //should have 0 document |
| 256 | + Assert.assertEquals(this.datastore.getDocumentCount(), 0); |
| 257 | + //Checkpoint should be created in targetDb |
| 258 | + String checkpoint = |
| 259 | + (String) pullStrategy.targetDb.getCheckpoint(pullStrategy.getReplicationId()); |
| 260 | + Assert.assertEquals(checkpoint, "10-d9e5b0147af143e5b6d1979378ad957b"); |
| 261 | + //make sure the correct events were fired |
| 262 | + verify(mockListener).complete(any(ReplicationStrategyCompleted.class)); |
| 263 | + verify(mockListener, never()).error(any(ReplicationStrategyErrored.class)); |
| 264 | + } |
| 265 | + |
| 266 | + @Test |
| 267 | + public void testDoNotSetCheckpointWhenNotModified() throws Exception { |
| 268 | + try { |
| 269 | + CouchDB mockRemoteDb = mock(CouchDB.class); |
| 270 | + when(mockRemoteDb.changes((PullFilter) null, "10-d9e5b0147af143e5b6d1979378ad957b", 1000)).then(new Answer<Object>() { |
| 271 | + @Override |
| 272 | + public Object answer(InvocationOnMock invocation) throws Throwable { |
| 273 | + FileReader fr = new FileReader(TestUtils.loadFixture |
| 274 | + ("fixture/empty_changes.json")); |
| 275 | + return JSONUtils.fromJson(fr, ChangesResult.class); |
| 276 | + } |
| 277 | + }); |
| 278 | + when(mockRemoteDb.exists()).thenReturn(true); |
| 279 | + |
| 280 | + DatastoreWrapper mockLocalDb = mock(DatastoreWrapper.class); |
| 281 | + when(mockLocalDb.getCheckpoint(any(String.class))).thenReturn("10" + |
| 282 | + "-d9e5b0147af143e5b6d1979378ad957b"); |
| 283 | + |
| 284 | + StrategyListener mockListener = mock(StrategyListener.class); |
| 285 | + PullStrategy pullStrategy = super.getPullStrategy(); |
| 286 | + pullStrategy.sourceDb = mockRemoteDb; |
| 287 | + pullStrategy.targetDb = mockLocalDb; |
| 288 | + pullStrategy.getEventBus().register(mockListener); |
| 289 | + pullStrategy.run(); |
| 290 | + |
| 291 | + //make sure the correct events were fired |
| 292 | + verify(mockListener).complete(any(ReplicationStrategyCompleted.class)); |
| 293 | + verify(mockListener, never()).error(any(ReplicationStrategyErrored.class)); |
| 294 | + verify(mockLocalDb, never()).putCheckpoint(anyString(), any()); |
| 295 | + } catch(UnsupportedOperationException uoe) { |
| 296 | + assumeNoException("Cannot proxy DatastoreWrapper on Dalvik", uoe); |
| 297 | + } |
| 298 | + } |
| 299 | + |
| 300 | + @Test |
| 301 | + public void testSetCheckpointWhenModified() throws Exception { |
| 302 | + try { |
| 303 | + CouchDB mockRemoteDb = mock(CouchDB.class); |
| 304 | + when(mockRemoteDb.changes((PullFilter) null, "9-d9e5b0147af143e5b6d1979378ad957b", |
| 305 | + 1000)).then(new Answer<Object>() { |
| 306 | + @Override |
| 307 | + public Object answer(InvocationOnMock invocation) throws Throwable { |
| 308 | + FileReader fr = new FileReader(TestUtils.loadFixture |
| 309 | + ("fixture/empty_changes.json")); |
| 310 | + return JSONUtils.fromJson(fr, ChangesResult.class); |
| 311 | + } |
| 312 | + }); |
| 313 | + when(mockRemoteDb.exists()).thenReturn(true); |
| 314 | + |
| 315 | + DatastoreWrapper mockLocalDb = mock(DatastoreWrapper.class); |
| 316 | + when(mockLocalDb.getCheckpoint(any(String.class))).thenReturn("9" + |
| 317 | + "-d9e5b0147af143e5b6d1979378ad957b"); |
| 318 | + |
| 319 | + StrategyListener mockListener = mock(StrategyListener.class); |
| 320 | + PullStrategy pullStrategy = super.getPullStrategy(); |
| 321 | + pullStrategy.sourceDb = mockRemoteDb; |
| 322 | + pullStrategy.targetDb = mockLocalDb; |
| 323 | + pullStrategy.getEventBus().register(mockListener); |
| 324 | + pullStrategy.run(); |
| 325 | + |
| 326 | + //make sure the correct events were fired |
| 327 | + verify(mockListener).complete(any(ReplicationStrategyCompleted.class)); |
| 328 | + verify(mockListener, never()).error(any(ReplicationStrategyErrored.class)); |
| 329 | + verify(mockLocalDb).putCheckpoint(anyString(), eq("10" + |
| 330 | + "-d9e5b0147af143e5b6d1979378ad957b")); |
| 331 | + } catch (UnsupportedOperationException uoe) { |
| 332 | + assumeNoException("Cannot proxy DatastoreWrapper on Dalvik", uoe); |
| 333 | + } |
| 334 | + } |
| 335 | + |
| 336 | + |
234 | 337 | public class StrategyListener { |
235 | 338 |
|
236 | 339 | @Subscribe |
|
0 commit comments