using System; using System.Threading.Tasks; using AutoMapper; using DevHive.Data.Interfaces; using DevHive.Data.Models; using DevHive.Services.Models.Comment; using DevHive.Services.Services; using Moq; using NUnit.Framework; namespace DevHive.Services.Tests { [TestFixture] public class CommentServiceTests { private const string MESSAGE = "Gosho Trapov"; private Mock _userRepositoryMock; private Mock _postRepositoryMock; private Mock _commentRepositoryMock; private Mock _mapperMock; private CommentService _commentService; #region Setup [SetUp] public void Setup() { this._userRepositoryMock = new Mock(); this._postRepositoryMock = new Mock(); this._commentRepositoryMock = new Mock(); this._mapperMock = new Mock(); this._commentService = new CommentService(this._userRepositoryMock.Object, this._postRepositoryMock.Object, this._commentRepositoryMock.Object, this._mapperMock.Object); } #endregion #region AddComment [Test] public async Task AddComment_ReturnsNonEmptyGuid_WhenEntityIsAddedSuccessfully() { Guid id = Guid.NewGuid(); User creator = new() { Id = Guid.NewGuid() }; CreateCommentServiceModel createCommentServiceModel = new() { Message = MESSAGE }; Comment comment = new() { Message = MESSAGE, Id = id, }; this._commentRepositoryMock .Setup(p => p.AddAsync(It.IsAny())) .Returns(Task.FromResult(true)); this._commentRepositoryMock .Setup(p => p.GetCommentByIssuerAndTimeCreatedAsync(It.IsAny(), It.IsAny())) .Returns(Task.FromResult(comment)); this._postRepositoryMock .Setup(p => p.DoesPostExist(It.IsAny())) .Returns(Task.FromResult(true)); this._userRepositoryMock .Setup(p => p.GetByIdAsync(It.IsAny())) .Returns(Task.FromResult(creator)); this._mapperMock .Setup(p => p.Map(It.IsAny())) .Returns(comment); Guid result = await this._commentService.AddComment(createCommentServiceModel); Assert.AreEqual(id, result); } [Test] public async Task AddComment_ReturnsEmptyGuid_WhenEntityIsNotAddedSuccessfully() { CreateCommentServiceModel createCommentServiceModel = new() { Message = MESSAGE }; Comment comment = new() { Message = MESSAGE, }; this._commentRepositoryMock .Setup(p => p.AddAsync(It.IsAny())) .Returns(Task.FromResult(false)); this._postRepositoryMock .Setup(p => p.DoesPostExist(It.IsAny())) .Returns(Task.FromResult(true)); this._mapperMock .Setup(p => p.Map(It.IsAny())) .Returns(comment); Guid result = await this._commentService.AddComment(createCommentServiceModel); Assert.IsTrue(result == Guid.Empty); } [Test] public void AddComment_ThrowsException_WhenPostDoesNotExist() { const string EXCEPTION_MESSAGE = "Post does not exist!"; CreateCommentServiceModel createCommentServiceModel = new() { Message = MESSAGE }; Exception ex = Assert.ThrowsAsync(() => this._commentService.AddComment(createCommentServiceModel), "AddComment does not throw excpeion when the post does not exist"); Assert.AreEqual(EXCEPTION_MESSAGE, ex.Message, "Incorecct exception message"); } #endregion #region GetCommentById [Test] public async Task GetCommentById_ReturnsTheComment_WhenItExists() { Guid creatorId = new(); User creator = new() { Id = creatorId }; Comment comment = new() { Message = MESSAGE, Creator = creator }; ReadCommentServiceModel commentServiceModel = new() { Message = MESSAGE }; User user = new() { Id = creatorId, }; this._commentRepositoryMock .Setup(p => p.GetByIdAsync(It.IsAny())) .Returns(Task.FromResult(comment)); this._userRepositoryMock .Setup(p => p.GetByIdAsync(It.IsAny())) .Returns(Task.FromResult(user)); this._mapperMock .Setup(p => p.Map(It.IsAny())) .Returns(commentServiceModel); ReadCommentServiceModel result = await this._commentService.GetCommentById(Guid.NewGuid()); Assert.AreEqual(MESSAGE, result.Message); } [Test] public void GetCommentById_ThorwsException_WhenTheUserDoesNotExist() { const string EXCEPTION_MESSAGE = "The user does not exist"; Guid creatorId = new(); User creator = new() { Id = creatorId }; Comment comment = new() { Message = MESSAGE, Creator = creator }; this._commentRepositoryMock .Setup(p => p.GetByIdAsync(It.IsAny())) .Returns(Task.FromResult(comment)); Exception ex = Assert.ThrowsAsync(() => this._commentService.GetCommentById(Guid.NewGuid()), "GetCommentById does not throw exception when the user does not exist"); Assert.AreEqual(EXCEPTION_MESSAGE, ex.Message); } [Test] public void GetCommentById_ThrowsException_WhenCommentDoesNotExist() { string exceptionMessage = "The comment does not exist"; Guid creatorId = new(); User user = new() { Id = creatorId, }; this._commentRepositoryMock .Setup(p => p.GetByIdAsync(It.IsAny())) .Returns(Task.FromResult(null)); this._userRepositoryMock .Setup(p => p.GetByIdAsync(It.IsAny())) .Returns(Task.FromResult(user)); Exception ex = Assert.ThrowsAsync(() => this._commentService.GetCommentById(Guid.NewGuid())); Assert.AreEqual(exceptionMessage, ex.Message, "Incorecct exception message"); } #endregion #region UpdateComment [Test] public async Task UpdateComment_ReturnsTheIdOfTheComment_WhenUpdatedSuccessfully() { Guid id = Guid.NewGuid(); Comment comment = new() { Id = id, Message = MESSAGE }; UpdateCommentServiceModel updateCommentServiceModel = new() { CommentId = id, NewMessage = MESSAGE }; this._commentRepositoryMock .Setup(p => p.DoesCommentExist(It.IsAny())) .Returns(Task.FromResult(true)); this._commentRepositoryMock .Setup(p => p.EditAsync(It.IsAny(), It.IsAny())) .Returns(Task.FromResult(true)); this._commentRepositoryMock .Setup(p => p.GetByIdAsync(It.IsAny())) .Returns(Task.FromResult(comment)); this._mapperMock .Setup(p => p.Map(It.IsAny())) .Returns(comment); Guid result = await this._commentService.UpdateComment(updateCommentServiceModel); Assert.AreEqual(updateCommentServiceModel.CommentId, result); } [Test] public async Task UpdateComment_ReturnsEmptyId_WhenTheCommentIsNotUpdatedSuccessfully() { Comment comment = new() { Message = MESSAGE }; UpdateCommentServiceModel updateCommentServiceModel = new() { CommentId = Guid.NewGuid(), NewMessage = MESSAGE }; this._commentRepositoryMock .Setup(p => p.DoesCommentExist(It.IsAny())) .Returns(Task.FromResult(true)); this._commentRepositoryMock .Setup(p => p.EditAsync(It.IsAny(), It.IsAny())) .Returns(Task.FromResult(false)); this._mapperMock .Setup(p => p.Map(It.IsAny())) .Returns(comment); Guid result = await this._commentService.UpdateComment(updateCommentServiceModel); Assert.AreEqual(Guid.Empty, result); } [Test] public void UpdateComment_ThrowsArgumentException_WhenCommentDoesNotExist() { string exceptionMessage = "Comment does not exist!"; UpdateCommentServiceModel updateCommentServiceModel = new() { }; this._commentRepositoryMock .Setup(p => p.DoesCommentExist(It.IsAny())) .Returns(Task.FromResult(false)); Exception ex = Assert.ThrowsAsync(() => this._commentService.UpdateComment(updateCommentServiceModel)); Assert.AreEqual(exceptionMessage, ex.Message, "Incorecct exception message"); } #endregion #region DeleteComment [Test] [TestCase(true)] [TestCase(false)] public async Task DeleteComment_ShouldReturnIfDeletionIsSuccessfull_WhenCommentExists(bool shouldPass) { Guid id = new(); Comment comment = new(); this._commentRepositoryMock .Setup(p => p.DoesCommentExist(It.IsAny())) .Returns(Task.FromResult(true)); this._commentRepositoryMock .Setup(p => p.GetByIdAsync(It.IsAny())) .Returns(Task.FromResult(comment)); this._commentRepositoryMock .Setup(p => p.DeleteAsync(It.IsAny())) .Returns(Task.FromResult(shouldPass)); bool result = await this._commentService.DeleteComment(id); Assert.AreEqual(shouldPass, result); } [Test] public void DeleteComment_ThrowsException_WhenCommentDoesNotExist() { string exceptionMessage = "Comment does not exist!"; Guid id = new(); this._commentRepositoryMock .Setup(p => p.DoesCommentExist(It.IsAny())) .Returns(Task.FromResult(false)); Exception ex = Assert.ThrowsAsync(() => this._commentService.DeleteComment(id)); Assert.AreEqual(exceptionMessage, ex.Message, "Incorecct exception message"); } #endregion } }