use of com.ibm.watson.developer_cloud.assistant.v1.model.Value in project java-sdk by watson-developer-cloud.
the class ValuesIT method testCreateValue.
/**
* Test createValue.
*/
@Test
public void testCreateValue() {
String entity = "beverage";
String entityValue = "coffee" + UUID.randomUUID().toString();
// metadata
Map<String, Object> valueMetadata = new HashMap<String, Object>();
String metadataValue = "value for " + entityValue;
valueMetadata.put("key", metadataValue);
try {
CreateEntityOptions createOptions = new CreateEntityOptions.Builder(workspaceId, entity).build();
service.createEntity(createOptions).execute();
} catch (Exception ex) {
// Exception is okay if is for Unique Violation
assertTrue(ex.getLocalizedMessage().startsWith("Unique Violation"));
}
CreateValueOptions createOptions = new CreateValueOptions.Builder().workspaceId(workspaceId).entity(entity).value(entityValue).metadata(valueMetadata).build();
Value response = service.createValue(createOptions).execute();
try {
assertNotNull(response);
assertNotNull(response.getValueText());
assertEquals(response.getValueText(), entityValue);
assertNotNull(response.getMetadata());
// metadata
assertNotNull(response.getMetadata());
assertNotNull(response.getMetadata().get("key"));
assertEquals(response.getMetadata().get("key"), metadataValue);
} catch (Exception ex) {
fail(ex.getMessage());
} finally {
// Clean up
DeleteValueOptions deleteOptions = new DeleteValueOptions.Builder(workspaceId, entity, entityValue).build();
service.deleteValue(deleteOptions).execute();
}
}
use of com.ibm.watson.developer_cloud.assistant.v1.model.Value in project java-sdk by watson-developer-cloud.
the class ValuesIT method testDeleteValue.
/**
* Test deleteValue.
*/
@Test
public void testDeleteValue() {
String entity = "beverage";
String entityValue = "coffee" + UUID.randomUUID().toString();
try {
CreateEntityOptions createOptions = new CreateEntityOptions.Builder(workspaceId, entity).build();
service.createEntity(createOptions).execute();
} catch (Exception ex) {
// Exception is okay if is for Unique Violation
assertTrue(ex.getLocalizedMessage().startsWith("Unique Violation"));
}
CreateValueOptions createOptions = new CreateValueOptions.Builder(workspaceId, entity, entityValue).build();
Value response = service.createValue(createOptions).execute();
try {
assertNotNull(response);
assertNotNull(response.getValueText());
assertEquals(response.getValueText(), entityValue);
assertNull(response.getMetadata());
} catch (Exception ex) {
// Clean up
DeleteValueOptions deleteOptions = new DeleteValueOptions.Builder(workspaceId, entity, entityValue).build();
service.deleteValue(deleteOptions).execute();
fail(ex.getMessage());
}
DeleteValueOptions deleteOptions = new DeleteValueOptions.Builder().workspaceId(workspaceId).entity(entity).value(entityValue).build();
service.deleteValue(deleteOptions).execute();
try {
GetValueOptions getOptions = new GetValueOptions.Builder(workspaceId, entity, entityValue).build();
service.getValue(getOptions).execute();
fail("deleteValue failed");
} catch (Exception ex) {
// Expected result
assertTrue(ex instanceof NotFoundException);
}
}
use of com.ibm.watson.developer_cloud.assistant.v1.model.Value in project java-sdk by watson-developer-cloud.
the class ValuesIT method testUpdateValue.
/**
* Test updateValue.
*/
@Test
public void testUpdateValue() {
String entity = "beverage";
String entityValue1 = "coffee" + UUID.randomUUID().toString();
String entityValue2 = "coffee" + UUID.randomUUID().toString();
String synonym1 = "java";
String synonym2 = "joe";
// metadata
Map<String, Object> valueMetadata = new HashMap<String, Object>();
String metadataValue = "value for " + entityValue2;
valueMetadata.put("key", metadataValue);
try {
CreateEntityOptions createOptions = new CreateEntityOptions.Builder(workspaceId, entity).build();
service.createEntity(createOptions).execute();
} catch (Exception ex) {
// Exception is okay if is for Unique Violation
assertTrue(ex.getLocalizedMessage().startsWith("Unique Violation"));
}
try {
CreateValueOptions createOptions = new CreateValueOptions.Builder(workspaceId, entity, entityValue1).build();
service.createValue(createOptions).execute();
} catch (Exception ex) {
// Exception is okay if is for Unique Violation
assertTrue(ex.getLocalizedMessage().startsWith("Unique Violation"));
}
UpdateValueOptions updateOptions = new UpdateValueOptions.Builder().workspaceId(workspaceId).entity(entity).value(entityValue1).newValue(entityValue2).newSynonyms(new ArrayList<String>(Arrays.asList(synonym1, synonym2))).newMetadata(valueMetadata).build();
Value response = service.updateValue(updateOptions).execute();
try {
assertNotNull(response);
assertNotNull(response.getValueText());
assertEquals(response.getValueText(), entityValue2);
GetValueOptions getOptions = new GetValueOptions.Builder(workspaceId, entity, entityValue2).export(true).includeAudit(true).build();
ValueExport vResponse = service.getValue(getOptions).execute();
assertNotNull(vResponse);
assertNotNull(vResponse.getValueText());
assertEquals(vResponse.getValueText(), entityValue2);
assertNotNull(vResponse.getCreated());
assertNotNull(vResponse.getUpdated());
assertNotNull(vResponse.getSynonyms());
assertTrue(vResponse.getSynonyms().size() == 2);
assertTrue(vResponse.getSynonyms().contains(synonym1));
assertTrue(vResponse.getSynonyms().contains(synonym2));
// metadata
assertNotNull(response.getMetadata());
assertNotNull(response.getMetadata().get("key"));
assertEquals(response.getMetadata().get("key"), metadataValue);
} catch (Exception ex) {
fail(ex.getMessage());
} finally {
// Clean up
DeleteValueOptions deleteOptions = new DeleteValueOptions.Builder(workspaceId, entity, entityValue2).build();
service.deleteValue(deleteOptions).execute();
}
}
use of com.ibm.watson.developer_cloud.assistant.v1.model.Value in project java-sdk by watson-developer-cloud.
the class EntitiesIT method testCreateEntity.
/**
* Test createEntity.
*/
@Test
public void testCreateEntity() {
// gotta be unique
String entity = "Hello" + UUID.randomUUID().toString();
String entityDescription = "Description of " + entity;
Map<String, Object> entityMetadata = new HashMap<String, Object>();
String metadataValue = "value for " + entity;
entityMetadata.put("key", metadataValue);
CreateEntityOptions.Builder optionsBuilder = new CreateEntityOptions.Builder();
optionsBuilder.workspaceId(workspaceId);
optionsBuilder.entity(entity);
optionsBuilder.description(entityDescription);
optionsBuilder.metadata(entityMetadata);
// default is false
optionsBuilder.fuzzyMatch(true);
Entity response = service.createEntity(optionsBuilder.build()).execute();
try {
assertNotNull(response);
assertNotNull(response.getEntityName());
assertEquals(response.getEntityName(), entity);
assertNotNull(response.getDescription());
assertEquals(response.getDescription(), entityDescription);
assertNotNull(response.getMetadata());
assertNotNull(response.getMetadata().get("key"));
assertEquals(response.getMetadata().get("key"), metadataValue);
assertNotNull(response.isFuzzyMatch());
assertTrue(response.isFuzzyMatch());
} catch (Exception ex) {
fail(ex.getMessage());
} finally {
// Clean up
DeleteEntityOptions deleteOptions = new DeleteEntityOptions.Builder(workspaceId, entity).build();
service.deleteEntity(deleteOptions).execute();
}
}
use of com.ibm.watson.developer_cloud.assistant.v1.model.Value in project java-sdk by watson-developer-cloud.
the class EntitiesIT method testListEntities.
/**
* Test listEntities.
*/
@Test
@Ignore("To be run locally until we fix the Rate limitation issue")
public void testListEntities() {
// gotta be unique
String entity = "Hello" + UUID.randomUUID().toString();
try {
ListEntitiesOptions listOptions = new ListEntitiesOptions.Builder(workspaceId).build();
EntityCollection response = service.listEntities(listOptions).execute();
assertNotNull(response);
assertNotNull(response.getEntities());
assertNotNull(response.getPagination());
assertNotNull(response.getPagination().getRefreshUrl());
// nextUrl may be null
// Now add an entity and make sure we get it back
String entityDescription = "Description of " + entity;
String entityValue = "Value of " + entity;
CreateEntityOptions options = new CreateEntityOptions.Builder(workspaceId, entity).description(entityDescription).addValue(new CreateValue.Builder(entityValue).build()).build();
service.createEntity(options).execute();
ListEntitiesOptions listOptions2 = listOptions.newBuilder().sort("-updated").pageLimit(5L).export(true).build();
EntityCollection response2 = service.listEntities(listOptions2).execute();
assertNotNull(response2);
assertNotNull(response2.getEntities());
List<EntityExport> entities = response2.getEntities();
EntityExport ieResponse = null;
for (EntityExport resp : entities) {
if (resp.getEntityName().equals(entity)) {
ieResponse = resp;
break;
}
}
assertNotNull(ieResponse);
assertNotNull(ieResponse.getDescription());
assertEquals(ieResponse.getDescription(), entityDescription);
assertNotNull(ieResponse.getValues());
assertTrue(ieResponse.getValues().size() == 1);
assertTrue(ieResponse.getValues().get(0).getValueText().equals(entityValue));
} catch (Exception ex) {
fail(ex.getMessage());
} finally {
// Clean up
DeleteEntityOptions deleteOptions = new DeleteEntityOptions.Builder(workspaceId, entity).build();
service.deleteEntity(deleteOptions).execute();
}
}
Aggregations