|
| 1 | +/** |
| 2 | +* Copyright 2015 IBM Corp. All Rights Reserved. |
| 3 | +* |
| 4 | +* Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +* you may not use this file except in compliance with the License. |
| 6 | +* You may obtain a copy of the License at |
| 7 | +* |
| 8 | +* http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +* |
| 10 | +* Unless required by applicable law or agreed to in writing, software |
| 11 | +* distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +* See the License for the specific language governing permissions and |
| 14 | +* limitations under the License. |
| 15 | +* |
| 16 | +*/ |
| 17 | + |
| 18 | +using System.Collections.Generic; |
| 19 | +using IBM.Watson.DeveloperCloud.Services.Discovery.v1; |
| 20 | +using IBM.Watson.DeveloperCloud.Logging; |
| 21 | +using UnityEngine; |
| 22 | +using System.Collections; |
| 23 | + |
| 24 | +namespace IBM.Watson.DeveloperCloud.UnitTests |
| 25 | +{ |
| 26 | + public class TestDiscovery : UnitTest |
| 27 | + { |
| 28 | + Discovery m_Discovery = new Discovery(); |
| 29 | + |
| 30 | + // Default news values |
| 31 | + private string m_DefaultEnvironmentID = "6c8647b7-9dd4-42c8-9cb0-117b40b14517"; |
| 32 | + private string m_DefaultConfigurationID = "662a2032-9e2c-472b-9eaa-1a2fa098c22e"; |
| 33 | + private string m_DefaultCollectionID = "336f2f0e-e771-424e-a7b4-331240c8f136"; |
| 34 | + |
| 35 | + // Environment |
| 36 | + private string m_CreatedEnvironmentName = "unity-sdk-integration-test"; |
| 37 | + private string m_CreatedEnvironmentDescription = "Integration test running for Unity SDK. Please do not delete this environment until 10 minutes after the status is 'active'. The test should delete this environment."; |
| 38 | + private string m_CreatedEnvironmentID; |
| 39 | + |
| 40 | + // Configuration |
| 41 | + private string m_ConfigurationJsonPath; |
| 42 | + private string m_CreatedConfigurationID; |
| 43 | + |
| 44 | + // Collection |
| 45 | + private string m_CreatedCollectionName = "Unity SDK Created Collection"; |
| 46 | + private string m_CreatedCollectionDescription = "A collection created by the Unity SDK."; |
| 47 | + private string m_CreatedCollectionID; |
| 48 | + |
| 49 | + // Document |
| 50 | + private string m_FilePathToIngest; // File path for PreviewConfiguration |
| 51 | + private string m_DocumentFilePath; |
| 52 | + private string m_Metadata = "{\n\t\"Creator\": \"Unity SDK Integration Test\",\n\t\"Subject\": \"Discovery service\"\n}"; |
| 53 | + private string m_CreatedDocumentID; |
| 54 | + |
| 55 | + // Query |
| 56 | + private string m_Query = "What is the capital of china?"; |
| 57 | + |
| 58 | + // Add and get Environments |
| 59 | + private bool m_GetEnvironmentsTested = false; |
| 60 | + private bool m_AddEnvironmentsTested = false; |
| 61 | + private bool m_GetEnvironmentTested = false; |
| 62 | + |
| 63 | + // Add and get Configurations |
| 64 | + private bool m_GetConfigurationsTested = false; |
| 65 | + private bool m_AddConfigurationTested = false; |
| 66 | + private bool m_GetConfigurationTested = false; |
| 67 | + |
| 68 | + // Preview Configuration |
| 69 | + private bool m_PreviewConfigurationTested = false; |
| 70 | + |
| 71 | + // Add and get Collections |
| 72 | + private bool m_GetCollectionsTested = false; |
| 73 | + private bool m_AddCollectionTested = false; |
| 74 | + private bool m_GetCollectionTested = false; |
| 75 | + |
| 76 | + // Get Fields |
| 77 | + private bool m_GetFieldsTested = false; |
| 78 | + |
| 79 | + // Add and get Documents |
| 80 | + private bool m_AddDocumentTested = false; |
| 81 | + private bool m_GetDocumentTested = false; |
| 82 | + private bool m_UpdateDocumentTested = false; |
| 83 | + |
| 84 | + // Query |
| 85 | + private bool m_QueryTested = false; |
| 86 | + |
| 87 | + // Delete |
| 88 | + private bool m_DeleteDocumentTested = false; |
| 89 | + private bool m_DeleteCollectionTested = false; |
| 90 | + private bool m_DeleteConfigurationTested = false; |
| 91 | + private bool m_DeleteEnvironmentTested = false; |
| 92 | + |
| 93 | + public override IEnumerator RunTest() |
| 94 | + { |
| 95 | + LogSystem.InstallDefaultReactors(); |
| 96 | + |
| 97 | + m_ConfigurationJsonPath = Application.dataPath + "/Watson/Examples/ServiceExamples/TestData/Discovery/exampleConfigurationData.json"; |
| 98 | + m_FilePathToIngest = Application.dataPath + "/Watson/Examples/ServiceExamples/TestData/watson_beats_jeopardy.html"; |
| 99 | + m_DocumentFilePath = Application.dataPath + "/Watson/Examples/ServiceExamples/TestData/watson_beats_jeopardy.html"; |
| 100 | + |
| 101 | + #region Environments |
| 102 | + // Get Environments |
| 103 | + Log.Debug("ExampleDiscoveryV1", "Attempting to get environments"); |
| 104 | + if (!m_Discovery.GetEnvironments((GetEnvironmentsResponse resp, string data) => |
| 105 | + { |
| 106 | + if (resp != null) |
| 107 | + { |
| 108 | + foreach (Environment environment in resp.environments) |
| 109 | + Log.Debug("ExampleDiscoveryV1", "environment_id: {0}", environment.environment_id); |
| 110 | + } |
| 111 | + else |
| 112 | + { |
| 113 | + Log.Debug("ExampleDiscoveryV1", "Discovery.GetEnvironments(); resp is null"); |
| 114 | + } |
| 115 | + |
| 116 | + Test(resp.environments != null); |
| 117 | + m_GetEnvironmentsTested = true; |
| 118 | + })) |
| 119 | + Log.Debug("ExampleDiscoveryV1", "Failed to get environments"); |
| 120 | + |
| 121 | + while (!m_GetEnvironmentsTested) |
| 122 | + yield return null; |
| 123 | + |
| 124 | + // Add Environment |
| 125 | + Log.Debug("ExampleDiscoveryV1", "Attempting to add environment"); |
| 126 | + if (!m_Discovery.AddEnvironment((Environment resp, string data) => |
| 127 | + { |
| 128 | + if (resp != null) |
| 129 | + { |
| 130 | + Log.Debug("ExampleDiscoveryV1", "Added {0}", resp.environment_id); |
| 131 | + m_CreatedEnvironmentID = resp.environment_id; |
| 132 | + } |
| 133 | + else |
| 134 | + { |
| 135 | + Log.Debug("ExampleDiscoveryV1", "Discovery.AddEnvironment(); resp is null, {0}", data); |
| 136 | + } |
| 137 | + |
| 138 | + Test(!string.IsNullOrEmpty(resp.environment_id)); |
| 139 | + m_AddEnvironmentsTested = true; |
| 140 | + }, m_CreatedEnvironmentName, m_CreatedEnvironmentDescription, 0)) |
| 141 | + Log.Debug("ExampleDiscoveryV1", "Failed to add environment"); |
| 142 | + |
| 143 | + while (!m_AddEnvironmentsTested) |
| 144 | + yield return null; |
| 145 | + |
| 146 | + // Get Environment |
| 147 | + Log.Debug("ExampleDiscoveryV1", "Attempting to get environment"); |
| 148 | + if (!m_Discovery.GetEnvironment((Environment resp, string data) => |
| 149 | + { |
| 150 | + if (resp != null) |
| 151 | + { |
| 152 | + Log.Debug("ExampleDiscoveryV1", "environment_name: {0}", resp.name); |
| 153 | + } |
| 154 | + else |
| 155 | + { |
| 156 | + Log.Debug("ExampleDiscoveryV1", "Discovery.GetEnvironment(); resp is null"); |
| 157 | + } |
| 158 | + |
| 159 | + Test(!string.IsNullOrEmpty(resp.name)); |
| 160 | + m_GetEnvironmentTested = true; |
| 161 | + }, m_CreatedEnvironmentID)) |
| 162 | + Log.Debug("ExampleDiscoveryV1", "Failed to get environment"); |
| 163 | + |
| 164 | + while (!m_GetEnvironmentTested) |
| 165 | + yield return null; |
| 166 | + #endregion |
| 167 | + |
| 168 | + #region Configurations |
| 169 | + #endregion |
| 170 | + |
| 171 | + #region Collections |
| 172 | + #endregion |
| 173 | + |
| 174 | + #region Fields |
| 175 | + #endregion |
| 176 | + |
| 177 | + #region Documents |
| 178 | + #endregion |
| 179 | + |
| 180 | + #region Query |
| 181 | + #endregion |
| 182 | + |
| 183 | + #region Delete |
| 184 | + // Delete Environment |
| 185 | + Log.Debug("ExampleDiscoveryV1", "Attempting to delete environment {0}", m_CreatedEnvironmentID); |
| 186 | + if (!m_Discovery.DeleteEnvironment((bool success, string data) => |
| 187 | + { |
| 188 | + if (success) |
| 189 | + { |
| 190 | + Log.Debug("ExampleDiscoveryV1", "Delete environment successful"); |
| 191 | + m_CreatedEnvironmentID = default(string); |
| 192 | + } |
| 193 | + else |
| 194 | + { |
| 195 | + Log.Debug("ExampleDiscoveryV1", "Delete environment failed"); |
| 196 | + } |
| 197 | + |
| 198 | + Test(success); |
| 199 | + m_DeleteEnvironmentTested = true; |
| 200 | + }, m_CreatedEnvironmentID)) |
| 201 | + Log.Debug("ExampleDiscoveryV1", "Failed to delete environment"); |
| 202 | + |
| 203 | + while (!m_DeleteEnvironmentTested) |
| 204 | + yield return null; |
| 205 | + #endregion |
| 206 | + |
| 207 | + yield break; |
| 208 | + } |
| 209 | + } |
| 210 | +} |
0 commit comments