@@ -49,9 +49,12 @@ namespace CommandQuery.Sample.AWSLambda;
49
49
50
50
public class Command (ICommandFunction commandFunction )
51
51
{
52
- [LambdaFunction ]
52
+ [LambdaFunction ( Policies = " AWSLambdaBasicExecutionRole " , MemorySize = 256 , Timeout = 30 ) ]
53
53
[RestApi (LambdaHttpMethod .Post , " /command/{commandName}" )]
54
- public async Task <APIGatewayProxyResponse > Handle (APIGatewayProxyRequest request , ILambdaContext context , string commandName ) =>
54
+ public async Task <APIGatewayProxyResponse > Post (
55
+ APIGatewayProxyRequest request ,
56
+ ILambdaContext context ,
57
+ string commandName ) =>
55
58
await commandFunction .HandleAsync (commandName , request , context .Logger );
56
59
}
57
60
```
@@ -79,14 +82,20 @@ namespace CommandQuery.Sample.AWSLambda
79
82
{
80
83
public class Query (IQueryFunction queryFunction )
81
84
{
82
- [LambdaFunction ]
85
+ [LambdaFunction ( Policies = " AWSLambdaBasicExecutionRole " , MemorySize = 256 , Timeout = 30 ) ]
83
86
[RestApi (LambdaHttpMethod .Get , " /query/{queryName}" )]
84
- public async Task <APIGatewayProxyResponse > Get (APIGatewayProxyRequest request , ILambdaContext context , string queryName ) =>
87
+ public async Task <APIGatewayProxyResponse > Get (
88
+ APIGatewayProxyRequest request ,
89
+ ILambdaContext context ,
90
+ string queryName ) =>
85
91
await queryFunction .HandleAsync (queryName , request , context .Logger );
86
92
87
- [LambdaFunction ]
93
+ [LambdaFunction ( Policies = " AWSLambdaBasicExecutionRole " , MemorySize = 256 , Timeout = 30 ) ]
88
94
[RestApi (LambdaHttpMethod .Post , " /query/{queryName}" )]
89
- public async Task <APIGatewayProxyResponse > Post (APIGatewayProxyRequest request , ILambdaContext context , string queryName ) =>
95
+ public async Task <APIGatewayProxyResponse > Post (
96
+ APIGatewayProxyRequest request ,
97
+ ILambdaContext context ,
98
+ string queryName ) =>
90
99
await queryFunction .HandleAsync (queryName , request , context .Logger );
91
100
}
92
101
}
@@ -153,7 +162,7 @@ Configuration in `serverless.template`:
153
162
"Transform" : " AWS::Serverless-2016-10-31" ,
154
163
"Description" : " An AWS Serverless Application. This template is partially managed by Amazon.Lambda.Annotations (v1.5.0.0)." ,
155
164
"Resources" : {
156
- "CommandQuerySampleAWSLambdaCommandHandleGenerated " : {
165
+ "CommandQuerySampleAWSLambdaCommandPostGenerated " : {
157
166
"Type" : " AWS::Serverless::Function" ,
158
167
"Metadata" : {
159
168
"Tool" : " Amazon.Lambda.Annotations" ,
@@ -171,7 +180,7 @@ Configuration in `serverless.template`:
171
180
"Architectures" : [
172
181
" x86_64"
173
182
],
174
- "Handler" : " CommandQuery.Sample.AWSLambda::CommandQuery.Sample.AWSLambda.Command_Handle_Generated::Handle " ,
183
+ "Handler" : " CommandQuery.Sample.AWSLambda::CommandQuery.Sample.AWSLambda.Command_Post_Generated::Post " ,
175
184
"Runtime" : " dotnet8" ,
176
185
"CodeUri" : " ." ,
177
186
"MemorySize" : 256 ,
@@ -284,104 +293,48 @@ Configuration in `serverless.template`:
284
293
You can [ test] ( https://github.com/aws/aws-lambda-dotnet/blob/master/Libraries/src/Amazon.Lambda.TestUtilities/README.md ) your lambdas with the [ Amazon.Lambda.TestUtilities] ( https://www.nuget.org/packages/Amazon.Lambda.TestUtilities ) package.
285
294
286
295
``` cs
296
+ using System .Text .Json ;
287
297
using Amazon .Lambda .APIGatewayEvents ;
288
- using Amazon .Lambda .Core ;
289
298
using Amazon .Lambda .TestUtilities ;
290
299
using CommandQuery .AWSLambda ;
291
- using CommandQuery .Sample .Contracts .Queries ;
300
+ using CommandQuery .Sample .Contracts .Commands ;
292
301
using FluentAssertions ;
293
302
using Microsoft .Extensions .DependencyInjection ;
294
303
using NUnit .Framework ;
295
304
296
305
namespace CommandQuery .Sample .AWSLambda .Tests
297
306
{
298
- public class QueryTests
307
+ public class CommandTests
299
308
{
300
- public class when_using_the_real_function_via_Post
309
+ [SetUp ]
310
+ public void SetUp ()
301
311
{
302
- [SetUp ]
303
- public void SetUp ()
304
- {
305
- var serviceCollection = new ServiceCollection ();
306
- new Startup ().ConfigureServices (serviceCollection );
307
- var serviceProvider = serviceCollection .BuildServiceProvider ();
308
-
309
- Subject = new Query (serviceProvider .GetRequiredService <IQueryFunction >());
310
- Request = GetRequest (" POST" , content : " { \" Id\" : 1 }" );
311
- Context = new TestLambdaContext ();
312
- }
313
-
314
- [Test ]
315
- public async Task should_work ()
316
- {
317
- var result = await Subject .Post (Request , Context , " BarQuery" );
318
- var value = result .As <Bar >()! ;
319
-
320
- value .Id .Should ().Be (1 );
321
- value .Value .Should ().NotBeEmpty ();
322
- }
323
-
324
- [Test ]
325
- public async Task should_handle_errors ()
326
- {
327
- var result = await Subject .Post (Request , Context , " FailQuery" );
328
-
329
- result .ShouldBeError (" The query type 'FailQuery' could not be found" );
330
- }
312
+ var serviceCollection = new ServiceCollection ();
313
+ new Startup ().ConfigureServices (serviceCollection );
314
+ var serviceProvider = serviceCollection .BuildServiceProvider ();
331
315
332
- Query Subject = null ! ;
333
- APIGatewayProxyRequest Request = null ! ;
334
- ILambdaContext Context = null ! ;
316
+ Subject = new Command (serviceProvider .GetRequiredService <ICommandFunction >());
317
+ Context = new TestLambdaContext ();
335
318
}
336
319
337
- public class when_using_the_real_function_via_Get
320
+ [Test ]
321
+ public async Task should_handle_command ()
338
322
{
339
- [SetUp ]
340
- public void SetUp ()
341
- {
342
- var serviceCollection = new ServiceCollection ();
343
- new Startup ().ConfigureServices (serviceCollection );
344
- var serviceProvider = serviceCollection .BuildServiceProvider ();
345
-
346
- Subject = new Query (serviceProvider .GetRequiredService <IQueryFunction >());
347
- Request = GetRequest (" GET" , query : new Dictionary <string , IList <string >> { { " Id" , new List <string > { " 1" } } });
348
- Context = new TestLambdaContext ();
349
- }
350
-
351
- [Test ]
352
- public async Task should_work ()
353
- {
354
- var result = await Subject .Get (Request , Context , " BarQuery" );
355
- var value = result .As <Bar >()! ;
356
-
357
- value .Id .Should ().Be (1 );
358
- value .Value .Should ().NotBeEmpty ();
359
- }
360
-
361
- [Test ]
362
- public async Task should_handle_errors ()
363
- {
364
- var result = await Subject .Get (Request , Context , " FailQuery" );
365
-
366
- result .ShouldBeError (" The query type 'FailQuery' could not be found" );
367
- }
368
-
369
- Query Subject = null ! ;
370
- APIGatewayProxyRequest Request = null ! ;
371
- ILambdaContext Context = null ! ;
323
+ var response = await Subject .Post (GetRequest (new FooCommand { Value = " Foo" }), Context , " FooCommand" );
324
+ response .StatusCode .Should ().Be (200 );
372
325
}
373
326
374
- static APIGatewayProxyRequest GetRequest (string method , string ? content = null , Dictionary <string , IList <string >>? query = null )
327
+ [Test ]
328
+ public async Task should_handle_errors ()
375
329
{
376
- var request = new APIGatewayProxyRequest
377
- {
378
- HttpMethod = method ,
379
- Body = content ,
380
- MultiValueQueryStringParameters = query
381
- };
382
-
383
- return request ;
330
+ var response = await Subject .Post (GetRequest (new FooCommand ()), Context , " FooCommand" );
331
+ response .ShouldBeError (" Value cannot be null or empty" );
384
332
}
333
+
334
+ static APIGatewayProxyRequest GetRequest (object body ) => new () { Body = JsonSerializer .Serialize (body ) };
335
+
336
+ Command Subject = null ! ;
337
+ TestLambdaContext Context = null ! ;
385
338
}
386
339
}
387
340
```
0 commit comments