Search in sources :

Example 11 with DeleteDocumentOptions

use of com.ibm.cloud.cloudant.v1.model.DeleteDocumentOptions in project java-sdk by watson-developer-cloud.

the class DiscoveryTest method testDeleteDocumentWOptions.

// Test the deleteDocument operation with a valid options model parameter
@Test
public void testDeleteDocumentWOptions() throws Throwable {
    // Register a mock response
    String mockResponseBody = "{\"document_id\": \"documentId\", \"status\": \"deleted\"}";
    String deleteDocumentPath = "/v1/environments/testString/collections/testString/documents/testString";
    server.enqueue(new MockResponse().setHeader("Content-type", "application/json").setResponseCode(200).setBody(mockResponseBody));
    // Construct an instance of the DeleteDocumentOptions model
    DeleteDocumentOptions deleteDocumentOptionsModel = new DeleteDocumentOptions.Builder().environmentId("testString").collectionId("testString").documentId("testString").build();
    // Invoke deleteDocument() with a valid options model and verify the result
    Response<DeleteDocumentResponse> response = discoveryService.deleteDocument(deleteDocumentOptionsModel).execute();
    assertNotNull(response);
    DeleteDocumentResponse responseObj = response.getResult();
    assertNotNull(responseObj);
    // Verify the contents of the request sent to the mock server
    RecordedRequest request = server.takeRequest();
    assertNotNull(request);
    assertEquals(request.getMethod(), "DELETE");
    // Verify request path
    String parsedPath = TestUtilities.parseReqPath(request);
    assertEquals(parsedPath, deleteDocumentPath);
    // Verify query params
    Map<String, String> query = TestUtilities.parseQueryString(request);
    assertNotNull(query);
    assertEquals(query.get("version"), "testString");
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) DeleteDocumentResponse(com.ibm.watson.discovery.v1.model.DeleteDocumentResponse) DeleteDocumentOptions(com.ibm.watson.discovery.v1.model.DeleteDocumentOptions) Test(org.testng.annotations.Test)

Example 12 with DeleteDocumentOptions

use of com.ibm.cloud.cloudant.v1.model.DeleteDocumentOptions 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)

Aggregations

RecordedRequest (okhttp3.mockwebserver.RecordedRequest)6 Test (org.testng.annotations.Test)5 DeleteDocumentOptions (com.ibm.cloud.cloudant.v1.model.DeleteDocumentOptions)4 DocumentResult (com.ibm.cloud.cloudant.v1.model.DocumentResult)4 MockResponse (okhttp3.mockwebserver.MockResponse)4 Document (com.ibm.cloud.cloudant.v1.model.Document)2 NotFoundException (com.ibm.cloud.sdk.core.service.exception.NotFoundException)2 DeleteDocumentOptions (com.ibm.watson.developer_cloud.discovery.v1.model.DeleteDocumentOptions)2 DeleteDocumentOptions (com.ibm.watson.discovery.v1.model.DeleteDocumentOptions)2 DeleteDocumentResponse (com.ibm.watson.discovery.v1.model.DeleteDocumentResponse)2 DeleteDocumentOptions (com.ibm.watson.discovery.v2.model.DeleteDocumentOptions)2 Test (org.junit.Test)2 GsonBuilder (com.google.gson.GsonBuilder)1 Cloudant (com.ibm.cloud.cloudant.v1.Cloudant)1 AllDocsResult (com.ibm.cloud.cloudant.v1.model.AllDocsResult)1 DocsResultRow (com.ibm.cloud.cloudant.v1.model.DocsResultRow)1 GetDocumentOptions (com.ibm.cloud.cloudant.v1.model.GetDocumentOptions)1 PostAllDocsOptions (com.ibm.cloud.cloudant.v1.model.PostAllDocsOptions)1 RequestBuilder (com.ibm.cloud.sdk.core.http.RequestBuilder)1 Authenticator (com.ibm.cloud.sdk.core.security.Authenticator)1