Search in sources :

Example 6 with GetDocumentOptions

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

the class GetDocumentOptionsTest method testGetDocumentOptions.

@Test
public void testGetDocumentOptions() throws Throwable {
    GetDocumentOptions getDocumentOptionsModel = new GetDocumentOptions.Builder().db("testString").docId("testString").ifNoneMatch("testString").attachments(false).attEncodingInfo(false).conflicts(false).deletedConflicts(false).latest(false).localSeq(false).meta(false).rev("testString").revs(false).revsInfo(false).build();
    assertEquals(getDocumentOptionsModel.db(), "testString");
    assertEquals(getDocumentOptionsModel.docId(), "testString");
    assertEquals(getDocumentOptionsModel.ifNoneMatch(), "testString");
    assertEquals(getDocumentOptionsModel.attachments(), Boolean.valueOf(false));
    assertEquals(getDocumentOptionsModel.attEncodingInfo(), Boolean.valueOf(false));
    assertEquals(getDocumentOptionsModel.conflicts(), Boolean.valueOf(false));
    assertEquals(getDocumentOptionsModel.deletedConflicts(), Boolean.valueOf(false));
    assertEquals(getDocumentOptionsModel.latest(), Boolean.valueOf(false));
    assertEquals(getDocumentOptionsModel.localSeq(), Boolean.valueOf(false));
    assertEquals(getDocumentOptionsModel.meta(), Boolean.valueOf(false));
    assertEquals(getDocumentOptionsModel.rev(), "testString");
    assertEquals(getDocumentOptionsModel.revs(), Boolean.valueOf(false));
    assertEquals(getDocumentOptionsModel.revsInfo(), Boolean.valueOf(false));
}
Also used : GetDocumentOptions(com.ibm.cloud.cloudant.v1.model.GetDocumentOptions) Test(org.testng.annotations.Test)

Example 7 with GetDocumentOptions

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

the class DeleteDoc method main.

public static void main(String[] args) {
    // 1. Create a client with `CLOUDANT` default service name ============
    Cloudant client = Cloudant.newInstance();
    // 2. Delete the document =============================================
    // Set the options to get the document out of the database if it exists
    String exampleDbName = "orders";
    String exampleDocId = "example";
    GetDocumentOptions documentInfoOptions = new GetDocumentOptions.Builder().db(exampleDbName).docId(exampleDocId).build();
    try {
        // Try to get the document if it previously existed in the database
        Document document = client.getDocument(documentInfoOptions).execute().getResult();
        // Delete the document from the database
        DeleteDocumentOptions deleteDocumentOptions = new DeleteDocumentOptions.Builder().db(exampleDbName).docId(// docId is required for DELETE
        exampleDocId).rev(// rev is required for DELETE
        document.getRev()).build();
        DocumentResult deleteDocumentResponse = client.deleteDocument(deleteDocumentOptions).execute().getResult();
        if (deleteDocumentResponse.isOk()) {
            System.out.println("You have deleted the document.");
        }
    } catch (NotFoundException nfe) {
        System.out.println("Cannot delete document because " + "either \"orders\" database or the \"example\" " + "document was not found.");
    }
}
Also used : DocumentResult(com.ibm.cloud.cloudant.v1.model.DocumentResult) GetDocumentOptions(com.ibm.cloud.cloudant.v1.model.GetDocumentOptions) NotFoundException(com.ibm.cloud.sdk.core.service.exception.NotFoundException) Cloudant(com.ibm.cloud.cloudant.v1.Cloudant) Document(com.ibm.cloud.cloudant.v1.model.Document) DeleteDocumentOptions(com.ibm.cloud.cloudant.v1.model.DeleteDocumentOptions)

Example 8 with GetDocumentOptions

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

the class CloudantTest method testGetDocumentWOptions.

@Test
public void testGetDocumentWOptions() throws Throwable {
    // Schedule some responses.
    String mockResponseBody = "{\"_attachments\": {\"mapKey\": {\"content_type\": \"contentType\", \"data\": \"VGhpcyBpcyBhbiBlbmNvZGVkIGJ5dGUgYXJyYXku\", \"digest\": \"digest\", \"encoded_length\": 0, \"encoding\": \"encoding\", \"follows\": false, \"length\": 0, \"revpos\": 1, \"stub\": true}}, \"_conflicts\": [\"conflicts\"], \"_deleted\": false, \"_deleted_conflicts\": [\"deletedConflicts\"], \"_id\": \"id\", \"_local_seq\": \"localSeq\", \"_rev\": \"rev\", \"_revisions\": {\"ids\": [\"ids\"], \"start\": 1}, \"_revs_info\": [{\"rev\": \"rev\", \"status\": \"available\"}]}";
    String getDocumentPath = "/testString/testString";
    server.enqueue(new MockResponse().setHeader("Content-type", "application/json").setResponseCode(200).setBody(mockResponseBody));
    constructClientService();
    // Construct an instance of the GetDocumentOptions model
    GetDocumentOptions getDocumentOptionsModel = new GetDocumentOptions.Builder().db("testString").docId("testString").ifNoneMatch("testString").attachments(false).attEncodingInfo(false).conflicts(false).deletedConflicts(false).latest(false).localSeq(false).meta(false).rev("testString").revs(false).revsInfo(false).build();
    // Invoke operation with valid options model (positive test)
    Response<Document> response = cloudantService.getDocument(getDocumentOptionsModel).execute();
    assertNotNull(response);
    Document responseObj = response.getResult();
    assertNotNull(responseObj);
    // Verify the contents of the request
    RecordedRequest request = server.takeRequest();
    assertNotNull(request);
    assertEquals(request.getMethod(), "GET");
    // Check query
    Map<String, String> query = TestUtilities.parseQueryString(request);
    assertNotNull(query);
    // Get query params
    assertEquals(Boolean.valueOf(query.get("attachments")), Boolean.valueOf(false));
    assertEquals(Boolean.valueOf(query.get("att_encoding_info")), Boolean.valueOf(false));
    assertEquals(Boolean.valueOf(query.get("conflicts")), Boolean.valueOf(false));
    assertEquals(Boolean.valueOf(query.get("deleted_conflicts")), Boolean.valueOf(false));
    assertEquals(Boolean.valueOf(query.get("latest")), Boolean.valueOf(false));
    assertEquals(Boolean.valueOf(query.get("local_seq")), Boolean.valueOf(false));
    assertEquals(Boolean.valueOf(query.get("meta")), Boolean.valueOf(false));
    assertEquals(query.get("rev"), "testString");
    assertEquals(Boolean.valueOf(query.get("revs")), Boolean.valueOf(false));
    assertEquals(Boolean.valueOf(query.get("revs_info")), Boolean.valueOf(false));
    // Check request path
    String parsedPath = TestUtilities.parseReqPath(request);
    assertEquals(parsedPath, getDocumentPath);
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) GetDocumentOptions(com.ibm.cloud.cloudant.v1.model.GetDocumentOptions) 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) Test(org.testng.annotations.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 9 with GetDocumentOptions

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

the class CloudantTest method testGetDocumentAsMixedWOptions.

@Test
public void testGetDocumentAsMixedWOptions() throws Throwable {
    // Schedule some responses.
    String mockResponseBody = "This is a mock binary response.";
    String getDocumentAsMixedPath = "/testString/testString";
    server.enqueue(new MockResponse().setHeader("Content-type", "multipart/mixed").setResponseCode(200).setBody(mockResponseBody));
    constructClientService();
    // Construct an instance of the GetDocumentOptions model
    GetDocumentOptions getDocumentOptionsModel = new GetDocumentOptions.Builder().db("testString").docId("testString").ifNoneMatch("testString").attachments(false).attEncodingInfo(false).conflicts(false).deletedConflicts(false).latest(false).localSeq(false).meta(false).rev("testString").revs(false).revsInfo(false).build();
    // Invoke operation with valid options model (positive test)
    Response<InputStream> response = cloudantService.getDocumentAsMixed(getDocumentOptionsModel).execute();
    assertNotNull(response);
    InputStream responseObj = response.getResult();
    assertNotNull(responseObj);
    // Verify the contents of the request
    RecordedRequest request = server.takeRequest();
    assertNotNull(request);
    assertEquals(request.getMethod(), "GET");
    // Check query
    Map<String, String> query = TestUtilities.parseQueryString(request);
    assertNotNull(query);
    // Get query params
    assertEquals(Boolean.valueOf(query.get("attachments")), Boolean.valueOf(false));
    assertEquals(Boolean.valueOf(query.get("att_encoding_info")), Boolean.valueOf(false));
    assertEquals(Boolean.valueOf(query.get("conflicts")), Boolean.valueOf(false));
    assertEquals(Boolean.valueOf(query.get("deleted_conflicts")), Boolean.valueOf(false));
    assertEquals(Boolean.valueOf(query.get("latest")), Boolean.valueOf(false));
    assertEquals(Boolean.valueOf(query.get("local_seq")), Boolean.valueOf(false));
    assertEquals(Boolean.valueOf(query.get("meta")), Boolean.valueOf(false));
    assertEquals(query.get("rev"), "testString");
    assertEquals(Boolean.valueOf(query.get("revs")), Boolean.valueOf(false));
    assertEquals(Boolean.valueOf(query.get("revs_info")), Boolean.valueOf(false));
    // Check request path
    String parsedPath = TestUtilities.parseReqPath(request);
    assertEquals(parsedPath, getDocumentAsMixedPath);
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) InputStream(java.io.InputStream) GetDocumentOptions(com.ibm.cloud.cloudant.v1.model.GetDocumentOptions) Test(org.testng.annotations.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Aggregations

GetDocumentOptions (com.ibm.cloud.cloudant.v1.model.GetDocumentOptions)8 Document (com.ibm.cloud.cloudant.v1.model.Document)5 Test (org.testng.annotations.Test)5 MockResponse (okhttp3.mockwebserver.MockResponse)4 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)4 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)4 Cloudant (com.ibm.cloud.cloudant.v1.Cloudant)3 InputStream (java.io.InputStream)3 DesignDocument (com.ibm.cloud.cloudant.v1.model.DesignDocument)2 DocumentResult (com.ibm.cloud.cloudant.v1.model.DocumentResult)2 ReplicationDocument (com.ibm.cloud.cloudant.v1.model.ReplicationDocument)2 SchedulerDocument (com.ibm.cloud.cloudant.v1.model.SchedulerDocument)2 NotFoundException (com.ibm.cloud.sdk.core.service.exception.NotFoundException)2 BulkGetQueryDocument (com.ibm.cloud.cloudant.v1.model.BulkGetQueryDocument)1 BulkGetResultDocument (com.ibm.cloud.cloudant.v1.model.BulkGetResultDocument)1 DatabaseInformation (com.ibm.cloud.cloudant.v1.model.DatabaseInformation)1 DeleteDocumentOptions (com.ibm.cloud.cloudant.v1.model.DeleteDocumentOptions)1 GetDatabaseInformationOptions (com.ibm.cloud.cloudant.v1.model.GetDatabaseInformationOptions)1 PostDocumentOptions (com.ibm.cloud.cloudant.v1.model.PostDocumentOptions)1 ServerInformation (com.ibm.cloud.cloudant.v1.model.ServerInformation)1