Esempio n. 1
0
        public void TestPusherDeletedDoc()
        {
            var remote         = GetReplicationURL();
            var docIdTimestamp = Convert.ToString(Runtime.CurrentTimeMillis());

            // Create some documentsConvert
            var documentProperties = new Dictionary <string, object>();
            var doc1Id             = string.Format("doc1-{0}", docIdTimestamp);

            documentProperties["_id"] = doc1Id;
            documentProperties["foo"] = 1;
            documentProperties["bar"] = false;

            var body   = new Body(documentProperties);
            var rev1   = new RevisionInternal(body, database);
            var status = new Status();

            rev1 = database.PutRevision(rev1, null, false, status);
            Assert.AreEqual(StatusCode.Created, status.GetCode());

            documentProperties["_rev"]     = rev1.GetRevId();
            documentProperties["UPDATED"]  = true;
            documentProperties["_deleted"] = true;
            database.PutRevision(new RevisionInternal(documentProperties, database), rev1.GetRevId(), false, status);
            Assert.IsTrue((int)status.GetCode() >= 200 && (int)status.GetCode() < 300);

            var repl = database.CreatePushReplication(remote);

            if (!IsSyncGateway(remote))
            {
                ((Pusher)repl).CreateTarget = true;
            }

            RunReplication(repl);

            Assert.IsNull(repl.LastError);

            // make sure doc1 is deleted
            var replicationUrlTrailing = new Uri(string.Format("{0}/", remote));
            var pathToDoc = new Uri(replicationUrlTrailing, doc1Id);

            Log.D(Tag, "Send http request to " + pathToDoc);
            var httpRequestDoneSignal = new CountDownLatch(1);
            var httpclient            = new HttpClient();

            try
            {
                var getDocResponse = httpclient.GetAsync(pathToDoc.ToString()).Result;
                var statusLine     = getDocResponse.StatusCode;
                Log.D(ReplicationTest.Tag, "statusLine " + statusLine);
                Assert.AreEqual(Couchbase.Lite.StatusCode.NotFound, statusLine.GetStatusCode());
            }
            catch (ProtocolViolationException e)
            {
                Assert.IsNull(e, "Got ClientProtocolException: " + e.Message);
            }
            catch (IOException e)
            {
                Assert.IsNull(e, "Got IOException: " + e.Message);
            }
            finally
            {
                httpRequestDoneSignal.CountDown();
            }
            Log.D(Tag, "Waiting for http request to finish");
            try
            {
                httpRequestDoneSignal.Await(TimeSpan.FromSeconds(10));
                Log.D(Tag, "http request finished");
            }
            catch (Exception e)
            {
                Runtime.PrintStackTrace(e);
            }
            Log.D(Tag, "testPusherDeletedDoc() finished");
        }
Esempio n. 2
0
        public void TestCRUDOperations()
        {
            database.Changed += (sender, e) => {
                var changes = e.Changes.ToList();
                foreach (DocumentChange change in changes)
                {
                    var rev = change.AddedRevision;
                    Assert.IsNotNull(rev);
                    Assert.IsNotNull(rev.GetDocId());
                    Assert.IsNotNull(rev.GetRevId());
                    Assert.AreEqual(rev.GetDocId(), rev.GetProperties()["_id"]);
                    Assert.AreEqual(rev.GetRevId(), rev.GetProperties()["_rev"]);
                }
            };

            var privateUUID = database.PrivateUUID();
            var publicUUID  = database.PublicUUID();

            Log.V(Tag, "DB private UUID = '" + privateUUID + "', public UUID = '" +
                  publicUUID + "'");
            Assert.IsTrue(privateUUID.Length >= 20);
            Assert.IsTrue(publicUUID.Length >= 20);

            //create a document
            var documentProperties = new Dictionary <string, object>();

            documentProperties["foo"] = 1;
            documentProperties["bar"] = false;
            documentProperties["baz"] = "touch";

            var body = new Body(documentProperties);
            var rev1 = new RevisionInternal(body);

            var status = new Status();

            rev1 = database.PutRevision(rev1, null, false, status);
            Log.V(Tag, "Created " + rev1);
            Assert.IsTrue(rev1.GetDocId().Length >= 10);
            Assert.IsTrue(rev1.GetRevId().StartsWith("1-"));

            //read it back
            var readRev = database.GetDocument(rev1.GetDocId(), null,
                                               true);

            Assert.IsNotNull(readRev);

            var userReadRevProps = UserProperties(readRev.GetProperties());
            var userBodyProps    = UserProperties(body.GetProperties());

            Assert.AreEqual(userReadRevProps.Count, userBodyProps.Count);
            foreach (var key in userReadRevProps.Keys)
            {
                Assert.AreEqual(userReadRevProps[key], userBodyProps[key]);
            }

            //now update it
            documentProperties           = (Dictionary <string, object>)readRev.GetProperties();
            documentProperties["status"] = "updated!";
            body = new Body(documentProperties);
            var rev2      = new RevisionInternal(body);
            var rev2input = rev2;

            rev2 = database.PutRevision(rev2, rev1.GetRevId(), false, status);
            Log.V(Tag, "Updated " + rev1);
            Assert.AreEqual(rev1.GetDocId(), rev2.GetDocId());
            Assert.IsTrue(rev2.GetRevId().StartsWith("2-"));

            //read it back
            readRev = database.GetDocument(rev2.GetDocId(), null,
                                           true);
            Assert.IsNotNull(readRev);
            Assert.AreEqual(UserProperties(readRev.GetProperties()), UserProperties
                                (body.GetProperties()));

            // Try to update the first rev, which should fail:
            database.PutRevision(rev2input, rev1.GetRevId(), false, status);
            Assert.AreEqual(StatusCode.Conflict, status.Code);

            // Check the changes feed, with and without filters:
            var changeRevisions = database.ChangesSince(0, null, null, null);

            Log.V(Tag, "Changes = " + changeRevisions);
            Assert.AreEqual(1, changeRevisions.Count);

            changeRevisions = database.ChangesSince(0, null,
                                                    (revision, items) => "updated!".Equals(revision.Properties.Get("status")), null);
            Assert.AreEqual(1, changeRevisions.Count);

            changeRevisions = database.ChangesSince(0, null,
                                                    (revision, items) => "not updated!".Equals(revision.Properties.Get("status")), null);
            Assert.AreEqual(0, changeRevisions.Count);

            // Delete it:
            var revD = new RevisionInternal(rev2.GetDocId(), null, true);
            RevisionInternal revResult = null;

            revResult = database.PutRevision(revD, null, false, status);
            Assert.AreEqual(StatusCode.Conflict, status.Code);
            Assert.IsNull(revResult);

            revD = database.PutRevision(revD, rev2.GetRevId(), false, status);
            Assert.AreEqual(StatusCode.Ok, status.Code);
            Assert.AreEqual(revD.GetDocId(), rev2.GetDocId());
            Assert.IsTrue(revD.GetRevId().StartsWith("3-"));

            // Delete nonexistent doc:
            var revFake = new RevisionInternal("fake", null, true);

            database.PutRevision(revFake, null, false, status);
            Assert.AreEqual(StatusCode.NotFound, status.Code);

            // Read it back (should fail):
            readRev = database.GetDocument(revD.GetDocId(), null,
                                           true);
            Assert.IsNull(readRev);

            // Get Changes feed:
            changeRevisions = database.ChangesSince(0, null, null, null);
            Assert.IsTrue(changeRevisions.Count == 1);

            // Get Revision History:
            IList <RevisionInternal> history = database.Storage.GetRevisionHistory(revD, null);

            Assert.AreEqual(revD, history[0]);
            Assert.AreEqual(rev2, history[1]);
            Assert.AreEqual(rev1, history[2]);
        }
Esempio n. 3
0
        public void TestPusher()
        {
            var remote         = GetReplicationURL();
            var docIdTimestamp = Convert.ToString(Runtime.CurrentTimeMillis());

            // Create some documents:
            var documentProperties = new Dictionary <string, object>();
            var doc1Id             = string.Format("doc1-{0}", docIdTimestamp);

            documentProperties["_id"] = doc1Id;
            documentProperties["foo"] = 1;
            documentProperties["bar"] = false;

            var body   = new Body(documentProperties);
            var rev1   = new RevisionInternal(body, database);
            var status = new Status();

            rev1 = database.PutRevision(rev1, null, false, status);
            Assert.AreEqual(StatusCode.Created, status.GetCode());

            documentProperties.Put("_rev", rev1.GetRevId());
            documentProperties["UPDATED"] = true;
            database.PutRevision(new RevisionInternal(documentProperties, database), rev1.GetRevId(), false, status);
            Assert.AreEqual(StatusCode.Created, status.GetCode());

            documentProperties = new Dictionary <string, object>();
            var doc2Id = string.Format("doc2-{0}", docIdTimestamp);

            documentProperties["_id"]   = doc2Id;
            documentProperties["baz"]   = 666;
            documentProperties["fnord"] = true;

            database.PutRevision(new RevisionInternal(documentProperties, database), null, false, status);
            Assert.AreEqual(StatusCode.Created, status.GetCode());

            var doc2             = database.GetDocument(doc2Id);
            var doc2UnsavedRev   = doc2.CreateRevision();
            var attachmentStream = GetAsset("attachment.png");

            doc2UnsavedRev.SetAttachment("attachment.png", "image/png", attachmentStream);
            var doc2Rev = doc2UnsavedRev.Save();

            Assert.IsNotNull(doc2Rev);

            const bool continuous = false;
            var        repl       = database.CreatePushReplication(remote);

            repl.Continuous = continuous;
            if (!IsSyncGateway(remote))
            {
                repl.CreateTarget = true;
            }

            // Check the replication's properties:
            Assert.AreEqual(database, repl.LocalDatabase);
            Assert.AreEqual(remote, repl.RemoteUrl);
            Assert.IsFalse(repl.IsPull);
            Assert.IsFalse(repl.Continuous);
            Assert.IsNull(repl.Filter);
            Assert.IsNull(repl.FilterParams);
            Assert.IsNull(repl.DocIds);
            // TODO: CAssertNil(r1.headers); still not null!
            // Check that the replication hasn't started running:
            Assert.IsFalse(repl.IsRunning);
            Assert.AreEqual((int)repl.Status, (int)ReplicationStatus.Stopped);
            Assert.AreEqual(0, repl.CompletedChangesCount);
            Assert.AreEqual(0, repl.ChangesCount);
            Assert.IsNull(repl.LastError);

            RunReplication(repl);

            // TODO: Verify the foloowing 2 asserts. ChangesCount and CompletedChangesCount
            // should already be reset when the replicator stopped.
            Assert.IsTrue(repl.ChangesCount >= 2);
            Assert.IsTrue(repl.CompletedChangesCount >= 2);
            Assert.IsNull(repl.LastError);

            VerifyRemoteDocExists(remote, doc1Id);

            // Add doc3
            documentProperties = new Dictionary <string, object>();
            var doc3Id = string.Format("doc3-{0}", docIdTimestamp);
            var doc3   = database.GetDocument(doc3Id);

            documentProperties["bat"] = 677;
            doc3.PutProperties(documentProperties);

            // re-run push replication
            var repl2 = database.CreatePushReplication(remote);

            repl2.Continuous = continuous;
            if (!IsSyncGateway(remote))
            {
                repl2.CreateTarget = true;
            }

            var repl2CheckedpointId = repl2.RemoteCheckpointDocID();

            RunReplication(repl2);

            Assert.IsNull(repl2.LastError);

            // make sure trhe doc has been added
            VerifyRemoteDocExists(remote, doc3Id);

            Assert.AreEqual(repl2.LastSequence, database.LastSequenceWithCheckpointId(repl2CheckedpointId));

            System.Threading.Thread.Sleep(2000);
            var json = GetRemoteDoc(remote, repl2CheckedpointId);
            var remoteLastSequence = (string)json["lastSequence"];

            Assert.AreEqual(repl2.LastSequence, remoteLastSequence);

            Log.D(Tag, "testPusher() finished");
        }
        /// <exception cref="Couchbase.Lite.CouchbaseLiteException"></exception>
        public virtual void TestLocalDocs()
        {
            //create a document
            IDictionary <string, object> documentProperties = new Dictionary <string, object>();

            documentProperties["_id"] = "_local/doc1";
            documentProperties["foo"] = 1;
            documentProperties["bar"] = false;
            Body             body   = new Body(documentProperties);
            RevisionInternal rev1   = new RevisionInternal(body, database);
            Status           status = new Status();

            rev1 = database.PutLocalRevision(rev1, null);
            Log.V(Tag, "Created " + rev1);
            NUnit.Framework.Assert.AreEqual("_local/doc1", rev1.GetDocId());
            NUnit.Framework.Assert.IsTrue(rev1.GetRevId().StartsWith("1-"));
            //read it back
            RevisionInternal readRev = database.GetLocalDocument(rev1.GetDocId(), null);

            NUnit.Framework.Assert.IsNotNull(readRev);
            IDictionary <string, object> readRevProps = readRev.Properties;

            NUnit.Framework.Assert.AreEqual(rev1.GetDocId(), readRev.Properties.Get("_id"
                                                                                    ));
            NUnit.Framework.Assert.AreEqual(rev1.GetRevId(), readRev.Properties.Get("_rev"
                                                                                    ));
            NUnit.Framework.Assert.AreEqual(UserProperties(readRevProps), UserProperties(body
                                                                                         .Properties));
            //now update it
            documentProperties           = readRev.Properties;
            documentProperties["status"] = "updated!";
            body = new Body(documentProperties);
            RevisionInternal rev2      = new RevisionInternal(body, database);
            RevisionInternal rev2input = rev2;

            rev2 = database.PutLocalRevision(rev2, rev1.GetRevId());
            Log.V(Tag, "Updated " + rev1);
            NUnit.Framework.Assert.AreEqual(rev1.GetDocId(), rev2.GetDocId());
            NUnit.Framework.Assert.IsTrue(rev2.GetRevId().StartsWith("2-"));
            //read it back
            readRev = database.GetLocalDocument(rev2.GetDocId(), null);
            NUnit.Framework.Assert.IsNotNull(readRev);
            NUnit.Framework.Assert.AreEqual(UserProperties(readRev.Properties), UserProperties
                                                (body.Properties));
            // Try to update the first rev, which should fail:
            bool gotException = false;

            try
            {
                database.PutLocalRevision(rev2input, rev1.GetRevId());
            }
            catch (CouchbaseLiteException e)
            {
                NUnit.Framework.Assert.AreEqual(StatusCode.Conflict, e.GetCBLStatus().GetCode());
                gotException = true;
            }
            NUnit.Framework.Assert.IsTrue(gotException);
            // Delete it:
            RevisionInternal revD = new RevisionInternal(rev2.GetDocId(), null, true, database
                                                         );

            gotException = false;
            try
            {
                RevisionInternal revResult = database.PutLocalRevision(revD, null);
                NUnit.Framework.Assert.IsNull(revResult);
            }
            catch (CouchbaseLiteException e)
            {
                NUnit.Framework.Assert.AreEqual(StatusCode.Conflict, e.GetCBLStatus().GetCode());
                gotException = true;
            }
            NUnit.Framework.Assert.IsTrue(gotException);
            revD = database.PutLocalRevision(revD, rev2.GetRevId());
            // Delete nonexistent doc:
            gotException = false;
            RevisionInternal revFake = new RevisionInternal("_local/fake", null, true, database
                                                            );

            try
            {
                database.PutLocalRevision(revFake, null);
            }
            catch (CouchbaseLiteException e)
            {
                NUnit.Framework.Assert.AreEqual(StatusCode.Conflict, e.GetCBLStatus().GetCode());
                gotException = true;
            }
            NUnit.Framework.Assert.IsTrue(gotException);
            // Read it back (should fail):
            readRev = database.GetLocalDocument(revD.GetDocId(), null);
            NUnit.Framework.Assert.IsNull(readRev);
        }