Skip to content
This repository was archived by the owner on Mar 11, 2022. It is now read-only.

Commit 6464126

Browse files
author
Tom Blench
committed
Fix deleted revision bug:
Fetching attachments on a deleted revision would cause an exception to be thrown. Fix and add test for this.
1 parent f0bfc71 commit 6464126

File tree

2 files changed

+56
-1
lines changed

2 files changed

+56
-1
lines changed

cloudant-sync-datastore-core/src/main/java/com/cloudant/sync/internal/documentstore/callables/DeleteDocumentCallable.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@
1414

1515
package com.cloudant.sync.internal.documentstore.callables;
1616

17+
import com.cloudant.sync.documentstore.Attachment;
1718
import com.cloudant.sync.documentstore.ConflictException;
1819
import com.cloudant.sync.documentstore.DocumentStoreException;
1920
import com.cloudant.sync.internal.common.CouchUtils;
20-
import com.cloudant.sync.internal.documentstore.DatabaseImpl;
2121
import com.cloudant.sync.documentstore.DocumentBodyFactory;
2222
import com.cloudant.sync.documentstore.DocumentNotFoundException;
2323
import com.cloudant.sync.internal.documentstore.InternalDocumentRevision;
@@ -30,6 +30,7 @@
3030
import com.cloudant.sync.internal.util.Misc;
3131

3232
import java.sql.SQLException;
33+
import java.util.Collections;
3334

3435
/**
3536
* Delete a Document for given Document ID and Revision ID.
@@ -125,6 +126,7 @@ public InternalDocumentRevision call(SQLDatabase db) throws ConflictException,
125126
.setCurrent(current)
126127
.setBody(DocumentBodyFactory.create(JSONUtils.emptyJSONObjectAsBytes()))
127128
.setSequence(newSequence)
129+
.setAttachments(Collections.<String, Attachment>emptyMap())
128130
.build();
129131
}
130132

cloudant-sync-datastore-core/src/test/java/com/cloudant/sync/internal/documentstore/AttachmentTest.java

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,10 @@
2323
import com.cloudant.sync.documentstore.AttachmentException;
2424
import com.cloudant.sync.documentstore.AttachmentNotSavedException;
2525
import com.cloudant.sync.documentstore.ConflictException;
26+
import com.cloudant.sync.documentstore.DocumentBodyFactory;
2627
import com.cloudant.sync.documentstore.DocumentException;
2728
import com.cloudant.sync.documentstore.DocumentRevision;
29+
import com.cloudant.sync.documentstore.DocumentStore;
2830
import com.cloudant.sync.documentstore.UnsavedFileAttachment;
2931
import com.cloudant.sync.documentstore.UnsavedStreamAttachment;
3032
import com.cloudant.sync.documentstore.encryption.NullKeyProvider;
@@ -45,6 +47,7 @@
4547
import java.nio.charset.Charset;
4648
import java.util.ArrayList;
4749
import java.util.Collections;
50+
import java.util.HashMap;
4851
import java.util.List;
4952
import java.util.Map;
5053
import java.util.regex.Matcher;
@@ -411,4 +414,54 @@ public void attachmentOrderingTest() throws Exception {
411414
Matcher m = p.matcher(regex);
412415
Assert.assertTrue("Should match pattern", m.matches());
413416
}
417+
418+
// test for regression on fetching attachments after deleting revision which previously had no
419+
// attachments
420+
@Test
421+
public void getAttachmentsOnDeletedRevisionNoAttachments() throws Exception {
422+
423+
DocumentRevision revision = new DocumentRevision();
424+
Map<String, Object> body = new HashMap<String, Object>();
425+
body.put("animal", "cat");
426+
revision.setBody(DocumentBodyFactory.create(body));
427+
428+
// create rev
429+
DocumentRevision saved = documentStore.database().create(revision);
430+
Map<String, Attachment> attachments = saved.getAttachments();
431+
Assert.assertTrue(attachments.isEmpty());
432+
433+
DocumentRevision deleted = documentStore.database().delete(saved);
434+
attachments = deleted.getAttachments();
435+
436+
Assert.assertTrue(attachments.isEmpty());
437+
}
438+
439+
440+
// test for regression on fetching attachments after deleting revision which previously had some
441+
// attachments
442+
@Test
443+
public void getAttachmentsOnDeletedRevisionSomeAttachments() throws Exception {
444+
445+
DocumentRevision revision = new DocumentRevision();
446+
Map<String, Object> body = new HashMap<String, Object>();
447+
body.put("animal", "cat");
448+
revision.setBody(DocumentBodyFactory.create(body));
449+
450+
// add attachment
451+
String imageAttachmentName = "bonsai-boston.jpg";
452+
File imageFile = TestUtils.loadFixture("fixture/" + imageAttachmentName);
453+
Attachment att = new UnsavedFileAttachment(imageFile, "image/jpeg");
454+
revision.getAttachments().put(imageAttachmentName, att);
455+
456+
//create rev
457+
DocumentRevision saved = documentStore.database().create(revision);
458+
Map<String, Attachment> attachments = saved.getAttachments();
459+
Assert.assertEquals(attachments.size(), 1);
460+
461+
DocumentRevision deleted = documentStore.database().delete(saved);
462+
attachments = deleted.getAttachments();
463+
464+
Assert.assertTrue(attachments.isEmpty());
465+
}
466+
414467
}

0 commit comments

Comments
 (0)