|
| 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 FullSerializer; |
| 19 | +using IBM.Watson.DeveloperCloud.Connection; |
| 20 | +using IBM.Watson.DeveloperCloud.Logging; |
| 21 | +using IBM.Watson.DeveloperCloud.Services.LanguageTranslator.v3; |
| 22 | +using IBM.Watson.DeveloperCloud.Utilities; |
| 23 | +using System.Collections; |
| 24 | +using System.Collections.Generic; |
| 25 | +using System.IO; |
| 26 | +using UnityEngine; |
| 27 | + |
| 28 | +namespace IBM.Watson.DeveloperCloud.UnitTests |
| 29 | +{ |
| 30 | + public class TestLanguageTranslatorV3ApikeyAsBasicauth : UnitTest |
| 31 | + { |
| 32 | + private string _pharseToTranslate = "Hello, welcome to IBM Watson!"; |
| 33 | + private fsSerializer _serializer = new fsSerializer(); |
| 34 | + |
| 35 | + private LanguageTranslator _languageTranslator; |
| 36 | + |
| 37 | + private bool _getTranslationTested = false; |
| 38 | + private bool _getModelsTested = false; |
| 39 | + private bool _getModelTested = false; |
| 40 | + private bool _identifyTested = false; |
| 41 | + private bool _getLanguagesTested = false; |
| 42 | + private string _versionDate = "2018-05-01"; |
| 43 | + |
| 44 | + public override IEnumerator RunTest() |
| 45 | + { |
| 46 | + LogSystem.InstallDefaultReactors(); |
| 47 | + |
| 48 | + VcapCredentials vcapCredentials = new VcapCredentials(); |
| 49 | + fsData data = null; |
| 50 | + |
| 51 | + string result = null; |
| 52 | + string credentialsFilepath = "../sdk-credentials/credentials.json"; |
| 53 | + |
| 54 | + // Load credentials file if it exists. If it doesn't exist, don't run the tests. |
| 55 | + if (File.Exists(credentialsFilepath)) |
| 56 | + result = File.ReadAllText(credentialsFilepath); |
| 57 | + else |
| 58 | + yield break; |
| 59 | + |
| 60 | + // Add in a parent object because Unity does not like to deserialize root level collection types. |
| 61 | + result = Utility.AddTopLevelObjectToJson(result, "VCAP_SERVICES"); |
| 62 | + |
| 63 | + // Convert json to fsResult |
| 64 | + fsResult r = fsJsonParser.Parse(result, out data); |
| 65 | + if (!r.Succeeded) |
| 66 | + throw new WatsonException(r.FormattedMessages); |
| 67 | + |
| 68 | + // Convert fsResult to VcapCredentials |
| 69 | + object obj = vcapCredentials; |
| 70 | + r = _serializer.TryDeserialize(data, obj.GetType(), ref obj); |
| 71 | + if (!r.Succeeded) |
| 72 | + throw new WatsonException(r.FormattedMessages); |
| 73 | + |
| 74 | + // Set credentials from imported credntials |
| 75 | + Credential credential = vcapCredentials.GetCredentialByname("language-translator-v3-sdk-rc-wdc")[0].Credentials; |
| 76 | + _url = credential.Url.ToString(); |
| 77 | + |
| 78 | + Credentials credentials = new Credentials("apikey", credential.IamApikey, credential.Url); |
| 79 | + |
| 80 | + // Wait for tokendata |
| 81 | + while (!credentials.HasIamTokenData()) |
| 82 | + yield return null; |
| 83 | + |
| 84 | + _languageTranslator = new LanguageTranslator(_versionDate, credentials); |
| 85 | + |
| 86 | + if (!_languageTranslator.GetTranslation(OnGetTranslation, OnFail, _pharseToTranslate, "en", "es")) |
| 87 | + Log.Debug("TestLanguageTranslator.GetTranslation()", "Failed to translate."); |
| 88 | + while (!_getTranslationTested) |
| 89 | + yield return null; |
| 90 | + |
| 91 | + if (!_languageTranslator.GetModels(OnGetModels, OnFail)) |
| 92 | + Log.Debug("TestLanguageTranslator.GetModels()", "Failed to get models."); |
| 93 | + while (!_getModelsTested) |
| 94 | + yield return null; |
| 95 | + |
| 96 | + if (!_languageTranslator.GetModel(OnGetModel, OnFail, "en-es")) |
| 97 | + Log.Debug("TestLanguageTranslator.GetModel()", "Failed to get model."); |
| 98 | + while (!_getModelTested) |
| 99 | + yield return null; |
| 100 | + |
| 101 | + if (!_languageTranslator.Identify(OnIdentify, OnFail, _pharseToTranslate)) |
| 102 | + Log.Debug("TestLanguageTranslator.Identify()", "Failed to identify language."); |
| 103 | + while (!_identifyTested) |
| 104 | + yield return null; |
| 105 | + |
| 106 | + if (!_languageTranslator.GetLanguages(OnGetLanguages, OnFail)) |
| 107 | + Log.Debug("TestLanguageTranslator.GetLanguages()", "Failed to get languages."); |
| 108 | + while (!_getLanguagesTested) |
| 109 | + yield return null; |
| 110 | + |
| 111 | + Log.Debug("TestLanguageTranslator.RunTest()", "Language Translator examples complete."); |
| 112 | + |
| 113 | + yield break; |
| 114 | + } |
| 115 | + |
| 116 | + private void OnGetModels(TranslationModels models, Dictionary<string, object> customData) |
| 117 | + { |
| 118 | + Log.Debug("TestLanguageTranslator.OnGetModels()", "Language Translator - Get models response: {0}", customData["json"].ToString()); |
| 119 | + Test(models != null); |
| 120 | + _getModelsTested = true; |
| 121 | + } |
| 122 | + |
| 123 | + private void OnGetModel(TranslationModel model, Dictionary<string, object> customData) |
| 124 | + { |
| 125 | + Log.Debug("TestLanguageTranslator.OnGetModel()", "Language Translator - Get model response: {0}", customData["json"].ToString()); |
| 126 | + Test(model != null); |
| 127 | + _getModelTested = true; |
| 128 | + } |
| 129 | + |
| 130 | + private void OnGetTranslation(Translations translation, Dictionary<string, object> customData) |
| 131 | + { |
| 132 | + Log.Debug("TestLanguageTranslator.OnGetTranslation()", "Langauge Translator - Translate Response: {0}", customData["json"].ToString()); |
| 133 | + Test(translation != null); |
| 134 | + _getTranslationTested = true; |
| 135 | + } |
| 136 | + |
| 137 | + private void OnIdentify(IdentifiedLanguages identifiedLanguages, Dictionary<string, object> customData) |
| 138 | + { |
| 139 | + Log.Debug("TestLanguageTranslator.OnIdentify()", "Language Translator - Identify response: {0}", customData["json"].ToString()); |
| 140 | + Test(identifiedLanguages != null); |
| 141 | + _identifyTested = true; |
| 142 | + } |
| 143 | + |
| 144 | + private void OnGetLanguages(Languages languages, Dictionary<string, object> customData) |
| 145 | + { |
| 146 | + Log.Debug("TestLanguageTranslator.OnGetLanguages()", "Language Translator - Get languages response: {0}", customData["json"].ToString()); |
| 147 | + Test(languages != null); |
| 148 | + _getLanguagesTested = true; |
| 149 | + } |
| 150 | + |
| 151 | + private void OnFail(RESTConnector.Error error, Dictionary<string, object> customData) |
| 152 | + { |
| 153 | + Log.Error("TestLanguageTranslator.OnFail()", "Error received: {0}", error.ToString()); |
| 154 | + } |
| 155 | + } |
| 156 | +} |
0 commit comments