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) --- .../DevHive.Web.Tests/PostController.Tests.cs | 231 ++++++++++++++++++++- 1 file changed, 227 insertions(+), 4 deletions(-) (limited to 'src/DevHive.Tests/DevHive.Web.Tests/PostController.Tests.cs') diff --git a/src/DevHive.Tests/DevHive.Web.Tests/PostController.Tests.cs b/src/DevHive.Tests/DevHive.Web.Tests/PostController.Tests.cs index 18ed1b9..ace1cae 100644 --- a/src/DevHive.Tests/DevHive.Web.Tests/PostController.Tests.cs +++ b/src/DevHive.Tests/DevHive.Web.Tests/PostController.Tests.cs @@ -1,12 +1,235 @@ -using System; -using System.Collections.Generic; +using System; using System.Linq; -using System.Text; using System.Threading.Tasks; +using AutoMapper; +using DevHive.Services.Interfaces; +using DevHive.Services.Models.Post.Comment; +using DevHive.Web.Controllers; +using DevHive.Web.Models.Post.Comment; +using Microsoft.AspNetCore.Mvc; +using Moq; +using NUnit.Framework; namespace DevHive.Web.Tests { - class PostController + [TestFixture] + public class PostControllerTests { + const string MESSAGE = "Gosho Trapov"; + private Mock PostServiceMock { get; set; } + private Mock MapperMock { get; set; } + private PostController PostController { get; set; } + + [SetUp] + public void SetUp() + { + this.PostServiceMock = new Mock(); + this.MapperMock = new Mock(); + this.PostController = new PostController(this.PostServiceMock.Object, this.MapperMock.Object); + } + + #region Comment + #region Create + [Test] + public void AddComment_ReturnsOkObjectResult_WhenCommentIsSuccessfullyCreated() + { + CommentWebModel commentWebModel = new CommentWebModel + { + Message = MESSAGE + }; + CreateCommentServiceModel createCommentServiceModel = new CreateCommentServiceModel + { + Message = MESSAGE + }; + Guid id = Guid.NewGuid(); + + this.MapperMock.Setup(p => p.Map(It.IsAny())).Returns(createCommentServiceModel); + this.PostServiceMock.Setup(p => p.AddComment(It.IsAny())).Returns(Task.FromResult(id)); + + IActionResult result = this.PostController.AddComment(commentWebModel).Result; + + Assert.IsInstanceOf(result); + + var splitted = (result as OkObjectResult).Value + .ToString() + .Split('{', '}', '=', ' ') + .Where(x => !string.IsNullOrEmpty(x)) + .ToArray(); + + Guid resultId = Guid.Parse(splitted[1]); + + Assert.AreEqual(id, resultId); + } + + [Test] + public void AddComment_ReturnsBadRequestObjectResult_WhenCommentIsNotCreatedSuccessfully() + { + CommentWebModel commentWebModel = new CommentWebModel + { + Message = MESSAGE + }; + CreateCommentServiceModel createCommentServiceModel = new CreateCommentServiceModel + { + Message = MESSAGE + }; + Guid id = Guid.Empty; + string errorMessage = $"Could not create comment"; + + this.MapperMock.Setup(p => p.Map(It.IsAny())).Returns(createCommentServiceModel); + this.PostServiceMock.Setup(p => p.AddComment(It.IsAny())).Returns(Task.FromResult(id)); + + IActionResult result = this.PostController.AddComment(commentWebModel).Result; + + Assert.IsInstanceOf(result); + + BadRequestObjectResult badRequsetObjectResult = result as BadRequestObjectResult; + string resultMessage = badRequsetObjectResult.Value.ToString(); + + Assert.AreEqual(errorMessage, resultMessage); + } + #endregion + + #region Read + [Test] + public void GetCommentById_ReturnsTheComment_WhenItExists() + { + Guid id = Guid.NewGuid(); + + CommentServiceModel commentServiceModel = new CommentServiceModel + { + Message = MESSAGE + }; + CommentWebModel commentWebModel = new CommentWebModel + { + Message = MESSAGE + }; + + this.PostServiceMock.Setup(p => p.GetCommentById(It.IsAny())).Returns(Task.FromResult(commentServiceModel)); + this.MapperMock.Setup(p => p.Map(It.IsAny())).Returns(commentWebModel); + + IActionResult result = this.PostController.GetCommentById(id).Result; + + Assert.IsInstanceOf(result); + + OkObjectResult okObjectResult = result as OkObjectResult; + CommentWebModel resultModel = okObjectResult.Value as Models.Post.Comment.CommentWebModel; + + Assert.AreEqual(MESSAGE, resultModel.Message); + } + #endregion + + #region Update + [Test] + public void UpdateComment_ShouldReturnOkResult_WhenCommentIsUpdatedSuccessfully() + { + Guid id = Guid.NewGuid(); + CommentWebModel commentWebModel = new CommentWebModel + { + Message = MESSAGE + }; + UpdateCommentServiceModel updateCommentServiceModel = new UpdateCommentServiceModel + { + Message = MESSAGE + }; + + this.PostServiceMock.Setup(p => p.UpdateComment(It.IsAny())).Returns(Task.FromResult(true)); + this.MapperMock.Setup(p => p.Map(It.IsAny())).Returns(updateCommentServiceModel); + this.PostServiceMock.Setup(p => p.ValidateJwtForComment(It.IsAny(), It.IsAny())).Returns(Task.FromResult(true)); + + IActionResult result = this.PostController.UpdateComment(id, commentWebModel, null).Result; + + Assert.IsInstanceOf(result); + } + + [Test] + public void UpdateComment_ShouldReturnBadObjectResult_WhenCommentIsNotUpdatedSuccessfully() + { + Guid id = Guid.NewGuid(); + string message = "Could not update Comment"; + CommentWebModel commentWebModel = new CommentWebModel + { + Message = MESSAGE + }; + UpdateCommentServiceModel updateCommentServiceModel = new UpdateCommentServiceModel + { + Message = MESSAGE + }; + + this.PostServiceMock.Setup(p => p.UpdateComment(It.IsAny())).Returns(Task.FromResult(false)); + this.MapperMock.Setup(p => p.Map(It.IsAny())).Returns(updateCommentServiceModel); + this.PostServiceMock.Setup(p => p.ValidateJwtForComment(It.IsAny(), It.IsAny())).Returns(Task.FromResult(true)); + + IActionResult result = this.PostController.UpdateComment(id, commentWebModel, null).Result; + Assert.IsInstanceOf(result); + + BadRequestObjectResult badRequestObjectResult = result as BadRequestObjectResult; + string resultModel = badRequestObjectResult.Value.ToString(); + + Assert.AreEqual(message, resultModel); + } + + [Test] + public void UpdateComment_ShouldReturnUnauthorizedResult_WhenJwtIsNotValid() + { + Guid id = Guid.NewGuid(); + + CommentWebModel commentWebModel = new CommentWebModel + { + Message = MESSAGE + }; + + this.PostServiceMock.Setup(p => p.ValidateJwtForComment(It.IsAny(), It.IsAny())).Returns(Task.FromResult(false)); + + IActionResult result = this.PostController.UpdateComment(id, commentWebModel, null).Result; + Assert.IsInstanceOf(result); + } + #endregion + + #region Delete + [Test] + public void DeleteComment_ReturnsOkResult_WhenLanguageIsDeletedSuccessfully() + { + Guid id = Guid.NewGuid(); + + this.PostServiceMock.Setup(p => p.DeleteComment(It.IsAny())).Returns(Task.FromResult(true)); + this.PostServiceMock.Setup(p => p.ValidateJwtForComment(It.IsAny(), It.IsAny())).Returns(Task.FromResult(true)); + + IActionResult result = this.PostController.DeleteComment(id, null).Result; + + Assert.IsInstanceOf(result); + } + + [Test] + public void DeletComment_ReturnsBadRequestObjectResult_WhenLanguageIsNotDeletedSuccessfully() + { + string message = "Could not delete Comment"; + Guid id = Guid.NewGuid(); + + this.PostServiceMock.Setup(p => p.DeleteComment(It.IsAny())).Returns(Task.FromResult(false)); + this.PostServiceMock.Setup(p => p.ValidateJwtForComment(It.IsAny(), It.IsAny())).Returns(Task.FromResult(true)); + + IActionResult result = this.PostController.DeleteComment(id, null).Result; + + Assert.IsInstanceOf(result); + + BadRequestObjectResult badRequestObjectResult = result as BadRequestObjectResult; + string resultModel = badRequestObjectResult.Value.ToString(); + + Assert.AreEqual(message, resultModel); + } + + [Test] + public void DeletComment_ReturnsUnauthorizedResult_WhenJwtIsNotValid() + { + Guid id = Guid.NewGuid(); + + this.PostServiceMock.Setup(p => p.ValidateJwtForComment(It.IsAny(), It.IsAny())).Returns(Task.FromResult(false)); + + IActionResult result = this.PostController.DeleteComment(id, null).Result; + + Assert.IsInstanceOf(result); + } + #endregion + #endregion } } -- cgit v1.2.3