Skip to content

Commit

Permalink
#116: create project structure and add first tests
Browse files Browse the repository at this point in the history
  • Loading branch information
eggwhat committed Jan 18, 2024
1 parent f580468 commit 75e8745
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 0 deletions.
12 changes: 12 additions & 0 deletions SwiftParcel.Services.Orders/SwiftParcel.Services.Orders.sln
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "SwiftParcel.Services.Orders
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SwiftParcel.Services.Orders.Infrastructure", "src\SwiftParcel.Services.Orders.Infrastructure\SwiftParcel.Services.Orders.Infrastructure\SwiftParcel.Services.Orders.Infrastructure.csproj", "{1256CFDD-28BC-4233-A2E2-11CC5925C154}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests", "tests", "{EDFB6002-392F-431B-AAE2-DB2B1414E097}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "SwiftParcel.Services.Orders.Application.UnitTests", "SwiftParcel.Services.Orders.Application.UnitTests", "{D0F373EE-A9D3-45BF-82D0-C2198C734C6F}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SwiftParcel.Services.Orders.Application.UnitTests", "tests\SwiftParcel.Services.Orders.Application.UnitTests\SwiftParcel.Services.Orders.Application.UnitTests\SwiftParcel.Services.Orders.Application.UnitTests.csproj", "{54AAAE25-748F-4F96-A197-D42986F388F2}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -46,6 +52,10 @@ Global
{1256CFDD-28BC-4233-A2E2-11CC5925C154}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1256CFDD-28BC-4233-A2E2-11CC5925C154}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1256CFDD-28BC-4233-A2E2-11CC5925C154}.Release|Any CPU.Build.0 = Release|Any CPU
{54AAAE25-748F-4F96-A197-D42986F388F2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{54AAAE25-748F-4F96-A197-D42986F388F2}.Debug|Any CPU.Build.0 = Debug|Any CPU
{54AAAE25-748F-4F96-A197-D42986F388F2}.Release|Any CPU.ActiveCfg = Release|Any CPU
{54AAAE25-748F-4F96-A197-D42986F388F2}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{6DA713C4-A10F-4248-B54C-9AE6AFB1E882} = {32E2CA73-681D-46E3-BFF4-F9DD0126E32A}
Expand All @@ -56,5 +66,7 @@ Global
{E894C4D2-05B4-479B-93E3-C49CDDF49B9C} = {58C56B7C-E6D8-4CBD-B730-3F307C1DB930}
{69CF0B3C-64D8-44EE-AC46-7066C12E81DB} = {32E2CA73-681D-46E3-BFF4-F9DD0126E32A}
{1256CFDD-28BC-4233-A2E2-11CC5925C154} = {69CF0B3C-64D8-44EE-AC46-7066C12E81DB}
{D0F373EE-A9D3-45BF-82D0-C2198C734C6F} = {EDFB6002-392F-431B-AAE2-DB2B1414E097}
{54AAAE25-748F-4F96-A197-D42986F388F2} = {D0F373EE-A9D3-45BF-82D0-C2198C734C6F}
EndGlobalSection
EndGlobal
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>();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
global using Xunit;
global using Moq;
global using FluentAssertions;
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>

0 comments on commit 75e8745

Please sign in to comment.