using FluentAssertions; using Microsoft.EntityFrameworkCore; using Moq; using NUnit.Framework; using Strata.ContinuousImprovement.Api.Controllers; using Strata.ContinuousImprovement.Api.Test.Unit.Api.Generics; using Strata.ContinuousImprovement.Biz.Generics; using Strata.ContinuousImprovement.Biz.OpportunityComments; using Strata.ContinuousImprovement.Biz.Test.Unit.Utilities; using Strata.ContinuousImprovement.JazzEntityFrameworkStub; using Strata.CoreLib.Claims.Extensions; using System; using System.Linq; using System.Threading; using System.Threading.Tasks; using static Strata.ContinuousImprovement.Api.Controllers.CommentsController; namespace Strata.ContinuousImprovement.Api.Test.Unit.Controllers { [TestFixture, Category("Unit"), Category("Controller")] public class CommentsControllerTests { [TestCase(true, TestName = "Get Comments Global")] [TestCase(false, TestName = "Get Comments Not Global")] public async Task TestGetComments(bool isGlobal) { var jeff = new JazzEntityFrameworkFactory(); var dbContext = await jeff.CreateCentralDbContextAsync(nameof(CommentsControllerTests), CancellationToken.None); var comments = await dbContext.Comments.ToListAsync(CancellationToken.None); var clientClaimsPrincipalAccessor = TestUtilities.GetClaimsPrincipalAccessor(); var service = Mock.Of(); Mock.Get(service) .Setup(s => s.GetCommentsAsync(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())) .Returns(Task.FromResult(comments.Where(c => c.OpportunityId == 1).AsEnumerable())); var controller = new CommentsController(service, clientClaimsPrincipalAccessor); var options = new CommentOptions { IsGlobal = isGlobal, OpportunityId = 1L, OpportunityType = $"{(int)OpportunityTypes.ChargeCode}" }; var result = await controller.GetComments(options, CancellationToken.None); result.Should().NotBeEmpty(); } [TestCase(true, true, TestName = "Add Comment Global")] [TestCase(false, true, TestName = "Add Comment Not Global")] [TestCase(true, false, TestName = "Add No Comment Global")] [TestCase(false, false, TestName = "Add No Comment Not Global")] public async Task TestAddComments(bool isGlobal, bool hasComment) { var jeff = new JazzEntityFrameworkFactory(); var dbContext = await jeff.CreateCentralDbContextAsync(nameof(CommentsControllerTests), CancellationToken.None); var comments = await dbContext.Comments.ToListAsync(CancellationToken.None); var newComment = new NewComment { IsGlobal = isGlobal, OpportunityId = 1L, OpportunityType = $"{(int)OpportunityTypes.ChargeCode}", CommentDate = DateTimeOffset.Now, Comment = hasComment ? "This is a new comment" : "" }; var expected = newComment.Map(); var clientClaimsPrincipalAccessor = TestUtilities.GetClaimsPrincipalAccessor(); var user = clientClaimsPrincipalAccessor.GetCurrentClaimsPrincipal(); var name = user.GetUserFirstName(); var service = Mock.Of(); Mock.Get(service) .Setup(s => s.AddCommentAsync(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())) .Returns(Task.FromResult(expected)); var controller = new CommentsController(service, clientClaimsPrincipalAccessor); var result = await controller.Create(newComment, CancellationToken.None); if (!hasComment) { result.Should().Be(default); } else { result.Should().BeEquivalentTo(expected); } } } }