Search in sources :

Example 1 with PutLocalDocumentOptions

use of com.ibm.cloud.cloudant.v1.model.PutLocalDocumentOptions in project cloudant-java-sdk by IBM.

the class Cloudant method putLocalDocument.

/**
 * Create or modify a local document.
 *
 * Stores the specified local document. The semantics are identical to storing a standard document in the specified
 * database, except that the document is not replicated.
 *
 * @param putLocalDocumentOptions the {@link PutLocalDocumentOptions} containing the options for the call
 * @return a {@link ServiceCall} with a result of type {@link DocumentResult}
 */
public ServiceCall<DocumentResult> putLocalDocument(PutLocalDocumentOptions putLocalDocumentOptions) {
    com.ibm.cloud.sdk.core.util.Validator.notNull(putLocalDocumentOptions, "putLocalDocumentOptions cannot be null");
    if (putLocalDocumentOptions.document() != null && putLocalDocumentOptions.contentType() == null) {
        putLocalDocumentOptions = putLocalDocumentOptions.newBuilder().contentType("application/json").build();
    }
    Map<String, String> pathParamsMap = new HashMap<String, String>();
    pathParamsMap.put("db", putLocalDocumentOptions.db());
    pathParamsMap.put("doc_id", putLocalDocumentOptions.docId());
    RequestBuilder builder = RequestBuilder.put(RequestBuilder.resolveRequestUrl(getServiceUrl(), "/{db}/_local/{doc_id}", pathParamsMap));
    Map<String, String> sdkHeaders = SdkCommon.getSdkHeaders("cloudant", "v1", "putLocalDocument");
    for (Entry<String, String> header : sdkHeaders.entrySet()) {
        builder.header(header.getKey(), header.getValue());
    }
    builder.header("Accept", "application/json");
    if (putLocalDocumentOptions.contentType() != null) {
        builder.header("Content-Type", putLocalDocumentOptions.contentType());
    }
    if (putLocalDocumentOptions.batch() != null) {
        builder.query("batch", String.valueOf(putLocalDocumentOptions.batch()));
    }
    builder.bodyContent(putLocalDocumentOptions.contentType(), putLocalDocumentOptions.document(), null, putLocalDocumentOptions.body());
    ResponseConverter<DocumentResult> responseConverter = ResponseConverterUtils.getValue(new com.google.gson.reflect.TypeToken<DocumentResult>() {
    }.getType());
    return createServiceCall(builder.build(), responseConverter);
}
Also used : RequestBuilder(com.ibm.cloud.sdk.core.http.RequestBuilder) HashMap(java.util.HashMap) DocumentResult(com.ibm.cloud.cloudant.v1.model.DocumentResult)

Example 2 with PutLocalDocumentOptions

use of com.ibm.cloud.cloudant.v1.model.PutLocalDocumentOptions in project cloudant-java-sdk by IBM.

the class PutLocalDocumentOptionsTest method testPutLocalDocumentOptions.

@Test
public void testPutLocalDocumentOptions() throws Throwable {
    Attachment attachmentModel = new Attachment.Builder().contentType("testString").data(TestUtilities.createMockByteArray("This is a mock byte array value.")).digest("testString").encodedLength(Long.valueOf("0")).encoding("testString").follows(true).length(Long.valueOf("0")).revpos(Long.valueOf("1")).stub(true).build();
    assertEquals(attachmentModel.contentType(), "testString");
    assertEquals(attachmentModel.data(), TestUtilities.createMockByteArray("This is a mock byte array value."));
    assertEquals(attachmentModel.digest(), "testString");
    assertEquals(attachmentModel.encodedLength(), Long.valueOf("0"));
    assertEquals(attachmentModel.encoding(), "testString");
    assertEquals(attachmentModel.follows(), Boolean.valueOf(true));
    assertEquals(attachmentModel.length(), Long.valueOf("0"));
    assertEquals(attachmentModel.revpos(), Long.valueOf("1"));
    assertEquals(attachmentModel.stub(), Boolean.valueOf(true));
    Revisions revisionsModel = new Revisions.Builder().ids(new java.util.ArrayList<String>(java.util.Arrays.asList("testString"))).start(Long.valueOf("1")).build();
    assertEquals(revisionsModel.ids(), new java.util.ArrayList<String>(java.util.Arrays.asList("testString")));
    assertEquals(revisionsModel.start(), Long.valueOf("1"));
    DocumentRevisionStatus documentRevisionStatusModel = new DocumentRevisionStatus.Builder().rev("testString").status("available").build();
    assertEquals(documentRevisionStatusModel.rev(), "testString");
    assertEquals(documentRevisionStatusModel.status(), "available");
    Document documentModel = new Document.Builder().attachments(new java.util.HashMap<String, Attachment>() {

        {
            put("foo", attachmentModel);
        }
    }).conflicts(new java.util.ArrayList<String>(java.util.Arrays.asList("testString"))).deleted(true).deletedConflicts(new java.util.ArrayList<String>(java.util.Arrays.asList("testString"))).id("testString").localSeq("testString").rev("testString").revisions(revisionsModel).revsInfo(new java.util.ArrayList<DocumentRevisionStatus>(java.util.Arrays.asList(documentRevisionStatusModel))).add("foo", "testString").build();
    assertEquals(documentModel.getAttachments(), new java.util.HashMap<String, Attachment>() {

        {
            put("foo", attachmentModel);
        }
    });
    assertEquals(documentModel.getConflicts(), new java.util.ArrayList<String>(java.util.Arrays.asList("testString")));
    assertEquals(documentModel.isDeleted(), Boolean.valueOf(true));
    assertEquals(documentModel.getDeletedConflicts(), new java.util.ArrayList<String>(java.util.Arrays.asList("testString")));
    assertEquals(documentModel.getId(), "testString");
    assertEquals(documentModel.getLocalSeq(), "testString");
    assertEquals(documentModel.getRev(), "testString");
    assertEquals(documentModel.getRevisions(), revisionsModel);
    assertEquals(documentModel.getRevsInfo(), new java.util.ArrayList<DocumentRevisionStatus>(java.util.Arrays.asList(documentRevisionStatusModel)));
    assertEquals(documentModel.get("foo"), "testString");
    PutLocalDocumentOptions putLocalDocumentOptionsModel = new PutLocalDocumentOptions.Builder().db("testString").docId("testString").document(documentModel).body(TestUtilities.createMockStream("This is a mock file.")).contentType("application/json").batch("ok").build();
    assertEquals(putLocalDocumentOptionsModel.db(), "testString");
    assertEquals(putLocalDocumentOptionsModel.docId(), "testString");
    assertEquals(putLocalDocumentOptionsModel.document(), documentModel);
    assertEquals(IOUtils.toString(putLocalDocumentOptionsModel.body()), IOUtils.toString(TestUtilities.createMockStream("This is a mock file.")));
    assertEquals(putLocalDocumentOptionsModel.contentType(), "application/json");
    assertEquals(putLocalDocumentOptionsModel.batch(), "ok");
}
Also used : PutLocalDocumentOptions(com.ibm.cloud.cloudant.v1.model.PutLocalDocumentOptions) ArrayList(java.util.ArrayList) Attachment(com.ibm.cloud.cloudant.v1.model.Attachment) Revisions(com.ibm.cloud.cloudant.v1.model.Revisions) Document(com.ibm.cloud.cloudant.v1.model.Document) DocumentRevisionStatus(com.ibm.cloud.cloudant.v1.model.DocumentRevisionStatus) Test(org.testng.annotations.Test)

Example 3 with PutLocalDocumentOptions

use of com.ibm.cloud.cloudant.v1.model.PutLocalDocumentOptions in project cloudant-java-sdk by IBM.

the class CloudantTest method testPutLocalDocumentWOptions.

@Test
public void testPutLocalDocumentWOptions() throws Throwable {
    // Schedule some responses.
    String mockResponseBody = "{\"id\": \"id\", \"rev\": \"rev\", \"ok\": true, \"caused_by\": \"causedBy\", \"error\": \"error\", \"reason\": \"reason\"}";
    String putLocalDocumentPath = "/testString/_local/testString";
    server.enqueue(new MockResponse().setHeader("Content-type", "application/json").setResponseCode(201).setBody(mockResponseBody));
    constructClientService();
    // Construct an instance of the Attachment model
    Attachment attachmentModel = new Attachment.Builder().contentType("testString").data(TestUtilities.createMockByteArray("This is a mock byte array value.")).digest("testString").encodedLength(Long.valueOf("0")).encoding("testString").follows(true).length(Long.valueOf("0")).revpos(Long.valueOf("1")).stub(true).build();
    // Construct an instance of the Revisions model
    Revisions revisionsModel = new Revisions.Builder().ids(new java.util.ArrayList<String>(java.util.Arrays.asList("testString"))).start(Long.valueOf("1")).build();
    // Construct an instance of the DocumentRevisionStatus model
    DocumentRevisionStatus documentRevisionStatusModel = new DocumentRevisionStatus.Builder().rev("testString").status("available").build();
    // Construct an instance of the Document model
    Document documentModel = new Document.Builder().attachments(new java.util.HashMap<String, Attachment>() {

        {
            put("foo", attachmentModel);
        }
    }).conflicts(new java.util.ArrayList<String>(java.util.Arrays.asList("testString"))).deleted(true).deletedConflicts(new java.util.ArrayList<String>(java.util.Arrays.asList("testString"))).id("exampleid").localSeq("testString").rev("testString").revisions(revisionsModel).revsInfo(new java.util.ArrayList<DocumentRevisionStatus>(java.util.Arrays.asList(documentRevisionStatusModel))).add("brand", "Foo").add("colours", "[\"red\",\"green\",\"black\",\"blue\"]").add("description", "Slim Colourful Design Electronic Cooking Appliance for ...").add("image", "assets/img/0gmsnghhew.jpg").add("keywords", "[\"Foo\",\"Scales\",\"Weight\",\"Digital\",\"Kitchen\"]").add("name", "Digital Kitchen Scales").add("price", "14.99").add("productid", "1000042").add("taxonomy", "[\"Home\",\"Kitchen\",\"Small Appliances\"]").add("type", "product").build();
    // Construct an instance of the PutLocalDocumentOptions model
    PutLocalDocumentOptions putLocalDocumentOptionsModel = new PutLocalDocumentOptions.Builder().db("testString").docId("testString").document(documentModel).contentType("application/json").batch("ok").build();
    // Invoke operation with valid options model (positive test)
    Response<DocumentResult> response = cloudantService.putLocalDocument(putLocalDocumentOptionsModel).execute();
    assertNotNull(response);
    DocumentResult responseObj = response.getResult();
    assertNotNull(responseObj);
    // Verify the contents of the request
    RecordedRequest request = server.takeRequest();
    assertNotNull(request);
    assertEquals(request.getMethod(), "PUT");
    // Check query
    Map<String, String> query = TestUtilities.parseQueryString(request);
    assertNotNull(query);
    // Get query params
    assertEquals(query.get("batch"), "ok");
    // Check request path
    String parsedPath = TestUtilities.parseReqPath(request);
    assertEquals(parsedPath, putLocalDocumentPath);
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) HashMap(java.util.HashMap) PutLocalDocumentOptions(com.ibm.cloud.cloudant.v1.model.PutLocalDocumentOptions) DocumentResult(com.ibm.cloud.cloudant.v1.model.DocumentResult) ArrayList(java.util.ArrayList) Attachment(com.ibm.cloud.cloudant.v1.model.Attachment) Revisions(com.ibm.cloud.cloudant.v1.model.Revisions) ReplicationDocument(com.ibm.cloud.cloudant.v1.model.ReplicationDocument) DesignDocument(com.ibm.cloud.cloudant.v1.model.DesignDocument) Document(com.ibm.cloud.cloudant.v1.model.Document) SchedulerDocument(com.ibm.cloud.cloudant.v1.model.SchedulerDocument) BulkGetResultDocument(com.ibm.cloud.cloudant.v1.model.BulkGetResultDocument) BulkGetQueryDocument(com.ibm.cloud.cloudant.v1.model.BulkGetQueryDocument) DocumentRevisionStatus(com.ibm.cloud.cloudant.v1.model.DocumentRevisionStatus) Test(org.testng.annotations.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Aggregations

Attachment (com.ibm.cloud.cloudant.v1.model.Attachment)2 Document (com.ibm.cloud.cloudant.v1.model.Document)2 DocumentResult (com.ibm.cloud.cloudant.v1.model.DocumentResult)2 DocumentRevisionStatus (com.ibm.cloud.cloudant.v1.model.DocumentRevisionStatus)2 PutLocalDocumentOptions (com.ibm.cloud.cloudant.v1.model.PutLocalDocumentOptions)2 Revisions (com.ibm.cloud.cloudant.v1.model.Revisions)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 Test (org.testng.annotations.Test)2 BulkGetQueryDocument (com.ibm.cloud.cloudant.v1.model.BulkGetQueryDocument)1 BulkGetResultDocument (com.ibm.cloud.cloudant.v1.model.BulkGetResultDocument)1 DesignDocument (com.ibm.cloud.cloudant.v1.model.DesignDocument)1 ReplicationDocument (com.ibm.cloud.cloudant.v1.model.ReplicationDocument)1 SchedulerDocument (com.ibm.cloud.cloudant.v1.model.SchedulerDocument)1 RequestBuilder (com.ibm.cloud.sdk.core.http.RequestBuilder)1 MockResponse (okhttp3.mockwebserver.MockResponse)1 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)1 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)1