Search in sources :

Example 1 with AddDocumentOptions

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

the class DiscoveryServiceTest method addDocumentWithoutRequiredParametersFails.

@Test(expected = IllegalArgumentException.class)
public void addDocumentWithoutRequiredParametersFails() {
    AddDocumentOptions options = new AddDocumentOptions.Builder(environmentId, collectionId).build();
    DocumentAccepted response = discoveryService.addDocument(options).execute();
}
Also used : DocumentAccepted(com.ibm.watson.developer_cloud.discovery.v1.model.DocumentAccepted) AddDocumentOptions(com.ibm.watson.developer_cloud.discovery.v1.model.AddDocumentOptions) WatsonServiceUnitTest(com.ibm.watson.developer_cloud.WatsonServiceUnitTest) Test(org.junit.Test)

Example 2 with AddDocumentOptions

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

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

the class DiscoveryServiceTest method addDocumentWithoutRequiredParametersFails.

/**
 * Adds the document without required parameters fails.
 */
@Test(expected = IllegalArgumentException.class)
public void addDocumentWithoutRequiredParametersFails() {
    AddDocumentOptions options = new AddDocumentOptions.Builder(environmentId, collectionId).build();
    discoveryService.addDocument(options).execute().getResult();
}
Also used : AddDocumentOptions(com.ibm.watson.discovery.v1.model.AddDocumentOptions) WatsonServiceUnitTest(com.ibm.watson.common.WatsonServiceUnitTest)

Example 4 with AddDocumentOptions

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

the class DiscoveryTest method testAddDocumentWOptions.

// Test the addDocument operation with a valid options model parameter
@Test
public void testAddDocumentWOptions() throws Throwable {
    // Register a mock response
    String mockResponseBody = "{\"document_id\": \"documentId\", \"status\": \"processing\", \"notices\": [{\"notice_id\": \"noticeId\", \"created\": \"2019-01-01T12:00:00.000Z\", \"document_id\": \"documentId\", \"query_id\": \"queryId\", \"severity\": \"warning\", \"step\": \"step\", \"description\": \"description\"}]}";
    String addDocumentPath = "/v1/environments/testString/collections/testString/documents";
    server.enqueue(new MockResponse().setHeader("Content-type", "application/json").setResponseCode(202).setBody(mockResponseBody));
    // Construct an instance of the AddDocumentOptions model
    AddDocumentOptions addDocumentOptionsModel = new AddDocumentOptions.Builder().environmentId("testString").collectionId("testString").file(TestUtilities.createMockStream("This is a mock file.")).filename("testString").fileContentType("application/json").metadata("testString").build();
    // Invoke addDocument() with a valid options model and verify the result
    Response<DocumentAccepted> response = discoveryService.addDocument(addDocumentOptionsModel).execute();
    assertNotNull(response);
    DocumentAccepted 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(), "POST");
    // Verify request path
    String parsedPath = TestUtilities.parseReqPath(request);
    assertEquals(parsedPath, addDocumentPath);
    // 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) DocumentAccepted(com.ibm.watson.discovery.v1.model.DocumentAccepted) AddDocumentOptions(com.ibm.watson.discovery.v1.model.AddDocumentOptions) Test(org.testng.annotations.Test)

Example 5 with AddDocumentOptions

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

the class DiscoveryTest method testAddDocumentWOptions.

@Test
public void testAddDocumentWOptions() throws Throwable {
    // Schedule some responses.
    String mockResponseBody = "{\"document_id\": \"documentId\", \"status\": \"processing\"}";
    String addDocumentPath = "/v2/projects/testString/collections/testString/documents";
    server.enqueue(new MockResponse().setHeader("Content-type", "application/json").setResponseCode(202).setBody(mockResponseBody));
    constructClientService();
    // Construct an instance of the AddDocumentOptions model
    AddDocumentOptions addDocumentOptionsModel = new AddDocumentOptions.Builder().projectId("testString").collectionId("testString").file(TestUtilities.createMockStream("This is a mock file.")).filename("testString").fileContentType("application/json").metadata("testString").xWatsonDiscoveryForce(false).build();
    // Invoke operation with valid options model (positive test)
    Response<DocumentAccepted> response = discoveryService.addDocument(addDocumentOptionsModel).execute();
    assertNotNull(response);
    DocumentAccepted responseObj = response.getResult();
    assertNotNull(responseObj);
    // Verify the contents of the request
    RecordedRequest request = server.takeRequest();
    assertNotNull(request);
    assertEquals(request.getMethod(), "POST");
    // 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, addDocumentPath);
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) DocumentAccepted(com.ibm.watson.discovery.v2.model.DocumentAccepted) AddDocumentOptions(com.ibm.watson.discovery.v2.model.AddDocumentOptions) Test(org.testng.annotations.Test)

Aggregations

DocumentAccepted (com.ibm.watson.discovery.v2.model.DocumentAccepted)4 AddDocumentOptions (com.ibm.watson.discovery.v2.model.AddDocumentOptions)3 AddDocumentOptions (com.ibm.watson.discovery.v1.model.AddDocumentOptions)2 MockResponse (okhttp3.mockwebserver.MockResponse)2 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)2 Test (org.testng.annotations.Test)2 JsonObject (com.google.gson.JsonObject)1 RequestBuilder (com.ibm.cloud.sdk.core.http.RequestBuilder)1 Authenticator (com.ibm.cloud.sdk.core.security.Authenticator)1 BearerTokenAuthenticator (com.ibm.cloud.sdk.core.security.BearerTokenAuthenticator)1 IamAuthenticator (com.ibm.cloud.sdk.core.security.IamAuthenticator)1 WatsonServiceUnitTest (com.ibm.watson.common.WatsonServiceUnitTest)1 WatsonServiceUnitTest (com.ibm.watson.developer_cloud.WatsonServiceUnitTest)1 AddDocumentOptions (com.ibm.watson.developer_cloud.discovery.v1.model.AddDocumentOptions)1 DocumentAccepted (com.ibm.watson.developer_cloud.discovery.v1.model.DocumentAccepted)1 DocumentAccepted (com.ibm.watson.discovery.v1.model.DocumentAccepted)1 Discovery (com.ibm.watson.discovery.v2.Discovery)1 DeleteDocumentOptions (com.ibm.watson.discovery.v2.model.DeleteDocumentOptions)1 QueryOptions (com.ibm.watson.discovery.v2.model.QueryOptions)1 QueryResponse (com.ibm.watson.discovery.v2.model.QueryResponse)1