|
| 1 | +/* |
| 2 | + * Copyright 2018 IBM Corp. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with |
| 5 | + * the License. You may obtain a copy of the License at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on |
| 10 | + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 11 | + * specific language governing permissions and limitations under the License. |
| 12 | + */ |
| 13 | +package com.ibm.watson.developer_cloud.assistant.v2; |
| 14 | + |
| 15 | +import com.ibm.watson.developer_cloud.assistant.v2.model.CreateSessionOptions; |
| 16 | +import com.ibm.watson.developer_cloud.assistant.v2.model.DeleteSessionOptions; |
| 17 | +import com.ibm.watson.developer_cloud.assistant.v2.model.DialogRuntimeResponseGeneric; |
| 18 | +import com.ibm.watson.developer_cloud.assistant.v2.model.MessageContext; |
| 19 | +import com.ibm.watson.developer_cloud.assistant.v2.model.MessageInput; |
| 20 | +import com.ibm.watson.developer_cloud.assistant.v2.model.MessageInputOptions; |
| 21 | +import com.ibm.watson.developer_cloud.assistant.v2.model.MessageOptions; |
| 22 | +import com.ibm.watson.developer_cloud.assistant.v2.model.MessageResponse; |
| 23 | +import com.ibm.watson.developer_cloud.assistant.v2.model.SessionResponse; |
| 24 | +import com.ibm.watson.developer_cloud.util.RetryRunner; |
| 25 | +import org.junit.Before; |
| 26 | +import org.junit.Test; |
| 27 | +import org.junit.runner.RunWith; |
| 28 | + |
| 29 | +import java.util.Arrays; |
| 30 | +import java.util.List; |
| 31 | + |
| 32 | +import static org.junit.Assert.assertNotNull; |
| 33 | +import static org.junit.Assert.assertTrue; |
| 34 | + |
| 35 | +/** |
| 36 | + * Integration tests for Assistant v2. |
| 37 | + */ |
| 38 | +@RunWith(RetryRunner.class) |
| 39 | +public class AssistantServiceIT extends AssistantServiceTest { |
| 40 | + private Assistant service; |
| 41 | + private String assistantId; |
| 42 | + |
| 43 | + @Override |
| 44 | + @Before |
| 45 | + public void setUp() throws Exception { |
| 46 | + super.setUp(); |
| 47 | + this.service = getService(); |
| 48 | + this.assistantId = getAssistantId(); |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + public void testSendMessages() { |
| 53 | + // get session ID |
| 54 | + CreateSessionOptions createSessionOptions = new CreateSessionOptions.Builder() |
| 55 | + .assistantId(assistantId) |
| 56 | + .build(); |
| 57 | + SessionResponse sessionResponse = service.createSession(createSessionOptions).execute(); |
| 58 | + String sessionId = sessionResponse.getSessionId(); |
| 59 | + |
| 60 | + final List<String> messages = Arrays.asList( |
| 61 | + "I want some pizza.", |
| 62 | + "I'd like 3 pizzas.", |
| 63 | + "Large" |
| 64 | + ); |
| 65 | + MessageContext context = new MessageContext(); |
| 66 | + |
| 67 | + try { |
| 68 | + // send messages |
| 69 | + for (String message : messages) { |
| 70 | + MessageInputOptions inputOptions = new MessageInputOptions(); |
| 71 | + inputOptions.setDebug(true); |
| 72 | + MessageInput input = new MessageInput.Builder() |
| 73 | + .text(message) |
| 74 | + .messageType(MessageInput.MessageType.TEXT) |
| 75 | + .options(inputOptions) |
| 76 | + .build(); |
| 77 | + MessageOptions messageOptions = new MessageOptions.Builder() |
| 78 | + .assistantId(assistantId) |
| 79 | + .sessionId(sessionId) |
| 80 | + .input(input) |
| 81 | + .context(context) |
| 82 | + .build(); |
| 83 | + MessageResponse messageResponse = service.message(messageOptions).execute(); |
| 84 | + |
| 85 | + // message assertions |
| 86 | + List<DialogRuntimeResponseGeneric> genericResponses = messageResponse.getOutput().getGeneric(); |
| 87 | + assertNotNull(genericResponses); |
| 88 | + boolean foundTextResponse = false; |
| 89 | + for (DialogRuntimeResponseGeneric generic : genericResponses) { |
| 90 | + if (generic.getResponseType().equals(DialogRuntimeResponseGeneric.ResponseType.TEXT)) { |
| 91 | + foundTextResponse = true; |
| 92 | + break; |
| 93 | + } |
| 94 | + } |
| 95 | + assertTrue(foundTextResponse); |
| 96 | + assertNotNull(messageResponse.getOutput().getEntities()); |
| 97 | + assertNotNull(messageResponse.getOutput().getIntents()); |
| 98 | + assertNotNull(messageResponse.getOutput().getDebug()); |
| 99 | + |
| 100 | + context = messageResponse.getContext(); |
| 101 | + } |
| 102 | + } finally { |
| 103 | + // delete session |
| 104 | + DeleteSessionOptions deleteSessionOptions = new DeleteSessionOptions.Builder() |
| 105 | + .assistantId(assistantId) |
| 106 | + .sessionId(sessionId) |
| 107 | + .build(); |
| 108 | + service.deleteSession(deleteSessionOptions).execute(); |
| 109 | + } |
| 110 | + } |
| 111 | +} |
0 commit comments