use of com.ibm.watson.assistant.v1.model.CreateEntityOptions 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().getResult();
} 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().getResult();
try {
assertNotNull(response);
assertNotNull(response.value());
assertEquals(response.value(), entityValue);
assertNotNull(response.metadata());
// metadata
assertNotNull(response.metadata());
assertNotNull(response.metadata().get("key"));
assertEquals(response.metadata().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().getResult();
}
}
use of com.ibm.watson.assistant.v1.model.CreateEntityOptions in project java-sdk by watson-developer-cloud.
the class EntitiesIT method testDeleteEntity.
/**
* Test deleteEntity.
*/
@Test
public void testDeleteEntity() {
// gotta be unique
String entity = "Hello" + UUID.randomUUID().toString();
CreateEntityOptions options = new CreateEntityOptions.Builder(workspaceId, entity).build();
Entity response = service.createEntity(options).execute();
try {
assertNotNull(response);
assertNotNull(response.getEntityName());
assertEquals(response.getEntityName(), entity);
assertNull(response.getDescription());
assertNull(response.getMetadata());
assertTrue(response.isFuzzyMatch() == null || response.isFuzzyMatch().equals(Boolean.FALSE));
} catch (Exception ex) {
DeleteEntityOptions deleteOptions = new DeleteEntityOptions.Builder(workspaceId, entity).build();
service.deleteEntity(deleteOptions).execute();
fail(ex.getMessage());
}
DeleteEntityOptions deleteOptions = new DeleteEntityOptions.Builder(workspaceId, entity).build();
service.deleteEntity(deleteOptions).execute();
try {
GetEntityOptions getOptions = new GetEntityOptions.Builder(workspaceId, entity).build();
service.getEntity(getOptions).execute();
fail("deleteEntity failed");
} catch (Exception ex) {
// Expected result
assertTrue(ex instanceof NotFoundException);
}
}
use of com.ibm.watson.assistant.v1.model.CreateEntityOptions 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();
}
}
use of com.ibm.watson.assistant.v1.model.CreateEntityOptions in project java-sdk by watson-developer-cloud.
the class SynonymsIT method testCreateSynonym.
/**
* Test createSynonym.
*/
@Test
public void testCreateSynonym() {
String entity = "beverage";
String entityValue = "orange juice";
String synonym = "OJ";
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, entityValue).build();
service.createValue(createOptions).execute();
} catch (Exception ex) {
// Exception is okay if is for Unique Violation
assertTrue(ex.getLocalizedMessage().startsWith("Unique Violation"));
}
CreateSynonymOptions createOptions = new CreateSynonymOptions.Builder().workspaceId(workspaceId).entity(entity).value(entityValue).synonym(synonym).build();
Synonym response = service.createSynonym(createOptions).execute();
try {
assertNotNull(response);
assertNotNull(response.getSynonymText());
assertEquals(response.getSynonymText(), synonym);
} catch (Exception ex) {
fail(ex.getMessage());
} finally {
// Clean up
DeleteSynonymOptions deleteOptions = new DeleteSynonymOptions.Builder(workspaceId, entity, entityValue, synonym).build();
service.deleteSynonym(deleteOptions).execute();
}
}
use of com.ibm.watson.assistant.v1.model.CreateEntityOptions in project java-sdk by watson-developer-cloud.
the class SynonymsIT method testListSynonymsWithPaging.
/**
* Test listSynonyms with pagination.
*/
@Test
public void testListSynonymsWithPaging() {
String entity = "beverage";
String entityValue = "coffee";
String synonym1 = "java";
String synonym2 = "joe";
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, entityValue).build();
service.createValue(createOptions).execute();
} catch (Exception ex) {
// Exception is okay if is for Unique Violation
assertTrue(ex.getLocalizedMessage().startsWith("Unique Violation"));
}
CreateSynonymOptions createOptions = new CreateSynonymOptions.Builder(workspaceId, entity, entityValue, synonym1).build();
try {
service.createSynonym(createOptions).execute();
} catch (Exception ex) {
// Exception is okay if is for Unique Violation
assertTrue(ex.getLocalizedMessage().startsWith("Unique Violation"));
}
try {
service.createSynonym(createOptions.newBuilder().synonym(synonym2).build()).execute();
} catch (Exception ex) {
// Exception is okay if is for Unique Violation
assertTrue(ex.getLocalizedMessage().startsWith("Unique Violation"));
}
try {
ListSynonymsOptions.Builder listOptionsBuilder = new ListSynonymsOptions.Builder(workspaceId, entity, entityValue);
listOptionsBuilder.sort("modified");
listOptionsBuilder.pageLimit(1L);
SynonymCollection response = service.listSynonyms(listOptionsBuilder.build()).execute();
assertNotNull(response);
assertNotNull(response.getPagination());
assertNotNull(response.getPagination().getRefreshUrl());
assertNotNull(response.getPagination().getNextUrl());
assertNotNull(response.getPagination().getCursor());
boolean found1 = false, found2 = false;
while (true) {
assertNotNull(response.getSynonyms());
assertTrue(response.getSynonyms().size() == 1);
found1 |= response.getSynonyms().get(0).getSynonymText().equals(synonym1);
found2 |= response.getSynonyms().get(0).getSynonymText().equals(synonym2);
if (response.getPagination().getCursor() == null) {
break;
}
String cursor = response.getPagination().getCursor();
response = service.listSynonyms(listOptionsBuilder.cursor(cursor).build()).execute();
}
assertTrue(found1 && found2);
} catch (Exception ex) {
fail(ex.getMessage());
} finally {
DeleteSynonymOptions deleteOptions = new DeleteSynonymOptions.Builder(workspaceId, entity, entityValue, synonym1).build();
service.deleteSynonym(deleteOptions).execute();
service.deleteSynonym(deleteOptions.newBuilder().synonym(synonym2).build()).execute();
}
}
Aggregations