-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#116: create project structure and add first tests
- Loading branch information
Showing
4 changed files
with
143 additions
and
0 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
97 changes: 97 additions & 0 deletions
97
...sts/SwiftParcel.Services.Orders.Application.UnitTests/Commands/CreateOrderHandlerTests.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,97 @@ | ||
using SwiftParcel.Services.Orders.Application.Commands; | ||
using SwiftParcel.Services.Orders.Application.Commands.Handlers; | ||
using SwiftParcel.Services.Orders.Application.Exceptions; | ||
using SwiftParcel.Services.Orders.Core.Entities; | ||
using SwiftParcel.Services.Orders.Core.Exceptions; | ||
using SwiftParcel.Services.Orders.Core.Repositories; | ||
using Convey.CQRS.Commands; | ||
|
||
namespace SwiftParcel.Services.Orders.Application.UnitTests.Commands | ||
{ | ||
public class CreateOrderHandlerTests | ||
{ | ||
private readonly CreateOrderHandler _createOrderHandler; | ||
private readonly Mock<ICustomerRepository> _customerRepositoryMock; | ||
private readonly Mock<ICommandDispatcher> _commandDispatcherMock; | ||
|
||
public CreateOrderHandlerTests() | ||
{ | ||
_customerRepositoryMock = new Mock<ICustomerRepository>(); | ||
_commandDispatcherMock = new Mock<ICommandDispatcher>(); | ||
_createOrderHandler = new CreateOrderHandler(_customerRepositoryMock.Object, _commandDispatcherMock.Object); | ||
} | ||
|
||
[Fact] | ||
public async Task HandleAsync_WithValidCustomerAndSwiftParcelCompany_ShouldSendCreateOrderSwiftParcelCommand() | ||
{ | ||
// Arrange | ||
var orderId = Guid.NewGuid(); | ||
var customerId = Guid.NewGuid(); | ||
var parcelId = Guid.NewGuid(); | ||
var command = new CreateOrder(orderId, customerId, parcelId, "", "", new Address(), Company.SwiftParcel); | ||
var cancellationToken = new CancellationToken(); | ||
|
||
_customerRepositoryMock.Setup(repo => repo.ExistsAsync(command.CustomerId)).ReturnsAsync(true); | ||
|
||
// Act | ||
await _createOrderHandler.HandleAsync(command, cancellationToken); | ||
|
||
// Assert | ||
_commandDispatcherMock.Verify(dispatcher => dispatcher.SendAsync(It.IsAny<CreateOrderSwiftParcel>(), cancellationToken), Times.Once); | ||
} | ||
|
||
[Fact] | ||
public async Task HandleAsync_WithValidCustomerAndMiniCurrierCompany_ShouldSendCreateOrderMiniCurrierCommand() | ||
{ | ||
// Arrange | ||
var orderId = Guid.NewGuid(); | ||
var customerId = Guid.NewGuid(); | ||
var parcelId = Guid.NewGuid(); | ||
var command = new CreateOrder(orderId, customerId, parcelId, "", "", new Address(), Company.MiniCurrier); | ||
var cancellationToken = new CancellationToken(); | ||
|
||
_customerRepositoryMock.Setup(repo => repo.ExistsAsync(command.CustomerId)).ReturnsAsync(true); | ||
|
||
// Act | ||
await _createOrderHandler.HandleAsync(command, cancellationToken); | ||
|
||
// Assert | ||
_commandDispatcherMock.Verify(dispatcher => dispatcher.SendAsync(It.IsAny<CreateOrderMiniCurrier>(), cancellationToken), Times.Once); | ||
} | ||
|
||
[Fact] | ||
public async Task HandleAsync_WithInvalidCustomer_ShouldThrowCustomerNotFoundException() | ||
{ | ||
// Arrange | ||
var orderId = Guid.NewGuid(); | ||
var customerId = Guid.NewGuid(); | ||
var parcelId = Guid.NewGuid(); | ||
var command = new CreateOrder(orderId, customerId, parcelId, "", "", new Address(), Company.MiniCurrier); | ||
var cancellationToken = new CancellationToken(); | ||
|
||
_customerRepositoryMock.Setup(repo => repo.ExistsAsync(command.CustomerId)).ReturnsAsync(false); | ||
|
||
// Act & Assert | ||
|
||
Func<Task> act = async () => await _createOrderHandler.HandleAsync(command, cancellationToken); | ||
await act.Should().ThrowAsync<CustomerNotFoundException>(); | ||
} | ||
|
||
[Fact] | ||
public async Task HandleAsync_WithInvalidCompany_ShouldThrowCompanyNotFoundException() | ||
{ | ||
// Arrange | ||
var orderId = Guid.NewGuid(); | ||
var customerId = Guid.NewGuid(); | ||
var parcelId = Guid.NewGuid(); | ||
var command = new CreateOrder(orderId, customerId, parcelId, "", "", new Address(), (Company)int.MaxValue); | ||
var cancellationToken = new CancellationToken(); | ||
|
||
_customerRepositoryMock.Setup(repo => repo.ExistsAsync(command.CustomerId)).ReturnsAsync(true); | ||
|
||
// Act & Assert | ||
Func<Task> act = async () => await _createOrderHandler.HandleAsync(command, cancellationToken); | ||
await act.Should().ThrowAsync<CompanyNotFoundException>(); | ||
} | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
...s.Application.UnitTests/SwiftParcel.Services.Orders.Application.UnitTests/GlobalUsings.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,3 @@ | ||
global using Xunit; | ||
global using Moq; | ||
global using FluentAssertions; |
31 changes: 31 additions & 0 deletions
31
...ces.Orders.Application.UnitTests/SwiftParcel.Services.Orders.Application.UnitTests.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,31 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>disable</Nullable> | ||
|
||
<IsPackable>false</IsPackable> | ||
<IsTestProject>true</IsTestProject> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="FluentAssertions" Version="6.12.0" /> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.1" /> | ||
<PackageReference Include="Moq" Version="4.20.70" /> | ||
<PackageReference Include="xunit" Version="2.4.2" /> | ||
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5"> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
<PrivateAssets>all</PrivateAssets> | ||
</PackageReference> | ||
<PackageReference Include="coverlet.collector" Version="3.2.0"> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
<PrivateAssets>all</PrivateAssets> | ||
</PackageReference> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\..\src\SwiftParcel.Services.Orders.Application\SwiftParcel.Services.Orders.Application\SwiftParcel.Services.Orders.Application.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |