33using System . Linq ;
44using System . Text ;
55using System . Threading . Tasks ;
6- using Elasticsearch . Net ;
7- using Elasticsearch . Net . Extensions ;
8- using FluentAssertions ;
9- using Nest ;
10- using Tests . Core . Extensions ;
11- using Tests . Framework . Extensions ;
12-
13- namespace Tests . Framework . VirtualClustering . Audit
6+ using Elasticsearch . Net . VirtualizedCluster . Extensions ;
7+
8+ namespace Elasticsearch . Net . VirtualizedCluster . Audit
149{
1510 public class Auditor
1611 {
@@ -36,8 +31,8 @@ private Auditor(VirtualizedCluster cluster, VirtualizedCluster clusterAsync)
3631 public List < Elasticsearch . Net . Audit > AuditTrail { get ; set ; }
3732 public Func < VirtualizedCluster > Cluster { get ; set ; }
3833
39- public IResponse Response { get ; internal set ; }
40- public IResponse ResponseAsync { get ; internal set ; }
34+ public IElasticsearchResponse Response { get ; internal set ; }
35+ public IElasticsearchResponse ResponseAsync { get ; internal set ; }
4136
4237 private bool StartedUp { get ; }
4338
@@ -66,7 +61,7 @@ public async Task<Auditor> TraceStartup(ClientCall callTrace = null)
6661 _clusterAsync = _clusterAsync ?? Cluster ( ) ;
6762 if ( ! StartedUp ) AssertPoolBeforeStartup ? . Invoke ( _clusterAsync . ConnectionPool ) ;
6863 AssertPoolBeforeCall ? . Invoke ( _clusterAsync . ConnectionPool ) ;
69- ResponseAsync = await _clusterAsync . ClientCallAsync ( callTrace ? . RequestOverrides ) ;
64+ ResponseAsync = await _clusterAsync . ClientCallAsync ( callTrace ? . RequestOverrides ) . ConfigureAwait ( false ) ;
7065 AsyncAuditTrail = ResponseAsync . ApiCall . AuditTrail ;
7166 if ( ! StartedUp ) AssertPoolAfterStartup ? . Invoke ( _clusterAsync . ConnectionPool ) ;
7267 AssertPoolAfterCall ? . Invoke ( _clusterAsync . ConnectionPool ) ;
@@ -75,7 +70,7 @@ public async Task<Auditor> TraceStartup(ClientCall callTrace = null)
7570
7671 public async Task < Auditor > TraceCall ( ClientCall callTrace , int nthCall = 0 )
7772 {
78- await TraceStartup ( callTrace ) ;
73+ await TraceStartup ( callTrace ) . ConfigureAwait ( false ) ;
7974 return AssertAuditTrails ( callTrace , nthCall ) ;
8075 }
8176
@@ -89,18 +84,16 @@ private async Task TraceException<TException>(ClientCall callTrace, Action<TExce
8984 AssertPoolBeforeCall ? . Invoke ( _cluster . ConnectionPool ) ;
9085
9186 Action call = ( ) => _cluster . ClientCall ( callTrace ? . RequestOverrides ) ;
92- var exception = call . Should ( ) . ThrowExactly < TException > ( )
93- . Subject . First ( ) ;
87+ var exception = TryCall ( call , assert ) ;
9488 assert ( exception ) ;
9589
9690 AuditTrail = exception . AuditTrail ;
9791 AssertPoolAfterCall ? . Invoke ( _cluster . ConnectionPool ) ;
9892
9993 _clusterAsync = _clusterAsync ?? Cluster ( ) ;
10094 _clusterAsync . ClientThrows ( true ) ;
101- Func < Task > callAsync = async ( ) => await _clusterAsync . ClientCallAsync ( callTrace ? . RequestOverrides ) ;
102- exception = callAsync . Should ( ) . ThrowExactly < TException > ( )
103- . Subject . First ( ) ;
95+ Func < Task > callAsync = ( ) => _clusterAsync . ClientCallAsync ( callTrace ? . RequestOverrides ) ;
96+ exception = await TryCallAsync ( callAsync , assert ) . ConfigureAwait ( false ) ;
10497 assert ( exception ) ;
10598
10699 AsyncAuditTrail = exception . AuditTrail ;
@@ -109,14 +102,14 @@ private async Task TraceException<TException>(ClientCall callTrace, Action<TExce
109102
110103 public async Task < Auditor > TraceElasticsearchException ( ClientCall callTrace , Action < ElasticsearchClientException > assert )
111104 {
112- await TraceException ( callTrace , assert ) ;
105+ await TraceException ( callTrace , assert ) . ConfigureAwait ( false ) ;
113106 var audit = new Auditor ( _cluster , _clusterAsync ) ;
114- return await audit . TraceElasticsearchExceptionOnResponse ( callTrace , assert ) ;
107+ return await audit . TraceElasticsearchExceptionOnResponse ( callTrace , assert ) . ConfigureAwait ( false ) ;
115108 }
116109
117110 public async Task < Auditor > TraceUnexpectedElasticsearchException ( ClientCall callTrace , Action < UnexpectedElasticsearchClientException > assert )
118111 {
119- await TraceException ( callTrace , assert ) ;
112+ await TraceException ( callTrace , assert ) . ConfigureAwait ( false ) ;
120113 return new Auditor ( _cluster , _clusterAsync ) ;
121114 }
122115
@@ -129,23 +122,24 @@ public async Task<Auditor> TraceElasticsearchExceptionOnResponse(ClientCall call
129122 AssertPoolBeforeCall ? . Invoke ( _cluster . ConnectionPool ) ;
130123
131124 Action call = ( ) => { Response = _cluster . ClientCall ( callTrace ? . RequestOverrides ) ; } ;
132- call . Should ( ) . NotThrow ( ) ;
133-
134- Response . ShouldNotBeValid ( ) ;
125+ call ( ) ;
126+
127+ if ( Response . ApiCall . Success ) throw new Exception ( "Expected call to not be valid" ) ;
128+
135129 var exception = Response . ApiCall . OriginalException as ElasticsearchClientException ;
136- exception . Should ( ) . NotBeNull ( "OriginalException on response is not expected ElasticsearchClientException" ) ;
130+ if ( exception == null ) throw new Exception ( "OriginalException on response is not expected ElasticsearchClientException" ) ;
137131 assert ( exception ) ;
138132
139133 AuditTrail = exception . AuditTrail ;
140134 AssertPoolAfterCall ? . Invoke ( _cluster . ConnectionPool ) ;
141135
142136 _clusterAsync = _clusterAsync ?? Cluster ( ) ;
143137 _clusterAsync . ClientThrows ( false ) ;
144- Func < Task > callAsync = async ( ) => { ResponseAsync = await _clusterAsync . ClientCallAsync ( callTrace ? . RequestOverrides ) ; } ;
145- callAsync . Should ( ) . NotThrow ( ) ;
146- ResponseAsync . ShouldNotBeValid ( ) ;
138+ Func < Task > callAsync = async ( ) => { ResponseAsync = await _clusterAsync . ClientCallAsync ( callTrace ? . RequestOverrides ) . ConfigureAwait ( false ) ; } ;
139+ await callAsync ( ) . ConfigureAwait ( false ) ;
140+ if ( Response . ApiCall . Success ) throw new Exception ( "Expected call to not be valid" ) ;
147141 exception = ResponseAsync . ApiCall . OriginalException as ElasticsearchClientException ;
148- exception . Should ( ) . NotBeNull ( "OriginalException on response is not expected ElasticsearchClientException" ) ;
142+ if ( exception == null ) throw new Exception ( "OriginalException on response is not expected ElasticsearchClientException" ) ;
149143 assert ( exception ) ;
150144
151145 AsyncAuditTrail = exception . AuditTrail ;
@@ -163,17 +157,15 @@ public async Task<Auditor> TraceUnexpectedException(ClientCall callTrace, Action
163157 AssertPoolBeforeCall ? . Invoke ( _cluster . ConnectionPool ) ;
164158
165159 Action call = ( ) => _cluster . ClientCall ( callTrace ? . RequestOverrides ) ;
166- var exception = call . Should ( ) . ThrowExactly < UnexpectedElasticsearchClientException > ( )
167- . Subject . First ( ) ;
160+ var exception = TryCall ( call , assert ) ;
168161 assert ( exception ) ;
169162
170163 AuditTrail = exception . AuditTrail ;
171164 AssertPoolAfterCall ? . Invoke ( _cluster . ConnectionPool ) ;
172165
173166 _clusterAsync = _clusterAsync ?? Cluster ( ) ;
174- Func < Task > callAsync = async ( ) => await _clusterAsync . ClientCallAsync ( callTrace ? . RequestOverrides ) ;
175- exception = callAsync . Should ( ) . ThrowExactly < UnexpectedElasticsearchClientException > ( )
176- . Subject . First ( ) ;
167+ Func < Task > callAsync = ( ) => _clusterAsync . ClientCallAsync ( callTrace ? . RequestOverrides ) ;
168+ exception = await TryCallAsync ( callAsync , assert ) . ConfigureAwait ( false ) ;
177169 assert ( exception ) ;
178170
179171 AsyncAuditTrail = exception . AuditTrail ;
@@ -183,10 +175,9 @@ public async Task<Auditor> TraceUnexpectedException(ClientCall callTrace, Action
183175
184176 private Auditor AssertAuditTrails ( ClientCall callTrace , int nthCall )
185177 {
186- AuditTrail . Count . Should ( )
187- . Be ( AsyncAuditTrail . Count ,
188- $ "{ nthCall } has a mismatch between sync and async. \r \n async:{ AuditTrail } \r \n sync:{ AsyncAuditTrail } ") ;
189-
178+ if ( AuditTrail . Count != AsyncAuditTrail . Count )
179+ throw new Exception ( $ "{ nthCall } has a mismatch between sync and async. \r \n async:{ AuditTrail } \r \n sync:{ AsyncAuditTrail } ") ;
180+
190181 AssertTrailOnResponse ( callTrace , AuditTrail , true , nthCall ) ;
191182 AssertTrailOnResponse ( callTrace , AuditTrail , false , nthCall ) ;
192183
@@ -223,7 +214,7 @@ private static string AuditTrailToString(List<Elasticsearch.Net.Audit> auditTrai
223214 public async Task < Auditor > TraceCalls ( params ClientCall [ ] audits )
224215 {
225216 var auditor = this ;
226- foreach ( var a in audits . Select ( ( a , i ) => new { a , i } ) ) auditor = await auditor . TraceCall ( a . a , a . i ) ;
217+ foreach ( var a in audits . Select ( ( a , i ) => new { a , i } ) ) auditor = await auditor . TraceCall ( a . a , a . i ) . ConfigureAwait ( false ) ;
227218 return auditor ;
228219 }
229220
@@ -235,11 +226,11 @@ private static void AssertTrailOnResponse(ClientCall callTrace, List<Elasticsear
235226 var actualAuditTrail = auditTrail . Aggregate ( new StringBuilder ( Environment . NewLine ) ,
236227 ( sb , a ) => sb . AppendLine ( $ "-> { a } ") ,
237228 sb => sb . ToString ( ) ) ;
238-
239- callTrace . Select ( c => c . Event )
240- . Should ( )
241- . ContainInOrder ( auditTrail . Select ( a => a . Event ) ,
242- $ "the { nthClientCall } client call's { typeOfTrail } should assert ALL audit trail items{ actualAuditTrail } ") ;
229+
230+ var traceEvents = callTrace . Select ( c => c . Event ) . ToList ( ) ;
231+ var auditEvents = auditTrail . Select ( a => a . Event ) . ToList ( ) ;
232+ if ( ! traceEvents . SequenceEqual ( auditEvents ) )
233+ throw new Exception ( $ "the { nthClientCall } client call's { typeOfTrail } should assert ALL audit trail items{ actualAuditTrail } ") ;
243234
244235 foreach ( var t in auditTrail . Select ( ( a , i ) => new { a , i } ) )
245236 {
@@ -248,13 +239,51 @@ private static void AssertTrailOnResponse(ClientCall callTrace, List<Elasticsear
248239 var nthAuditTrailItem = ( i + 1 ) . ToOrdinal ( ) ;
249240 var because = $ "thats the {{0}} specified on the { nthAuditTrailItem } item in the { nthClientCall } client call's { typeOfTrail } ";
250241 var c = callTrace [ i ] ;
251- audit . Event . Should ( ) . Be ( c . Event , string . Format ( because , "event" ) ) ;
252- if ( c . Port . HasValue ) audit . Node . Uri . Port . Should ( ) . Be ( c . Port . Value , string . Format ( because , "port" ) ) ;
242+ if ( audit . Event != c . Event )
243+ throw new Exception ( string . Format ( because , "event" ) ) ;
244+ if ( c . Port . HasValue && audit . Node . Uri . Port != c . Port . Value )
245+ throw new Exception ( string . Format ( because , "port" ) ) ;
246+
253247 c . SimpleAssert ? . Invoke ( audit ) ;
254248 c . AssertWithBecause ? . Invoke ( string . Format ( because , "custom assertion" ) , audit ) ;
255249 }
256250
257- callTrace . Count . Should ( ) . Be ( auditTrail . Count , $ "actual auditTrail { actualAuditTrail } ") ;
251+ if ( callTrace . Count != auditTrail . Count )
252+ throw new Exception ( $ "callTrace has { callTrace . Count } items. Actual auditTrail { actualAuditTrail } ") ;
258253 }
254+
255+ private static TException TryCall < TException > ( Action call , Action < TException > assert ) where TException : ElasticsearchClientException
256+ {
257+ TException exception = null ;
258+ try
259+ {
260+ call ( ) ;
261+ }
262+ catch ( TException ex )
263+ {
264+ exception = ex ;
265+ assert ( ex ) ;
266+ }
267+ if ( exception is null ) throw new Exception ( "No exception happened while one was expected" ) ;
268+
269+ return exception ;
270+ }
271+ private static async Task < TException > TryCallAsync < TException > ( Func < Task > call , Action < TException > assert ) where TException : ElasticsearchClientException
272+ {
273+ TException exception = null ;
274+ try
275+ {
276+ await call ( ) . ConfigureAwait ( false ) ;
277+ }
278+ catch ( TException ex )
279+ {
280+ exception = ex ;
281+ assert ( ex ) ;
282+ }
283+ if ( exception is null ) throw new Exception ( "No exception happened while one was expected" ) ;
284+
285+ return exception ;
286+ }
287+
259288 }
260289}
0 commit comments