Skip to content

Commit 6ea7bc8

Browse files
committed
added namespace for Conversation test
1 parent aadd5fc commit 6ea7bc8

File tree

1 file changed

+119
-116
lines changed

1 file changed

+119
-116
lines changed

Scripts/UnitTests/TestConversation.cs

Lines changed: 119 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -25,149 +25,152 @@
2525
using System.IO;
2626
using System;
2727

28-
public class TestConversation : UnitTest
28+
namespace IBM.Watson.DeveloperCloud.UnitTests
2929
{
30-
private string _username;
31-
private string _password;
32-
private string _workspaceId;
33-
//private string _token = "<authentication-token>";
34-
35-
private Conversation _conversation;
36-
private string _conversationVersionDate = "2017-05-26";
37-
38-
private string[] _questionArray = { "can you turn up the AC", "can you turn on the wipers", "can you turn off the wipers", "can you turn down the ac", "can you unlock the door" };
39-
private fsSerializer _serializer = new fsSerializer();
40-
private Dictionary<string, object> _context = null;
41-
private int _questionCount = -1;
42-
private bool _waitingForResponse = true;
43-
44-
public override IEnumerator RunTest()
30+
public class TestConversation : UnitTest
4531
{
46-
LogSystem.InstallDefaultReactors();
47-
try
48-
{
49-
VcapCredentials vcapCredentials = new VcapCredentials();
50-
fsData data = null;
32+
private string _username;
33+
private string _password;
34+
private string _workspaceId;
35+
//private string _token = "<authentication-token>";
5136

52-
// Get credentials from a credential file defined in environmental variables in the VCAP_SERVICES format.
53-
// See https://www.ibm.com/watson/developercloud/doc/common/getting-started-variables.html.
54-
var environmentalVariable = Environment.GetEnvironmentVariable("VCAP_SERVICES");
55-
var fileContent = File.ReadAllText(environmentalVariable);
56-
57-
// Add in a parent object because Unity does not like to deserialize root level collection types.
58-
fileContent = Utility.AddTopLevelObjectToJson(fileContent, "VCAP_SERVICES");
59-
60-
// Convert json to fsResult
61-
fsResult r = fsJsonParser.Parse(fileContent, out data);
62-
if (!r.Succeeded)
63-
throw new WatsonException(r.FormattedMessages);
37+
private Conversation _conversation;
38+
private string _conversationVersionDate = "2017-05-26";
6439

65-
// Convert fsResult to VcapCredentials
66-
object obj = vcapCredentials;
67-
r = _serializer.TryDeserialize(data, obj.GetType(), ref obj);
68-
if (!r.Succeeded)
69-
throw new WatsonException(r.FormattedMessages);
40+
private string[] _questionArray = { "can you turn up the AC", "can you turn on the wipers", "can you turn off the wipers", "can you turn down the ac", "can you unlock the door" };
41+
private fsSerializer _serializer = new fsSerializer();
42+
private Dictionary<string, object> _context = null;
43+
private int _questionCount = -1;
44+
private bool _waitingForResponse = true;
7045

71-
// Set credentials from imported credntials
72-
Credential credential = vcapCredentials.VCAP_SERVICES["conversation"][TestCredentialIndex].Credentials;
73-
_username = credential.Username.ToString();
74-
_password = credential.Password.ToString();
75-
_url = credential.Url.ToString();
76-
_workspaceId = credential.WorkspaceId.ToString();
77-
}
78-
catch
46+
public override IEnumerator RunTest()
7947
{
80-
Log.Debug("TestConversation", "Failed to get credentials from VCAP_SERVICES file. Please configure credentials to run this test. For more information, see: https://github.com/watson-developer-cloud/unity-sdk/#authentication");
81-
}
48+
LogSystem.InstallDefaultReactors();
49+
try
50+
{
51+
VcapCredentials vcapCredentials = new VcapCredentials();
52+
fsData data = null;
53+
54+
// Get credentials from a credential file defined in environmental variables in the VCAP_SERVICES format.
55+
// See https://www.ibm.com/watson/developercloud/doc/common/getting-started-variables.html.
56+
var environmentalVariable = Environment.GetEnvironmentVariable("VCAP_SERVICES");
57+
var fileContent = File.ReadAllText(environmentalVariable);
58+
59+
// Add in a parent object because Unity does not like to deserialize root level collection types.
60+
fileContent = Utility.AddTopLevelObjectToJson(fileContent, "VCAP_SERVICES");
61+
62+
// Convert json to fsResult
63+
fsResult r = fsJsonParser.Parse(fileContent, out data);
64+
if (!r.Succeeded)
65+
throw new WatsonException(r.FormattedMessages);
66+
67+
// Convert fsResult to VcapCredentials
68+
object obj = vcapCredentials;
69+
r = _serializer.TryDeserialize(data, obj.GetType(), ref obj);
70+
if (!r.Succeeded)
71+
throw new WatsonException(r.FormattedMessages);
72+
73+
// Set credentials from imported credntials
74+
Credential credential = vcapCredentials.VCAP_SERVICES["conversation"][TestCredentialIndex].Credentials;
75+
_username = credential.Username.ToString();
76+
_password = credential.Password.ToString();
77+
_url = credential.Url.ToString();
78+
_workspaceId = credential.WorkspaceId.ToString();
79+
}
80+
catch
81+
{
82+
Log.Debug("TestConversation", "Failed to get credentials from VCAP_SERVICES file. Please configure credentials to run this test. For more information, see: https://github.com/watson-developer-cloud/unity-sdk/#authentication");
83+
}
8284

83-
// Create credential and instantiate service
84-
Credentials credentials = new Credentials(_username, _password, _url);
85+
// Create credential and instantiate service
86+
Credentials credentials = new Credentials(_username, _password, _url);
8587

86-
_conversation = new Conversation(credentials);
87-
_conversation.VersionDate = _conversationVersionDate;
88+
_conversation = new Conversation(credentials);
89+
_conversation.VersionDate = _conversationVersionDate;
8890

89-
if (!_conversation.Message(OnMessage, _workspaceId, "hello"))
90-
Log.Debug("ExampleConversation", "Failed to message!");
91+
if (!_conversation.Message(OnMessage, _workspaceId, "hello"))
92+
Log.Debug("ExampleConversation", "Failed to message!");
9193

92-
while (_waitingForResponse)
93-
yield return null;
94+
while (_waitingForResponse)
95+
yield return null;
9496

95-
_waitingForResponse = true;
96-
_questionCount++;
97+
_waitingForResponse = true;
98+
_questionCount++;
9799

98-
AskQuestion();
99-
while (_waitingForResponse)
100-
yield return null;
100+
AskQuestion();
101+
while (_waitingForResponse)
102+
yield return null;
101103

102-
_questionCount++;
104+
_questionCount++;
103105

104-
_waitingForResponse = true;
106+
_waitingForResponse = true;
105107

106-
AskQuestion();
107-
while (_waitingForResponse)
108-
yield return null;
109-
_questionCount++;
108+
AskQuestion();
109+
while (_waitingForResponse)
110+
yield return null;
111+
_questionCount++;
110112

111-
_waitingForResponse = true;
113+
_waitingForResponse = true;
112114

113-
AskQuestion();
114-
while (_waitingForResponse)
115-
yield return null;
116-
_questionCount++;
115+
AskQuestion();
116+
while (_waitingForResponse)
117+
yield return null;
118+
_questionCount++;
117119

118-
_waitingForResponse = true;
120+
_waitingForResponse = true;
119121

120-
AskQuestion();
121-
while (_waitingForResponse)
122-
yield return null;
122+
AskQuestion();
123+
while (_waitingForResponse)
124+
yield return null;
123125

124-
Log.Debug("ExampleConversation", "Conversation examples complete.");
126+
Log.Debug("ExampleConversation", "Conversation examples complete.");
125127

126-
yield break;
127-
}
128+
yield break;
129+
}
128130

129-
private void AskQuestion()
130-
{
131-
MessageRequest messageRequest = new MessageRequest()
131+
private void AskQuestion()
132132
{
133-
input = new Dictionary<string, object>()
133+
MessageRequest messageRequest = new MessageRequest()
134+
{
135+
input = new Dictionary<string, object>()
134136
{
135137
{ "text", _questionArray[_questionCount] }
136138
},
137-
context = _context
138-
};
139+
context = _context
140+
};
139141

140-
if (!_conversation.Message(OnMessage, _workspaceId, messageRequest))
141-
Log.Debug("ExampleConversation", "Failed to message!");
142-
}
142+
if (!_conversation.Message(OnMessage, _workspaceId, messageRequest))
143+
Log.Debug("ExampleConversation", "Failed to message!");
144+
}
143145

144-
private void OnMessage(object resp, string data)
145-
{
146-
Log.Debug("ExampleConversation", "Conversation: Message Response: {0}", data);
147-
148-
// Convert resp to fsdata
149-
fsData fsdata = null;
150-
fsResult r = _serializer.TrySerialize(resp.GetType(), resp, out fsdata);
151-
if (!r.Succeeded)
152-
throw new WatsonException(r.FormattedMessages);
153-
154-
// Convert fsdata to MessageResponse
155-
MessageResponse messageResponse = new MessageResponse();
156-
object obj = messageResponse;
157-
r = _serializer.TryDeserialize(fsdata, obj.GetType(), ref obj);
158-
if (!r.Succeeded)
159-
throw new WatsonException(r.FormattedMessages);
160-
161-
object _tempContext = null;
162-
// Set context for next round of messaging
163-
(resp as Dictionary<string, object>).TryGetValue("context", out _tempContext);
164-
165-
if (_tempContext != null)
166-
_context = _tempContext as Dictionary<string, object>;
167-
else
168-
Log.Debug("ExampleConversation", "Failed to get context");
169-
170-
Test(messageResponse != null);
171-
_waitingForResponse = false;
146+
private void OnMessage(object resp, string data)
147+
{
148+
Log.Debug("ExampleConversation", "Conversation: Message Response: {0}", data);
149+
150+
// Convert resp to fsdata
151+
fsData fsdata = null;
152+
fsResult r = _serializer.TrySerialize(resp.GetType(), resp, out fsdata);
153+
if (!r.Succeeded)
154+
throw new WatsonException(r.FormattedMessages);
155+
156+
// Convert fsdata to MessageResponse
157+
MessageResponse messageResponse = new MessageResponse();
158+
object obj = messageResponse;
159+
r = _serializer.TryDeserialize(fsdata, obj.GetType(), ref obj);
160+
if (!r.Succeeded)
161+
throw new WatsonException(r.FormattedMessages);
162+
163+
object _tempContext = null;
164+
// Set context for next round of messaging
165+
(resp as Dictionary<string, object>).TryGetValue("context", out _tempContext);
166+
167+
if (_tempContext != null)
168+
_context = _tempContext as Dictionary<string, object>;
169+
else
170+
Log.Debug("ExampleConversation", "Failed to get context");
171+
172+
Test(messageResponse != null);
173+
_waitingForResponse = false;
174+
}
172175
}
173176
}

0 commit comments

Comments
 (0)