1515*
1616*/
1717
18- using System . Collections . Generic ;
1918using IBM . Watson . DeveloperCloud . Services . Discovery . v1 ;
2019using IBM . Watson . DeveloperCloud . Logging ;
2120using UnityEngine ;
@@ -25,7 +24,22 @@ namespace IBM.Watson.DeveloperCloud.UnitTests
2524{
2625 public class TestDiscovery : UnitTest
2726 {
28- Discovery m_Discovery = new Discovery ( ) ;
27+ private Discovery m_Discovery = new Discovery ( ) ;
28+ private System . Timers . Timer m_StateTimer = new System . Timers . Timer ( 1000 ) ;
29+ private bool m_IsCheckingState = false ;
30+ private bool IsCheckingState
31+ {
32+ get
33+ {
34+ return m_IsCheckingState ;
35+ }
36+ set
37+ {
38+ m_IsCheckingState = value ;
39+ if ( m_IsCheckingState )
40+ CheckState ( ) ;
41+ }
42+ }
2943
3044 // Default news values
3145 private string m_DefaultEnvironmentID = "6c8647b7-9dd4-42c8-9cb0-117b40b14517" ;
@@ -36,6 +50,7 @@ public class TestDiscovery : UnitTest
3650 private string m_CreatedEnvironmentName = "unity-sdk-integration-test" ;
3751 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." ;
3852 private string m_CreatedEnvironmentID ;
53+ private bool m_IsEnvironmentActive = false ;
3954
4055 // Configuration
4156 private string m_ConfigurationJsonPath ;
@@ -166,6 +181,108 @@ public override IEnumerator RunTest()
166181 #endregion
167182
168183 #region Configurations
184+ // Get Configurations
185+ Log . Debug ( "ExampleDiscoveryV1" , "Attempting to get configurations" ) ;
186+ if ( ! m_Discovery . GetConfigurations ( ( GetConfigurationsResponse resp , string data ) =>
187+ {
188+ if ( resp != null )
189+ {
190+ if ( resp . configurations != null && resp . configurations . Length > 0 )
191+ {
192+ foreach ( ConfigurationRef configuration in resp . configurations )
193+ {
194+ Log . Debug ( "ExampleDiscoveryV1" , "Configuration: {0}, {1}" , configuration . configuration_id , configuration . name ) ;
195+ }
196+ }
197+ else
198+ {
199+ Log . Debug ( "ExampleDiscoveryV1" , "There are no configurations for this environment." ) ;
200+ }
201+ }
202+ else
203+ {
204+ Log . Debug ( "ExampleDiscoveryV1" , "Discovery.GetConfigurations(); resp is null, {0}" , data ) ;
205+ }
206+
207+ Test ( resp . configurations != null ) ;
208+ m_GetConfigurationsTested = true ;
209+ } , m_DefaultEnvironmentID ) )
210+ Log . Debug ( "ExampleDiscoveryV1" , "Failed to get configurations" ) ;
211+
212+ while ( ! m_GetConfigurationsTested )
213+ yield return null ;
214+
215+ // Add Configuration
216+ Log . Debug ( "ExampleDiscoveryV1" , "Attempting to add configuration" ) ;
217+ if ( ! m_Discovery . AddConfiguration ( ( Configuration resp , string data ) =>
218+ {
219+ if ( resp != null )
220+ {
221+ Log . Debug ( "ExampleDiscoveryV1" , "Configuration: {0}, {1}" , resp . configuration_id , resp . name ) ;
222+ m_CreatedConfigurationID = resp . configuration_id ;
223+
224+
225+ }
226+ else
227+ {
228+ Log . Debug ( "ExampleDiscoveryV1" , "Discovery.AddConfiguration(); resp is null, {0}" , data ) ;
229+ }
230+
231+ Test ( ! string . IsNullOrEmpty ( resp . configuration_id ) ) ;
232+ m_AddConfigurationTested = true ;
233+ } , m_CreatedEnvironmentID , m_ConfigurationJsonPath ) )
234+ Log . Debug ( "ExampleDiscoveryV1" , "Failed to add configuration" ) ;
235+
236+ while ( ! m_AddConfigurationTested )
237+ yield return null ;
238+
239+ // Get Configuration
240+ Log . Debug ( "ExampleDiscoveryV1" , "Attempting to get configuration" ) ;
241+ if ( ! m_Discovery . GetConfiguration ( ( Configuration resp , string data ) =>
242+ {
243+ if ( resp != null )
244+ {
245+ Log . Debug ( "ExampleDiscoveryV1" , "Configuration: {0}, {1}" , resp . configuration_id , resp . name ) ;
246+ }
247+ else
248+ {
249+ Log . Debug ( "ExampleDiscoveryV1" , "Discovery.GetConfiguration(); resp is null, {0}" , data ) ;
250+ }
251+
252+ Test ( ! string . IsNullOrEmpty ( resp . name ) ) ;
253+ m_GetConfigurationTested = true ;
254+ } , m_DefaultEnvironmentID , m_DefaultConfigurationID ) )
255+ Log . Debug ( "ExampleDiscoveryV1" , "Failed to get configuration" ) ;
256+
257+ while ( ! m_GetConfigurationTested )
258+ yield return null ;
259+
260+ // Preview Configuration
261+ Log . Debug ( "ExampleDiscoveryV1" , "Attempting to preview configuration" ) ;
262+ if ( ! m_Discovery . PreviewConfiguration ( ( TestDocument resp , string data ) =>
263+ {
264+ if ( resp != null )
265+ {
266+ Log . Debug ( "ExampleDiscoveryV1" , "Preview succeeded: {0}" , resp . status ) ;
267+ }
268+ else
269+ {
270+ Log . Debug ( "ExampleDiscoveryV1" , "Discovery.PreviewConfiguration(); resp is null {0}" , data ) ;
271+ }
272+
273+ Test ( resp != null ) ;
274+ m_PreviewConfigurationTested = true ;
275+ } , m_CreatedEnvironmentID , m_CreatedConfigurationID , null , m_FilePathToIngest , m_Metadata ) )
276+ Log . Debug ( "ExampleDiscoveryV1" , "Failed to preview configuration" ) ;
277+
278+ while ( ! m_PreviewConfigurationTested )
279+ yield return null ;
280+
281+ IsCheckingState = true ;
282+
283+ while ( ! m_IsEnvironmentActive )
284+ yield return null ;
285+
169286 #endregion
170287
171288 #region Collections
@@ -181,6 +298,26 @@ public override IEnumerator RunTest()
181298 #endregion
182299
183300 #region Delete
301+ // DeleteEnvironment
302+ Log . Debug ( "ExampleDiscoveryV1" , "Attempting to delete configuration {0}" , m_CreatedConfigurationID ) ;
303+ if ( ! m_Discovery . DeleteConfiguration ( ( bool success , string data ) =>
304+ {
305+ if ( success )
306+ {
307+ Log . Debug ( "ExampleDiscoveryV1" , "Delete configuration successful" ) ;
308+ m_CreatedConfigurationID = default ( string ) ;
309+ }
310+ else
311+ Log . Debug ( "ExampleDiscoveryV1" , "Delete configuration failed" ) ;
312+
313+ Test ( success ) ;
314+ m_DeleteConfigurationTested = true ;
315+ } , m_CreatedEnvironmentID , m_CreatedConfigurationID ) )
316+ Log . Debug ( "ExampleDiscoveryV1" , "Failed to delete configuration" ) ;
317+
318+ while ( ! m_DeleteConfigurationTested )
319+ yield return null ;
320+
184321 // Delete Environment
185322 Log . Debug ( "ExampleDiscoveryV1" , "Attempting to delete environment {0}" , m_CreatedEnvironmentID ) ;
186323 if ( ! m_Discovery . DeleteEnvironment ( ( bool success , string data ) =>
@@ -206,5 +343,33 @@ public override IEnumerator RunTest()
206343
207344 yield break ;
208345 }
346+
347+ private void CheckState ( )
348+ {
349+ Log . Debug ( "ExampleDiscoveryV1" , "Attempting to get environment state" ) ;
350+
351+ m_Discovery . GetEnvironment ( ( Environment resp , string data ) =>
352+ {
353+ Log . Debug ( "ExampleDiscoveryV1" , "Environment {0} is {1}" , resp . environment_id , resp . status ) ;
354+
355+ IsCheckingState = false ;
356+ if ( resp . status == "active" )
357+ {
358+ m_IsEnvironmentActive = true ;
359+ }
360+ else
361+ {
362+ m_StateTimer . Elapsed += OnTimer ;
363+ m_StateTimer . Enabled = true ;
364+ }
365+ } , m_CreatedEnvironmentID ) ;
366+ }
367+
368+ private void OnTimer ( object source , System . Timers . ElapsedEventArgs e )
369+ {
370+ m_StateTimer . Enabled = false ;
371+ m_StateTimer . Elapsed -= OnTimer ;
372+ IsCheckingState = true ;
373+ }
209374 }
210375}
0 commit comments