Android Open Source - couchbase-lite-android Mock Changes Feed






From Project

Back to project page couchbase-lite-android.

License

The source code is released under:

Apache License

If you think the Android project couchbase-lite-android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.couchbase.lite.mockserver;
//w ww.j a va 2s.  c  om
import com.couchbase.lite.Manager;
import com.squareup.okhttp.mockwebserver.MockResponse;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/*

    Generate mock changes feed, eg:

    {
       "results":[
          {
             "seq":1,
             "id":"doc1-1402588904847",
             "changes":[
                {
                   "rev":"1-d57b1bc60eb9273c3349d932e15f9949"
                }
             ]
          },
          {
             "seq":2,
             "id":"doc2-1402588904847",
             "changes":[
                {
                   "rev":"1-d57b1bc60eb9273c3349d932e15f9949"
                }
             ]
          }
       ],
       "last_seq":2
    }

 */
public class MockChangesFeed {

    private List<MockChangedDoc> mockChangedDocs;

    public MockChangesFeed() {
        mockChangedDocs = new ArrayList<MockChangedDoc>();
    }

    public void add(MockChangedDoc mockChangedDoc) {
        mockChangedDocs.add(mockChangedDoc);
    }

    private int getHighestSeq() {
        if (mockChangedDocs.size() == 0) {
            return -1;
        }
        // just assume they were added in the right order,
        // so get the last one and use that
        int indexLast = mockChangedDocs.size() - 1;
        MockChangedDoc lastDoc = mockChangedDocs.get(indexLast);
        return lastDoc.getSeq();
    }

    private Map<String, Object> generateChangesMap() {
        Map<String, Object> changesMap = new HashMap<String, Object>();
        int highestSeq = getHighestSeq();
        if (highestSeq > 0) {
            changesMap.put("last_seq", highestSeq);
        }
        List results = new ArrayList();
        for (MockChangedDoc mockChangedDoc : mockChangedDocs) {
            results.add(mockChangedDoc.exportAsMap());
        }
        changesMap.put("results", results);
        return changesMap;
    }

    private String generateChangesBody() {
        Map changesMap = generateChangesMap();
        try {
            return Manager.getObjectMapper().writeValueAsString(changesMap);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public MockResponse generateMockResponse() {
        MockResponse mockResponse = new MockResponse();
        mockResponse.setBody(generateChangesBody());
        MockHelper.set200OKJson(mockResponse);
        return mockResponse;
    }

    public static class MockChangedDoc {

        private int seq;
        private String docId;
        private List<String> changedRevIds;

        public MockChangedDoc() {
        }

        public MockChangedDoc(MockDocumentGet.MockDocument mockDocument) {
            this();
            this.seq = mockDocument.getDocSeq();
            this.docId = mockDocument.getDocId();
            this.changedRevIds = Arrays.asList(mockDocument.getDocRev());
        }

        public MockChangedDoc setSeq(int seq) {
            this.seq = seq;
            return this;
        }

        public MockChangedDoc setDocId(String docId) {
            this.docId = docId;
            return this;
        }

        public MockChangedDoc setChangedRevIds(List<String> changedRevIds) {
            this.changedRevIds = changedRevIds;
            return this;
        }

        public int getSeq() {
            return seq;
        }

        public String getDocId() {
            return docId;
        }

        public List<String> getChangedRevIds() {
            return changedRevIds;
        }

        /**
         * Export as a map
         *
         * @return map, eg {"seq":2,"id":"doc2","changes":[{"rev":"1-5e38"}]}
         */
        public Map<String, Object> exportAsMap() {
            Map<String, Object> exported = new HashMap<String, Object>();
            exported.put("seq", getSeq());
            exported.put("id", getDocId());
            List changes = new ArrayList();
            for (String changeRevId : changedRevIds) {
                Map<String, Object> revIdMap = new HashMap<String, Object>();
                revIdMap.put("rev", changeRevId);
                changes.add(revIdMap);
            }
            exported.put("changes", changes);
            return exported;
        }

    }


}




Java Source Code List

com.couchbase.lite.ApiTest.java
com.couchbase.lite.AttachmentsTest.java
com.couchbase.lite.AuthTest.java
com.couchbase.lite.Base64Test.java
com.couchbase.lite.BlobStoreWriterTest.java
com.couchbase.lite.CRUDOperationsTest.java
com.couchbase.lite.CacheTest.java
com.couchbase.lite.ChangesTest.java
com.couchbase.lite.CollationTest.java
com.couchbase.lite.DatabaseTest.java
com.couchbase.lite.DocumentTest.java
com.couchbase.lite.LitePerfTestCase.java
com.couchbase.lite.LiteTestCase.java
com.couchbase.lite.LiteTestContext.java
com.couchbase.lite.LocalDocsTest.java
com.couchbase.lite.ManagerTest.java
com.couchbase.lite.MiscTest.java
com.couchbase.lite.MultipartReaderTest.java
com.couchbase.lite.RevTreeTest.java
com.couchbase.lite.RevisionsTest.java
com.couchbase.lite.RouterTest.java
com.couchbase.lite.SequenceMapTest.java
com.couchbase.lite.ValidationsTest.java
com.couchbase.lite.ViewsTest.java
com.couchbase.lite.android.AndroidContext.java
com.couchbase.lite.android.AndroidLogger.java
com.couchbase.lite.android.AndroidNetworkReachabilityManager.java
com.couchbase.lite.android.AndroidSQLiteStorageEngineFactory.java
com.couchbase.lite.android.AndroidSQLiteStorageEngine.java
com.couchbase.lite.mockserver.MockBulkDocs.java
com.couchbase.lite.mockserver.MockChangesFeedNoResponse.java
com.couchbase.lite.mockserver.MockChangesFeed.java
com.couchbase.lite.mockserver.MockCheckpointGet.java
com.couchbase.lite.mockserver.MockCheckpointPut.java
com.couchbase.lite.mockserver.MockDispatcher.java
com.couchbase.lite.mockserver.MockDocumentBulkGet.java
com.couchbase.lite.mockserver.MockDocumentGet.java
com.couchbase.lite.mockserver.MockDocumentPut.java
com.couchbase.lite.mockserver.MockFacebookAuthPost.java
com.couchbase.lite.mockserver.MockHelper.java
com.couchbase.lite.mockserver.MockPreloadedPullTarget.java
com.couchbase.lite.mockserver.MockRevsDiff.java
com.couchbase.lite.mockserver.MockSessionGet.java
com.couchbase.lite.mockserver.SmartMockResponse.java
com.couchbase.lite.mockserver.WrappedSmartMockResponse.java
com.couchbase.lite.performance2.Test01_CreateDocs.java
com.couchbase.lite.performance2.Test02_CreateDocsUnoptimizedWay.java
com.couchbase.lite.performance2.Test03_CreateDocsWithAttachments.java
com.couchbase.lite.performance2.Test06_PullReplication.java
com.couchbase.lite.performance2.Test07_PushReplication.java
com.couchbase.lite.performance2.Test08_DocRevisions.java
com.couchbase.lite.performance2.Test09_LoadDB.java
com.couchbase.lite.performance2.Test10_DeleteDB.java
com.couchbase.lite.performance2.Test11_DeleteDocs.java
com.couchbase.lite.performance2.Test12_IndexView.java
com.couchbase.lite.performance2.Test13_QueryView.java
com.couchbase.lite.performance2.Test14_ReduceView.java
com.couchbase.lite.performance2.Test28_KeySizes.java
com.couchbase.lite.performance2.Test29_AllDocQuery.java
com.couchbase.lite.performance2.Test30_LiveQuery.java
com.couchbase.lite.performance2.Test31_CompactDB.java
com.couchbase.lite.performance.Test10_DeleteDB.java
com.couchbase.lite.performance.Test11_DeleteDocs.java
com.couchbase.lite.performance.Test12_IndexView.java
com.couchbase.lite.performance.Test13_QueryView.java
com.couchbase.lite.performance.Test14_ReduceView.java
com.couchbase.lite.performance.Test16_ParallelPushReplication.java
com.couchbase.lite.performance.Test1_CreateDocs.java
com.couchbase.lite.performance.Test2_CreateDocsUnoptimizedWay.java
com.couchbase.lite.performance.Test3_CreateDocsWithAttachments.java
com.couchbase.lite.performance.Test6_PushReplication.java
com.couchbase.lite.performance.Test7_PullReplication.java
com.couchbase.lite.performance.Test8_DocRevisions.java
com.couchbase.lite.performance.Test9_LoadDB.java
com.couchbase.lite.replicator.BulkDownloaderTest.java
com.couchbase.lite.replicator.ChangeTrackerTest.java
com.couchbase.lite.replicator.CustomizableMockHttpClient.java
com.couchbase.lite.replicator.ReplicationTest.java
com.couchbase.lite.replicator.ResponderChain.java
com.couchbase.lite.support.BatcherTest.java
com.couchbase.lite.support.JsonDocumentTest.java
com.couchbase.lite.support.PersistentCookieStoreTest.java
com.couchbase.lite.support.RemoteRequestTest.java
com.couchbase.touchdb.RevCollator.java
com.couchbase.touchdb.TDCollateJSON.java