diff options
| author | Kamen Mladenov <kamen.d.mladenov@protonmail.com> | 2021-04-09 19:51:35 +0300 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-04-09 19:51:35 +0300 |
| commit | 233f38915ba0079079233eff55434ef349c05c45 (patch) | |
| tree | 6c5f69017865bcab87355e910c87339453da1406 /src/Services/DevHive.Services.Tests/RoleService.Tests.cs | |
| parent | f4a70c6430db923af9fa9958a11c2d6612cb52cc (diff) | |
| parent | a992357efcf1bc1ece81b95ecee5e05a0b73bfdc (diff) | |
| download | DevHive-0.2.tar DevHive-0.2.tar.gz DevHive-0.2.zip | |
Merge pull request #28 from Team-Kaleidoscope/devHEADv0.2mainheroku/main
Second stage: Complete
Diffstat (limited to 'src/Services/DevHive.Services.Tests/RoleService.Tests.cs')
| -rw-r--r-- | src/Services/DevHive.Services.Tests/RoleService.Tests.cs | 271 |
1 files changed, 271 insertions, 0 deletions
diff --git a/src/Services/DevHive.Services.Tests/RoleService.Tests.cs b/src/Services/DevHive.Services.Tests/RoleService.Tests.cs new file mode 100644 index 0000000..c286c80 --- /dev/null +++ b/src/Services/DevHive.Services.Tests/RoleService.Tests.cs @@ -0,0 +1,271 @@ +using System; +using System.Data; +using System.Threading.Tasks; +using AutoMapper; +using DevHive.Data.Interfaces; +using DevHive.Data.Models; +using DevHive.Services.Models.Role; +using DevHive.Services.Services; +using Moq; +using NUnit.Framework; + +namespace DevHive.Services.Tests +{ + [TestFixture] + public class RoleServiceTests + { + private Mock<IRoleRepository> _roleRepositoryMock; + private Mock<IMapper> _mapperMock; + private RoleService _roleService; + + #region SetUps + [SetUp] + public void Setup() + { + this._roleRepositoryMock = new Mock<IRoleRepository>(); + this._mapperMock = new Mock<IMapper>(); + this._roleService = new RoleService(this._roleRepositoryMock.Object, this._mapperMock.Object); + } + #endregion + + #region CreateRole + [Test] + public async Task CreateRole_ReturnsNonEmptyGuid_WhenEntityIsAddedSuccessfully() + { + string roleName = "Gosho Trapov"; + Guid id = Guid.NewGuid(); + CreateRoleServiceModel createRoleServiceModel = new CreateRoleServiceModel + { + Name = roleName + }; + Role role = new() + { + Name = roleName, + Id = id + }; + + this._roleRepositoryMock + .Setup(p => p.DoesNameExist(It.IsAny<string>())) + .ReturnsAsync(false); + this._roleRepositoryMock + .Setup(p => p.AddAsync(It.IsAny<Role>())) + .ReturnsAsync(true); + this._roleRepositoryMock + .Setup(p => p.GetByNameAsync(It.IsAny<string>())) + .ReturnsAsync(role); + this._mapperMock + .Setup(p => p.Map<Role>(It.IsAny<CreateRoleServiceModel>())) + .Returns(role); + + Guid result = await this._roleService.CreateRole(createRoleServiceModel); + + Assert.AreEqual(id, result); + } + + [Test] + public async Task CreateRoley_ReturnsEmptyGuid_WhenEntityIsNotAddedSuccessfully() + { + string roleName = "Gosho Trapov"; + + CreateRoleServiceModel createRoleServiceModel = new CreateRoleServiceModel + { + Name = roleName + }; + Role role = new Role + { + Name = roleName + }; + + this._roleRepositoryMock + .Setup(p => p.DoesNameExist(It.IsAny<string>())) + .ReturnsAsync(false); + this._roleRepositoryMock + .Setup(p => p.AddAsync(It.IsAny<Role>())) + .ReturnsAsync(false); + this._mapperMock + .Setup(p => p.Map<Role>(It.IsAny<CreateRoleServiceModel>())) + .Returns(role); + + Guid result = await this._roleService.CreateRole(createRoleServiceModel); + + Assert.IsTrue(result == Guid.Empty); + } + + [Test] + public void CreateTechnology_ThrowsArgumentException_WhenEntityAlreadyExists() + { + string exceptionMessage = "Role already exists!"; + string roleName = "Gosho Trapov"; + + CreateRoleServiceModel createRoleServiceModel = new CreateRoleServiceModel + { + Name = roleName + }; + + this._roleRepositoryMock + .Setup(p => p.DoesNameExist(It.IsAny<string>())) + .ReturnsAsync(true); + + Exception ex = Assert.ThrowsAsync<DuplicateNameException>(() => this._roleService.CreateRole(createRoleServiceModel)); + + // Assert.AreEqual(exceptionMessage, ex.Message, "Incorecct exception message"); + } + #endregion + + #region GetRoleById + [Test] + public async Task GetRoleById_ReturnsTheRole_WhenItExists() + { + Guid id = Guid.NewGuid(); + string name = "Gosho Trapov"; + Role role = new Role + { + Name = name + }; + RoleServiceModel roleServiceModel = new RoleServiceModel + { + Name = name + }; + + this._roleRepositoryMock + .Setup(p => p.GetByIdAsync(It.IsAny<Guid>())) + .ReturnsAsync(role); + this._mapperMock + .Setup(p => p.Map<RoleServiceModel>(It.IsAny<Role>())) + .Returns(roleServiceModel); + + RoleServiceModel result = await this._roleService.GetRoleById(id); + + Assert.AreEqual(name, result.Name); + } + + [Test] + public void GetRoleById_ThrowsException_WhenRoleDoesNotExist() + { + string exceptionMessage = "Role does not exist!"; + Guid id = Guid.NewGuid(); + this._roleRepositoryMock + .Setup(p => p.GetByIdAsync(It.IsAny<Guid>())) + .Returns(Task.FromResult<Role>(null)); + + Exception ex = Assert.ThrowsAsync<ArgumentNullException>(() => this._roleService.GetRoleById(id)); + + // Assert.AreEqual(exceptionMessage, ex.Message, "Incorecct exception message"); + } + #endregion + + #region UpdateRole + [Test] + [TestCase(true)] + [TestCase(false)] + public async Task UpdateRole_ReturnsIfUpdateIsSuccessfull_WhenRoleExistsy(bool shouldPass) + { + string name = "Gosho Trapov"; + Guid id = Guid.NewGuid(); + Role role = new Role + { + Name = name, + Id = id + }; + UpdateRoleServiceModel updateRoleServiceModel = new UpdateRoleServiceModel + { + Name = name, + }; + + this._roleRepositoryMock + .Setup(p => p.DoesRoleExist(It.IsAny<Guid>())) + .ReturnsAsync(true); + this._roleRepositoryMock + .Setup(p => p.DoesNameExist(It.IsAny<string>())) + .ReturnsAsync(false); + this._roleRepositoryMock + .Setup(p => p.EditAsync(It.IsAny<Guid>(), It.IsAny<Role>())) + .ReturnsAsync(shouldPass); + this._mapperMock + .Setup(p => p.Map<Role>(It.IsAny<UpdateRoleServiceModel>())) + .Returns(role); + + bool result = await this._roleService.UpdateRole(updateRoleServiceModel); + + Assert.AreEqual(shouldPass, result); + } + + [Test] + public void UpdateRole_ThrowsException_WhenRoleDoesNotExist() + { + string exceptionMessage = "Role does not exist!"; + UpdateRoleServiceModel updateRoleServiceModel = new UpdateRoleServiceModel + { + }; + + this._roleRepositoryMock + .Setup(p => p.DoesRoleExist(It.IsAny<Guid>())) + .ReturnsAsync(false); + + Exception ex = Assert.ThrowsAsync<ArgumentNullException>(() => this._roleService.UpdateRole(updateRoleServiceModel)); + + // Assert.AreEqual(exceptionMessage, ex.Message, "Incorecct exception message"); + } + + [Test] + public void UpdateRole_ThrowsException_WhenRoleNameAlreadyExists() + { + string exceptionMessage = "Role name already exists!"; + UpdateRoleServiceModel updateRoleServiceModel = new UpdateRoleServiceModel + { + }; + + this._roleRepositoryMock + .Setup(p => p.DoesRoleExist(It.IsAny<Guid>())) + .ReturnsAsync(true); + this._roleRepositoryMock + .Setup(p => p.DoesNameExist(It.IsAny<string>())) + .ReturnsAsync(true); + + Exception ex = Assert.ThrowsAsync<DuplicateNameException>(() => this._roleService.UpdateRole(updateRoleServiceModel)); + + // Assert.AreEqual(exceptionMessage, ex.Message, "Incorecct exception message"); + } + #endregion + + #region DeleteRole + [Test] + [TestCase(true)] + [TestCase(false)] + public async Task DeleteRole_ShouldReturnIfDeletionIsSuccessfull_WhenRoleExists(bool shouldPass) + { + Guid id = Guid.NewGuid(); + Role role = new Role(); + + this._roleRepositoryMock + .Setup(p => p.DoesRoleExist(It.IsAny<Guid>())) + .ReturnsAsync(true); + this._roleRepositoryMock + .Setup(p => p.GetByIdAsync(It.IsAny<Guid>())) + .ReturnsAsync(role); + this._roleRepositoryMock + .Setup(p => p.DeleteAsync(It.IsAny<Role>())) + .ReturnsAsync(shouldPass); + + bool result = await this._roleService.DeleteRole(id); + + Assert.AreEqual(shouldPass, result); + } + + [Test] + public void DeleteRole_ThrowsException_WhenRoleDoesNotExist() + { + string exceptionMessage = "Role does not exist!"; + Guid id = Guid.NewGuid(); + + this._roleRepositoryMock + .Setup(p => p.DoesRoleExist(It.IsAny<Guid>())) + .ReturnsAsync(false); + + Exception ex = Assert.ThrowsAsync<ArgumentNullException>(() => this._roleService.DeleteRole(id)); + + // Assert.AreEqual(exceptionMessage, ex.Message, "Incorecct exception message"); + } + #endregion + } +} |
