Skip to content

Commit ac17e38

Browse files
authored
Merge pull request #179 from aws-samples/fix/sqs-lambda-documentation
Documentation Tweaks
2 parents 10975f8 + 99c5e6e commit ac17e38

File tree

5 files changed

+72
-28
lines changed

5 files changed

+72
-28
lines changed
92.3 KB
Loading

dotnet-test-samples/sqs-lambda/README.md

+63-19
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
1-
## Amazon Simple Queue Service (SQS) & AWS Lambda Function
1+
[![Built with: .Net 6](https://img.shields.io/badge/Microsoft-.Net%206-blue?style=plastic&logo=microsoft)](https://learn.microsoft.com/en-us/dotnet/core/introduction)
2+
[![Amazon: SQS](https://img.shields.io/badge/Amazon-SQS-blueviolet?style=plastic&logo=amazonaws)]()
3+
[![AWS: Lambda](https://img.shields.io/badge/AWS-Lambda-orange?style=plastic&logo=amazonaws)]()
4+
[![AWS: DynamoDB](https://img.shields.io/badge/Amazon-DynamoDB-darkblue?style=plastic&logo=amazonaws)]()
5+
[![test: unit](https://img.shields.io/badge/Test-Unit-blue?style=plastic&logo=)]()
6+
[![test: integration](https://img.shields.io/badge/Test-Integration-yellow?style=plastic&logo=)]()
7+
8+
## Amazon Simple Queue Service (SQS), AWS Lambda Function & Amazon DynamoDB
29

310
### Description
411

@@ -21,10 +28,47 @@ The AWS services used in this pattern are
2128
2229
## Topology
2330

24-
<img src="../docs/sqs-lambda.png" alt="topology" width="80%"/>
31+
### System Under Test (SUT)
32+
33+
The SUT in this pattern is a Lambda function invoked by the presence of messages in an SQS queue as the event source.
34+
35+
![System Under Test (SUT)](../docs/sqs-lambda-sut.png)
36+
37+
### Goal
38+
39+
This pattern is intended to perform rapid functional testing without affecting cloud resources. Testing is conducted in a self-contained local environment. The test goal is to validate that the Lambda function exhibits proper message handling with both singleton messages and batch messages.
40+
41+
### Description
42+
43+
In this pattern the Lambda function processes SQS messages without interacting directly with the SQS queue itself, instead relying on the [default visibility and deletion behaviors](https://docs.aws.amazon.com/lambda/latest/dg/with-sqs.html) of the SQS event. This project demonstrates several techniques for executing tests including running Lambda function locally with a simulated payload as well integration tests in the cloud.
44+
45+
![System Under Test Description (SUT)](../docs/sqs-lambda.png)
46+
47+
This example contains an [Amazon SQS](https://aws.amazon.com/sqs/), [AWS Lambda](https://aws.amazon.com/lambda/) and [Amazon DynamoDB](https://aws.amazon.com/dynamodb/) table core resources.
48+
49+
The AWS Lambda function in this example expects SQS Queue Event data to contain a JSON object with 6 properties:
50+
51+
```json
52+
{
53+
"employee_id": "string",
54+
"email": "string",
55+
"first_name": "string",
56+
"last_name": "string",
57+
"dob": "DateTime",
58+
"doh": "DateTime"
59+
}
60+
```
61+
62+
- `employee_id`: unique identifier for each individual record. Each record should have a unique `id` property value
63+
- `email`: email address of the employee
64+
- `first_name`: first name of the employee
65+
- `last_name`: last name of the employee
66+
- `dob`: date of birth of the employee
67+
- `doh`: date of hire of the employee
68+
69+
The AWS Lambda function converts the incoming event data into the processed record JSON, setting the `employee_id` to be the DynamoDB Partition Key.
2570

26-
## Description
27-
The SAM template contains all the information to deploy AWS resources (An Amazon SQS queue and an AWS Lambda function) and also the permissions required by these services to communicate.
71+
> The SAM template contains all the information to deploy AWS resources (An Amazon SQS queue and an AWS Lambda function) and also the permissions required by these services to communicate.
2872
2973
You will be able to create and delete the CloudFormation stack using the SAM CLI.
3074

@@ -76,13 +120,13 @@ The source code for this sample includes automated unit and integration tests. [
76120

77121
### Unit Tests
78122

79-
#### [ProcessEmployeeFunctionTests.cs](./tests/SqsEventHandler.UnitTests/ProcessEmployeeFunctionTests.cs)
123+
#### [ProcessEmployeeFunctionTests.cs](./tests/SqsEventHandler.UnitTests/Functions/ProcessEmployeeFunctionTests.cs)
80124
The goal of these tests is to run a unit test on the ProcessSqsMessage method which is called by the handler method of the Lambda function.
81125
The system under test here is completely abstracted from any cloud resources.
82126

83127
```c#
84128
[Fact]
85-
public async Task ProcessEmployeeFunction_Should_ExecuteSuccessfully()
129+
public Task ProcessEmployeeFunction_Should_ExecuteSuccessfully()
86130
{
87131
//Arrange
88132
var repository = new Mock<IDynamoDbRepository<EmployeeDto>>();
@@ -96,20 +140,21 @@ public async Task ProcessEmployeeFunction_Should_ExecuteSuccessfully()
96140
var context = new TestLambdaContext();
97141

98142
//Act
99-
var exception = await Record.ExceptionAsync(() => sut.ProcessSqsMessage(employee, context));
143+
var taskResult = sut.ProcessSqsMessage(employee, context);
100144

101145
//Assert
102-
Assert.Null(exception);
146+
Assert.True(taskResult.IsCompleted);
147+
return Task.CompletedTask;
103148
}
104149
```
105150

106-
#### [SqsEventTriggerTests.cs](./tests/SqsEventHandler.UnitTests/Triggers/SqsEventTriggerTests.cs)
151+
#### [SqsEventHandlerTests.cs](./tests/SqsEventHandler.UnitTests/Handlers/SqsEventHandlerTests.cs)
107152
The goal of these tests is to run a unit test on the SqsEventTrigger which implements the handler method of the Lambda function.
108153
It uses [Moq](https://github.com/moq/moq4) for the mocking framework. The `ProcessSqsMessage` method is mocked.
109154

110155
```c#
111156
[Fact]
112-
public async Task SqsEventTrigger_Should_CallProcessSqsMessageOnce()
157+
public async Task SqsEventHandler_Should_CallProcessSqsMessageOnce()
113158
{
114159
//Arrange
115160
var expected = new EmployeeBuilder().Build();
@@ -132,11 +177,11 @@ public async Task SqsEventTrigger_Should_CallProcessSqsMessageOnce()
132177
To execute the tests:
133178

134179
**Powershell**
135-
```powershell
180+
```shell
136181
dotnet test tests\SQSEventHandler.UnitTests\SQSEventHandler.UnitTests.csproj
137182
```
138183
**Bash**
139-
```powershell
184+
```shell
140185
dotnet test tests/SQSEventHandler.UnitTests/SQSEventHandler.UnitTests.csproj
141186
```
142187

@@ -156,9 +201,8 @@ public async Task PublishToProcessEmployeeQueue_Should_ReturnSuccess()
156201
var sqsMessage = new EmployeeBuilder().WithEmployeeId(EmployeeId);
157202

158203
//Act
159-
var response = await _setup.SendMessageAsync(
160-
_client,
161-
_setup.SqsEventQueueUrl,
204+
var response = await _processEmployeeFixture.SendMessageAsync(
205+
_processEmployeeFixture.SqsEventQueueUrl,
162206
JsonSerializer.Serialize(sqsMessage)
163207
);
164208

@@ -172,15 +216,15 @@ public async Task PublishToProcessEmployeeQueue_Should_UpsertEmployee()
172216
{
173217
//Act
174218
using var cts = new CancellationTokenSource();
175-
var response = await _setup.TestEmployeeRepository!.GetItemAsync(EmployeeId, cts.Token);
219+
var response = await _processEmployeeFixture.TestEmployeeRepository!.GetItemAsync(EmployeeId, cts.Token);
176220

177221
//Assert
178222
response.Should().NotBeNull();
179223
response!.EmployeeId.Should().Be(EmployeeId);
180224
_testOutputHelper.WriteLine(response.ToString());
181225

182226
//Dispose
183-
_setup.CreatedEmployeeIds.Add(EmployeeId);
227+
_processEmployeeFixture.CreatedEmployeeIds.Add(EmployeeId);
184228
}
185229
```
186230

@@ -189,14 +233,14 @@ public async Task PublishToProcessEmployeeQueue_Should_UpsertEmployee()
189233
To execute the tests:
190234

191235
**Powershell**
192-
```powershell
236+
```shell
193237
$env:AWS_SAM_STACK_NAME = <STACK_NAME_USED_IN_SAM_DEPLOY>
194238
$env:AWS_SAM_REGION_NAME = <REGION_NAME_USED_IN_SAM_DEPLOY>
195239
dotnet test ./tests/SqsEventHandler.IntegrationTests/SqsEventHandler.IntegrationTests.csproj
196240
```
197241

198242
**Bash**
199-
```bash
243+
```shell
200244
AWS_SAM_STACK_NAME=<STACK_NAME_USED_IN_SAM_DEPLOY>
201245
AWS_SAM_REGION_NAME=<REGION_NAME_USED_IN_SAM_DEPLOY>
202246
dotnet test ./tests/SqsEventHandler.IntegrationTests/SqsEventHandler.IntegrationTests.csproj
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
{
2-
"title": "Amazon Simple Queue Service (SQS) with AWS Lambda Function",
2+
"title": "Amazon Simple Queue Service (SQS), AWS Lambda Function & Amazon DynamoDB",
33
"description": "This project contains an AWS Lambda function that consumes messages from an Amazon Simple Queue Service (Amazon SQS) queue using SAM and .NET 6.",
44
"content_language": "English",
55
"language": ".NET",
66
"type": ["Unit", "Integration"],
7-
"diagram": "../docs/syntevent-trigger.png",
7+
"diagram": "../docs/sqs-lambda.png",
88
"git_repo_url": "https://github.com/aws-samples/serverless-test-samples",
99
"framework": "SAM",
10-
"services": ["sqs", "lambda"],
10+
"services": ["sqs", "lambda", "dynamodb"],
1111
"pattern_detail_tabs": [
1212
{
1313
"title": "Application Code",
1414
"filepath": "/src/SQSEventHandler/Functions/ProcessEmployeeFunction.cs"
1515
},
1616
{
1717
"title": "Unit Tests",
18-
"filepath": "/tests/SqsEventHandler.UnitTests/ProcessEmployeeFunctionTests.cs"
18+
"filepath": "/tests/SqsEventHandler.UnitTests/Functions/ProcessEmployeeFunctionTests.cs"
1919
},
2020
{
2121
"title": "Integration Tests",
@@ -28,7 +28,7 @@
2828
"image": "https://media.licdn.com/dms/image/D4E03AQExIU1IcSs9lg/profile-displayphoto-shrink_400_400/0/1663268087652?e=1686182400&v=beta&t=xzsZ2Jxcw9rzafTAfB4wqu6noJlJI9bX3hfe3cvjpNA",
2929
"bio": "Cloud Application Architect at AWS",
3030
"linkedin": "https://www.linkedin.com/in/leslie-daniel-raj-21b55449/",
31-
"twitter": ""
31+
"twitter": "https://twitter.com/lesliedanielraj"
3232
}
3333
]
3434
}

dotnet-test-samples/sqs-lambda/sqs-lambda.sln

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SqsEventHandler.UnitTests",
66
EndProject
77
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "docs", "docs", "{00140B9F-D533-4F2C-85B4-ABAEA8529CE4}"
88
ProjectSection(SolutionItems) = preProject
9-
docs\README.md = docs\README.md
9+
README.md = README.md
1010
EndProjectSection
1111
EndProject
1212
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests", "tests", "{0CFC5F99-9F8B-4703-AC45-5536D7075C8D}"

dotnet-test-samples/sqs-lambda/tests/SqsEventHandler.UnitTests/Handlers/SqsEventHandlerTests.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public SqsEventHandlerTests()
2424
}
2525

2626
[Fact]
27-
public async Task SqsEventTrigger_Should_CallProcessSqsMessageOnce()
27+
public async Task SqsEventHandler_Should_CallProcessSqsMessageOnce()
2828
{
2929
//Arrange
3030
var expected = new EmployeeBuilder().Build();
@@ -44,7 +44,7 @@ public async Task SqsEventTrigger_Should_CallProcessSqsMessageOnce()
4444
}
4545

4646
[Fact]
47-
public async Task SqsEventTrigger_Should_CallProcessSqsMessageTwice()
47+
public async Task SqsEventHandler_Should_CallProcessSqsMessageTwice()
4848
{
4949
//Arrange
5050
var expected1 = new EmployeeBuilder().WithEmployeeId("101");
@@ -70,7 +70,7 @@ public async Task SqsEventTrigger_Should_CallProcessSqsMessageTwice()
7070
}
7171

7272
[Fact]
73-
public async Task SqsEventTrigger_Should_ReturnBatchItemFailures()
73+
public async Task SqsEventHandler_Should_ReturnBatchItemFailures()
7474
{
7575
//Arrange
7676
var sqsEvent = new SqsEventBuilder().WithoutEmployees();

0 commit comments

Comments
 (0)