Search in sources :

Example 6 with DeleteDocumentOptions

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

the class Discovery method deleteDocument.

/**
 * Delete a document.
 *
 * <p>If the given document ID is invalid, or if the document is not found, then the a success
 * response is returned (HTTP status code `200`) with the status set to 'deleted'.
 *
 * @param deleteDocumentOptions the {@link DeleteDocumentOptions} containing the options for the
 *     call
 * @return a {@link ServiceCall} with a result of type {@link DeleteDocumentResponse}
 */
public ServiceCall<DeleteDocumentResponse> 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("environment_id", deleteDocumentOptions.environmentId());
    pathParamsMap.put("collection_id", deleteDocumentOptions.collectionId());
    pathParamsMap.put("document_id", deleteDocumentOptions.documentId());
    RequestBuilder builder = RequestBuilder.delete(RequestBuilder.resolveRequestUrl(getServiceUrl(), "/v1/environments/{environment_id}/collections/{collection_id}/documents/{document_id}", pathParamsMap));
    Map<String, String> sdkHeaders = SdkCommon.getSdkHeaders("discovery", "v1", "deleteDocument");
    for (Entry<String, String> header : sdkHeaders.entrySet()) {
        builder.header(header.getKey(), header.getValue());
    }
    builder.header("Accept", "application/json");
    builder.query("version", String.valueOf(this.version));
    ResponseConverter<DeleteDocumentResponse> responseConverter = ResponseConverterUtils.getValue(new com.google.gson.reflect.TypeToken<DeleteDocumentResponse>() {
    }.getType());
    return createServiceCall(builder.build(), responseConverter);
}
Also used : DeleteDocumentResponse(com.ibm.watson.discovery.v1.model.DeleteDocumentResponse) RequestBuilder(com.ibm.cloud.sdk.core.http.RequestBuilder) HashMap(java.util.HashMap)

Example 7 with DeleteDocumentOptions

use of com.ibm.watson.discovery.v1.model.DeleteDocumentOptions in project knative-eventing-java-app by IBM.

the class CloudEventStoreCloudant method removeAllEvents.

@Override
public void removeAllEvents() throws Exception {
    try {
        PostAllDocsOptions docsOptions = new PostAllDocsOptions.Builder().db(this.dbName).includeDocs(true).build();
        AllDocsResult allDocResults = this.client.postAllDocs(docsOptions).execute().getResult();
        for (DocsResultRow docResult : allDocResults.getRows()) {
            Document document = docResult.getDoc();
            DeleteDocumentOptions deleteDocumentOptions = new DeleteDocumentOptions.Builder().db(this.dbName).docId(document.getId()).rev(document.getRev()).build();
            DocumentResult deleteDocumentResponse = client.deleteDocument(deleteDocumentOptions).execute().getResult();
            if (!deleteDocumentResponse.isOk()) {
                logger.info("Could not delete a document.");
            }
        }
    } catch (NotFoundException e) {
        String errMsg = "Unable to retrieve all documents from Cloudant";
        logger.error(errMsg, e);
        throw new Exception(errMsg, e);
    }
}
Also used : DocsResultRow(com.ibm.cloud.cloudant.v1.model.DocsResultRow) DocumentResult(com.ibm.cloud.cloudant.v1.model.DocumentResult) GsonBuilder(com.google.gson.GsonBuilder) NotFoundException(com.ibm.cloud.sdk.core.service.exception.NotFoundException) AllDocsResult(com.ibm.cloud.cloudant.v1.model.AllDocsResult) PostAllDocsOptions(com.ibm.cloud.cloudant.v1.model.PostAllDocsOptions) Document(com.ibm.cloud.cloudant.v1.model.Document) DeleteDocumentOptions(com.ibm.cloud.cloudant.v1.model.DeleteDocumentOptions) NotFoundException(com.ibm.cloud.sdk.core.service.exception.NotFoundException)

Example 8 with DeleteDocumentOptions

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

the class DiscoveryServiceIT method deleteDocumentIsSuccessful.

@Test
public void deleteDocumentIsSuccessful() {
    Collection collection = createTestCollection();
    String collectionId = collection.getCollectionId();
    DocumentAccepted documentAccepted = createTestDocument("test_document", collectionId);
    DeleteDocumentOptions deleteOptions = new DeleteDocumentOptions.Builder(environmentId, collectionId, documentAccepted.getDocumentId()).build();
    discovery.deleteDocument(deleteOptions).execute();
}
Also used : DocumentAccepted(com.ibm.watson.developer_cloud.discovery.v1.model.DocumentAccepted) Collection(com.ibm.watson.developer_cloud.discovery.v1.model.Collection) DeleteDocumentOptions(com.ibm.watson.developer_cloud.discovery.v1.model.DeleteDocumentOptions) Test(org.junit.Test) WatsonServiceTest(com.ibm.watson.developer_cloud.WatsonServiceTest)

Example 9 with DeleteDocumentOptions

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

the class DiscoveryTest method testDeleteDocumentWOptions.

@Test
public void testDeleteDocumentWOptions() throws Throwable {
    // Schedule some responses.
    String mockResponseBody = "{\"document_id\": \"documentId\", \"status\": \"deleted\"}";
    String deleteDocumentPath = "/v2/projects/testString/collections/testString/documents/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().projectId("testString").collectionId("testString").documentId("testString").xWatsonDiscoveryForce(false).build();
    // Invoke operation with valid options model (positive test)
    Response<DeleteDocumentResponse> response = discoveryService.deleteDocument(deleteDocumentOptionsModel).execute();
    assertNotNull(response);
    DeleteDocumentResponse 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("version"), "testString");
    // Check request path
    String parsedPath = TestUtilities.parseReqPath(request);
    assertEquals(parsedPath, deleteDocumentPath);
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) DeleteDocumentResponse(com.ibm.watson.discovery.v2.model.DeleteDocumentResponse) DeleteDocumentOptions(com.ibm.watson.discovery.v2.model.DeleteDocumentOptions) Test(org.testng.annotations.Test)

Example 10 with DeleteDocumentOptions

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

the class LanguageTranslatorTest method testDeleteDocumentWOptions.

// Test the deleteDocument operation with a valid options model parameter
@Test
public void testDeleteDocumentWOptions() throws Throwable {
    // Register a mock response
    String mockResponseBody = "";
    String deleteDocumentPath = "/v3/documents/testString";
    server.enqueue(new MockResponse().setResponseCode(204).setBody(mockResponseBody));
    // Construct an instance of the DeleteDocumentOptions model
    DeleteDocumentOptions deleteDocumentOptionsModel = new DeleteDocumentOptions.Builder().documentId("testString").build();
    // Invoke deleteDocument() with a valid options model and verify the result
    Response<Void> response = languageTranslatorService.deleteDocument(deleteDocumentOptionsModel).execute();
    assertNotNull(response);
    Void responseObj = response.getResult();
    assertNull(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"), "2018-05-01");
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) DeleteDocumentOptions(com.ibm.watson.language_translator.v3.model.DeleteDocumentOptions) Test(org.testng.annotations.Test)

Aggregations

RecordedRequest (okhttp3.mockwebserver.RecordedRequest)6 Test (org.testng.annotations.Test)5 DeleteDocumentOptions (com.ibm.cloud.cloudant.v1.model.DeleteDocumentOptions)4 MockResponse (okhttp3.mockwebserver.MockResponse)4 DocumentResult (com.ibm.cloud.cloudant.v1.model.DocumentResult)3 DeleteDocumentResponse (com.ibm.watson.discovery.v1.model.DeleteDocumentResponse)3 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 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