Search in sources :

Example 11 with DocumentAccepted

use of com.ibm.watson.developer_cloud.discovery.v1.model.DocumentAccepted 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 12 with DocumentAccepted

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

the class DiscoveryServiceTest method addDocumentFromInputStreamWithMediaTypeIsSuccessful.

@Test
public void addDocumentFromInputStreamWithMediaTypeIsSuccessful() throws InterruptedException {
    server.enqueue(jsonResponse(createDocResp));
    String myDocumentJson = "{\"field\":\"value\"}";
    JsonObject myMetadata = new JsonObject();
    myMetadata.add("foo", new JsonPrimitive("bar"));
    InputStream documentStream = new ByteArrayInputStream(myDocumentJson.getBytes());
    AddDocumentOptions.Builder builder = new AddDocumentOptions.Builder(environmentId, collectionId);
    builder.file(documentStream).fileContentType(HttpMediaType.APPLICATION_JSON);
    builder.filename("test_file");
    builder.metadata(myMetadata.toString());
    DocumentAccepted response = discoveryService.addDocument(builder.build()).execute();
    RecordedRequest request = server.takeRequest();
    assertEquals(DOCS1_PATH, request.getPath());
    assertEquals(POST, request.getMethod());
    assertEquals(createDocResp, response);
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) DocumentAccepted(com.ibm.watson.developer_cloud.discovery.v1.model.DocumentAccepted) JsonPrimitive(com.google.gson.JsonPrimitive) ByteArrayInputStream(java.io.ByteArrayInputStream) AddDocumentOptions(com.ibm.watson.developer_cloud.discovery.v1.model.AddDocumentOptions) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) JsonObject(com.google.gson.JsonObject) WatsonServiceUnitTest(com.ibm.watson.developer_cloud.WatsonServiceUnitTest) Test(org.junit.Test)

Example 13 with DocumentAccepted

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

the class DiscoveryServiceTest method updateDocumentIsSuccessful.

// Deleted tests for (create)addDocument with file parameter as this is deprecated
@Test
public void updateDocumentIsSuccessful() throws InterruptedException {
    server.enqueue(jsonResponse(updateDocResp));
    UpdateDocumentOptions.Builder updateBuilder = new UpdateDocumentOptions.Builder(environmentId, collectionId, documentId);
    String myDocumentJson = "{\"field\":\"value2\"}";
    JsonObject myMetadata = new JsonObject();
    myMetadata.add("foo", new JsonPrimitive("bar"));
    InputStream documentStream = new ByteArrayInputStream(myDocumentJson.getBytes());
    updateBuilder.file(documentStream).fileContentType(HttpMediaType.APPLICATION_JSON);
    updateBuilder.filename("test_file");
    updateBuilder.metadata(myMetadata.toString());
    DocumentAccepted response = discoveryService.updateDocument(updateBuilder.build()).execute();
    RecordedRequest request = server.takeRequest();
    assertEquals(DOCS2_PATH, request.getPath());
    assertEquals(POST, request.getMethod());
    assertEquals(updateDocResp, response);
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) UpdateDocumentOptions(com.ibm.watson.developer_cloud.discovery.v1.model.UpdateDocumentOptions) DocumentAccepted(com.ibm.watson.developer_cloud.discovery.v1.model.DocumentAccepted) JsonPrimitive(com.google.gson.JsonPrimitive) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) JsonObject(com.google.gson.JsonObject) WatsonServiceUnitTest(com.ibm.watson.developer_cloud.WatsonServiceUnitTest) Test(org.junit.Test)

Example 14 with DocumentAccepted

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

the class DiscoveryServiceTest method addDocumentIsSuccessful.

// Document tests
@Test
public void addDocumentIsSuccessful() throws InterruptedException {
    server.enqueue(jsonResponse(createDocResp));
    String myDocumentJson = "{\"field\":\"value\"}";
    JsonObject myMetadata = new JsonObject();
    myMetadata.add("foo", new JsonPrimitive("bar"));
    InputStream documentStream = new ByteArrayInputStream(myDocumentJson.getBytes());
    AddDocumentOptions.Builder builder = new AddDocumentOptions.Builder(environmentId, collectionId);
    builder.file(documentStream).fileContentType(HttpMediaType.APPLICATION_JSON);
    builder.filename("test_file");
    builder.metadata(myMetadata.toString());
    DocumentAccepted response = discoveryService.addDocument(builder.build()).execute();
    RecordedRequest request = server.takeRequest();
    assertEquals(DOCS1_PATH, request.getPath());
    assertEquals(POST, request.getMethod());
    assertEquals(createDocResp, response);
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) DocumentAccepted(com.ibm.watson.developer_cloud.discovery.v1.model.DocumentAccepted) JsonPrimitive(com.google.gson.JsonPrimitive) ByteArrayInputStream(java.io.ByteArrayInputStream) AddDocumentOptions(com.ibm.watson.developer_cloud.discovery.v1.model.AddDocumentOptions) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) JsonObject(com.google.gson.JsonObject) WatsonServiceUnitTest(com.ibm.watson.developer_cloud.WatsonServiceUnitTest) Test(org.junit.Test)

Example 15 with DocumentAccepted

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

the class DiscoveryQueryExample method main.

public static void main(String[] args) {
    Discovery discovery = new Discovery("2017-11-07");
    discovery.setEndPoint("https://gateway.watsonplatform.net/discovery/api");
    discovery.setUsernameAndPassword("<username>", "<password");
    String environmentId = null;
    String configurationId = null;
    String collectionId = null;
    String documentId = null;
    // See if an environment already exists
    System.out.println("Check if environment exists");
    ListEnvironmentsOptions listOptions = new ListEnvironmentsOptions.Builder().build();
    ListEnvironmentsResponse listResponse = discovery.listEnvironments(listOptions).execute();
    for (Environment environment : listResponse.getEnvironments()) {
        // look for an existing environment that isn't read only
        if (!environment.isReadOnly()) {
            environmentId = environment.getEnvironmentId();
            System.out.println("Found existing environment ID: " + environmentId);
            break;
        }
    }
    if (environmentId == null) {
        System.out.println("No environment found, creating new one...");
        // no environment found, create a new one (assuming we are a FREE plan)
        String environmentName = "watson_developer_cloud_test_environment";
        CreateEnvironmentOptions createOptions = new CreateEnvironmentOptions.Builder().name(environmentName).size(0L).build();
        Environment createResponse = discovery.createEnvironment(createOptions).execute();
        environmentId = createResponse.getEnvironmentId();
        System.out.println("Created new environment ID: " + environmentId);
        // wait for environment to be ready
        System.out.println("Waiting for environment to be ready...");
        boolean environmentReady = false;
        while (!environmentReady) {
            GetEnvironmentOptions getEnvironmentOptions = new GetEnvironmentOptions.Builder(environmentId).build();
            Environment getEnvironmentResponse = discovery.getEnvironment(getEnvironmentOptions).execute();
            environmentReady = getEnvironmentResponse.getStatus().equals(Environment.Status.ACTIVE);
            try {
                if (!environmentReady) {
                    Thread.sleep(500);
                }
            } catch (InterruptedException e) {
                throw new RuntimeException("Interrupted", e);
            }
        }
        System.out.println("Environment Ready!");
    }
    // find the default configuration
    System.out.println("Finding the default configuration");
    ListConfigurationsOptions listConfigsOptions = new ListConfigurationsOptions.Builder(environmentId).build();
    ListConfigurationsResponse listConfigsResponse = discovery.listConfigurations(listConfigsOptions).execute();
    for (Configuration configuration : listConfigsResponse.getConfigurations()) {
        if (configuration.getName().equals(DEFAULT_CONFIG_NAME)) {
            configurationId = configuration.getConfigurationId();
            System.out.println("Found default configuration ID: " + configurationId);
            break;
        }
    }
    // create a new collection
    System.out.println("Creating a new collection...");
    String collectionName = "my_watson_developer_cloud_collection";
    CreateCollectionOptions createCollectionOptions = new CreateCollectionOptions.Builder(environmentId, collectionName).configurationId(configurationId).build();
    Collection collection = discovery.createCollection(createCollectionOptions).execute();
    collectionId = collection.getCollectionId();
    System.out.println("Created a collection ID: " + collectionId);
    // wait for the collection to be "available"
    System.out.println("Waiting for collection to be ready...");
    boolean collectionReady = false;
    while (!collectionReady) {
        GetCollectionOptions getCollectionOptions = new GetCollectionOptions.Builder(environmentId, collectionId).build();
        Collection getCollectionResponse = discovery.getCollection(getCollectionOptions).execute();
        collectionReady = getCollectionResponse.getStatus().equals(Collection.Status.ACTIVE);
        try {
            if (!collectionReady) {
                Thread.sleep(500);
            }
        } catch (InterruptedException e) {
            throw new RuntimeException("Interrupted", e);
        }
    }
    System.out.println("Collection Ready!");
    // add a document
    System.out.println("Creating a new document...");
    String documentJson = "{\"field\":\"value\"}";
    InputStream documentStream = new ByteArrayInputStream(documentJson.getBytes());
    AddDocumentOptions.Builder createDocumentBuilder = new AddDocumentOptions.Builder(environmentId, collectionId);
    createDocumentBuilder.file(documentStream).fileMediaType(HttpMediaType.APPLICATION_JSON);
    DocumentAccepted createDocumentResponse = discovery.addDocument(createDocumentBuilder.build()).execute();
    documentId = createDocumentResponse.getDocumentId();
    System.out.println("Created a document ID: " + documentId);
    // wait for document to be ready
    System.out.println("Waiting for document to be ready...");
    boolean documentReady = false;
    while (!documentReady) {
        GetDocumentStatusOptions getDocumentStatusOptions = new GetDocumentStatusOptions.Builder(environmentId, collectionId, documentId).build();
        DocumentStatus getDocumentResponse = discovery.getDocument(getDocumentStatusOptions).execute();
        documentReady = !getDocumentResponse.getStatus().equals(DocumentStatus.Status.PROCESSING);
        try {
            if (!documentReady) {
                Thread.sleep(500);
            }
        } catch (InterruptedException e) {
            throw new RuntimeException("Interrupted");
        }
    }
    System.out.println("Document Ready!");
    // query document
    System.out.println("Querying the collection...");
    QueryOptions.Builder queryBuilder = new QueryOptions.Builder(environmentId, collectionId);
    queryBuilder.query("field:value");
    QueryResponse queryResponse = discovery.query(queryBuilder.build()).execute();
    // print out the results
    System.out.println("Query Results:");
    System.out.println(queryResponse);
    // cleanup the collection created
    System.out.println("Deleting the collection...");
    DeleteCollectionOptions deleteOptions = new DeleteCollectionOptions.Builder(environmentId, collectionId).build();
    discovery.deleteCollection(deleteOptions).execute();
    System.out.println("Collection deleted!");
    System.out.println("Discovery example finished");
}
Also used : Configuration(com.ibm.watson.developer_cloud.discovery.v1.model.configuration.Configuration) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) QueryResponse(com.ibm.watson.developer_cloud.discovery.v1.model.query.QueryResponse) Environment(com.ibm.watson.developer_cloud.discovery.v1.model.environment.Environment)

Aggregations

DocumentAccepted (com.ibm.watson.developer_cloud.discovery.v1.model.DocumentAccepted)20 Test (org.junit.Test)16 ByteArrayInputStream (java.io.ByteArrayInputStream)14 InputStream (java.io.InputStream)14 AddDocumentOptions (com.ibm.watson.developer_cloud.discovery.v1.model.AddDocumentOptions)11 Collection (com.ibm.watson.developer_cloud.discovery.v1.model.Collection)10 WatsonServiceTest (com.ibm.watson.developer_cloud.WatsonServiceTest)9 JsonObject (com.google.gson.JsonObject)8 JsonPrimitive (com.google.gson.JsonPrimitive)8 FileInputStream (java.io.FileInputStream)8 WatsonServiceUnitTest (com.ibm.watson.developer_cloud.WatsonServiceUnitTest)7 UpdateDocumentOptions (com.ibm.watson.developer_cloud.discovery.v1.model.UpdateDocumentOptions)5 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)5 DocumentStatus (com.ibm.watson.developer_cloud.discovery.v1.model.DocumentStatus)4 GetDocumentStatusOptions (com.ibm.watson.developer_cloud.discovery.v1.model.GetDocumentStatusOptions)4 WaitFor (com.ibm.watson.developer_cloud.util.WaitFor)4 QueryOptions (com.ibm.watson.developer_cloud.discovery.v1.model.QueryOptions)3 QueryResponse (com.ibm.watson.developer_cloud.discovery.v1.model.QueryResponse)3 Ignore (org.junit.Ignore)3 Configuration (com.ibm.watson.developer_cloud.discovery.v1.model.Configuration)2