24
24
import java .util .List ;
25
25
import java .util .Map ;
26
26
import java .util .Set ;
27
+ import java .util .function .Consumer ;
27
28
import lombok .Cleanup ;
28
29
import org .junit .jupiter .api .AfterEach ;
29
30
import org .junit .jupiter .api .BeforeAll ;
@@ -192,6 +193,27 @@ public static <T extends StripeObjectInterface> void verifyRequest(
192
193
RequestOptions options )
193
194
throws StripeException {
194
195
196
+ verifyRequest (
197
+ (req ) -> {
198
+ assertEquals (baseAddress , req .getBaseAddress ());
199
+ assertEquals (method , req .getMethod ());
200
+ assertEquals (path , req .getPath ());
201
+ if (params != null ) {
202
+ final String msg =
203
+ String .format (
204
+ "Params did not match - expected: %s, received: %s" , params , req .getParams ());
205
+ assertTrue (msg , compareParamObjects (params , req .getParams ()));
206
+ }
207
+ if (options != null ) {
208
+ assertEquals (options , req .getOptions ());
209
+ }
210
+ });
211
+ }
212
+
213
+ @ SuppressWarnings ("AssertionFailureIgnored" )
214
+ public static <T extends StripeObjectInterface > void verifyRequest (
215
+ Consumer <ApiRequest > assertOnApiRequest ) throws StripeException {
216
+
195
217
ArgumentCaptor <ApiRequest > requestCaptor = ArgumentCaptor .forClass (ApiRequest .class );
196
218
List <AssertionError > exceptions = new ArrayList <AssertionError >();
197
219
try {
@@ -203,19 +225,35 @@ public static <T extends StripeObjectInterface> void verifyRequest(
203
225
204
226
for (ApiRequest req : requestCaptor .getAllValues ()) {
205
227
try {
206
- assertEquals (baseAddress , req .getBaseAddress ());
207
- assertEquals (method , req .getMethod ());
208
- assertEquals (path , req .getPath ());
209
- if (params != null ) {
210
- final String msg =
211
- String .format (
212
- "Params did not match - expected: %s, received: %s" , params , req .getParams ());
213
- assertTrue (msg , compareParamObjects (params , req .getParams ()));
214
- }
215
- if (options != null ) {
216
- assertEquals (options , req .getOptions ());
217
- }
218
- // If we get here, we have found a request that triggered no assertion failures.
228
+ assertOnApiRequest .accept (req );
229
+ return ;
230
+ } catch (AssertionError e ) {
231
+ exceptions .add (e );
232
+ }
233
+ }
234
+
235
+ // If we get here, each request failed an assertion.
236
+ if (exceptions .size () != 0 ) {
237
+ // Combine all exceptions into a single message
238
+ String msg = "" ;
239
+ for (AssertionError e : exceptions ) {
240
+ msg += e .getMessage () + "\n \n " ;
241
+ }
242
+ throw new AssertionError (msg );
243
+ }
244
+ }
245
+
246
+ @ SuppressWarnings ("AssertionFailureIgnored" )
247
+ public static <T extends StripeObjectInterface > void verifyStripeRequest (
248
+ Consumer <StripeRequest > assertOnStripeRequest ) throws StripeException {
249
+
250
+ ArgumentCaptor <StripeRequest > requestCaptor = ArgumentCaptor .forClass (StripeRequest .class );
251
+ List <AssertionError > exceptions = new ArrayList <AssertionError >();
252
+ Mockito .verify (httpClientSpy , Mockito .atLeastOnce ()).request (requestCaptor .capture ());
253
+
254
+ for (StripeRequest req : requestCaptor .getAllValues ()) {
255
+ try {
256
+ assertOnStripeRequest .accept (req );
219
257
return ;
220
258
} catch (AssertionError e ) {
221
259
exceptions .add (e );
0 commit comments