Search in sources :

Example 31 with Ok

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

the class PostDocumentOptionsTest method testPostDocumentOptions.

@Test
public void testPostDocumentOptions() 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");
    PostDocumentOptions postDocumentOptionsModel = new PostDocumentOptions.Builder().db("testString").document(documentModel).body(TestUtilities.createMockStream("This is a mock file.")).contentType("application/json").batch("ok").build();
    assertEquals(postDocumentOptionsModel.db(), "testString");
    assertEquals(postDocumentOptionsModel.document(), documentModel);
    assertEquals(IOUtils.toString(postDocumentOptionsModel.body()), IOUtils.toString(TestUtilities.createMockStream("This is a mock file.")));
    assertEquals(postDocumentOptionsModel.contentType(), "application/json");
    assertEquals(postDocumentOptionsModel.batch(), "ok");
}
Also used : 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) PostDocumentOptions(com.ibm.cloud.cloudant.v1.model.PostDocumentOptions) Test(org.testng.annotations.Test)

Example 32 with Ok

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

the class PostPartitionSearchOptionsTest method testPostPartitionSearchOptions.

@Test
public void testPostPartitionSearchOptions() throws Throwable {
    PostPartitionSearchOptions postPartitionSearchOptionsModel = new PostPartitionSearchOptions.Builder().db("testString").partitionKey("testString").ddoc("testString").index("testString").query("testString").bookmark("testString").highlightFields(new java.util.ArrayList<String>(java.util.Arrays.asList("testString"))).highlightNumber(Long.valueOf("1")).highlightPostTag("</em>").highlightPreTag("<em>").highlightSize(Long.valueOf("1")).includeDocs(false).includeFields(new java.util.ArrayList<String>(java.util.Arrays.asList("testString"))).limit(Long.valueOf("0")).sort(new java.util.ArrayList<String>(java.util.Arrays.asList("testString"))).stale("ok").build();
    assertEquals(postPartitionSearchOptionsModel.db(), "testString");
    assertEquals(postPartitionSearchOptionsModel.partitionKey(), "testString");
    assertEquals(postPartitionSearchOptionsModel.ddoc(), "testString");
    assertEquals(postPartitionSearchOptionsModel.index(), "testString");
    assertEquals(postPartitionSearchOptionsModel.query(), "testString");
    assertEquals(postPartitionSearchOptionsModel.bookmark(), "testString");
    assertEquals(postPartitionSearchOptionsModel.highlightFields(), new java.util.ArrayList<String>(java.util.Arrays.asList("testString")));
    assertEquals(postPartitionSearchOptionsModel.highlightNumber(), Long.valueOf("1"));
    assertEquals(postPartitionSearchOptionsModel.highlightPostTag(), "</em>");
    assertEquals(postPartitionSearchOptionsModel.highlightPreTag(), "<em>");
    assertEquals(postPartitionSearchOptionsModel.highlightSize(), Long.valueOf("1"));
    assertEquals(postPartitionSearchOptionsModel.includeDocs(), Boolean.valueOf(false));
    assertEquals(postPartitionSearchOptionsModel.includeFields(), new java.util.ArrayList<String>(java.util.Arrays.asList("testString")));
    assertEquals(postPartitionSearchOptionsModel.limit(), Long.valueOf("0"));
    assertEquals(postPartitionSearchOptionsModel.sort(), new java.util.ArrayList<String>(java.util.Arrays.asList("testString")));
    assertEquals(postPartitionSearchOptionsModel.stale(), "ok");
}
Also used : PostPartitionSearchOptions(com.ibm.cloud.cloudant.v1.model.PostPartitionSearchOptions) Test(org.testng.annotations.Test)

Example 33 with Ok

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

the class GetGeoOptionsTest method testGetGeoOptions.

@Test
public void testGetGeoOptions() throws Throwable {
    GetGeoOptions getGeoOptionsModel = new GetGeoOptions.Builder().db("testString").ddoc("testString").index("testString").bbox("testString").bookmark("testString").format("view").g("testString").includeDocs(false).lat(Double.valueOf("-90")).limit(Long.valueOf("0")).lon(Double.valueOf("-180")).nearest(false).radius(Double.valueOf("0")).rangex(Double.valueOf("0")).rangey(Double.valueOf("0")).relation("intersects").skip(Long.valueOf("0")).stale("ok").build();
    assertEquals(getGeoOptionsModel.db(), "testString");
    assertEquals(getGeoOptionsModel.ddoc(), "testString");
    assertEquals(getGeoOptionsModel.index(), "testString");
    assertEquals(getGeoOptionsModel.bbox(), "testString");
    assertEquals(getGeoOptionsModel.bookmark(), "testString");
    assertEquals(getGeoOptionsModel.format(), "view");
    assertEquals(getGeoOptionsModel.g(), "testString");
    assertEquals(getGeoOptionsModel.includeDocs(), Boolean.valueOf(false));
    assertEquals(getGeoOptionsModel.lat(), Double.valueOf("-90"));
    assertEquals(getGeoOptionsModel.limit(), Long.valueOf("0"));
    assertEquals(getGeoOptionsModel.lon(), Double.valueOf("-180"));
    assertEquals(getGeoOptionsModel.nearest(), Boolean.valueOf(false));
    assertEquals(getGeoOptionsModel.radius(), Double.valueOf("0"));
    assertEquals(getGeoOptionsModel.rangex(), Double.valueOf("0"));
    assertEquals(getGeoOptionsModel.rangey(), Double.valueOf("0"));
    assertEquals(getGeoOptionsModel.relation(), "intersects");
    assertEquals(getGeoOptionsModel.skip(), Long.valueOf("0"));
    assertEquals(getGeoOptionsModel.stale(), "ok");
}
Also used : GetGeoOptions(com.ibm.cloud.cloudant.v1.model.GetGeoOptions) Test(org.testng.annotations.Test)

Example 34 with Ok

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

the class CreateDbAndDoc method main.

public static void main(String[] args) {
    // 1. Create a client with `CLOUDANT` default service name ============
    Cloudant client = Cloudant.newInstance();
    // 2. Create a database ===============================================
    // Create a database object with "orders" id
    String exampleDbName = "orders";
    PutDatabaseOptions putDbOptions = new PutDatabaseOptions.Builder().db(exampleDbName).build();
    // Try to create database if it doesn't exist
    try {
        Ok putDatabaseResult = client.putDatabase(putDbOptions).execute().getResult();
        if (putDatabaseResult.isOk()) {
            System.out.println("\"" + exampleDbName + "\" database created.");
        }
    } catch (ServiceResponseException sre) {
        if (sre.getStatusCode() == 412)
            System.out.println("Cannot create \"" + exampleDbName + "\" database, it already exists.");
    }
    // 3. Create a document ===============================================
    // Create a document object with "example" id
    String exampleDocId = "example";
    Document exampleDocument = new Document();
    // Setting id for the document is optional when "postDocument" method is used for CREATE.
    // When id is not provided the server will generate one for your document.
    exampleDocument.setId(exampleDocId);
    // Add "name" and "joined" fields to the document
    exampleDocument.put("name", "Bob Smith");
    exampleDocument.put("joined", "2019-01-24T10:42:59.000Z");
    // Save the document in the database with "postDocument" method
    PostDocumentOptions createDocumentOptions = new PostDocumentOptions.Builder().db(exampleDbName).document(exampleDocument).build();
    DocumentResult createDocumentResponse = client.postDocument(createDocumentOptions).execute().getResult();
    // ====================================================================
    // Note: saving the document can also be done with the "putDocument"
    // method. In this case `docId` is required for a CREATE operation:
    /*
        PutDocumentOptions createDocumentOptions =
            new PutDocumentOptions.Builder()
                .db(exampleDbName)
                .docId(exampleDocId)
                .document(exampleDocument)
                .build();
        DocumentResult createDocumentResponse = client
            .putDocument(createDocumentOptions)
            .execute()
            .getResult();
        */
    // ====================================================================
    // Keeping track of the revision number of the document object
    // is necessary for further UPDATE/DELETE operations:
    exampleDocument.setRev(createDocumentResponse.getRev());
    System.out.println("You have created the document:\n" + exampleDocument);
}
Also used : PutDatabaseOptions(com.ibm.cloud.cloudant.v1.model.PutDatabaseOptions) DocumentResult(com.ibm.cloud.cloudant.v1.model.DocumentResult) ServiceResponseException(com.ibm.cloud.sdk.core.service.exception.ServiceResponseException) Cloudant(com.ibm.cloud.cloudant.v1.Cloudant) Ok(com.ibm.cloud.cloudant.v1.model.Ok) Document(com.ibm.cloud.cloudant.v1.model.Document) PostDocumentOptions(com.ibm.cloud.cloudant.v1.model.PostDocumentOptions)

Example 35 with Ok

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

the class CloudantTest method testDeleteAttachmentWOptions.

@Test
public void testDeleteAttachmentWOptions() throws Throwable {
    // Schedule some responses.
    String mockResponseBody = "{\"id\": \"id\", \"rev\": \"rev\", \"ok\": true, \"caused_by\": \"causedBy\", \"error\": \"error\", \"reason\": \"reason\"}";
    String deleteAttachmentPath = "/testString/testString/testString";
    server.enqueue(new MockResponse().setHeader("Content-type", "application/json").setResponseCode(201).setBody(mockResponseBody));
    constructClientService();
    // Construct an instance of the DeleteAttachmentOptions model
    DeleteAttachmentOptions deleteAttachmentOptionsModel = new DeleteAttachmentOptions.Builder().db("testString").docId("testString").attachmentName("testString").ifMatch("testString").rev("testString").batch("ok").build();
    // Invoke operation with valid options model (positive test)
    Response<DocumentResult> response = cloudantService.deleteAttachment(deleteAttachmentOptionsModel).execute();
    assertNotNull(response);
    DocumentResult responseObj = response.getResult();
    assertNotNull(responseObj);
    // Verify the contents of the request
    RecordedRequest request = server.takeRequest();
    assertNotNull(request);
    assertEquals(request.getMethod(), "DELETE");
    // Check query
    Map<String, String> query = TestUtilities.parseQueryString(request);
    assertNotNull(query);
    // Get query params
    assertEquals(query.get("rev"), "testString");
    assertEquals(query.get("batch"), "ok");
    // Check request path
    String parsedPath = TestUtilities.parseReqPath(request);
    assertEquals(parsedPath, deleteAttachmentPath);
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) DeleteAttachmentOptions(com.ibm.cloud.cloudant.v1.model.DeleteAttachmentOptions) DocumentResult(com.ibm.cloud.cloudant.v1.model.DocumentResult) Test(org.testng.annotations.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Aggregations

Test (org.testng.annotations.Test)43 MockResponse (okhttp3.mockwebserver.MockResponse)29 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)29 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)29 ArrayList (java.util.ArrayList)19 Ok (com.ibm.cloud.cloudant.v1.model.Ok)18 DocumentResult (com.ibm.cloud.cloudant.v1.model.DocumentResult)13 HashMap (java.util.HashMap)13 Attachment (com.ibm.cloud.cloudant.v1.model.Attachment)11 DocumentRevisionStatus (com.ibm.cloud.cloudant.v1.model.DocumentRevisionStatus)11 Revisions (com.ibm.cloud.cloudant.v1.model.Revisions)11 Document (com.ibm.cloud.cloudant.v1.model.Document)8 RequestBuilder (com.ibm.cloud.sdk.core.http.RequestBuilder)8 DesignDocument (com.ibm.cloud.cloudant.v1.model.DesignDocument)6 ReplicationDocument (com.ibm.cloud.cloudant.v1.model.ReplicationDocument)6 BulkGetQueryDocument (com.ibm.cloud.cloudant.v1.model.BulkGetQueryDocument)5 JsonObject (com.google.gson.JsonObject)4 BulkGetResultDocument (com.ibm.cloud.cloudant.v1.model.BulkGetResultDocument)4 SchedulerDocument (com.ibm.cloud.cloudant.v1.model.SchedulerDocument)4 List (java.util.List)4