|
| 1 | +/** |
| 2 | +* Copyright 2015 IBM Corp. All Rights Reserved. |
| 3 | +* |
| 4 | +* Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +* you may not use this file except in compliance with the License. |
| 6 | +* You may obtain a copy of the License at |
| 7 | +* |
| 8 | +* http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +* |
| 10 | +* Unless required by applicable law or agreed to in writing, software |
| 11 | +* distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +* See the License for the specific language governing permissions and |
| 14 | +* limitations under the License. |
| 15 | +* |
| 16 | +*/ |
| 17 | + |
| 18 | +using System.Collections; |
| 19 | +using System.Collections.Generic; |
| 20 | +using IBM.Watson.DeveloperCloud.UnitTests; |
| 21 | +using IBM.Watson.DeveloperCloud.Utilities; |
| 22 | +using IBM.Watson.DeveloperCloud.Logging; |
| 23 | +using FullSerializer; |
| 24 | +using System.IO; |
| 25 | +using System; |
| 26 | +using IBM.Watson.DeveloperCloud.Connection; |
| 27 | +using IBM.Watson.DeveloperCloud.Services.Assistant.v1; |
| 28 | + |
| 29 | +namespace IBM.Watson.DeveloperCloud.UnitTests |
| 30 | +{ |
| 31 | + public class TestAssistant : UnitTest |
| 32 | + { |
| 33 | + private string _username = null; |
| 34 | + private string _password = null; |
| 35 | + private string _workspaceId; |
| 36 | + |
| 37 | + private Assistant _service; |
| 38 | + private string _assistantVersionDate = "2017-05-26"; |
| 39 | + |
| 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; |
| 45 | + |
| 46 | + public override IEnumerator RunTest() |
| 47 | + { |
| 48 | + LogSystem.InstallDefaultReactors(); |
| 49 | + |
| 50 | + VcapCredentials vcapCredentials = new VcapCredentials(); |
| 51 | + fsData data = null; |
| 52 | + |
| 53 | + string result = null; |
| 54 | + |
| 55 | + var vcapUrl = Environment.GetEnvironmentVariable("VCAP_URL"); |
| 56 | + var vcapUsername = Environment.GetEnvironmentVariable("VCAP_USERNAME"); |
| 57 | + var vcapPassword = Environment.GetEnvironmentVariable("VCAP_PASSWORD"); |
| 58 | + |
| 59 | + using (SimpleGet simpleGet = new SimpleGet(vcapUrl, vcapUsername, vcapPassword)) |
| 60 | + { |
| 61 | + while (!simpleGet.IsComplete) |
| 62 | + yield return null; |
| 63 | + |
| 64 | + result = simpleGet.Result; |
| 65 | + } |
| 66 | + |
| 67 | + // Add in a parent object because Unity does not like to deserialize root level collection types. |
| 68 | + result = Utility.AddTopLevelObjectToJson(result, "VCAP_SERVICES"); |
| 69 | + |
| 70 | + // Convert json to fsResult |
| 71 | + fsResult r = fsJsonParser.Parse(result, out data); |
| 72 | + if (!r.Succeeded) |
| 73 | + throw new WatsonException(r.FormattedMessages); |
| 74 | + |
| 75 | + // Convert fsResult to VcapCredentials |
| 76 | + object obj = vcapCredentials; |
| 77 | + r = _serializer.TryDeserialize(data, obj.GetType(), ref obj); |
| 78 | + if (!r.Succeeded) |
| 79 | + throw new WatsonException(r.FormattedMessages); |
| 80 | + |
| 81 | + // Set credentials from imported credntials |
| 82 | + Credential credential = vcapCredentials.VCAP_SERVICES["conversation"]; |
| 83 | + _username = credential.Username.ToString(); |
| 84 | + _password = credential.Password.ToString(); |
| 85 | + _url = credential.Url.ToString(); |
| 86 | + _workspaceId = credential.WorkspaceId.ToString(); |
| 87 | + |
| 88 | + // Create credential and instantiate service |
| 89 | + Credentials credentials = new Credentials(_username, _password, _url); |
| 90 | + |
| 91 | + _service = new Assistant(credentials); |
| 92 | + _service.VersionDate = _assistantVersionDate; |
| 93 | + |
| 94 | + |
| 95 | + |
| 96 | + //// Test initate with empty string |
| 97 | + //if (!_service.Message(OnSuccess, OnFail, _workspaceId, "")) |
| 98 | + // Log.Debug("TestAssistant.RunTest()", "Failed to message!"); |
| 99 | + |
| 100 | + //// Test initiate with empty string message object |
| 101 | + //MessageRequest messageRequest = new MessageRequest() |
| 102 | + //{ |
| 103 | + // input = new Dictionary<string, object>() |
| 104 | + //{ |
| 105 | + // { "text", "" } |
| 106 | + //}, |
| 107 | + // context = _context |
| 108 | + //}; |
| 109 | + |
| 110 | + //if (!_service.Message(OnSuccess, OnFail, _workspaceId, messageRequest)) |
| 111 | + // Log.Debug("TestAssistant.RunTest()", "Failed to message!"); |
| 112 | + |
| 113 | + //if (!_service.Message(OnSuccess, OnFail, _workspaceId, "hello")) |
| 114 | + // Log.Debug("TestAssistant.RunTest()", "Failed to message!"); |
| 115 | + |
| 116 | + //while (_waitingForResponse) |
| 117 | + // yield return null; |
| 118 | + |
| 119 | + //_waitingForResponse = true; |
| 120 | + //_questionCount++; |
| 121 | + |
| 122 | + //AskQuestion(); |
| 123 | + //while (_waitingForResponse) |
| 124 | + // yield return null; |
| 125 | + |
| 126 | + //_questionCount++; |
| 127 | + |
| 128 | + //_waitingForResponse = true; |
| 129 | + |
| 130 | + //AskQuestion(); |
| 131 | + //while (_waitingForResponse) |
| 132 | + // yield return null; |
| 133 | + //_questionCount++; |
| 134 | + |
| 135 | + //_waitingForResponse = true; |
| 136 | + |
| 137 | + //AskQuestion(); |
| 138 | + //while (_waitingForResponse) |
| 139 | + // yield return null; |
| 140 | + //_questionCount++; |
| 141 | + |
| 142 | + //_waitingForResponse = true; |
| 143 | + |
| 144 | + //AskQuestion(); |
| 145 | + //while (_waitingForResponse) |
| 146 | + // yield return null; |
| 147 | + |
| 148 | + //Log.Debug("TestAssistant.RunTest()", "Conversation examples complete."); |
| 149 | + |
| 150 | + yield break; |
| 151 | + } |
| 152 | + |
| 153 | + //private void AskQuestion() |
| 154 | + //{ |
| 155 | + // MessageRequest messageRequest = new MessageRequest() |
| 156 | + // { |
| 157 | + // input = new Dictionary<string, object>() |
| 158 | + // { |
| 159 | + // { "text", _questionArray[_questionCount] } |
| 160 | + // }, |
| 161 | + // context = _context |
| 162 | + // }; |
| 163 | + |
| 164 | + // if (!_service.Message(OnSuccess, OnFail, _workspaceId, messageRequest)) |
| 165 | + // Log.Debug("TestAssistant.AskQuestion()", "Failed to message!"); |
| 166 | + //} |
| 167 | + |
| 168 | + //private void OnSuccess(object resp, Dictionary<string, object> customData) |
| 169 | + //{ |
| 170 | + // Log.Debug("TestAssistant.OnMessage()", "Conversation: Message Response: {0}", customData["json"].ToString()); |
| 171 | + |
| 172 | + // // Convert resp to fsdata |
| 173 | + // fsData fsdata = null; |
| 174 | + // fsResult r = _serializer.TrySerialize(resp.GetType(), resp, out fsdata); |
| 175 | + // if (!r.Succeeded) |
| 176 | + // throw new WatsonException(r.FormattedMessages); |
| 177 | + |
| 178 | + // // Convert fsdata to MessageResponse |
| 179 | + // MessageResponse messageResponse = new MessageResponse(); |
| 180 | + // object obj = messageResponse; |
| 181 | + // r = _serializer.TryDeserialize(fsdata, obj.GetType(), ref obj); |
| 182 | + // if (!r.Succeeded) |
| 183 | + // throw new WatsonException(r.FormattedMessages); |
| 184 | + |
| 185 | + // object _tempContext = null; |
| 186 | + // // Set context for next round of messaging |
| 187 | + // (resp as Dictionary<string, object>).TryGetValue("context", out _tempContext); |
| 188 | + |
| 189 | + // if (_tempContext != null) |
| 190 | + // _context = _tempContext as Dictionary<string, object>; |
| 191 | + // else |
| 192 | + // Log.Debug("TestAssistant.OnMessage()", "Failed to get context"); |
| 193 | + |
| 194 | + // Test(messageResponse != null); |
| 195 | + // _waitingForResponse = false; |
| 196 | + //} |
| 197 | + |
| 198 | + private void OnFail(RESTConnector.Error error, Dictionary<string, object> customData) |
| 199 | + { |
| 200 | + Log.Error("TestAssistant.OnFail()", "Error received: {0}", error.ToString()); |
| 201 | + } |
| 202 | + } |
| 203 | +} |
0 commit comments