Skip to content

Commit

Permalink
(#169) Application: add event handler tests
Browse files Browse the repository at this point in the history
  • Loading branch information
olegkiprik committed May 27, 2024
1 parent 32a5fab commit c90332c
Show file tree
Hide file tree
Showing 6 changed files with 144 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public async Task HandleAsync(PostCreated @event, CancellationToken cancellation
{
if (await _postRepository.ExistsAsync(@event.PostId))
{
throw new StudentAlreadyAddedException(@event.PostId);
throw new PostAlreadyAddedException(@event.PostId);
}

await _postRepository.AddAsync(new Post(@event.PostId));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace MiniSpace.Services.Reactions.Application.Exceptions
{
public class PostAlreadyAddedException : AppException
{
public override string Code { get; } = "post_already_added";
public Guid PostId { get; }

Check warning on line 6 in MiniSpace.Services.Reactions/src/MiniSpace.Services.Reactions.Application/Exceptions/PostAlreadyAddedException.cs

View check run for this annotation

Codecov / codecov/patch

MiniSpace.Services.Reactions/src/MiniSpace.Services.Reactions.Application/Exceptions/PostAlreadyAddedException.cs#L6

Added line #L6 was not covered by tests

public PostAlreadyAddedException(Guid postId)
: base($"Post with id: {postId} was already added.")
{
PostId = postId;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
using Xunit;
using Moq;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using MiniSpace.Services.Reactions.Application.Exceptions;
using MiniSpace.Services.Reactions.Application.Services;
using MiniSpace.Services.Reactions.Core.Entities;
using MiniSpace.Services.Reactions.Core.Repositories;
using MiniSpace.Services.Reactions.Application.Commands.Handlers;
using MiniSpace.Services.Reactions.Application.Commands;
using MiniSpace.Services.Reactions.Infrastructure.Contexts;
using System.Threading;
using FluentAssertions;
using MiniSpace.Services.Reactions.Application.Events.External.Handlers;
using MiniSpace.Services.Reactions.Application.Events.External;
using System.ComponentModel.Design;
using Convey.CQRS.Commands;
using MiniSpace.Services.Reactions.Application.Events;

namespace MiniSpace.Services.Reactions.Application.UnitTests.Events.External.Handlers
{
public class PostCreatedHandlerTest
{
private readonly PostCreatedHandler _postDeletedHandler;
private readonly Mock<IPostRepository> _postRepositoryMock;

public PostCreatedHandlerTest()
{
_postRepositoryMock = new();
_postDeletedHandler = new PostCreatedHandler(_postRepositoryMock.Object);
}

[Fact]
public async Task HandleAsync_ValidData_ShouldNotThrowExeption()
{
// Arrange
var postId = Guid.NewGuid();
var post = new PostCreated(postId);

_postRepositoryMock.Setup(repo => repo.ExistsAsync(postId))
.ReturnsAsync(false);

// Act & Assert
Func<Task> act = async () => await _postDeletedHandler.HandleAsync(post, new CancellationToken());
await act.Should().NotThrowAsync();
}

[Fact]
public async Task HandleAsync_PostAlreadyCreated_ShouldThrowPostAlreadyExistsException()
{
// Arrange
var postId = Guid.NewGuid();
var post = new PostCreated(postId);

_postRepositoryMock.Setup(repo => repo.ExistsAsync(postId))
.ReturnsAsync(true);

// Act & Assert
Func<Task> act = async () => await _postDeletedHandler.HandleAsync(post, new CancellationToken());
await act.Should().ThrowAsync<PostAlreadyAddedException>();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
using Xunit;
using Moq;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using MiniSpace.Services.Reactions.Application.Exceptions;
using MiniSpace.Services.Reactions.Application.Services;
using MiniSpace.Services.Reactions.Core.Entities;
using MiniSpace.Services.Reactions.Core.Repositories;
using MiniSpace.Services.Reactions.Application.Commands.Handlers;
using MiniSpace.Services.Reactions.Application.Commands;
using MiniSpace.Services.Reactions.Infrastructure.Contexts;
using System.Threading;
using FluentAssertions;
using MiniSpace.Services.Reactions.Application.Events.External.Handlers;
using MiniSpace.Services.Reactions.Application.Events.External;
using System.ComponentModel.Design;

namespace MiniSpace.Services.Reactions.Application.UnitTests.Events.External.Handlers
{
public class StudentCreatedHandlerTest
{
private readonly StudentCreatedHandler _studentCreatedHandler;
private readonly Mock<IStudentRepository> _studentRepositoryMock;

public StudentCreatedHandlerTest()
{
_studentRepositoryMock = new Mock<IStudentRepository>();
_studentCreatedHandler = new StudentCreatedHandler(_studentRepositoryMock.Object);
}

[Fact]
public async Task HandleAsync_ValidData_ShouldNotThrowExeption()
{
// Arrange
var studentId = Guid.NewGuid();
var @event = new StudentCreated(studentId, "Jan Kowalski");
var cancellationToken = new CancellationToken();

_studentRepositoryMock.Setup(repo => repo.ExistsAsync(studentId))
.ReturnsAsync(false);

// Act & Assert
Func<Task> act = async () => await _studentCreatedHandler.HandleAsync(@event, cancellationToken);
await act.Should().NotThrowAsync();

}

[Fact]
public async Task HandleAsync_StudentAlreadyCreated_ShuldThrowStudentAlreadyAddedException()
{
// Arrange
var studentId = Guid.NewGuid();
var @event = new StudentCreated(studentId, "Jan Kowalski");
var cancellationToken = new CancellationToken();

_studentRepositoryMock.Setup(repo => repo.ExistsAsync(studentId))
.ReturnsAsync(true);

// Act & Assert
Func<Task> act = async () => await _studentCreatedHandler.HandleAsync(@event, cancellationToken);
await act.Should().ThrowAsync<StudentAlreadyAddedException>();
}
}
}

0 comments on commit c90332c

Please sign in to comment.