1515import org .junit .jupiter .params .provider .CsvSource ;
1616import software .amazon .awssdk .auth .credentials .DefaultCredentialsProvider ;
1717import software .amazon .awssdk .core .SdkBytes ;
18+ import software .amazon .awssdk .regions .Region ;
1819import software .amazon .awssdk .services .lambda .LambdaClient ;
1920import software .amazon .awssdk .services .lambda .model .OperationStatus ;
2021import software .amazon .awssdk .services .sts .StsClient ;
@@ -29,6 +30,7 @@ class CloudBasedIntegrationTest {
2930 private static String account ;
3031 private static String region ;
3132 private static String functionNameSuffix ;
33+ private static LambdaClient lambdaClient ;
3234
3335 static boolean isEnabled () {
3436 var enabled = "true" .equals (System .getProperty ("test.cloud.enabled" ));
@@ -51,12 +53,18 @@ static void setup() {
5153 functionNameSuffix = System .getProperty ("test.function.name.suffix" , "" );
5254
5355 if (account == null || region == null ) {
54- var sts = StsClient .create ();
55- if (account == null ) account = sts .getCallerIdentity ().account ();
56- if (region == null )
57- region = sts .serviceClientConfiguration ().region ().id ();
56+ try (var sts = StsClient .create ()) {
57+ if (account == null ) account = sts .getCallerIdentity ().account ();
58+ if (region == null )
59+ region = sts .serviceClientConfiguration ().region ().id ();
60+ }
5861 }
5962
63+ lambdaClient = LambdaClient .builder ()
64+ .credentialsProvider (DefaultCredentialsProvider .builder ().build ())
65+ .region (Region .of (region ))
66+ .build ();
67+
6068 System .out .println ("☁️ Running cloud integration tests against account " + account + " in " + region );
6169 }
6270
@@ -68,7 +76,7 @@ private static String arn(String functionName) {
6876 @ Test
6977 void testSimpleStepExample () {
7078 var runner = CloudDurableTestRunner .create (
71- arn ("simple-step-example" ), new TypeToken <Map <String , String >>() {}, get (String .class ));
79+ arn ("simple-step-example" ), new TypeToken <Map <String , String >>() {}, get (String .class ), lambdaClient );
7280 var result = runner .run (Map .of ("message" , "test" ));
7381
7482 assertEquals (ExecutionStatus .SUCCEEDED , result .getStatus ());
@@ -82,7 +90,7 @@ void testSimpleStepExample() {
8290 @ Test
8391 void testNoopExampleWithLargeInput () {
8492 var runner = CloudDurableTestRunner .create (
85- arn ("noop-example" ), new TypeToken <Map <String , String >>() {}, get (String .class ));
93+ arn ("noop-example" ), new TypeToken <Map <String , String >>() {}, get (String .class ), lambdaClient );
8694 // 6MB large input
8795 var largeInput = "A" .repeat (1024 * 1024 * 6 - 12 );
8896 var result = runner .run (Map .of ("name" , largeInput ));
@@ -94,7 +102,7 @@ void testNoopExampleWithLargeInput() {
94102 @ Test
95103 void testSimpleInvokeExample () {
96104 var runner = CloudDurableTestRunner .create (
97- arn ("simple-invoke-example" ), new TypeToken <Map <String , String >>() {}, get (String .class ));
105+ arn ("simple-invoke-example" ), new TypeToken <Map <String , String >>() {}, get (String .class ), lambdaClient );
98106 var result = runner .run (Map .of ("name" , functionNameSuffix ));
99107
100108 assertEquals (ExecutionStatus .SUCCEEDED , result .getStatus ());
@@ -111,7 +119,7 @@ void testSimpleInvokeExample() {
111119
112120 @ Test
113121 void testRetryExample () {
114- var runner = CloudDurableTestRunner .create (arn ("retry-example" ), String .class , String .class );
122+ var runner = CloudDurableTestRunner .create (arn ("retry-example" ), String .class , String .class , lambdaClient );
115123 var result = runner .run ("{}" );
116124
117125 assertEquals (ExecutionStatus .SUCCEEDED , result .getStatus ());
@@ -131,7 +139,8 @@ void testRetryExample() {
131139
132140 @ Test
133141 void testRetryInProcessExample () {
134- var runner = CloudDurableTestRunner .create (arn ("retry-in-process-example" ), String .class , String .class );
142+ var runner = CloudDurableTestRunner .create (
143+ arn ("retry-in-process-example" ), String .class , String .class , lambdaClient );
135144 var result = runner .run ("{}" );
136145
137146 assertEquals (ExecutionStatus .SUCCEEDED , result .getStatus ());
@@ -153,7 +162,8 @@ void testRetryInProcessExample() {
153162
154163 @ Test
155164 void testWaitExample () {
156- var runner = CloudDurableTestRunner .create (arn ("wait-example" ), GreetingRequest .class , String .class );
165+ var runner =
166+ CloudDurableTestRunner .create (arn ("wait-example" ), GreetingRequest .class , String .class , lambdaClient );
157167 var result = runner .run (new GreetingRequest ("TestUser" ));
158168
159169 assertEquals (ExecutionStatus .SUCCEEDED , result .getStatus ());
@@ -171,7 +181,8 @@ void testWaitExample() {
171181
172182 @ Test
173183 void testWaitAtLeastExample () {
174- var runner = CloudDurableTestRunner .create (arn ("wait-at-least-example" ), GreetingRequest .class , String .class );
184+ var runner = CloudDurableTestRunner .create (
185+ arn ("wait-at-least-example" ), GreetingRequest .class , String .class , lambdaClient );
175186 var result = runner .run (new GreetingRequest ("TestUser" ));
176187
177188 assertEquals (ExecutionStatus .SUCCEEDED , result .getStatus ());
@@ -188,7 +199,7 @@ void testWaitAtLeastExample() {
188199 @ Test
189200 void testWaitAtLeastInProcessExample () {
190201 var runner = CloudDurableTestRunner .create (
191- arn ("wait-at-least-in-process-example" ), GreetingRequest .class , String .class );
202+ arn ("wait-at-least-in-process-example" ), GreetingRequest .class , String .class , lambdaClient );
192203 var result = runner .run (new GreetingRequest ("TestUser" ));
193204
194205 assertEquals (ExecutionStatus .SUCCEEDED , result .getStatus ());
@@ -205,7 +216,10 @@ void testWaitAtLeastInProcessExample() {
205216 @ Test
206217 void testGenericTypesExample () {
207218 var runner = CloudDurableTestRunner .create (
208- arn ("generic-types-example" ), GenericTypesExample .Input .class , GenericTypesExample .Output .class );
219+ arn ("generic-types-example" ),
220+ GenericTypesExample .Input .class ,
221+ GenericTypesExample .Output .class ,
222+ lambdaClient );
209223 var result = runner .run (new GenericTypesExample .Input ("user123" ));
210224
211225 assertEquals (ExecutionStatus .SUCCEEDED , result .getStatus ());
@@ -244,7 +258,8 @@ void testGenericInputOutputExample() {
244258 final TypeToken <Map <String , Map <String , List <String >>>> resultType = new TypeToken <>() {};
245259 final TypeToken <Map <String , String >> inputType = new TypeToken <>() {};
246260
247- var runner = CloudDurableTestRunner .create (arn ("generic-input-output-example" ), inputType , resultType );
261+ var runner =
262+ CloudDurableTestRunner .create (arn ("generic-input-output-example" ), inputType , resultType , lambdaClient );
248263 var result = runner .run (new HashMap <>(Map .of ("userId" , "user123" )));
249264
250265 assertEquals (ExecutionStatus .SUCCEEDED , result .getStatus ());
@@ -266,7 +281,8 @@ void testGenericInputOutputExample() {
266281
267282 @ Test
268283 void testCustomConfigExample () {
269- var runner = CloudDurableTestRunner .create (arn ("custom-config-example" ), String .class , String .class );
284+ var runner =
285+ CloudDurableTestRunner .create (arn ("custom-config-example" ), String .class , String .class , lambdaClient );
270286 var result = runner .run ("test-input" );
271287
272288 assertEquals (ExecutionStatus .SUCCEEDED , result .getStatus ());
@@ -295,7 +311,8 @@ void testCustomConfigExample() {
295311
296312 @ Test
297313 void testErrorHandlingExample () {
298- var runner = CloudDurableTestRunner .create (arn ("error-handling-example" ), String .class , String .class );
314+ var runner =
315+ CloudDurableTestRunner .create (arn ("error-handling-example" ), String .class , String .class , lambdaClient );
299316 var result = runner .run ("test-input" );
300317
301318 assertEquals (ExecutionStatus .SUCCEEDED , result .getStatus ());
@@ -310,16 +327,16 @@ void testErrorHandlingExample() {
310327 @ Test
311328 void testCallbackExample () {
312329 // happy case covering both createCallback (approval) and waitForCallback (preapproval-callback)
313- var runner = CloudDurableTestRunner .create (arn ( "callback-example" ), ApprovalRequest . class , String . class );
314- var lambda = LambdaClient . create ( );
330+ var runner = CloudDurableTestRunner .create (
331+ arn ( "callback-example" ), ApprovalRequest . class , String . class , lambdaClient );
315332
316333 // Start async execution
317334 var execution = runner .startAsync (new ApprovalRequest ("Purchase order" , 5000.0 ));
318335
319336 // Complete the preapproval callback
320337 execution .pollUntil (exec -> exec .hasCallback ("preapproval-callback" ));
321338 var preapprovalCallbackId = execution .getCallbackId ("preapproval-callback" );
322- lambda .sendDurableExecutionCallbackSuccess (
339+ lambdaClient .sendDurableExecutionCallbackSuccess (
323340 req -> req .callbackId (preapprovalCallbackId ).result (SdkBytes .fromUtf8String ("\" preapproved\" " )));
324341
325342 // Wait for callback to appear
@@ -330,7 +347,7 @@ void testCallbackExample() {
330347 assertNotNull (callbackId );
331348
332349 // Complete the callback using AWS SDK
333- lambda .sendDurableExecutionCallbackSuccess (
350+ lambdaClient .sendDurableExecutionCallbackSuccess (
334351 req -> req .callbackId (callbackId ).result (SdkBytes .fromUtf8String ("\" approved\" " )));
335352
336353 // Wait for execution to complete
@@ -352,15 +369,15 @@ void testCallbackExample() {
352369
353370 @ Test
354371 void testCallbackExampleWithFailure () {
355- var runner = CloudDurableTestRunner .create (arn ( "callback-example" ), ApprovalRequest . class , String . class );
356- var lambda = LambdaClient . create ( );
372+ var runner = CloudDurableTestRunner .create (
373+ arn ( "callback-example" ), ApprovalRequest . class , String . class , lambdaClient );
357374
358375 // Start async execution
359376 var execution = runner .startAsync (new ApprovalRequest ("Purchase order" , 5000.0 ));
360377
361378 execution .pollUntil (exec -> exec .hasCallback ("preapproval-callback" ));
362379 var preapprovalCallbackId = execution .getCallbackId ("preapproval-callback" );
363- lambda .sendDurableExecutionCallbackSuccess (
380+ lambdaClient .sendDurableExecutionCallbackSuccess (
364381 req -> req .callbackId (preapprovalCallbackId ).result (SdkBytes .fromUtf8String ("\" preapproved\" " )));
365382
366383 // Wait for callback to appear
@@ -371,7 +388,7 @@ void testCallbackExampleWithFailure() {
371388 assertNotNull (callbackId );
372389
373390 // Fail the callback using AWS SDK
374- lambda .sendDurableExecutionCallbackFailure (req -> req .callbackId (callbackId )
391+ lambdaClient .sendDurableExecutionCallbackFailure (req -> req .callbackId (callbackId )
375392 .error (err -> err .errorType ("ApprovalRejected" ).errorMessage ("Approval rejected by manager" )));
376393
377394 // Wait for execution to complete
@@ -390,15 +407,15 @@ void testCallbackExampleWithFailure() {
390407
391408 @ Test
392409 void testCallbackExampleWithTimeout () {
393- var runner = CloudDurableTestRunner .create (arn ( "callback-example" ), ApprovalRequest . class , String . class );
394- var lambda = LambdaClient . create ( );
410+ var runner = CloudDurableTestRunner .create (
411+ arn ( "callback-example" ), ApprovalRequest . class , String . class , lambdaClient );
395412
396413 // Start async execution with 10 second timeout
397414 var execution = runner .startAsync (new ApprovalRequest ("Purchase order" , 5000.0 , 10 ));
398415
399416 execution .pollUntil (exec -> exec .hasCallback ("preapproval-callback" ));
400417 var preapprovalCallbackId = execution .getCallbackId ("preapproval-callback" );
401- lambda .sendDurableExecutionCallbackSuccess (
418+ lambdaClient .sendDurableExecutionCallbackSuccess (
402419 req -> req .callbackId (preapprovalCallbackId ).result (SdkBytes .fromUtf8String ("\" preapproved\" " )));
403420
404421 // Wait for callback to appear
@@ -421,15 +438,15 @@ void testCallbackExampleWithTimeout() {
421438 @ Test
422439 void testCallbackExampleWithWaitForCallbackFailure () {
423440 // fail the waitForCallback (preapproval-callback) callback
424- var runner = CloudDurableTestRunner .create (arn ( "callback-example" ), ApprovalRequest . class , String . class );
425- var lambda = LambdaClient . create ( );
441+ var runner = CloudDurableTestRunner .create (
442+ arn ( "callback-example" ), ApprovalRequest . class , String . class , lambdaClient );
426443
427444 // Start async execution with 10 second timeout
428445 var execution = runner .startAsync (new ApprovalRequest ("Purchase order" , 5000.0 , 10 ));
429446
430447 execution .pollUntil (exec -> exec .hasCallback ("preapproval-callback" ));
431448 var preapprovalCallbackId = execution .getCallbackId ("preapproval-callback" );
432- lambda .sendDurableExecutionCallbackFailure (
449+ lambdaClient .sendDurableExecutionCallbackFailure (
433450 req -> req .callbackId (preapprovalCallbackId ).error (err -> err .errorMessage ("preapproval denied" )));
434451
435452 // Wait for callback to appear
@@ -451,7 +468,8 @@ void testCallbackExampleWithWaitForCallbackFailure() {
451468
452469 @ Test
453470 void testChildContextExample () {
454- var runner = CloudDurableTestRunner .create (arn ("child-context-example" ), GreetingRequest .class , String .class );
471+ var runner = CloudDurableTestRunner .create (
472+ arn ("child-context-example" ), GreetingRequest .class , String .class , lambdaClient );
455473 var result = runner .run (new GreetingRequest ("Alice" ));
456474
457475 assertEquals (ExecutionStatus .SUCCEEDED , result .getStatus ());
@@ -474,7 +492,8 @@ void testManyAsyncStepsExample(int steps, long maxExecutionTime, long maxReplayT
474492 var runner = CloudDurableTestRunner .create (
475493 arn ("many-async-steps-example" ),
476494 ManyAsyncStepsExample .Input .class ,
477- ManyAsyncStepsExample .Output .class );
495+ ManyAsyncStepsExample .Output .class ,
496+ lambdaClient );
478497 var result = runner .run (new ManyAsyncStepsExample .Input (2 , steps ));
479498
480499 assertEquals (ExecutionStatus .SUCCEEDED , result .getStatus ());
@@ -511,7 +530,8 @@ void testManyAsyncChildContextExample(int steps, long maxExecutionTime, long max
511530 var runner = CloudDurableTestRunner .create (
512531 arn ("many-async-child-context-example" ),
513532 ManyAsyncChildContextExample .Input .class ,
514- ManyAsyncChildContextExample .Output .class );
533+ ManyAsyncChildContextExample .Output .class ,
534+ lambdaClient );
515535 var result = runner .run (new ManyAsyncChildContextExample .Input (2 , steps ));
516536
517537 assertEquals (ExecutionStatus .SUCCEEDED , result .getStatus ());
@@ -542,7 +562,8 @@ void testManyAsyncChildContextExample(int steps, long maxExecutionTime, long max
542562
543563 @ Test
544564 void testSimpleMapExample () {
545- var runner = CloudDurableTestRunner .create (arn ("simple-map-example" ), GreetingRequest .class , String .class );
565+ var runner = CloudDurableTestRunner .create (
566+ arn ("simple-map-example" ), GreetingRequest .class , String .class , lambdaClient );
546567 var result = runner .run (new GreetingRequest ("Alice" ));
547568
548569 assertEquals (ExecutionStatus .SUCCEEDED , result .getStatus ());
@@ -551,8 +572,8 @@ void testSimpleMapExample() {
551572
552573 @ Test
553574 void testMapErrorHandlingExample () {
554- var runner =
555- CloudDurableTestRunner . create ( arn ("map-error-handling-example" ), GreetingRequest .class , String .class );
575+ var runner = CloudDurableTestRunner . create (
576+ arn ("map-error-handling-example" ), GreetingRequest .class , String .class , lambdaClient );
556577 var result = runner .run (new GreetingRequest ("Alice" ));
557578
558579 assertEquals (ExecutionStatus .SUCCEEDED , result .getStatus ());
@@ -569,7 +590,8 @@ void testMapErrorHandlingExample() {
569590
570591 @ Test
571592 void testMapConfigExample () {
572- var runner = CloudDurableTestRunner .create (arn ("map-config-example" ), GreetingRequest .class , String .class );
593+ var runner = CloudDurableTestRunner .create (
594+ arn ("map-config-example" ), GreetingRequest .class , String .class , lambdaClient );
573595 var result = runner .run (new GreetingRequest ("Alice" ));
574596
575597 assertEquals (ExecutionStatus .SUCCEEDED , result .getStatus ());
0 commit comments