Search in sources :

Example 1 with DeleteDocumentOptions

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

the class Cloudant method deleteDocument.

/**
 * Delete a document.
 *
 * Marks the specified document as deleted by adding a `_deleted` field with the value `true`. Documents with this
 * field are not returned within requests anymore but stay in the database. You must supply the current (latest)
 * revision, either by using the `rev` parameter or by using the `If-Match` header to specify the revision.
 *
 * @param deleteDocumentOptions the {@link DeleteDocumentOptions} containing the options for the call
 * @return a {@link ServiceCall} with a result of type {@link DocumentResult}
 */
public ServiceCall<DocumentResult> deleteDocument(DeleteDocumentOptions deleteDocumentOptions) {
    com.ibm.cloud.sdk.core.util.Validator.notNull(deleteDocumentOptions, "deleteDocumentOptions cannot be null");
    Map<String, String> pathParamsMap = new HashMap<String, String>();
    pathParamsMap.put("db", deleteDocumentOptions.db());
    pathParamsMap.put("doc_id", deleteDocumentOptions.docId());
    RequestBuilder builder = RequestBuilder.delete(RequestBuilder.resolveRequestUrl(getServiceUrl(), "/{db}/{doc_id}", pathParamsMap));
    Map<String, String> sdkHeaders = SdkCommon.getSdkHeaders("cloudant", "v1", "deleteDocument");
    for (Entry<String, String> header : sdkHeaders.entrySet()) {
        builder.header(header.getKey(), header.getValue());
    }
    builder.header("Accept", "application/json");
    if (deleteDocumentOptions.ifMatch() != null) {
        builder.header("If-Match", deleteDocumentOptions.ifMatch());
    }
    if (deleteDocumentOptions.batch() != null) {
        builder.query("batch", String.valueOf(deleteDocumentOptions.batch()));
    }
    if (deleteDocumentOptions.rev() != null) {
        builder.query("rev", String.valueOf(deleteDocumentOptions.rev()));
    }
    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 DeleteDocumentOptions

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

the class CloudantTest method testDeleteDocumentWOptions.

@Test
public void testDeleteDocumentWOptions() throws Throwable {
    // Schedule some responses.
    String mockResponseBody = "{\"id\": \"id\", \"rev\": \"rev\", \"ok\": true, \"caused_by\": \"causedBy\", \"error\": \"error\", \"reason\": \"reason\"}";
    String deleteDocumentPath = "/testString/testString";
    server.enqueue(new MockResponse().setHeader("Content-type", "application/json").setResponseCode(200).setBody(mockResponseBody));
    constructClientService();
    // Construct an instance of the DeleteDocumentOptions model
    DeleteDocumentOptions deleteDocumentOptionsModel = new DeleteDocumentOptions.Builder().db("testString").docId("testString").ifMatch("testString").batch("ok").rev("testString").build();
    // Invoke operation with valid options model (positive test)
    Response<DocumentResult> response = cloudantService.deleteDocument(deleteDocumentOptionsModel).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("batch"), "ok");
    assertEquals(query.get("rev"), "testString");
    // Check request path
    String parsedPath = TestUtilities.parseReqPath(request);
    assertEquals(parsedPath, deleteDocumentPath);
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) DocumentResult(com.ibm.cloud.cloudant.v1.model.DocumentResult) DeleteDocumentOptions(com.ibm.cloud.cloudant.v1.model.DeleteDocumentOptions) Test(org.testng.annotations.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 3 with DeleteDocumentOptions

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

the class DeleteDocumentOptionsTest method testDeleteDocumentOptions.

@Test
public void testDeleteDocumentOptions() throws Throwable {
    DeleteDocumentOptions deleteDocumentOptionsModel = new DeleteDocumentOptions.Builder().db("testString").docId("testString").ifMatch("testString").batch("ok").rev("testString").build();
    assertEquals(deleteDocumentOptionsModel.db(), "testString");
    assertEquals(deleteDocumentOptionsModel.docId(), "testString");
    assertEquals(deleteDocumentOptionsModel.ifMatch(), "testString");
    assertEquals(deleteDocumentOptionsModel.batch(), "ok");
    assertEquals(deleteDocumentOptionsModel.rev(), "testString");
}
Also used : DeleteDocumentOptions(com.ibm.cloud.cloudant.v1.model.DeleteDocumentOptions) Test(org.testng.annotations.Test)

Example 4 with DeleteDocumentOptions

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

the class DiscoveryServiceTest method deleteDocumentIsSuccessful.

@Test
public void deleteDocumentIsSuccessful() throws InterruptedException {
    server.enqueue(jsonResponse(deleteDocResp));
    DeleteDocumentOptions deleteRequest = new DeleteDocumentOptions.Builder(environmentId, collectionId, documentId).build();
    discoveryService.deleteDocument(deleteRequest).execute();
    RecordedRequest request = server.takeRequest();
    assertEquals(DOCS2_PATH, request.getPath());
    assertEquals(DELETE, request.getMethod());
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) DeleteDocumentOptions(com.ibm.watson.developer_cloud.discovery.v1.model.DeleteDocumentOptions) WatsonServiceUnitTest(com.ibm.watson.developer_cloud.WatsonServiceUnitTest) Test(org.junit.Test)

Example 5 with DeleteDocumentOptions

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

the class DiscoveryV2Example method main.

public static void main(String[] args) throws IOException {
    Authenticator authenticator = new BearerTokenAuthenticator("{bearer_token}");
    Discovery service = new Discovery("2019-11-22", authenticator);
    service.setServiceUrl("{url}");
    // This example assumes you have a project and collection set up which can accept documents.
    // Paste those
    // IDs below.
    String projectId = "";
    String collectionId = "";
    // Add a new document to our collection. Fill in the file path with the file you want to send.
    InputStream file = new FileInputStream("");
    AddDocumentOptions addDocumentOptions = new AddDocumentOptions.Builder().projectId(projectId).collectionId(collectionId).file(file).filename("example-file").build();
    DocumentAccepted addResponse = service.addDocument(addDocumentOptions).execute().getResult();
    String documentId = addResponse.getDocumentId();
    // Query your collection with the new document inside.
    QueryOptions queryOptions = new QueryOptions.Builder().projectId(projectId).addCollectionIds(collectionId).naturalLanguageQuery(// Feel free to replace this to query something different.
    "Watson").build();
    QueryResponse queryResponse = service.query(queryOptions).execute().getResult();
    System.out.println(queryResponse.getMatchingResults() + " results were returned by the query!");
    // See if the added document got returned by the query.
    for (QueryResult result : queryResponse.getResults()) {
        if (result.getDocumentId().equals(documentId)) {
            System.out.println("Our new document matched the query!");
        }
    }
    // Delete our uploaded document from the collection.
    DeleteDocumentOptions deleteDocumentOptions = new DeleteDocumentOptions.Builder().projectId(projectId).collectionId(collectionId).documentId(documentId).build();
    service.deleteDocument(deleteDocumentOptions).execute();
}
Also used : DocumentAccepted(com.ibm.watson.discovery.v2.model.DocumentAccepted) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) QueryOptions(com.ibm.watson.discovery.v2.model.QueryOptions) FileInputStream(java.io.FileInputStream) QueryResult(com.ibm.watson.discovery.v2.model.QueryResult) AddDocumentOptions(com.ibm.watson.discovery.v2.model.AddDocumentOptions) QueryResponse(com.ibm.watson.discovery.v2.model.QueryResponse) BearerTokenAuthenticator(com.ibm.cloud.sdk.core.security.BearerTokenAuthenticator) Authenticator(com.ibm.cloud.sdk.core.security.Authenticator) BearerTokenAuthenticator(com.ibm.cloud.sdk.core.security.BearerTokenAuthenticator) DeleteDocumentOptions(com.ibm.watson.discovery.v2.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