Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 0 additions & 11 deletions examples/Idempotency/test/HelloWorld.Test/FunctionTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,11 @@
*/

using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using Amazon.DynamoDBv2;
using Amazon.DynamoDBv2.DataModel;
using Amazon.DynamoDBv2.Model;
using Xunit;
using Amazon.Lambda.APIGatewayEvents;
using Amazon.Lambda.TestUtilities;
using DotNet.Testcontainers.Builders;
using DotNet.Testcontainers.Containers;
using Moq;
using Moq.Protected;
using Xunit.Abstractions;

namespace HelloWorld.Tests
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@
<PackageReference Include="Amazon.Lambda.Core" Version="2.1.0" />
<PackageReference Include="Amazon.Lambda.TestUtilities" Version="2.0.0" />
<PackageReference Include="Amazon.Lambda.Serialization.SystemTextJson" Version="2.3.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.3" />
<PackageReference Include="Moq" Version="4.18.4" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.0" />
<PackageReference Include="xunit" Version="2.5.0" />
<PackageReference Include="Testcontainers" Version="3.3.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand Down
4 changes: 2 additions & 2 deletions examples/Logging/src/HelloWorld/HelloWorld.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Amazon.Lambda.Core" Version="2.1.0" />
<PackageReference Include="Amazon.Lambda.APIGatewayEvents" Version="2.5.0" />
<PackageReference Include="Amazon.Lambda.APIGatewayEvents" Version="2.6.0" />
<PackageReference Include="Amazon.Lambda.Serialization.SystemTextJson" Version="2.3.0" />
<PackageReference Include="AWS.Lambda.Powertools.Logging" Version="1.1.0" />
<PackageReference Include="AWS.Lambda.Powertools.Logging" Version="1.1.1" />
<PackageReference Include="AWSSDK.DynamoDBv2" Version="3.7.101.14" />
</ItemGroup>
</Project>
40 changes: 22 additions & 18 deletions examples/Logging/test/HelloWorld.Test/FunctionTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@
using Xunit;
using Amazon.Lambda.APIGatewayEvents;
using Amazon.Lambda.TestUtilities;
using Moq;
using Moq.Protected;
using NSubstitute;
using Xunit.Abstractions;

namespace HelloWorld.Tests
Expand All @@ -39,6 +38,20 @@ public FunctionTest(ITestOutputHelper testOutputHelper)
_testOutputHelper = testOutputHelper;
}

private class MockHttpMessageHandler : HttpMessageHandler
{
private readonly HttpResponseMessage _responseMessage;
public MockHttpMessageHandler(HttpResponseMessage responseMessage)
{
_responseMessage = responseMessage;
}

protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
return await Task.FromResult(_responseMessage);
}
}

[Fact]
public async Task TestHelloWorldFunctionHandler()
{
Expand All @@ -47,21 +60,12 @@ public async Task TestHelloWorldFunctionHandler()
var accountId = Guid.NewGuid().ToString("D");
var location = "192.158. 1.38";

var dynamoDbContext = new Mock<IDynamoDBContext>();
var handlerMock = new Mock<HttpMessageHandler>();
handlerMock
.Protected()
.Setup<Task<HttpResponseMessage>>(
"SendAsync",
ItExpr.IsAny<HttpRequestMessage>(),
ItExpr.IsAny<CancellationToken>()
)
.ReturnsAsync(new HttpResponseMessage
{
StatusCode = HttpStatusCode.OK,
Content = new StringContent(location)
})
.Verifiable();
var dynamoDbContext = Substitute.For<IDynamoDBContext>();
var handlerMock = new MockHttpMessageHandler(new HttpResponseMessage
{
StatusCode = HttpStatusCode.OK,
Content = new StringContent(location)
});

var request = new APIGatewayProxyRequest
{
Expand Down Expand Up @@ -94,7 +98,7 @@ public async Task TestHelloWorldFunctionHandler()
Headers = new Dictionary<string, string> { { "Content-Type", "application/json" } }
};

var function = new Function(dynamoDbContext.Object, new HttpClient(handlerMock.Object));
var function = new Function(dynamoDbContext, new HttpClient(handlerMock));
var response = await function.FunctionHandler(request, context);

_testOutputHelper.WriteLine("Lambda Response: \n" + response.Body);
Expand Down
8 changes: 4 additions & 4 deletions examples/Logging/test/HelloWorld.Test/HelloWorld.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
<PackageReference Include="Amazon.Lambda.TestUtilities" Version="2.0.0" />
<PackageReference Include="Amazon.Lambda.Serialization.SystemTextJson" Version="2.3.0" />
<PackageReference Include="AWSSDK.DynamoDBv2" Version="3.7.101.14" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.5.0-preview-20221221-03" />
<PackageReference Include="Moq" Version="4.18.3" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.0" />
<PackageReference Include="NSubstitute" Version="5.0.0" />
<PackageReference Include="xunit" Version="2.5.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand Down
6 changes: 3 additions & 3 deletions examples/Metrics/src/HelloWorld/HelloWorld.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Amazon.Lambda.Core" Version="2.1.0" />
<PackageReference Include="Amazon.Lambda.APIGatewayEvents" Version="2.5.0" />
<PackageReference Include="Amazon.Lambda.APIGatewayEvents" Version="2.6.0" />
<PackageReference Include="Amazon.Lambda.Serialization.SystemTextJson" Version="2.3.0" />
<PackageReference Include="AWS.Lambda.Powertools.Logging" Version="1.1.0" />
<PackageReference Include="AWS.Lambda.Powertools.Metrics" Version="1.2.0" />
<PackageReference Include="AWS.Lambda.Powertools.Logging" Version="1.1.1" />
<PackageReference Include="AWS.Lambda.Powertools.Metrics" Version="1.3.2" />
<PackageReference Include="AWSSDK.DynamoDBv2" Version="3.7.101.14" />
</ItemGroup>
</Project>
40 changes: 22 additions & 18 deletions examples/Metrics/test/HelloWorld.Test/FunctionTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@
using Xunit;
using Amazon.Lambda.APIGatewayEvents;
using Amazon.Lambda.TestUtilities;
using Moq;
using Moq.Protected;
using NSubstitute;
using Xunit.Abstractions;

namespace HelloWorld.Tests
Expand All @@ -39,6 +38,20 @@ public FunctionTest(ITestOutputHelper testOutputHelper)
_testOutputHelper = testOutputHelper;
}

private class MockHttpMessageHandler : HttpMessageHandler
{
private readonly HttpResponseMessage _responseMessage;
public MockHttpMessageHandler(HttpResponseMessage responseMessage)
{
_responseMessage = responseMessage;
}

protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
return await Task.FromResult(_responseMessage);
}
}

[Fact]
public async Task TestHelloWorldFunctionHandler()
{
Expand All @@ -47,21 +60,12 @@ public async Task TestHelloWorldFunctionHandler()
var location = "192.158. 1.38";
Environment.SetEnvironmentVariable("POWERTOOLS_METRICS_NAMESPACE","AWSLambdaPowertools");

var dynamoDbContext = new Mock<IDynamoDBContext>();
var handlerMock = new Mock<HttpMessageHandler>();
handlerMock
.Protected()
.Setup<Task<HttpResponseMessage>>(
"SendAsync",
ItExpr.IsAny<HttpRequestMessage>(),
ItExpr.IsAny<CancellationToken>()
)
.ReturnsAsync(new HttpResponseMessage
{
StatusCode = HttpStatusCode.OK,
Content = new StringContent(location)
})
.Verifiable();
var dynamoDbContext = Substitute.For<IDynamoDBContext>();
var handlerMock = new MockHttpMessageHandler(new HttpResponseMessage
{
StatusCode = HttpStatusCode.OK,
Content = new StringContent(location)
});

var request = new APIGatewayProxyRequest
{
Expand Down Expand Up @@ -94,7 +98,7 @@ public async Task TestHelloWorldFunctionHandler()
Headers = new Dictionary<string, string> { { "Content-Type", "application/json" } }
};

var function = new Function(dynamoDbContext.Object, new HttpClient(handlerMock.Object));
var function = new Function(dynamoDbContext, new HttpClient(handlerMock));
var response = await function.FunctionHandler(request, context);

_testOutputHelper.WriteLine("Lambda Response: \n" + response.Body);
Expand Down
8 changes: 4 additions & 4 deletions examples/Metrics/test/HelloWorld.Test/HelloWorld.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
<PackageReference Include="Amazon.Lambda.TestUtilities" Version="2.0.0" />
<PackageReference Include="Amazon.Lambda.Serialization.SystemTextJson" Version="2.3.0" />
<PackageReference Include="AWSSDK.DynamoDBv2" Version="3.7.101.14" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.5.0-preview-20221221-03" />
<PackageReference Include="Moq" Version="4.18.3" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.0" />
<PackageReference Include="NSubstitute" Version="5.0.0" />
<PackageReference Include="xunit" Version="2.5.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand Down
2 changes: 1 addition & 1 deletion examples/Parameters/src/HelloWorld/HelloWorld.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Amazon.Lambda.Core" Version="2.1.0" />
<PackageReference Include="Amazon.Lambda.APIGatewayEvents" Version="2.5.0" />
<PackageReference Include="Amazon.Lambda.APIGatewayEvents" Version="2.6.0" />
<PackageReference Include="Amazon.Lambda.Serialization.SystemTextJson" Version="2.3.0" />
<PackageReference Include="AWS.Lambda.Powertools.Parameters" Version="0.0.2-preview" />
</ItemGroup>
Expand Down
54 changes: 22 additions & 32 deletions examples/Parameters/test/HelloWorld.Test/FunctionTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
using Amazon.Lambda.APIGatewayEvents;
using Amazon.Lambda.TestUtilities;
using Xunit;
using Moq;
using NSubstitute;
using System.Text.Json;

namespace HelloWorld.Tests
Expand All @@ -32,7 +32,7 @@ public async Task TestHelloWorldFunctionHandler()
// Arrange
var requestId = Guid.NewGuid().ToString("D");
var accountId = Guid.NewGuid().ToString("D");

var request = new APIGatewayProxyRequest
{
RequestContext = new APIGatewayProxyRequest.ProxyRequestContext
Expand All @@ -41,28 +41,26 @@ public async Task TestHelloWorldFunctionHandler()
AccountId = accountId
}
};

var context = new TestLambdaContext()
{
FunctionName = Guid.NewGuid().ToString("D"),
FunctionVersion = "1",
MemoryLimitInMB = 215,
AwsRequestId = requestId
};
var helper = new Mock<IParameterLookupHelper>();

var helper = Substitute.For<IParameterLookupHelper>();

var singleSsmRecord = new ParameterLookupRecord
{
Provider = ParameterProviderType.SsmProvider,
Method = ParameterLookupMethod.Get,
Key = Guid.NewGuid().ToString("D"),
Value = Guid.NewGuid().ToString("D")
};
helper.Setup(c =>
c.GetSingleParameterWithSsmProvider()
).ReturnsAsync(singleSsmRecord);

helper.GetSingleParameterWithSsmProvider().Returns(singleSsmRecord);

var multipleSsmRecord = new ParameterLookupRecord
{
Provider = ParameterProviderType.SsmProvider,
Expand All @@ -74,9 +72,7 @@ public async Task TestHelloWorldFunctionHandler()
{ Guid.NewGuid().ToString("D"), Guid.NewGuid().ToString("D") }
}
};
helper.Setup(c =>
c.GetMultipleParametersWithSsmProvider()
).ReturnsAsync(multipleSsmRecord);
helper.GetMultipleParametersWithSsmProvider().Returns(multipleSsmRecord);

var singleSecretRecord = new ParameterLookupRecord
{
Expand All @@ -89,21 +85,17 @@ public async Task TestHelloWorldFunctionHandler()
Password = Guid.NewGuid().ToString("D")
}
};
helper.Setup(c =>
c.GetSingleSecretWithSecretsProvider()
).ReturnsAsync(singleSecretRecord);

helper.GetSingleSecretWithSecretsProvider().Returns(singleSecretRecord);

var singleDynamoDbRecord = new ParameterLookupRecord
{
Provider = ParameterProviderType.DynamoDBProvider,
Method = ParameterLookupMethod.Get,
Key = Guid.NewGuid().ToString("D"),
Value = Guid.NewGuid().ToString("D")
};
helper.Setup(c =>
c.GetSingleParameterWithDynamoDBProvider()
).ReturnsAsync(singleDynamoDbRecord);

helper.GetSingleParameterWithDynamoDBProvider().Returns(singleDynamoDbRecord);

var multipleDynamoDbRecord = new ParameterLookupRecord
{
Provider = ParameterProviderType.DynamoDBProvider,
Expand All @@ -115,10 +107,8 @@ public async Task TestHelloWorldFunctionHandler()
{ Guid.NewGuid().ToString("D"), Guid.NewGuid().ToString("D") }
}
};
helper.Setup(c =>
c.GetMultipleParametersWithDynamoDBProvider()
).ReturnsAsync(multipleDynamoDbRecord);

helper.GetMultipleParametersWithDynamoDBProvider().Returns(multipleDynamoDbRecord);

var body = new Dictionary<string, object>
{
{ "RequestId", requestId },
Expand All @@ -134,7 +124,7 @@ public async Task TestHelloWorldFunctionHandler()
}
}
};

var expectedResponse = new APIGatewayProxyResponse
{
Body = JsonSerializer.Serialize(body),
Expand All @@ -143,15 +133,15 @@ public async Task TestHelloWorldFunctionHandler()
};

// Act
var function = new Function(helper.Object);
var function = new Function(helper);
var response = await function.FunctionHandler(request, context).ConfigureAwait(false);

// Assert
helper.Verify(v => v.GetSingleParameterWithSsmProvider(), Times.Once);
helper.Verify(v => v.GetMultipleParametersWithSsmProvider(), Times.Once);
helper.Verify(v => v.GetSingleSecretWithSecretsProvider(), Times.Once);
helper.Verify(v => v.GetSingleParameterWithDynamoDBProvider(), Times.Once);
helper.Verify(v => v.GetMultipleParametersWithDynamoDBProvider(), Times.Once);
await helper.Received(1).GetSingleParameterWithSsmProvider();
await helper.Received(1).GetMultipleParametersWithSsmProvider();
await helper.Received(1).GetSingleSecretWithSecretsProvider();
await helper.Received(1).GetSingleParameterWithDynamoDBProvider();
await helper.Received(1).GetMultipleParametersWithDynamoDBProvider();
Assert.Equal(expectedResponse.Body, response.Body);
Assert.Equal(expectedResponse.Headers, response.Headers);
Assert.Equal(expectedResponse.StatusCode, response.StatusCode);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
<PackageReference Include="Amazon.Lambda.Core" Version="2.1.0" />
<PackageReference Include="Amazon.Lambda.TestUtilities" Version="2.0.0" />
<PackageReference Include="Amazon.Lambda.Serialization.SystemTextJson" Version="2.3.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.0-preview.23280.1" />
<PackageReference Include="Moq" Version="4.18.4" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.0" />
<PackageReference Include="NSubstitute" Version="5.0.0" />
<PackageReference Include="xunit" Version="2.5.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand Down
Loading