@@ -14,10 +14,21 @@ namespace IntegrationEngine.Core.Tests.Storage
1414{
1515 public class ElasticsearchRepositoryTest : TestBase < ElasticsearchRepository >
1616 {
17+ Mock < StubElasticClient > ElasticClient { get ; set ; }
18+ Mock < ILog > Log { get ; set ; }
19+
20+ [ SetUp ]
21+ public void Setup ( )
22+ {
23+ ElasticClient = new Mock < StubElasticClient > ( ) ;
24+ Subject . ElasticClient = ElasticClient . Object ;
25+ Log = new Mock < ILog > ( ) ;
26+ Subject . Log = Log . Object ;
27+ }
28+
1729 [ Test ]
1830 public void ShouldReturnListOfDocumentsWithIdsFromElasticsearch ( )
1931 {
20- var elasticClient = new Mock < StubElasticClient > ( ) ;
2132 var searchResponse = new Mock < StubSearchResponse < CronTrigger > > ( ) ;
2233 var hits = new List < IHit < CronTrigger > > ( ) ;
2334 var cronTrigger = new CronTrigger ( ) ;
@@ -27,48 +38,44 @@ public void ShouldReturnListOfDocumentsWithIdsFromElasticsearch()
2738 hit . SetupGet ( x => x . Id ) . Returns ( ( ) => expectedId ) ;
2839 hits . Add ( hit . Object ) ;
2940 searchResponse . SetupGet ( x => x . Hits ) . Returns ( ( ) => hits ) ;
30- elasticClient . Setup ( x => x . Search < CronTrigger > ( It . IsAny < Func < SearchDescriptor < CronTrigger > , SearchDescriptor < CronTrigger > > > ( ) ) )
41+ ElasticClient . Setup ( x => x . Search < CronTrigger > ( It . IsAny < Func < SearchDescriptor < CronTrigger > , SearchDescriptor < CronTrigger > > > ( ) ) )
3142 . Returns ( searchResponse . Object ) ;
32- Subject . ElasticClient = elasticClient . Object ;
43+
3344
3445 var actual = Subject . SelectAll < CronTrigger > ( ) ;
3546
3647 Assert . That ( actual , Is . Not . Empty ) ;
3748 Assert . That ( actual . First ( ) . Id , Is . EqualTo ( expectedId ) ) ;
38- elasticClient . Verify ( x => x . Search ( It . IsAny < Func < SearchDescriptor < CronTrigger > , SearchDescriptor < CronTrigger > > > ( ) ) , Times . Once ) ;
49+ ElasticClient . Verify ( x => x . Search ( It . IsAny < Func < SearchDescriptor < CronTrigger > , SearchDescriptor < CronTrigger > > > ( ) ) , Times . Once ) ;
3950 }
4051
4152 [ Test ]
4253 public void ShouldReturnNullIfDocumentIsNotFoundById ( )
4354 {
44- var elasticClient = new Mock < StubElasticClient > ( ) ;
4555 var getResponse = new Mock < StubGetResponse < CronTrigger > > ( ) ;
46- elasticClient . Setup ( x => x . Get ( It . IsAny < Func < GetDescriptor < CronTrigger > , GetDescriptor < CronTrigger > > > ( ) ) )
56+ ElasticClient . Setup ( x => x . Get ( It . IsAny < Func < GetDescriptor < CronTrigger > , GetDescriptor < CronTrigger > > > ( ) ) )
4757 . Returns ( getResponse . Object ) ;
48- Subject . ElasticClient = elasticClient . Object ;
4958
5059 var actual = Subject . SelectById < CronTrigger > ( "1" ) ;
5160
5261 Assert . That ( actual , Is . Null ) ;
53- elasticClient . Verify ( x => x . Get ( It . IsAny < Func < GetDescriptor < CronTrigger > , GetDescriptor < CronTrigger > > > ( ) ) , Times . Once ) ;
62+ ElasticClient . Verify ( x => x . Get ( It . IsAny < Func < GetDescriptor < CronTrigger > , GetDescriptor < CronTrigger > > > ( ) ) , Times . Once ) ;
5463 }
5564
5665 [ Test ]
5766 public void ShouldReturnSingleDocumentGivenAnId ( )
5867 {
59- var elasticClient = new Mock < StubElasticClient > ( ) ;
6068 var getResponse = new Mock < StubGetResponse < CronTrigger > > ( ) ;
6169 var expectedId = "1" ;
6270 getResponse . SetupGet ( x => x . Id ) . Returns ( ( ) => expectedId ) ;
6371 getResponse . SetupGet ( x => x . Source ) . Returns ( ( ) => new CronTrigger ( ) ) ;
64- elasticClient . Setup ( x => x . Get ( It . IsAny < Func < GetDescriptor < CronTrigger > , GetDescriptor < CronTrigger > > > ( ) ) )
72+ ElasticClient . Setup ( x => x . Get ( It . IsAny < Func < GetDescriptor < CronTrigger > , GetDescriptor < CronTrigger > > > ( ) ) )
6573 . Returns ( getResponse . Object ) ;
66- Subject . ElasticClient = elasticClient . Object ;
6774
6875 var actual = Subject . SelectById < CronTrigger > ( expectedId ) ;
6976
7077 Assert . That ( actual . Id , Is . EqualTo ( expectedId ) ) ;
71- elasticClient . Verify ( x => x . Get ( It . IsAny < Func < GetDescriptor < CronTrigger > , GetDescriptor < CronTrigger > > > ( ) ) , Times . Once ) ;
78+ ElasticClient . Verify ( x => x . Get ( It . IsAny < Func < GetDescriptor < CronTrigger > , GetDescriptor < CronTrigger > > > ( ) ) , Times . Once ) ;
7279 }
7380
7481 [ Test ]
@@ -77,22 +84,20 @@ public void ShouldInsertAndReturnDocument()
7784 var expected = new CronTrigger ( ) {
7885 Id = "1" ,
7986 } ;
80- var elasticClient = new Mock < StubElasticClient > ( ) ;
8187 var getResponse = new Mock < StubGetResponse < CronTrigger > > ( ) ;
8288 getResponse . SetupGet ( x => x . Id ) . Returns ( ( ) => expected . Id ) ;
8389 getResponse . SetupGet ( x => x . Source ) . Returns ( ( ) => new CronTrigger ( ) ) ;
84- elasticClient . Setup ( x => x . Get < CronTrigger > ( It . IsAny < Func < GetDescriptor < CronTrigger > , GetDescriptor < CronTrigger > > > ( ) ) )
90+ ElasticClient . Setup ( x => x . Get < CronTrigger > ( It . IsAny < Func < GetDescriptor < CronTrigger > , GetDescriptor < CronTrigger > > > ( ) ) )
8591 . Returns ( getResponse . Object ) ;
8692 var indexResponse = new Mock < StubIndexResponse > ( ) ;
8793 indexResponse . SetupGet ( x => x . Id ) . Returns ( ( ) => expected . Id ) ;
88- elasticClient . Setup ( x => x . Index ( expected , It . IsAny < Func < IndexDescriptor < CronTrigger > , IndexDescriptor < CronTrigger > > > ( ) ) )
94+ ElasticClient . Setup ( x => x . Index ( expected , It . IsAny < Func < IndexDescriptor < CronTrigger > , IndexDescriptor < CronTrigger > > > ( ) ) )
8995 . Returns ( indexResponse . Object ) ;
90- Subject . ElasticClient = elasticClient . Object ;
9196
9297 var actual = Subject . Insert ( expected ) ;
9398
9499 Assert . That ( actual . Id , Is . EqualTo ( expected . Id ) ) ;
95- elasticClient . Verify ( x => x . Index ( expected , It . IsAny < Func < IndexDescriptor < CronTrigger > , IndexDescriptor < CronTrigger > > > ( ) ) , Times . Once ) ;
100+ ElasticClient . Verify ( x => x . Index ( expected , It . IsAny < Func < IndexDescriptor < CronTrigger > , IndexDescriptor < CronTrigger > > > ( ) ) , Times . Once ) ;
96101 }
97102
98103 [ Test ]
@@ -101,22 +106,20 @@ public void ShouldUpdateAndReturnDocument()
101106 var expected = new CronTrigger ( ) {
102107 Id = "1" ,
103108 } ;
104- var elasticClient = new Mock < StubElasticClient > ( ) ;
105109 var getResponse = new Mock < StubGetResponse < CronTrigger > > ( ) ;
106110 getResponse . SetupGet ( x => x . Id ) . Returns ( ( ) => expected . Id ) ;
107111 getResponse . SetupGet ( x => x . Source ) . Returns ( ( ) => new CronTrigger ( ) ) ;
108- elasticClient . Setup ( x => x . Get < CronTrigger > ( It . IsAny < Func < GetDescriptor < CronTrigger > , GetDescriptor < CronTrigger > > > ( ) ) )
112+ ElasticClient . Setup ( x => x . Get < CronTrigger > ( It . IsAny < Func < GetDescriptor < CronTrigger > , GetDescriptor < CronTrigger > > > ( ) ) )
109113 . Returns ( getResponse . Object ) ;
110114 var updateResponse = new Mock < StubUpdateResponse > ( ) ;
111115 updateResponse . SetupGet ( x => x . Id ) . Returns ( ( ) => expected . Id ) ;
112- elasticClient . Setup ( x => x . Update < CronTrigger > ( It . IsAny < Func < UpdateDescriptor < CronTrigger , CronTrigger > , UpdateDescriptor < CronTrigger , CronTrigger > > > ( ) ) )
116+ ElasticClient . Setup ( x => x . Update < CronTrigger > ( It . IsAny < Func < UpdateDescriptor < CronTrigger , CronTrigger > , UpdateDescriptor < CronTrigger , CronTrigger > > > ( ) ) )
113117 . Returns ( updateResponse . Object ) ;
114- Subject . ElasticClient = elasticClient . Object ;
115118
116119 var actual = Subject . Update ( expected ) ;
117120
118121 Assert . That ( actual . Id , Is . EqualTo ( expected . Id ) ) ;
119- elasticClient . Verify (
122+ ElasticClient . Verify (
120123 x => x . Update < CronTrigger > ( It . IsAny < Func < UpdateDescriptor < CronTrigger , CronTrigger > , UpdateDescriptor < CronTrigger , CronTrigger > > > ( ) ) ,
121124 Times . Once ) ;
122125 }
@@ -125,25 +128,21 @@ public void ShouldUpdateAndReturnDocument()
125128 public void ShouldDeleteDocument ( )
126129 {
127130 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+ ElasticClient . Setup ( x => x . Delete ( It . IsAny < Func < DeleteDescriptor < CronTrigger > , DeleteDescriptor < CronTrigger > > > ( ) ) ) ;
131132
132133 Subject . Delete < CronTrigger > ( id ) ;
133134
134- elasticClient . Verify ( x => x . Delete ( It . IsAny < Func < DeleteDescriptor < CronTrigger > , DeleteDescriptor < CronTrigger > > > ( ) ) , Times . Once ) ;
135+ ElasticClient . Verify ( x => x . Delete ( It . IsAny < Func < DeleteDescriptor < CronTrigger > , DeleteDescriptor < CronTrigger > > > ( ) ) , Times . Once ) ;
135136 }
136137
137138 [ Test ]
138139 public void ShouldShouldReturnTrueIfServerIsAvailable ( )
139140 {
140- var elasticClient = new Mock < StubElasticClient > ( ) ;
141141 var elasticsearchResponse = new Mock < StubElasticsearchResponse > ( ) ;
142142 elasticsearchResponse . SetupGet ( x => x . Success ) . Returns ( ( ) => true ) ;
143143 var pingResponse = new Mock < StubPingResponse > ( ) ;
144144 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 ;
145+ ElasticClient . Setup ( x => x . Ping ( It . IsAny < PingRequest > ( ) ) ) . Returns ( pingResponse . Object ) ;
147146
148147 var actual = Subject . IsServerAvailable ( ) ;
149148
@@ -153,13 +152,11 @@ public void ShouldShouldReturnTrueIfServerIsAvailable()
153152 [ Test ]
154153 public void ShouldShouldReturnFalseIfServerIsUnavailable ( )
155154 {
156- var elasticClient = new Mock < StubElasticClient > ( ) ;
157155 var elasticsearchResponse = new Mock < StubElasticsearchResponse > ( ) ;
158156 elasticsearchResponse . SetupGet ( x => x . Success ) . Returns ( ( ) => false ) ;
159157 var pingResponse = new Mock < StubPingResponse > ( ) ;
160158 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 ;
159+ ElasticClient . Setup ( x => x . Ping ( It . IsAny < PingRequest > ( ) ) ) . Returns ( pingResponse . Object ) ;
163160
164161 var actual = Subject . IsServerAvailable ( ) ;
165162
@@ -169,17 +166,13 @@ public void ShouldShouldReturnFalseIfServerIsUnavailable()
169166 [ Test ]
170167 public void ShouldShouldReturnFalseIfServerIsUnavailableBecauseExceptionOccured ( )
171168 {
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 ;
169+ ElasticClient . Setup ( x => x . Ping ( It . IsAny < PingRequest > ( ) ) ) . Returns < PingResponse > ( null ) ;
170+ Log . Setup ( x => x . Error ( It . IsAny < Exception > ( ) ) ) ;
178171
179172 var actual = Subject . IsServerAvailable ( ) ;
180173
181174 Assert . That ( actual , Is . False ) ;
182- log . Verify ( x => x . Error ( It . IsAny < Exception > ( ) ) , Times . Once ) ;
175+ Log . Verify ( x => x . Error ( It . IsAny < Exception > ( ) ) , Times . Once ) ;
183176 }
184177 }
185178}
0 commit comments