From d081118c8ab8b7ff874cb6bb976ac3f87edf7a50 Mon Sep 17 00:00:00 2001 From: OJ Date: Sun, 19 May 2024 13:27:07 +0200 Subject: [PATCH 1/8] Add like handler test --- .../MiniSpace.Services.Comments.sln | 6 ++ .../Contexts/IdentityContext.cs | 3 + .../Commands/Handlers/AddLikeHandlerTest.cs | 102 ++++++++++++++++++ ...ices.Comments.Application.UnitTests.csproj | 33 ++++++ 4 files changed, 144 insertions(+) create mode 100644 MiniSpace.Services.Comments/tests/MiniSpace.Services.Comments.Application.UnitTests/Commands/Handlers/AddLikeHandlerTest.cs create mode 100644 MiniSpace.Services.Comments/tests/MiniSpace.Services.Comments.Application.UnitTests/MiniSpace.Services.Comments.Application.UnitTests.csproj diff --git a/MiniSpace.Services.Comments/MiniSpace.Services.Comments.sln b/MiniSpace.Services.Comments/MiniSpace.Services.Comments.sln index b474e20c8..605629bc0 100644 --- a/MiniSpace.Services.Comments/MiniSpace.Services.Comments.sln +++ b/MiniSpace.Services.Comments/MiniSpace.Services.Comments.sln @@ -11,6 +11,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MiniSpace.Services.Comments EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MiniSpace.Services.Comments.Infrastructure", "src\MiniSpace.Services.Comments.Infrastructure\MiniSpace.Services.Comments.Infrastructure.csproj", "{85941F19-E28E-467B-A338-7D358D32CFC8}" EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MiniSpace.Services.Comments.Application.UnitTests", "tests\MiniSpace.Services.Comments.Application.UnitTests\MiniSpace.Services.Comments.Application.UnitTests.csproj", "{63DF71AF-1D31-4A4D-8127-9B8EB359889D}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -33,6 +35,10 @@ Global {85941F19-E28E-467B-A338-7D358D32CFC8}.Debug|Any CPU.Build.0 = Debug|Any CPU {85941F19-E28E-467B-A338-7D358D32CFC8}.Release|Any CPU.ActiveCfg = Release|Any CPU {85941F19-E28E-467B-A338-7D358D32CFC8}.Release|Any CPU.Build.0 = Release|Any CPU + {63DF71AF-1D31-4A4D-8127-9B8EB359889D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {63DF71AF-1D31-4A4D-8127-9B8EB359889D}.Debug|Any CPU.Build.0 = Debug|Any CPU + {63DF71AF-1D31-4A4D-8127-9B8EB359889D}.Release|Any CPU.ActiveCfg = Release|Any CPU + {63DF71AF-1D31-4A4D-8127-9B8EB359889D}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/MiniSpace.Services.Comments/src/MiniSpace.Services.Comments.Infrastructure/Contexts/IdentityContext.cs b/MiniSpace.Services.Comments/src/MiniSpace.Services.Comments.Infrastructure/Contexts/IdentityContext.cs index 12d43b7d7..259d5ef24 100644 --- a/MiniSpace.Services.Comments/src/MiniSpace.Services.Comments.Infrastructure/Contexts/IdentityContext.cs +++ b/MiniSpace.Services.Comments/src/MiniSpace.Services.Comments.Infrastructure/Contexts/IdentityContext.cs @@ -1,4 +1,7 @@ using MiniSpace.Services.Comments.Application; +using System.Runtime.CompilerServices; + +[assembly: InternalsVisibleTo("MiniSpace.Services.Comments.Application.UnitTests")] namespace MiniSpace.Services.Comments.Infrastructure.Contexts { diff --git a/MiniSpace.Services.Comments/tests/MiniSpace.Services.Comments.Application.UnitTests/Commands/Handlers/AddLikeHandlerTest.cs b/MiniSpace.Services.Comments/tests/MiniSpace.Services.Comments.Application.UnitTests/Commands/Handlers/AddLikeHandlerTest.cs new file mode 100644 index 000000000..727d31020 --- /dev/null +++ b/MiniSpace.Services.Comments/tests/MiniSpace.Services.Comments.Application.UnitTests/Commands/Handlers/AddLikeHandlerTest.cs @@ -0,0 +1,102 @@ +using Xunit; +using Moq; +using System; +using System.Collections.Generic; +using System.Threading.Tasks; +using System.Linq; +using System.Text; +using MiniSpace.Services.Comments.Application.Events; +using MiniSpace.Services.Comments.Application.Exceptions; +using MiniSpace.Services.Comments.Application.Services; +using MiniSpace.Services.Comments.Core.Entities; +using MiniSpace.Services.Comments.Core.Repositories; +using MiniSpace.Services.Comments.Application.Commands.Handlers; +using MiniSpace.Services.Comments.Application.Commands; +using MiniSpace.Services.Comments.Infrastructure.Contexts; +using Convey.CQRS.Commands; +using System.Threading; +using System.Security.Claims; +using FluentAssertions; + +namespace MiniSpace.Services.Comments.Application.UnitTests.Commands.Handlers +{ + public class AddLikeHandlerTest + { + private readonly AddLikeHandler _addLikeHandler; + private readonly Mock _commentRepositoryMock; + private readonly Mock _messageBrokerMock; + private readonly Mock _eventMapperMock; + private readonly Mock _appContextMock; + private readonly Mock _dateTimeProviderMock; + + public AddLikeHandlerTest() + { + _commentRepositoryMock = new Mock(); + _messageBrokerMock = new Mock(); + _eventMapperMock = new Mock(); + _appContextMock = new Mock(); + _dateTimeProviderMock = new Mock(); + _addLikeHandler = new AddLikeHandler(_commentRepositoryMock.Object, _appContextMock.Object, _messageBrokerMock.Object); + } + + [Fact] + public async Task AddLike_WithValidCommentAndAuthorised_ShouldAddLike() + { + // Arrange + var commentId = Guid.NewGuid(); + var comand = new AddLike(commentId); + + var comment = Comment.Create(new AggregateId(commentId), Guid.NewGuid(), CommentContext.Post, Guid.NewGuid(), "Adam", Guid.NewGuid(), "text", DateTime.Now); + + var identityContext = new IdentityContext(Guid.NewGuid().ToString(), "", true, new Dictionary()); + + _appContextMock.Setup(ctx => ctx.Identity).Returns(identityContext); + _commentRepositoryMock.Setup(repo => repo.GetAsync(comment.Id)).ReturnsAsync(comment); + + var cancelationToken = new CancellationToken(); + + // Act + await _addLikeHandler.HandleAsync(comand, cancelationToken); + + // Assert + _commentRepositoryMock.Verify(repo => repo.UpdateAsync(comment), Times.Once()); + _eventMapperMock.Verify(mapper => mapper.MapAll(comment.Events), Times.Once()); + } + + [Fact] + public async Task AddLike_WithInvalidComment_ShouldThrowCommentNotFoundExeption() + { + // Arrange + var commentId = Guid.NewGuid(); + var comand = new AddLike(commentId); + _commentRepositoryMock.Setup(repo => repo.GetAsync(comand.CommentId)).ReturnsAsync((Comment)null); + var cancelationToken = new CancellationToken(); + + // Act & Assert + Func act = async () => await _addLikeHandler.HandleAsync(comand, cancelationToken); + await act.Should().ThrowAsync(); + } + + [Fact] + public async Task AddLike_WithNonPermitedIdentity_ShouldThrowUnauthorizedCommentAccessException() + { + // Arrange + var commentId = Guid.NewGuid(); + var comand = new AddLike(commentId); + + var comment = Comment.Create(new AggregateId(commentId), Guid.NewGuid(), CommentContext.Post, Guid.NewGuid(), "Adam", Guid.NewGuid(), "text", DateTime.Now); + + var identityContext = new IdentityContext(Guid.NewGuid().ToString(), "", false, new Dictionary()); + + _appContextMock.Setup(ctx => ctx.Identity).Returns(identityContext); + _commentRepositoryMock.Setup(repo => repo.GetAsync(comment.Id)).ReturnsAsync(comment); + + var cancelationToken = new CancellationToken(); + + // Act & Assert + Func act = async () => await _addLikeHandler.HandleAsync(comand, cancelationToken); + await act.Should().ThrowAsync(); + + } + } +} diff --git a/MiniSpace.Services.Comments/tests/MiniSpace.Services.Comments.Application.UnitTests/MiniSpace.Services.Comments.Application.UnitTests.csproj b/MiniSpace.Services.Comments/tests/MiniSpace.Services.Comments.Application.UnitTests/MiniSpace.Services.Comments.Application.UnitTests.csproj new file mode 100644 index 000000000..50d2c1525 --- /dev/null +++ b/MiniSpace.Services.Comments/tests/MiniSpace.Services.Comments.Application.UnitTests/MiniSpace.Services.Comments.Application.UnitTests.csproj @@ -0,0 +1,33 @@ + + + + net8.0 + disable + + false + true + + + + + + + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + + + + + + + + + From a30ef3c2b9955dbfd526ea89104df964b9a20e79 Mon Sep 17 00:00:00 2001 From: OJ Date: Sun, 19 May 2024 14:50:28 +0200 Subject: [PATCH 2/8] Create comment Handler Tests --- .../Commands/Handlers/AddLikeHandlerTest.cs | 16 +- .../Handlers/CreateCommentHandlerTest.cs | 231 ++++++++++++++++++ .../Handlers/DeleteCommentHandlerTest.cs | 37 +++ ...ices.Comments.Application.UnitTests.csproj | 3 +- 4 files changed, 272 insertions(+), 15 deletions(-) create mode 100644 MiniSpace.Services.Comments/tests/MiniSpace.Services.Comments.Application.UnitTests/Commands/Handlers/CreateCommentHandlerTest.cs create mode 100644 MiniSpace.Services.Comments/tests/MiniSpace.Services.Comments.Application.UnitTests/Commands/Handlers/DeleteCommentHandlerTest.cs diff --git a/MiniSpace.Services.Comments/tests/MiniSpace.Services.Comments.Application.UnitTests/Commands/Handlers/AddLikeHandlerTest.cs b/MiniSpace.Services.Comments/tests/MiniSpace.Services.Comments.Application.UnitTests/Commands/Handlers/AddLikeHandlerTest.cs index 727d31020..6a196b5d2 100644 --- a/MiniSpace.Services.Comments/tests/MiniSpace.Services.Comments.Application.UnitTests/Commands/Handlers/AddLikeHandlerTest.cs +++ b/MiniSpace.Services.Comments/tests/MiniSpace.Services.Comments.Application.UnitTests/Commands/Handlers/AddLikeHandlerTest.cs @@ -3,9 +3,6 @@ using System; using System.Collections.Generic; using System.Threading.Tasks; -using System.Linq; -using System.Text; -using MiniSpace.Services.Comments.Application.Events; using MiniSpace.Services.Comments.Application.Exceptions; using MiniSpace.Services.Comments.Application.Services; using MiniSpace.Services.Comments.Core.Entities; @@ -13,9 +10,7 @@ using MiniSpace.Services.Comments.Application.Commands.Handlers; using MiniSpace.Services.Comments.Application.Commands; using MiniSpace.Services.Comments.Infrastructure.Contexts; -using Convey.CQRS.Commands; using System.Threading; -using System.Security.Claims; using FluentAssertions; namespace MiniSpace.Services.Comments.Application.UnitTests.Commands.Handlers @@ -25,22 +20,18 @@ public class AddLikeHandlerTest private readonly AddLikeHandler _addLikeHandler; private readonly Mock _commentRepositoryMock; private readonly Mock _messageBrokerMock; - private readonly Mock _eventMapperMock; private readonly Mock _appContextMock; - private readonly Mock _dateTimeProviderMock; public AddLikeHandlerTest() { _commentRepositoryMock = new Mock(); _messageBrokerMock = new Mock(); - _eventMapperMock = new Mock(); _appContextMock = new Mock(); - _dateTimeProviderMock = new Mock(); _addLikeHandler = new AddLikeHandler(_commentRepositoryMock.Object, _appContextMock.Object, _messageBrokerMock.Object); } [Fact] - public async Task AddLike_WithValidCommentAndAuthorised_ShouldAddLike() + public async Task HandleAsync_WithValidCommentAndAuthorised_ShouldAddLike() { // Arrange var commentId = Guid.NewGuid(); @@ -60,11 +51,10 @@ public async Task AddLike_WithValidCommentAndAuthorised_ShouldAddLike() // Assert _commentRepositoryMock.Verify(repo => repo.UpdateAsync(comment), Times.Once()); - _eventMapperMock.Verify(mapper => mapper.MapAll(comment.Events), Times.Once()); } [Fact] - public async Task AddLike_WithInvalidComment_ShouldThrowCommentNotFoundExeption() + public async Task HandleAsync_WithInvalidComment_ShouldThrowCommentNotFoundExeption() { // Arrange var commentId = Guid.NewGuid(); @@ -78,7 +68,7 @@ public async Task AddLike_WithInvalidComment_ShouldThrowCommentNotFoundExeption( } [Fact] - public async Task AddLike_WithNonPermitedIdentity_ShouldThrowUnauthorizedCommentAccessException() + public async Task HandleAsync_WithNonPermitedIdentity_ShouldThrowUnauthorizedCommentAccessException() { // Arrange var commentId = Guid.NewGuid(); diff --git a/MiniSpace.Services.Comments/tests/MiniSpace.Services.Comments.Application.UnitTests/Commands/Handlers/CreateCommentHandlerTest.cs b/MiniSpace.Services.Comments/tests/MiniSpace.Services.Comments.Application.UnitTests/Commands/Handlers/CreateCommentHandlerTest.cs new file mode 100644 index 000000000..d24036a62 --- /dev/null +++ b/MiniSpace.Services.Comments/tests/MiniSpace.Services.Comments.Application.UnitTests/Commands/Handlers/CreateCommentHandlerTest.cs @@ -0,0 +1,231 @@ +using Xunit; +using Moq; +using System; +using System.Collections.Generic; +using System.Threading.Tasks; +using System.Linq; +using System.Text; +using MiniSpace.Services.Comments.Application.Events; +using MiniSpace.Services.Comments.Application.Exceptions; +using MiniSpace.Services.Comments.Application.Services; +using MiniSpace.Services.Comments.Core.Entities; +using MiniSpace.Services.Comments.Core.Repositories; +using MiniSpace.Services.Comments.Application.Commands.Handlers; +using MiniSpace.Services.Comments.Application.Commands; +using MiniSpace.Services.Comments.Infrastructure.Contexts; +using Convey.CQRS.Commands; +using System.Threading; +using System.Security.Claims; +using FluentAssertions; + +namespace MiniSpace.Services.Comments.Application.UnitTests.Commands.Handlers +{ + public class CreateCommentHandlerTest + { + private readonly CreateCommentHandler _createCommentHandler; + private readonly Mock _commentRepositoryMock; + private readonly Mock _studentRepositoryMock; + private readonly Mock _messageBrokerMock; + private readonly Mock _eventMapperMock; + private readonly Mock _appContextMock; + private readonly Mock _dateTimeProviderMock; + + public CreateCommentHandlerTest() + { + _commentRepositoryMock = new Mock(); + _studentRepositoryMock = new Mock(); + _messageBrokerMock = new Mock(); + _eventMapperMock = new Mock(); + _appContextMock = new Mock(); + _dateTimeProviderMock = new Mock(); + _createCommentHandler = new CreateCommentHandler( + _commentRepositoryMock.Object, + _studentRepositoryMock.Object, + _dateTimeProviderMock.Object, + _messageBrokerMock.Object, + _appContextMock.Object + ); + } + + [Fact] + public async Task HandleAsync_WithVaildStudentAndContextNoParent_ShouldNotThrowExeption() + { + // Arrange + var commentId = Guid.NewGuid(); + var contextId = Guid.NewGuid(); + var studentId = Guid.NewGuid(); + var parentId = Guid.Empty; + var command = new CreateComment(commentId, contextId, "Post", studentId, + parentId, "text"); + var cancelationTocken = new CancellationToken(); + + var identityContext = new IdentityContext(studentId.ToString(), "", + true, new Dictionary()); + _appContextMock.Setup(ctx => ctx.Identity).Returns(identityContext); + + _studentRepositoryMock.Setup(repo => repo.ExistsAsync(studentId)) + .ReturnsAsync(true); + + // Act & Assert + Func act = async () => await _createCommentHandler.HandleAsync(command, cancelationTocken); + await act.Should().NotThrowAsync(); + } + + [Fact] + public async Task HandleAsync_WithVaildStudentAndContextandOneParent_ShouldNotThrowExeptionAndUpdateParent() + { + // Arrange + var commentId = Guid.NewGuid(); + var contextId = Guid.NewGuid(); + var studentId = Guid.NewGuid(); + var parentId = Guid.NewGuid(); + var parentComment = Comment.Create(new AggregateId(parentId), contextId, + CommentContext.Post, Guid.NewGuid(), "alex", Guid.Empty, "text", DateTime.Now); + var command = new CreateComment(commentId, contextId, "Post", studentId, + parentId, "text"); + var cancelationTocken = new CancellationToken(); + + var identityContext = new IdentityContext(studentId.ToString(), "", + true, new Dictionary()); + _appContextMock.Setup(ctx => ctx.Identity).Returns(identityContext); + + _studentRepositoryMock.Setup(repo => repo.ExistsAsync(studentId)) + .ReturnsAsync(true); + _commentRepositoryMock.Setup(repo => repo.GetAsync(parentId)) + .ReturnsAsync(parentComment); + + // Act & Assert + Func act = async () => await _createCommentHandler.HandleAsync(command, cancelationTocken); + await act.Should().NotThrowAsync(); + _commentRepositoryMock.Verify(repo => repo.UpdateAsync(parentComment), Times.Once()); + } + + [Fact] + public async Task HandleAsync_WithNonPermitedIdentity_ShouldThrowUnauthorizedCommentAccessException() + { + // Arrange + var commentId = Guid.NewGuid(); + var contextId = Guid.NewGuid(); + var studentId = Guid.NewGuid(); + var parentId = Guid.Empty; + var command = new CreateComment(commentId, contextId, "Post", studentId, + parentId, "text"); + var cancelationTocken = new CancellationToken(); + + var identityContext = new IdentityContext(Guid.NewGuid().ToString(), "", + true, new Dictionary()); + _appContextMock.Setup(ctx => ctx.Identity).Returns(identityContext); + + _studentRepositoryMock.Setup(repo => repo.ExistsAsync(studentId)) + .ReturnsAsync(true); + + // Act & Assert + Func act = async () => await _createCommentHandler.HandleAsync(command, cancelationTocken); + await act.Should().ThrowAsync(); + } + + [Fact] + public async Task HandleAsync_WithWrongStudentId_ShouldThrowStudentNotFoundException() + { + // Arrange + var commentId = Guid.NewGuid(); + var contextId = Guid.NewGuid(); + var studentId = Guid.NewGuid(); + var parentId = Guid.Empty; + var command = new CreateComment(commentId, contextId, "Post", studentId, + parentId, "text"); + var cancelationTocken = new CancellationToken(); + + var identityContext = new IdentityContext(studentId.ToString(), "", + false, new Dictionary()); + _appContextMock.Setup(ctx => ctx.Identity).Returns(identityContext); + + _studentRepositoryMock.Setup(repo => repo.ExistsAsync(studentId)) + .ReturnsAsync(false); + + // Act & Assert + Func act = async () => await _createCommentHandler.HandleAsync(command, cancelationTocken); + await act.Should().ThrowAsync(); + } + + [Fact] + public async Task HandleAsync_WithWrongCommentContextEnum_ShouldThrowInvalidCommentContextEnumException() + { + // Arrange + var commentId = Guid.NewGuid(); + var contextId = Guid.NewGuid(); + var studentId = Guid.NewGuid(); + var parentId = Guid.Empty; + var command = new CreateComment(commentId, contextId, "wrongenumstring", studentId, + parentId, "text"); + var cancelationTocken = new CancellationToken(); + + var identityContext = new IdentityContext(studentId.ToString(), "", + false, new Dictionary()); + _appContextMock.Setup(ctx => ctx.Identity).Returns(identityContext); + + _studentRepositoryMock.Setup(repo => repo.ExistsAsync(studentId)) + .ReturnsAsync(true); + + // Act & Assert + Func act = async () => await _createCommentHandler.HandleAsync(command, cancelationTocken); + await act.Should().ThrowAsync(); + } + + [Fact] + public async Task HandleAsync_WithWrongParentId_ShouldThrowParentCommentNotFoundException() + { + // Arrange + var commentId = Guid.NewGuid(); + var contextId = Guid.NewGuid(); + var studentId = Guid.NewGuid(); + var parentId = Guid.NewGuid(); + var parentComment = Comment.Create(new AggregateId(parentId), contextId, + CommentContext.Post, Guid.NewGuid(), "alex", Guid.Empty, "text", DateTime.Now); + var command = new CreateComment(commentId, contextId, "Post", studentId, + parentId, "text"); + var cancelationTocken = new CancellationToken(); + + var identityContext = new IdentityContext(studentId.ToString(), "", + false, new Dictionary()); + _appContextMock.Setup(ctx => ctx.Identity).Returns(identityContext); + + _studentRepositoryMock.Setup(repo => repo.ExistsAsync(studentId)) + .ReturnsAsync(true); + _commentRepositoryMock.Setup(repo => repo.GetAsync(parentId)) + .ReturnsAsync((Comment)null); + + // Act & Assert + Func act = async () => await _createCommentHandler.HandleAsync(command, cancelationTocken); + await act.Should().ThrowAsync(); + } + + [Fact] + public async Task HandleAsync_WithParentThatHasAParent_ShouldThrowInvalidParentCommentException() + { + // Arrange + var commentId = Guid.NewGuid(); + var contextId = Guid.NewGuid(); + var studentId = Guid.NewGuid(); + var parentId = Guid.NewGuid(); + var parentComment = Comment.Create(new AggregateId(parentId), contextId, + CommentContext.Post, Guid.NewGuid(), "alex", Guid.NewGuid(), "text", DateTime.Now); + var command = new CreateComment(commentId, contextId, "Post", studentId, + parentId, "text"); + var cancelationTocken = new CancellationToken(); + + var identityContext = new IdentityContext(studentId.ToString(), "", + true, new Dictionary()); + _appContextMock.Setup(ctx => ctx.Identity).Returns(identityContext); + + _studentRepositoryMock.Setup(repo => repo.ExistsAsync(studentId)) + .ReturnsAsync(true); + _commentRepositoryMock.Setup(repo => repo.GetAsync(parentId)) + .ReturnsAsync(parentComment); + + // Act & Assert + Func act = async () => await _createCommentHandler.HandleAsync(command, cancelationTocken); + await act.Should().ThrowAsync(); + } + } +} diff --git a/MiniSpace.Services.Comments/tests/MiniSpace.Services.Comments.Application.UnitTests/Commands/Handlers/DeleteCommentHandlerTest.cs b/MiniSpace.Services.Comments/tests/MiniSpace.Services.Comments.Application.UnitTests/Commands/Handlers/DeleteCommentHandlerTest.cs new file mode 100644 index 000000000..75051703f --- /dev/null +++ b/MiniSpace.Services.Comments/tests/MiniSpace.Services.Comments.Application.UnitTests/Commands/Handlers/DeleteCommentHandlerTest.cs @@ -0,0 +1,37 @@ +using Xunit; +using Moq; +using System; +using System.Collections.Generic; +using System.Threading.Tasks; +using MiniSpace.Services.Comments.Application.Exceptions; +using MiniSpace.Services.Comments.Application.Services; +using MiniSpace.Services.Comments.Core.Entities; +using MiniSpace.Services.Comments.Core.Repositories; +using MiniSpace.Services.Comments.Application.Commands.Handlers; +using MiniSpace.Services.Comments.Application.Commands; +using MiniSpace.Services.Comments.Infrastructure.Contexts; +using System.Threading; +using FluentAssertions; + +namespace MiniSpace.Services.Comments.Application.UnitTests.Commands.Handlers +{ + public class DeleteCommentHandlerTest + { + private readonly DeleteCommentHandler _deleteCommentHandler; + private readonly Mock _commentRepositoryMock; + private readonly Mock _messageBrokerMock; + private readonly Mock _appContextMock; + + public DeleteCommentHandlerTest() + { + _commentRepositoryMock = new Mock(); + _messageBrokerMock = new Mock(); + _appContextMock = new Mock(); + _deleteCommentHandler = new DeleteCommentHandler( + _commentRepositoryMock.Object, + _appContextMock.Object, + _messageBrokerMock.Object + ); + } + } +} diff --git a/MiniSpace.Services.Comments/tests/MiniSpace.Services.Comments.Application.UnitTests/MiniSpace.Services.Comments.Application.UnitTests.csproj b/MiniSpace.Services.Comments/tests/MiniSpace.Services.Comments.Application.UnitTests/MiniSpace.Services.Comments.Application.UnitTests.csproj index 50d2c1525..0d888754d 100644 --- a/MiniSpace.Services.Comments/tests/MiniSpace.Services.Comments.Application.UnitTests/MiniSpace.Services.Comments.Application.UnitTests.csproj +++ b/MiniSpace.Services.Comments/tests/MiniSpace.Services.Comments.Application.UnitTests/MiniSpace.Services.Comments.Application.UnitTests.csproj @@ -1,4 +1,4 @@ - + net8.0 @@ -27,7 +27,6 @@ - From 72a023f3399196cffdbb688267d27912818390cb Mon Sep 17 00:00:00 2001 From: OJ Date: Sun, 19 May 2024 15:19:44 +0200 Subject: [PATCH 3/8] delete Comment and Like Handler tests --- .../Handlers/DeleteCommentHandlerTest.cs | 104 ++++++++++++++++ .../Handlers/DeleteLikeHandlerTest.cs | 116 ++++++++++++++++++ 2 files changed, 220 insertions(+) create mode 100644 MiniSpace.Services.Comments/tests/MiniSpace.Services.Comments.Application.UnitTests/Commands/Handlers/DeleteLikeHandlerTest.cs diff --git a/MiniSpace.Services.Comments/tests/MiniSpace.Services.Comments.Application.UnitTests/Commands/Handlers/DeleteCommentHandlerTest.cs b/MiniSpace.Services.Comments/tests/MiniSpace.Services.Comments.Application.UnitTests/Commands/Handlers/DeleteCommentHandlerTest.cs index 75051703f..82333165e 100644 --- a/MiniSpace.Services.Comments/tests/MiniSpace.Services.Comments.Application.UnitTests/Commands/Handlers/DeleteCommentHandlerTest.cs +++ b/MiniSpace.Services.Comments/tests/MiniSpace.Services.Comments.Application.UnitTests/Commands/Handlers/DeleteCommentHandlerTest.cs @@ -33,5 +33,109 @@ public DeleteCommentHandlerTest() _messageBrokerMock.Object ); } + + [Fact] + public async Task HandleAsync_WithVaildStudentAndPermited_ShouldUpdateRepository() + { + // Arrange + var commentId = Guid.NewGuid(); + var comand = new DeleteComment(commentId); + var studentId = Guid.NewGuid(); + + var comment = Comment.Create(new AggregateId(commentId), Guid.NewGuid(), + CommentContext.Post, studentId, "Adam", Guid.NewGuid(), "text", DateTime.Now); + + var identityContext = new IdentityContext(studentId.ToString(), + "", true, new Dictionary()); + + _appContextMock.Setup(ctx => ctx.Identity).Returns(identityContext); + _commentRepositoryMock.Setup(repo => repo.GetAsync(comment.Id)) + .ReturnsAsync(comment); + + var cancelationToken = new CancellationToken(); + + // Act + await _deleteCommentHandler.HandleAsync(comand, cancelationToken); + + // Assert + _commentRepositoryMock.Verify(repo => repo.UpdateAsync(comment), Times.Once()); + } + + [Fact] + public async Task HandleAsync_WithInvalidComment_ShouldThrowCommentNotFoundExeption() + { + // Arrange + var commentId = Guid.NewGuid(); + var comand = new DeleteComment(commentId); + var studentId = Guid.NewGuid(); + + var comment = Comment.Create(new AggregateId(commentId), Guid.NewGuid(), + CommentContext.Post, studentId, "Adam", Guid.NewGuid(), "text", DateTime.Now); + + var identityContext = new IdentityContext(studentId.ToString(), + "", true, new Dictionary()); + + _appContextMock.Setup(ctx => ctx.Identity).Returns(identityContext); + _commentRepositoryMock.Setup(repo => repo.GetAsync(comment.Id)) + .ReturnsAsync((Comment)null); + + var cancelationToken = new CancellationToken(); + + // Act & Assert + Func act = async () => await _deleteCommentHandler.HandleAsync(comand, cancelationToken); + await act.Should().ThrowAsync(); + } + + [Fact] + public async Task HandleAsync_WithAdminNotTheirs_ShouldNotThrowException() + { + // Arrange + var commentId = Guid.NewGuid(); + var comand = new DeleteComment(commentId); + var studentId = Guid.NewGuid(); + + var comment = Comment.Create(new AggregateId(commentId), Guid.NewGuid(), + CommentContext.Post, studentId, "Adam", Guid.NewGuid(), "text", DateTime.Now); + + var identityContext = new IdentityContext(Guid.NewGuid().ToString(), + "Admin", true, new Dictionary()); + + _appContextMock.Setup(ctx => ctx.Identity).Returns(identityContext); + _commentRepositoryMock.Setup(repo => repo.GetAsync(comment.Id)) + .ReturnsAsync(comment); + + var cancelationToken = new CancellationToken(); + + // Act & Assert + Func act = async () => await _deleteCommentHandler.HandleAsync(comand, cancelationToken); + await act.Should().NotThrowAsync(); + + } + + [Fact] + public async Task HandleAsync_WithNotAdminNotTheirs_ShouldThrowUnauthorizedCommentAccessException() + { + // Arrange + var commentId = Guid.NewGuid(); + var comand = new DeleteComment(commentId); + var studentId = Guid.NewGuid(); + + var comment = Comment.Create(new AggregateId(commentId), Guid.NewGuid(), + CommentContext.Post, studentId, "Adam", Guid.NewGuid(), "text", DateTime.Now); + + var identityContext = new IdentityContext(Guid.NewGuid().ToString(), + "", true, new Dictionary()); + + _appContextMock.Setup(ctx => ctx.Identity).Returns(identityContext); + _commentRepositoryMock.Setup(repo => repo.GetAsync(comment.Id)) + .ReturnsAsync(comment); + + var cancelationToken = new CancellationToken(); + + // Act & Assert + Func act = async () => await _deleteCommentHandler.HandleAsync(comand, cancelationToken); + await act.Should().ThrowAsync(); + + } } } diff --git a/MiniSpace.Services.Comments/tests/MiniSpace.Services.Comments.Application.UnitTests/Commands/Handlers/DeleteLikeHandlerTest.cs b/MiniSpace.Services.Comments/tests/MiniSpace.Services.Comments.Application.UnitTests/Commands/Handlers/DeleteLikeHandlerTest.cs new file mode 100644 index 000000000..f61c2da2c --- /dev/null +++ b/MiniSpace.Services.Comments/tests/MiniSpace.Services.Comments.Application.UnitTests/Commands/Handlers/DeleteLikeHandlerTest.cs @@ -0,0 +1,116 @@ +using Xunit; +using Moq; +using System; +using System.Collections.Generic; +using System.Threading.Tasks; +using MiniSpace.Services.Comments.Application.Exceptions; +using MiniSpace.Services.Comments.Application.Services; +using MiniSpace.Services.Comments.Core.Entities; +using MiniSpace.Services.Comments.Core.Repositories; +using MiniSpace.Services.Comments.Application.Commands.Handlers; +using MiniSpace.Services.Comments.Application.Commands; +using MiniSpace.Services.Comments.Infrastructure.Contexts; +using System.Threading; +using FluentAssertions; + +namespace MiniSpace.Services.Comments.Application.UnitTests.Commands.Handlers +{ + public class DeleteLikeHandlerTest + { + private readonly DeleteLikeHandler _deleteLikeHandler; + private readonly Mock _commentRepositoryMock; + private readonly Mock _messageBrokerMock; + private readonly Mock _appContextMock; + + public DeleteLikeHandlerTest() + { + _commentRepositoryMock = new Mock(); + _messageBrokerMock = new Mock(); + _appContextMock = new Mock(); + _deleteLikeHandler = new DeleteLikeHandler( + _commentRepositoryMock.Object, + _appContextMock.Object, + _messageBrokerMock.Object + ); + } + + [Fact] + public async Task HandleAsync_WithVaildStudentAndPermited_ShouldUpdateRepository() + { + // Arrange + var commentId = Guid.NewGuid(); + var comand = new DeleteLike(commentId); + var studentId = Guid.NewGuid(); + + var comment = Comment.Create(new AggregateId(commentId), Guid.NewGuid(), + CommentContext.Post, studentId, "Adam", Guid.NewGuid(), "text", DateTime.Now); + comment.Like(studentId); + + var identityContext = new IdentityContext(studentId.ToString(), + "", true, new Dictionary()); + + _appContextMock.Setup(ctx => ctx.Identity).Returns(identityContext); + _commentRepositoryMock.Setup(repo => repo.GetAsync(comment.Id)) + .ReturnsAsync(comment); + + var cancelationToken = new CancellationToken(); + + // Act + await _deleteLikeHandler.HandleAsync(comand, cancelationToken); + + // Assert + _commentRepositoryMock.Verify(repo => repo.UpdateAsync(comment), Times.Once()); + } + + [Fact] + public async Task HandleAsync_WithInvalidComment_ShouldThrowCommentNotFoundExeption() + { + // Arrange + var commentId = Guid.NewGuid(); + var comand = new DeleteLike(commentId); + var studentId = Guid.NewGuid(); + + var comment = Comment.Create(new AggregateId(commentId), Guid.NewGuid(), + CommentContext.Post, studentId, "Adam", Guid.NewGuid(), "text", DateTime.Now); + + var identityContext = new IdentityContext(studentId.ToString(), + "", true, new Dictionary()); + + _appContextMock.Setup(ctx => ctx.Identity).Returns(identityContext); + _commentRepositoryMock.Setup(repo => repo.GetAsync(comment.Id)) + .ReturnsAsync((Comment)null); + + var cancelationToken = new CancellationToken(); + + // Act & Assert + Func act = async () => await _deleteLikeHandler.HandleAsync(comand, cancelationToken); + await act.Should().ThrowAsync(); + } + + [Fact] + public async Task HandleAsync_WithNotTheirs_ShouldThrowUnauthorizedCommentAccessException() + { + // Arrange + var commentId = Guid.NewGuid(); + var comand = new DeleteLike(commentId); + var studentId = Guid.NewGuid(); + + var comment = Comment.Create(new AggregateId(commentId), Guid.NewGuid(), + CommentContext.Post, studentId, "Adam", Guid.NewGuid(), "text", DateTime.Now); + + var identityContext = new IdentityContext(Guid.NewGuid().ToString(), + "", true, new Dictionary()); + + _appContextMock.Setup(ctx => ctx.Identity).Returns(identityContext); + _commentRepositoryMock.Setup(repo => repo.GetAsync(comment.Id)) + .ReturnsAsync(comment); + + var cancelationToken = new CancellationToken(); + + // Act & Assert + Func act = async () => await _deleteLikeHandler.HandleAsync(comand, cancelationToken); + await act.Should().ThrowAsync(); + + } + } +} From 8f687d45dd669a844e15185b0822b83dcffad84b Mon Sep 17 00:00:00 2001 From: OJ Date: Sun, 19 May 2024 15:32:10 +0200 Subject: [PATCH 4/8] ubdate comment Handler test --- .../Handlers/UpdateCommnetHandlerTest.cs | 156 ++++++++++++++++++ 1 file changed, 156 insertions(+) create mode 100644 MiniSpace.Services.Comments/tests/MiniSpace.Services.Comments.Application.UnitTests/Commands/Handlers/UpdateCommnetHandlerTest.cs diff --git a/MiniSpace.Services.Comments/tests/MiniSpace.Services.Comments.Application.UnitTests/Commands/Handlers/UpdateCommnetHandlerTest.cs b/MiniSpace.Services.Comments/tests/MiniSpace.Services.Comments.Application.UnitTests/Commands/Handlers/UpdateCommnetHandlerTest.cs new file mode 100644 index 000000000..e96d8cb60 --- /dev/null +++ b/MiniSpace.Services.Comments/tests/MiniSpace.Services.Comments.Application.UnitTests/Commands/Handlers/UpdateCommnetHandlerTest.cs @@ -0,0 +1,156 @@ +using Xunit; +using Moq; +using System; +using System.Collections.Generic; +using System.Threading.Tasks; +using MiniSpace.Services.Comments.Application.Exceptions; +using MiniSpace.Services.Comments.Application.Services; +using MiniSpace.Services.Comments.Core.Entities; +using MiniSpace.Services.Comments.Core.Repositories; +using MiniSpace.Services.Comments.Application.Commands.Handlers; +using MiniSpace.Services.Comments.Application.Commands; +using MiniSpace.Services.Comments.Infrastructure.Contexts; +using System.Threading; +using FluentAssertions; + +namespace MiniSpace.Services.Comments.Application.UnitTests.Commands.Handlers +{ + public class UpdateCommnetHandlerTest + { + public readonly UpdateCommentHandler _updateCommentHandler; + private readonly Mock _commentRepositoryMock; + private readonly Mock _messageBrokerMock; + private readonly Mock _appContextMock; + private readonly Mock _dateTimeProviderMock; + + public UpdateCommnetHandlerTest() + { + _commentRepositoryMock = new Mock(); + _messageBrokerMock = new Mock(); + _appContextMock = new Mock(); + _dateTimeProviderMock = new Mock(); + _updateCommentHandler = new UpdateCommentHandler( + _commentRepositoryMock.Object, + _appContextMock.Object, + _messageBrokerMock.Object, + _dateTimeProviderMock.Object + ); + } + + [Fact] + public async Task HandleAsync_WithVaildStudentAndPermited_ShouldUpdateRepository() + { + // Arrange + var commentId = Guid.NewGuid(); + var comand = new UpdateComment(commentId, "newText"); + var studentId = Guid.NewGuid(); + + var comment = Comment.Create(new AggregateId(commentId), Guid.NewGuid(), + CommentContext.Post, studentId, "Adam", Guid.NewGuid(), "text", DateTime.Now); + + var identityContext = new IdentityContext(studentId.ToString(), + "", true, new Dictionary()); + + var dateTimeNow = DateTime.Now; + + _appContextMock.Setup(ctx => ctx.Identity).Returns(identityContext); + _commentRepositoryMock.Setup(repo => repo.GetAsync(comment.Id)) + .ReturnsAsync(comment); + _dateTimeProviderMock.Setup(dtp => dtp.Now).Returns(dateTimeNow); + + var cancelationToken = new CancellationToken(); + + // Act + await _updateCommentHandler.HandleAsync(comand, cancelationToken); + + // Assert + _commentRepositoryMock.Verify(repo => repo.UpdateAsync(comment), Times.Once()); + } + + [Fact] + public async Task HandleAsync_WithInvalidComment_ShouldThrowCommentNotFoundExeption() + { + // Arrange + var commentId = Guid.NewGuid(); + var comand = new UpdateComment(commentId, "newText"); + var studentId = Guid.NewGuid(); + + var comment = Comment.Create(new AggregateId(commentId), Guid.NewGuid(), + CommentContext.Post, studentId, "Adam", Guid.NewGuid(), "text", DateTime.Now); + + var identityContext = new IdentityContext(studentId.ToString(), + "", true, new Dictionary()); + + var dateTimeNow = DateTime.Now; + + _appContextMock.Setup(ctx => ctx.Identity).Returns(identityContext); + _commentRepositoryMock.Setup(repo => repo.GetAsync(comment.Id)) + .ReturnsAsync((Comment)null); + _dateTimeProviderMock.Setup(dtp => dtp.Now).Returns(dateTimeNow); + + var cancelationToken = new CancellationToken(); + + // Act & Assert + Func act = async () => await _updateCommentHandler.HandleAsync(comand, cancelationToken); + await act.Should().ThrowAsync(); + } + + [Fact] + public async Task HandleAsync_WithAdminNotTheirs_ShouldNotThrowException() + { + // Arrange + var commentId = Guid.NewGuid(); + var comand = new UpdateComment(commentId, "newText"); + var studentId = Guid.NewGuid(); + + var comment = Comment.Create(new AggregateId(commentId), Guid.NewGuid(), + CommentContext.Post, studentId, "Adam", Guid.NewGuid(), "text", DateTime.Now); + + var identityContext = new IdentityContext(Guid.NewGuid().ToString(), + "Admin", true, new Dictionary()); + + var dateTimeNow = DateTime.Now; + + _appContextMock.Setup(ctx => ctx.Identity).Returns(identityContext); + _commentRepositoryMock.Setup(repo => repo.GetAsync(comment.Id)) + .ReturnsAsync(comment); + _dateTimeProviderMock.Setup(dtp => dtp.Now).Returns(dateTimeNow); + + var cancelationToken = new CancellationToken(); + + // Act & Assert + Func act = async () => await _updateCommentHandler.HandleAsync(comand, cancelationToken); + await act.Should().NotThrowAsync(); + + } + + [Fact] + public async Task HandleAsync_WithNotAdminNotTheirs_ShouldThrowUnauthorizedCommentAccessException() + { + // Arrange + var commentId = Guid.NewGuid(); + var comand = new UpdateComment(commentId, "newText"); + var studentId = Guid.NewGuid(); + + var comment = Comment.Create(new AggregateId(commentId), Guid.NewGuid(), + CommentContext.Post, studentId, "Adam", Guid.NewGuid(), "text", DateTime.Now); + + var identityContext = new IdentityContext(Guid.NewGuid().ToString(), + "", true, new Dictionary()); + + var dateTimeNow = DateTime.Now; + + _appContextMock.Setup(ctx => ctx.Identity).Returns(identityContext); + _commentRepositoryMock.Setup(repo => repo.GetAsync(comment.Id)) + .ReturnsAsync(comment); + _dateTimeProviderMock.Setup(dtp => dtp.Now).Returns(dateTimeNow); + + var cancelationToken = new CancellationToken(); + + // Act & Assert + Func act = async () => await _updateCommentHandler.HandleAsync(comand, cancelationToken); + await act.Should().ThrowAsync(); + + } + } +} From 6e7e8195a1d84dce12a789fe7997cb627e6e7a70 Mon Sep 17 00:00:00 2001 From: OJ Date: Sun, 19 May 2024 16:19:59 +0200 Subject: [PATCH 5/8] external Handlers Tests --- .../Handlers/EventDeletedHandlerTest.cs | 59 +++++++++++++++++ .../Handlers/PostDeletedHandlerTest.cs | 59 +++++++++++++++++ .../Handlers/StudentCreatedHandlerTest.cs | 63 ++++++++++++++++++ .../Handlers/StudentDeletedHandlerTest.cs | 64 +++++++++++++++++++ 4 files changed, 245 insertions(+) create mode 100644 MiniSpace.Services.Comments/tests/MiniSpace.Services.Comments.Application.UnitTests/Events/External/Handlers/EventDeletedHandlerTest.cs create mode 100644 MiniSpace.Services.Comments/tests/MiniSpace.Services.Comments.Application.UnitTests/Events/External/Handlers/PostDeletedHandlerTest.cs create mode 100644 MiniSpace.Services.Comments/tests/MiniSpace.Services.Comments.Application.UnitTests/Events/External/Handlers/StudentCreatedHandlerTest.cs create mode 100644 MiniSpace.Services.Comments/tests/MiniSpace.Services.Comments.Application.UnitTests/Events/External/Handlers/StudentDeletedHandlerTest.cs diff --git a/MiniSpace.Services.Comments/tests/MiniSpace.Services.Comments.Application.UnitTests/Events/External/Handlers/EventDeletedHandlerTest.cs b/MiniSpace.Services.Comments/tests/MiniSpace.Services.Comments.Application.UnitTests/Events/External/Handlers/EventDeletedHandlerTest.cs new file mode 100644 index 000000000..f78a7cfcf --- /dev/null +++ b/MiniSpace.Services.Comments/tests/MiniSpace.Services.Comments.Application.UnitTests/Events/External/Handlers/EventDeletedHandlerTest.cs @@ -0,0 +1,59 @@ +using Xunit; +using Moq; +using System; +using System.Collections.Generic; +using System.Threading.Tasks; +using MiniSpace.Services.Comments.Application.Exceptions; +using MiniSpace.Services.Comments.Application.Services; +using MiniSpace.Services.Comments.Core.Entities; +using MiniSpace.Services.Comments.Core.Repositories; +using MiniSpace.Services.Comments.Application.Commands.Handlers; +using MiniSpace.Services.Comments.Application.Commands; +using MiniSpace.Services.Comments.Infrastructure.Contexts; +using System.Threading; +using FluentAssertions; +using MiniSpace.Services.Comments.Application.Events.External.Handlers; +using MiniSpace.Services.Comments.Application.Events.External; +using System.ComponentModel.Design; + +namespace MiniSpace.Services.Comments.Application.UnitTests.Events.External.Handlers +{ + public class EventDeletedHandlerTest + { + private readonly EventDeletedHandler _eventDeletedHandler; + private readonly Mock _commentRepositoryMock; + + public EventDeletedHandlerTest() + { + _commentRepositoryMock = new Mock(); + _eventDeletedHandler = new EventDeletedHandler(_commentRepositoryMock.Object); + } + + [Fact] + public async Task HandleAsync_ValidData_ShouldNotThrowExeption() + { + // Arrange + var eventId = System.Guid.NewGuid(); + var @event = new EventDeleted(eventId); + var commentId1 = System.Guid.NewGuid(); + var commentId2 = System.Guid.NewGuid(); + var comment1 = Comment.Create(new AggregateId(commentId1), eventId, + CommentContext.Event, System.Guid.NewGuid(), "Adam", System.Guid.NewGuid(), "text", DateTime.Now); + var comment2 = Comment.Create(new AggregateId(commentId2), eventId, + CommentContext.Event, Guid.NewGuid(), "Bartek", Guid.NewGuid(), "text", DateTime.Now); + var commentsList = new List { comment1, comment2 }; + + _commentRepositoryMock.Setup(repo => repo.GetByEventIdAsync(eventId)) + .ReturnsAsync(commentsList); + + var cancelationTocken = new CancellationToken(); + + // Act + await _eventDeletedHandler.HandleAsync(@event, cancelationTocken); + + // Assert + _commentRepositoryMock.Verify(repo => repo.DeleteAsync(commentId1), Times.Once()); + _commentRepositoryMock.Verify(repo => repo.DeleteAsync(commentId2), Times.Once()); + } + } +} diff --git a/MiniSpace.Services.Comments/tests/MiniSpace.Services.Comments.Application.UnitTests/Events/External/Handlers/PostDeletedHandlerTest.cs b/MiniSpace.Services.Comments/tests/MiniSpace.Services.Comments.Application.UnitTests/Events/External/Handlers/PostDeletedHandlerTest.cs new file mode 100644 index 000000000..6e1569f7a --- /dev/null +++ b/MiniSpace.Services.Comments/tests/MiniSpace.Services.Comments.Application.UnitTests/Events/External/Handlers/PostDeletedHandlerTest.cs @@ -0,0 +1,59 @@ +using Xunit; +using Moq; +using System; +using System.Collections.Generic; +using System.Threading.Tasks; +using MiniSpace.Services.Comments.Application.Exceptions; +using MiniSpace.Services.Comments.Application.Services; +using MiniSpace.Services.Comments.Core.Entities; +using MiniSpace.Services.Comments.Core.Repositories; +using MiniSpace.Services.Comments.Application.Commands.Handlers; +using MiniSpace.Services.Comments.Application.Commands; +using MiniSpace.Services.Comments.Infrastructure.Contexts; +using System.Threading; +using FluentAssertions; +using MiniSpace.Services.Comments.Application.Events.External.Handlers; +using MiniSpace.Services.Comments.Application.Events.External; +using System.ComponentModel.Design; + +namespace MiniSpace.Services.Comments.Application.UnitTests.Events.External.Handlers +{ + public class PostDeletedHandlerTest + { + private readonly PostDeletedHandler _postDeletedHandler; + private readonly Mock _commentRepositoryMock; + + public PostDeletedHandlerTest() + { + _commentRepositoryMock = new Mock(); + _postDeletedHandler = new PostDeletedHandler(_commentRepositoryMock.Object); + } + + [Fact] + public async Task HandleAsync_ValidData_ShouldNotThrowExeption() + { + // Arrange + var postId = System.Guid.NewGuid(); + var @event = new PostDeleted(postId); + var commentId1 = System.Guid.NewGuid(); + var commentId2 = System.Guid.NewGuid(); + var comment1 = Comment.Create(new AggregateId(commentId1), postId, + CommentContext.Event, System.Guid.NewGuid(), "Adam", System.Guid.NewGuid(), "text", DateTime.Now); + var comment2 = Comment.Create(new AggregateId(commentId2), postId, + CommentContext.Event, Guid.NewGuid(), "Bartek", Guid.NewGuid(), "text", DateTime.Now); + var commentsList = new List { comment1, comment2 }; + + _commentRepositoryMock.Setup(repo => repo.GetByPostIdAsync(postId)) + .ReturnsAsync(commentsList); + + var cancelationTocken = new CancellationToken(); + + // Act + await _postDeletedHandler.HandleAsync(@event, cancelationTocken); + + // Assert + _commentRepositoryMock.Verify(repo => repo.DeleteAsync(commentId1), Times.Once()); + _commentRepositoryMock.Verify(repo => repo.DeleteAsync(commentId2), Times.Once()); + } + } +} diff --git a/MiniSpace.Services.Comments/tests/MiniSpace.Services.Comments.Application.UnitTests/Events/External/Handlers/StudentCreatedHandlerTest.cs b/MiniSpace.Services.Comments/tests/MiniSpace.Services.Comments.Application.UnitTests/Events/External/Handlers/StudentCreatedHandlerTest.cs new file mode 100644 index 000000000..078119316 --- /dev/null +++ b/MiniSpace.Services.Comments/tests/MiniSpace.Services.Comments.Application.UnitTests/Events/External/Handlers/StudentCreatedHandlerTest.cs @@ -0,0 +1,63 @@ +using Xunit; +using Moq; +using System; +using System.Collections.Generic; +using System.Threading.Tasks; +using MiniSpace.Services.Comments.Application.Exceptions; +using MiniSpace.Services.Comments.Application.Services; +using MiniSpace.Services.Comments.Core.Entities; +using MiniSpace.Services.Comments.Core.Repositories; +using MiniSpace.Services.Comments.Application.Commands.Handlers; +using MiniSpace.Services.Comments.Application.Commands; +using MiniSpace.Services.Comments.Infrastructure.Contexts; +using System.Threading; +using FluentAssertions; +using MiniSpace.Services.Comments.Application.Events.External.Handlers; +using MiniSpace.Services.Comments.Application.Events.External; +using System.ComponentModel.Design; + +namespace MiniSpace.Services.Comments.Application.UnitTests.Events.External.Handlers +{ + public class StudentCreatedHandlerTest + { + private readonly StudentCreatedHandler _studentCreatedHandler; + private readonly Mock _studentRepositoryMock; + + public StudentCreatedHandlerTest() + { + _studentRepositoryMock = new Mock(); + _studentCreatedHandler = new StudentCreatedHandler(_studentRepositoryMock.Object); + } + + [Fact] + public async Task HandleAsync_ValidData_ShouldNotThrowExeption() + { + // Arrange + var studentId = Guid.NewGuid(); + var @event = new StudentCreated(studentId, "Jan Kowalski"); + + _studentRepositoryMock.Setup(repo => repo.ExistsAsync(studentId)) + .ReturnsAsync(false); + + // Act & Assert + Func act = async () => await _studentCreatedHandler.HandleAsync(@event); + await act.Should().NotThrowAsync(); + + } + + [Fact] + public async Task HandleAsync_StudentAlreadyCreated_ShuldThrowStudentNotFoundException() + { + // Arrange + var studentId = Guid.NewGuid(); + var @event = new StudentCreated(studentId, "Jan Kowalski"); + + _studentRepositoryMock.Setup(repo => repo.ExistsAsync(studentId)) + .ReturnsAsync(true); + + // Act & Assert + Func act = async () => await _studentCreatedHandler.HandleAsync(@event); + await act.Should().ThrowAsync(); + } + } +} diff --git a/MiniSpace.Services.Comments/tests/MiniSpace.Services.Comments.Application.UnitTests/Events/External/Handlers/StudentDeletedHandlerTest.cs b/MiniSpace.Services.Comments/tests/MiniSpace.Services.Comments.Application.UnitTests/Events/External/Handlers/StudentDeletedHandlerTest.cs new file mode 100644 index 000000000..d54b22eeb --- /dev/null +++ b/MiniSpace.Services.Comments/tests/MiniSpace.Services.Comments.Application.UnitTests/Events/External/Handlers/StudentDeletedHandlerTest.cs @@ -0,0 +1,64 @@ +using Xunit; +using Moq; +using System; +using System.Collections.Generic; +using System.Threading.Tasks; +using MiniSpace.Services.Comments.Application.Exceptions; +using MiniSpace.Services.Comments.Application.Services; +using MiniSpace.Services.Comments.Core.Entities; +using MiniSpace.Services.Comments.Core.Repositories; +using MiniSpace.Services.Comments.Application.Commands.Handlers; +using MiniSpace.Services.Comments.Application.Commands; +using MiniSpace.Services.Comments.Infrastructure.Contexts; +using System.Threading; +using FluentAssertions; +using MiniSpace.Services.Comments.Application.Events.External.Handlers; +using MiniSpace.Services.Comments.Application.Events.External; +using System.ComponentModel.Design; + +namespace MiniSpace.Services.Comments.Application.UnitTests.Events.External.Handlers +{ + public class StudentDeletedHandlerTest + { + private readonly StudentDeletedHandler _studentDeletedHandler; + private readonly Mock _studentRepositoryMock; + + public StudentDeletedHandlerTest() + { + _studentRepositoryMock = new Mock(); + _studentDeletedHandler = new StudentDeletedHandler(_studentRepositoryMock.Object); + } + + [Fact] + public async Task HandleAsync_ValidData_ShouldNotThrowExeption() + { + // Arrange + var studentId = Guid.NewGuid(); + var @event = new StudentDeleted(studentId, "Jan Kowalski"); + + _studentRepositoryMock.Setup(repo => repo.ExistsAsync(studentId)) + .ReturnsAsync(true); + + // Act + await _studentDeletedHandler.HandleAsync(@event); + + // Assert + _studentRepositoryMock.Verify(repo => repo.DeleteAsync(studentId), Times.Once()); + } + + [Fact] + public async Task HandleAsync_StudentAlreadyDeleted_ShuldThrowStudentNotFoundException() + { + // Arrange + var studentId = Guid.NewGuid(); + var @event = new StudentDeleted(studentId, "Jan Kowalski"); + + _studentRepositoryMock.Setup(repo => repo.ExistsAsync(studentId)) + .ReturnsAsync(false); + + // Act & Assert + Func act = async () => await _studentDeletedHandler.HandleAsync(@event); + await act.Should().ThrowAsync(); + } + } +} From 3032c84432cbe939ae8da8798eb816bc88a50241 Mon Sep 17 00:00:00 2001 From: OJ Date: Sun, 19 May 2024 17:05:08 +0200 Subject: [PATCH 6/8] (#148) core entity comment tests --- .../MiniSpace.Services.Comments.sln | 6 + .../Entities/CommentTest.cs | 120 ++++++++++++++++++ ...ce.Services.Comments.Core.UnitTests.csproj | 32 +++++ 3 files changed, 158 insertions(+) create mode 100644 MiniSpace.Services.Comments/tests/MiniSpace.Services.Comments.Core.UnitTests/Entities/CommentTest.cs create mode 100644 MiniSpace.Services.Comments/tests/MiniSpace.Services.Comments.Core.UnitTests/MiniSpace.Services.Comments.Core.UnitTests.csproj diff --git a/MiniSpace.Services.Comments/MiniSpace.Services.Comments.sln b/MiniSpace.Services.Comments/MiniSpace.Services.Comments.sln index 605629bc0..84c2c0564 100644 --- a/MiniSpace.Services.Comments/MiniSpace.Services.Comments.sln +++ b/MiniSpace.Services.Comments/MiniSpace.Services.Comments.sln @@ -13,6 +13,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MiniSpace.Services.Comments EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MiniSpace.Services.Comments.Application.UnitTests", "tests\MiniSpace.Services.Comments.Application.UnitTests\MiniSpace.Services.Comments.Application.UnitTests.csproj", "{63DF71AF-1D31-4A4D-8127-9B8EB359889D}" EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MiniSpace.Services.Comments.Core.UnitTests", "tests\MiniSpace.Services.Comments.Core.UnitTests\MiniSpace.Services.Comments.Core.UnitTests.csproj", "{C0DE6BE3-4EE5-4DB5-A170-57351B19AAF8}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -39,6 +41,10 @@ Global {63DF71AF-1D31-4A4D-8127-9B8EB359889D}.Debug|Any CPU.Build.0 = Debug|Any CPU {63DF71AF-1D31-4A4D-8127-9B8EB359889D}.Release|Any CPU.ActiveCfg = Release|Any CPU {63DF71AF-1D31-4A4D-8127-9B8EB359889D}.Release|Any CPU.Build.0 = Release|Any CPU + {C0DE6BE3-4EE5-4DB5-A170-57351B19AAF8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {C0DE6BE3-4EE5-4DB5-A170-57351B19AAF8}.Debug|Any CPU.Build.0 = Debug|Any CPU + {C0DE6BE3-4EE5-4DB5-A170-57351B19AAF8}.Release|Any CPU.ActiveCfg = Release|Any CPU + {C0DE6BE3-4EE5-4DB5-A170-57351B19AAF8}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/MiniSpace.Services.Comments/tests/MiniSpace.Services.Comments.Core.UnitTests/Entities/CommentTest.cs b/MiniSpace.Services.Comments/tests/MiniSpace.Services.Comments.Core.UnitTests/Entities/CommentTest.cs new file mode 100644 index 000000000..37e364fa6 --- /dev/null +++ b/MiniSpace.Services.Comments/tests/MiniSpace.Services.Comments.Core.UnitTests/Entities/CommentTest.cs @@ -0,0 +1,120 @@ +using Xunit; +using Moq; +using System; +using System.Collections.Generic; +using System.Threading.Tasks; +using System.Linq; +using System.Text; +using MiniSpace.Services.Comments.Application.Events; +using MiniSpace.Services.Comments.Application.Exceptions; +using MiniSpace.Services.Comments.Application.Services; +using MiniSpace.Services.Comments.Core.Entities; +using MiniSpace.Services.Comments.Core.Repositories; +using MiniSpace.Services.Comments.Application.Commands.Handlers; +using MiniSpace.Services.Comments.Application.Commands; +using MiniSpace.Services.Comments.Infrastructure.Contexts; +using Convey.CQRS.Commands; +using System.Threading; +using System.Security.Claims; +using FluentAssertions; +using MiniSpace.Services.Comments.Core.Exceptions; +using Microsoft.AspNetCore.DataProtection.KeyManagement; + +namespace MiniSpace.Services.Comments.Core.UnitTests.Entities +{ + public class CommentTest + { + [Fact] + public void Like_NotLiked_ShouldAddALike() + { + // Arrange + var studentId = Guid.NewGuid(); + var likes = new HashSet() { }; + var comment = new Comment(Guid.NewGuid(), Guid.NewGuid(), CommentContext.Post, + Guid.NewGuid(), "Adam", likes, Guid.Empty, "text", + DateTime.Now, DateTime.Now, DateTime.Now, 0, false); + + // Act + comment.Like(studentId); + + // Assert + Assert.Contains(comment.Likes, id => id == studentId); + } + + [Fact] + public void Like_IsLiked_ShouldThrowStudentAlreadyLikesCommentException() + { + // Arrange + var studentId = Guid.NewGuid(); + var likes = new HashSet() { studentId }; + var comment = new Comment(Guid.NewGuid(), Guid.NewGuid(), CommentContext.Post, + Guid.NewGuid(), "Adam", likes, Guid.Empty, "text", + DateTime.Now, DateTime.Now, DateTime.Now, 0, false); + + // Act & Assert + var act = new Action(() => comment.Like(studentId)); + Assert.Throws(act); + } + + [Fact] + public void UnLike_IsLiked_ShouldRemoveALike() + { + // Arrange + var studentId = Guid.NewGuid(); + var likes = new HashSet() { studentId }; + var comment = new Comment(Guid.NewGuid(), Guid.NewGuid(), CommentContext.Post, + Guid.NewGuid(), "Adam", likes, Guid.Empty, "text", + DateTime.Now, DateTime.Now, DateTime.Now, 0, false); + + // Act + comment.UnLike(studentId); + + // Assert + Assert.DoesNotContain(comment.Likes, id => id == studentId); + } + + [Fact] + public void Like_NotLiked_ShouldThrowStudentNotLikeCommentException() + { + // Arrange + var studentId = Guid.NewGuid(); + var likes = new HashSet() { }; + var comment = new Comment(Guid.NewGuid(), Guid.NewGuid(), CommentContext.Post, + Guid.NewGuid(), "Adam", likes, Guid.Empty, "text", + DateTime.Now, DateTime.Now, DateTime.Now, 0, false); + + // Act & Assert + var act = new Action(() => comment.UnLike(studentId)); + Assert.Throws(act); + } + + [Fact] + public void Create_TooLongText_ShouldThrowInvalidCommentContentException() + { + // Arrange + var commentId = new AggregateId( Guid.NewGuid()); + var text = new String('x', 301); + + // Act & Assert + var act = new Action(() => Comment.Create(commentId, Guid.NewGuid(), CommentContext.Post, + Guid.NewGuid(), "Adam", Guid.Empty, text, DateTime.Now)); + Assert.Throws(act); + } + + [Fact] + public void Update_Empty_ShouldThrowInvalidCommentContentException() + { + // Arrange + var commentId = Guid.NewGuid(); + var text = String.Empty; + var comment = new Comment(Guid.NewGuid(), Guid.NewGuid(), CommentContext.Post, + Guid.NewGuid(), "Adam", new HashSet() { }, Guid.Empty, "text", + DateTime.Now, DateTime.Now, DateTime.Now, 0, false); + + // Act & Assert + var act = new Action(() => comment.Update(text, DateTime.Now)); + Assert.Throws(act); + } + + } +} diff --git a/MiniSpace.Services.Comments/tests/MiniSpace.Services.Comments.Core.UnitTests/MiniSpace.Services.Comments.Core.UnitTests.csproj b/MiniSpace.Services.Comments/tests/MiniSpace.Services.Comments.Core.UnitTests/MiniSpace.Services.Comments.Core.UnitTests.csproj new file mode 100644 index 000000000..0d888754d --- /dev/null +++ b/MiniSpace.Services.Comments/tests/MiniSpace.Services.Comments.Core.UnitTests/MiniSpace.Services.Comments.Core.UnitTests.csproj @@ -0,0 +1,32 @@ + + + + net8.0 + disable + + false + true + + + + + + + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + + + + + + + + From 620d2defe7be15476b2e4b109599b3f76a26679e Mon Sep 17 00:00:00 2001 From: OJ Date: Sun, 19 May 2024 17:14:04 +0200 Subject: [PATCH 7/8] (#148) core enttity AggregatedId Tests --- .../Entities/AggregatedIdTest.cs | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 MiniSpace.Services.Comments/tests/MiniSpace.Services.Comments.Core.UnitTests/Entities/AggregatedIdTest.cs diff --git a/MiniSpace.Services.Comments/tests/MiniSpace.Services.Comments.Core.UnitTests/Entities/AggregatedIdTest.cs b/MiniSpace.Services.Comments/tests/MiniSpace.Services.Comments.Core.UnitTests/Entities/AggregatedIdTest.cs new file mode 100644 index 000000000..ff803ff3b --- /dev/null +++ b/MiniSpace.Services.Comments/tests/MiniSpace.Services.Comments.Core.UnitTests/Entities/AggregatedIdTest.cs @@ -0,0 +1,52 @@ +using Xunit; +using Moq; +using System; +using System.Collections.Generic; +using System.Threading.Tasks; +using System.Linq; +using System.Text; +using MiniSpace.Services.Comments.Application.Events; +using MiniSpace.Services.Comments.Application.Exceptions; +using MiniSpace.Services.Comments.Application.Services; +using MiniSpace.Services.Comments.Core.Entities; +using MiniSpace.Services.Comments.Core.Repositories; +using MiniSpace.Services.Comments.Application.Commands.Handlers; +using MiniSpace.Services.Comments.Application.Commands; +using MiniSpace.Services.Comments.Infrastructure.Contexts; +using Convey.CQRS.Commands; +using System.Threading; +using System.Security.Claims; +using FluentAssertions; +using MiniSpace.Services.Comments.Core.Exceptions; +using Microsoft.AspNetCore.DataProtection.KeyManagement; + +namespace MiniSpace.Services.Comments.Core.UnitTests.Entities +{ + public class AggregatedIdTest + { + [Fact] + public void AggregateId_CreatedTwice_ShuldBeDiffrent() + { + // Arrange & Act + var id1 = new AggregateId(); + var id2 = new AggregateId(); + + // Assert + Assert.NotEqual(id1.Value, id2.Value); + } + + [Fact] + public void AggregateId_CreatedTwiceSameGuid_ShuldBeSame() + { + // Arrange + var id = Guid.NewGuid(); + + // Act + var id1 = new AggregateId(id); + var id2 = new AggregateId(id); + + // Assert + Assert.Equal(id1.Value, id2.Value); + } + } +} From 7a7abfba50c40928018e4e95c89fbd4542d8d4e1 Mon Sep 17 00:00:00 2001 From: OJ Date: Sun, 19 May 2024 17:34:40 +0200 Subject: [PATCH 8/8] (#148) infrastructure service MessageBroker Tests --- .../MiniSpace.Services.Comments.sln | 6 + .../Services/MessageBroker.cs | 2 + ...s.Comments.Infrastructure.UnitTests.csproj | 32 ++++ .../Services/MessageBrokerTest.cs | 142 ++++++++++++++++++ 4 files changed, 182 insertions(+) create mode 100644 MiniSpace.Services.Comments/tests/MiniSpace.Services.Comments.Infrastructure.UnitTests/MiniSpace.Services.Comments.Infrastructure.UnitTests.csproj create mode 100644 MiniSpace.Services.Comments/tests/MiniSpace.Services.Comments.Infrastructure.UnitTests/Services/MessageBrokerTest.cs diff --git a/MiniSpace.Services.Comments/MiniSpace.Services.Comments.sln b/MiniSpace.Services.Comments/MiniSpace.Services.Comments.sln index 84c2c0564..97759c180 100644 --- a/MiniSpace.Services.Comments/MiniSpace.Services.Comments.sln +++ b/MiniSpace.Services.Comments/MiniSpace.Services.Comments.sln @@ -15,6 +15,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MiniSpace.Services.Comments EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MiniSpace.Services.Comments.Core.UnitTests", "tests\MiniSpace.Services.Comments.Core.UnitTests\MiniSpace.Services.Comments.Core.UnitTests.csproj", "{C0DE6BE3-4EE5-4DB5-A170-57351B19AAF8}" EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MiniSpace.Services.Comments.Infrastructure.UnitTests", "tests\MiniSpace.Services.Comments.Infrastructure.UnitTests\MiniSpace.Services.Comments.Infrastructure.UnitTests.csproj", "{81D7C37A-34BE-43BB-9709-0A866D67F8EB}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -45,6 +47,10 @@ Global {C0DE6BE3-4EE5-4DB5-A170-57351B19AAF8}.Debug|Any CPU.Build.0 = Debug|Any CPU {C0DE6BE3-4EE5-4DB5-A170-57351B19AAF8}.Release|Any CPU.ActiveCfg = Release|Any CPU {C0DE6BE3-4EE5-4DB5-A170-57351B19AAF8}.Release|Any CPU.Build.0 = Release|Any CPU + {81D7C37A-34BE-43BB-9709-0A866D67F8EB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {81D7C37A-34BE-43BB-9709-0A866D67F8EB}.Debug|Any CPU.Build.0 = Debug|Any CPU + {81D7C37A-34BE-43BB-9709-0A866D67F8EB}.Release|Any CPU.ActiveCfg = Release|Any CPU + {81D7C37A-34BE-43BB-9709-0A866D67F8EB}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/MiniSpace.Services.Comments/src/MiniSpace.Services.Comments.Infrastructure/Services/MessageBroker.cs b/MiniSpace.Services.Comments/src/MiniSpace.Services.Comments.Infrastructure/Services/MessageBroker.cs index c4355c2a4..935d792e0 100644 --- a/MiniSpace.Services.Comments/src/MiniSpace.Services.Comments.Infrastructure/Services/MessageBroker.cs +++ b/MiniSpace.Services.Comments/src/MiniSpace.Services.Comments.Infrastructure/Services/MessageBroker.cs @@ -6,7 +6,9 @@ using Microsoft.Extensions.Logging; using OpenTracing; using MiniSpace.Services.Comments.Application.Services; +using System.Runtime.CompilerServices; +[assembly: InternalsVisibleTo("MiniSpace.Services.Comments.Infrastructure.UnitTests")] namespace MiniSpace.Services.Comments.Infrastructure.Services { internal sealed class MessageBroker : IMessageBroker diff --git a/MiniSpace.Services.Comments/tests/MiniSpace.Services.Comments.Infrastructure.UnitTests/MiniSpace.Services.Comments.Infrastructure.UnitTests.csproj b/MiniSpace.Services.Comments/tests/MiniSpace.Services.Comments.Infrastructure.UnitTests/MiniSpace.Services.Comments.Infrastructure.UnitTests.csproj new file mode 100644 index 000000000..0d888754d --- /dev/null +++ b/MiniSpace.Services.Comments/tests/MiniSpace.Services.Comments.Infrastructure.UnitTests/MiniSpace.Services.Comments.Infrastructure.UnitTests.csproj @@ -0,0 +1,32 @@ + + + + net8.0 + disable + + false + true + + + + + + + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + + + + + + + + diff --git a/MiniSpace.Services.Comments/tests/MiniSpace.Services.Comments.Infrastructure.UnitTests/Services/MessageBrokerTest.cs b/MiniSpace.Services.Comments/tests/MiniSpace.Services.Comments.Infrastructure.UnitTests/Services/MessageBrokerTest.cs new file mode 100644 index 000000000..0976929bc --- /dev/null +++ b/MiniSpace.Services.Comments/tests/MiniSpace.Services.Comments.Infrastructure.UnitTests/Services/MessageBrokerTest.cs @@ -0,0 +1,142 @@ +using Xunit; +using Moq; +using Convey.CQRS.Events; +using System; +using System.Collections.Generic; +using System.Threading.Tasks; +using Convey.MessageBrokers; +using Convey.MessageBrokers.Outbox; +using Convey.MessageBrokers.RabbitMQ; +using Microsoft.AspNetCore.Http; +using Microsoft.Extensions.Logging; +using OpenTracing; +using MiniSpace.Services.Comments.Infrastructure.Services; +using MiniSpace.Services.Comments.Application.Services; +using MiniSpace.Services.Comments.Application.Events; + +namespace MiniSpace.Services.Comments.Infrastructure.UnitTests.Services +{ + public class MessageBrokerTest + { + private readonly MessageBroker _messageBroker; + private readonly Mock _mockBusPublisher; + private readonly Mock _mockMessageOutbox; + private readonly Mock _mockContextAccessor; + private readonly Mock _mockHttpContextAccessor; + private readonly Mock _mockMessagePropertiesAccessor; + private readonly Mock _mockTracer; + private readonly Mock> _mockLogger; + + public MessageBrokerTest() + { + _mockBusPublisher = new Mock(); + _mockMessageOutbox = new Mock(); + _mockContextAccessor = new Mock(); + _mockHttpContextAccessor = new Mock(); + _mockMessagePropertiesAccessor = new Mock(); + _mockTracer = new Mock(); + _mockLogger = new Mock>(); + + _messageBroker = new MessageBroker(_mockBusPublisher.Object, _mockMessageOutbox.Object, _mockContextAccessor.Object, + _mockHttpContextAccessor.Object, _mockMessagePropertiesAccessor.Object, new RabbitMqOptions(), + _mockTracer.Object, _mockLogger.Object); + } + + [Fact] + public async Task PublishAsync_WithEventsAndOutboxDisabled_PublishesEvents() + { + //Arrange + var events = new List + { + new CommentCreated(Guid.NewGuid()) + }; + _mockMessageOutbox.Setup(x => x.Enabled).Returns(false); + + //Act + await _messageBroker.PublishAsync(events); + + //Assert + _mockMessageOutbox.Verify( + x => x.SendAsync(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), + It.IsAny(), It.IsAny(), It.IsAny>()), + Times.Never + ); + _mockBusPublisher.Verify( + x => x.PublishAsync(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), + It.IsAny(), It.IsAny>()), Times.Exactly(events.Count) + ); + } + + [Fact] + public async Task PublishAsync_WithEventsAndOutboxEnabled_SendsMessagesToOutbox() + { + //Arrange + var events = new List + { + new CommentCreated(Guid.NewGuid()) + }; + _mockMessageOutbox.Setup(x => x.Enabled).Returns(true); + + //Act + await _messageBroker.PublishAsync(events); + + //Assert + _mockMessageOutbox.Verify( + x => x.SendAsync(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), + It.IsAny(), It.IsAny(), It.IsAny>()), + Times.Exactly(events.Count) + ); + _mockBusPublisher.Verify( + x => x.PublishAsync(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), + It.IsAny(), It.IsAny>()), Times.Never + ); + } + + [Fact] + public async Task PublishAsync_WithoutEvents_Returns() + { + //Arrange + List events = null; + + //Act + await _messageBroker.PublishAsync(events); + + //Assert + _mockMessageOutbox.Verify( + x => x.SendAsync(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), + It.IsAny(), It.IsAny(), It.IsAny>()), + Times.Never + ); + _mockBusPublisher.Verify( + x => x.PublishAsync(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), + It.IsAny(), It.IsAny>()), Times.Never + ); + } + + [Fact] + public async Task PublishAsync_WithNullEventAndOutboxDisabled_PublishesOneLessEvent() + { + //Arrange + var events = new List + { + new CommentCreated(Guid.NewGuid()), + null + }; + _mockMessageOutbox.Setup(x => x.Enabled).Returns(false); + + //Act + await _messageBroker.PublishAsync(events); + + //Assert + _mockMessageOutbox.Verify( + x => x.SendAsync(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), + It.IsAny(), It.IsAny(), It.IsAny>()), + Times.Never + ); + _mockBusPublisher.Verify( + x => x.PublishAsync(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), + It.IsAny(), It.IsAny>()), Times.Exactly(events.Count - 1) + ); + } + } +}