11using BeekmanLabs . UnitTesting ;
2+ using Common . Logging ;
3+ using Elasticsearch . Net ;
24using IntegrationEngine . Core . Storage ;
35using IntegrationEngine . Model ;
46using Moq ;
@@ -33,20 +35,22 @@ public void ShouldReturnListOfDocumentsWithIdsFromElasticsearch()
3335
3436 Assert . That ( actual , Is . Not . Empty ) ;
3537 Assert . That ( actual . First ( ) . Id , Is . EqualTo ( expectedId ) ) ;
38+ elasticClient . Verify ( x => x . Search ( It . IsAny < Func < SearchDescriptor < CronTrigger > , SearchDescriptor < CronTrigger > > > ( ) ) , Times . Once ) ;
3639 }
3740
3841 [ Test ]
3942 public void ShouldReturnNullIfDocumentIsNotFoundById ( )
4043 {
4144 var elasticClient = new Mock < StubElasticClient > ( ) ;
4245 var getResponse = new Mock < StubGetResponse < CronTrigger > > ( ) ;
43- elasticClient . Setup ( x => x . Get < CronTrigger > ( It . IsAny < Func < GetDescriptor < CronTrigger > , GetDescriptor < CronTrigger > > > ( ) ) )
46+ elasticClient . Setup ( x => x . Get ( It . IsAny < Func < GetDescriptor < CronTrigger > , GetDescriptor < CronTrigger > > > ( ) ) )
4447 . Returns ( getResponse . Object ) ;
4548 Subject . ElasticClient = elasticClient . Object ;
4649
4750 var actual = Subject . SelectById < CronTrigger > ( "1" ) ;
4851
4952 Assert . That ( actual , Is . Null ) ;
53+ elasticClient . Verify ( x => x . Get ( It . IsAny < Func < GetDescriptor < CronTrigger > , GetDescriptor < CronTrigger > > > ( ) ) , Times . Once ) ;
5054 }
5155
5256 [ Test ]
@@ -57,13 +61,125 @@ public void ShouldReturnSingleDocumentGivenAnId()
5761 var expectedId = "1" ;
5862 getResponse . SetupGet ( x => x . Id ) . Returns ( ( ) => expectedId ) ;
5963 getResponse . SetupGet ( x => x . Source ) . Returns ( ( ) => new CronTrigger ( ) ) ;
60- elasticClient . Setup ( x => x . Get < CronTrigger > ( It . IsAny < Func < GetDescriptor < CronTrigger > , GetDescriptor < CronTrigger > > > ( ) ) )
64+ elasticClient . Setup ( x => x . Get ( It . IsAny < Func < GetDescriptor < CronTrigger > , GetDescriptor < CronTrigger > > > ( ) ) )
6165 . Returns ( getResponse . Object ) ;
6266 Subject . ElasticClient = elasticClient . Object ;
6367
6468 var actual = Subject . SelectById < CronTrigger > ( expectedId ) ;
6569
6670 Assert . That ( actual . Id , Is . EqualTo ( expectedId ) ) ;
71+ elasticClient . Verify ( x => x . Get ( It . IsAny < Func < GetDescriptor < CronTrigger > , GetDescriptor < CronTrigger > > > ( ) ) , Times . Once ) ;
72+ }
73+
74+ [ Test ]
75+ public void ShouldInsertAndReturnDocument ( )
76+ {
77+ var expected = new CronTrigger ( ) {
78+ Id = "1" ,
79+ } ;
80+ var elasticClient = new Mock < StubElasticClient > ( ) ;
81+ var getResponse = new Mock < StubGetResponse < CronTrigger > > ( ) ;
82+ getResponse . SetupGet ( x => x . Id ) . Returns ( ( ) => expected . Id ) ;
83+ getResponse . SetupGet ( x => x . Source ) . Returns ( ( ) => new CronTrigger ( ) ) ;
84+ elasticClient . Setup ( x => x . Get < CronTrigger > ( It . IsAny < Func < GetDescriptor < CronTrigger > , GetDescriptor < CronTrigger > > > ( ) ) )
85+ . Returns ( getResponse . Object ) ;
86+ var indexResponse = new Mock < StubIndexResponse > ( ) ;
87+ indexResponse . SetupGet ( x => x . Id ) . Returns ( ( ) => expected . Id ) ;
88+ elasticClient . Setup ( x => x . Index ( expected , It . IsAny < Func < IndexDescriptor < CronTrigger > , IndexDescriptor < CronTrigger > > > ( ) ) )
89+ . Returns ( indexResponse . Object ) ;
90+ Subject . ElasticClient = elasticClient . Object ;
91+
92+ var actual = Subject . Insert ( expected ) ;
93+
94+ Assert . That ( actual . Id , Is . EqualTo ( expected . Id ) ) ;
95+ elasticClient . Verify ( x => x . Index ( expected , It . IsAny < Func < IndexDescriptor < CronTrigger > , IndexDescriptor < CronTrigger > > > ( ) ) , Times . Once ) ;
96+ }
97+
98+ [ Test ]
99+ public void ShouldUpdateAndReturnDocument ( )
100+ {
101+ var expected = new CronTrigger ( ) {
102+ Id = "1" ,
103+ } ;
104+ var elasticClient = new Mock < StubElasticClient > ( ) ;
105+ var getResponse = new Mock < StubGetResponse < CronTrigger > > ( ) ;
106+ getResponse . SetupGet ( x => x . Id ) . Returns ( ( ) => expected . Id ) ;
107+ getResponse . SetupGet ( x => x . Source ) . Returns ( ( ) => new CronTrigger ( ) ) ;
108+ elasticClient . Setup ( x => x . Get < CronTrigger > ( It . IsAny < Func < GetDescriptor < CronTrigger > , GetDescriptor < CronTrigger > > > ( ) ) )
109+ . Returns ( getResponse . Object ) ;
110+ var updateResponse = new Mock < StubUpdateResponse > ( ) ;
111+ updateResponse . SetupGet ( x => x . Id ) . Returns ( ( ) => expected . Id ) ;
112+ elasticClient . Setup ( x => x . Update < CronTrigger > ( It . IsAny < Func < UpdateDescriptor < CronTrigger , CronTrigger > , UpdateDescriptor < CronTrigger , CronTrigger > > > ( ) ) )
113+ . Returns ( updateResponse . Object ) ;
114+ Subject . ElasticClient = elasticClient . Object ;
115+
116+ var actual = Subject . Update ( expected ) ;
117+
118+ Assert . That ( actual . Id , Is . EqualTo ( expected . Id ) ) ;
119+ elasticClient . Verify (
120+ x => x . Update < CronTrigger > ( It . IsAny < Func < UpdateDescriptor < CronTrigger , CronTrigger > , UpdateDescriptor < CronTrigger , CronTrigger > > > ( ) ) ,
121+ Times . Once ) ;
122+ }
123+
124+ [ Test ]
125+ public void ShouldDeleteDocument ( )
126+ {
127+ var id = "1" ;
128+ var elasticClient = new Mock < StubElasticClient > ( ) ;
129+ elasticClient . Setup ( x => x . Delete ( It . IsAny < Func < DeleteDescriptor < CronTrigger > , DeleteDescriptor < CronTrigger > > > ( ) ) ) ;
130+ Subject . ElasticClient = elasticClient . Object ;
131+
132+ Subject . Delete < CronTrigger > ( id ) ;
133+
134+ elasticClient . Verify ( x => x . Delete ( It . IsAny < Func < DeleteDescriptor < CronTrigger > , DeleteDescriptor < CronTrigger > > > ( ) ) , Times . Once ) ;
135+ }
136+
137+ [ Test ]
138+ public void ShouldShouldReturnTrueIfServerIsAvailable ( )
139+ {
140+ var elasticClient = new Mock < StubElasticClient > ( ) ;
141+ var elasticsearchResponse = new Mock < StubElasticsearchResponse > ( ) ;
142+ elasticsearchResponse . SetupGet ( x => x . Success ) . Returns ( ( ) => true ) ;
143+ var pingResponse = new Mock < StubPingResponse > ( ) ;
144+ pingResponse . SetupGet ( x => x . ConnectionStatus ) . Returns ( ( ) => elasticsearchResponse . Object ) ;
145+ elasticClient . Setup ( x => x . Ping ( It . IsAny < PingRequest > ( ) ) ) . Returns ( pingResponse . Object ) ;
146+ Subject . ElasticClient = elasticClient . Object ;
147+
148+ var actual = Subject . IsServerAvailable ( ) ;
149+
150+ Assert . That ( actual , Is . True ) ;
151+ }
152+
153+ [ Test ]
154+ public void ShouldShouldReturnFalseIfServerIsUnavailable ( )
155+ {
156+ var elasticClient = new Mock < StubElasticClient > ( ) ;
157+ var elasticsearchResponse = new Mock < StubElasticsearchResponse > ( ) ;
158+ elasticsearchResponse . SetupGet ( x => x . Success ) . Returns ( ( ) => false ) ;
159+ var pingResponse = new Mock < StubPingResponse > ( ) ;
160+ pingResponse . SetupGet ( x => x . ConnectionStatus ) . Returns ( ( ) => elasticsearchResponse . Object ) ;
161+ elasticClient . Setup ( x => x . Ping ( It . IsAny < PingRequest > ( ) ) ) . Returns ( pingResponse . Object ) ;
162+ Subject . ElasticClient = elasticClient . Object ;
163+
164+ var actual = Subject . IsServerAvailable ( ) ;
165+
166+ Assert . That ( actual , Is . False ) ;
167+ }
168+
169+ [ Test ]
170+ public void ShouldShouldReturnFalseIfServerIsUnavailableBecauseExceptionOccured ( )
171+ {
172+ var elasticClient = new Mock < StubElasticClient > ( ) ;
173+ elasticClient . Setup ( x => x . Ping ( It . IsAny < PingRequest > ( ) ) ) . Returns < PingResponse > ( null ) ;
174+ Subject . ElasticClient = elasticClient . Object ;
175+ var log = new Mock < ILog > ( ) ;
176+ log . Setup ( x => x . Error ( It . IsAny < Exception > ( ) ) ) ;
177+ Subject . Log = log . Object ;
178+
179+ var actual = Subject . IsServerAvailable ( ) ;
180+
181+ Assert . That ( actual , Is . False ) ;
182+ log . Verify ( x => x . Error ( It . IsAny < Exception > ( ) ) , Times . Once ) ;
67183 }
68184 }
69185}
0 commit comments