use of com.ibm.watson.assistant.v1.model.CreateEntityOptions in project java-sdk by watson-developer-cloud.
the class ValuesIT method testListValuesWithPaging.
/**
* Test listValues with pagination.
*/
@Test
public void testListValuesWithPaging() {
String entity = "beverage";
String entityValue1 = "coffee";
String entityValue2 = "orange juice";
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, entity, entityValue1).build();
try {
service.createValue(createOptions).execute().getResult();
} catch (Exception ex) {
// Exception is okay if is for Unique Violation
assertTrue(ex.getLocalizedMessage().startsWith("Unique Violation"));
}
try {
service.createValue(createOptions.newBuilder().value(entityValue2).build()).execute().getResult();
} catch (Exception ex) {
// Exception is okay if is for Unique Violation
assertTrue(ex.getLocalizedMessage().startsWith("Unique Violation"));
}
try {
ListValuesOptions.Builder listOptionsBuilder = new ListValuesOptions.Builder(workspaceId, entity);
listOptionsBuilder.sort("updated");
listOptionsBuilder.pageLimit(1L);
listOptionsBuilder.export(true);
ValueCollection response = service.listValues(listOptionsBuilder.build()).execute().getResult();
assertNotNull(response);
assertNotNull(response.getPagination());
assertNotNull(response.getPagination().getRefreshUrl());
assertNotNull(response.getPagination().getNextUrl());
assertNotNull(response.getPagination().getNextCursor());
boolean found1 = false;
boolean found2 = false;
while (true) {
assertNotNull(response.getValues());
assertTrue(response.getValues().size() == 1);
found1 |= response.getValues().get(0).value().equals(entityValue1);
found2 |= response.getValues().get(0).value().equals(entityValue2);
if (response.getPagination().getNextCursor() == null) {
break;
}
String cursor = response.getPagination().getNextCursor();
response = service.listValues(listOptionsBuilder.cursor(cursor).build()).execute().getResult();
}
assertTrue(found1 && found2);
} catch (Exception ex) {
fail(ex.getMessage());
} finally {
DeleteValueOptions deleteOptions = new DeleteValueOptions.Builder(workspaceId, entity, entityValue1).build();
service.deleteValue(deleteOptions).execute().getResult();
service.deleteValue(deleteOptions.newBuilder().value(entityValue2).build()).execute().getResult();
}
}
use of com.ibm.watson.assistant.v1.model.CreateEntityOptions 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 metadataValue = "value for " + entityValue2;
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"));
}
try {
CreateValueOptions createOptions = new CreateValueOptions.Builder(workspaceId, entity, entityValue1).build();
service.createValue(createOptions).execute().getResult();
} 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<>(Arrays.asList(synonym1, synonym2))).newMetadata(valueMetadata).build();
Value response = service.updateValue(updateOptions).execute().getResult();
try {
assertNotNull(response);
assertNotNull(response.value());
assertEquals(response.value(), entityValue2);
GetValueOptions getOptions = new GetValueOptions.Builder(workspaceId, entity, entityValue2).export(true).includeAudit(true).build();
Value vResponse = service.getValue(getOptions).execute().getResult();
assertNotNull(vResponse);
assertNotNull(vResponse.value());
assertEquals(vResponse.value(), entityValue2);
assertNotNull(vResponse.created());
assertNotNull(vResponse.updated());
assertNotNull(vResponse.synonyms());
assertTrue(vResponse.synonyms().size() == 2);
assertTrue(vResponse.synonyms().contains(synonym1));
assertTrue(vResponse.synonyms().contains(synonym2));
// 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, entityValue2).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 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().getResult();
} 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().getResult();
try {
assertNotNull(response);
assertNotNull(response.value());
assertEquals(response.value(), entityValue);
assertNull(response.metadata());
} catch (Exception ex) {
// Clean up
DeleteValueOptions deleteOptions = new DeleteValueOptions.Builder(workspaceId, entity, entityValue).build();
service.deleteValue(deleteOptions).execute().getResult();
fail(ex.getMessage());
}
DeleteValueOptions deleteOptions = new DeleteValueOptions.Builder().workspaceId(workspaceId).entity(entity).value(entityValue).build();
service.deleteValue(deleteOptions).execute().getResult();
try {
GetValueOptions getOptions = new GetValueOptions.Builder(workspaceId, entity, entityValue).build();
service.getValue(getOptions).execute().getResult();
fail("deleteValue 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 ValuesIT method testListValues.
/**
* Test listValues.
*/
@Test
public void testListValues() {
String entity = "beverage";
String entityValue1 = "coffee";
String entityValue2 = "orange juice";
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, entity, entityValue1).build();
try {
service.createValue(createOptions).execute().getResult();
} catch (Exception ex) {
// Exception is okay if is for Unique Violation
assertTrue(ex.getLocalizedMessage().startsWith("Unique Violation"));
}
try {
service.createValue(createOptions.newBuilder().value(entityValue2).build()).execute().getResult();
} catch (Exception ex) {
// Exception is okay if is for Unique Violation
assertTrue(ex.getLocalizedMessage().startsWith("Unique Violation"));
}
try {
ListValuesOptions listOptions = new ListValuesOptions.Builder().workspaceId(workspaceId).entity(entity).build();
final ValueCollection response = service.listValues(listOptions).execute().getResult();
assertNotNull(response);
assertNotNull(response.getValues());
assertNotNull(response.getPagination());
assertNotNull(response.getPagination().getRefreshUrl());
// nextUrl may be null
if (response.getPagination().getNextUrl() == null) {
assertNull(response.getPagination().getNextCursor());
} else {
assertNotNull(response.getPagination().getNextCursor());
}
assertTrue(response.getValues().size() >= 2);
// Should not be paginated, but just to be sure
if (response.getPagination().getNextUrl() == null) {
// assertTrue(response.getValues().stream().filter(r ->
// r.getValue().equals(synonym1)).count() == 1);
boolean found1 = false;
boolean found2 = false;
for (Value valueResponse : response.getValues()) {
found1 |= valueResponse.value().equals(entityValue1);
found2 |= valueResponse.value().equals(entityValue2);
}
assertTrue(found1 && found2);
}
} catch (Exception ex) {
fail(ex.getMessage());
} finally {
DeleteValueOptions deleteOptions = new DeleteValueOptions.Builder(workspaceId, entity, entityValue1).build();
service.deleteValue(deleteOptions).execute().getResult();
service.deleteValue(deleteOptions.newBuilder().value(entityValue2).build()).execute().getResult();
}
}
Aggregations