Search in sources :

Example 1 with RuntimeIntent

use of com.ibm.watson.developer_cloud.assistant.v1.model.RuntimeIntent 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);
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MessageOptions(com.ibm.watson.developer_cloud.conversation.v1.model.MessageOptions) RuntimeIntent(com.ibm.watson.developer_cloud.conversation.v1.model.RuntimeIntent) RuntimeEntity(com.ibm.watson.developer_cloud.conversation.v1.model.RuntimeEntity) MessageResponse(com.ibm.watson.developer_cloud.conversation.v1.model.MessageResponse) JsonObject(com.google.gson.JsonObject) InputData(com.ibm.watson.developer_cloud.conversation.v1.model.InputData) JsonParser(com.google.gson.JsonParser) WatsonServiceUnitTest(com.ibm.watson.developer_cloud.WatsonServiceUnitTest) Test(org.junit.Test)

Example 2 with RuntimeIntent

use of com.ibm.watson.developer_cloud.assistant.v1.model.RuntimeIntent in project java-sdk by watson-developer-cloud.

the class AssistantTest 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);
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MessageOptions(com.ibm.watson.developer_cloud.assistant.v1.model.MessageOptions) RuntimeIntent(com.ibm.watson.developer_cloud.assistant.v1.model.RuntimeIntent) RuntimeEntity(com.ibm.watson.developer_cloud.assistant.v1.model.RuntimeEntity) MessageResponse(com.ibm.watson.developer_cloud.assistant.v1.model.MessageResponse) JsonObject(com.google.gson.JsonObject) InputData(com.ibm.watson.developer_cloud.assistant.v1.model.InputData) JsonParser(com.google.gson.JsonParser) WatsonServiceUnitTest(com.ibm.watson.developer_cloud.WatsonServiceUnitTest) Test(org.junit.Test)

Example 3 with RuntimeIntent

use of com.ibm.watson.developer_cloud.assistant.v1.model.RuntimeIntent in project java-sdk by watson-developer-cloud.

the class AssistantServiceIT method testSendMessages.

/**
 * Test send messages.
 *
 * @throws InterruptedException the interrupted exception
 */
@Test
public void testSendMessages() throws InterruptedException {
    final String[] messages = new String[] { "turn ac on", "turn right", "no", "yes" };
    Context context = null;
    for (final String message : messages) {
        MessageOptions request = new MessageOptions.Builder(workspaceId).input(new InputData.Builder(message).build()).alternateIntents(true).context(context).nodesVisitedDetails(true).build();
        if (message.equals("yes")) {
            RuntimeIntent offTopic = new RuntimeIntent();
            offTopic.setIntent("off_topic");
            offTopic.setConfidence(1.0);
            request = request.newBuilder().addIntent(offTopic).build();
        }
        MessageResponse response = service.message(request).execute();
        assertMessageFromService(response);
        assertNotNull(response.getOutput().getNodesVisitedDetails());
        for (RuntimeEntity entity : response.getEntities()) {
            assertNotNull(entity.getGroups());
        }
        context = new Context();
        context.putAll(response.getContext());
        Thread.sleep(500);
    }
}
Also used : Context(com.ibm.watson.developer_cloud.assistant.v1.model.Context) MessageOptions(com.ibm.watson.developer_cloud.assistant.v1.model.MessageOptions) RuntimeIntent(com.ibm.watson.developer_cloud.assistant.v1.model.RuntimeIntent) RuntimeEntity(com.ibm.watson.developer_cloud.assistant.v1.model.RuntimeEntity) MessageResponse(com.ibm.watson.developer_cloud.assistant.v1.model.MessageResponse) Test(org.junit.Test)

Example 4 with RuntimeIntent

use of com.ibm.watson.developer_cloud.assistant.v1.model.RuntimeIntent in project java-sdk by watson-developer-cloud.

the class ConversationServiceIT method testSendMessages.

/**
 * Test send messages.
 *
 * @throws InterruptedException the interrupted exception
 */
@Test
public void testSendMessages() throws InterruptedException {
    final String[] messages = new String[] { "turn ac on", "turn right", "no", "yes" };
    Context context = null;
    for (final String message : messages) {
        MessageOptions request = new MessageOptions.Builder(workspaceId).input(new InputData.Builder(message).build()).alternateIntents(true).context(context).nodesVisitedDetails(true).build();
        if (message.equals("yes")) {
            RuntimeIntent offTopic = new RuntimeIntent();
            offTopic.setIntent("off_topic");
            offTopic.setConfidence(1.0);
            request = request.newBuilder().addIntent(offTopic).build();
        }
        MessageResponse response = service.message(request).execute();
        assertMessageFromService(response);
        assertNotNull(response.getOutput().getNodesVisitedDetails());
        for (RuntimeEntity entity : response.getEntities()) {
            assertNotNull(entity.getGroups());
        }
        context = new Context();
        context.putAll(response.getContext());
        Thread.sleep(500);
    }
}
Also used : Context(com.ibm.watson.developer_cloud.conversation.v1.model.Context) MessageOptions(com.ibm.watson.developer_cloud.conversation.v1.model.MessageOptions) RuntimeIntent(com.ibm.watson.developer_cloud.conversation.v1.model.RuntimeIntent) RuntimeEntity(com.ibm.watson.developer_cloud.conversation.v1.model.RuntimeEntity) MessageResponse(com.ibm.watson.developer_cloud.conversation.v1.model.MessageResponse) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)4 JsonObject (com.google.gson.JsonObject)2 JsonParser (com.google.gson.JsonParser)2 WatsonServiceUnitTest (com.ibm.watson.developer_cloud.WatsonServiceUnitTest)2 MessageOptions (com.ibm.watson.developer_cloud.assistant.v1.model.MessageOptions)2 MessageResponse (com.ibm.watson.developer_cloud.assistant.v1.model.MessageResponse)2 RuntimeEntity (com.ibm.watson.developer_cloud.assistant.v1.model.RuntimeEntity)2 RuntimeIntent (com.ibm.watson.developer_cloud.assistant.v1.model.RuntimeIntent)2 MessageOptions (com.ibm.watson.developer_cloud.conversation.v1.model.MessageOptions)2 MessageResponse (com.ibm.watson.developer_cloud.conversation.v1.model.MessageResponse)2 RuntimeEntity (com.ibm.watson.developer_cloud.conversation.v1.model.RuntimeEntity)2 RuntimeIntent (com.ibm.watson.developer_cloud.conversation.v1.model.RuntimeIntent)2 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)2 Context (com.ibm.watson.developer_cloud.assistant.v1.model.Context)1 InputData (com.ibm.watson.developer_cloud.assistant.v1.model.InputData)1 Context (com.ibm.watson.developer_cloud.conversation.v1.model.Context)1 InputData (com.ibm.watson.developer_cloud.conversation.v1.model.InputData)1