Esempio n. 1
0
        public override IEnumerator RunTest()
        {
            if (Config.Instance.FindCredentials(m_Translate.GetServiceID()) == null)
            {
                yield break;
            }

            m_Translate.GetModel("en-es", OnGetModel);
            while (!m_GetModelTested)
            {
                yield return(null);
            }

            m_Translate.GetModels(OnGetModels);
            while (!m_GetModelsTested)
            {
                yield return(null);
            }

            m_Translate.GetLanguages(OnGetLanguages);
            while (!m_GetLanguagesTested)
            {
                yield return(null);
            }

            m_Translate.Identify("What does the fox say?", OnIdentify);
            while (!m_IdentifyTested)
            {
                yield return(null);
            }

            m_Translate.GetTranslation("What does the fox say?", "en", "es", OnGetTranslation);
            while (!m_TranslateTested)
            {
                yield return(null);
            }

            yield break;
        }
    private IEnumerator Examples()
    {
        if (!_languageTranslator.GetTranslation(OnGetTranslation, OnFail, _pharseToTranslate, "en", "es"))
        {
            Log.Debug("ExampleLanguageTranslator.GetTranslation()", "Failed to translate.");
        }
        while (!_getTranslationTested)
        {
            yield return(null);
        }

        if (!_languageTranslator.GetModels(OnGetModels, OnFail))
        {
            Log.Debug("ExampleLanguageTranslator.GetModels()", "Failed to get models.");
        }
        while (!_getModelsTested)
        {
            yield return(null);
        }

        if (!_languageTranslator.CreateModel(OnCreateModel, OnFail, _baseModelName, _customModelName, _forcedGlossaryFilePath))
        {
            Log.Debug("ExampleLanguageTranslator.CreateModel()", "Failed to create model.");
        }
        while (!_createModelTested)
        {
            yield return(null);
        }

        if (!_languageTranslator.GetModel(OnGetModel, OnFail, _customLanguageModelId))
        {
            Log.Debug("ExampleLanguageTranslator.GetModel()", "Failed to get model.");
        }
        while (!_getModelTested)
        {
            yield return(null);
        }

        if (!_languageTranslator.DeleteModel(OnDeleteModel, OnFail, _customLanguageModelId))
        {
            Log.Debug("ExampleLanguageTranslator.DeleteModel()", "Failed to delete model.");
        }
        while (!_deleteModelTested)
        {
            yield return(null);
        }

        if (!_languageTranslator.Identify(OnIdentify, OnFail, _pharseToIdentify))
        {
            Log.Debug("ExampleLanguageTranslator.Identify()", "Failed to identify language.");
        }
        while (!_identifyTested)
        {
            yield return(null);
        }

        if (!_languageTranslator.GetLanguages(OnGetLanguages, OnFail))
        {
            Log.Debug("ExampleLanguageTranslator.GetLanguages()", "Failed to get languages.");
        }
        while (!_getLanguagesTested)
        {
            yield return(null);
        }

        Log.Debug("ExampleLanguageTranslator.Examples()", "Language Translator examples complete.");
    }
Esempio n. 3
0
        public override IEnumerator RunTest()
        {
            LogSystem.InstallDefaultReactors();

            VcapCredentials vcapCredentials = new VcapCredentials();
            fsData          data            = null;

            string result = null;
            string credentialsFilepath = "../sdk-credentials/credentials.json";

            //  Load credentials file if it exists. If it doesn't exist, don't run the tests.
            if (File.Exists(credentialsFilepath))
            {
                result = File.ReadAllText(credentialsFilepath);
            }
            else
            {
                yield break;
            }

            //  Add in a parent object because Unity does not like to deserialize root level collection types.
            result = Utility.AddTopLevelObjectToJson(result, "VCAP_SERVICES");

            //  Convert json to fsResult
            fsResult r = fsJsonParser.Parse(result, out data);

            if (!r.Succeeded)
            {
                throw new WatsonException(r.FormattedMessages);
            }

            //  Convert fsResult to VcapCredentials
            object obj = vcapCredentials;

            r = _serializer.TryDeserialize(data, obj.GetType(), ref obj);
            if (!r.Succeeded)
            {
                throw new WatsonException(r.FormattedMessages);
            }

            //  Set credentials from imported credntials
            Credential credential = vcapCredentials.GetCredentialByname("language-translator-sdk")[0].Credentials;

            _url = credential.Url.ToString();

            //  Create credential and instantiate service
            TokenOptions tokenOptions = new TokenOptions()
            {
                IamApiKey = credential.IamApikey,
                IamUrl    = credential.IamUrl
            };

            Credentials credentials = new Credentials(tokenOptions, credential.Url);

            //  Wait for tokendata
            while (!credentials.HasIamTokenData())
            {
                yield return(null);
            }

            _languageTranslator = new LanguageTranslator(_versionDate, credentials);

            if (!_languageTranslator.GetTranslation(OnGetTranslation, OnFail, _pharseToTranslate, "en", "es"))
            {
                Log.Debug("TestLanguageTranslator.GetTranslation()", "Failed to translate.");
            }
            while (!_getTranslationTested)
            {
                yield return(null);
            }

            if (!_languageTranslator.GetModels(OnGetModels, OnFail))
            {
                Log.Debug("TestLanguageTranslator.GetModels()", "Failed to get models.");
            }
            while (!_getModelsTested)
            {
                yield return(null);
            }

            if (!_languageTranslator.GetModel(OnGetModel, OnFail, "en-es"))
            {
                Log.Debug("TestLanguageTranslator.GetModel()", "Failed to get model.");
            }
            while (!_getModelTested)
            {
                yield return(null);
            }

            if (!_languageTranslator.Identify(OnIdentify, OnFail, _pharseToTranslate))
            {
                Log.Debug("TestLanguageTranslator.Identify()", "Failed to identify language.");
            }
            while (!_identifyTested)
            {
                yield return(null);
            }

            if (!_languageTranslator.GetLanguages(OnGetLanguages, OnFail))
            {
                Log.Debug("TestLanguageTranslator.GetLanguages()", "Failed to get languages.");
            }
            while (!_getLanguagesTested)
            {
                yield return(null);
            }

            Log.Debug("TestLanguageTranslator.RunTest()", "Language Translator examples complete.");

            yield break;
        }
Esempio n. 4
0
        public override IEnumerator RunTest()
        {
            LogSystem.InstallDefaultReactors();

            try
            {
                VcapCredentials vcapCredentials = new VcapCredentials();
                fsData          data            = null;

                //  Get credentials from a credential file defined in environmental variables in the VCAP_SERVICES format.
                //  See https://www.ibm.com/watson/developercloud/doc/common/getting-started-variables.html.
                var environmentalVariable = Environment.GetEnvironmentVariable("VCAP_SERVICES");
                var fileContent           = File.ReadAllText(environmentalVariable);

                //  Add in a parent object because Unity does not like to deserialize root level collection types.
                fileContent = Utility.AddTopLevelObjectToJson(fileContent, "VCAP_SERVICES");

                //  Convert json to fsResult
                fsResult r = fsJsonParser.Parse(fileContent, out data);
                if (!r.Succeeded)
                {
                    throw new WatsonException(r.FormattedMessages);
                }

                //  Convert fsResult to VcapCredentials
                object obj = vcapCredentials;
                r = _serializer.TryDeserialize(data, obj.GetType(), ref obj);
                if (!r.Succeeded)
                {
                    throw new WatsonException(r.FormattedMessages);
                }

                //  Set credentials from imported credntials
                Credential credential = vcapCredentials.VCAP_SERVICES["language_translator"][TestCredentialIndex].Credentials;
                _username = credential.Username.ToString();
                _password = credential.Password.ToString();
                _url      = credential.Url.ToString();
            }
            catch
            {
                Log.Debug("TestLanguageTranslator", "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");
            }

            //  Create credential and instantiate service
            Credentials credentials = new Credentials(_username, _password, _url);

            //  Or authenticate using token
            //Credentials credentials = new Credentials(_url)
            //{
            //    AuthenticationToken = _token
            //};

            _languageTranslator = new LanguageTranslator(credentials);

            _forcedGlossaryFilePath = Application.dataPath + "/Watson/Examples/ServiceExamples/TestData/glossary.tmx";

            if (!_languageTranslator.GetTranslation(_pharseToTranslate, "en", "es", OnGetTranslation))
            {
                Log.Debug("TestLanguageTranslator", "Failed to translate.");
            }
            while (!_getTranslationTested)
            {
                yield return(null);
            }

            if (!_languageTranslator.GetModels(OnGetModels))
            {
                Log.Debug("TestLanguageTranslator", "Failed to get models.");
            }
            while (!_getModelsTested)
            {
                yield return(null);
            }

            if (!_languageTranslator.CreateModel(OnCreateModel, _baseModelName, _customModelName, _forcedGlossaryFilePath))
            {
                Log.Debug("TestLanguageTranslator", "Failed to create model.");
            }
            while (!_createModelTested)
            {
                yield return(null);
            }

            if (!_languageTranslator.GetModel(OnGetModel, _customLanguageModelId))
            {
                Log.Debug("TestLanguageTranslator", "Failed to get model.");
            }
            while (!_getModelTested)
            {
                yield return(null);
            }

            if (!_languageTranslator.DeleteModel(OnDeleteModel, _customLanguageModelId))
            {
                Log.Debug("TestLanguageTranslator", "Failed to delete model.");
            }
            while (!_deleteModelTested)
            {
                yield return(null);
            }

            if (!_languageTranslator.Identify(OnIdentify, _pharseToTranslate))
            {
                Log.Debug("TestLanguageTranslator", "Failed to identify language.");
            }
            while (!_identifyTested)
            {
                yield return(null);
            }

            if (!_languageTranslator.GetLanguages(OnGetLanguages))
            {
                Log.Debug("TestLanguageTranslator", "Failed to get languages.");
            }
            while (!_getLanguagesTested)
            {
                yield return(null);
            }

            Log.Debug("TestLanguageTranslator", "Language Translator examples complete.");

            yield break;
        }
        public override IEnumerator RunTest()
        {
            LogSystem.InstallDefaultReactors();

            VcapCredentials vcapCredentials = new VcapCredentials();
            fsData          data            = null;

            string result = null;
            string credentialsFilepath = "../sdk-credentials/credentials.json";

            //  Load credentials file if it exists. If it doesn't exist, don't run the tests.
            if (File.Exists(credentialsFilepath))
            {
                result = File.ReadAllText(credentialsFilepath);
            }
            else
            {
                yield break;
            }

            //  Add in a parent object because Unity does not like to deserialize root level collection types.
            result = Utility.AddTopLevelObjectToJson(result, "VCAP_SERVICES");

            //  Convert json to fsResult
            fsResult r = fsJsonParser.Parse(result, out data);

            if (!r.Succeeded)
            {
                throw new WatsonException(r.FormattedMessages);
            }

            //  Convert fsResult to VcapCredentials
            object obj = vcapCredentials;

            r = _serializer.TryDeserialize(data, obj.GetType(), ref obj);
            if (!r.Succeeded)
            {
                throw new WatsonException(r.FormattedMessages);
            }

            //  Set credentials from imported credntials
            Credential credential = vcapCredentials.GetCredentialByname("language-translator-sdk")[0].Credentials;

            _username = credential.Username.ToString();
            _password = credential.Password.ToString();
            _url      = credential.Url.ToString();

            //  Create credential and instantiate service
            Credentials credentials = new Credentials(_username, _password, _url);

            //  Or authenticate using token
            //Credentials credentials = new Credentials(_url)
            //{
            //    AuthenticationToken = _token
            //};

            _languageTranslator = new LanguageTranslator(credentials);

            _forcedGlossaryFilePath = Application.dataPath + "/Watson/Examples/ServiceExamples/TestData/glossary.tmx";

            if (!_languageTranslator.GetTranslation(OnGetTranslation, OnFail, _pharseToTranslate, "en", "es"))
            {
                Log.Debug("TestLanguageTranslator.GetTranslation()", "Failed to translate.");
            }
            while (!_getTranslationTested)
            {
                yield return(null);
            }

            if (!_languageTranslator.GetModels(OnGetModels, OnFail))
            {
                Log.Debug("TestLanguageTranslator.GetModels()", "Failed to get models.");
            }
            while (!_getModelsTested)
            {
                yield return(null);
            }

            if (!_languageTranslator.CreateModel(OnCreateModel, OnFail, _baseModelName, _customModelName, _forcedGlossaryFilePath))
            {
                Log.Debug("TestLanguageTranslator.CreateModel()", "Failed to create model.");
            }
            while (!_createModelTested)
            {
                yield return(null);
            }

            if (!_languageTranslator.GetModel(OnGetModel, OnFail, _customLanguageModelId))
            {
                Log.Debug("TestLanguageTranslator.GetModel()", "Failed to get model.");
            }
            while (!_getModelTested)
            {
                yield return(null);
            }

            if (!_languageTranslator.DeleteModel(OnDeleteModel, OnFail, _customLanguageModelId))
            {
                Log.Debug("TestLanguageTranslator.DeleteModel()", "Failed to delete model.");
            }
            while (!_deleteModelTested)
            {
                yield return(null);
            }

            if (!_languageTranslator.Identify(OnIdentify, OnFail, _pharseToTranslate))
            {
                Log.Debug("TestLanguageTranslator.Identify()", "Failed to identify language.");
            }
            while (!_identifyTested)
            {
                yield return(null);
            }

            if (!_languageTranslator.GetLanguages(OnGetLanguages, OnFail))
            {
                Log.Debug("TestLanguageTranslator.GetLanguages()", "Failed to get languages.");
            }
            while (!_getLanguagesTested)
            {
                yield return(null);
            }

            Log.Debug("TestLanguageTranslator.RunTest()", "Language Translator examples complete.");

            yield break;
        }
Esempio n. 6
0
        public override IEnumerator RunTest()
        {
            LogSystem.InstallDefaultReactors();

            VcapCredentials vcapCredentials = new VcapCredentials();
            fsData          data            = null;

            string result = null;

            var vcapUrl      = Environment.GetEnvironmentVariable("VCAP_URL");
            var vcapUsername = Environment.GetEnvironmentVariable("VCAP_USERNAME");
            var vcapPassword = Environment.GetEnvironmentVariable("VCAP_PASSWORD");

            using (SimpleGet simpleGet = new SimpleGet(vcapUrl, vcapUsername, vcapPassword))
            {
                while (!simpleGet.IsComplete)
                {
                    yield return(null);
                }

                result = simpleGet.Result;
            }

            //  Add in a parent object because Unity does not like to deserialize root level collection types.
            result = Utility.AddTopLevelObjectToJson(result, "VCAP_SERVICES");

            //  Convert json to fsResult
            fsResult r = fsJsonParser.Parse(result, out data);

            if (!r.Succeeded)
            {
                throw new WatsonException(r.FormattedMessages);
            }

            //  Convert fsResult to VcapCredentials
            object obj = vcapCredentials;

            r = _serializer.TryDeserialize(data, obj.GetType(), ref obj);
            if (!r.Succeeded)
            {
                throw new WatsonException(r.FormattedMessages);
            }

            //  Set credentials from imported credntials
            Credential credential = vcapCredentials.VCAP_SERVICES["language_translator"];

            _username = credential.Username.ToString();
            _password = credential.Password.ToString();
            _url      = credential.Url.ToString();

            //  Create credential and instantiate service
            Credentials credentials = new Credentials(_username, _password, _url);

            //  Or authenticate using token
            //Credentials credentials = new Credentials(_url)
            //{
            //    AuthenticationToken = _token
            //};

            _languageTranslator = new LanguageTranslator(credentials);

            _forcedGlossaryFilePath = Application.dataPath + "/Watson/Examples/ServiceExamples/TestData/glossary.tmx";

            if (!_languageTranslator.GetTranslation(OnGetTranslation, OnFail, _pharseToTranslate, "en", "es"))
            {
                Log.Debug("TestLanguageTranslator.GetTranslation()", "Failed to translate.");
            }
            while (!_getTranslationTested)
            {
                yield return(null);
            }

            if (!_languageTranslator.GetModels(OnGetModels, OnFail))
            {
                Log.Debug("TestLanguageTranslator.GetModels()", "Failed to get models.");
            }
            while (!_getModelsTested)
            {
                yield return(null);
            }

            if (!_languageTranslator.CreateModel(OnCreateModel, OnFail, _baseModelName, _customModelName, _forcedGlossaryFilePath))
            {
                Log.Debug("TestLanguageTranslator.CreateModel()", "Failed to create model.");
            }
            while (!_createModelTested)
            {
                yield return(null);
            }

            if (!_languageTranslator.GetModel(OnGetModel, OnFail, _customLanguageModelId))
            {
                Log.Debug("TestLanguageTranslator.GetModel()", "Failed to get model.");
            }
            while (!_getModelTested)
            {
                yield return(null);
            }

            if (!_languageTranslator.DeleteModel(OnDeleteModel, OnFail, _customLanguageModelId))
            {
                Log.Debug("TestLanguageTranslator.DeleteModel()", "Failed to delete model.");
            }
            while (!_deleteModelTested)
            {
                yield return(null);
            }

            if (!_languageTranslator.Identify(OnIdentify, OnFail, _pharseToTranslate))
            {
                Log.Debug("TestLanguageTranslator.Identify()", "Failed to identify language.");
            }
            while (!_identifyTested)
            {
                yield return(null);
            }

            if (!_languageTranslator.GetLanguages(OnGetLanguages, OnFail))
            {
                Log.Debug("TestLanguageTranslator.GetLanguages()", "Failed to get languages.");
            }
            while (!_getLanguagesTested)
            {
                yield return(null);
            }

            Log.Debug("TestLanguageTranslator.RunTest()", "Language Translator examples complete.");

            yield break;
        }