Search in sources :

Example 1 with DeleteDocumentOptions

use of com.ibm.watson.discovery.v2.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 2 with DeleteDocumentOptions

use of com.ibm.watson.discovery.v2.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 3 with DeleteDocumentOptions

use of com.ibm.watson.discovery.v2.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 4 with DeleteDocumentOptions

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

Example 5 with DeleteDocumentOptions

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

the class DiscoveryServiceTest method deleteDocumentIsSuccessful.

/**
 * Delete document is successful.
 *
 * @throws InterruptedException the interrupted exception
 */
@Test
public void deleteDocumentIsSuccessful() throws InterruptedException {
    server.enqueue(jsonResponse(deleteDocResp));
    DeleteDocumentOptions deleteRequest = new DeleteDocumentOptions.Builder(environmentId, collectionId, documentId).build();
    DeleteDocumentResponse response = discoveryService.deleteDocument(deleteRequest).execute().getResult();
    RecordedRequest request = server.takeRequest();
    assertEquals(DOCS2_PATH, request.getPath());
    assertEquals(DELETE, request.getMethod());
    assertEquals(deleteDocResp.getDocumentId(), response.getDocumentId());
    assertEquals(deleteDocResp.getStatus(), response.getStatus());
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) DeleteDocumentResponse(com.ibm.watson.discovery.v1.model.DeleteDocumentResponse) DeleteDocumentOptions(com.ibm.watson.discovery.v1.model.DeleteDocumentOptions) WatsonServiceUnitTest(com.ibm.watson.common.WatsonServiceUnitTest)

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 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 DeleteDocumentResponse (com.ibm.watson.discovery.v2.model.DeleteDocumentResponse)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