Search in sources :

Example 6 with MessageOptions

use of com.ibm.watson.assistant.v2.model.MessageOptions 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 7 with MessageOptions

use of com.ibm.watson.assistant.v2.model.MessageOptions in project java-sdk by watson-developer-cloud.

the class AssistantTest method testAssistantWithNullWorkspaceId.

/**
 * Negative - Test assistant with null workspaceId.
 */
@Test(expected = IllegalArgumentException.class)
public void testAssistantWithNullWorkspaceId() {
    MessageOptions options = new MessageOptions.Builder().build();
    service.message(options).execute();
}
Also used : MessageOptions(com.ibm.watson.developer_cloud.assistant.v1.model.MessageOptions) WatsonServiceUnitTest(com.ibm.watson.developer_cloud.WatsonServiceUnitTest) Test(org.junit.Test)

Example 8 with MessageOptions

use of com.ibm.watson.assistant.v2.model.MessageOptions in project java-sdk by watson-developer-cloud.

the class AssistantTest method testSendMessageWithNullWorkspaceId.

/**
 * Negative - Test message with null workspace id.
 *
 * @throws InterruptedException the interrupted exception
 */
@Test(expected = IllegalArgumentException.class)
public void testSendMessageWithNullWorkspaceId() throws InterruptedException {
    String text = "I'd like to get insurance to for my home";
    InputData input = new InputData.Builder(text).build();
    MessageOptions options = new MessageOptions.Builder().input(input).alternateIntents(true).build();
    service.message(options).execute();
}
Also used : MessageOptions(com.ibm.watson.developer_cloud.assistant.v1.model.MessageOptions) InputData(com.ibm.watson.developer_cloud.assistant.v1.model.InputData) WatsonServiceUnitTest(com.ibm.watson.developer_cloud.WatsonServiceUnitTest) Test(org.junit.Test)

Example 9 with MessageOptions

use of com.ibm.watson.assistant.v2.model.MessageOptions in project java-sdk by watson-developer-cloud.

the class AssistantTest method testAssistantWithEmptyWorkspaceId.

/**
 * Negative - Test assistant with empty workspaceId.
 */
@Test(expected = IllegalArgumentException.class)
public void testAssistantWithEmptyWorkspaceId() {
    MessageOptions options = new MessageOptions.Builder("").build();
    service.message(options).execute();
}
Also used : MessageOptions(com.ibm.watson.developer_cloud.assistant.v1.model.MessageOptions) WatsonServiceUnitTest(com.ibm.watson.developer_cloud.WatsonServiceUnitTest) Test(org.junit.Test)

Example 10 with MessageOptions

use of com.ibm.watson.assistant.v2.model.MessageOptions in project java-sdk by watson-developer-cloud.

the class ConversationServiceIT method pingBadCredentialsThrowsException.

@Test(expected = UnauthorizedException.class)
public void pingBadCredentialsThrowsException() {
    Conversation badService = new Conversation("2018-02-16", "foo", "bar");
    MessageOptions options = new MessageOptions.Builder(workspaceId).build();
    badService.message(options).execute();
}
Also used : MessageOptions(com.ibm.watson.developer_cloud.conversation.v1.model.MessageOptions) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)20 MessageOptions (com.ibm.watson.developer_cloud.assistant.v1.model.MessageOptions)12 WatsonServiceUnitTest (com.ibm.watson.developer_cloud.WatsonServiceUnitTest)10 MessageOptions (com.ibm.watson.developer_cloud.conversation.v1.model.MessageOptions)10 MessageResponse (com.ibm.watson.developer_cloud.assistant.v1.model.MessageResponse)7 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)6 InputData (com.ibm.watson.developer_cloud.assistant.v1.model.InputData)5 InputData (com.ibm.watson.developer_cloud.conversation.v1.model.InputData)5 MessageResponse (com.ibm.watson.developer_cloud.conversation.v1.model.MessageResponse)5 HashMap (java.util.HashMap)4 JsonObject (com.google.gson.JsonObject)3 MessageInput (com.ibm.watson.assistant.v1.model.MessageInput)3 MessageOptions (com.ibm.watson.assistant.v1.model.MessageOptions)3 MessageResponse (com.ibm.watson.assistant.v1.model.MessageResponse)3 CompletableFuture (jersey.repackaged.jsr166e.CompletableFuture)3 JsonParser (com.google.gson.JsonParser)2 Response (com.ibm.cloud.sdk.core.http.Response)2 Authenticator (com.ibm.cloud.sdk.core.security.Authenticator)2 IamAuthenticator (com.ibm.cloud.sdk.core.security.IamAuthenticator)2 Context (com.ibm.watson.assistant.v1.model.Context)2