From 092f8fb6a50e41a7a9ecdd0929f271cde4cd05c9 Mon Sep 17 00:00:00 2001 From: Danail Dimitrov Date: Thu, 21 Jan 2021 23:52:44 +0200 Subject: Adding Comment layer unit test(Note: Data is to be refactored no data layer tests are added) --- .../LanguageService.Tests.cs | 4 +- .../DevHive.Services.Tests/PostService.Tests.cs | 189 +++++++++++++++++++++ 2 files changed, 191 insertions(+), 2 deletions(-) create mode 100644 src/DevHive.Tests/DevHive.Services.Tests/PostService.Tests.cs (limited to 'src/DevHive.Tests/DevHive.Services.Tests') diff --git a/src/DevHive.Tests/DevHive.Services.Tests/LanguageService.Tests.cs b/src/DevHive.Tests/DevHive.Services.Tests/LanguageService.Tests.cs index 67f1284..fd4a828 100644 --- a/src/DevHive.Tests/DevHive.Services.Tests/LanguageService.Tests.cs +++ b/src/DevHive.Tests/DevHive.Services.Tests/LanguageService.Tests.cs @@ -104,7 +104,7 @@ namespace DevHive.Services.Tests { Guid id = new Guid(); string name = "Gosho Trapov"; - Language language = new Language() + Language language = new Language { Name = name }; @@ -161,7 +161,7 @@ namespace DevHive.Services.Tests } [Test] - public void UpdateLanguage_ReturnsIfUpdateIsSuccessfull_WhenLanguageExistsy() + public void UpdateLanguage_ThrowsArgumentException_WhenLanguageDoesNotExist() { string exceptionMessage = "Language does not exist!"; UpdateLanguageServiceModel updateTechnologyServiceModel = new UpdateLanguageServiceModel diff --git a/src/DevHive.Tests/DevHive.Services.Tests/PostService.Tests.cs b/src/DevHive.Tests/DevHive.Services.Tests/PostService.Tests.cs new file mode 100644 index 0000000..bf6817b --- /dev/null +++ b/src/DevHive.Tests/DevHive.Services.Tests/PostService.Tests.cs @@ -0,0 +1,189 @@ +using System; +using System.Threading.Tasks; +using AutoMapper; +using DevHive.Data.Interfaces.Repositories; +using DevHive.Data.Models; +using DevHive.Services.Models.Post.Comment; +using DevHive.Services.Services; +using Moq; +using NUnit.Framework; + +namespace DevHive.Services.Tests +{ + [TestFixture] + public class PostServiceTests + { + private const string MESSAGE = "Gosho Trapov"; + private Mock PostRepositoryMock { get; set; } + private Mock UserRepositoryMock { get; set; } + private Mock MapperMock { get; set; } + private PostService PostService { get; set; } + + [SetUp] + public void Setup() + { + this.PostRepositoryMock = new Mock(); + this.MapperMock = new Mock(); + this.UserRepositoryMock = new Mock(); + this.PostService = new PostService(this.PostRepositoryMock.Object, this.UserRepositoryMock.Object, this.MapperMock.Object); + } + + #region Comment + #region Create + [Test] + public async Task AddComment_ReturnsNonEmptyGuid_WhenEntityIsAddedSuccessfully() + { + Guid id = Guid.NewGuid(); + CreateCommentServiceModel createCommentServiceModel = new CreateCommentServiceModel + { + Message = MESSAGE + }; + Comment comment = new Comment + { + Message = MESSAGE, + Id = id + }; + + this.PostRepositoryMock.Setup(p => p.AddCommentAsync(It.IsAny())).Returns(Task.FromResult(true)); + this.PostRepositoryMock.Setup(p => p.GetCommentByIssuerAndTimeCreatedAsync(It.IsAny(), It.IsAny())).Returns(Task.FromResult(comment)); + this.MapperMock.Setup(p => p.Map(It.IsAny())).Returns(comment); + + Guid result = await this.PostService.AddComment(createCommentServiceModel); + + Assert.AreEqual(id, result); + } + + [Test] + public async Task CreateLanguage_ReturnsEmptyGuid_WhenEntityIsNotAddedSuccessfully() + { + CreateCommentServiceModel createCommentServiceModel = new CreateCommentServiceModel + { + Message = MESSAGE + }; + Comment comment = new Comment + { + Message = MESSAGE, + }; + + this.PostRepositoryMock.Setup(p => p.AddCommentAsync(It.IsAny())).Returns(Task.FromResult(false)); + this.MapperMock.Setup(p => p.Map(It.IsAny())).Returns(comment); + + Guid result = await this.PostService.AddComment(createCommentServiceModel); + + Assert.IsTrue(result == Guid.Empty); + } + #endregion + + #region Read + [Test] + public async Task GetCommentById_ReturnsTheComment_WhenItExists() + { + Guid id = new Guid(); + Comment comment = new Comment + { + Message = MESSAGE + }; + CommentServiceModel commentServiceModel = new CommentServiceModel + { + Message = MESSAGE + }; + + this.PostRepositoryMock.Setup(p => p.GetCommentByIdAsync(It.IsAny())).Returns(Task.FromResult(comment)); + this.MapperMock.Setup(p => p.Map(It.IsAny())).Returns(commentServiceModel); + + CommentServiceModel result = await this.PostService.GetCommentById(id); + + Assert.AreEqual(MESSAGE, result.Message); + } + + [Test] + public void GetLanguageById_ThrowsException_WhenLanguageDoesNotExist() + { + string exceptionMessage = "The comment does not exist"; + Guid id = new Guid(); + this.PostRepositoryMock.Setup(p => p.GetCommentByIdAsync(It.IsAny())).Returns(Task.FromResult(null)); + + Exception ex = Assert.ThrowsAsync(() => this.PostService.GetCommentById(id)); + + Assert.AreEqual(exceptionMessage, ex.Message, "Incorecct exception message"); + } + #endregion + + #region Update + [Test] + [TestCase(true)] + [TestCase(false)] + public async Task UpdateComment_ReturnsIfUpdateIsSuccessfull_WhenCommentExistsy(bool shouldPass) + { + Comment comment = new Comment + { + Message = MESSAGE + }; + UpdateCommentServiceModel updateCommentServiceModel = new UpdateCommentServiceModel + { + Message = MESSAGE + }; + + this.PostRepositoryMock.Setup(p => p.DoesCommentExist(It.IsAny())).Returns(Task.FromResult(true)); + this.PostRepositoryMock.Setup(p => p.EditCommentAsync(It.IsAny())).Returns(Task.FromResult(shouldPass)); + this.MapperMock.Setup(p => p.Map(It.IsAny())).Returns(comment); + + bool result = await this.PostService.UpdateComment(updateCommentServiceModel); + + Assert.AreEqual(shouldPass, result); + } + + [Test] + public void UpdateLanguage_ThrowsArgumentException_WhenCommentDoesNotExist() + { + string exceptionMessage = "Comment does not exist!"; + UpdateCommentServiceModel updateCommentServiceModel = new UpdateCommentServiceModel + { + }; + + this.PostRepositoryMock.Setup(p => p.DoesCommentExist(It.IsAny())).Returns(Task.FromResult(false)); + + Exception ex = Assert.ThrowsAsync(() => this.PostService.UpdateComment(updateCommentServiceModel)); + + Assert.AreEqual(exceptionMessage, ex.Message, "Incorecct exception message"); + } + #endregion + + #region Delete + [Test] + [TestCase(true)] + [TestCase(false)] + public async Task DeleteComment_ShouldReturnIfDeletionIsSuccessfull_WhenCommentExists(bool shouldPass) + { + Guid id = new Guid(); + Comment comment = new Comment(); + + this.PostRepositoryMock.Setup(p => p.DoesCommentExist(It.IsAny())).Returns(Task.FromResult(true)); + this.PostRepositoryMock.Setup(p => p.GetCommentByIdAsync(It.IsAny())).Returns(Task.FromResult(comment)); + this.PostRepositoryMock.Setup(p => p.DeleteCommentAsync(It.IsAny())).Returns(Task.FromResult(shouldPass)); + + bool result = await this.PostService.DeleteComment(id); + + Assert.AreEqual(shouldPass, result); + } + + [Test] + public void DeleteLanguage_ThrowsException_WhenLanguageDoesNotExist() + { + string exceptionMessage = "Comment does not exist!"; + Guid id = new Guid(); + + this.PostRepositoryMock.Setup(p => p.DoesCommentExist(It.IsAny())).Returns(Task.FromResult(false)); + + Exception ex = Assert.ThrowsAsync(() => this.PostService.DeleteComment(id)); + + Assert.AreEqual(exceptionMessage, ex.Message, "Incorecct exception message"); + } + #endregion + + #region ValidateJwtForComment + //TO DO: Implement + #endregion + #endregion + } +} -- cgit v1.2.3