|
| 1 | +/* |
| 2 | + * Copyright © 2016 IBM Corp. All rights reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file |
| 5 | + * except in compliance with the License. You may obtain a copy of the License at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software distributed under the |
| 10 | + * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, |
| 11 | + * either express or implied. See the License for the specific language governing permissions |
| 12 | + * and limitations under the License. |
| 13 | + */ |
| 14 | + |
| 15 | +package com.cloudant.sync.replication; |
| 16 | + |
| 17 | + |
| 18 | +import static org.mockito.Matchers.anyInt; |
| 19 | +import static org.mockito.Matchers.anyMapOf; |
| 20 | +import static org.mockito.Matchers.anyString; |
| 21 | +import static org.mockito.Mockito.doAnswer; |
| 22 | +import static org.mockito.Mockito.spy; |
| 23 | + |
| 24 | +import com.cloudant.mazha.ChangesResult; |
| 25 | +import com.cloudant.mazha.CouchClient; |
| 26 | +import com.cloudant.sync.datastore.DocumentRevision; |
| 27 | +import com.cloudant.sync.datastore.DocumentRevisionTree; |
| 28 | + |
| 29 | +import org.junit.Assert; |
| 30 | +import org.junit.Before; |
| 31 | +import org.junit.Test; |
| 32 | +import org.mockito.Matchers; |
| 33 | +import org.mockito.invocation.InvocationOnMock; |
| 34 | +import org.mockito.stubbing.Answer; |
| 35 | + |
| 36 | + |
| 37 | +public class MissingRevsReplicationTest extends ReplicationTestBase { |
| 38 | + |
| 39 | + private CouchClient clientMock; |
| 40 | + |
| 41 | + @Before |
| 42 | + public void setupMocks() throws Exception { |
| 43 | + // Partially mock the remote and replace it for our test |
| 44 | + clientMock = spy(remoteDb.couchClient); |
| 45 | + remoteDb.couchClient = clientMock; |
| 46 | + couchClient = clientMock; |
| 47 | + } |
| 48 | + |
| 49 | + @Override |
| 50 | + protected PullStrategy getPullStrategy() { |
| 51 | + PullStrategy s = super.getPullStrategy(); |
| 52 | + s.sourceDb = remoteDb; |
| 53 | + return s; |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + public void testReplicationWithMissingRevision() throws Exception { |
| 58 | + // Create doc |
| 59 | + Bar a = BarUtils.createBar(remoteDb, "testdoc", "alpha", 1); |
| 60 | + // Update doc |
| 61 | + final Bar b = BarUtils.updateBar(remoteDb, "testdoc", "beta", 2); |
| 62 | + |
| 63 | + Bar c = BarUtils.updateBar(remoteDb, "testdoc", "gamma", 3); |
| 64 | + // We have 3 remote revisions |
| 65 | + |
| 66 | + doAnswer(new Answer<ChangesResult>() { |
| 67 | + |
| 68 | + @Override |
| 69 | + public ChangesResult answer(InvocationOnMock invocation) throws Throwable { |
| 70 | + @SuppressWarnings("unchecked") |
| 71 | + ChangesResult changesResult = (ChangesResult) invocation.callRealMethod(); |
| 72 | + |
| 73 | + // modify the changes feed so that it returns a revision that isn't a leaf revision. |
| 74 | + changesResult.getResults().get(0).getChanges().get(0).setRev(b.getRevision()); |
| 75 | + return changesResult; |
| 76 | + |
| 77 | + } |
| 78 | + }).when(clientMock).changes(anyString(), anyMapOf(String.class, String.class), Matchers.anyObject(), anyInt()); |
| 79 | + |
| 80 | + // Do the pull replication |
| 81 | + pull(); |
| 82 | + // Validate the document was created at the correct rev |
| 83 | + DocumentRevision rev = datastore.getDocument("testdoc"); |
| 84 | + Assert.assertNotNull("The document should be present", rev); |
| 85 | + Assert.assertEquals("The local revision should be the third generation", |
| 86 | + c.getRevision(), |
| 87 | + rev.getRevision()); |
| 88 | + // Validate that there are 3 revisions. |
| 89 | + DocumentRevisionTree tree = datastore.getAllRevisionsOfDocument("testdoc"); |
| 90 | + Assert.assertEquals("The 3rd generation current revision should have depth 2", 2, tree.depth |
| 91 | + (tree.getCurrentRevision().getSequence())); |
| 92 | + } |
| 93 | +} |
0 commit comments