use of com.ibm.watson.developer_cloud.conversation.v1.model.Entity in project java-sdk by watson-developer-cloud.
the class ConversationTest method testCreateEntityOptionsBuilder.
/**
* Test CreateEntityOptions builder.
*/
@Test
public void testCreateEntityOptionsBuilder() {
String entity = "anEntity";
CreateValue entityValue0 = new CreateValue.Builder().value("entityValue0").addPattern("pattern0").build();
CreateValue entityValue1 = new CreateValue.Builder().value("entityValue1").addPattern("pattern1").build();
CreateEntityOptions createOptions = new CreateEntityOptions.Builder().workspaceId(WORKSPACE_ID).entity(entity).addValue(entityValue0).addValue(entityValue1).build();
assertEquals(createOptions.workspaceId(), WORKSPACE_ID);
assertEquals(createOptions.entity(), entity);
assertEquals(createOptions.values().size(), 2);
assertEquals(createOptions.values().get(0), entityValue0);
assertEquals(createOptions.values().get(1), entityValue1);
CreateEntityOptions.Builder builder = createOptions.newBuilder();
CreateValue entityValue2 = new CreateValue.Builder().value("entityValue2").addPattern("pattern2").build();
builder.values(Arrays.asList(entityValue2));
CreateEntityOptions options2 = builder.build();
assertEquals(options2.workspaceId(), WORKSPACE_ID);
assertEquals(options2.entity(), entity);
assertEquals(options2.values().size(), 1);
assertEquals(options2.values().get(0), entityValue2);
}
use of com.ibm.watson.developer_cloud.conversation.v1.model.Entity in project java-sdk by watson-developer-cloud.
the class ConversationTest method testSendMessage.
/**
* Test send message.
*
* @throws IOException Signals that an I/O exception has occurred.
* @throws InterruptedException the interrupted exception
*/
@Test
public void testSendMessage() throws IOException, InterruptedException {
String text = "I'd like to get insurance to for my home";
MessageResponse mockResponse = loadFixture(FIXTURE, MessageResponse.class);
server.enqueue(jsonResponse(mockResponse));
InputData input = new InputData.Builder(text).build();
RuntimeIntent intent = new RuntimeIntent();
intent.setIntent("turn_off");
intent.setConfidence(0.0);
RuntimeEntity entity = new RuntimeEntity();
entity.setEntity("car");
entity.setValue("ford");
MessageOptions options = new MessageOptions.Builder(WORKSPACE_ID).input(input).addIntent(intent).addEntity(entity).alternateIntents(true).build();
// execute first request
MessageResponse serviceResponse = service.message(options).execute();
// first request
RecordedRequest request = server.takeRequest();
String path = StringUtils.join(PATH_MESSAGE, "?", VERSION, "=2018-02-16");
assertEquals(path, request.getPath());
assertArrayEquals(new String[] { "Do you want to get a quote?" }, serviceResponse.getOutput().getText().toArray(new String[0]));
assertEquals(request.getMethod(), "POST");
assertNotNull(request.getHeader(HttpHeaders.AUTHORIZATION));
String expected = "{" + "\"input\":{\"text\":\"I'd like to get insurance to for my home\"}," + "\"intents\":[{\"confidence\":0.0,\"intent\":\"turn_off\"}]," + "\"entities\":[{\"value\":\"ford\",\"entity\":\"car\"}]," + "\"alternate_intents\":true" + "}";
JsonParser parser = new JsonParser();
JsonObject expectedObj = parser.parse(expected).getAsJsonObject();
JsonObject actualObj = parser.parse(request.getBody().readUtf8()).getAsJsonObject();
assertTrue(expectedObj.equals(actualObj));
assertEquals(serviceResponse, mockResponse);
}
use of com.ibm.watson.developer_cloud.conversation.v1.model.Entity in project java-sdk by watson-developer-cloud.
the class ConversationTest method testUpdateEntityOptionsBuilder.
/**
* Test UpdateEntityOptions builder.
*/
@Test
public void testUpdateEntityOptionsBuilder() {
String entity = "anEntity";
String newEntity = "renamedEntity";
CreateValue entityValue0 = new CreateValue.Builder().value("entityValue0").addPattern("pattern0").build();
CreateValue entityValue1 = new CreateValue.Builder().value("entityValue1").addPattern("pattern1").build();
UpdateEntityOptions updateOptions = new UpdateEntityOptions.Builder().workspaceId(WORKSPACE_ID).entity(entity).newEntity(newEntity).addValue(entityValue0).addValue(entityValue1).build();
assertEquals(updateOptions.workspaceId(), WORKSPACE_ID);
assertEquals(updateOptions.entity(), entity);
assertEquals(updateOptions.newEntity(), newEntity);
assertEquals(updateOptions.newValues().size(), 2);
assertEquals(updateOptions.newValues().get(0), entityValue0);
assertEquals(updateOptions.newValues().get(1), entityValue1);
UpdateEntityOptions.Builder builder = updateOptions.newBuilder();
CreateValue entityValue2 = new CreateValue.Builder().value("entityValue2").addPattern("pattern2").build();
builder.newValues(Arrays.asList(entityValue2));
UpdateEntityOptions options2 = builder.build();
assertEquals(options2.workspaceId(), WORKSPACE_ID);
assertEquals(options2.entity(), entity);
assertEquals(options2.newEntity(), newEntity);
assertEquals(options2.newValues().size(), 1);
assertEquals(options2.newValues().get(0), entityValue2);
}
use of com.ibm.watson.developer_cloud.conversation.v1.model.Entity in project java-sdk by watson-developer-cloud.
the class EntitiesIT method testListEntitiesWithPaging.
/**
* Test listEntities with pagination.
*/
@Test
@Ignore("To be run locally until we fix the Rate limitation issue")
public void testListEntitiesWithPaging() {
// gotta be unique
String entity1 = "Hello" + UUID.randomUUID().toString();
// gotta be unique
String entity2 = "Goodbye" + UUID.randomUUID().toString();
CreateEntityOptions createOptions = new CreateEntityOptions.Builder(workspaceId, entity1).build();
service.createEntity(createOptions).execute();
service.createEntity(createOptions.newBuilder().entity(entity2).build()).execute();
try {
ListEntitiesOptions listOptions = new ListEntitiesOptions.Builder(workspaceId).sort("entity").pageLimit(1L).build();
EntityCollection response = service.listEntities(listOptions).execute();
assertNotNull(response);
assertNotNull(response.getEntities());
assertNotNull(response.getPagination());
assertNotNull(response.getPagination().getRefreshUrl());
assertNotNull(response.getPagination().getNextUrl());
assertNotNull(response.getPagination().getCursor());
assertTrue(!response.getEntities().get(0).getEntityName().equals(entity1));
EntityExport ieResponse = null;
while (response.getPagination().getCursor() != null) {
assertNotNull(response.getEntities());
assertTrue(response.getEntities().size() == 1);
if (response.getEntities().get(0).getEntityName().equals(entity1)) {
ieResponse = response.getEntities().get(0);
break;
}
String cursor = response.getPagination().getCursor();
response = service.listEntities(listOptions.newBuilder().cursor(cursor).build()).execute();
}
assertNotNull(ieResponse);
} catch (Exception ex) {
fail(ex.getMessage());
} finally {
// Clean up
DeleteEntityOptions deleteOptions = new DeleteEntityOptions.Builder(workspaceId, entity1).build();
service.deleteEntity(deleteOptions).execute();
service.deleteEntity(deleteOptions.newBuilder().entity(entity2).build()).execute();
}
}
use of com.ibm.watson.developer_cloud.conversation.v1.model.Entity in project java-sdk by watson-developer-cloud.
the class SynonymsIT method testUpdateSynonym.
/**
* Test updateSynonym.
*/
@Test
public void testUpdateSynonym() {
String entity = "beverage";
String entityValue = "coffee";
String synonym1 = "joe";
String synonym2 = "mud";
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();
} catch (Exception ex) {
// Exception is okay if is for Unique Violation
assertTrue(ex.getLocalizedMessage().startsWith("Unique Violation"));
}
UpdateSynonymOptions updateOptions = new UpdateSynonymOptions.Builder().workspaceId(workspaceId).entity(entity).value(entityValue).synonym(synonym1).newSynonym(synonym2).build();
Synonym response = service.updateSynonym(updateOptions).execute();
try {
assertNotNull(response);
assertNotNull(response.getSynonymText());
assertEquals(response.getSynonymText(), synonym2);
} catch (Exception ex) {
fail(ex.getMessage());
} finally {
// Clean up
DeleteSynonymOptions deleteOptions = new DeleteSynonymOptions.Builder(workspaceId, entity, entityValue, synonym2).build();
service.deleteSynonym(deleteOptions).execute();
}
}
Aggregations