use of com.ibm.watson.assistant.v1.model.Synonym in project java-sdk by watson-developer-cloud.
the class SynonymsIT method testListSynonyms.
/**
* Test listSynonyms.
*/
@Test
public void testListSynonyms() {
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"));
}
try {
CreateSynonymOptions createOptions = new CreateSynonymOptions.Builder(workspaceId, entity, entityValue, synonym1).build();
service.createSynonym(createOptions).execute();
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 listOptions = new ListSynonymsOptions.Builder().workspaceId(workspaceId).entity(entity).value(entityValue).build();
final SynonymCollection response = service.listSynonyms(listOptions).execute();
assertNotNull(response);
assertNotNull(response.getSynonyms());
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.getSynonyms().size() >= 2);
// Should not be paginated, but just to be sure
if (response.getPagination().getNextUrl() == null) {
// assertTrue(response.getSynonyms().stream().filter(r -> r.getSynonym().equals(synonym1)).count() == 1);
boolean found1 = false, found2 = false;
for (Synonym synonymResponse : response.getSynonyms()) {
found1 |= synonymResponse.getSynonymText().equals(synonym1);
found2 |= synonymResponse.getSynonymText().equals(synonym2);
}
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();
}
}
use of com.ibm.watson.assistant.v1.model.Synonym in project java-sdk by watson-developer-cloud.
the class Conversation method getSynonym.
/**
* Get entity value synonym.
*
* Get information about a synonym for an entity value.
*
* @param getSynonymOptions the {@link GetSynonymOptions} containing the options for the call
* @return a {@link ServiceCall} with a response type of {@link Synonym}
*/
public ServiceCall<Synonym> getSynonym(GetSynonymOptions getSynonymOptions) {
Validator.notNull(getSynonymOptions, "getSynonymOptions cannot be null");
String[] pathSegments = { "v1/workspaces", "entities", "values", "synonyms" };
String[] pathParameters = { getSynonymOptions.workspaceId(), getSynonymOptions.entity(), getSynonymOptions.value(), getSynonymOptions.synonym() };
RequestBuilder builder = RequestBuilder.get(RequestBuilder.constructHttpUrl(getEndPoint(), pathSegments, pathParameters));
builder.query(VERSION, versionDate);
if (getSynonymOptions.includeAudit() != null) {
builder.query("include_audit", String.valueOf(getSynonymOptions.includeAudit()));
}
return createServiceCall(builder.build(), ResponseConverterUtils.getObject(Synonym.class));
}
use of com.ibm.watson.assistant.v1.model.Synonym in project java-sdk by watson-developer-cloud.
the class Assistant method updateSynonym.
/**
* Update entity value synonym.
*
* Update an existing entity value synonym with new text. This operation is limited to 1000 requests per 30 minutes.
* For more information, see **Rate limiting**.
*
* @param updateSynonymOptions the {@link UpdateSynonymOptions} containing the options for the call
* @return a {@link ServiceCall} with a response type of {@link Synonym}
*/
public ServiceCall<Synonym> updateSynonym(UpdateSynonymOptions updateSynonymOptions) {
Validator.notNull(updateSynonymOptions, "updateSynonymOptions cannot be null");
String[] pathSegments = { "v1/workspaces", "entities", "values", "synonyms" };
String[] pathParameters = { updateSynonymOptions.workspaceId(), updateSynonymOptions.entity(), updateSynonymOptions.value(), updateSynonymOptions.synonym() };
RequestBuilder builder = RequestBuilder.post(RequestBuilder.constructHttpUrl(getEndPoint(), pathSegments, pathParameters));
builder.query(VERSION, versionDate);
final JsonObject contentJson = new JsonObject();
if (updateSynonymOptions.newSynonym() != null) {
contentJson.addProperty("synonym", updateSynonymOptions.newSynonym());
}
builder.bodyJson(contentJson);
return createServiceCall(builder.build(), ResponseConverterUtils.getObject(Synonym.class));
}
use of com.ibm.watson.assistant.v1.model.Synonym in project java-sdk by watson-developer-cloud.
the class AssistantTest method testDeleteSynonymWOptions.
// Test the deleteSynonym operation with a valid options model parameter
@Test
public void testDeleteSynonymWOptions() throws Throwable {
// Register a mock response
String mockResponseBody = "";
String deleteSynonymPath = "/v1/workspaces/testString/entities/testString/values/testString/synonyms/testString";
server.enqueue(new MockResponse().setResponseCode(200).setBody(mockResponseBody));
// Construct an instance of the DeleteSynonymOptions model
DeleteSynonymOptions deleteSynonymOptionsModel = new DeleteSynonymOptions.Builder().workspaceId("testString").entity("testString").value("testString").synonym("testString").build();
// Invoke deleteSynonym() with a valid options model and verify the result
Response<Void> response = assistantService.deleteSynonym(deleteSynonymOptionsModel).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, deleteSynonymPath);
// Verify query params
Map<String, String> query = TestUtilities.parseQueryString(request);
assertNotNull(query);
assertEquals(query.get("version"), "testString");
}
use of com.ibm.watson.assistant.v1.model.Synonym in project java-sdk by watson-developer-cloud.
the class AssistantTest method testCreateValueWOptions.
// Test the createValue operation with a valid options model parameter
@Test
public void testCreateValueWOptions() 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 createValuePath = "/v1/workspaces/testString/entities/testString/values";
server.enqueue(new MockResponse().setHeader("Content-type", "application/json").setResponseCode(201).setBody(mockResponseBody));
// Construct an instance of the CreateValueOptions model
CreateValueOptions createValueOptionsModel = new CreateValueOptions.Builder().workspaceId("testString").entity("testString").value("testString").metadata(new java.util.HashMap<String, Object>() {
{
put("foo", "testString");
}
}).type("synonyms").synonyms(new java.util.ArrayList<String>(java.util.Arrays.asList("testString"))).patterns(new java.util.ArrayList<String>(java.util.Arrays.asList("testString"))).includeAudit(false).build();
// Invoke createValue() with a valid options model and verify the result
Response<Value> response = assistantService.createValue(createValueOptionsModel).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, createValuePath);
// Verify query params
Map<String, String> query = TestUtilities.parseQueryString(request);
assertNotNull(query);
assertEquals(query.get("version"), "testString");
assertEquals(Boolean.valueOf(query.get("include_audit")), Boolean.valueOf(false));
}
Aggregations