Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(#148) Comments serice tests #164

Closed
wants to merge 22 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
d081118
Add like handler test
May 19, 2024
a30ef3c
Create comment Handler Tests
May 19, 2024
72a023f
delete Comment and Like Handler tests
May 19, 2024
8f687d4
ubdate comment Handler test
May 19, 2024
6e7e819
external Handlers Tests
May 19, 2024
3032c84
(#148) core entity comment tests
May 19, 2024
620d2de
(#148) core enttity AggregatedId Tests
May 19, 2024
7a7abfb
(#148) infrastructure service MessageBroker Tests
May 19, 2024
ed65423
Merge remote-tracking branch 'remotes/origin/dev' into comments_test
SaintAngeLs May 20, 2024
9e2f6b0
(#148) udpate the build test script
SaintAngeLs May 20, 2024
3757876
(#148) udpate the incorrect project directory
SaintAngeLs May 20, 2024
a77a024
(#148) udpat the script
SaintAngeLs May 20, 2024
07249c6
(#148) Infrastucture: removing mongo database and decorators from tes…
May 25, 2024
b45f7d3
(#148) Core: excluding wrapers from core tests
May 25, 2024
dc574af
(#148) Application: excluding dto from application tests range
May 25, 2024
891ffa5
(#148) Infrastucture: excluding logger related files from Infrastruct…
May 25, 2024
52cf871
(#148) update the build-tests script, update tesct coverage statistic…
SaintAngeLs May 25, 2024
3355f0c
(#148) updat ebuild microserices
SaintAngeLs May 25, 2024
ee33b83
(#148) udpate the history depth checkout
SaintAngeLs May 25, 2024
72e66f8
(#148) specifying base commit
SaintAngeLs May 25, 2024
e0f8b5c
(#148) adding the fail ci if error
SaintAngeLs May 25, 2024
70cb506
(#148) adding the fail ci if error
SaintAngeLs May 25, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
11 changes: 8 additions & 3 deletions .github/workflows/build_microservices.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,16 @@ jobs:
- project: 'MiniSpace.APIGateway/src/MiniSpace.APIGateway'
- project: 'MiniSpace.Services.Identity/src/MiniSpace.Services.Identity.Api'
test_dir: 'MiniSpace.Services.Identity/tests'
- project: 'MiniSpace.Services.Comments/src/MiniSpace.Services.Comments.Api'
test_dir: 'MiniSpace.Services.Comments/tests'
- project: 'MiniSpace.Web/src/MiniSpace.Web'
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
with:
fetch-depth: 0

- name: Setup .NET 8.0.x
uses: actions/setup-dotnet@v1
uses: actions/setup-dotnet@v2
with:
dotnet-version: '8.0.x'

Expand Down Expand Up @@ -46,4 +50,5 @@ jobs:
token: ${{ secrets.CODECOV_TOKEN }}
files: '**/coverage.cobertura.xml'
slug: SaintAngeLs/p_software_engineering_2

commit: ${{ github.event.pull_request.base.sha }}
# fail_ci_if_error: true
18 changes: 18 additions & 0 deletions MiniSpace.Services.Comments/MiniSpace.Services.Comments.sln
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ 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
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
Expand All @@ -33,6 +39,18 @@ 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
{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
{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
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using MiniSpace.Services.Comments.Core.Entities;

namespace MiniSpace.Services.Comments.Application.Dto
{
[ExcludeFromCodeCoverage]
public class CommentDto
{
public Guid Id { get; set; }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
namespace MiniSpace.Services.Comments.Application.Dto
using System.Diagnostics.CodeAnalysis;

namespace MiniSpace.Services.Comments.Application.Dto
{
[ExcludeFromCodeCoverage]
public class PageableDto
{
public int Page { get; set; }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;

namespace MiniSpace.Services.Comments.Application.Dto
{
[ExcludeFromCodeCoverage]
public class SortDto
{
public IEnumerable<string> SortBy { get; set; }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
namespace MiniSpace.Services.Comments.Application.Wrappers
using System.Diagnostics.CodeAnalysis;

namespace MiniSpace.Services.Comments.Application.Wrappers
{
[ExcludeFromCodeCoverage]
public class PagedResponse<T> : Response<T>
{
public int TotalPages { get; }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
namespace MiniSpace.Services.Comments.Application.Wrappers
using System.Diagnostics.CodeAnalysis;

namespace MiniSpace.Services.Comments.Application.Wrappers
{
[ExcludeFromCodeCoverage]
public class Response<T>
{
public T Content { get; set; }
Expand Down
Original file line number Diff line number Diff line change
@@ -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
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
using Convey.MessageBrokers;
using Convey.MessageBrokers.Outbox;
using Convey.Types;
using System.Diagnostics.CodeAnalysis;

namespace MiniSpace.Services.Comments.Infrastructure.Decorators
{
[ExcludeFromCodeCoverage]
[Decorator]
internal sealed class OutboxCommandHandlerDecorator<TCommand> : ICommandHandler<TCommand>
where TCommand : class, ICommand
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
using Convey.MessageBrokers;
using Convey.MessageBrokers.Outbox;
using Convey.Types;
using System.Diagnostics.CodeAnalysis;

namespace MiniSpace.Services.Comments.Infrastructure.Decorators
{
[ExcludeFromCodeCoverage]
[Decorator]
internal sealed class OutboxEventHandlerDecorator<TEvent> : IEventHandler<TEvent>
where TEvent : class, IEvent
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
using MiniSpace.Services.Comments.Application.Events.Rejected;
using MiniSpace.Services.Comments.Application.Exceptions;
using MiniSpace.Services.Comments.Core.Exceptions;
using System.Diagnostics.CodeAnalysis;

namespace MiniSpace.Services.Comments.Infrastructure.Exceptions
{
[ExcludeFromCodeCoverage]
internal sealed class ExceptionToMessageMapper : IExceptionToMessageMapper
{
public object Map(Exception exception, object message)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Concurrent;
using System.Diagnostics.CodeAnalysis;
using System.Net;
using Convey;
using Convey.WebApi.Exceptions;
Expand All @@ -7,6 +8,7 @@

namespace MiniSpace.Services.Comments.Infrastructure.Exceptions
{
[ExcludeFromCodeCoverage]
internal sealed class ExceptionToResponseMapper : IExceptionToResponseMapper
{
private static readonly ConcurrentDictionary<Type, string> Codes = new ConcurrentDictionary<Type, string>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
using Convey.Logging.CQRS;
using Microsoft.Extensions.DependencyInjection;
using MiniSpace.Services.Comments.Application.Commands;
using System.Diagnostics.CodeAnalysis;

namespace MiniSpace.Services.Comments.Infrastructure.Logging
{
[ExcludeFromCodeCoverage]
internal static class Extensions
{
public static IConveyBuilder AddHandlersLogging(this IConveyBuilder builder)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
using MiniSpace.Services.Comments.Application.Commands;
using MiniSpace.Services.Comments.Application.Events;
using MiniSpace.Services.Comments.Application.Events.External;
using System.Diagnostics.CodeAnalysis;

namespace MiniSpace.Services.Comments.Infrastructure.Logging
{
[ExcludeFromCodeCoverage]
internal sealed class MessageToLogTemplateMapper : IMessageToLogTemplateMapper
{
private static IReadOnlyDictionary<Type, HandlerLogTemplate> MessageTemplates
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using Convey.Types;
using MiniSpace.Services.Comments.Core.Entities;
using System.Diagnostics.CodeAnalysis;

namespace MiniSpace.Services.Comments.Infrastructure.Mongo.Documents
{
[ExcludeFromCodeCoverage]
public class CommentDocument : IIdentifiable<Guid>
{
public Guid Id { get; set; }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using MiniSpace.Services.Comments.Application.Dto;
using MiniSpace.Services.Comments.Core.Entities;
using System.Diagnostics.CodeAnalysis;

namespace MiniSpace.Services.Comments.Infrastructure.Mongo.Documents
{
[ExcludeFromCodeCoverage]
public static class Extensions
{
public static Comment AsEntity(this CommentDocument document)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using Convey.Types;
using System.Diagnostics.CodeAnalysis;

namespace MiniSpace.Services.Comments.Infrastructure.Mongo.Documents
{
[ExcludeFromCodeCoverage]
public class StudentDocument : IIdentifiable<Guid>
{
public Guid Id { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
using MiniSpace.Services.Comments.Infrastructure.Mongo.Documents;
using MongoDB.Driver;
using MongoDB.Driver.Linq;
using System.Diagnostics.CodeAnalysis;

namespace MiniSpace.Services.Comments.Infrastructure.Mongo.Repositories
{
[ExcludeFromCodeCoverage]
public class CommentMongoRepository : ICommentRepository
{
private readonly IMongoRepository<CommentDocument, Guid> _repository;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
Expand All @@ -10,6 +11,7 @@

namespace MiniSpace.Services.Comments.Infrastructure.Mongo.Repositories
{
[ExcludeFromCodeCoverage]
public static class Extensions
{
private static readonly FilterDefinitionBuilder<CommentDocument> FilterDefinitionBuilder = Builders<CommentDocument>.Filter;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
using MiniSpace.Services.Comments.Core.Entities;
using MiniSpace.Services.Comments.Core.Repositories;
using MiniSpace.Services.Comments.Infrastructure.Mongo.Documents;
using System.Diagnostics.CodeAnalysis;

namespace MiniSpace.Services.Comments.Infrastructure.Mongo.Repositories
{
[ExcludeFromCodeCoverage]
public class StudentMongoRepository : IStudentRepository
{
private readonly IMongoRepository<StudentDocument, Guid> _repository;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
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 AddLikeHandlerTest
{
private readonly AddLikeHandler _addLikeHandler;
private readonly Mock<ICommentRepository> _commentRepositoryMock;
private readonly Mock<IMessageBroker> _messageBrokerMock;
private readonly Mock<IAppContext> _appContextMock;

public AddLikeHandlerTest()
{
_commentRepositoryMock = new Mock<ICommentRepository>();
_messageBrokerMock = new Mock<IMessageBroker>();
_appContextMock = new Mock<IAppContext>();
_addLikeHandler = new AddLikeHandler(_commentRepositoryMock.Object, _appContextMock.Object, _messageBrokerMock.Object);
}

[Fact]
public async Task HandleAsync_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<string, string>());

_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());
}

[Fact]
public async Task HandleAsync_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<Task> act = async () => await _addLikeHandler.HandleAsync(comand, cancelationToken);
await act.Should().ThrowAsync<CommentNotFoundException>();
}

[Fact]
public async Task HandleAsync_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<string, string>());

_appContextMock.Setup(ctx => ctx.Identity).Returns(identityContext);
_commentRepositoryMock.Setup(repo => repo.GetAsync(comment.Id)).ReturnsAsync(comment);

var cancelationToken = new CancellationToken();

// Act & Assert
Func<Task> act = async () => await _addLikeHandler.HandleAsync(comand, cancelationToken);
await act.Should().ThrowAsync<UnauthorizedCommentAccessException>();

}
}
}
Loading
Loading