-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tests: implement unit tests for gateways controller
- Loading branch information
Showing
6 changed files
with
267 additions
and
7 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
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
232
Gateways.Api.Tests/Controllers/GatewaysControllerTests.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,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); | ||
} | ||
} |
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
File renamed without changes.
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