Example #1
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("discovery-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);

            _discovery             = new Discovery(credentials);
            _discovery.VersionDate = _discoveryVersionDate;
            _filePathToIngest      = Application.dataPath + "/Watson/Examples/ServiceExamples/TestData/Discovery/constitution.pdf";
            _documentFilePath      = Application.dataPath + "/Watson/Examples/ServiceExamples/TestData/Discovery/constitution.pdf";

            //  Get Environments
            Log.Debug("TestDiscovery.RunTest()", "Attempting to get environments");
            if (!_discovery.GetEnvironments(OnGetEnvironments, OnFail))
            {
                Log.Debug("TestDiscovery.GetEnvironments()", "Failed to get environments");
            }
            while (!_getEnvironmentsTested)
            {
                yield return(null);
            }

            //  Wait for environment to be ready
            Runnable.Run(CheckEnvironmentState(0f));
            while (!_isEnvironmentReady)
            {
                yield return(null);
            }

            //  GetEnvironment
            Log.Debug("TestDiscovery.RunTest()", "Attempting to get environment");
            if (!_discovery.GetEnvironment(OnGetEnvironment, OnFail, _environmentId))
            {
                Log.Debug("TestDiscovery.GetEnvironment()", "Failed to get environment");
            }
            while (!_getEnvironmentTested)
            {
                yield return(null);
            }

            //  Get Configurations
            Log.Debug("TestDiscovery.RunTest()", "Attempting to get configurations");
            if (!_discovery.GetConfigurations(OnGetConfigurations, OnFail, _environmentId))
            {
                Log.Debug("TestDiscovery.GetConfigurations()", "Failed to get configurations");
            }
            while (!_getConfigurationsTested)
            {
                yield return(null);
            }

            //  Add Configuration
            Log.Debug("TestDiscovery.RunTest()", "Attempting to add configuration");
            if (!_discovery.AddConfiguration(OnAddConfiguration, OnFail, _environmentId, _configurationJson.Replace("{guid}", System.Guid.NewGuid().ToString())))
            {
                Log.Debug("TestDiscovery.AddConfiguration()", "Failed to add configuration");
            }
            while (!_addConfigurationTested)
            {
                yield return(null);
            }

            //  Get Configuration
            Log.Debug("TestDiscovery.RunTest()", "Attempting to get configuration");
            if (!_discovery.GetConfiguration(OnGetConfiguration, OnFail, _environmentId, _createdConfigurationID))
            {
                Log.Debug("TestDiscovery.GetConfiguration()", "Failed to get configuration");
            }
            while (!_getConfigurationTested)
            {
                yield return(null);
            }

            //  Preview Configuration
            Log.Debug("TestDiscovery.RunTest()", "Attempting to preview configuration");
            if (!_discovery.PreviewConfiguration(OnPreviewConfiguration, OnFail, _environmentId, _createdConfigurationID, null, _filePathToIngest, _metadata))
            {
                Log.Debug("TestDiscovery.PreviewConfiguration()", "Failed to preview configuration");
            }
            while (!_previewConfigurationTested)
            {
                yield return(null);
            }

            //  Get Collections
            Log.Debug("TestDiscovery.RunTest()", "Attempting to get collections");
            if (!_discovery.GetCollections(OnGetCollections, OnFail, _environmentId))
            {
                Log.Debug("TestDiscovery.GetCollections()", "Failed to get collections");
            }
            while (!_getCollectionsTested)
            {
                yield return(null);
            }

            //  Add Collection
            Log.Debug("TestDiscovery.RunTest()", "Attempting to add collection");
            if (!_discovery.AddCollection(OnAddCollection, OnFail, _environmentId, _createdCollectionName + System.Guid.NewGuid().ToString(), _createdCollectionDescription, _createdConfigurationID))
            {
                Log.Debug("TestDiscovery.AddCollection()", "Failed to add collection");
            }
            while (!_addCollectionTested)
            {
                yield return(null);
            }

            //  Get Collection
            Log.Debug("TestDiscovery.RunTest()", "Attempting to get collection");
            if (!_discovery.GetCollection(OnGetCollection, OnFail, _environmentId, _createdCollectionId))
            {
                Log.Debug("TestDiscovery.GetCollection()", "Failed to get collection");
            }
            while (!_getCollectionTested)
            {
                yield return(null);
            }

            if (!_discovery.GetFields(OnGetFields, OnFail, _environmentId, _createdCollectionId))
            {
                Log.Debug("TestDiscovery.GetFields()", "Failed to get fields");
            }
            while (!_getFieldsTested)
            {
                yield return(null);
            }

            //  Add Document
            Log.Debug("TestDiscovery.RunTest()", "Attempting to add document");
            if (!_discovery.AddDocument(OnAddDocument, OnFail, _environmentId, _createdCollectionId, _documentFilePath, _createdConfigurationID, null))
            {
                Log.Debug("TestDiscovery.AddDocument()", "Failed to add document");
            }
            while (!_addDocumentTested)
            {
                yield return(null);
            }

            //  Get Document
            Log.Debug("TestDiscovery.RunTest()", "Attempting to get document");
            if (!_discovery.GetDocument(OnGetDocument, OnFail, _environmentId, _createdCollectionId, _createdDocumentID))
            {
                Log.Debug("TestDiscovery.GetDocument()", "Failed to get document");
            }
            while (!_getDocumentTested)
            {
                yield return(null);
            }

            //  Update Document
            Log.Debug("TestDiscovery.RunTest()", "Attempting to update document");
            if (!_discovery.UpdateDocument(OnUpdateDocument, OnFail, _environmentId, _createdCollectionId, _createdDocumentID, _documentFilePath, _createdConfigurationID, null))
            {
                Log.Debug("TestDiscovery.UpdateDocument()", "Failed to update document");
            }
            while (!_updateDocumentTested)
            {
                yield return(null);
            }

            //  Query
            Log.Debug("TestDiscovery.RunTest()", "Attempting to query");
            if (!_discovery.Query(OnQuery, OnFail, _environmentId, _createdCollectionId, naturalLanguageQuery: _query))
            {
                while (!_queryTested)
                {
                    yield return(null);
                }
            }

            //  List Credentials
            Log.Debug("TestDiscovery.RunTest()", "Attempting to list credentials");
            _discovery.ListCredentials(OnListCredentials, OnFail, _environmentId);
            while (!_listCredentialsTested)
            {
                yield return(null);
            }

            //  Create Credentials
            Log.Debug("TestDiscovery.RunTest()", "Attempting to create credentials");
            SourceCredentials credentialsParameter = new SourceCredentials()
            {
                SourceType        = SourceCredentials.SourceTypeEnum.box,
                CredentialDetails = new CredentialDetails()
                {
                    CredentialType = CredentialDetails.CredentialTypeEnum.oauth2,
                    EnterpriseId   = "myEnterpriseId",
                    ClientId       = "myClientId",
                    ClientSecret   = "myClientSecret",
                    PublicKeyId    = "myPublicIdKey",
                    Passphrase     = "myPassphrase",
                    PrivateKey     = "myPrivateKey"
                }
            };

            _discovery.CreateCredentials(OnCreateCredentials, OnFail, _environmentId, credentialsParameter);
            while (!_createCredentialsTested)
            {
                yield return(null);
            }

            //  Get Credential
            Log.Debug("TestDiscovery.RunTest()", "Attempting to get credential");
            _discovery.GetCredential(OnGetCredential, OnFail, _environmentId, _createdCredentialId);
            while (!_getCredentialTested)
            {
                yield return(null);
            }

            //  Get metrics event rate
            Log.Debug("TestDiscovery.RunTest()", "Attempting to Get metrics event rate");
            _discovery.GetMetricsEventRate(OnGetMetricsEventRate, OnFail);
            while (!_getMetricsEventRateTested)
            {
                yield return(null);
            }

            //  Get metrics query
            Log.Debug("TestDiscovery.RunTest()", "Attempting to Get metrics query");
            _discovery.GetMetricsQuery(OnGetMetricsQuery, OnFail);
            while (!_getMetricsQueryTested)
            {
                yield return(null);
            }

            //  Get metrics query event
            Log.Debug("TestDiscovery.RunTest()", "Attempting to Get metrics query event");
            _discovery.GetMetricsQueryEvent(OnGetMetricsQueryEvent, OnFail);
            while (!_getMetricsQueryEventTested)
            {
                yield return(null);
            }

            //  Get metrics query no result
            Log.Debug("TestDiscovery.RunTest()", "Attempting to Get metrics query no result");
            _discovery.GetMetricsQueryNoResults(OnGetMetricsQueryNoResult, OnFail);
            while (!_getMetricsQueryNoResultTested)
            {
                yield return(null);
            }

            //  Get metrics query token event
            Log.Debug("TestDiscovery.RunTest()", "Attempting to Get metrics query token event");
            _discovery.GetMetricsQueryTokenEvent(OnGetMetricsQueryTokenEvent, OnFail);
            while (!_getMetricsQueryTokenEventTested)
            {
                yield return(null);
            }

            //  Query log
            Log.Debug("TestDiscovery.RunTest()", "Attempting to Query log");
            _discovery.QueryLog(OnQueryLog, OnFail);
            while (!_queryLogTested)
            {
                yield return(null);
            }

            //  Create event
            Log.Debug("TestDiscovery.RunTest()", "Attempting to create event");
            CreateEventObject queryEvent = new CreateEventObject()
            {
                Type = CreateEventObject.TypeEnum.click,
                Data = new EventData()
                {
                    EnvironmentId = _environmentId,
                    SessionToken  = _sessionToken,
                    CollectionId  = _createdCollectionId,
                    DocumentId    = _createdDocumentID
                }
            };

            _discovery.CreateEvent(OnCreateEvent, OnFail, queryEvent);
            while (!_createEventTested)
            {
                yield return(null);
            }

            //  DeleteCredential
            Log.Debug("TestDiscovery.RunTest()", "Attempting to delete credential");
            _discovery.DeleteCredentials(OnDeleteCredentials, OnFail, _environmentId, _createdCredentialId);
            while (!_deleteCredentialsTested)
            {
                yield return(null);
            }

            //  Delete Document
            Log.Debug("TestDiscovery.RunTest()", "Attempting to delete document {0}", _createdDocumentID);
            if (!_discovery.DeleteDocument(OnDeleteDocument, OnFail, _environmentId, _createdCollectionId, _createdDocumentID))
            {
                Log.Debug("TestDiscovery.DeleteDocument()", "Failed to delete document");
            }
            while (!_deleteDocumentTested)
            {
                yield return(null);
            }

            _isEnvironmentReady = false;
            Runnable.Run(CheckEnvironmentState(_waitTime));
            while (!_isEnvironmentReady)
            {
                yield return(null);
            }

            //  Delete Collection
            Log.Debug("TestDiscovery.RunTest()", "Attempting to delete collection {0}", _createdCollectionId);
            if (!_discovery.DeleteCollection(OnDeleteCollection, OnFail, _environmentId, _createdCollectionId))
            {
                Log.Debug("TestDiscovery.DeleteCollection()", "Failed to delete collection");
            }
            while (!_deleteCollectionTested)
            {
                yield return(null);
            }

            _isEnvironmentReady = false;
            Runnable.Run(CheckEnvironmentState(_waitTime));
            while (!_isEnvironmentReady)
            {
                yield return(null);
            }

            //  Delete Configuration
            Log.Debug("TestDiscovery.RunTest()", "Attempting to delete configuration {0}", _createdConfigurationID);
            if (!_discovery.DeleteConfiguration(OnDeleteConfiguration, OnFail, _environmentId, _createdConfigurationID))
            {
                Log.Debug("TestDiscovery.DeleteConfiguration()", "Failed to delete configuration");
            }
            while (!_deleteConfigurationTested)
            {
                yield return(null);
            }

            //  Delete User Data
            Log.Debug("TestDiscovery.RunTest()", "Attempting to delete user data.");
            _discovery.DeleteUserData(OnDeleteUserData, OnFail, "test-unity-user-id");
            while (!_deleteUserDataTested)
            {
                yield return(null);
            }

            Log.Debug("TestDiscovery.RunTest()", "Discovery unit tests complete.");

            yield break;
        }
Example #2
0
    private IEnumerator Examples()
    {
        //  Get Environments
        Log.Debug("TestDiscovery.RunTest()", "Attempting to get environments");
        if (!_service.GetEnvironments(OnGetEnvironments, OnFail))
        {
            Log.Debug("TestDiscovery.GetEnvironments()", "Failed to get environments");
        }
        while (!_getEnvironmentsTested)
        {
            yield return(null);
        }

        //  Wait for environment to be ready
        Runnable.Run(CheckEnvironmentState(0f));
        while (!_isEnvironmentReady)
        {
            yield return(null);
        }

        //  GetEnvironment
        Log.Debug("TestDiscovery.RunTest()", "Attempting to get environment");
        if (!_service.GetEnvironment(OnGetEnvironment, OnFail, _environmentId))
        {
            Log.Debug("TestDiscovery.GetEnvironment()", "Failed to get environment");
        }
        while (!_getEnvironmentTested)
        {
            yield return(null);
        }

        //  Get Configurations
        Log.Debug("TestDiscovery.RunTest()", "Attempting to get configurations");
        if (!_service.GetConfigurations(OnGetConfigurations, OnFail, _environmentId))
        {
            Log.Debug("TestDiscovery.GetConfigurations()", "Failed to get configurations");
        }
        while (!_getConfigurationsTested)
        {
            yield return(null);
        }

        //  Add Configuration
        Log.Debug("TestDiscovery.RunTest()", "Attempting to add configuration");
        if (!_service.AddConfiguration(OnAddConfiguration, OnFail, _environmentId, _configurationJson.Replace("{guid}", System.Guid.NewGuid().ToString())))
        {
            Log.Debug("TestDiscovery.AddConfiguration()", "Failed to add configuration");
        }
        while (!_addConfigurationTested)
        {
            yield return(null);
        }

        //  Get Configuration
        Log.Debug("TestDiscovery.RunTest()", "Attempting to get configuration");
        if (!_service.GetConfiguration(OnGetConfiguration, OnFail, _environmentId, _environmentId))
        {
            Log.Debug("TestDiscovery.GetConfiguration()", "Failed to get configuration");
        }
        while (!_getConfigurationTested)
        {
            yield return(null);
        }

        //  Preview Configuration
        Log.Debug("TestDiscovery.RunTest()", "Attempting to preview configuration");
        if (!_service.PreviewConfiguration(OnPreviewConfiguration, OnFail, _environmentId, _environmentId, null, _filePathToIngest, _metadata))
        {
            Log.Debug("TestDiscovery.PreviewConfiguration()", "Failed to preview configuration");
        }
        while (!_previewConfigurationTested)
        {
            yield return(null);
        }

        //  Get Collections
        Log.Debug("TestDiscovery.RunTest()", "Attempting to get collections");
        if (!_service.GetCollections(OnGetCollections, OnFail, _environmentId))
        {
            Log.Debug("TestDiscovery.GetCollections()", "Failed to get collections");
        }
        while (!_getCollectionsTested)
        {
            yield return(null);
        }

        //  Add Collection
        Log.Debug("TestDiscovery.RunTest()", "Attempting to add collection");
        if (!_service.AddCollection(OnAddCollection, OnFail, _environmentId, _createdCollectionName + System.Guid.NewGuid().ToString(), _createdCollectionDescription, _environmentId))
        {
            Log.Debug("TestDiscovery.AddCollection()", "Failed to add collection");
        }
        while (!_addCollectionTested)
        {
            yield return(null);
        }

        //  Get Collection
        Log.Debug("TestDiscovery.RunTest()", "Attempting to get collection");
        if (!_service.GetCollection(OnGetCollection, OnFail, _environmentId, _createdCollectionID))
        {
            Log.Debug("TestDiscovery.GetCollection()", "Failed to get collection");
        }
        while (!_getCollectionTested)
        {
            yield return(null);
        }

        if (!_service.GetFields(OnGetFields, OnFail, _environmentId, _createdCollectionID))
        {
            Log.Debug("TestDiscovery.GetFields()", "Failed to get fields");
        }
        while (!_getFieldsTested)
        {
            yield return(null);
        }

        //  Add Document
        Log.Debug("TestDiscovery.RunTest()", "Attempting to add document");
        if (!_service.AddDocument(OnAddDocument, OnFail, _environmentId, _createdCollectionID, _documentFilePath, _environmentId, null))
        {
            Log.Debug("TestDiscovery.AddDocument()", "Failed to add document");
        }
        while (!_addDocumentTested)
        {
            yield return(null);
        }

        //  Get Document
        Log.Debug("TestDiscovery.RunTest()", "Attempting to get document");
        if (!_service.GetDocument(OnGetDocument, OnFail, _environmentId, _createdCollectionID, _createdDocumentID))
        {
            Log.Debug("TestDiscovery.GetDocument()", "Failed to get document");
        }
        while (!_getDocumentTested)
        {
            yield return(null);
        }

        //  Update Document
        Log.Debug("TestDiscovery.RunTest()", "Attempting to update document");
        if (!_service.UpdateDocument(OnUpdateDocument, OnFail, _environmentId, _createdCollectionID, _createdDocumentID, _documentFilePath, _environmentId, null))
        {
            Log.Debug("TestDiscovery.UpdateDocument()", "Failed to update document");
        }
        while (!_updateDocumentTested)
        {
            yield return(null);
        }

        //  Query
        Log.Debug("TestDiscovery.RunTest()", "Attempting to query");
        if (!_service.Query(OnQuery, OnFail, _environmentId, _createdCollectionID, null, _query, null, 10, null, 0))
        {
            Log.Debug("TestDiscovery.Query()", "Failed to query");
        }
        while (!_queryTested)
        {
            yield return(null);
        }

        //  Delete Document
        Log.Debug("TestDiscovery.RunTest()", "Attempting to delete document {0}", _createdDocumentID);
        if (!_service.DeleteDocument(OnDeleteDocument, OnFail, _environmentId, _createdCollectionID, _createdDocumentID))
        {
            Log.Debug("TestDiscovery.DeleteDocument()", "Failed to delete document");
        }
        while (!_deleteDocumentTested)
        {
            yield return(null);
        }

        //  Delay
        Log.Debug("TestDiscovery.RunTest()", "Delaying delete collection for 10 sec");
        Runnable.Run(Delay(_waitTime));
        while (!_readyToContinue)
        {
            yield return(null);
        }

        _isEnvironmentReady = false;
        Runnable.Run(CheckEnvironmentState(_waitTime));
        while (!_isEnvironmentReady)
        {
            yield return(null);
        }

        _readyToContinue = false;
        //  Delete Collection
        Log.Debug("TestDiscovery.RunTest()", "Attempting to delete collection {0}", _createdCollectionID);
        if (!_service.DeleteCollection(OnDeleteCollection, OnFail, _environmentId, _createdCollectionID))
        {
            Log.Debug("TestDiscovery.DeleteCollection()", "Failed to delete collection");
        }
        while (!_deleteCollectionTested)
        {
            yield return(null);
        }

        //  Delay
        Log.Debug("TestDiscovery.RunTest()", "Delaying delete configuration for 10 sec");
        Runnable.Run(Delay(_waitTime));
        while (!_readyToContinue)
        {
            yield return(null);
        }

        _isEnvironmentReady = false;
        Runnable.Run(CheckEnvironmentState(_waitTime));
        while (!_isEnvironmentReady)
        {
            yield return(null);
        }

        _readyToContinue = false;
        //  Delete Configuration
        Log.Debug("TestDiscovery.RunTest()", "Attempting to delete configuration {0}", _environmentId);
        if (!_service.DeleteConfiguration(OnDeleteConfiguration, OnFail, _environmentId, _environmentId))
        {
            Log.Debug("TestDiscovery.DeleteConfiguration()", "Failed to delete configuration");
        }
        while (!_deleteConfigurationTested)
        {
            yield return(null);
        }

        //  Delay
        Log.Debug("TestDiscovery.RunTest()", "Delaying delete environment for 10 sec");
        Runnable.Run(Delay(_waitTime));
        while (!_readyToContinue)
        {
            yield return(null);
        }

        _isEnvironmentReady = false;
        Runnable.Run(CheckEnvironmentState(_waitTime));
        while (!_isEnvironmentReady)
        {
            yield return(null);
        }

        Log.Debug("TestDiscovery.RunTest()", "Discovery examples complete.");
    }
Example #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("discovery-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);

            _discovery             = new Discovery(credentials);
            _discovery.VersionDate = _discoveryVersionDate;
            _filePathToIngest      = Application.dataPath + "/Watson/Examples/ServiceExamples/TestData/Discovery/constitution.pdf";
            _documentFilePath      = Application.dataPath + "/Watson/Examples/ServiceExamples/TestData/Discovery/constitution.pdf";

            //  Get Environments
            Log.Debug("TestDiscovery.RunTest()", "Attempting to get environments");
            if (!_discovery.GetEnvironments(OnGetEnvironments, OnFail))
            {
                Log.Debug("TestDiscovery.GetEnvironments()", "Failed to get environments");
            }
            while (!_getEnvironmentsTested)
            {
                yield return(null);
            }

            //  Wait for environment to be ready
            Runnable.Run(CheckEnvironmentState(0f));
            while (!_isEnvironmentReady)
            {
                yield return(null);
            }

            //  GetEnvironment
            Log.Debug("TestDiscovery.RunTest()", "Attempting to get environment");
            if (!_discovery.GetEnvironment(OnGetEnvironment, OnFail, _environmentId))
            {
                Log.Debug("TestDiscovery.GetEnvironment()", "Failed to get environment");
            }
            while (!_getEnvironmentTested)
            {
                yield return(null);
            }

            //  Get Configurations
            Log.Debug("TestDiscovery.RunTest()", "Attempting to get configurations");
            if (!_discovery.GetConfigurations(OnGetConfigurations, OnFail, _environmentId))
            {
                Log.Debug("TestDiscovery.GetConfigurations()", "Failed to get configurations");
            }
            while (!_getConfigurationsTested)
            {
                yield return(null);
            }

            //  Add Configuration
            Log.Debug("TestDiscovery.RunTest()", "Attempting to add configuration");
            if (!_discovery.AddConfiguration(OnAddConfiguration, OnFail, _environmentId, _configurationJson.Replace("{guid}", GUID.Generate().ToString())))
            {
                Log.Debug("TestDiscovery.AddConfiguration()", "Failed to add configuration");
            }
            while (!_addConfigurationTested)
            {
                yield return(null);
            }

            //  Get Configuration
            Log.Debug("TestDiscovery.RunTest()", "Attempting to get configuration");
            if (!_discovery.GetConfiguration(OnGetConfiguration, OnFail, _environmentId, _createdConfigurationID))
            {
                Log.Debug("TestDiscovery.GetConfiguration()", "Failed to get configuration");
            }
            while (!_getConfigurationTested)
            {
                yield return(null);
            }

            //  Preview Configuration
            Log.Debug("TestDiscovery.RunTest()", "Attempting to preview configuration");
            if (!_discovery.PreviewConfiguration(OnPreviewConfiguration, OnFail, _environmentId, _createdConfigurationID, null, _filePathToIngest, _metadata))
            {
                Log.Debug("TestDiscovery.PreviewConfiguration()", "Failed to preview configuration");
            }
            while (!_previewConfigurationTested)
            {
                yield return(null);
            }

            //  Get Collections
            Log.Debug("TestDiscovery.RunTest()", "Attempting to get collections");
            if (!_discovery.GetCollections(OnGetCollections, OnFail, _environmentId))
            {
                Log.Debug("TestDiscovery.GetCollections()", "Failed to get collections");
            }
            while (!_getCollectionsTested)
            {
                yield return(null);
            }

            //  Add Collection
            Log.Debug("TestDiscovery.RunTest()", "Attempting to add collection");
            if (!_discovery.AddCollection(OnAddCollection, OnFail, _environmentId, _createdCollectionName + GUID.Generate().ToString(), _createdCollectionDescription, _createdConfigurationID))
            {
                Log.Debug("TestDiscovery.AddCollection()", "Failed to add collection");
            }
            while (!_addCollectionTested)
            {
                yield return(null);
            }

            //  Get Collection
            Log.Debug("TestDiscovery.RunTest()", "Attempting to get collection");
            if (!_discovery.GetCollection(OnGetCollection, OnFail, _environmentId, _createdCollectionID))
            {
                Log.Debug("TestDiscovery.GetCollection()", "Failed to get collection");
            }
            while (!_getCollectionTested)
            {
                yield return(null);
            }

            if (!_discovery.GetFields(OnGetFields, OnFail, _environmentId, _createdCollectionID))
            {
                Log.Debug("TestDiscovery.GetFields()", "Failed to get fields");
            }
            while (!_getFieldsTested)
            {
                yield return(null);
            }

            //  Add Document
            Log.Debug("TestDiscovery.RunTest()", "Attempting to add document");
            if (!_discovery.AddDocument(OnAddDocument, OnFail, _environmentId, _createdCollectionID, _documentFilePath, _createdConfigurationID, null))
            {
                Log.Debug("TestDiscovery.AddDocument()", "Failed to add document");
            }
            while (!_addDocumentTested)
            {
                yield return(null);
            }

            //  Get Document
            Log.Debug("TestDiscovery.RunTest()", "Attempting to get document");
            if (!_discovery.GetDocument(OnGetDocument, OnFail, _environmentId, _createdCollectionID, _createdDocumentID))
            {
                Log.Debug("TestDiscovery.GetDocument()", "Failed to get document");
            }
            while (!_getDocumentTested)
            {
                yield return(null);
            }

            //  Update Document
            Log.Debug("TestDiscovery.RunTest()", "Attempting to update document");
            if (!_discovery.UpdateDocument(OnUpdateDocument, OnFail, _environmentId, _createdCollectionID, _createdDocumentID, _documentFilePath, _createdConfigurationID, null))
            {
                Log.Debug("TestDiscovery.UpdateDocument()", "Failed to update document");
            }
            while (!_updateDocumentTested)
            {
                yield return(null);
            }

            //  Query
            Log.Debug("TestDiscovery.RunTest()", "Attempting to query");
            if (!_discovery.Query(OnQuery, OnFail, _environmentId, _createdCollectionID, null, _query, null, 10, null, 0))
            {
                Log.Debug("TestDiscovery.Query()", "Failed to query");
            }
            while (!_queryTested)
            {
                yield return(null);
            }

            //  Delete Document
            Log.Debug("TestDiscovery.RunTest()", "Attempting to delete document {0}", _createdDocumentID);
            if (!_discovery.DeleteDocument(OnDeleteDocument, OnFail, _environmentId, _createdCollectionID, _createdDocumentID))
            {
                Log.Debug("TestDiscovery.DeleteDocument()", "Failed to delete document");
            }
            while (!_deleteDocumentTested)
            {
                yield return(null);
            }

            //  Delay
            Log.Debug("TestDiscovery.RunTest()", "Delaying delete collection for 10 sec");
            Runnable.Run(Delay(_waitTime));
            while (!_readyToContinue)
            {
                yield return(null);
            }

            _isEnvironmentReady = false;
            Runnable.Run(CheckEnvironmentState(_waitTime));
            while (!_isEnvironmentReady)
            {
                yield return(null);
            }

            _readyToContinue = false;
            //  Delete Collection
            Log.Debug("TestDiscovery.RunTest()", "Attempting to delete collection {0}", _createdCollectionID);
            if (!_discovery.DeleteCollection(OnDeleteCollection, OnFail, _environmentId, _createdCollectionID))
            {
                Log.Debug("TestDiscovery.DeleteCollection()", "Failed to delete collection");
            }
            while (!_deleteCollectionTested)
            {
                yield return(null);
            }

            //  Delay
            Log.Debug("TestDiscovery.RunTest()", "Delaying delete configuration for 10 sec");
            Runnable.Run(Delay(_waitTime));
            while (!_readyToContinue)
            {
                yield return(null);
            }

            _isEnvironmentReady = false;
            Runnable.Run(CheckEnvironmentState(_waitTime));
            while (!_isEnvironmentReady)
            {
                yield return(null);
            }

            _readyToContinue = false;
            //  Delete Configuration
            Log.Debug("TestDiscovery.RunTest()", "Attempting to delete configuration {0}", _createdConfigurationID);
            if (!_discovery.DeleteConfiguration(OnDeleteConfiguration, OnFail, _environmentId, _createdConfigurationID))
            {
                Log.Debug("TestDiscovery.DeleteConfiguration()", "Failed to delete configuration");
            }
            while (!_deleteConfigurationTested)
            {
                yield return(null);
            }

            //  Delete User Data
            Log.Debug("TestDiscovery.RunTest()", "Attempting to delete user data.");
            _discovery.DeleteUserData(OnDeleteUserData, OnFail, "test-unity-user-id");
            while (!_deleteUserDataTested)
            {
                yield return(null);
            }

            Log.Debug("TestDiscovery.RunTest()", "Discovery unit tests complete.");

            yield break;
        }
Example #4
0
    private IEnumerator Examples()
    {
        //  Get Environments
        Log.Debug("ExampleDiscovery.RunTest()", "Attempting to get environments");
        if (!_service.GetEnvironments(OnGetEnvironments, OnFail))
        {
            Log.Debug("ExampleDiscovery.GetEnvironments()", "Failed to get environments");
        }
        while (!_getEnvironmentsTested)
        {
            yield return(null);
        }

        //  Wait for environment to be ready
        Runnable.Run(CheckEnvironmentState(0f));
        while (!_isEnvironmentReady)
        {
            yield return(null);
        }

        //  GetEnvironment
        Log.Debug("ExampleDiscovery.RunTest()", "Attempting to get environment");
        if (!_service.GetEnvironment(OnGetEnvironment, OnFail, _environmentId))
        {
            Log.Debug("ExampleDiscovery.GetEnvironment()", "Failed to get environment");
        }
        while (!_getEnvironmentTested)
        {
            yield return(null);
        }

        //  Get Configurations
        Log.Debug("ExampleDiscovery.RunTest()", "Attempting to get configurations");
        if (!_service.GetConfigurations(OnGetConfigurations, OnFail, _environmentId))
        {
            Log.Debug("ExampleDiscovery.GetConfigurations()", "Failed to get configurations");
        }
        while (!_getConfigurationsTested)
        {
            yield return(null);
        }

        //  Add Configuration
        Log.Debug("ExampleDiscovery.RunTest()", "Attempting to add configuration");
        if (!_service.AddConfiguration(OnAddConfiguration, OnFail, _environmentId, _configurationJson.Replace("{guid}", System.Guid.NewGuid().ToString())))
        {
            Log.Debug("ExampleDiscovery.AddConfiguration()", "Failed to add configuration");
        }
        while (!_addConfigurationTested)
        {
            yield return(null);
        }

        //  Get Configuration
        Log.Debug("ExampleDiscovery.RunTest()", "Attempting to get configuration");
        if (!_service.GetConfiguration(OnGetConfiguration, OnFail, _environmentId, _createdConfigurationId))
        {
            Log.Debug("ExampleDiscovery.GetConfiguration()", "Failed to get configuration");
        }
        while (!_getConfigurationTested)
        {
            yield return(null);
        }

        //  Preview Configuration
        Log.Debug("ExampleDiscovery.RunTest()", "Attempting to preview configuration");
        if (!_service.PreviewConfiguration(OnPreviewConfiguration, OnFail, _environmentId, _createdConfigurationId, null, _filePathToIngest, _metadata))
        {
            Log.Debug("ExampleDiscovery.PreviewConfiguration()", "Failed to preview configuration");
        }
        while (!_previewConfigurationTested)
        {
            yield return(null);
        }

        //  Get Collections
        Log.Debug("ExampleDiscovery.RunTest()", "Attempting to get collections");
        if (!_service.GetCollections(OnGetCollections, OnFail, _environmentId))
        {
            Log.Debug("ExampleDiscovery.GetCollections()", "Failed to get collections");
        }
        while (!_getCollectionsTested)
        {
            yield return(null);
        }

        //  Add Collection
        Log.Debug("ExampleDiscovery.RunTest()", "Attempting to add collection");
        if (!_service.AddCollection(OnAddCollection, OnFail, _environmentId, _createdCollectionName + System.Guid.NewGuid().ToString(), _createdCollectionDescription, _createdConfigurationId))
        {
            Log.Debug("ExampleDiscovery.AddCollection()", "Failed to add collection");
        }
        while (!_addCollectionTested)
        {
            yield return(null);
        }

        //  Get Collection
        Log.Debug("ExampleDiscovery.RunTest()", "Attempting to get collection");
        if (!_service.GetCollection(OnGetCollection, OnFail, _environmentId, _createdCollectionId))
        {
            Log.Debug("ExampleDiscovery.GetCollection()", "Failed to get collection");
        }
        while (!_getCollectionTested)
        {
            yield return(null);
        }

        if (!_service.GetFields(OnGetFields, OnFail, _environmentId, _createdCollectionId))
        {
            Log.Debug("ExampleDiscovery.GetFields()", "Failed to get fields");
        }
        while (!_getFieldsTested)
        {
            yield return(null);
        }

        //  Add Document
        Log.Debug("ExampleDiscovery.RunTest()", "Attempting to add document");
        if (!_service.AddDocument(OnAddDocument, OnFail, _environmentId, _createdCollectionId, _documentFilePath, _createdConfigurationId, null))
        {
            Log.Debug("ExampleDiscovery.AddDocument()", "Failed to add document");
        }
        while (!_addDocumentTested)
        {
            yield return(null);
        }

        //  Get Document
        Log.Debug("ExampleDiscovery.RunTest()", "Attempting to get document");
        if (!_service.GetDocument(OnGetDocument, OnFail, _environmentId, _createdCollectionId, _createdDocumentID))
        {
            Log.Debug("ExampleDiscovery.GetDocument()", "Failed to get document");
        }
        while (!_getDocumentTested)
        {
            yield return(null);
        }

        //  Update Document
        Log.Debug("ExampleDiscovery.RunTest()", "Attempting to update document");
        if (!_service.UpdateDocument(OnUpdateDocument, OnFail, _environmentId, _createdCollectionId, _createdDocumentID, _documentFilePath, _createdConfigurationId, null))
        {
            Log.Debug("ExampleDiscovery.UpdateDocument()", "Failed to update document");
        }
        while (!_updateDocumentTested)
        {
            yield return(null);
        }

        //  Query
        Log.Debug("ExampleDiscovery.RunTest()", "Attempting to query");
        if (!_service.Query(OnQuery, OnFail, _environmentId, _createdCollectionId, naturalLanguageQuery: _query))
        {
            while (!_queryTested)
            {
                yield return(null);
            }
        }

        //  List Credentials
        Log.Debug("ExampleDiscovery.RunTest()", "Attempting to list credentials");
        _service.ListCredentials(OnListCredentials, OnFail, _environmentId);
        while (!_listCredentialsTested)
        {
            yield return(null);
        }

        //  Create Credentials
        Log.Debug("ExampleDiscovery.RunTest()", "Attempting to create credentials");
        SourceCredentials credentialsParameter = new SourceCredentials()
        {
            SourceType        = SourceCredentials.SourceTypeEnum.box,
            CredentialDetails = new CredentialDetails()
            {
                CredentialType = CredentialDetails.CredentialTypeEnum.oauth2,
                EnterpriseId   = "myEnterpriseId",
                ClientId       = "myClientId",
                ClientSecret   = "myClientSecret",
                PublicKeyId    = "myPublicIdKey",
                Passphrase     = "myPassphrase",
                PrivateKey     = "myPrivateKey"
            }
        };

        _service.CreateCredentials(OnCreateCredentials, OnFail, _environmentId, credentialsParameter);
        while (!_createCredentialsTested)
        {
            yield return(null);
        }

        //  Get Credential
        Log.Debug("ExampleDiscovery.RunTest()", "Attempting to get credential");
        _service.GetCredential(OnGetCredential, OnFail, _environmentId, _createdCredentialId);
        while (!_getCredentialTested)
        {
            yield return(null);
        }

        //  Get metrics event rate
        Log.Debug("ExampleDiscovery.RunTest()", "Attempting to Get metrics event rate");
        _service.GetMetricsEventRate(OnGetMetricsEventRate, OnFail);
        while (!_getMetricsEventRateTested)
        {
            yield return(null);
        }

        //  Get metrics query
        Log.Debug("ExampleDiscovery.RunTest()", "Attempting to Get metrics query");
        _service.GetMetricsQuery(OnGetMetricsQuery, OnFail);
        while (!_getMetricsQueryTested)
        {
            yield return(null);
        }

        //  Get metrics query event
        Log.Debug("ExampleDiscovery.RunTest()", "Attempting to Get metrics query event");
        _service.GetMetricsQueryEvent(OnGetMetricsQueryEvent, OnFail);
        while (!_getMetricsQueryEventTested)
        {
            yield return(null);
        }

        //  Get metrics query no result
        Log.Debug("ExampleDiscovery.RunTest()", "Attempting to Get metrics query no result");
        _service.GetMetricsQueryNoResults(OnGetMetricsQueryNoResult, OnFail);
        while (!_getMetricsQueryNoResultTested)
        {
            yield return(null);
        }

        //  Get metrics query token event
        Log.Debug("ExampleDiscovery.RunTest()", "Attempting to Get metrics query token event");
        _service.GetMetricsQueryTokenEvent(OnGetMetricsQueryTokenEvent, OnFail);
        while (!_getMetricsQueryTokenEventTested)
        {
            yield return(null);
        }

        //  Query log
        Log.Debug("ExampleDiscovery.RunTest()", "Attempting to Query log");
        _service.QueryLog(OnQueryLog, OnFail);
        while (!_queryLogTested)
        {
            yield return(null);
        }

        //  Create event
        Log.Debug("ExampleDiscovery.RunTest()", "Attempting to create event");
        CreateEventObject queryEvent = new CreateEventObject()
        {
            Type = CreateEventObject.TypeEnum.click,
            Data = new EventData()
            {
                EnvironmentId = _environmentId,
                SessionToken  = _sessionToken,
                CollectionId  = _createdCollectionId,
                DocumentId    = _createdDocumentID
            }
        };

        _service.CreateEvent(OnCreateEvent, OnFail, queryEvent);
        while (!_createEventTested)
        {
            yield return(null);
        }

        //  DeleteCredential
        Log.Debug("ExampleDiscovery.RunTest()", "Attempting to delete credential");
        _service.DeleteCredentials(OnDeleteCredentials, OnFail, _environmentId, _createdCredentialId);
        while (!_deleteCredentialsTested)
        {
            yield return(null);
        }

        //  Delete Document
        Log.Debug("ExampleDiscovery.RunTest()", "Attempting to delete document {0}", _createdDocumentID);
        if (!_service.DeleteDocument(OnDeleteDocument, OnFail, _environmentId, _createdCollectionId, _createdDocumentID))
        {
            Log.Debug("ExampleDiscovery.DeleteDocument()", "Failed to delete document");
        }
        while (!_deleteDocumentTested)
        {
            yield return(null);
        }

        _isEnvironmentReady = false;
        Runnable.Run(CheckEnvironmentState(_waitTime));
        while (!_isEnvironmentReady)
        {
            yield return(null);
        }

        //  Delete Collection
        Log.Debug("ExampleDiscovery.RunTest()", "Attempting to delete collection {0}", _createdCollectionId);
        if (!_service.DeleteCollection(OnDeleteCollection, OnFail, _environmentId, _createdCollectionId))
        {
            Log.Debug("ExampleDiscovery.DeleteCollection()", "Failed to delete collection");
        }
        while (!_deleteCollectionTested)
        {
            yield return(null);
        }

        _isEnvironmentReady = false;
        Runnable.Run(CheckEnvironmentState(_waitTime));
        while (!_isEnvironmentReady)
        {
            yield return(null);
        }

        //  Delete Configuration
        Log.Debug("ExampleDiscovery.RunTest()", "Attempting to delete configuration {0}", _environmentId);
        if (!_service.DeleteConfiguration(OnDeleteConfiguration, OnFail, _environmentId, _createdConfigurationId))
        {
            Log.Debug("ExampleDiscovery.DeleteConfiguration()", "Failed to delete configuration");
        }
        while (!_deleteConfigurationTested)
        {
            yield return(null);
        }

        _isEnvironmentReady = false;
        Runnable.Run(CheckEnvironmentState(_waitTime));
        while (!_isEnvironmentReady)
        {
            yield return(null);
        }

        Log.Debug("ExampleDiscovery.RunTest()", "Discovery examples complete.");
    }
Example #5
0
        public override IEnumerator RunTest()
        {
            LogSystem.InstallDefaultReactors();

            m_ConfigurationJsonPath = Application.dataPath + "/Watson/Examples/ServiceExamples/TestData/Discovery/exampleConfigurationData.json";
            m_FilePathToIngest      = Application.dataPath + "/Watson/Examples/ServiceExamples/TestData/watson_beats_jeopardy.html";
            m_DocumentFilePath      = Application.dataPath + "/Watson/Examples/ServiceExamples/TestData/watson_beats_jeopardy.html";

            #region Get Environments and Add Environment
            //  Get Environments
            Log.Debug("ExampleDiscoveryV1", "Attempting to get environments");
            if (!m_Discovery.GetEnvironments((GetEnvironmentsResponse resp, string data) =>
            {
                if (resp != null)
                {
                    foreach (Environment environment in resp.environments)
                    {
                        Log.Debug("ExampleDiscoveryV1", "environment_id: {0}", environment.environment_id);
                    }
                }
                else
                {
                    Log.Debug("ExampleDiscoveryV1", "Discovery.GetEnvironments(); resp is null");
                }

                Test(resp.environments != null);
                m_GetEnvironmentsTested = true;
            }))
            {
                Log.Debug("ExampleDiscoveryV1", "Failed to get environments");
            }

            while (!m_GetEnvironmentsTested)
            {
                yield return(null);
            }

            //  Add Environment
            Log.Debug("ExampleDiscoveryV1", "Attempting to add environment");
            if (!m_Discovery.AddEnvironment((Environment resp, string data) =>
            {
                if (resp != null)
                {
                    Log.Debug("ExampleDiscoveryV1", "Added {0}", resp.environment_id);
                    m_CreatedEnvironmentID = resp.environment_id;
                }
                else
                {
                    Log.Debug("ExampleDiscoveryV1", "Discovery.AddEnvironment(); resp is null, {0}", data);
                }

                Test(!string.IsNullOrEmpty(resp.environment_id));
                m_AddEnvironmentsTested = true;
            }, m_CreatedEnvironmentName, m_CreatedEnvironmentDescription, 1))
            {
                Log.Debug("ExampleDiscoveryV1", "Failed to add environment");
            }

            while (!m_AddEnvironmentsTested)
            {
                yield return(null);
            }

            //  Check for active environment
            Runnable.Run(CheckEnvironmentStatus());
            while (!m_IsEnvironmentActive)
            {
                yield return(null);
            }
            #endregion

            #region Get Configurations and add Configuration
            //  Get Configurations
            Log.Debug("ExampleDiscoveryV1", "Attempting to get configurations");
            if (!m_Discovery.GetConfigurations((GetConfigurationsResponse resp, string data) =>
            {
                if (resp != null)
                {
                    if (resp.configurations != null && resp.configurations.Length > 0)
                    {
                        foreach (ConfigurationRef configuration in resp.configurations)
                        {
                            Log.Debug("ExampleDiscoveryV1", "Configuration: {0}, {1}", configuration.configuration_id, configuration.name);
                        }
                    }
                    else
                    {
                        Log.Debug("ExampleDiscoveryV1", "There are no configurations for this environment.");
                    }
                }
                else
                {
                    Log.Debug("ExampleDiscoveryV1", "Discovery.GetConfigurations(); resp is null, {0}", data);
                }

                Test(resp.configurations != null);
                m_GetConfigurationsTested = true;
            }, m_CreatedEnvironmentID))
            {
                Log.Debug("ExampleDiscoveryV1", "Failed to get configurations");
            }

            while (!m_GetConfigurationsTested)
            {
                yield return(null);
            }

            //  Add Configuration
            Log.Debug("ExampleDiscoveryV1", "Attempting to add configuration");
            if (!m_Discovery.AddConfiguration((Configuration resp, string data) =>
            {
                if (resp != null)
                {
                    Log.Debug("ExampleDiscoveryV1", "Configuration: {0}, {1}", resp.configuration_id, resp.name);
                    m_CreatedConfigurationID = resp.configuration_id;
                }
                else
                {
                    Log.Debug("ExampleDiscoveryV1", "Discovery.AddConfiguration(); resp is null, {0}", data);
                }

                Test(!string.IsNullOrEmpty(resp.configuration_id));
                m_AddConfigurationTested = true;
            }, m_CreatedEnvironmentID, m_ConfigurationJsonPath))
            {
                Log.Debug("ExampleDiscoveryV1", "Failed to add configuration");
            }

            while (!m_AddConfigurationTested)
            {
                yield return(null);
            }
            #endregion

            #region GetCollections and Add Collection
            //  Get Collections
            Log.Debug("ExampleDiscoveryV1", "Attempting to get collections");
            if (!m_Discovery.GetCollections((GetCollectionsResponse resp, string customData) =>
            {
                if (resp != null)
                {
                    if (resp.collections != null && resp.collections.Length > 0)
                    {
                        foreach (CollectionRef collection in resp.collections)
                        {
                            Log.Debug("ExampleDiscoveryV1", "Collection: {0}, {1}", collection.collection_id, collection.name);
                        }
                    }
                    else
                    {
                        Log.Debug("ExampleDiscoveryV1", "There are no collections");
                    }
                }
                else
                {
                    Log.Debug("ExampleDiscoveryV1", "Discovery.GetCollections(); resp is null");
                }

                Test(resp != null);
                m_GetCollectionsTested = true;
            }, m_CreatedEnvironmentID))
            {
                Log.Debug("ExampleDiscovery", "Failed to get collections");
            }

            while (!m_GetCollectionsTested)
            {
                yield return(null);
            }

            //  Add Collection
            Log.Debug("ExampleDiscoveryV1", "Attempting to add collection");
            if (!m_Discovery.AddCollection((CollectionRef resp, string data) =>
            {
                if (resp != null)
                {
                    Log.Debug("ExampleDiscoveryV1", "Collection: {0}, {1}", resp.collection_id, resp.name);
                    m_CreatedCollectionID = resp.collection_id;
                }
                else
                {
                    Log.Debug("ExampleDiscoveryV1", "Discovery.AddCollection(); resp is null, {0}", data);
                }

                Test(!string.IsNullOrEmpty(resp.collection_id));
                m_AddCollectionTested = true;
            }, m_CreatedEnvironmentID, m_CreatedCollectionName, m_CreatedCollectionDescription, m_CreatedConfigurationID))
            {
                Log.Debug("ExampleDiscovery", "Failed to add collection");
            }

            while (!m_AddCollectionTested)
            {
                yield return(null);
            }
            #endregion

            #region Get Fields
            Log.Debug("ExampleDiscoveryV1", "Attempting to get fields");
            if (!m_Discovery.GetFields((GetFieldsResponse resp, string customData) =>
            {
                if (resp != null)
                {
                    foreach (Field field in resp.fields)
                    {
                        Log.Debug("ExampleDiscoveryV1", "Field: {0}, type: {1}", field.field, field.type);
                    }
                }
                else
                {
                    Log.Debug("ExampleDiscoveryV1", "Discovery.GetFields(); resp is null");
                }

                Test(resp != null);
                m_GetFieldsTested = true;
            }, m_CreatedEnvironmentID, m_CreatedCollectionID))
            {
                Log.Debug("ExampleDiscoveryV1", "Failed to get fields");
            }

            while (!m_GetFieldsTested)
            {
                yield return(null);
            }
            #endregion

            #region Add Document
            //  Add Document
            Log.Debug("ExampleDiscoveryV1", "Attempting to add document");
            if (!m_Discovery.AddDocument((DocumentAccepted resp, string data) =>
            {
                if (resp != null)
                {
                    Log.Debug("ExampleDiscoveryV1", "Added Document {0} {1}", resp.document_id, resp.status);
                    m_CreatedDocumentID = resp.document_id;
                }
                else
                {
                    Log.Debug("ExampleDiscoveryV1", "Discovery.AddDocument(); resp is null, {0}", data);
                }

                Test(!string.IsNullOrEmpty(resp.document_id));
                m_AddDocumentTested = true;
            }, m_CreatedEnvironmentID, m_CreatedCollectionID, m_DocumentFilePath, m_CreatedConfigurationID, null))
            {
                Log.Debug("ExampleDiscovery", "Failed to add document");
            }

            while (!m_AddDocumentTested)
            {
                yield return(null);
            }
            #endregion

            #region Query
            //  Query
            Log.Debug("ExampleDiscoveryV1", "Attempting to query");
            if (!m_Discovery.Query((QueryResponse resp, string data) =>
            {
                if (resp != null)
                {
                    Log.Debug("ExampleDiscoveryV1", resp.ToString());
                }
                else
                {
                    Log.Debug("ExampleDiscoveryV1", "resp is null, {0}", data);
                }

                Test(resp != null);
                m_QueryTested = true;
            }, m_CreatedEnvironmentID, m_CreatedCollectionID, null, m_Query, null, 10, null, 0))
            {
                Log.Debug("ExampleDiscovery", "Failed to query");
            }

            while (!m_QueryTested)
            {
                yield return(null);
            }
            #endregion

            #region Document
            //  Get Document
            Log.Debug("ExampleDiscoveryV1", "Attempting to get document");
            if (!m_Discovery.GetDocument((DocumentStatus resp, string data) =>
            {
                if (resp != null)
                {
                    Log.Debug("ExampleDiscoveryV1", "Got Document {0} {1}", resp.document_id, resp.status);
                }
                else
                {
                    Log.Debug("ExampleDiscoveryV1", "Discovery.GetDocument(); resp is null, {0}", data);
                }

                Test(!string.IsNullOrEmpty(resp.status));
                m_GetDocumentTested = true;
            }, m_CreatedEnvironmentID, m_CreatedCollectionID, m_CreatedDocumentID))
            {
                Log.Debug("ExampleDiscovery", "Failed to get document");
            }

            while (!m_GetDocumentTested)
            {
                yield return(null);
            }

            //  Update Document
            Log.Debug("ExampleDiscoveryV1", "Attempting to update document");
            if (!m_Discovery.UpdateDocument((DocumentAccepted resp, string data) =>
            {
                if (resp != null)
                {
                    Log.Debug("ExampleDiscoveryV1", "Updated Document {0} {1}", resp.document_id, resp.status);
                }
                else
                {
                    Log.Debug("ExampleDiscoveryV1", "Discovery.UpdateDocument(); resp is null, {0}", data);
                }

                Test(!string.IsNullOrEmpty(resp.status));
                m_UpdateDocumentTested = true;
            }, m_CreatedEnvironmentID, m_CreatedCollectionID, m_CreatedDocumentID, m_DocumentFilePath, m_CreatedConfigurationID, null))
            {
                Log.Debug("ExampleDiscovery", "Failed to update document");
            }

            while (!m_UpdateDocumentTested)
            {
                yield return(null);
            }

            //  Delete Document
            Runnable.Run(DeleteDocument());

            while (!m_DeleteDocumentTested)
            {
                yield return(null);
            }
            #endregion

            #region Collection
            //  Get Collection
            Log.Debug("ExampleDiscoveryV1", "Attempting to get collection");
            if (!m_Discovery.GetCollection((Collection resp, string data) =>
            {
                if (resp != null)
                {
                    Log.Debug("ExampleDiscoveryV1", "Collection: {0}, {1}", resp.collection_id, resp.name);
                }
                else
                {
                    Log.Debug("ExampleDiscoveryV1", "Failed to get collections");
                }

                Test(!string.IsNullOrEmpty(resp.name));
                m_GetCollectionTested = true;
            }, m_CreatedEnvironmentID, m_CreatedCollectionID))
            {
                Log.Debug("ExampleDiscovery", "Failed to get collection");
            }

            while (!m_GetCollectionTested)
            {
                yield return(null);
            }

            //  Delete Collection
            Runnable.Run(DeleteCollection());
            while (!m_DeleteCollectionTested)
            {
                yield return(null);
            }
            #endregion

            #region Configuration
            //  Get Configuration
            Log.Debug("ExampleDiscoveryV1", "Attempting to get configuration");
            if (!m_Discovery.GetConfiguration((Configuration resp, string data) =>
            {
                if (resp != null)
                {
                    Log.Debug("ExampleDiscoveryV1", "Configuration: {0}, {1}", resp.configuration_id, resp.name);
                }
                else
                {
                    Log.Debug("ExampleDiscoveryV1", "Discovery.GetConfiguration(); resp is null, {0}", data);
                }

                Test(!string.IsNullOrEmpty(resp.name));
                m_GetConfigurationTested = true;
            }, m_CreatedEnvironmentID, m_CreatedConfigurationID))
            {
                Log.Debug("ExampleDiscoveryV1", "Failed to get configuration");
            }

            while (!m_GetConfigurationTested)
            {
                yield return(null);
            }

            //  Preview Configuration
            Log.Debug("ExampleDiscoveryV1", "Attempting to preview configuration");
            if (!m_Discovery.PreviewConfiguration((TestDocument resp, string data) =>
            {
                if (resp != null)
                {
                    Log.Debug("ExampleDiscoveryV1", "Preview succeeded: {0}", resp.status);
                }
                else
                {
                    Log.Debug("ExampleDiscoveryV1", "Discovery.PreviewConfiguration(); resp is null {0}", data);
                }

                Test(resp != null);
                m_PreviewConfigurationTested = true;
            }, m_CreatedEnvironmentID, m_CreatedConfigurationID, null, m_FilePathToIngest, m_Metadata))
            {
                Log.Debug("ExampleDiscoveryV1", "Failed to preview configuration");
            }

            while (!m_PreviewConfigurationTested)
            {
                yield return(null);
            }

            //  Delete Configuration
            Runnable.Run(DeleteConfiguration());
            while (!m_DeleteConfigurationTested)
            {
                yield return(null);
            }
            #endregion

            #region Environment
            //  Get Environment
            Log.Debug("ExampleDiscoveryV1", "Attempting to get environment");
            if (!m_Discovery.GetEnvironment((Environment resp, string data) =>
            {
                if (resp != null)
                {
                    Log.Debug("ExampleDiscoveryV1", "environment_name: {0}", resp.name);
                }
                else
                {
                    Log.Debug("ExampleDiscoveryV1", "Discovery.GetEnvironment(); resp is null");
                }

                Test(!string.IsNullOrEmpty(resp.name));
                m_GetEnvironmentTested = true;
            }, m_CreatedEnvironmentID))
            {
                Log.Debug("ExampleDiscoveryV1", "Failed to get environment");
            }

            while (!m_GetEnvironmentTested)
            {
                yield return(null);
            }

            //  Delete Environment
            Runnable.Run(DeleteEnvironment());

            while (!m_DeleteEnvironmentTested)
            {
                yield return(null);
            }
            #endregion

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

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

            string result = null;

            var vcapUrl      = System.Environment.GetEnvironmentVariable("VCAP_URL");
            var vcapUsername = System.Environment.GetEnvironmentVariable("VCAP_USERNAME");
            var vcapPassword = System.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["discovery"];

            _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
            //};

            _discovery             = new Discovery(credentials);
            _discovery.VersionDate = _discoveryVersionDate;
            _configurationJsonPath = Application.dataPath + "/Watson/Examples/ServiceExamples/TestData/Discovery/exampleConfigurationData.json";
            _filePathToIngest      = Application.dataPath + "/Watson/Examples/ServiceExamples/TestData/watson_beats_jeopardy.html";
            _documentFilePath      = Application.dataPath + "/Watson/Examples/ServiceExamples/TestData/watson_beats_jeopardy.html";

            //  Get Environments
            Log.Debug("TestDiscovery.RunTest()", "Attempting to get environments");
            if (!_discovery.GetEnvironments(OnGetEnvironments, OnFail))
            {
                Log.Debug("TestDiscovery.GetEnvironments()", "Failed to get environments");
            }
            while (!_getEnvironmentsTested)
            {
                yield return(null);
            }

            //  AddEnvironment
            Log.Debug("TestDiscovery.RunTest()", "Attempting to add environment");
            if (!_discovery.AddEnvironment(OnAddEnvironment, OnFail, "unity-testing-AddEnvironment-do-not-delete-until-active", "Testing addEnvironment in Unity SDK. Please do not delete this environment until the status is 'active'", 1))
            {
                Log.Debug("TestDiscovery.AddEnvironment()", "Failed to add environment");
            }
            while (!_addEnvironmentTested)
            {
                yield return(null);
            }

            //  Wait for environment to be ready
            Runnable.Run(CheckEnvironmentState(_waitTime));
            while (!_isEnvironmentReady)
            {
                yield return(null);
            }

            //  GetEnvironment
            Log.Debug("TestDiscovery.RunTest()", "Attempting to get environment");
            if (!_discovery.GetEnvironment(OnGetEnvironment, OnFail, _createdEnvironmentID))
            {
                Log.Debug("TestDiscovery.GetEnvironment()", "Failed to get environment");
            }
            while (!_getEnvironmentTested)
            {
                yield return(null);
            }

            //  Get Configurations
            Log.Debug("TestDiscovery.RunTest()", "Attempting to get configurations");
            if (!_discovery.GetConfigurations(OnGetConfigurations, OnFail, _createdEnvironmentID))
            {
                Log.Debug("TestDiscovery.GetConfigurations()", "Failed to get configurations");
            }
            while (!_getConfigurationsTested)
            {
                yield return(null);
            }

            //  Add Configuration
            Log.Debug("TestDiscovery.RunTest()", "Attempting to add configuration");
            if (!_discovery.AddConfiguration(OnAddConfiguration, OnFail, _createdEnvironmentID, _configurationJsonPath))
            {
                Log.Debug("TestDiscovery.AddConfiguration()", "Failed to add configuration");
            }
            while (!_addConfigurationTested)
            {
                yield return(null);
            }

            //  Get Configuration
            Log.Debug("TestDiscovery.RunTest()", "Attempting to get configuration");
            if (!_discovery.GetConfiguration(OnGetConfiguration, OnFail, _createdEnvironmentID, _createdConfigurationID))
            {
                Log.Debug("TestDiscovery.GetConfiguration()", "Failed to get configuration");
            }
            while (!_getConfigurationTested)
            {
                yield return(null);
            }

            //  Preview Configuration
            Log.Debug("TestDiscovery.RunTest()", "Attempting to preview configuration");
            if (!_discovery.PreviewConfiguration(OnPreviewConfiguration, OnFail, _createdEnvironmentID, _createdConfigurationID, null, _filePathToIngest, _metadata))
            {
                Log.Debug("TestDiscovery.PreviewConfiguration()", "Failed to preview configuration");
            }
            while (!_previewConfigurationTested)
            {
                yield return(null);
            }

            //  Get Collections
            Log.Debug("TestDiscovery.RunTest()", "Attempting to get collections");
            if (!_discovery.GetCollections(OnGetCollections, OnFail, _createdEnvironmentID))
            {
                Log.Debug("TestDiscovery.GetCollections()", "Failed to get collections");
            }
            while (!_getCollectionsTested)
            {
                yield return(null);
            }

            //  Add Collection
            Log.Debug("TestDiscovery.RunTest()", "Attempting to add collection");
            if (!_discovery.AddCollection(OnAddCollection, OnFail, _createdEnvironmentID, _createdCollectionName, _createdCollectionDescription, _createdConfigurationID))
            {
                Log.Debug("TestDiscovery.AddCollection()", "Failed to add collection");
            }
            while (!_addCollectionTested)
            {
                yield return(null);
            }

            //  Get Collection
            Log.Debug("TestDiscovery.RunTest()", "Attempting to get collection");
            if (!_discovery.GetCollection(OnGetCollection, OnFail, _createdEnvironmentID, _createdCollectionID))
            {
                Log.Debug("TestDiscovery.GetCollection()", "Failed to get collection");
            }
            while (!_getCollectionTested)
            {
                yield return(null);
            }

            if (!_discovery.GetFields(OnGetFields, OnFail, _createdEnvironmentID, _createdCollectionID))
            {
                Log.Debug("TestDiscovery.GetFields()", "Failed to get fields");
            }
            while (!_getFieldsTested)
            {
                yield return(null);
            }

            //  Add Document
            Log.Debug("TestDiscovery.RunTest()", "Attempting to add document");
            if (!_discovery.AddDocument(OnAddDocument, OnFail, _createdEnvironmentID, _createdCollectionID, _documentFilePath, _createdConfigurationID, null))
            {
                Log.Debug("TestDiscovery.AddDocument()", "Failed to add document");
            }
            while (!_addDocumentTested)
            {
                yield return(null);
            }

            //  Get Document
            Log.Debug("TestDiscovery.RunTest()", "Attempting to get document");
            if (!_discovery.GetDocument(OnGetDocument, OnFail, _createdEnvironmentID, _createdCollectionID, _createdDocumentID))
            {
                Log.Debug("TestDiscovery.GetDocument()", "Failed to get document");
            }
            while (!_getDocumentTested)
            {
                yield return(null);
            }

            //  Update Document
            Log.Debug("TestDiscovery.RunTest()", "Attempting to update document");
            if (!_discovery.UpdateDocument(OnUpdateDocument, OnFail, _createdEnvironmentID, _createdCollectionID, _createdDocumentID, _documentFilePath, _createdConfigurationID, null))
            {
                Log.Debug("TestDiscovery.UpdateDocument()", "Failed to update document");
            }
            while (!_updateDocumentTested)
            {
                yield return(null);
            }

            //  Query
            Log.Debug("TestDiscovery.RunTest()", "Attempting to query");
            if (!_discovery.Query(OnQuery, OnFail, _createdEnvironmentID, _createdCollectionID, null, _query, null, 10, null, 0))
            {
                Log.Debug("TestDiscovery.Query()", "Failed to query");
            }
            while (!_queryTested)
            {
                yield return(null);
            }

            //  Delete Document
            Log.Debug("TestDiscovery.RunTest()", "Attempting to delete document {0}", _createdDocumentID);
            if (!_discovery.DeleteDocument(OnDeleteDocument, OnFail, _createdEnvironmentID, _createdCollectionID, _createdDocumentID))
            {
                Log.Debug("TestDiscovery.DeleteDocument()", "Failed to delete document");
            }
            while (!_deleteDocumentTested)
            {
                yield return(null);
            }

            //  Delay
            Log.Debug("TestDiscovery.RunTest()", "Delaying delete collection for 10 sec");
            Runnable.Run(Delay(_waitTime));
            while (!_readyToContinue)
            {
                yield return(null);
            }

            _isEnvironmentReady = false;
            Runnable.Run(CheckEnvironmentState(_waitTime));
            while (!_isEnvironmentReady)
            {
                yield return(null);
            }

            _readyToContinue = false;
            //  Delete Collection
            Log.Debug("TestDiscovery.RunTest()", "Attempting to delete collection {0}", _createdCollectionID);
            if (!_discovery.DeleteCollection(OnDeleteCollection, OnFail, _createdEnvironmentID, _createdCollectionID))
            {
                Log.Debug("TestDiscovery.DeleteCollection()", "Failed to add collection");
            }
            while (!_deleteCollectionTested)
            {
                yield return(null);
            }

            //  Delay
            Log.Debug("TestDiscovery.RunTest()", "Delaying delete configuration for 10 sec");
            Runnable.Run(Delay(_waitTime));
            while (!_readyToContinue)
            {
                yield return(null);
            }

            _isEnvironmentReady = false;
            Runnable.Run(CheckEnvironmentState(_waitTime));
            while (!_isEnvironmentReady)
            {
                yield return(null);
            }

            _readyToContinue = false;
            //  Delete Configuration
            Log.Debug("TestDiscovery.RunTest()", "Attempting to delete configuration {0}", _createdConfigurationID);
            if (!_discovery.DeleteConfiguration(OnDeleteConfiguration, OnFail, _createdEnvironmentID, _createdConfigurationID))
            {
                Log.Debug("TestDiscovery.DeleteConfiguration()", "Failed to delete configuration");
            }
            while (!_deleteConfigurationTested)
            {
                yield return(null);
            }

            //  Delay
            Log.Debug("TestDiscovery.RunTest()", "Delaying delete environment for 10 sec");
            Runnable.Run(Delay(_waitTime));
            while (!_readyToContinue)
            {
                yield return(null);
            }

            _isEnvironmentReady = false;
            Runnable.Run(CheckEnvironmentState(_waitTime));
            while (!_isEnvironmentReady)
            {
                yield return(null);
            }

            _readyToContinue = false;
            //  Delete Environment
            Log.Debug("TestDiscovery.RunTest()", "Attempting to delete environment {0}", _createdEnvironmentID);
            if (!_discovery.DeleteEnvironment(OnDeleteEnvironment, OnFail, _createdEnvironmentID))
            {
                Log.Debug("TestDiscovery.DeleteEnvironment()", "Failed to delete environment");
            }
            while (!_deleteEnvironmentTested)
            {
                yield return(null);
            }

            if (!string.IsNullOrEmpty(_createdEnvironmentID))
            {
                if (!_discovery.GetEnvironment(OnGetEnvironment, OnFail, _createdEnvironmentID))
                {
                    _discovery.DeleteEnvironment(OnDeleteEnvironment, OnFail, _createdEnvironmentID);
                }
            }

            Log.Debug("TestDiscovery.RunTest()", "Discovery examples complete.");

            yield break;
        }
Example #7
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 = System.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["discovery"][TestCredentialIndex].Credentials;
                _username = credential.Username.ToString();
                _password = credential.Password.ToString();
                _url      = credential.Url.ToString();
            }
            catch
            {
                Log.Debug("TestDiscovery", "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
            //};

            _discovery             = new Discovery(credentials);
            _discovery.VersionDate = _discoveryVersionDate;
            _configurationJsonPath = Application.dataPath + "/Watson/Examples/ServiceExamples/TestData/Discovery/exampleConfigurationData.json";
            _filePathToIngest      = Application.dataPath + "/Watson/Examples/ServiceExamples/TestData/watson_beats_jeopardy.html";
            _documentFilePath      = Application.dataPath + "/Watson/Examples/ServiceExamples/TestData/watson_beats_jeopardy.html";

            //  Get Environments
            Log.Debug("ExampleDiscoveryV1", "Attempting to get environments");
            if (!_discovery.GetEnvironments(OnGetEnvironments))
            {
                Log.Debug("ExampleDiscoveryV1", "Failed to get environments");
            }
            while (!_getEnvironmentsTested)
            {
                yield return(null);
            }

            //  AddEnvironment
            Log.Debug("ExampleDiscoveryV1", "Attempting to add environment");
            if (!_discovery.AddEnvironment(OnAddEnvironment, "unity-testing-AddEnvironment-do-not-delete-until-active", "Testing addEnvironment in Unity SDK. Please do not delete this environment until the status is 'active'", 1))
            {
                Log.Debug("ExampleDiscoveryV1", "Failed to add environment");
            }
            while (!_addEnvironmentTested)
            {
                yield return(null);
            }

            //  Wait for environment to be ready
            Runnable.Run(CheckEnvironmentState(_waitTime));
            while (!_isEnvironmentReady)
            {
                yield return(null);
            }

            //  GetEnvironment
            Log.Debug("ExampleDiscoveryV1", "Attempting to get environment");
            if (!_discovery.GetEnvironment(OnGetEnvironment, _createdEnvironmentID))
            {
                Log.Debug("ExampleDiscoveryV1", "Failed to get environment");
            }
            while (!_getEnvironmentTested)
            {
                yield return(null);
            }

            //  Get Configurations
            Log.Debug("ExampleDiscoveryV1", "Attempting to get configurations");
            if (!_discovery.GetConfigurations(OnGetConfigurations, _createdEnvironmentID))
            {
                Log.Debug("ExampleDiscoveryV1", "Failed to get configurations");
            }
            while (!_getConfigurationsTested)
            {
                yield return(null);
            }

            //  Add Configuration
            Log.Debug("ExampleDiscoveryV1", "Attempting to add configuration");
            if (!_discovery.AddConfiguration(OnAddConfiguration, _createdEnvironmentID, _configurationJsonPath))
            {
                Log.Debug("ExampleDiscoveryV1", "Failed to add configuration");
            }
            while (!_addConfigurationTested)
            {
                yield return(null);
            }

            //  Get Configuration
            Log.Debug("ExampleDiscoveryV1", "Attempting to get configuration");
            if (!_discovery.GetConfiguration(OnGetConfiguration, _createdEnvironmentID, _createdConfigurationID))
            {
                Log.Debug("ExampleDiscoveryV1", "Failed to get configuration");
            }
            while (!_getConfigurationTested)
            {
                yield return(null);
            }

            //  Preview Configuration
            Log.Debug("ExampleDiscoveryV1", "Attempting to preview configuration");
            if (!_discovery.PreviewConfiguration(OnPreviewConfiguration, _createdEnvironmentID, _createdConfigurationID, null, _filePathToIngest, _metadata))
            {
                Log.Debug("ExampleDiscoveryV1", "Failed to preview configuration");
            }
            while (!_previewConfigurationTested)
            {
                yield return(null);
            }

            //  Get Collections
            Log.Debug("ExampleDiscoveryV1", "Attempting to get collections");
            if (!_discovery.GetCollections(OnGetCollections, _createdEnvironmentID))
            {
                Log.Debug("ExampleDiscovery", "Failed to get collections");
            }
            while (!_getCollectionsTested)
            {
                yield return(null);
            }

            //  Add Collection
            Log.Debug("ExampleDiscoveryV1", "Attempting to add collection");
            if (!_discovery.AddCollection(OnAddCollection, _createdEnvironmentID, _createdCollectionName, _createdCollectionDescription, _createdConfigurationID))
            {
                Log.Debug("ExampleDiscovery", "Failed to add collection");
            }
            while (!_addCollectionTested)
            {
                yield return(null);
            }

            //  Get Collection
            Log.Debug("ExampleDiscoveryV1", "Attempting to get collection");
            if (!_discovery.GetCollection(OnGetCollection, _createdEnvironmentID, _createdCollectionID))
            {
                Log.Debug("ExampleDiscovery", "Failed to get collection");
            }
            while (!_getCollectionTested)
            {
                yield return(null);
            }

            if (!_discovery.GetFields(OnGetFields, _createdEnvironmentID, _createdCollectionID))
            {
                Log.Debug("ExampleDiscovery", "Failed to get fields");
            }
            while (!_getFieldsTested)
            {
                yield return(null);
            }

            //  Add Document
            Log.Debug("ExampleDiscoveryV1", "Attempting to add document");
            if (!_discovery.AddDocument(OnAddDocument, _createdEnvironmentID, _createdCollectionID, _documentFilePath, _createdConfigurationID, null))
            {
                Log.Debug("ExampleDiscovery", "Failed to add document");
            }
            while (!_addDocumentTested)
            {
                yield return(null);
            }

            //  Get Document
            Log.Debug("ExampleDiscoveryV1", "Attempting to get document");
            if (!_discovery.GetDocument(OnGetDocument, _createdEnvironmentID, _createdCollectionID, _createdDocumentID))
            {
                Log.Debug("ExampleDiscovery", "Failed to get document");
            }
            while (!_getDocumentTested)
            {
                yield return(null);
            }

            //  Update Document
            Log.Debug("ExampleDiscoveryV1", "Attempting to update document");
            if (!_discovery.UpdateDocument(OnUpdateDocument, _createdEnvironmentID, _createdCollectionID, _createdDocumentID, _documentFilePath, _createdConfigurationID, null))
            {
                Log.Debug("ExampleDiscovery", "Failed to update document");
            }
            while (!_updateDocumentTested)
            {
                yield return(null);
            }

            //  Query
            Log.Debug("ExampleDiscoveryV1", "Attempting to query");
            if (!_discovery.Query(OnQuery, _createdEnvironmentID, _createdCollectionID, null, _query, null, 10, null, 0))
            {
                Log.Debug("ExampleDiscovery", "Failed to query");
            }
            while (!_queryTested)
            {
                yield return(null);
            }

            //  Delete Document
            Log.Debug("ExampleDiscoveryV1", "Attempting to delete document {0}", _createdDocumentID);
            if (!_discovery.DeleteDocument(OnDeleteDocument, _createdEnvironmentID, _createdCollectionID, _createdDocumentID))
            {
                Log.Debug("ExampleDiscovery", "Failed to delete document");
            }
            while (!_deleteDocumentTested)
            {
                yield return(null);
            }

            //  Delay
            Log.Debug("ExampleDiscovery", "Delaying delete collection for 10 sec");
            Runnable.Run(Delay(_waitTime));
            while (!_readyToContinue)
            {
                yield return(null);
            }

            _isEnvironmentReady = false;
            Runnable.Run(CheckEnvironmentState(_waitTime));
            while (!_isEnvironmentReady)
            {
                yield return(null);
            }

            _readyToContinue = false;
            //  Delete Collection
            Log.Debug("ExampleDiscoveryV1", "Attempting to delete collection {0}", _createdCollectionID);
            if (!_discovery.DeleteCollection(OnDeleteCollection, _createdEnvironmentID, _createdCollectionID))
            {
                Log.Debug("ExampleDiscovery", "Failed to add collection");
            }
            while (!_deleteCollectionTested)
            {
                yield return(null);
            }

            //  Delay
            Log.Debug("ExampleDiscovery", "Delaying delete configuration for 10 sec");
            Runnable.Run(Delay(_waitTime));
            while (!_readyToContinue)
            {
                yield return(null);
            }

            _isEnvironmentReady = false;
            Runnable.Run(CheckEnvironmentState(_waitTime));
            while (!_isEnvironmentReady)
            {
                yield return(null);
            }

            _readyToContinue = false;
            //  Delete Configuration
            Log.Debug("ExampleDiscoveryV1", "Attempting to delete configuration {0}", _createdConfigurationID);
            if (!_discovery.DeleteConfiguration(OnDeleteConfiguration, _createdEnvironmentID, _createdConfigurationID))
            {
                Log.Debug("ExampleDiscoveryV1", "Failed to delete configuration");
            }
            while (!_deleteConfigurationTested)
            {
                yield return(null);
            }

            //  Delay
            Log.Debug("ExampleDiscovery", "Delaying delete environment for 10 sec");
            Runnable.Run(Delay(_waitTime));
            while (!_readyToContinue)
            {
                yield return(null);
            }

            _isEnvironmentReady = false;
            Runnable.Run(CheckEnvironmentState(_waitTime));
            while (!_isEnvironmentReady)
            {
                yield return(null);
            }

            _readyToContinue = false;
            //  Delete Environment
            Log.Debug("ExampleDiscoveryV1", "Attempting to delete environment {0}", _createdEnvironmentID);
            if (!_discovery.DeleteEnvironment(OnDeleteEnvironment, _createdEnvironmentID))
            {
                Log.Debug("ExampleDiscoveryV1", "Failed to delete environment");
            }
            while (!_deleteEnvironmentTested)
            {
                yield return(null);
            }

            if (!string.IsNullOrEmpty(_createdEnvironmentID))
            {
                if (!_discovery.GetEnvironment(OnGetEnvironment, _createdEnvironmentID))
                {
                    _discovery.DeleteEnvironment(OnDeleteEnvironment, _createdEnvironmentID);
                }
            }

            Log.Debug("ExampleDiscoveryV1", "Discovery examples complete.");

            yield break;
        }
Example #8
0
    private IEnumerator Examples()
    {
        //  Get Environments
        Log.Debug("ExampleDiscoveryV1.Examples()", "Attempting to get environments");
        if (!_discovery.GetEnvironments(OnGetEnvironments, OnFail))
        {
            Log.Debug("ExampleDiscoveryV1.GetEnvironments()", "Failed to get environments");
        }
        while (!_getEnvironmentsTested)
        {
            yield return(null);
        }

        //  AddEnvironment
        Log.Debug("ExampleDiscovery.Examples()", "Attempting to add environment");
        if (!_discovery.AddEnvironment(OnAddEnvironment, OnFail, "unity-testing-AddEnvironment-do-not-delete-until-active", "Testing addEnvironment in Unity SDK. Please do not delete this environment until the status is 'active'", 1))
        {
            Log.Debug("ExampleDiscovery.AddEnvironment()", "Failed to add environment");
        }
        while (!_addEnvironmentTested)
        {
            yield return(null);
        }

        //  Wait for environment to be ready
        CheckEnvironmentState();
        while (!_isEnvironmentReady)
        {
            yield return(null);
        }

        //  GetEnvironment
        Log.Debug("ExampleDiscovery.Examples()", "Attempting to get environment");
        if (!_discovery.GetEnvironment(OnGetEnvironment, OnFail, _createdEnvironmentID))
        {
            Log.Debug("ExampleDiscovery.GetEnvironment()", "Failed to get environment");
        }
        while (!_getEnvironmentTested)
        {
            yield return(null);
        }

        //  Get Configurations
        Log.Debug("ExampleDiscovery.Examples()", "Attempting to get configurations");
        if (!_discovery.GetConfigurations(OnGetConfigurations, OnFail, _createdEnvironmentID))
        {
            Log.Debug("ExampleDiscovery.GetConfigurations()", "Failed to get configurations");
        }
        while (!_getConfigurationsTested)
        {
            yield return(null);
        }

        //  Add Configuration
        Log.Debug("ExampleDiscovery.Examples()", "Attempting to add configuration");
        if (!_discovery.AddConfiguration(OnAddConfiguration, OnFail, _createdEnvironmentID, _configurationJsonPath))
        {
            Log.Debug("ExampleDiscovery.AddConfiguration()", "Failed to add configuration");
        }
        while (!_addConfigurationTested)
        {
            yield return(null);
        }

        //  Get Configuration
        Log.Debug("ExampleDiscovery.Examples()", "Attempting to get configuration");
        if (!_discovery.GetConfiguration(OnGetConfiguration, OnFail, _createdEnvironmentID, _createdConfigurationID))
        {
            Log.Debug("ExampleDiscovery.GetConfiguration()", "Failed to get configuration");
        }
        while (!_getConfigurationTested)
        {
            yield return(null);
        }

        //  Preview Configuration
        Log.Debug("ExampleDiscovery.Examples()", "Attempting to preview configuration");
        if (!_discovery.PreviewConfiguration(OnPreviewConfiguration, OnFail, _createdEnvironmentID, _createdConfigurationID, null, _filePathToIngest, _metadata))
        {
            Log.Debug("ExampleDiscovery.PreviewConfiguration()", "Failed to preview configuration");
        }
        while (!_previewConfigurationTested)
        {
            yield return(null);
        }

        //  Get Collections
        Log.Debug("ExampleDiscovery.Examples()", "Attempting to get collections");
        if (!_discovery.GetCollections(OnGetCollections, OnFail, _createdEnvironmentID))
        {
            Log.Debug("ExampleDiscovery.GetCollections()", "Failed to get collections");
        }
        while (!_getCollectionsTested)
        {
            yield return(null);
        }

        //  Add Collection
        Log.Debug("ExampleDiscovery.Examples()", "Attempting to add collection");
        if (!_discovery.AddCollection(OnAddCollection, OnFail, _createdEnvironmentID, _createdCollectionName, _createdCollectionDescription, _createdConfigurationID))
        {
            Log.Debug("ExampleDiscovery.AddCollection()", "Failed to add collection");
        }
        while (!_addCollectionTested)
        {
            yield return(null);
        }

        //  Get Collection
        Log.Debug("ExampleDiscovery.Examples()", "Attempting to get collection");
        if (!_discovery.GetCollection(OnGetCollection, OnFail, _createdEnvironmentID, _createdCollectionID))
        {
            Log.Debug("ExampleDiscovery.GetCollection()", "Failed to get collection");
        }
        while (!_getCollectionTested)
        {
            yield return(null);
        }

        //  Get fields
        if (!_discovery.GetFields(OnGetFields, OnFail, _createdEnvironmentID, _createdCollectionID))
        {
            Log.Debug("ExampleDiscovery.GetFields()", "Failed to get fields");
        }
        while (!_getFieldsTested)
        {
            yield return(null);
        }

        //  Add Document
        Log.Debug("ExampleDiscovery.Examples()", "Attempting to add document");
        if (!_discovery.AddDocument(OnAddDocument, OnFail, _createdEnvironmentID, _createdCollectionID, _documentFilePath, _createdConfigurationID, null))
        {
            Log.Debug("ExampleDiscovery.AddDocument()", "Failed to add document");
        }
        while (!_addDocumentTested)
        {
            yield return(null);
        }

        //  Get Document
        Log.Debug("ExampleDiscovery.Examples()", "Attempting to get document");
        if (!_discovery.GetDocument(OnGetDocument, OnFail, _createdEnvironmentID, _createdCollectionID, _createdDocumentID))
        {
            Log.Debug("ExampleDiscovery.GetDocument()", "Failed to get document");
        }
        while (!_getDocumentTested)
        {
            yield return(null);
        }

        //  Update Document
        Log.Debug("ExampleDiscovery.Examples()", "Attempting to update document");
        if (!_discovery.UpdateDocument(OnUpdateDocument, OnFail, _createdEnvironmentID, _createdCollectionID, _createdDocumentID, _documentFilePath, _createdConfigurationID, null))
        {
            Log.Debug("ExampleDiscovery.UpdateDocument()", "Failed to update document");
        }
        while (!_updateDocumentTested)
        {
            yield return(null);
        }

        //  Query
        Log.Debug("ExampleDiscovery.Examples()", "Attempting to query");
        if (!_discovery.Query(OnQuery, OnFail, _createdEnvironmentID, _createdCollectionID, null, _query, null, 10, null, 0))
        {
            Log.Debug("ExampleDiscovery.Query()", "Failed to query");
        }
        while (!_queryTested)
        {
            yield return(null);
        }

        //  Delete Document
        Log.Debug("ExampleDiscovery.Examples()", "Attempting to delete document {0}", _createdDocumentID);
        if (!_discovery.DeleteDocument(OnDeleteDocument, OnFail, _createdEnvironmentID, _createdCollectionID, _createdDocumentID))
        {
            Log.Debug("ExampleDiscovery.DeleteDocument()", "Failed to delete document");
        }
        while (!_deleteDocumentTested)
        {
            yield return(null);
        }

        //  Delay
        Log.Debug("ExampleDiscovery.Examples()", "Delaying delete collection for 10 sec");
        Invoke("Delay", 10f);
        while (!_readyToContinue)
        {
            yield return(null);
        }

        _readyToContinue = false;
        //  Delete Collection
        Log.Debug("ExampleDiscovery.Examples()", "Attempting to delete collection {0}", _createdCollectionID);
        if (!_discovery.DeleteCollection(OnDeleteCollection, OnFail, _createdEnvironmentID, _createdCollectionID))
        {
            Log.Debug("ExampleDiscovery.DeleteCollection()", "Failed to add collection");
        }
        while (!_deleteCollectionTested)
        {
            yield return(null);
        }

        //  Delay
        Log.Debug("ExampleDiscovery.Examples()", "Delaying delete configuration for 10 sec");
        Invoke("Delay", 10f);
        while (!_readyToContinue)
        {
            yield return(null);
        }

        _readyToContinue = false;
        //  Delete Configuration
        Log.Debug("ExampleDiscovery.Examples()", "Attempting to delete configuration {0}", _createdConfigurationID);
        if (!_discovery.DeleteConfiguration(OnDeleteConfiguration, OnFail, _createdEnvironmentID, _createdConfigurationID))
        {
            Log.Debug("ExampleDiscovery.DeleteConfiguration()", "Failed to delete configuration");
        }
        while (!_deleteConfigurationTested)
        {
            yield return(null);
        }

        //  Delay
        Log.Debug("ExampleDiscovery.Examples()", "Delaying delete environment for 10 sec");
        Invoke("Delay", 10f);
        while (!_readyToContinue)
        {
            yield return(null);
        }

        _readyToContinue = false;
        //  Delete Environment
        Log.Debug("ExampleDiscovery.Examples()", "Attempting to delete environment {0}", _createdEnvironmentID);
        if (!_discovery.DeleteEnvironment(OnDeleteEnvironment, OnFail, _createdEnvironmentID))
        {
            Log.Debug("ExampleDiscovery.DeleteEnvironment()", "Failed to delete environment");
        }
        while (!_deleteEnvironmentTested)
        {
            yield return(null);
        }

        Log.Debug("ExampleDiscovery.Examples()", "Discovery examples complete.");
    }
    private IEnumerator Examples()
    {
        //  Get Environments
        Log.Debug("TestDiscovery.RunTest()", "Attempting to get environments");
        if (!_service.GetEnvironments(OnGetEnvironments, OnFail))
        {
            Log.Debug("TestDiscovery.GetEnvironments()", "Failed to get environments");
        }
        while (!_getEnvironmentsTested)
        {
            yield return(null);
        }

        //  Wait for environment to be ready
        Runnable.Run(CheckEnvironmentState(0f));
        while (!_isEnvironmentReady)
        {
            yield return(null);
        }

        //  GetEnvironment
        Log.Debug("TestDiscovery.RunTest()", "Attempting to get environment");
        if (!_service.GetEnvironment(OnGetEnvironment, OnFail, _environmentId))
        {
            Log.Debug("TestDiscovery.GetEnvironment()", "Failed to get environment");
        }
        while (!_getEnvironmentTested)
        {
            yield return(null);
        }

        //  Get Configurations
        Log.Debug("TestDiscovery.RunTest()", "Attempting to get configurations");
        if (!_service.GetConfigurations(OnGetConfigurations, OnFail, _environmentId))
        {
            Log.Debug("TestDiscovery.GetConfigurations()", "Failed to get configurations");
        }
        while (!_getConfigurationsTested)
        {
            yield return(null);
        }

        //  Add Configuration
        Log.Debug("TestDiscovery.RunTest()", "Attempting to add configuration");
        if (!_service.AddConfiguration(OnAddConfiguration, OnFail, _environmentId, _configurationJson.Replace("{guid}", System.Guid.NewGuid().ToString())))
        {
            Log.Debug("TestDiscovery.AddConfiguration()", "Failed to add configuration");
        }
        while (!_addConfigurationTested)
        {
            yield return(null);
        }

        //  Get Configuration
        Log.Debug("TestDiscovery.RunTest()", "Attempting to get configuration");
        if (!_service.GetConfiguration(OnGetConfiguration, OnFail, _environmentId, _environmentId))
        {
            Log.Debug("TestDiscovery.GetConfiguration()", "Failed to get configuration");
        }
        while (!_getConfigurationTested)
        {
            yield return(null);
        }

        //  Preview Configuration
        Log.Debug("TestDiscovery.RunTest()", "Attempting to preview configuration");
        if (!_service.PreviewConfiguration(OnPreviewConfiguration, OnFail, _environmentId, _environmentId, null, _filePathToIngest, _metadata))
        {
            Log.Debug("TestDiscovery.PreviewConfiguration()", "Failed to preview configuration");
        }
        while (!_previewConfigurationTested)
        {
            yield return(null);
        }

        //  Get Collections
        Log.Debug("TestDiscovery.RunTest()", "Attempting to get collections");
        if (!_service.GetCollections(OnGetCollections, OnFail, _environmentId))
        {
            Log.Debug("TestDiscovery.GetCollections()", "Failed to get collections");
        }
        while (!_getCollectionsTested)
        {
            yield return(null);
        }

        //  Add Collection
        Log.Debug("TestDiscovery.RunTest()", "Attempting to add collection");
        if (!_service.AddCollection(OnAddCollection, OnFail, _environmentId, _createdCollectionName + System.Guid.NewGuid().ToString(), _createdCollectionDescription, _environmentId))
        {
            Log.Debug("TestDiscovery.AddCollection()", "Failed to add collection");
        }
        while (!_addCollectionTested)
        {
            yield return(null);
        }

        //  Get Collection
        Log.Debug("TestDiscovery.RunTest()", "Attempting to get collection");
        if (!_service.GetCollection(OnGetCollection, OnFail, _environmentId, _createdCollectionID))
        {
            Log.Debug("TestDiscovery.GetCollection()", "Failed to get collection");
        }
        while (!_getCollectionTested)
        {
            yield return(null);
        }

        if (!_service.GetFields(OnGetFields, OnFail, _environmentId, _createdCollectionID))
        {
            Log.Debug("TestDiscovery.GetFields()", "Failed to get fields");
        }
        while (!_getFieldsTested)
        {
            yield return(null);
        }

        //  Add Document
        Log.Debug("TestDiscovery.RunTest()", "Attempting to add document");
        if (!_service.AddDocument(OnAddDocument, OnFail, _environmentId, _createdCollectionID, _documentFilePath, _environmentId, null))
        {
            Log.Debug("TestDiscovery.AddDocument()", "Failed to add document");
        }
        while (!_addDocumentTested)
        {
            yield return(null);
        }

        //  Get Document
        Log.Debug("TestDiscovery.RunTest()", "Attempting to get document");
        if (!_service.GetDocument(OnGetDocument, OnFail, _environmentId, _createdCollectionID, _createdDocumentID))
        {
            Log.Debug("TestDiscovery.GetDocument()", "Failed to get document");
        }
        while (!_getDocumentTested)
        {
            yield return(null);
        }

        //  Update Document
        Log.Debug("TestDiscovery.RunTest()", "Attempting to update document");
        if (!_service.UpdateDocument(OnUpdateDocument, OnFail, _environmentId, _createdCollectionID, _createdDocumentID, _documentFilePath, _environmentId, null))
        {
            Log.Debug("TestDiscovery.UpdateDocument()", "Failed to update document");
        }
        while (!_updateDocumentTested)
        {
            yield return(null);
        }

        //  Query
        Log.Debug("TestDiscovery.RunTest()", "Attempting to query");
        if (!_service.Query(OnQuery, OnFail, _environmentId, _createdCollectionID, null, _query, null, 10, null, 0))
        {
            Log.Debug("TestDiscovery.Query()", "Failed to query");
        }
        while (!_queryTested)
        {
            yield return(null);
        }

        //  Delete Document
        Log.Debug("TestDiscovery.RunTest()", "Attempting to delete document {0}", _createdDocumentID);
        if (!_service.DeleteDocument(OnDeleteDocument, OnFail, _environmentId, _createdCollectionID, _createdDocumentID))
        {
            Log.Debug("TestDiscovery.DeleteDocument()", "Failed to delete document");
        }
        while (!_deleteDocumentTested)
        {
            yield return(null);
        }

        //  Delay
        Log.Debug("TestDiscovery.RunTest()", "Delaying delete collection for 10 sec");
        Runnable.Run(Delay(_waitTime));
        while (!_readyToContinue)
        {
            yield return(null);
        }

        _isEnvironmentReady = false;
        Runnable.Run(CheckEnvironmentState(_waitTime));
        while (!_isEnvironmentReady)
        {
            yield return(null);
        }

        _readyToContinue = false;
        //  Delete Collection
        Log.Debug("TestDiscovery.RunTest()", "Attempting to delete collection {0}", _createdCollectionID);
        if (!_service.DeleteCollection(OnDeleteCollection, OnFail, _environmentId, _createdCollectionID))
        {
            Log.Debug("TestDiscovery.DeleteCollection()", "Failed to delete collection");
        }
        while (!_deleteCollectionTested)
        {
            yield return(null);
        }

        //  Delay
        Log.Debug("TestDiscovery.RunTest()", "Delaying delete configuration for 10 sec");
        Runnable.Run(Delay(_waitTime));
        while (!_readyToContinue)
        {
            yield return(null);
        }

        _isEnvironmentReady = false;
        Runnable.Run(CheckEnvironmentState(_waitTime));
        while (!_isEnvironmentReady)
        {
            yield return(null);
        }

        _readyToContinue = false;
        //  Delete Configuration
        Log.Debug("TestDiscovery.RunTest()", "Attempting to delete configuration {0}", _environmentId);
        if (!_service.DeleteConfiguration(OnDeleteConfiguration, OnFail, _environmentId, _environmentId))
        {
            Log.Debug("TestDiscovery.DeleteConfiguration()", "Failed to delete configuration");
        }
        while (!_deleteConfigurationTested)
        {
            yield return(null);
        }

        //  Delay
        Log.Debug("TestDiscovery.RunTest()", "Delaying delete environment for 10 sec");
        Runnable.Run(Delay(_waitTime));
        while (!_readyToContinue)
        {
            yield return(null);
        }

        _isEnvironmentReady = false;
        Runnable.Run(CheckEnvironmentState(_waitTime));
        while (!_isEnvironmentReady)
        {
            yield return(null);
        }

        //  List Credentials
        Log.Debug("TestDiscovery.RunTest()", "Attempting to list credentials");
        _service.ListCredentials(OnListCredentials, OnFail, _environmentId);
        while (!_listCredentialsTested)
        {
            yield return(null);
        }

        //  Create Credentials
        Log.Debug("TestDiscovery.RunTest()", "Attempting to create credentials");
        SourceCredentials credentialsParameter = new SourceCredentials()
        {
            SourceType        = SourceCredentials.SourceTypeEnum.box,
            CredentialDetails = new CredentialDetails()
            {
                CredentialType = CredentialDetails.CredentialTypeEnum.oauth2,
                EnterpriseId   = "myEnterpriseId",
                ClientId       = "myClientId",
                ClientSecret   = "myClientSecret",
                PublicKeyId    = "myPublicIdKey",
                Passphrase     = "myPassphrase",
                PrivateKey     = "myPrivateKey"
            }
        };

        _service.CreateCredentials(OnCreateCredentials, OnFail, _environmentId, credentialsParameter);
        while (!_createCredentialsTested)
        {
            yield return(null);
        }

        //  Get Credential
        Log.Debug("TestDiscovery.RunTest()", "Attempting to get credential");
        _service.GetCredential(OnGetCredential, OnFail, _environmentId, _createdCredentialId);
        while (!_getCredentialTested)
        {
            yield return(null);
        }

        //  DeleteCredential
        Log.Debug("TestDiscovery.RunTest()", "Attempting to delete credential");
        _service.DeleteCredentials(OnDeleteCredentials, OnFail, _environmentId, _createdCredentialId);
        while (!_deleteCredentialsTested)
        {
            yield return(null);
        }

        Log.Debug("TestDiscovery.RunTest()", "Discovery examples complete.");
    }
        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("discovery-sdk")[0].Credentials;
            //  Create credential and instantiate service
            TokenOptions tokenOptions = new TokenOptions()
            {
                IamApiKey = credential.IamApikey,
            };

            //  Create credential and instantiate service
            Credentials credentials = new Credentials(tokenOptions, credential.Url);

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

            _discovery             = new Discovery(credentials);
            _discovery.VersionDate = _discoveryVersionDate;
            _filePathToIngest      = Application.dataPath + "/Watson/Examples/ServiceExamples/TestData/Discovery/constitution.pdf";
            _documentFilePath      = Application.dataPath + "/Watson/Examples/ServiceExamples/TestData/Discovery/constitution.pdf";
            //_stopwordsFilepath = Application.dataPath + "/Watson/Examples/ServiceExamples/TestData/Discovery/stopwords.txt";
            //  Get Environments
            Log.Debug("TestDiscovery.RunTest()", "Attempting to get environments");
            if (!_discovery.GetEnvironments(OnGetEnvironments, OnFail))
            {
                Log.Debug("TestDiscovery.GetEnvironments()", "Failed to get environments");
            }
            while (!_getEnvironmentsTested)
            {
                yield return(null);
            }

            //  Wait for environment to be ready
            Runnable.Run(CheckEnvironmentState(0f));
            while (!_isEnvironmentReady)
            {
                yield return(null);
            }

            //  GetEnvironment
            Log.Debug("TestDiscovery.RunTest()", "Attempting to get environment");
            if (!_discovery.GetEnvironment(OnGetEnvironment, OnFail, _environmentId))
            {
                Log.Debug("TestDiscovery.GetEnvironment()", "Failed to get environment");
            }
            while (!_getEnvironmentTested)
            {
                yield return(null);
            }

            //  Get Configurations
            Log.Debug("TestDiscovery.RunTest()", "Attempting to get configurations");
            if (!_discovery.GetConfigurations(OnGetConfigurations, OnFail, _environmentId))
            {
                Log.Debug("TestDiscovery.GetConfigurations()", "Failed to get configurations");
            }
            while (!_getConfigurationsTested)
            {
                yield return(null);
            }

            //  Add Configuration
            Log.Debug("TestDiscovery.RunTest()", "Attempting to add configuration");
            if (!_discovery.AddConfiguration(OnAddConfiguration, OnFail, _environmentId, _configurationJson.Replace("{guid}", System.Guid.NewGuid().ToString())))
            {
                Log.Debug("TestDiscovery.AddConfiguration()", "Failed to add configuration");
            }
            while (!_addConfigurationTested)
            {
                yield return(null);
            }

            //  Get Configuration
            Log.Debug("TestDiscovery.RunTest()", "Attempting to get configuration");
            if (!_discovery.GetConfiguration(OnGetConfiguration, OnFail, _environmentId, _createdConfigurationID))
            {
                Log.Debug("TestDiscovery.GetConfiguration()", "Failed to get configuration");
            }
            while (!_getConfigurationTested)
            {
                yield return(null);
            }

            //  Preview Configuration
            Log.Debug("TestDiscovery.RunTest()", "Attempting to preview configuration");
            if (!_discovery.PreviewConfiguration(OnPreviewConfiguration, OnFail, _environmentId, _createdConfigurationID, null, _filePathToIngest, _metadata))
            {
                Log.Debug("TestDiscovery.PreviewConfiguration()", "Failed to preview configuration");
            }
            while (!_previewConfigurationTested)
            {
                yield return(null);
            }

            //  Get Collections
            Log.Debug("TestDiscovery.RunTest()", "Attempting to get collections");
            if (!_discovery.GetCollections(OnGetCollections, OnFail, _environmentId))
            {
                Log.Debug("TestDiscovery.GetCollections()", "Failed to get collections");
            }
            while (!_getCollectionsTested)
            {
                yield return(null);
            }

            //  Add Collection
            Log.Debug("TestDiscovery.RunTest()", "Attempting to add collection");
            if (!_discovery.AddCollection(OnAddCollection, OnFail, _environmentId, _createdCollectionName + System.Guid.NewGuid().ToString(), _createdCollectionDescription, _createdConfigurationID, "en"))
            {
                Log.Debug("TestDiscovery.AddCollection()", "Failed to add collection");
            }
            while (!_addCollectionTested)
            {
                yield return(null);
            }

            //  Get Collection
            Log.Debug("TestDiscovery.RunTest()", "Attempting to get collection");
            if (!_discovery.GetCollection(OnGetCollection, OnFail, _environmentId, _createdCollectionId))
            {
                Log.Debug("TestDiscovery.GetCollection()", "Failed to get collection");
            }
            while (!_getCollectionTested)
            {
                yield return(null);
            }

            if (!_discovery.GetFields(OnGetFields, OnFail, _environmentId, _createdCollectionId))
            {
                Log.Debug("TestDiscovery.GetFields()", "Failed to get fields");
            }
            while (!_getFieldsTested)
            {
                yield return(null);
            }

            //  Add Document
            Log.Debug("TestDiscovery.RunTest()", "Attempting to add document");
            if (!_discovery.AddDocument(OnAddDocument, OnFail, _environmentId, _createdCollectionId, _documentFilePath, _createdConfigurationID, null))
            {
                Log.Debug("TestDiscovery.AddDocument()", "Failed to add document");
            }
            while (!_addDocumentTested)
            {
                yield return(null);
            }

            //  Get Document
            Log.Debug("TestDiscovery.RunTest()", "Attempting to get document");
            if (!_discovery.GetDocument(OnGetDocument, OnFail, _environmentId, _createdCollectionId, _createdDocumentID))
            {
                Log.Debug("TestDiscovery.GetDocument()", "Failed to get document");
            }
            while (!_getDocumentTested)
            {
                yield return(null);
            }

            //  Update Document
            Log.Debug("TestDiscovery.RunTest()", "Attempting to update document");
            if (!_discovery.UpdateDocument(OnUpdateDocument, OnFail, _environmentId, _createdCollectionId, _createdDocumentID, _documentFilePath, _createdConfigurationID, null))
            {
                Log.Debug("TestDiscovery.UpdateDocument()", "Failed to update document");
            }
            while (!_updateDocumentTested)
            {
                yield return(null);
            }

            //  Query
            Log.Debug("TestDiscovery.RunTest()", "Attempting to query");
            if (!_discovery.Query(OnQuery, OnFail, _environmentId, _createdCollectionId, naturalLanguageQuery: _query))
            {
                while (!_queryTested)
                {
                    yield return(null);
                }
            }

            //  List Credentials
            Log.Debug("TestDiscovery.RunTest()", "Attempting to list credentials");
            _discovery.ListCredentials(OnListCredentials, OnFail, _environmentId);
            while (!_listCredentialsTested)
            {
                yield return(null);
            }

            //  Create Credentials
            Log.Debug("TestDiscovery.RunTest()", "Attempting to create credentials");
            SourceCredentials credentialsParameter = new SourceCredentials()
            {
                SourceType        = SourceCredentials.SourceTypeEnum.box,
                CredentialDetails = new CredentialDetails()
                {
                    CredentialType = CredentialDetails.CredentialTypeEnum.oauth2,
                    EnterpriseId   = "myEnterpriseId",
                    ClientId       = "myClientId",
                    ClientSecret   = "myClientSecret",
                    PublicKeyId    = "myPublicIdKey",
                    Passphrase     = "myPassphrase",
                    PrivateKey     = "myPrivateKey"
                }
            };

            _discovery.CreateCredentials(OnCreateCredentials, OnFail, _environmentId, credentialsParameter);
            while (!_createCredentialsTested)
            {
                yield return(null);
            }

            //  Get Credential
            Log.Debug("TestDiscovery.RunTest()", "Attempting to get credential");
            _discovery.GetCredential(OnGetCredential, OnFail, _environmentId, _createdCredentialId);
            while (!_getCredentialTested)
            {
                yield return(null);
            }

            //  Get metrics event rate
            Log.Debug("TestDiscovery.RunTest()", "Attempting to Get metrics event rate");
            _discovery.GetMetricsEventRate(OnGetMetricsEventRate, OnFail);
            while (!_getMetricsEventRateTested)
            {
                yield return(null);
            }

            //  Get metrics query
            Log.Debug("TestDiscovery.RunTest()", "Attempting to Get metrics query");
            _discovery.GetMetricsQuery(OnGetMetricsQuery, OnFail);
            while (!_getMetricsQueryTested)
            {
                yield return(null);
            }

            //  Get metrics query event
            Log.Debug("TestDiscovery.RunTest()", "Attempting to Get metrics query event");
            _discovery.GetMetricsQueryEvent(OnGetMetricsQueryEvent, OnFail);
            while (!_getMetricsQueryEventTested)
            {
                yield return(null);
            }

            //  Get metrics query no result
            Log.Debug("TestDiscovery.RunTest()", "Attempting to Get metrics query no result");
            _discovery.GetMetricsQueryNoResults(OnGetMetricsQueryNoResult, OnFail);
            while (!_getMetricsQueryNoResultTested)
            {
                yield return(null);
            }

            //  Get metrics query token event
            Log.Debug("TestDiscovery.RunTest()", "Attempting to Get metrics query token event");
            _discovery.GetMetricsQueryTokenEvent(OnGetMetricsQueryTokenEvent, OnFail);
            while (!_getMetricsQueryTokenEventTested)
            {
                yield return(null);
            }

            //  Query log
            Log.Debug("TestDiscovery.RunTest()", "Attempting to Query log");
            _discovery.QueryLog(OnQueryLog, OnFail);
            while (!_queryLogTested)
            {
                yield return(null);
            }

            //  Create event
            Log.Debug("TestDiscovery.RunTest()", "Attempting to create event");
            CreateEventObject queryEvent = new CreateEventObject()
            {
                Type = CreateEventObject.TypeEnum.click,
                Data = new EventData()
                {
                    EnvironmentId = _environmentId,
                    SessionToken  = _sessionToken,
                    CollectionId  = _createdCollectionId,
                    DocumentId    = _createdDocumentID
                }
            };

            _discovery.CreateEvent(OnCreateEvent, OnFail, queryEvent);
            while (!_createEventTested)
            {
                yield return(null);
            }

            //  List Expansions
            Log.Debug("TestDiscovery.RunTests()", "Attempting to list expansions");
            _discovery.ListExpansions(OnListExpansions, OnFail, _environmentId, _createdCollectionId);
            while (!_listExpansionsTested)
            {
                yield return(null);
            }

            // Create Expansions
            Log.Debug("TestDiscovery.RunTests()", "Attempting to create expansion");
            Expansions expansions = new Expansions()
            {
                ExpansionsProperty = new List <Expansion>()
                {
                    new Expansion()
                    {
                        ExpandedTerms = new List <string>()
                        {
                            "expanded-term"
                        },
                        InputTerms = new List <string>()
                        {
                            "input-term"
                        }
                    }
                }
            };

            _discovery.CreateExpansions(OnCreateExpansion, OnFail, _environmentId, _createdCollectionId, expansions);
            while (!_createExpansionTested)
            {
                yield return(null);
            }

            // Delete Expansions
            Log.Debug("TestDiscovery.RunTests()", "Attempting to delete expansion");
            _discovery.DeleteExpansions(OnDeleteExpansion, OnFail, _environmentId, _createdCollectionId);
            while (!_deleteExpansionTested)
            {
                yield return(null);
            }

            //  Create japanese collection
            Log.Debug("TestDiscovery.RunTest()", "Attempting to create jp collection");
            _discovery.AddCollection(OnCreateJpCollection, OnFail, _environmentId, _createdCollectionName + System.Guid.NewGuid().ToString(), _createdCollectionDescription, _createdConfigurationID, "ja");
            while (!_createJpCollectionTested)
            {
                yield return(null);
            }

            //  Create tokenization dictionary
            TokenDict tokenizationDictionary = new TokenDict()
            {
                TokenizationRules = new List <TokenDictRule>()
                {
                    new TokenDictRule()
                    {
                        Text   = "すしネコ",
                        Tokens = new List <string>()
                        {
                            "すし", "ネコ"
                        },
                        Readings = new List <string>()
                        {
                            "寿司", "ネコ"
                        },
                        PartOfSpeech = "カスタム名詞"
                    }
                }
            };

            Log.Debug("TestDiscovery.RunTests()", "Attempting to create tokenization dict");
            _discovery.CreateTokenizationDictionary(OnCreateTokenizationDictionary, OnCreateTokenizationDictionaryFail, _environmentId, _createdJpCollection, tokenizationDictionary);
            while (!_createTokenizationDictTested)
            {
                yield return(null);
            }

            //  Get tokenization dictionary status
            if (!_getTokenizationDictStatusTested)
            {
                Log.Debug("TestDiscovery.RunTests()", "Attempting to get tokenization dict status");
                _discovery.GetTokenizationDictionaryStatus(OnGetTokenizationDictonaryStatus, OnFail, _environmentId, _createdJpCollection);
                while (!_getTokenizationDictStatusTested)
                {
                    yield return(null);
                }
            }

            //  Delete tokenization dictionary
            if (!_deleteTokenizationDictTested)
            {
                Log.Debug("TestDiscovery.RunTests()", "Attempting to delete tokenization dict");
                _discovery.DeleteTokenizationDictionary(OnDeleteTokenizationDictionary, OnFail, _environmentId, _createdJpCollection);
                while (!_deleteTokenizationDictTested)
                {
                    yield return(null);
                }
            }

            //  Delete jp collection
            Log.Debug("TestDiscovery.RunTest()", "Attempting to delete jp collection");
            _discovery.DeleteCollection(OnDeleteJpCollection, OnFail, _environmentId, _createdJpCollection);
            while (!_deleteJpCollectionTested)
            {
                yield return(null);
            }

            //  DeleteCredential
            Log.Debug("TestDiscovery.RunTest()", "Attempting to delete credential");
            _discovery.DeleteCredentials(OnDeleteCredentials, OnFail, _environmentId, _createdCredentialId);
            while (!_deleteCredentialsTested)
            {
                yield return(null);
            }

            //  Delete Document
            Log.Debug("TestDiscovery.RunTest()", "Attempting to delete document {0}", _createdDocumentID);
            if (!_discovery.DeleteDocument(OnDeleteDocument, OnFail, _environmentId, _createdCollectionId, _createdDocumentID))
            {
                Log.Debug("TestDiscovery.DeleteDocument()", "Failed to delete document");
            }
            while (!_deleteDocumentTested)
            {
                yield return(null);
            }

            _isEnvironmentReady = false;
            Runnable.Run(CheckEnvironmentState(_waitTime));
            while (!_isEnvironmentReady)
            {
                yield return(null);
            }

            ////  Create stopword list
            //using (FileStream fs = File.OpenRead(_stopwordsFilepath))
            //{

            //    Log.Debug("TestDiscovery.RunTest()", "Attempting to create stopword list {0}", _createdCollectionId);
            //    _discovery.CreateStopwordList(OnCreateStopwordList, OnFail, _environmentId, _createdCollectionId, fs);
            //    while (!_createStopwordListTested)
            //        yield return null;
            //}

            ////  Delete stopword list
            //Log.Debug("TestDiscovery.RunTest()", "Attempting to delete stopword list {0}", _createdCollectionId);
            //_discovery.DeleteStopwordList(OnDeleteStopwordList, OnFail, _environmentId, _createdCollectionId);
            //while (!_deleteStopwordListTested)
            //    yield return null;

            //  List Gatways
            Log.Debug("TestDiscovery.RunTest()", "Attempting to list gateways.");
            _discovery.ListGateways(OnListGateways, OnFail, _environmentId);
            while (!_listGatewaysTested)
            {
                yield return(null);
            }

            //  Create Gateway
            Log.Debug("TestDiscovery.RunTest()", "Attempting to create gateway.");
            _discovery.CreateGateway(OnCreateGateway, OnFail, _environmentId);
            while (!_createGatewayTested)
            {
                yield return(null);
            }

            //  Get Gateway
            Log.Debug("TestDiscovery.RunTest()", "Attempting to get gateway.");
            _discovery.GetGateway(OnGetGateway, OnFail, _environmentId, _createdGatewayId);
            while (!_getGatewayTested)
            {
                yield return(null);
            }

            //  Delete Gateway
            Log.Debug("TestDiscovery.RunTest()", "Attempting to delete gateway.");
            _discovery.GetGateway(OnDelteGateway, OnFail, _environmentId, _createdGatewayId);
            while (!_deleteGatewayTested)
            {
                yield return(null);
            }

            //  Delete Collection
            Log.Debug("TestDiscovery.RunTest()", "Attempting to delete collection {0}", _createdCollectionId);
            if (!_discovery.DeleteCollection(OnDeleteCollection, OnFail, _environmentId, _createdCollectionId))
            {
                Log.Debug("TestDiscovery.DeleteCollection()", "Failed to delete collection");
            }
            while (!_deleteCollectionTested)
            {
                yield return(null);
            }

            _isEnvironmentReady = false;
            Runnable.Run(CheckEnvironmentState(_waitTime));
            while (!_isEnvironmentReady)
            {
                yield return(null);
            }

            //  Delete Configuration
            Log.Debug("TestDiscovery.RunTest()", "Attempting to delete configuration {0}", _createdConfigurationID);
            if (!_discovery.DeleteConfiguration(OnDeleteConfiguration, OnFail, _environmentId, _createdConfigurationID))
            {
                Log.Debug("TestDiscovery.DeleteConfiguration()", "Failed to delete configuration");
            }
            while (!_deleteConfigurationTested)
            {
                yield return(null);
            }

            //  Delete User Data
            Log.Debug("TestDiscovery.RunTest()", "Attempting to delete user data.");
            _discovery.DeleteUserData(OnDeleteUserData, OnFail, "test-unity-user-id");
            while (!_deleteUserDataTested)
            {
                yield return(null);
            }

            Log.Debug("TestDiscovery.RunTest()", "Discovery unit tests complete.");

            yield break;
        }