|
| 1 | +package com.couchbase.lite; |
| 2 | + |
| 3 | +import com.couchbase.lite.internal.RevisionInternal; |
| 4 | +import com.couchbase.lite.javascript.JavaScriptReplicationFilterCompiler; |
| 5 | + |
| 6 | +import junit.framework.TestCase; |
| 7 | + |
| 8 | +import java.util.HashMap; |
| 9 | +import java.util.Map; |
| 10 | + |
| 11 | +/** |
| 12 | + * Created by hideki on 11/9/15. |
| 13 | + */ |
| 14 | +public class ReplicationFilterTestCase extends TestCase { |
| 15 | + private JavaScriptReplicationFilterCompiler replicationFilterCompiler; |
| 16 | + |
| 17 | + @Override |
| 18 | + public void setUp() throws Exception { |
| 19 | + super.setUp(); |
| 20 | + replicationFilterCompiler = new JavaScriptReplicationFilterCompiler(); |
| 21 | + } |
| 22 | + |
| 23 | + public void testSimpleFilterTrue() { |
| 24 | + Map<String, Object> props = new HashMap<String, Object>(); |
| 25 | + props.put("_id", "foo"); |
| 26 | + props.put("_rev", "1-1111"); |
| 27 | + props.put("_deleted", false); |
| 28 | + props.put("type", "order"); |
| 29 | + props.put("SeatNumber", 10); |
| 30 | + RevisionInternal revisionInternal = new RevisionInternal(props); |
| 31 | + SavedRevision savedRevision = new SavedRevision((Document)null, revisionInternal); |
| 32 | + ReplicationFilter replicationFilter = replicationFilterCompiler.compileFilterFunction("function(doc, req) {if(doc.type && doc.type == 'order') {return true;}else{return false}}", "javascript"); |
| 33 | + assertTrue(replicationFilter.filter(savedRevision, null)); |
| 34 | + } |
| 35 | + |
| 36 | + public void testSimpleFilterFalse() { |
| 37 | + Map<String, Object> props = new HashMap<String, Object>(); |
| 38 | + props.put("_id", "foo"); |
| 39 | + props.put("_rev", "1-1111"); |
| 40 | + props.put("_deleted", false); |
| 41 | + props.put("type", "user"); |
| 42 | + props.put("Name", "tom"); |
| 43 | + RevisionInternal revisionInternal = new RevisionInternal(props); |
| 44 | + SavedRevision savedRevision = new SavedRevision((Document)null, revisionInternal); |
| 45 | + ReplicationFilter replicationFilter = replicationFilterCompiler.compileFilterFunction("function(doc, req) {if(doc.type && doc.type == 'order') {return true;}else{return false}}", "javascript"); |
| 46 | + assertFalse(replicationFilter.filter(savedRevision, null)); |
| 47 | + } |
| 48 | + |
| 49 | + |
| 50 | + public void testSimpleFilterWithReqTrue() { |
| 51 | + Map<String, Object> props = new HashMap<String, Object>(); |
| 52 | + props.put("_id", "foo"); |
| 53 | + props.put("_rev", "1-1111"); |
| 54 | + props.put("_deleted", false); |
| 55 | + props.put("type", "order"); |
| 56 | + props.put("SeatNumber", 10); |
| 57 | + RevisionInternal revisionInternal = new RevisionInternal(props); |
| 58 | + SavedRevision savedRevision = new SavedRevision((Document)null, revisionInternal); |
| 59 | + ReplicationFilter replicationFilter = replicationFilterCompiler.compileFilterFunction("function(doc, req) {if(doc.type && doc.type == 'order' && req.abc == 1) {return true;}else{return false}}", "javascript"); |
| 60 | + Map req = new HashMap(); |
| 61 | + req.put("abc", 1); |
| 62 | + assertTrue(replicationFilter.filter(savedRevision, req)); |
| 63 | + } |
| 64 | + |
| 65 | + public void testSimpleFilterWithReqFalse() { |
| 66 | + Map<String, Object> props = new HashMap<String, Object>(); |
| 67 | + props.put("_id", "foo"); |
| 68 | + props.put("_rev", "1-1111"); |
| 69 | + props.put("_deleted", false); |
| 70 | + props.put("type", "user"); |
| 71 | + props.put("Name", "tom"); |
| 72 | + RevisionInternal revisionInternal = new RevisionInternal(props); |
| 73 | + SavedRevision savedRevision = new SavedRevision((Document)null, revisionInternal); |
| 74 | + ReplicationFilter replicationFilter = replicationFilterCompiler.compileFilterFunction("function(doc, req) {if(doc.type && doc.type == 'order' && req.abc == 1) {return true;}else{return false}}", "javascript"); |
| 75 | + Map req = new HashMap(); |
| 76 | + req.put("abc", 2); |
| 77 | + assertFalse(replicationFilter.filter(savedRevision, req)); |
| 78 | + } |
| 79 | + public void testSimpleFilterWithReqFalse2() { |
| 80 | + Map<String, Object> props = new HashMap<String, Object>(); |
| 81 | + props.put("_id", "foo"); |
| 82 | + props.put("_rev", "1-1111"); |
| 83 | + props.put("_deleted", false); |
| 84 | + props.put("type", "user"); |
| 85 | + props.put("Name", "tom"); |
| 86 | + RevisionInternal revisionInternal = new RevisionInternal(props); |
| 87 | + SavedRevision savedRevision = new SavedRevision((Document)null, revisionInternal); |
| 88 | + ReplicationFilter replicationFilter = replicationFilterCompiler.compileFilterFunction("function(doc, req) {if(doc.type && doc.type == 'order' && req.abc == 1) {return true;}else{return false}}", "javascript"); |
| 89 | + assertFalse(replicationFilter.filter(savedRevision, null)); |
| 90 | + } |
| 91 | +} |
0 commit comments