use of com.ibm.watson.assistant.v1.model.DeleteValueOptions in project java-sdk by watson-developer-cloud.
the class ValuesIT method testGetValue.
/**
* Test getValue.
*/
@Test
public void testGetValue() {
String entity = "beverage";
String entityValue = "coffee" + UUID.randomUUID().toString();
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"));
}
CreateValueOptions createOptions = new CreateValueOptions.Builder(workspaceId, entity, entityValue).synonyms(new ArrayList<String>(Arrays.asList(synonym1, synonym2))).build();
service.createValue(createOptions).execute();
Date start = new Date();
try {
GetValueOptions getOptions = new GetValueOptions.Builder(workspaceId, entity, entityValue).export(true).includeAudit(true).build();
ValueExport response = service.getValue(getOptions).execute();
assertNotNull(response);
assertNotNull(response.getValueText());
assertEquals(response.getValueText(), entityValue);
assertNotNull(response.getCreated());
assertNotNull(response.getUpdated());
Date now = new Date();
assertTrue(fuzzyBefore(response.getCreated(), now));
assertTrue(fuzzyAfter(response.getCreated(), start));
assertTrue(fuzzyBefore(response.getUpdated(), now));
assertTrue(fuzzyAfter(response.getUpdated(), start));
assertNotNull(response.getSynonyms());
assertTrue(response.getSynonyms().size() == 2);
assertTrue(response.getSynonyms().contains(synonym1));
assertTrue(response.getSynonyms().contains(synonym2));
} catch (Exception ex) {
fail(ex.getMessage());
} finally {
DeleteValueOptions deleteOptions = new DeleteValueOptions.Builder(workspaceId, entity, entityValue).build();
service.deleteValue(deleteOptions).execute();
}
}
use of com.ibm.watson.assistant.v1.model.DeleteValueOptions 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();
} 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();
} 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();
} 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();
assertNotNull(response);
assertNotNull(response.getValues());
assertNotNull(response.getPagination());
assertNotNull(response.getPagination().getRefreshUrl());
// nextUrl may be null
if (response.getPagination().getNextUrl() == null) {
assertNull(response.getPagination().getCursor());
} else {
assertNotNull(response.getPagination().getCursor());
}
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, found2 = false;
for (ValueExport valueResponse : response.getValues()) {
found1 |= valueResponse.getValueText().equals(entityValue1);
found2 |= valueResponse.getValueText().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();
service.deleteValue(deleteOptions.newBuilder().value(entityValue2).build()).execute();
}
}
use of com.ibm.watson.assistant.v1.model.DeleteValueOptions 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();
} 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();
} 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();
} 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();
assertNotNull(response);
assertNotNull(response.getValues());
assertNotNull(response.getPagination());
assertNotNull(response.getPagination().getRefreshUrl());
// nextUrl may be null
if (response.getPagination().getNextUrl() == null) {
assertNull(response.getPagination().getCursor());
} else {
assertNotNull(response.getPagination().getCursor());
}
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, found2 = false;
for (ValueExport valueResponse : response.getValues()) {
found1 |= valueResponse.getValueText().equals(entityValue1);
found2 |= valueResponse.getValueText().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();
service.deleteValue(deleteOptions.newBuilder().value(entityValue2).build()).execute();
}
}
use of com.ibm.watson.assistant.v1.model.DeleteValueOptions 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.assistant.v1.model.DeleteValueOptions in project java-sdk by watson-developer-cloud.
the class AssistantTest method testDeleteValueWOptions.
// Test the deleteValue operation with a valid options model parameter
@Test
public void testDeleteValueWOptions() throws Throwable {
// Register a mock response
String mockResponseBody = "";
String deleteValuePath = "/v1/workspaces/testString/entities/testString/values/testString";
server.enqueue(new MockResponse().setResponseCode(200).setBody(mockResponseBody));
// Construct an instance of the DeleteValueOptions model
DeleteValueOptions deleteValueOptionsModel = new DeleteValueOptions.Builder().workspaceId("testString").entity("testString").value("testString").build();
// Invoke deleteValue() with a valid options model and verify the result
Response<Void> response = assistantService.deleteValue(deleteValueOptionsModel).execute();
assertNotNull(response);
Void responseObj = response.getResult();
assertNull(responseObj);
// Verify the contents of the request sent to the mock server
RecordedRequest request = server.takeRequest();
assertNotNull(request);
assertEquals(request.getMethod(), "DELETE");
// Verify request path
String parsedPath = TestUtilities.parseReqPath(request);
assertEquals(parsedPath, deleteValuePath);
// Verify query params
Map<String, String> query = TestUtilities.parseQueryString(request);
assertNotNull(query);
assertEquals(query.get("version"), "testString");
}
Aggregations