use of com.ibm.watson.assistant.v1.model.UpdateValueOptions in project java-sdk by watson-developer-cloud.
the class AssistantTest method testUpdateValueWOptions.
// Test the updateValue operation with a valid options model parameter
@Test
public void testUpdateValueWOptions() throws Throwable {
// Register a mock response
String mockResponseBody = "{\"value\": \"value\", \"metadata\": {\"mapKey\": \"anyValue\"}, \"type\": \"synonyms\", \"synonyms\": [\"synonym\"], \"patterns\": [\"pattern\"], \"created\": \"2019-01-01T12:00:00.000Z\", \"updated\": \"2019-01-01T12:00:00.000Z\"}";
String updateValuePath = "/v1/workspaces/testString/entities/testString/values/testString";
server.enqueue(new MockResponse().setHeader("Content-type", "application/json").setResponseCode(200).setBody(mockResponseBody));
// Construct an instance of the UpdateValueOptions model
UpdateValueOptions updateValueOptionsModel = new UpdateValueOptions.Builder().workspaceId("testString").entity("testString").value("testString").newValue("testString").newMetadata(new java.util.HashMap<String, Object>() {
{
put("foo", "testString");
}
}).newType("synonyms").newSynonyms(new java.util.ArrayList<String>(java.util.Arrays.asList("testString"))).newPatterns(new java.util.ArrayList<String>(java.util.Arrays.asList("testString"))).append(false).includeAudit(false).build();
// Invoke updateValue() with a valid options model and verify the result
Response<Value> response = assistantService.updateValue(updateValueOptionsModel).execute();
assertNotNull(response);
Value 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(), "POST");
// Verify request path
String parsedPath = TestUtilities.parseReqPath(request);
assertEquals(parsedPath, updateValuePath);
// Verify query params
Map<String, String> query = TestUtilities.parseQueryString(request);
assertNotNull(query);
assertEquals(query.get("version"), "testString");
assertEquals(Boolean.valueOf(query.get("append")), Boolean.valueOf(false));
assertEquals(Boolean.valueOf(query.get("include_audit")), Boolean.valueOf(false));
}
use of com.ibm.watson.assistant.v1.model.UpdateValueOptions 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();
}
}
Aggregations