-
-
Notifications
You must be signed in to change notification settings - Fork 506
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added E2E tests for Closing the books example
- Loading branch information
1 parent
e4bfb29
commit 0c70ed9
Showing
16 changed files
with
291 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
Sample/ClosingTheBooks/PointOfSales.Api.Tests/CashRegisters/EndToEndPositiveScenarios.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
using System.Net; | ||
|
||
namespace PointOfSales.Api.Tests.CashRegisters; | ||
|
||
using static Scenarios; | ||
|
||
public class EndToEndPositiveScenarios(ApiSpecification<Program> api): | ||
IClassFixture<ApiSpecification<Program>> | ||
{ | ||
[Fact] | ||
public Task ForRegisteredCashRegister_OpensFirstCashierShift() => | ||
api.Given([InitializedCashRegister(cashRegisterId)]) | ||
.When( | ||
POST, | ||
URI($"/api/cash-registers/{cashRegisterId}/cashier-shifts"), | ||
BODY(new OpenShiftRequest(Guid.NewGuid().ToString())) | ||
).Then(CREATED_WITH_DEFAULT_HEADERS($"/api/cash-registers/{cashRegisterId}/cashier-shifts/1")); | ||
|
||
[Fact] | ||
public Task ForOpenedCashRegister_DoesntOpensNextCashierShift() => | ||
api.Given([InitializedCashRegister(cashRegisterId), OpenedCashierShift(cashRegisterId)]) | ||
.When( | ||
POST, | ||
URI($"/api/cash-registers/{cashRegisterId}/cashier-shifts"), | ||
BODY(new OpenShiftRequest(Guid.NewGuid().ToString())) | ||
).Then(PRECONDITION_FAILED); | ||
|
||
[Fact] | ||
public Task ForOpenedCashRegister_ClosesCashierShift() => | ||
api.Given([ | ||
InitializedCashRegister(cashRegisterId), | ||
OpenedCashierShift(cashRegisterId) | ||
]) | ||
.When( | ||
POST, | ||
URI($"/api/cash-registers/{cashRegisterId}/cashier-shifts/1/close"), | ||
BODY(new CloseShiftRequest(100)), | ||
HEADERS(IF_MATCH(1)) | ||
).Then(OK); | ||
|
||
|
||
[Fact] | ||
public Task ForClosedCashRegister_OpensCashierShift() => | ||
api.Given([ | ||
InitializedCashRegister(cashRegisterId), | ||
OpenedCashierShift(cashRegisterId), | ||
ClosedCashierShift(cashRegisterId, 1, 1) | ||
]) | ||
.When( | ||
POST, | ||
URI($"/api/cash-registers/{cashRegisterId}/cashier-shifts"), | ||
BODY(new OpenShiftRequest(Guid.NewGuid().ToString())) | ||
).Then(CREATED_WITH_DEFAULT_HEADERS($"/api/cash-registers/{cashRegisterId}/cashier-shifts/2")); | ||
|
||
private readonly string cashRegisterId = Guid.NewGuid().ToString(); | ||
} |
15 changes: 15 additions & 0 deletions
15
Sample/ClosingTheBooks/PointOfSales.Api.Tests/CashRegisters/InitializeCashRegisterTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
namespace PointOfSales.Api.Tests.CashRegisters; | ||
|
||
public class InitializeCashRegisterTests(ApiSpecification<Program> api): | ||
IClassFixture<ApiSpecification<Program>> | ||
{ | ||
[Fact] | ||
public Task InitializeCashRegister() => | ||
api.Given() | ||
.When( | ||
POST, | ||
URI($"/api/cash-registers/{cashRegisterId}") | ||
).Then(CREATED_WITH_DEFAULT_HEADERS($"/api/cash-registers/{cashRegisterId}")); | ||
|
||
private readonly Guid cashRegisterId = Guid.NewGuid(); | ||
} |
46 changes: 46 additions & 0 deletions
46
Sample/ClosingTheBooks/PointOfSales.Api.Tests/CashRegisters/Scenarios.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
using Bogus; | ||
using Bogus.DataSets; | ||
|
||
namespace PointOfSales.Api.Tests.CashRegisters; | ||
|
||
public static class Scenarios | ||
{ | ||
private static readonly Faker faker = new(); | ||
|
||
public static RequestDefinition InitializedCashRegister(string? cashRegisterId = null) => | ||
SEND( | ||
"Initialize Cash Register", | ||
POST, | ||
URI($"/api/cash-registers/{cashRegisterId ?? Guid.NewGuid().ToString()}") | ||
); | ||
|
||
public static RequestDefinition OpenedCashierShift(string cashRegisterId, string? cashierId = null) => | ||
SEND( | ||
"Open Cashier Shift", | ||
POST, | ||
URI($"/api/cash-registers/{cashRegisterId}/cashier-shifts"), | ||
BODY(new OpenShiftRequest(cashierId ?? Guid.NewGuid().ToString())) | ||
); | ||
|
||
public static RequestDefinition RegisteredTransaction(Guid cashRegisterId, int shiftNumber, decimal? amount) => | ||
SEND( | ||
"Register Transaction", | ||
POST, | ||
URI($"/api/cash-registers/{cashRegisterId}/cashier-shifts/{shiftNumber}/transactions"), | ||
BODY(new RegisterTransactionRequest(amount ?? faker.Finance.Amount())) | ||
); | ||
|
||
public static RequestDefinition ClosedCashierShift( | ||
string cashRegisterId, | ||
int shiftNumber, | ||
int etag, | ||
decimal? declaredTender = null | ||
) => | ||
SEND( | ||
"Close Cashier Shift", | ||
POST, | ||
URI($"/api/cash-registers/{cashRegisterId}/cashier-shifts/{shiftNumber}/close"), | ||
BODY(new CloseShiftRequest(declaredTender ?? faker.Finance.Amount())), | ||
HEADERS(IF_MATCH(etag)) | ||
); | ||
} |
30 changes: 30 additions & 0 deletions
30
Sample/ClosingTheBooks/PointOfSales.Api.Tests/PointOfSales.Api.Tests.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="FluentAssertions" Version="6.12.0" /> | ||
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" /> | ||
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="8.0.0" /> | ||
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="8.0.0" /> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" /> | ||
<PackageReference Include="NSubstitute" Version="5.1.0" /> | ||
<PackageReference Include="xunit" Version="2.6.6" /> | ||
<PackageReference Include="Microsoft.AspNetCore.TestHost" Version="8.0.1" /> | ||
<PackageReference Include="Ogooreck" Version="0.8.0" /> | ||
<PackageReference Include="Bogus" Version="35.4.0" /> | ||
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.6"> | ||
<PrivateAssets>all</PrivateAssets> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
</PackageReference> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\PointOfSales.Api\PointOfSales.Api.csproj" /> | ||
</ItemGroup> | ||
|
||
<Import Project="..\..\..\Tests.Build.props" /> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
using Oakton; | ||
using Xunit.Abstractions; | ||
using Xunit.Sdk; | ||
|
||
[assembly: CollectionBehavior(DisableTestParallelization = true)] | ||
|
||
[assembly: TestFramework("PointOfSales.Api.Tests.AssemblyFixture", "PointOfSales.Api.Tests")] | ||
|
||
namespace PointOfSales.Api.Tests; | ||
|
||
public sealed class AssemblyFixture : XunitTestFramework | ||
{ | ||
public AssemblyFixture(IMessageSink messageSink) | ||
:base(messageSink) | ||
{ | ||
OaktonEnvironment.AutoStartHost = true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
global using Xunit; | ||
global using Ogooreck.API; | ||
global using static Ogooreck.API.ApiSpecification; |
62 changes: 62 additions & 0 deletions
62
...s/PointOfSales.Api/Core/Http/Middlewares/ExceptionHandling/ExceptionHandlingMiddleware.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
using System.ComponentModel.DataAnnotations; | ||
using System.Net; | ||
using Microsoft.AspNetCore.Diagnostics; | ||
using Microsoft.AspNetCore.Mvc; | ||
|
||
namespace Helpdesk.Api.Core.Http.Middlewares.ExceptionHandling; | ||
|
||
public class ExceptionToProblemDetailsHandler(Func<Exception, HttpContext, ProblemDetails?>? customExceptionMap) | ||
: IExceptionHandler | ||
{ | ||
public async ValueTask<bool> TryHandleAsync( | ||
HttpContext httpContext, | ||
Exception exception, | ||
CancellationToken cancellationToken | ||
) | ||
{ | ||
var details = customExceptionMap?.Invoke(exception, httpContext) ?? exception.MapToProblemDetails(); | ||
|
||
httpContext.Response.StatusCode = details.Status ?? StatusCodes.Status500InternalServerError; | ||
await httpContext.Response | ||
.WriteAsJsonAsync(details, cancellationToken: cancellationToken).ConfigureAwait(false); | ||
|
||
return true; | ||
} | ||
} | ||
|
||
public static class ExceptionHandlingMiddleware | ||
{ | ||
public static IServiceCollection AddDefaultExceptionHandler( | ||
this IServiceCollection serviceCollection, | ||
Func<Exception, HttpContext, ProblemDetails?>? customExceptionMap = null | ||
) => | ||
serviceCollection | ||
.AddProblemDetails() | ||
.AddSingleton<IExceptionHandler>(new ExceptionToProblemDetailsHandler(customExceptionMap)); | ||
} | ||
|
||
public static class ProblemDetailsExtensions | ||
{ | ||
public static ProblemDetails MapToProblemDetails(this Exception exception) | ||
{ | ||
var statusCode = exception switch | ||
{ | ||
ArgumentException _ => StatusCodes.Status400BadRequest, | ||
ValidationException _ => StatusCodes.Status400BadRequest, | ||
UnauthorizedAccessException _ => StatusCodes.Status401Unauthorized, | ||
InvalidOperationException _ => StatusCodes.Status403Forbidden, | ||
NotImplementedException _ => StatusCodes.Status501NotImplemented, | ||
_ => StatusCodes.Status500InternalServerError | ||
}; | ||
|
||
return exception.MapToProblemDetails(statusCode); | ||
} | ||
|
||
public static ProblemDetails MapToProblemDetails( | ||
this Exception exception, | ||
int statusCode, | ||
string? title = null, | ||
string? detail = null | ||
) => | ||
new() { Title = title ?? exception.GetType().Name, Detail = detail ?? exception.Message, Status = statusCode }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
Sample/ClosingTheBooks/PointOfSales.Api/appsettings.Development.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"Logging": { | ||
"LogLevel": { | ||
"Default": "Information", | ||
"Microsoft.AspNetCore": "Warning" | ||
} | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
Sample/ClosingTheBooks/PointOfSales.Api/appsettings.Production.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"Logging": { | ||
"LogLevel": { | ||
"Default": "Information", | ||
"Microsoft.AspNetCore": "Warning" | ||
} | ||
}, | ||
"AllowedHosts": "*", | ||
"ConnectionStrings": { | ||
"PointOfSales": "PORT = 5432; HOST = postgres; TIMEOUT = 15; POOLING = True; MINPOOLSIZE = 1; MAXPOOLSIZE = 100; COMMANDTIMEOUT = 20; DATABASE = 'postgres'; PASSWORD = 'Password12!'; USER ID = 'postgres'" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"Logging": { | ||
"LogLevel": { | ||
"Default": "Information", | ||
"Microsoft.AspNetCore": "Warning" | ||
} | ||
}, | ||
"AllowedHosts": "*", | ||
"ConnectionStrings": { | ||
"PointOfSales": "PORT = 5432; HOST = localhost; TIMEOUT = 15; POOLING = True; MINPOOLSIZE = 1; MAXPOOLSIZE = 100; COMMANDTIMEOUT = 20; DATABASE = 'postgres'; PASSWORD = 'Password12!'; USER ID = 'postgres'" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.