use of com.ibm.watson.discovery.v1.model.ListConfigurationsOptions in project java-sdk by watson-developer-cloud.
the class DiscoveryServiceIT method getConfigurationsIsSuccessful.
@Test
public void getConfigurationsIsSuccessful() {
ListConfigurationsOptions getOptions = new ListConfigurationsOptions.Builder(environmentId).build();
ListConfigurationsResponse getResponse = discovery.listConfigurations(getOptions).execute();
assertFalse(getResponse.getConfigurations().isEmpty());
}
use of com.ibm.watson.discovery.v1.model.ListConfigurationsOptions in project java-sdk by watson-developer-cloud.
the class DiscoveryServiceTest method getConfigurationsIsSuccessful.
@Test
public void getConfigurationsIsSuccessful() throws InterruptedException {
server.enqueue(jsonResponse(getConfsResp));
ListConfigurationsOptions getRequest = new ListConfigurationsOptions.Builder(environmentId).build();
ListConfigurationsResponse response = discoveryService.listConfigurations(getRequest).execute();
RecordedRequest request = server.takeRequest();
assertEquals(CONF1_PATH, request.getPath());
assertEquals(GET, request.getMethod());
assertEquals(getConfsResp, response);
}
use of com.ibm.watson.discovery.v1.model.ListConfigurationsOptions in project java-sdk by watson-developer-cloud.
the class DiscoveryTest method testListConfigurationsWOptions.
// Test the listConfigurations operation with a valid options model parameter
@Test
public void testListConfigurationsWOptions() throws Throwable {
// Register a mock response
String mockResponseBody = "{\"configurations\": [{\"configuration_id\": \"configurationId\", \"name\": \"name\", \"created\": \"2019-01-01T12:00:00.000Z\", \"updated\": \"2019-01-01T12:00:00.000Z\", \"description\": \"description\", \"conversions\": {\"pdf\": {\"heading\": {\"fonts\": [{\"level\": 5, \"min_size\": 7, \"max_size\": 7, \"bold\": true, \"italic\": true, \"name\": \"name\"}]}}, \"word\": {\"heading\": {\"fonts\": [{\"level\": 5, \"min_size\": 7, \"max_size\": 7, \"bold\": true, \"italic\": true, \"name\": \"name\"}], \"styles\": [{\"level\": 5, \"names\": [\"names\"]}]}}, \"html\": {\"exclude_tags_completely\": [\"excludeTagsCompletely\"], \"exclude_tags_keep_content\": [\"excludeTagsKeepContent\"], \"keep_content\": {\"xpaths\": [\"xpaths\"]}, \"exclude_content\": {\"xpaths\": [\"xpaths\"]}, \"keep_tag_attributes\": [\"keepTagAttributes\"], \"exclude_tag_attributes\": [\"excludeTagAttributes\"]}, \"segment\": {\"enabled\": false, \"selector_tags\": [\"selectorTags\"], \"annotated_fields\": [\"annotatedFields\"]}, \"json_normalizations\": [{\"operation\": \"copy\", \"source_field\": \"sourceField\", \"destination_field\": \"destinationField\"}], \"image_text_recognition\": true}, \"enrichments\": [{\"description\": \"description\", \"destination_field\": \"destinationField\", \"source_field\": \"sourceField\", \"overwrite\": false, \"enrichment\": \"enrichment\", \"ignore_downstream_errors\": false, \"options\": {\"features\": {\"keywords\": {\"sentiment\": false, \"emotion\": false, \"limit\": 5}, \"entities\": {\"sentiment\": false, \"emotion\": false, \"limit\": 5, \"mentions\": true, \"mention_types\": true, \"sentence_locations\": false, \"model\": \"model\"}, \"sentiment\": {\"document\": true, \"targets\": [\"target\"]}, \"emotion\": {\"document\": true, \"targets\": [\"target\"]}, \"categories\": {\"mapKey\": \"anyValue\"}, \"semantic_roles\": {\"entities\": true, \"keywords\": true, \"limit\": 5}, \"relations\": {\"model\": \"model\"}, \"concepts\": {\"limit\": 5}}, \"language\": \"ar\", \"model\": \"model\"}}], \"normalizations\": [{\"operation\": \"copy\", \"source_field\": \"sourceField\", \"destination_field\": \"destinationField\"}], \"source\": {\"type\": \"box\", \"credential_id\": \"credentialId\", \"schedule\": {\"enabled\": true, \"time_zone\": \"America/New_York\", \"frequency\": \"daily\"}, \"options\": {\"folders\": [{\"owner_user_id\": \"ownerUserId\", \"folder_id\": \"folderId\", \"limit\": 5}], \"objects\": [{\"name\": \"name\", \"limit\": 5}], \"site_collections\": [{\"site_collection_path\": \"siteCollectionPath\", \"limit\": 5}], \"urls\": [{\"url\": \"url\", \"limit_to_starting_hosts\": true, \"crawl_speed\": \"normal\", \"allow_untrusted_certificate\": false, \"maximum_hops\": 11, \"request_timeout\": 14, \"override_robots_txt\": false, \"blacklist\": [\"blacklist\"]}], \"buckets\": [{\"name\": \"name\", \"limit\": 5}], \"crawl_all_buckets\": false}}}]}";
String listConfigurationsPath = "/v1/environments/testString/configurations";
server.enqueue(new MockResponse().setHeader("Content-type", "application/json").setResponseCode(200).setBody(mockResponseBody));
// Construct an instance of the ListConfigurationsOptions model
ListConfigurationsOptions listConfigurationsOptionsModel = new ListConfigurationsOptions.Builder().environmentId("testString").name("testString").build();
// Invoke listConfigurations() with a valid options model and verify the result
Response<ListConfigurationsResponse> response = discoveryService.listConfigurations(listConfigurationsOptionsModel).execute();
assertNotNull(response);
ListConfigurationsResponse 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(), "GET");
// Verify request path
String parsedPath = TestUtilities.parseReqPath(request);
assertEquals(parsedPath, listConfigurationsPath);
// Verify query params
Map<String, String> query = TestUtilities.parseQueryString(request);
assertNotNull(query);
assertEquals(query.get("version"), "testString");
assertEquals(query.get("name"), "testString");
}
use of com.ibm.watson.discovery.v1.model.ListConfigurationsOptions in project java-sdk by watson-developer-cloud.
the class Discovery method listConfigurations.
/**
* List configurations.
*
* <p>Lists existing configurations for the service instance.
*
* @param listConfigurationsOptions the {@link ListConfigurationsOptions} containing the options
* for the call
* @return a {@link ServiceCall} with a result of type {@link ListConfigurationsResponse}
*/
public ServiceCall<ListConfigurationsResponse> listConfigurations(ListConfigurationsOptions listConfigurationsOptions) {
com.ibm.cloud.sdk.core.util.Validator.notNull(listConfigurationsOptions, "listConfigurationsOptions cannot be null");
Map<String, String> pathParamsMap = new HashMap<String, String>();
pathParamsMap.put("environment_id", listConfigurationsOptions.environmentId());
RequestBuilder builder = RequestBuilder.get(RequestBuilder.resolveRequestUrl(getServiceUrl(), "/v1/environments/{environment_id}/configurations", pathParamsMap));
Map<String, String> sdkHeaders = SdkCommon.getSdkHeaders("discovery", "v1", "listConfigurations");
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));
if (listConfigurationsOptions.name() != null) {
builder.query("name", String.valueOf(listConfigurationsOptions.name()));
}
ResponseConverter<ListConfigurationsResponse> responseConverter = ResponseConverterUtils.getValue(new com.google.gson.reflect.TypeToken<ListConfigurationsResponse>() {
}.getType());
return createServiceCall(builder.build(), responseConverter);
}
use of com.ibm.watson.discovery.v1.model.ListConfigurationsOptions in project java-sdk by watson-developer-cloud.
the class DiscoveryServiceIT method exampleIsSuccessful.
@Test
public void exampleIsSuccessful() {
// Discovery discovery = new Discovery("2016-12-15");
// 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" + UUID.randomUUID();
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).fileContentType(HttpMediaType.APPLICATION_JSON);
createDocumentBuilder.filename("test_file");
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.getDocumentStatus(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");
}
Aggregations