2424using System . IO ;
2525using System . Collections . Generic ;
2626using IBM . Watson . DeveloperCloud . Connection ;
27+ using System ;
28+ using Environment = IBM . Watson . DeveloperCloud . Services . Discovery . v1 . Environment ;
2729
2830namespace IBM . Watson . DeveloperCloud . UnitTests
2931{
@@ -76,6 +78,16 @@ public class TestDiscovery : UnitTest
7678 private bool _deleteCredentialsTested = false ;
7779 private string _createdCredentialId = null ;
7880
81+ private bool _listExpansionsTested = false ;
82+ private bool _createExpansionTested = false ;
83+ private bool _deleteExpansionTested = false ;
84+ private bool _getTokenizationDictStatusTested = false ;
85+ private bool _createTokenizationDictTested = false ;
86+ private bool _deleteTokenizationDictTested = false ;
87+ private string _createdJpCollection = null ;
88+ private bool _createJpCollectionTested = false ;
89+ private bool _deleteJpCollectionTested = false ;
90+
7991 private bool _createEventTested = false ;
8092 private bool _getMetricsEventRateTested = false ;
8193 private bool _getMetricsQueryTested = false ;
@@ -184,7 +196,7 @@ public override IEnumerator RunTest()
184196
185197 // Add Collection
186198 Log . Debug ( "TestDiscovery.RunTest()" , "Attempting to add collection" ) ;
187- if ( ! _discovery . AddCollection ( OnAddCollection , OnFail , _environmentId , _createdCollectionName + System . Guid . NewGuid ( ) . ToString ( ) , _createdCollectionDescription , _createdConfigurationID ) )
199+ if ( ! _discovery . AddCollection ( OnAddCollection , OnFail , _environmentId , _createdCollectionName + System . Guid . NewGuid ( ) . ToString ( ) , _createdCollectionDescription , _createdConfigurationID , "en" ) )
188200 Log . Debug ( "TestDiscovery.AddCollection()" , "Failed to add collection" ) ;
189201 while ( ! _addCollectionTested )
190202 yield return null ;
@@ -313,6 +325,88 @@ public override IEnumerator RunTest()
313325 while ( ! _createEventTested )
314326 yield return null ;
315327
328+ Log . Debug ( "TestDiscovery.RunTests()" , "Attempting to list expansions" ) ;
329+ _discovery . ListExpansions ( OnListExpansions , OnFail , _environmentId , _createdCollectionId ) ;
330+ while ( ! _listExpansionsTested )
331+ yield return null ;
332+
333+ Log . Debug ( "TestDiscovery.RunTests()" , "Attempting to create expansion" ) ;
334+ Expansions expansions = new Expansions ( )
335+ {
336+ ExpansionsProperty = new List < Expansion > ( )
337+ {
338+ new Expansion ( )
339+ {
340+ ExpandedTerms = new List < string > ( )
341+ {
342+ "expanded-term"
343+ } ,
344+ InputTerms = new List < string > ( )
345+ {
346+ "input-term"
347+ }
348+ }
349+ }
350+ } ;
351+ _discovery . CreateExpansions ( OnCreateExpansion , OnFail , _environmentId , _createdCollectionId , expansions ) ;
352+ while ( ! _createExpansionTested )
353+ yield return null ;
354+
355+ Log . Debug ( "TestDiscovery.RunTests()" , "Attempting to delete expansion" ) ;
356+ _discovery . DeleteExpansions ( OnDeleteExpansion , OnFail , _environmentId , _createdCollectionId ) ;
357+ while ( ! _deleteExpansionTested )
358+ yield return null ;
359+
360+ Log . Debug ( "TestDiscovery.RunTest()" , "Attempting to create jp collection" ) ;
361+ _discovery . AddCollection ( OnCreateJpCollection , OnFail , _environmentId , _createdCollectionName + System . Guid . NewGuid ( ) . ToString ( ) , _createdCollectionDescription , _createdConfigurationID , "ja" ) ;
362+ while ( ! _createJpCollectionTested )
363+ yield return null ;
364+
365+ TokenDict tokenizationDictionary = new TokenDict ( )
366+ {
367+ TokenizationRules = new List < TokenDictRule > ( )
368+ {
369+ new TokenDictRule ( )
370+ {
371+ Text = "すしネコ" ,
372+ Tokens = new List < string > ( )
373+ {
374+ "すし" , "ネコ"
375+ } ,
376+ Readings = new List < string > ( )
377+ {
378+ "寿司" , "ネコ"
379+ } ,
380+ PartOfSpeech = "カスタム名詞"
381+ }
382+ }
383+ } ;
384+ Log . Debug ( "TestDiscovery.RunTests()" , "Attempting to create tokenization dict" ) ;
385+ _discovery . CreateTokenizationDictionary ( OnCreateTokenizationDictionary , OnCreateTokenizationDictionaryFail , _environmentId , _createdJpCollection , tokenizationDictionary ) ;
386+ while ( ! _createTokenizationDictTested )
387+ yield return null ;
388+
389+ if ( ! _getTokenizationDictStatusTested )
390+ {
391+ Log . Debug ( "TestDiscovery.RunTests()" , "Attempting to get tokenization dict status" ) ;
392+ _discovery . GetTokenizationDictionaryStatus ( OnGetTokenizationDictonaryStatus , OnFail , _environmentId , _createdJpCollection ) ;
393+ while ( ! _getTokenizationDictStatusTested )
394+ yield return null ;
395+ }
396+
397+ if ( ! _deleteTokenizationDictTested )
398+ {
399+ Log . Debug ( "TestDiscovery.RunTests()" , "Attempting to delete tokenization dict" ) ;
400+ _discovery . DeleteTokenizationDictionary ( OnDeleteTokenizationDictionary , OnFail , _environmentId , _createdJpCollection ) ;
401+ while ( ! _deleteTokenizationDictTested )
402+ yield return null ;
403+ }
404+
405+ Log . Debug ( "TestDiscovery.RunTest()" , "Attempting to delete jp collection" ) ;
406+ _discovery . DeleteCollection ( OnDeleteJpCollection , OnFail , _environmentId , _createdJpCollection ) ;
407+ while ( ! _deleteJpCollectionTested )
408+ yield return null ;
409+
316410 // DeleteCredential
317411 Log . Debug ( "TestDiscovery.RunTest()" , "Attempting to delete credential" ) ;
318412 _discovery . DeleteCredentials ( OnDeleteCredentials , OnFail , _environmentId , _createdCredentialId ) ;
@@ -361,6 +455,73 @@ public override IEnumerator RunTest()
361455 yield break ;
362456 }
363457
458+ private void OnCreateJpCollection ( CollectionRef response , Dictionary < string , object > customData )
459+ {
460+ Log . Debug ( "TestDiscovery.OnAddJpCollection()" , "Discovery - add jp collection Response: added:{0}" , customData [ "json" ] . ToString ( ) ) ;
461+ _createdJpCollection = response . collection_id ;
462+ Test ( response != null ) ;
463+ _createJpCollectionTested = true ;
464+ }
465+
466+ private void OnDeleteJpCollection ( DeleteCollectionResponse response , Dictionary < string , object > customData )
467+ {
468+ Log . Debug ( "TestDiscovery.OnDeleteJpCollection()" , "Discovery - Delete jp collection Response: deleted:{0}" , customData [ "json" ] . ToString ( ) ) ;
469+
470+ _createdJpCollection = default ( string ) ;
471+ Test ( response != null ) ;
472+
473+ _deleteJpCollectionTested = true ;
474+ }
475+
476+ private void OnDeleteTokenizationDictionary ( object response , Dictionary < string , object > customData )
477+ {
478+ Log . Debug ( "TestDiscovery.OnDeleteTokenizationDictionary()" , "Discovery - delete tokenization dictionary: deleted:{0}" , customData [ "json" ] . ToString ( ) ) ;
479+ Test ( response != null ) ;
480+
481+ _deleteTokenizationDictTested = true ;
482+ }
483+
484+ private void OnGetTokenizationDictonaryStatus ( TokenDictStatusResponse response , Dictionary < string , object > customData )
485+ {
486+ Log . Debug ( "TestDiscovery.OnGetTokenizationDictonaryStatus()" , "Discovery - get tokenization dictionary status: {0}" , customData [ "json" ] . ToString ( ) ) ;
487+ Test ( response != null ) ;
488+ _getTokenizationDictStatusTested = true ;
489+ }
490+
491+
492+ private void OnCreateTokenizationDictionary ( TokenDictStatusResponse response , Dictionary < string , object > customData )
493+ {
494+ Log . Debug ( "TestDiscovery.OnCreateTokenizationDictionary()" , "Discovery - create tokenization dictionary status: {0}" , customData [ "json" ] . ToString ( ) ) ;
495+ Test ( response != null ) ;
496+ _createTokenizationDictTested = true ;
497+ }
498+
499+
500+
501+ private void OnDeleteExpansion ( object response , Dictionary < string , object > customData )
502+ {
503+ Log . Debug ( "TestDiscovery.OnDeleteExpansion()" , "Discovery - delete expansion: deleted" ) ;
504+ Test ( response != null ) ;
505+
506+ _deleteExpansionTested = true ;
507+ }
508+
509+ private void OnCreateExpansion ( Expansions response , Dictionary < string , object > customData )
510+ {
511+ Log . Debug ( "TestDiscovery.OnCreateExpansion()" , "Discovery - create expansion: {0}" , customData [ "json" ] . ToString ( ) ) ;
512+ Test ( response != null ) ;
513+
514+ _createExpansionTested = true ;
515+ }
516+
517+ private void OnListExpansions ( Expansions response , Dictionary < string , object > customData )
518+ {
519+ Log . Debug ( "TestDiscovery.OnListExpansions()" , "Discovery - list expansions: {0}" , customData [ "json" ] . ToString ( ) ) ;
520+ Test ( response != null ) ;
521+
522+ _listExpansionsTested = true ;
523+ }
524+
364525 #region Check State
365526 private IEnumerator CheckEnvironmentState ( float waitTime )
366527 {
@@ -542,59 +703,69 @@ private void OnDeleteUserData(object response, Dictionary<string, object> custom
542703 private void OnListCredentials ( CredentialsList response , Dictionary < string , object > customData )
543704 {
544705 Log . Debug ( "TestDiscovery.OnListCredentials()" , "Response: {0}" , customData [ "json" ] . ToString ( ) ) ;
706+ Test ( response != null ) ;
545707 _listCredentialsTested = true ;
546708 }
547709 private void OnCreateCredentials ( SourceCredentials response , Dictionary < string , object > customData )
548710 {
549711 Log . Debug ( "TestDiscovery.OnCreateCredentials()" , "Response: {0}" , customData [ "json" ] . ToString ( ) ) ;
712+ Test ( response != null ) ;
550713 _createdCredentialId = response . CredentialId ;
551714 _createCredentialsTested = true ;
552715 }
553716
554717 private void OnGetCredential ( SourceCredentials response , Dictionary < string , object > customData )
555718 {
556719 Log . Debug ( "TestDiscovery.OnGetCredential()" , "Response: {0}" , customData [ "json" ] . ToString ( ) ) ;
720+ Test ( response != null ) ;
557721 _getCredentialTested = true ;
558722 }
559723
560724 private void OnDeleteCredentials ( DeleteCredentials response , Dictionary < string , object > customData )
561725 {
562726 Log . Debug ( "TestDiscovery.OnDeleteCredentials()" , "Response: {0}" , customData [ "json" ] . ToString ( ) ) ;
727+ Test ( response != null ) ;
563728 _deleteCredentialsTested = true ;
564729 }
565730 private void OnCreateEvent ( CreateEventResponse response , Dictionary < string , object > customData )
566731 {
567732 Log . Debug ( "TestDiscovery.OnCreateEvent()" , "Response: {0}" , customData [ "json" ] . ToString ( ) ) ;
733+ Test ( response != null ) ;
568734 _createEventTested = true ;
569735 }
570736
571737 private void OnGetMetricsEventRate ( MetricResponse response , Dictionary < string , object > customData )
572738 {
573739 Log . Debug ( "TestDiscovery.OnGetMetricsEventRate()" , "Response: {0}" , customData [ "json" ] . ToString ( ) ) ;
740+ Test ( response != null ) ;
574741 _getMetricsEventRateTested = true ;
575742 }
576743
577744 private void OnGetMetricsQuery ( MetricResponse response , Dictionary < string , object > customData )
578745 {
579746 Log . Debug ( "TestDiscovery.OnGetMetricsQuery()" , "Response: {0}" , customData [ "json" ] . ToString ( ) ) ;
747+ Test ( response != null ) ;
580748 _getMetricsQueryTested = true ;
581749 }
582750
583751 private void OnGetMetricsQueryEvent ( MetricResponse response , Dictionary < string , object > customData )
584752 {
585753 Log . Debug ( "TestDiscovery.OnGetMetricsQueryEvent()" , "Response: {0}" , customData [ "json" ] . ToString ( ) ) ;
754+ Test ( response != null ) ;
586755 _getMetricsQueryEventTested = true ;
587756 }
588757
589758 private void OnGetMetricsQueryNoResult ( MetricResponse response , Dictionary < string , object > customData )
590759 {
591760 Log . Debug ( "TestDiscovery.OnGetMetricsQueryNoResult()" , "Response: {0}" , customData [ "json" ] . ToString ( ) ) ;
761+ Test ( response != null ) ;
592762 _getMetricsQueryNoResultTested = true ;
593763 }
594764
595765 private void OnGetMetricsQueryTokenEvent ( MetricTokenResponse response , Dictionary < string , object > customData )
596766 {
597767 Log . Debug ( "TestDiscovery.OnGetMetricsQueryTokenEvent()" , "Response: {0}" , customData [ "json" ] . ToString ( ) ) ;
768+ Test ( response != null ) ;
598769 _getMetricsQueryTokenEventTested = true ;
599770 }
600771
@@ -608,5 +779,13 @@ private void OnFail(RESTConnector.Error error, Dictionary<string, object> custom
608779 {
609780 Log . Error ( "TestDiscovery.OnFail()" , "Error received: {0}" , error . ToString ( ) ) ;
610781 }
782+
783+ private void OnCreateTokenizationDictionaryFail ( RESTConnector . Error error , Dictionary < string , object > customData )
784+ {
785+ Log . Error ( "TestDiscovery.OnCreateTokenizationDictionaryFail()" , "Error received - Continuing without testing tokenization dictionaries: {0}" , error . ToString ( ) ) ;
786+ _createTokenizationDictTested = true ;
787+ _getTokenizationDictStatusTested = true ;
788+ _deleteTokenizationDictTested = true ;
789+ }
611790 }
612791}
0 commit comments