Android Open Source - couchbase-lite-android Mock Document Bulk Get






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;
//from   w w  w  .j av  a 2  s . c o m

import com.couchbase.lite.Manager;
import com.couchbase.lite.util.Log;
import com.squareup.okhttp.mockwebserver.MockResponse;
import com.squareup.okhttp.mockwebserver.RecordedRequest;

import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/*

    Generate a _bulk_get response, eg

    Request:
    --------

    {"docs":[
      {"atts_since":null,"id":"48c2868b-ec8b","rev":"1-dd9df9"},
      {"atts_since":null,"id":"53a11c38-d729","rev":"1-dd9df9"},
      ..
    ]}

    Response:
    ---------

    Headers:

    Content Type: multipart/mixed; boundary="77d6fa0c04b501832f5dcf93d515cb73f39fa74104b01bc5a5b24dd57f4a"

    Body:

    --77d6fa0c04b501832f5dcf93d515cb73f39fa74104b01bc5a5b24dd57f4a
    Content-Type: application/json

    {"_deleted":true,"_id":"6f0ac441-9092-45e5-82db-c76be5bd0e48","_rev":"4-4e06228ca6233e67c363a6a2c48e10bd","_revisions":{"ids":["4e06228ca6233e67c363a6a2c48e10bd","3a6249a96cd3fbceaff91d8a3b182a0f","c709c454aaf733264d332d4fd5132af5","c4103f122d27677c9db144cae1394a66"],"start":4}}
    --77d6fa0c04b501832f5dcf93d515cb73f39fa74104b01bc5a5b24dd57f4a
    X-Doc-Id: doc1-1407952481755
    X-Rev-Id: 76-8fbee7ae13afadf9fe81d1d0831930cc
    Content-Type: multipart/related; boundary="aff16d5e5921568ad8fab41b9ec3c2dc86390da4f9dbbc2cec7bf5e2655f"

    --aff16d5e5921568ad8fab41b9ec3c2dc86390da4f9dbbc2cec7bf5e2655f
    Content-Type: application/json

    {"_attachments":{"attachment.png":{"content_type":"image/png","digest":"sha1-LmsoqJJ6LOn4YS60pYnvrKbBd64=","follows":true,"length":519173,"revpos":1}},"_id":"doc1-1407952481755","_rev":"76-8fbee7ae13afadf9fe81d1d0831930cc","_revisions":{"ids":["8fbee7ae13afadf9fe81d1d0831930cc"],"start":76},"checked":false,"created_at":"","list_id":"","ocr_decoded":" \n\n","title":"","type":""}
    --aff16d5e5921568ad8fab41b9ec3c2dc86390da4f9dbbc2cec7bf5e2655f
    Content-Type: image/png
    Content-Disposition: attachment; filename="attachment.png"

    ?PNG
    --aff16d5e5921568ad8fab41b9ec3c2dc86390da4f9dbbc2cec7bf5e2655f--
    --77d6fa0c04b501832f5dcf93d515cb73f39fa74104b01bc5a5b24dd57f4a--
    ...

 */
public class MockDocumentBulkGet implements SmartMockResponse {

    private Map<String, MockDocumentGet.MockDocument> documents;
    private boolean isSticky;
    private long delayMs;

    public MockDocumentBulkGet() {
        this.documents = new HashMap<String, MockDocumentGet.MockDocument>();
    }

    @Override
    public MockResponse generateMockResponse(RecordedRequest request) {

        try {

            Map <String, Object> bulkDocsJson = Manager.getObjectMapper().readValue(request.getUtf8Body(), Map.class);
            List docs = (List) bulkDocsJson.get("docs");

            MockResponse mockResponse = new MockResponse();

            String boundary = "77d6fa0c04b501832f5dcf93d515cb73f39fa74104b01bc5a5b24dd57f4a";
            String contentType = String.format("multipart/mixed; boundary=\"%s\"", boundary);
            mockResponse.setHeader("Content-Type", contentType);

            ByteArrayOutputStream o = new ByteArrayOutputStream();


            /*
                    byte[] mime = new String("--BOUNDARY\r\nFoo: Bar\r\n Header : Val ue \r\n\r\npart the first\r\n--BOUNDARY  \r\n\r\n2nd part\r\n--BOUNDARY--").getBytes(utf8);

             */

            int i = 0;
            for (Object doc : docs) {
                Map<String, Object> documentMap = (Map) doc;
                String docId = (String) documentMap.get("id");
                MockDocumentGet.MockDocument mockDocument = documents.get(docId);

                boolean addEmptyLine = false;
                if (i > 0) {
                    addEmptyLine = true;
                }
                addMockDocument(o, mockDocument, boundary, addEmptyLine);
                i += 1;
            }

            // final boundary to mark end
            append(o, "\r\n").append(o, "--").append(o, boundary).append(o, "--");

            byte[] byteArray = o.toByteArray();

            mockResponse.setBody(byteArray);

            mockResponse.setStatus("HTTP/1.1 200 OK");

            return mockResponse;

        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }


    }

    private void addMockDocument(ByteArrayOutputStream o, MockDocumentGet.MockDocument mockDocument, String boundary, boolean addEmptyLine) throws Exception {

        boolean attachmentFollows = true;

        if (addEmptyLine) {
            append(o, "\r\n");
        }

        append(o, "--").append(o, boundary).append(o, "\r\n");

        if (!mockDocument.hasAttachment()) {
            // no attachment, add json part
            append(o, "Content-Type: application/json");
            append(o, "\r\n\r\n");
            append(o, mockDocument.generateDocumentBody(attachmentFollows));
            append(o, "\r\n");

        } else {
            // has attachment

            // headers
            append(o, "X-Doc-Id: ").append(o, mockDocument.getDocId()).append(o, "\r\n");
            append(o, "X-Rev-Id: ").append(o, mockDocument.getDocRev()).append(o, "\r\n");
            String innerBoundary = "aff16d5e5921568ad8fab41b9ec3c2dc86390da4f9dbbc2cec7bf5e2655f";
            String innerContentType = String.format("multipart/related; boundary=\"%s\"", innerBoundary);
            append(o, "Content-Type: ").append(o, innerContentType);
            append(o, "\r\n\r\n");

            // boundary
            append(o, "--").append(o, innerBoundary).append(o, "\r\n");

            // json part
            append(o, "Content-Type: application/json");
            append(o, "\r\n\r\n");
            append(o, mockDocument.generateDocumentBody(attachmentFollows));
            append(o, "\r\n");

            // boundary
            append(o, "--").append(o, innerBoundary).append(o, "\r\n");

            // image part
            append(o, "Content-Type: image/png");
            append(o, "\r\n");
            String contentDisposition = String.format("Content-Disposition: attachment; filename=\"%s\"", mockDocument.getAttachmentName());
            append(o, contentDisposition);
            append(o, "\r\n\r\n");

            // image data
            byte[] attachmentBytes = MockDocumentGet.getAssetByteArray(mockDocument.getAttachmentName());
            o.write(attachmentBytes);

            // final boundary to mark end of part
            append(o, "\r\n").append(o, "--").append(o, innerBoundary).append(o, "--");

        }



    }

    private MockDocumentBulkGet append(ByteArrayOutputStream baos, String string) throws Exception {
        baos.write(string.getBytes("UTF-8"));
        return this;
    }

    public void addDocument(MockDocumentGet.MockDocument mockDocument) {
        this.documents.put(mockDocument.getDocId(), mockDocument);
    }

    @Override
    public boolean isSticky() {
        return this.isSticky;
    }

    @Override
    public long delayMs() {
        return delayMs;
    }

    public void setSticky(boolean isSticky) {
        this.isSticky = isSticky;
    }

    public long getDelayMs() {
        return delayMs;
    }

    public void setDelayMs(long delayMs) {
        this.delayMs = delayMs;
    }


}




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