Skip to content

Commit

Permalink
tests: implement unit tests for gateways controller
Browse files Browse the repository at this point in the history
  • Loading branch information
leynier committed May 19, 2022
1 parent 935a844 commit a9ab2c3
Show file tree
Hide file tree
Showing 6 changed files with 267 additions and 7 deletions.
16 changes: 16 additions & 0 deletions Gateways.Api.Tests/AutoMapperFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using AutoMapper;
using Gateways.Api.MapperProfiles;

namespace Gateways.Api.Tests;

public static class AutoMapperFactory
{
public static IMapper CreateMapper()
{
return new MapperConfiguration(config =>
{
config.AddProfile(new DeviceProfile());
config.AddProfile(new GatewayProfile());
}).CreateMapper();
}
}
232 changes: 232 additions & 0 deletions Gateways.Api.Tests/Controllers/GatewaysControllerTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,232 @@
using AutoMapper;
using Gateways.Api.Controllers;
using Gateways.Api.Models;
using Gateways.Business.Contracts.Entities;
using Gateways.Business.Contracts.Services;
using Gateways.Common.Errors;
using Gateways.Common.Models;
using Moq;

namespace Gateways.Api.Tests.Controllers;

public class GatewaysControllerTests
{
private readonly Mock<IGatewayService> gatewayService;
private readonly IMapper mapper;

public GatewaysControllerTests()
{
gatewayService = new Mock<IGatewayService>();
mapper = AutoMapperFactory.CreateMapper();
}

private GatewaysController Controller => new(gatewayService.Object, mapper);

private IQueryable<Gateway> GetQueryableWithData => new[]
{
new Gateway { Name = "Gateway 1" },
new Gateway { Name = "Gateway 2" },
new Gateway { Name = "Gateway 3" },
new Gateway { Name = "Gateway 4" },
new Gateway { Name = "Gateway 5" },
new Gateway { Name = "Gateway 6" },
new Gateway { Name = "Gateway 7" },
new Gateway { Name = "Gateway 8" },
new Gateway { Name = "Gateway 9" },
new Gateway { Name = "Gateway 10" },
}.AsQueryable();

[Fact]
public void GetAll_ShouldReturnEmptyResult()
{
// Arrange
var gatewayQueryable = Array.Empty<Gateway>().AsQueryable();
gatewayService.Reset();
gatewayService.Setup(x => x.Query()).Returns(gatewayQueryable);

// Act
var result = Controller.GetAll(new PaginationQueryModel());

// Assert
Assert.Equal(200, result.StatusCode);
Assert.NotNull(result.Data);
Assert.Empty(result.Data!.Items);
Assert.False(result.Data!.HasPrevious);
Assert.False(result.Data!.HasNext);
}

[Fact]
public void GetAll_ShouldReturnPaginatedResult()
{
// Arrange
gatewayService.Reset();
gatewayService.Setup(x => x.Query()).Returns(GetQueryableWithData);

// Act
var result = Controller.GetAll(new PaginationQueryModel { Page = 2, PageSize = 3 });

// Assert
Assert.Equal(200, result.StatusCode);
Assert.NotNull(result.Data);
Assert.Equal(3, result.Data!.Items.Count());
Assert.True(result.Data!.HasPrevious);
Assert.True(result.Data!.HasNext);
}

[Fact]
public void GetAll_ShouldReturnPaginatedResultWithEmptyItems()
{
// Arrange
var gatewayQueryable = GetQueryableWithData;
gatewayService.Reset();
gatewayService.Setup(x => x.Query()).Returns(gatewayQueryable);

// Act
var result = Controller.GetAll(new PaginationQueryModel { Page = 4, PageSize = 3 });

// Assert
Assert.Equal(200, result.StatusCode);
Assert.NotNull(result.Data);
Assert.Empty(result.Data!.Items);
Assert.True(result.Data!.HasPrevious);
Assert.False(result.Data!.HasNext);
}

[Fact]
public void GetAll_ShouldReturnPaginatedResultWithEmptyItemsAndNoPrevious()
{
// Arrange
var gatewayQueryable = GetQueryableWithData;
gatewayService.Reset();
gatewayService.Setup(x => x.Query()).Returns(gatewayQueryable);

// Act
var result = Controller.GetAll(new PaginationQueryModel { Page = 5, PageSize = 3 });

// Assert
Assert.Equal(200, result.StatusCode);
Assert.NotNull(result.Data);
Assert.Empty(result.Data!.Items);
Assert.False(result.Data!.HasPrevious);
Assert.False(result.Data!.HasNext);
}

[Fact]
public void Get_ShouldReturnNotFound()
{
// Arrange
var gatewayQueryable = Array.Empty<Gateway>().AsQueryable();
gatewayService.Reset();
gatewayService.Setup(x => x.Query()).Returns(gatewayQueryable);

// Act
var action = () => Controller.Get(Guid.NewGuid().ToString());

// Assert
Assert.Throws<NotFoundError>(action);
}

[Fact]
public void Get_ShouldReturnGateway()
{
// Arrange
var gatewayQueryable = GetQueryableWithData;
gatewayService.Reset();
gatewayService.Setup(x => x.Query()).Returns(gatewayQueryable);

// Act
var result = Controller.Get(gatewayQueryable.First().Id);

// Assert
Assert.Equal(200, result.StatusCode);
Assert.NotNull(result.Data);
Assert.Equal(gatewayQueryable.First().Name, result.Data!.Name);
}

[Fact]
public void Put_ShouldReturnNotFound()
{
// Arrange
var gatewayQueryable = Array.Empty<Gateway>().AsQueryable();
gatewayService.Reset();
gatewayService.Setup(x => x.Query()).Returns(gatewayQueryable);

// Act
var action = () => Controller.Put(Guid.NewGuid().ToString(), new GatewayPutModel());

// Assert
Assert.Throws<NotFoundError>(action);
}

[Fact]
public void Put_ShouldChangeName()
{
// Arrange
var gatewayQueryable = GetQueryableWithData;
gatewayService.Reset();
gatewayService.Setup(x => x.Query()).Returns(gatewayQueryable);

// Act
var result = Controller.Put(gatewayQueryable.First().Id, new GatewayPutModel { Name = "New Name" });

// Assert
Assert.Equal(200, result.StatusCode);
Assert.NotNull(result.Data);
Assert.Equal("New Name", result.Data!.Name);

gatewayService.Verify(x => x.Update(It.IsAny<Gateway>()), Times.Once);
}

[Fact]
public void Post_ShouldReturnGateway()
{
// Arrange
var gatewayQueryable = GetQueryableWithData;
gatewayService.Reset();
gatewayService.Setup(x => x.Query()).Returns(gatewayQueryable);

// Act
var result = Controller.Post(new GatewayPostModel { Name = "New Name" });

// Assert
Assert.Equal(200, result.StatusCode);
Assert.NotNull(result.Data);
Assert.Equal("New Name", result.Data!.Name);

gatewayService.Verify(x => x.Add(It.IsAny<Gateway>()), Times.Once);
}

[Fact]
public void Delete_ShouldReturnNotFound()
{
// Arrange
var gatewayQueryable = Array.Empty<Gateway>().AsQueryable();
gatewayService.Reset();
gatewayService.Setup(x => x.Query()).Returns(gatewayQueryable);

// Act
var action = () => Controller.Delete(Guid.NewGuid().ToString());

// Assert
Assert.Throws<NotFoundError>(action);
}

[Fact]
public void Delete_ShouldDeleteGateway()
{
// Arrange
var gatewayQueryable = GetQueryableWithData;
gatewayService.Reset();
gatewayService.Setup(x => x.Query()).Returns(gatewayQueryable);

// Act
var result = Controller.Delete(gatewayQueryable.First().Id);

// Assert
Assert.Equal(200, result.StatusCode);
Assert.NotNull(result.Data);
Assert.Equal(gatewayQueryable.First().Name, result.Data!.Name);

gatewayService.Verify(x => x.Remove(It.IsAny<Gateway>()), Times.Once);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,12 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="11.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="Moq" Version="4.18.1" />
<PackageReference Include="xunit" Version="2.4.1">
<TreatAsUsed>true</TreatAsUsed>
</PackageReference>
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
Expand All @@ -21,4 +25,8 @@
</PackageReference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Gateways.API\Gateways.Api.csproj" />
</ItemGroup>

</Project>
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="6.0.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Gateways.Business.Implementations\Gateways.Business.Implementations.csproj" />
<ProjectReference Include="..\Gateways.Data.Implementations\Gateways.Data.Implementations.csproj" />
Expand Down
12 changes: 6 additions & 6 deletions Gateways.sln
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Gateways.Business.Bootstrap
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Gateways.Common", "Gateways.Common\Gateways.Common.csproj", "{96CFC1DC-33CE-4CE0-9B5B-6A30F84D67F9}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Gateways.Business.Tests", "Gateways.Business.Tests\Gateways.Business.Tests.csproj", "{27B2E21A-EF98-43BD-A72C-26D99CA1F3F4}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Gateways.Api.Tests", "Gateways.Api.Tests\Gateways.Api.Tests.csproj", "{5B2334F1-1320-45D0-B1CA-744CA697A809}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down Expand Up @@ -53,10 +53,10 @@ Global
{96CFC1DC-33CE-4CE0-9B5B-6A30F84D67F9}.Debug|Any CPU.Build.0 = Debug|Any CPU
{96CFC1DC-33CE-4CE0-9B5B-6A30F84D67F9}.Release|Any CPU.ActiveCfg = Release|Any CPU
{96CFC1DC-33CE-4CE0-9B5B-6A30F84D67F9}.Release|Any CPU.Build.0 = Release|Any CPU
{27B2E21A-EF98-43BD-A72C-26D99CA1F3F4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{27B2E21A-EF98-43BD-A72C-26D99CA1F3F4}.Debug|Any CPU.Build.0 = Debug|Any CPU
{27B2E21A-EF98-43BD-A72C-26D99CA1F3F4}.Release|Any CPU.ActiveCfg = Release|Any CPU
{27B2E21A-EF98-43BD-A72C-26D99CA1F3F4}.Release|Any CPU.Build.0 = Release|Any CPU
{5B2334F1-1320-45D0-B1CA-744CA697A809}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5B2334F1-1320-45D0-B1CA-744CA697A809}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5B2334F1-1320-45D0-B1CA-744CA697A809}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5B2334F1-1320-45D0-B1CA-744CA697A809}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -68,7 +68,7 @@ Global
{7BF59563-BEFE-4B89-A71D-4531395F594F} = {6FCD5ADE-09B1-49F3-BCF8-07B0B9028815}
{D97B36E6-4D2B-4AC6-BAA9-158738C63223} = {69FD73B1-1BE8-43AA-8AD2-8D5E8363BE81}
{96CFC1DC-33CE-4CE0-9B5B-6A30F84D67F9} = {53DAF1D2-2E7F-4246-8A50-620FAF5B8864}
{27B2E21A-EF98-43BD-A72C-26D99CA1F3F4} = {69FD73B1-1BE8-43AA-8AD2-8D5E8363BE81}
{5B2334F1-1320-45D0-B1CA-744CA697A809} = {53DAF1D2-2E7F-4246-8A50-620FAF5B8864}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {8308257D-9973-436F-A44C-B9A279F26849}
Expand Down

0 comments on commit a9ab2c3

Please sign in to comment.