use of com.ibm.watson.discovery.v2.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");
}
use of com.ibm.watson.discovery.v2.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.");
}
}
Aggregations