Skip to content

Commit

Permalink
💁
Browse files Browse the repository at this point in the history
  • Loading branch information
hlaueriksson committed Jul 13, 2024
1 parent c9acb4c commit 445e014
Show file tree
Hide file tree
Showing 20 changed files with 308 additions and 515 deletions.
57 changes: 25 additions & 32 deletions samples/CommandQuery.Sample.AWSLambda.Tests/CommandTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System.Text.Json;
using Amazon.Lambda.APIGatewayEvents;
using Amazon.Lambda.TestUtilities;
using CommandQuery.AWSLambda;
using CommandQuery.Sample.Contracts.Commands;
using FluentAssertions;
using Microsoft.Extensions.DependencyInjection;
using NUnit.Framework;
Expand All @@ -9,43 +11,34 @@ namespace CommandQuery.Sample.AWSLambda.Tests
{
public class CommandTests
{
public class when_using_the_real_function
[SetUp]
public void SetUp()
{
[SetUp]
public void SetUp()
{
var serviceCollection = new ServiceCollection();
new Startup().ConfigureServices(serviceCollection);
var serviceProvider = serviceCollection.BuildServiceProvider();
var serviceCollection = new ServiceCollection();
new Startup().ConfigureServices(serviceCollection);
var serviceProvider = serviceCollection.BuildServiceProvider();

Subject = new Command(serviceProvider.GetRequiredService<ICommandFunction>());
}

[Test]
public async Task should_work()
{
var request = GetRequest("{ 'Value': 'Foo' }");
var context = new TestLambdaContext();

var result = await Subject.Post(request, context, "FooCommand");

result.Should().NotBeNull();
}

[Test]
public async Task should_handle_errors()
{
var request = GetRequest("{ 'Value': 'Foo' }");
var context = new TestLambdaContext();
Subject = new Command(serviceProvider.GetRequiredService<ICommandFunction>());
Context = new TestLambdaContext();
}

var result = await Subject.Post(request, context, "FailCommand");
[Test]
public async Task should_handle_command()
{
var response = await Subject.Post(GetRequest(new FooCommand { Value = "Foo" }), Context, "FooCommand");
response.StatusCode.Should().Be(200);
}

result.ShouldBeError("The command type 'FailCommand' could not be found");
}
[Test]
public async Task should_handle_errors()
{
var response = await Subject.Post(GetRequest(new FooCommand()), Context, "FooCommand");
response.ShouldBeError("Value cannot be null or empty");
}

static APIGatewayProxyRequest GetRequest(string content) => new() { Body = content };
static APIGatewayProxyRequest GetRequest(object body) => new() { Body = JsonSerializer.Serialize(body) };

Command Subject = null!;
}
Command Subject = null!;
TestLambdaContext Context = null!;
}
}
108 changes: 36 additions & 72 deletions samples/CommandQuery.Sample.AWSLambda.Tests/QueryTests.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System.Text.Json;
using System.Web;
using Amazon.Lambda.APIGatewayEvents;
using Amazon.Lambda.Core;
using Amazon.Lambda.TestUtilities;
Expand All @@ -11,90 +13,52 @@ namespace CommandQuery.Sample.AWSLambda.Tests
{
public class QueryTests
{
public class when_using_the_real_function_via_Post
[SetUp]
public void SetUp()
{
[SetUp]
public void SetUp()
{
var serviceCollection = new ServiceCollection();
new Startup().ConfigureServices(serviceCollection);
var serviceProvider = serviceCollection.BuildServiceProvider();

Subject = new Query(serviceProvider.GetRequiredService<IQueryFunction>());
Request = GetRequest("POST", content: "{ \"Id\": 1 }");
Context = new TestLambdaContext();
}

[Test]
public async Task should_work()
{
var result = await Subject.Post(Request, Context, "BarQuery");
var value = result.As<Bar>()!;

value.Id.Should().Be(1);
value.Value.Should().NotBeEmpty();
}

[Test]
public async Task should_handle_errors()
{
var result = await Subject.Post(Request, Context, "FailQuery");

result.ShouldBeError("The query type 'FailQuery' could not be found");
}
var serviceCollection = new ServiceCollection();
new Startup().ConfigureServices(serviceCollection);
var serviceProvider = serviceCollection.BuildServiceProvider();

Query Subject = null!;
APIGatewayProxyRequest Request = null!;
ILambdaContext Context = null!;
Subject = new Query(serviceProvider.GetRequiredService<IQueryFunction>());
Context = new TestLambdaContext();
}

public class when_using_the_real_function_via_Get
[Test]
public async Task should_handle_query_via_Post()
{
[SetUp]
public void SetUp()
{
var serviceCollection = new ServiceCollection();
new Startup().ConfigureServices(serviceCollection);
var serviceProvider = serviceCollection.BuildServiceProvider();

Subject = new Query(serviceProvider.GetRequiredService<IQueryFunction>());
Request = GetRequest("GET", query: new Dictionary<string, IList<string>> { { "Id", new List<string> { "1" } } });
Context = new TestLambdaContext();
}

[Test]
public async Task should_work()
{
var result = await Subject.Get(Request, Context, "BarQuery");
var value = result.As<Bar>()!;

value.Id.Should().Be(1);
value.Value.Should().NotBeEmpty();
}

[Test]
public async Task should_handle_errors()
{
var result = await Subject.Get(Request, Context, "FailQuery");

result.ShouldBeError("The query type 'FailQuery' could not be found");
}
var response = await Subject.Post(GetRequest("POST", new BarQuery { Id = 1 }), Context, "BarQuery");
var value = response.Body<Bar>()!;
value.Id.Should().Be(1);
}

Query Subject = null!;
APIGatewayProxyRequest Request = null!;
ILambdaContext Context = null!;
[Test]
public async Task should_handle_query_via_Get()
{
var response = await Subject.Get(GetRequest("GET", "?Id=1"), Context, "BarQuery");
var value = response.Body<Bar>()!;
value.Id.Should().Be(1);
}

static APIGatewayProxyRequest GetRequest(string method, string? content = null, Dictionary<string, IList<string>>? query = null)
static APIGatewayProxyRequest GetRequest(string method, object body) => new()
{
var request = new APIGatewayProxyRequest
HttpMethod = method,
Body = JsonSerializer.Serialize(body),
};

static APIGatewayProxyRequest GetRequest(string method, string query)
{
var collection = HttpUtility.ParseQueryString(query);
var parameters = collection.AllKeys.ToDictionary(k => k!, k => collection.GetValues(k)!.ToList() as IList<string>);

return new()
{
HttpMethod = method,
Body = content,
MultiValueQueryStringParameters = query
MultiValueQueryStringParameters = parameters
};

return request;
}

Query Subject = null!;
ILambdaContext Context = null!;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,14 @@ public static void ShouldBeError(this APIGatewayProxyResponse result, string mes
{
result.Should().NotBeNull();
result.StatusCode.Should().NotBe(200);
var value = JsonSerializer.Deserialize<Error>(result.Body)!;
var value = result.Body<Error>()!;
value.Should().NotBeNull();
value.Message.Should().Be(message);
}

public static T? Body<T>(this APIGatewayProxyResponse result)
{
return JsonSerializer.Deserialize<T>(result.Body);
}
}
}
13 changes: 0 additions & 13 deletions samples/CommandQuery.Sample.AWSLambda.Tests/TestExtensions.cs

This file was deleted.

5 changes: 4 additions & 1 deletion samples/CommandQuery.Sample.AWSLambda/Command.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ public class Command(ICommandFunction commandFunction)
{
[LambdaFunction(Policies = "AWSLambdaBasicExecutionRole", MemorySize = 256, Timeout = 30)]
[RestApi(LambdaHttpMethod.Post, "/command/{commandName}")]
public async Task<APIGatewayProxyResponse> Post(APIGatewayProxyRequest request, ILambdaContext context, string commandName) =>
public async Task<APIGatewayProxyResponse> Post(
APIGatewayProxyRequest request,
ILambdaContext context,
string commandName) =>
await commandFunction.HandleAsync(commandName, request, context.Logger);
}
10 changes: 8 additions & 2 deletions samples/CommandQuery.Sample.AWSLambda/Query.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,18 @@ public class Query(IQueryFunction queryFunction)
{
[LambdaFunction(Policies = "AWSLambdaBasicExecutionRole", MemorySize = 256, Timeout = 30)]
[RestApi(LambdaHttpMethod.Get, "/query/{queryName}")]
public async Task<APIGatewayProxyResponse> Get(APIGatewayProxyRequest request, ILambdaContext context, string queryName) =>
public async Task<APIGatewayProxyResponse> Get(
APIGatewayProxyRequest request,
ILambdaContext context,
string queryName) =>
await queryFunction.HandleAsync(queryName, request, context.Logger);

[LambdaFunction(Policies = "AWSLambdaBasicExecutionRole", MemorySize = 256, Timeout = 30)]
[RestApi(LambdaHttpMethod.Post, "/query/{queryName}")]
public async Task<APIGatewayProxyResponse> Post(APIGatewayProxyRequest request, ILambdaContext context, string queryName) =>
public async Task<APIGatewayProxyResponse> Post(
APIGatewayProxyRequest request,
ILambdaContext context,
string queryName) =>
await queryFunction.HandleAsync(queryName, request, context.Logger);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using System.Net;
using System.Text;
using CommandQuery.Sample.Contracts.Commands;
using FluentAssertions;
using Microsoft.AspNetCore.Mvc.Testing;
using NUnit.Framework;
Expand All @@ -8,46 +8,35 @@ namespace CommandQuery.Sample.AspNetCore.Tests
{
public class CommandControllerTests
{
public class when_using_the_real_controller
[SetUp]
public void SetUp()
{
[SetUp]
public void SetUp()
{
Factory = new WebApplicationFactory<Program>();
Client = Factory.CreateClient();
}

[TearDown]
public void TearDown()
{
Client.Dispose();
Factory.Dispose();
}

[Test]
public async Task should_work()
{
var content = new StringContent("{ \"Value\": \"Foo\" }", Encoding.UTF8, "application/json");

var result = await Client.PostAsync("/api/command/FooCommand", content);

result.EnsureSuccessStatusCode();
(await result.Content.ReadAsStringAsync()).Should().BeEmpty();
}

[Test]
public async Task should_handle_errors()
{
var content = new StringContent("{ \"Value\": \"Foo\" }", Encoding.UTF8, "application/json");
Factory = new WebApplicationFactory<Program>();
Client = Factory.CreateClient();
}

var result = await Client.PostAsync("/api/command/FailCommand", content);
[TearDown]
public void TearDown()
{
Client.Dispose();
Factory.Dispose();
}

result.StatusCode.Should().Be(HttpStatusCode.NotFound);
(await result.Content.ReadAsStringAsync()).Should().BeEmpty();
}
[Test]
public async Task should_handle_command()
{
var response = await Client.PostAsJsonAsync("/api/command/FooCommand", new FooCommand { Value = "Foo" });
response.StatusCode.Should().Be(HttpStatusCode.OK);
}

WebApplicationFactory<Program> Factory = null!;
HttpClient Client = null!;
[Test]
public async Task should_handle_errors()
{
var response = await Client.PostAsJsonAsync("/api/command/FooCommand", new FooCommand { Value = "" });
await response.ShouldBeErrorAsync("Value cannot be null or empty");
}

WebApplicationFactory<Program> Factory = null!;
HttpClient Client = null!;
}
}
Loading

0 comments on commit 445e014

Please sign in to comment.