1919using IBM . Watson . DeveloperCloud . Logging ;
2020using UnityEngine ;
2121using System . Collections ;
22+ using IBM . Watson . DeveloperCloud . Utilities ;
2223
2324namespace IBM . Watson . DeveloperCloud . UnitTests
2425{
2526 public class TestDiscovery : UnitTest
2627 {
2728 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- }
4329
4430 // Default news values
45- private string m_DefaultEnvironmentID = "6c8647b7-9dd4-42c8-9cb0-117b40b14517" ;
46- private string m_DefaultConfigurationID = "662a2032-9e2c-472b-9eaa-1a2fa098c22e" ;
47- private string m_DefaultCollectionID = "336f2f0e-e771-424e-a7b4-331240c8f136" ;
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";
4834
4935 // Environment
5036 private string m_CreatedEnvironmentName = "unity-sdk-integration-test" ;
@@ -206,7 +192,7 @@ public override IEnumerator RunTest()
206192
207193 Test ( resp . configurations != null ) ;
208194 m_GetConfigurationsTested = true ;
209- } , m_DefaultEnvironmentID ) )
195+ } , m_CreatedEnvironmentID ) )
210196 Log . Debug ( "ExampleDiscoveryV1" , "Failed to get configurations" ) ;
211197
212198 while ( ! m_GetConfigurationsTested )
@@ -251,7 +237,7 @@ public override IEnumerator RunTest()
251237
252238 Test ( ! string . IsNullOrEmpty ( resp . name ) ) ;
253239 m_GetConfigurationTested = true ;
254- } , m_DefaultEnvironmentID , m_DefaultConfigurationID ) )
240+ } , m_CreatedEnvironmentID , m_CreatedConfigurationID ) )
255241 Log . Debug ( "ExampleDiscoveryV1" , "Failed to get configuration" ) ;
256242
257243 while ( ! m_GetConfigurationTested )
@@ -278,27 +264,254 @@ public override IEnumerator RunTest()
278264 while ( ! m_PreviewConfigurationTested )
279265 yield return null ;
280266
281- IsCheckingState = true ;
267+ Runnable . Run ( CheckEnvironmentStatus ( ) ) ;
282268
283269 while ( ! m_IsEnvironmentActive )
284270 yield return null ;
285-
286271 #endregion
287272
288273 #region Collections
274+ // Get Collections
275+ Log . Debug ( "ExampleDiscoveryV1" , "Attempting to get collections" ) ;
276+ if ( ! m_Discovery . GetCollections ( ( GetCollectionsResponse resp , string customData ) =>
277+ {
278+ if ( resp != null )
279+ {
280+ if ( resp . collections != null && resp . collections . Length > 0 )
281+ {
282+ foreach ( CollectionRef collection in resp . collections )
283+ Log . Debug ( "ExampleDiscoveryV1" , "Collection: {0}, {1}" , collection . collection_id , collection . name ) ;
284+ }
285+ else
286+ {
287+ Log . Debug ( "ExampleDiscoveryV1" , "There are no collections" ) ;
288+ }
289+ }
290+ else
291+ {
292+ Log . Debug ( "ExampleDiscoveryV1" , "Discovery.GetCollections(); resp is null" ) ;
293+ }
294+
295+ Test ( resp != null ) ;
296+ m_GetCollectionsTested = true ;
297+ } , m_CreatedEnvironmentID ) )
298+ {
299+ Log . Debug ( "ExampleDiscovery" , "Failed to get collections" ) ;
300+ }
301+
302+ while ( ! m_GetCollectionsTested )
303+ yield return null ;
304+
305+ // Add Collection
306+ Log . Debug ( "ExampleDiscoveryV1" , "Attempting to add collection" ) ;
307+ if ( ! m_Discovery . AddCollection ( ( CollectionRef resp , string data ) =>
308+ {
309+ if ( resp != null )
310+ {
311+ Log . Debug ( "ExampleDiscoveryV1" , "Collection: {0}, {1}" , resp . collection_id , resp . name ) ;
312+ m_CreatedCollectionID = resp . collection_id ;
313+ }
314+ else
315+ {
316+ Log . Debug ( "ExampleDiscoveryV1" , "Discovery.AddCollection(); resp is null, {0}" , data ) ;
317+ }
318+
319+ Test ( ! string . IsNullOrEmpty ( resp . collection_id ) ) ;
320+ m_AddCollectionTested = true ;
321+ } , m_CreatedEnvironmentID , m_CreatedCollectionName , m_CreatedCollectionDescription , m_CreatedConfigurationID ) )
322+ Log . Debug ( "ExampleDiscovery" , "Failed to add collection" ) ;
323+
324+ while ( ! m_AddCollectionTested )
325+ yield return null ;
326+
327+ // Get Collection
328+ Log . Debug ( "ExampleDiscoveryV1" , "Attempting to get collection" ) ;
329+ if ( ! m_Discovery . GetCollection ( ( Collection resp , string data ) =>
330+ {
331+ if ( resp != null )
332+ {
333+ Log . Debug ( "ExampleDiscoveryV1" , "Collection: {0}, {1}" , resp . collection_id , resp . name ) ;
334+ }
335+ else
336+ {
337+ Log . Debug ( "ExampleDiscoveryV1" , "Failed to get collections" ) ;
338+ }
339+
340+ Test ( ! string . IsNullOrEmpty ( resp . name ) ) ;
341+ m_GetCollectionTested = true ;
342+ } , m_CreatedEnvironmentID , m_CreatedCollectionID ) )
343+ {
344+ Log . Debug ( "ExampleDiscovery" , "Failed to get collection" ) ;
345+ }
346+
347+ while ( ! m_GetCollectionTested )
348+ yield return null ;
289349 #endregion
290350
291351 #region Fields
352+ Log . Debug ( "ExampleDiscoveryV1" , "Attempting to get fields" ) ;
353+ if ( ! m_Discovery . GetFields ( ( GetFieldsResponse resp , string customData ) =>
354+ {
355+ if ( resp != null )
356+ {
357+ foreach ( Field field in resp . fields )
358+ Log . Debug ( "ExampleDiscoveryV1" , "Field: {0}, type: {1}" , field . field , field . type ) ;
359+ }
360+ else
361+ {
362+ Log . Debug ( "ExampleDiscoveryV1" , "Discovery.GetFields(); resp is null" ) ;
363+ }
364+
365+ Test ( resp != null ) ;
366+ m_GetFieldsTested = true ;
367+ } , m_CreatedEnvironmentID , m_CreatedCollectionID ) )
368+ Log . Debug ( "ExampleDiscoveryV1" , "Failed to get fields" ) ;
369+
370+ while ( ! m_GetFieldsTested )
371+ yield return null ;
372+
292373 #endregion
293374
294375 #region Documents
376+ // Add Document
377+ Log . Debug ( "ExampleDiscoveryV1" , "Attempting to add document" ) ;
378+ if ( ! m_Discovery . AddDocument ( ( DocumentAccepted resp , string data ) =>
379+ {
380+ if ( resp != null )
381+ {
382+ Log . Debug ( "ExampleDiscoveryV1" , "Added Document {0} {1}" , resp . document_id , resp . status ) ;
383+ m_CreatedDocumentID = resp . document_id ;
384+ }
385+ else
386+ {
387+ Log . Debug ( "ExampleDiscoveryV1" , "Discovery.AddDocument(); resp is null, {0}" , data ) ;
388+ }
389+
390+ Test ( ! string . IsNullOrEmpty ( resp . document_id ) ) ;
391+ m_AddDocumentTested = true ;
392+ } , m_CreatedEnvironmentID , m_CreatedCollectionID , m_DocumentFilePath , m_CreatedConfigurationID , null ) )
393+ Log . Debug ( "ExampleDiscovery" , "Failed to add document" ) ;
394+
395+ while ( ! m_AddDocumentTested )
396+ yield return null ;
397+
398+ // Get Document
399+ Log . Debug ( "ExampleDiscoveryV1" , "Attempting to get document" ) ;
400+ if ( ! m_Discovery . GetDocument ( ( DocumentStatus resp , string data ) =>
401+ {
402+ if ( resp != null )
403+ {
404+ Log . Debug ( "ExampleDiscoveryV1" , "Got Document {0} {1}" , resp . document_id , resp . status ) ;
405+ }
406+ else
407+ {
408+ Log . Debug ( "ExampleDiscoveryV1" , "Discovery.GetDocument(); resp is null, {0}" , data ) ;
409+ }
410+
411+ Test ( ! string . IsNullOrEmpty ( resp . status ) ) ;
412+ m_GetDocumentTested = true ;
413+ } , m_CreatedEnvironmentID , m_CreatedCollectionID , m_CreatedDocumentID ) )
414+ Log . Debug ( "ExampleDiscovery" , "Failed to get document" ) ;
415+
416+ while ( ! m_GetDocumentTested )
417+ yield return null ;
418+
419+ // Update Document
420+ Log . Debug ( "ExampleDiscoveryV1" , "Attempting to update document" ) ;
421+ if ( ! m_Discovery . UpdateDocument ( ( DocumentAccepted resp , string data ) =>
422+ {
423+ if ( resp != null )
424+ {
425+ Log . Debug ( "ExampleDiscoveryV1" , "Updated Document {0} {1}" , resp . document_id , resp . status ) ;
426+ }
427+ else
428+ {
429+ Log . Debug ( "ExampleDiscoveryV1" , "Discovery.UpdateDocument(); resp is null, {0}" , data ) ;
430+ }
431+
432+ Test ( ! string . IsNullOrEmpty ( resp . status ) ) ;
433+ m_UpdateDocumentTested = true ;
434+ } , m_CreatedEnvironmentID , m_CreatedCollectionID , m_CreatedDocumentID , m_DocumentFilePath , m_CreatedConfigurationID , null ) )
435+ Log . Debug ( "ExampleDiscovery" , "Failed to update document" ) ;
436+
437+ while ( ! m_UpdateDocumentTested )
438+ yield return null ;
295439 #endregion
296440
297441 #region Query
442+ // Query
443+ Log . Debug ( "ExampleDiscoveryV1" , "Attempting to query" ) ;
444+ if ( ! m_Discovery . Query ( ( QueryResponse resp , string data ) =>
445+ {
446+ if ( resp != null )
447+ {
448+ //Log.Debug("ExampleDiscoveryV1", "key: {0}, matching results: {1}", resp.aggregations.term.results.key, resp.aggregations.term.results.matching_results);
449+
450+ foreach ( QueryResult result in resp . results )
451+ Log . Debug ( "ExampleDiscoveryV1" , "Query response: id: {0}, score: {1}" , result . id , result . score ) ;
452+ }
453+ else
454+ {
455+ Log . Debug ( "ExampleDiscoveryV1" , "resp is null, {0}" , data ) ;
456+ }
457+
458+ Test ( resp != null ) ;
459+ m_QueryTested = true ;
460+ } , m_CreatedEnvironmentID , m_CreatedCollectionID , null , m_Query , null , 10 , null , 0 ) )
461+ {
462+ Log . Debug ( "ExampleDiscovery" , "Failed to query" ) ;
463+ }
464+
465+ while ( ! m_QueryTested )
466+ yield return null ;
298467 #endregion
299468
300469 #region Delete
301- // DeleteEnvironment
470+ // Delete Document
471+ Log . Debug ( "ExampleDiscoveryV1" , "Attempting to delete document {0}" , m_CreatedDocumentID ) ;
472+ if ( ! m_Discovery . DeleteDocument ( ( bool success , string data ) =>
473+ {
474+ if ( success )
475+ {
476+ Log . Debug ( "ExampleDiscoveryV1" , "Delete document successful" ) ;
477+ m_CreatedDocumentID = default ( string ) ;
478+ }
479+ else
480+ {
481+ Log . Debug ( "ExampleDiscoveryV1" , "Delete collection failed" ) ;
482+ }
483+
484+ Test ( success ) ;
485+ m_DeleteDocumentTested = true ;
486+ } , m_CreatedEnvironmentID , m_CreatedCollectionID , m_CreatedDocumentID ) )
487+ Log . Debug ( "ExampleDiscovery" , "Failed to delete document" ) ;
488+
489+ while ( ! m_DeleteDocumentTested )
490+ yield return null ;
491+
492+ // Delete Collection
493+ Log . Debug ( "ExampleDiscoveryV1" , "Attempting to delete collection {0}" , m_CreatedCollectionID ) ;
494+ if ( ! m_Discovery . DeleteCollection ( ( bool success , string data ) =>
495+ {
496+ if ( success )
497+ {
498+ Log . Debug ( "ExampleDiscoveryV1" , "Delete collection successful" ) ;
499+ m_CreatedCollectionID = default ( string ) ;
500+ }
501+ else
502+ {
503+ Log . Debug ( "ExampleDiscoveryV1" , "Delete collection failed" ) ;
504+ }
505+
506+ Test ( success ) ;
507+ m_DeleteCollectionTested = true ;
508+ } , m_CreatedEnvironmentID , m_CreatedCollectionID ) )
509+ Log . Debug ( "ExampleDiscovery" , "Failed to add collection" ) ;
510+
511+ while ( ! m_DeleteCollectionTested )
512+ yield return null ;
513+
514+ // Delete Configuration
302515 Log . Debug ( "ExampleDiscoveryV1" , "Attempting to delete configuration {0}" , m_CreatedConfigurationID ) ;
303516 if ( ! m_Discovery . DeleteConfiguration ( ( bool success , string data ) =>
304517 {
@@ -344,32 +557,33 @@ public override IEnumerator RunTest()
344557 yield break ;
345558 }
346559
347- private void CheckState ( )
560+ private IEnumerator CheckEnvironmentStatus ( )
348561 {
349- Log . Debug ( "ExampleDiscoveryV1" , "Attempting to get environment state" ) ;
562+ Log . Debug ( "ExampleDiscoveryV1" , "Waiting 5 seconds to check environment status..." ) ;
563+ yield return new WaitForSeconds ( 5f ) ;
564+
565+ Log . Debug ( "ExampleDiscoveryV1" , "Attempting to get environment status" ) ;
566+ if ( ! m_Discovery . GetEnvironment ( OnCheckEnvironmentStatus , m_CreatedEnvironmentID ) )
567+ Log . Debug ( "ExampleDiscoveryV1" , "Failed to get environment status" ) ;
568+ }
350569
351- m_Discovery . GetEnvironment ( ( Environment resp , string data ) =>
570+ private void OnCheckEnvironmentStatus ( Environment resp , string data )
571+ {
572+ if ( resp != null )
352573 {
353574 Log . Debug ( "ExampleDiscoveryV1" , "Environment {0} is {1}" , resp . environment_id , resp . status ) ;
354-
355- IsCheckingState = false ;
356575 if ( resp . status == "active" )
357- {
358576 m_IsEnvironmentActive = true ;
359- }
360577 else
361578 {
362- m_StateTimer . Elapsed += OnTimer ;
363- m_StateTimer . Enabled = true ;
579+ Runnable . Run ( CheckEnvironmentStatus ( ) ) ;
364580 }
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 ;
581+ }
582+ else
583+ {
584+ Log . Debug ( "ExampleDiscoveryV1" , "Failed to get environment status" ) ;
585+ Runnable . Run ( CheckEnvironmentStatus ( ) ) ;
586+ }
373587 }
374588 }
375589}
0 commit comments