using AutoMapper; using DevHive.Data.Interfaces; using DevHive.Data.Models; using DevHive.Services.Models.Technology; using DevHive.Services.Services; using Moq; using NUnit.Framework; using System; using System.Collections.Generic; using System.Threading.Tasks; namespace DevHive.Services.Tests { [TestFixture] public class TechnologyServicesTests { private Mock _technologyRepositoryMock; private Mock _mapperMock; private TechnologyService _technologyService; #region SetUps [SetUp] public void Setup() { this._technologyRepositoryMock = new Mock(); this._mapperMock = new Mock(); this._technologyService = new TechnologyService(this._technologyRepositoryMock.Object, this._mapperMock.Object); } #endregion #region CreateTechnology [Test] public async Task CreateTechnology_ReturnsNonEmptyGuid_WhenEntityIsAddedSuccessfully() { string technologyName = "Gosho Trapov"; Guid id = Guid.NewGuid(); CreateTechnologyServiceModel createTechnologyServiceModel = new() { Name = technologyName }; Technology technology = new() { Name = technologyName, Id = id }; this._technologyRepositoryMock .Setup(p => p.DoesTechnologyNameExistAsync(It.IsAny())) .ReturnsAsync(false); this._technologyRepositoryMock .Setup(p => p.AddAsync(It.IsAny())) .ReturnsAsync(true); this._technologyRepositoryMock .Setup(p => p.GetByNameAsync(It.IsAny())) .ReturnsAsync(technology); this._mapperMock .Setup(p => p.Map(It.IsAny())) .Returns(technology); Guid result = await this._technologyService.CreateTechnology(createTechnologyServiceModel); Assert.AreEqual(id, result); } [Test] public async Task CreateTechnology_ReturnsEmptyGuid_WhenEntityIsNotAddedSuccessfully() { string technologyName = "Gosho Trapov"; CreateTechnologyServiceModel createTechnologyServiceModel = new() { Name = technologyName }; Technology technology = new() { Name = technologyName }; this._technologyRepositoryMock .Setup(p => p.DoesTechnologyNameExistAsync(It.IsAny())) .ReturnsAsync(false); this._technologyRepositoryMock .Setup(p => p.AddAsync(It.IsAny())) .ReturnsAsync(false); this._mapperMock .Setup(p => p.Map(It.IsAny())) .Returns(technology); Guid result = await this._technologyService.CreateTechnology(createTechnologyServiceModel); Assert.IsTrue(result == Guid.Empty); } [Test] public void CreateTechnology_ThrowsArgumentException_WhenEntityAlreadyExists() { string exceptionMessage = "Technology already exists!"; string technologyName = "Gosho Trapov"; CreateTechnologyServiceModel createTechnologyServiceModel = new() { Name = technologyName }; this._technologyRepositoryMock .Setup(p => p.DoesTechnologyNameExistAsync(It.IsAny())) .ReturnsAsync(true); Exception ex = Assert.ThrowsAsync(() => this._technologyService.CreateTechnology(createTechnologyServiceModel)); Assert.AreEqual(exceptionMessage, ex.Message, "Incorecct exception message"); } #endregion #region GetTechnologyById [Test] public async Task GetTechnologyById_ReturnsTheTechnology_WhenItExists() { Guid id = Guid.NewGuid(); string name = "Gosho Trapov"; Technology technology = new() { Name = name }; ReadTechnologyServiceModel readTechnologyServiceModel = new() { Name = name }; this._technologyRepositoryMock .Setup(p => p.GetByIdAsync(It.IsAny())) .ReturnsAsync(technology); this._mapperMock .Setup(p => p.Map(It.IsAny())) .Returns(readTechnologyServiceModel); ReadTechnologyServiceModel result = await this._technologyService.GetTechnologyById(id); Assert.AreEqual(name, result.Name); } [Test] public void GetTechnologyById_ThrowsException_WhenTechnologyDoesNotExist() { string exceptionMessage = "The technology does not exist"; Guid id = Guid.NewGuid(); this._technologyRepositoryMock .Setup(p => p.GetByIdAsync(It.IsAny())) .Returns(Task.FromResult(null)); Exception ex = Assert.ThrowsAsync(() => this._technologyService.GetTechnologyById(id)); Assert.AreEqual(exceptionMessage, ex.Message, "Incorecct exception message"); } #endregion #region GetTechnologies [Test] public void GetTechnologies_ReturnsAllLanguages_IfAnyExist() { ReadTechnologyServiceModel firstTechnology = new ReadTechnologyServiceModel(); ReadTechnologyServiceModel secondTechnology = new ReadTechnologyServiceModel(); HashSet technologies = new HashSet(); technologies.Add(firstTechnology); technologies.Add(secondTechnology); this._technologyRepositoryMock .Setup(p => p.GetTechnologies()) .Returns(new HashSet()); this._mapperMock .Setup(p => p.Map>(It.IsAny>())) .Returns(technologies); HashSet result = this._technologyService.GetTechnologies(); Assert.GreaterOrEqual(2, result.Count, "GetTechnologies does not return all technologies"); } [Test] public void GetLanguages_ReturnsEmptyHashSet_IfNoLanguagesExist() { this._technologyRepositoryMock .Setup(p => p.GetTechnologies()) .Returns(new HashSet()); this._mapperMock .Setup(p => p.Map>(It.IsAny>())) .Returns(new HashSet()); HashSet result = this._technologyService.GetTechnologies(); Assert.IsEmpty(result, "GetTechnologies does not return empty string when no technologies exist"); } #endregion #region UpdateTechnology [Test] [TestCase(true)] [TestCase(false)] public async Task UpdateTechnology_ReturnsIfUpdateIsSuccessfull_WhenTechnologyExistsy(bool shouldPass) { string name = "Gosho Trapov"; Guid id = Guid.NewGuid(); Technology technology = new Technology { Name = name, Id = id }; UpdateTechnologyServiceModel updatetechnologyServiceModel = new UpdateTechnologyServiceModel { Name = name, }; this._technologyRepositoryMock .Setup(p => p.DoesTechnologyExistAsync(It.IsAny())) .ReturnsAsync(true); this._technologyRepositoryMock .Setup(p => p.DoesTechnologyNameExistAsync(It.IsAny())) .ReturnsAsync(false); this._technologyRepositoryMock .Setup(p => p.EditAsync(It.IsAny(), It.IsAny())) .ReturnsAsync(shouldPass); this._mapperMock .Setup(p => p.Map(It.IsAny())) .Returns(technology); bool result = await this._technologyService.UpdateTechnology(updatetechnologyServiceModel); Assert.AreEqual(shouldPass, result); } [Test] public void UpdateTechnology_ThrowsException_WhenTechnologyDoesNotExist() { string exceptionMessage = "Technology does not exist!"; UpdateTechnologyServiceModel updateTechnologyServiceModel = new UpdateTechnologyServiceModel { }; this._technologyRepositoryMock .Setup(p => p.DoesTechnologyExistAsync(It.IsAny())) .ReturnsAsync(false); Exception ex = Assert.ThrowsAsync(() => this._technologyService.UpdateTechnology(updateTechnologyServiceModel)); Assert.AreEqual(exceptionMessage, ex.Message, "Incorecct exception message"); } [Test] public void UpdateTechnology_ThrowsException_WhenTechnologyNameAlreadyExists() { string exceptionMessage = "Technology name already exists!"; UpdateTechnologyServiceModel updateTechnologyServiceModel = new UpdateTechnologyServiceModel { }; this._technologyRepositoryMock .Setup(p => p.DoesTechnologyExistAsync(It.IsAny())) .ReturnsAsync(true); this._technologyRepositoryMock .Setup(p => p.DoesTechnologyNameExistAsync(It.IsAny())) .ReturnsAsync(true); Exception ex = Assert.ThrowsAsync(() => this._technologyService.UpdateTechnology(updateTechnologyServiceModel)); Assert.AreEqual(exceptionMessage, ex.Message, "Incorecct exception message"); } #endregion #region DeleteTechnology [Test] [TestCase(true)] [TestCase(false)] public async Task DeleteTechnology_ShouldReturnIfDeletionIsSuccessfull_WhenTechnologyExists(bool shouldPass) { Guid id = Guid.NewGuid(); Technology technology = new Technology(); this._technologyRepositoryMock .Setup(p => p.DoesTechnologyExistAsync(It.IsAny())) .ReturnsAsync(true); this._technologyRepositoryMock .Setup(p => p.GetByIdAsync(It.IsAny())) .ReturnsAsync(technology); this._technologyRepositoryMock .Setup(p => p.DeleteAsync(It.IsAny())) .ReturnsAsync(shouldPass); bool result = await this._technologyService.DeleteTechnology(id); Assert.AreEqual(shouldPass, result); } [Test] public void DeleteTechnology_ThrowsException_WhenTechnologyDoesNotExist() { string exceptionMessage = "Technology does not exist!"; Guid id = Guid.NewGuid(); this._technologyRepositoryMock .Setup(p => p.DoesTechnologyExistAsync(It.IsAny())) .ReturnsAsync(false); Exception ex = Assert.ThrowsAsync(() => this._technologyService.DeleteTechnology(id)); Assert.AreEqual(exceptionMessage, ex.Message, "Incorecct exception message"); } #endregion } }