Skip to content

Commit 2bda500

Browse files
committed
test(Assistant v2): Add unit and integration tests
1 parent d2fd2d0 commit 2bda500

5 files changed

Lines changed: 745 additions & 0 deletions

File tree

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
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+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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.WatsonServiceTest;
16+
import org.junit.Assume;
17+
import org.junit.Before;
18+
19+
import java.util.Date;
20+
21+
public class AssistantServiceTest extends WatsonServiceTest {
22+
23+
private Assistant service;
24+
private String assistantId;
25+
26+
public Assistant getService() {
27+
return this.service;
28+
}
29+
30+
public String getAssistantId() {
31+
return this.assistantId;
32+
}
33+
34+
/*
35+
* (non-Javadoc)
36+
* @see com.ibm.watson.developer_cloud.WatsonServiceTest#setUp()
37+
*/
38+
@Override
39+
@Before
40+
public void setUp() throws Exception {
41+
super.setUp();
42+
String username = getProperty("assistant.v2.username");
43+
String password = getProperty("assistant.v2.password");
44+
assistantId = getProperty("assistant.v2.assistant_id");
45+
46+
Assume.assumeFalse("config.properties doesn't have valid credentials.",
47+
(username == null) || username.equals(PLACEHOLDER));
48+
49+
service = new Assistant("2018-07-10");
50+
service.setEndPoint(getProperty("assistant.v2.url"));
51+
service.setUsernameAndPassword(username, password);
52+
service.setDefaultHeaders(getDefaultHeaders());
53+
}
54+
55+
private long tolerance = 2000; // 2 secs in ms
56+
57+
/**
58+
* return `true` if ldate before rdate within tolerance.
59+
*/
60+
public boolean fuzzyBefore(Date ldate, Date rdate) {
61+
return (ldate.getTime() - rdate.getTime()) < tolerance;
62+
}
63+
64+
/**
65+
* return `true` if ldate after rdate within tolerance.
66+
*/
67+
public boolean fuzzyAfter(Date ldate, Date rdate) {
68+
return (rdate.getTime() - ldate.getTime()) < tolerance;
69+
}
70+
71+
}

0 commit comments

Comments
 (0)