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 { get; set; } private Mock MapperMock { get; set; } private TechnologyService TechnologyService { get; set; } #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())).Returns(Task.FromResult(false)); this.TechnologyRepositoryMock.Setup(p => p.AddAsync(It.IsAny())).Returns(Task.FromResult(true)); this.TechnologyRepositoryMock.Setup(p => p.GetByNameAsync(It.IsAny())).Returns(Task.FromResult(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())).Returns(Task.FromResult(false)); this.TechnologyRepositoryMock.Setup(p => p.AddAsync(It.IsAny())).Returns(Task.FromResult(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 }; Technology technology = new() { Name = technologyName }; this.TechnologyRepositoryMock.Setup(p => p.DoesTechnologyNameExistAsync(It.IsAny())).Returns(Task.FromResult(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 = new Guid(); string name = "Gosho Trapov"; Technology technology = new() { Name = name }; ReadTechnologyServiceModel readTechnologyServiceModel = new() { Name = name }; this.TechnologyRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny())).Returns(Task.FromResult(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 = new Guid(); 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())).Returns(Task.FromResult(true)); this.TechnologyRepositoryMock.Setup(p => p.DoesTechnologyNameExistAsync(It.IsAny())).Returns(Task.FromResult(false)); this.TechnologyRepositoryMock.Setup(p => p.EditAsync(It.IsAny(), It.IsAny())).Returns(Task.FromResult(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())).Returns(Task.FromResult(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())).Returns(Task.FromResult(true)); this.TechnologyRepositoryMock.Setup(p => p.DoesTechnologyNameExistAsync(It.IsAny())).Returns(Task.FromResult(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 = new Guid(); Technology technology = new Technology(); this.TechnologyRepositoryMock.Setup(p => p.DoesTechnologyExistAsync(It.IsAny())).Returns(Task.FromResult(true)); this.TechnologyRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny())).Returns(Task.FromResult(technology)); this.TechnologyRepositoryMock.Setup(p => p.DeleteAsync(It.IsAny())).Returns(Task.FromResult(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 = new Guid(); this.TechnologyRepositoryMock.Setup(p => p.DoesTechnologyExistAsync(It.IsAny())).Returns(Task.FromResult(false)); Exception ex = Assert.ThrowsAsync(() => this.TechnologyService.DeleteTechnology(id)); Assert.AreEqual(exceptionMessage, ex.Message, "Incorecct exception message"); } #endregion } }