diff --git a/.github/workflows/build_microservices.yml b/.github/workflows/build_microservices.yml index 0c9dd3a6b..d53f07a0d 100644 --- a/.github/workflows/build_microservices.yml +++ b/.github/workflows/build_microservices.yml @@ -20,6 +20,8 @@ jobs: test_dir: 'MiniSpace.Services.Posts/tests' - project: 'MiniSpace.Services.Comments/src/MiniSpace.Services.Comments.Api' test_dir: 'MiniSpace.Services.Comments/tests' + - project: 'MiniSpace.Services.Organizations/src/MiniSpace.Services.Organizations.Api' + test_dir: 'MiniSpace.Services.Organizations/tests' - project: 'MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Api' test_dir: 'MiniSpace.Services.Posts/tests' - project: 'MiniSpace.Web/src/MiniSpace.Web' diff --git a/MiniSpace.Services.Organizations/MiniSpace.Services.Organizations.sln b/MiniSpace.Services.Organizations/MiniSpace.Services.Organizations.sln index ff08f2bbe..4d3fd8b1b 100644 --- a/MiniSpace.Services.Organizations/MiniSpace.Services.Organizations.sln +++ b/MiniSpace.Services.Organizations/MiniSpace.Services.Organizations.sln @@ -13,6 +13,12 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MiniSpace.Services.Organiza EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MiniSpace.Services.Organizations.Infrastructure", "src\MiniSpace.Services.Organizations.Infrastructure\MiniSpace.Services.Organizations.Infrastructure.csproj", "{3C5F46E5-9CF0-4A0A-AF56-9F1D455E7D1D}" EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests", "tests", "{D697BE2B-E69A-40FF-9A78-0BB96CF4E6DA}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MiniSpace.Services.Organizations.Application.UnitTests", "tests\MiniSpace.Services.Organizations.Application.UnitTests\MiniSpace.Services.Organizations.Application.UnitTests.csproj", "{5CCAF099-8250-4DC6-AB75-95E35C9C91B3}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MiniSpace.Services.Organizations.Core.UnitTests", "tests\MiniSpace.Services.Organizations.Core.UnitTests\MiniSpace.Services.Organizations.Core.UnitTests.csproj", "{FBE0DFB5-3945-4A9E-B4A0-C3BED6C74726}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -38,11 +44,21 @@ Global {3C5F46E5-9CF0-4A0A-AF56-9F1D455E7D1D}.Debug|Any CPU.Build.0 = Debug|Any CPU {3C5F46E5-9CF0-4A0A-AF56-9F1D455E7D1D}.Release|Any CPU.ActiveCfg = Release|Any CPU {3C5F46E5-9CF0-4A0A-AF56-9F1D455E7D1D}.Release|Any CPU.Build.0 = Release|Any CPU + {5CCAF099-8250-4DC6-AB75-95E35C9C91B3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {5CCAF099-8250-4DC6-AB75-95E35C9C91B3}.Debug|Any CPU.Build.0 = Debug|Any CPU + {5CCAF099-8250-4DC6-AB75-95E35C9C91B3}.Release|Any CPU.ActiveCfg = Release|Any CPU + {5CCAF099-8250-4DC6-AB75-95E35C9C91B3}.Release|Any CPU.Build.0 = Release|Any CPU + {FBE0DFB5-3945-4A9E-B4A0-C3BED6C74726}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {FBE0DFB5-3945-4A9E-B4A0-C3BED6C74726}.Debug|Any CPU.Build.0 = Debug|Any CPU + {FBE0DFB5-3945-4A9E-B4A0-C3BED6C74726}.Release|Any CPU.ActiveCfg = Release|Any CPU + {FBE0DFB5-3945-4A9E-B4A0-C3BED6C74726}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(NestedProjects) = preSolution {D8843565-EA51-407D-9F3B-BC0741B104A0} = {9A2329ED-4000-46C9-B9CD-956F6DA767CB} {299EC532-840F-4ED9-A98F-CDC7C4DD315E} = {9A2329ED-4000-46C9-B9CD-956F6DA767CB} {550E45D1-9BE7-4AE1-BA82-DDF57F436BEB} = {9A2329ED-4000-46C9-B9CD-956F6DA767CB} {3C5F46E5-9CF0-4A0A-AF56-9F1D455E7D1D} = {9A2329ED-4000-46C9-B9CD-956F6DA767CB} + {5CCAF099-8250-4DC6-AB75-95E35C9C91B3} = {D697BE2B-E69A-40FF-9A78-0BB96CF4E6DA} + {FBE0DFB5-3945-4A9E-B4A0-C3BED6C74726} = {D697BE2B-E69A-40FF-9A78-0BB96CF4E6DA} EndGlobalSection EndGlobal diff --git a/MiniSpace.Services.Organizations/src/MiniSpace.Services.Organizations.Application/Dto/OrganizationDetailsDto.cs b/MiniSpace.Services.Organizations/src/MiniSpace.Services.Organizations.Application/Dto/OrganizationDetailsDto.cs index 509a1ea18..9c6d89049 100644 --- a/MiniSpace.Services.Organizations/src/MiniSpace.Services.Organizations.Application/Dto/OrganizationDetailsDto.cs +++ b/MiniSpace.Services.Organizations/src/MiniSpace.Services.Organizations.Application/Dto/OrganizationDetailsDto.cs @@ -1,8 +1,10 @@ using System.Collections; using MiniSpace.Services.Organizations.Core.Entities; +using System.Diagnostics.CodeAnalysis; namespace MiniSpace.Services.Organizations.Application.DTO { + [ExcludeFromCodeCoverage] public class OrganizationDetailsDto { public Guid Id { get; set; } diff --git a/MiniSpace.Services.Organizations/src/MiniSpace.Services.Organizations.Application/Dto/OrganizationDto.cs b/MiniSpace.Services.Organizations/src/MiniSpace.Services.Organizations.Application/Dto/OrganizationDto.cs index 8c14653b6..0aea03d2f 100644 --- a/MiniSpace.Services.Organizations/src/MiniSpace.Services.Organizations.Application/Dto/OrganizationDto.cs +++ b/MiniSpace.Services.Organizations/src/MiniSpace.Services.Organizations.Application/Dto/OrganizationDto.cs @@ -1,7 +1,9 @@ using MiniSpace.Services.Organizations.Core.Entities; +using System.Diagnostics.CodeAnalysis; namespace MiniSpace.Services.Organizations.Application.DTO { + [ExcludeFromCodeCoverage] public class OrganizationDto { public Guid Id { get; set; } diff --git a/MiniSpace.Services.Organizations/src/MiniSpace.Services.Organizations.Application/Queries/GetAllChildrenOrganizations.cs b/MiniSpace.Services.Organizations/src/MiniSpace.Services.Organizations.Application/Queries/GetAllChildrenOrganizations.cs index be66f4b12..dc365f69d 100644 --- a/MiniSpace.Services.Organizations/src/MiniSpace.Services.Organizations.Application/Queries/GetAllChildrenOrganizations.cs +++ b/MiniSpace.Services.Organizations/src/MiniSpace.Services.Organizations.Application/Queries/GetAllChildrenOrganizations.cs @@ -1,8 +1,10 @@ using Convey.CQRS.Queries; using MiniSpace.Services.Organizations.Application.DTO; +using System.Diagnostics.CodeAnalysis; namespace MiniSpace.Services.Organizations.Application.Queries { + [ExcludeFromCodeCoverage] public class GetAllChildrenOrganizations: IQuery> { public Guid OrganizationId { get; set; } diff --git a/MiniSpace.Services.Organizations/src/MiniSpace.Services.Organizations.Application/Queries/GetChildrenOrganizations.cs b/MiniSpace.Services.Organizations/src/MiniSpace.Services.Organizations.Application/Queries/GetChildrenOrganizations.cs index 2212c82be..adaf8bf05 100644 --- a/MiniSpace.Services.Organizations/src/MiniSpace.Services.Organizations.Application/Queries/GetChildrenOrganizations.cs +++ b/MiniSpace.Services.Organizations/src/MiniSpace.Services.Organizations.Application/Queries/GetChildrenOrganizations.cs @@ -1,8 +1,10 @@ using Convey.CQRS.Queries; using MiniSpace.Services.Organizations.Application.DTO; +using System.Diagnostics.CodeAnalysis; namespace MiniSpace.Services.Organizations.Application.Queries { + [ExcludeFromCodeCoverage] public class GetChildrenOrganizations: IQuery> { public Guid OrganizationId { get; set; } diff --git a/MiniSpace.Services.Organizations/src/MiniSpace.Services.Organizations.Application/Queries/GetOrganization.cs b/MiniSpace.Services.Organizations/src/MiniSpace.Services.Organizations.Application/Queries/GetOrganization.cs index 14b6b9b70..d39c8f60f 100644 --- a/MiniSpace.Services.Organizations/src/MiniSpace.Services.Organizations.Application/Queries/GetOrganization.cs +++ b/MiniSpace.Services.Organizations/src/MiniSpace.Services.Organizations.Application/Queries/GetOrganization.cs @@ -1,8 +1,10 @@ using Convey.CQRS.Queries; using MiniSpace.Services.Organizations.Application.DTO; +using System.Diagnostics.CodeAnalysis; namespace MiniSpace.Services.Organizations.Application.Queries { + [ExcludeFromCodeCoverage] public class GetOrganization : IQuery { public Guid OrganizationId { get; set; } diff --git a/MiniSpace.Services.Organizations/src/MiniSpace.Services.Organizations.Application/Queries/GetOrganizationDetails.cs b/MiniSpace.Services.Organizations/src/MiniSpace.Services.Organizations.Application/Queries/GetOrganizationDetails.cs index abf38f0c6..949bf2dad 100644 --- a/MiniSpace.Services.Organizations/src/MiniSpace.Services.Organizations.Application/Queries/GetOrganizationDetails.cs +++ b/MiniSpace.Services.Organizations/src/MiniSpace.Services.Organizations.Application/Queries/GetOrganizationDetails.cs @@ -1,8 +1,10 @@ using Convey.CQRS.Queries; using MiniSpace.Services.Organizations.Application.DTO; +using System.Diagnostics.CodeAnalysis; namespace MiniSpace.Services.Organizations.Application.Queries { + [ExcludeFromCodeCoverage] public class GetOrganizationDetails : IQuery { public Guid OrganizationId { get; set; } diff --git a/MiniSpace.Services.Organizations/src/MiniSpace.Services.Organizations.Application/Queries/GetOrganizerOrganizations.cs b/MiniSpace.Services.Organizations/src/MiniSpace.Services.Organizations.Application/Queries/GetOrganizerOrganizations.cs index b48d8f014..2aac41aa1 100644 --- a/MiniSpace.Services.Organizations/src/MiniSpace.Services.Organizations.Application/Queries/GetOrganizerOrganizations.cs +++ b/MiniSpace.Services.Organizations/src/MiniSpace.Services.Organizations.Application/Queries/GetOrganizerOrganizations.cs @@ -1,10 +1,15 @@ using Convey.CQRS.Queries; using MiniSpace.Services.Organizations.Application.DTO; +using System.Diagnostics.CodeAnalysis; + namespace MiniSpace.Services.Organizations.Application.Queries { + [ExcludeFromCodeCoverage] + public class GetOrganizerOrganizations: IQuery> { + public Guid OrganizerId { get; set; } public Guid RootId { get; set; } } diff --git a/MiniSpace.Services.Organizations/src/MiniSpace.Services.Organizations.Application/Queries/GetRootOrganizations.cs b/MiniSpace.Services.Organizations/src/MiniSpace.Services.Organizations.Application/Queries/GetRootOrganizations.cs index ca798f969..9b49d0835 100644 --- a/MiniSpace.Services.Organizations/src/MiniSpace.Services.Organizations.Application/Queries/GetRootOrganizations.cs +++ b/MiniSpace.Services.Organizations/src/MiniSpace.Services.Organizations.Application/Queries/GetRootOrganizations.cs @@ -1,8 +1,11 @@ using Convey.CQRS.Queries; using MiniSpace.Services.Organizations.Application.DTO; +using System.Diagnostics.CodeAnalysis; + namespace MiniSpace.Services.Organizations.Application.Queries { + [ExcludeFromCodeCoverage] public class GetRootOrganizations: IQuery> { } diff --git a/MiniSpace.Services.Organizations/src/MiniSpace.Services.Organizations.Infrastructure/Contexts/AppContext.cs b/MiniSpace.Services.Organizations/src/MiniSpace.Services.Organizations.Infrastructure/Contexts/AppContext.cs index ccb1647b6..6843cf90e 100644 --- a/MiniSpace.Services.Organizations/src/MiniSpace.Services.Organizations.Infrastructure/Contexts/AppContext.cs +++ b/MiniSpace.Services.Organizations/src/MiniSpace.Services.Organizations.Infrastructure/Contexts/AppContext.cs @@ -1,7 +1,9 @@ using MiniSpace.Services.Organizations.Application; +using System.Diagnostics.CodeAnalysis; namespace MiniSpace.Services.Organizations.Infrastructure.Contexts { + [ExcludeFromCodeCoverage] internal class AppContext : IAppContext { public string RequestId { get; } diff --git a/MiniSpace.Services.Organizations/src/MiniSpace.Services.Organizations.Infrastructure/Contexts/AppContextFactory.cs b/MiniSpace.Services.Organizations/src/MiniSpace.Services.Organizations.Infrastructure/Contexts/AppContextFactory.cs index ac2758b97..4d96dc46a 100644 --- a/MiniSpace.Services.Organizations/src/MiniSpace.Services.Organizations.Infrastructure/Contexts/AppContextFactory.cs +++ b/MiniSpace.Services.Organizations/src/MiniSpace.Services.Organizations.Infrastructure/Contexts/AppContextFactory.cs @@ -2,9 +2,11 @@ using Microsoft.AspNetCore.Http; using Newtonsoft.Json; using MiniSpace.Services.Organizations.Application; +using System.Diagnostics.CodeAnalysis; namespace MiniSpace.Services.Organizations.Infrastructure.Contexts { + [ExcludeFromCodeCoverage] internal sealed class AppContextFactory : IAppContextFactory { private readonly ICorrelationContextAccessor _contextAccessor; diff --git a/MiniSpace.Services.Organizations/src/MiniSpace.Services.Organizations.Infrastructure/Contexts/CorrelationContext.cs b/MiniSpace.Services.Organizations/src/MiniSpace.Services.Organizations.Infrastructure/Contexts/CorrelationContext.cs index cc5f8b300..aa0ebbc58 100644 --- a/MiniSpace.Services.Organizations/src/MiniSpace.Services.Organizations.Infrastructure/Contexts/CorrelationContext.cs +++ b/MiniSpace.Services.Organizations/src/MiniSpace.Services.Organizations.Infrastructure/Contexts/CorrelationContext.cs @@ -1,5 +1,8 @@ +using System.Diagnostics.CodeAnalysis; + namespace MiniSpace.Services.Organizations.Infrastructure.Contexts { + [ExcludeFromCodeCoverage] internal class CorrelationContext { public string CorrelationId { get; set; } diff --git a/MiniSpace.Services.Organizations/src/MiniSpace.Services.Organizations.Infrastructure/Contexts/IdentityContext.cs b/MiniSpace.Services.Organizations/src/MiniSpace.Services.Organizations.Infrastructure/Contexts/IdentityContext.cs index cc69bd583..18e9fddfb 100644 --- a/MiniSpace.Services.Organizations/src/MiniSpace.Services.Organizations.Infrastructure/Contexts/IdentityContext.cs +++ b/MiniSpace.Services.Organizations/src/MiniSpace.Services.Organizations.Infrastructure/Contexts/IdentityContext.cs @@ -1,7 +1,12 @@ using MiniSpace.Services.Organizations.Application; +using System.Runtime.CompilerServices; +using System.Diagnostics.CodeAnalysis; + +[assembly: InternalsVisibleTo("MiniSpace.Services.Organizations.Application.UnitTests")] namespace MiniSpace.Services.Organizations.Infrastructure.Contexts { + [ExcludeFromCodeCoverage] internal class IdentityContext : IIdentityContext { public Guid Id { get; } diff --git a/MiniSpace.Services.Organizations/src/MiniSpace.Services.Organizations.Infrastructure/Decorators/OutboxCommandHandlerDecorator.cs b/MiniSpace.Services.Organizations/src/MiniSpace.Services.Organizations.Infrastructure/Decorators/OutboxCommandHandlerDecorator.cs index 4fa3dff8b..247c6452d 100644 --- a/MiniSpace.Services.Organizations/src/MiniSpace.Services.Organizations.Infrastructure/Decorators/OutboxCommandHandlerDecorator.cs +++ b/MiniSpace.Services.Organizations/src/MiniSpace.Services.Organizations.Infrastructure/Decorators/OutboxCommandHandlerDecorator.cs @@ -2,10 +2,12 @@ using Convey.MessageBrokers; using Convey.MessageBrokers.Outbox; using Convey.Types; +using System.Diagnostics.CodeAnalysis; namespace MiniSpace.Services.Organizations.Infrastructure.Decorators { [Decorator] + [ExcludeFromCodeCoverage] internal sealed class OutboxCommandHandlerDecorator : ICommandHandler where TCommand : class, ICommand { diff --git a/MiniSpace.Services.Organizations/src/MiniSpace.Services.Organizations.Infrastructure/Decorators/OutboxEventHandlerDecorator.cs b/MiniSpace.Services.Organizations/src/MiniSpace.Services.Organizations.Infrastructure/Decorators/OutboxEventHandlerDecorator.cs index 478dcf8ea..cbe3777d8 100644 --- a/MiniSpace.Services.Organizations/src/MiniSpace.Services.Organizations.Infrastructure/Decorators/OutboxEventHandlerDecorator.cs +++ b/MiniSpace.Services.Organizations/src/MiniSpace.Services.Organizations.Infrastructure/Decorators/OutboxEventHandlerDecorator.cs @@ -2,10 +2,12 @@ using Convey.MessageBrokers; using Convey.MessageBrokers.Outbox; using Convey.Types; +using System.Diagnostics.CodeAnalysis; namespace MiniSpace.Services.Organizations.Infrastructure.Decorators { [Decorator] + [ExcludeFromCodeCoverage] internal sealed class OutboxEventHandlerDecorator : IEventHandler where TEvent : class, IEvent { diff --git a/MiniSpace.Services.Organizations/src/MiniSpace.Services.Organizations.Infrastructure/Exceptions/ExceptionToMessageMapper.cs b/MiniSpace.Services.Organizations/src/MiniSpace.Services.Organizations.Infrastructure/Exceptions/ExceptionToMessageMapper.cs index c69dff3a0..62ec3951b 100644 --- a/MiniSpace.Services.Organizations/src/MiniSpace.Services.Organizations.Infrastructure/Exceptions/ExceptionToMessageMapper.cs +++ b/MiniSpace.Services.Organizations/src/MiniSpace.Services.Organizations.Infrastructure/Exceptions/ExceptionToMessageMapper.cs @@ -4,9 +4,11 @@ // using MiniSpace.Services.Organizations.Application.Events.External; // using MiniSpace.Services.Organizations.Application.Exceptions; // using MiniSpace.Services.Organizations.Core; +using System.Diagnostics.CodeAnalysis; namespace MiniSpace.Services.Organizations.Infrastructure.Exceptions { + [ExcludeFromCodeCoverage] internal sealed class ExceptionToMessageMapper : IExceptionToMessageMapper { public object Map(Exception exception, object message) diff --git a/MiniSpace.Services.Organizations/src/MiniSpace.Services.Organizations.Infrastructure/Exceptions/ExceptionToResponseMapper.cs b/MiniSpace.Services.Organizations/src/MiniSpace.Services.Organizations.Infrastructure/Exceptions/ExceptionToResponseMapper.cs index a0d24d1b1..e6e53792d 100644 --- a/MiniSpace.Services.Organizations/src/MiniSpace.Services.Organizations.Infrastructure/Exceptions/ExceptionToResponseMapper.cs +++ b/MiniSpace.Services.Organizations/src/MiniSpace.Services.Organizations.Infrastructure/Exceptions/ExceptionToResponseMapper.cs @@ -4,9 +4,11 @@ using Convey.WebApi.Exceptions; using MiniSpace.Services.Organizations.Application.Exceptions; using MiniSpace.Services.Organizations.Core.Exceptions; +using System.Diagnostics.CodeAnalysis; namespace MiniSpace.Services.Organizations.Infrastructure.Exceptions { + [ExcludeFromCodeCoverage] internal sealed class ExceptionToResponseMapper : IExceptionToResponseMapper { private static readonly ConcurrentDictionary Codes = new ConcurrentDictionary(); diff --git a/MiniSpace.Services.Organizations/src/MiniSpace.Services.Organizations.Infrastructure/Extensions.cs b/MiniSpace.Services.Organizations/src/MiniSpace.Services.Organizations.Infrastructure/Extensions.cs index 22e0ef200..b24efa368 100644 --- a/MiniSpace.Services.Organizations/src/MiniSpace.Services.Organizations.Infrastructure/Extensions.cs +++ b/MiniSpace.Services.Organizations/src/MiniSpace.Services.Organizations.Infrastructure/Extensions.cs @@ -39,9 +39,11 @@ using MiniSpace.Services.Organizations.Infrastructure.Mongo.Documents; using MiniSpace.Services.Organizations.Infrastructure.Mongo.Repositories; using MiniSpace.Services.Organizations.Infrastructure.Services; +using System.Diagnostics.CodeAnalysis; namespace MiniSpace.Services.Organizations.Infrastructure { + [ExcludeFromCodeCoverage] public static class Extensions { public static IConveyBuilder AddInfrastructure(this IConveyBuilder builder) diff --git a/MiniSpace.Services.Organizations/src/MiniSpace.Services.Organizations.Infrastructure/Logging/Extensions.cs b/MiniSpace.Services.Organizations/src/MiniSpace.Services.Organizations.Infrastructure/Logging/Extensions.cs index c104dfd46..5feb4a9ab 100644 --- a/MiniSpace.Services.Organizations/src/MiniSpace.Services.Organizations.Infrastructure/Logging/Extensions.cs +++ b/MiniSpace.Services.Organizations/src/MiniSpace.Services.Organizations.Infrastructure/Logging/Extensions.cs @@ -2,9 +2,11 @@ using Convey.Logging.CQRS; using Microsoft.Extensions.DependencyInjection; using MiniSpace.Services.Organizations.Application.Commands; +using System.Diagnostics.CodeAnalysis; namespace MiniSpace.Services.Organizations.Infrastructure.Logging { + [ExcludeFromCodeCoverage] internal static class Extensions { public static IConveyBuilder AddHandlersLogging(this IConveyBuilder builder) diff --git a/MiniSpace.Services.Organizations/src/MiniSpace.Services.Organizations.Infrastructure/Logging/MessageToLogTemplateMapper.cs b/MiniSpace.Services.Organizations/src/MiniSpace.Services.Organizations.Infrastructure/Logging/MessageToLogTemplateMapper.cs index 246dd39ad..82111a8f3 100644 --- a/MiniSpace.Services.Organizations/src/MiniSpace.Services.Organizations.Infrastructure/Logging/MessageToLogTemplateMapper.cs +++ b/MiniSpace.Services.Organizations/src/MiniSpace.Services.Organizations.Infrastructure/Logging/MessageToLogTemplateMapper.cs @@ -1,9 +1,11 @@ using Convey.Logging.CQRS; using MiniSpace.Services.Organizations.Application.Commands; using MiniSpace.Services.Organizations.Application.Events.External; +using System.Diagnostics.CodeAnalysis; namespace MiniSpace.Services.Organizations.Infrastructure.Logging { + [ExcludeFromCodeCoverage] internal sealed class MessageToLogTemplateMapper : IMessageToLogTemplateMapper { private static IReadOnlyDictionary MessageTemplates diff --git a/MiniSpace.Services.Organizations/src/MiniSpace.Services.Organizations.Infrastructure/Mongo/Documents/Extensions.cs b/MiniSpace.Services.Organizations/src/MiniSpace.Services.Organizations.Infrastructure/Mongo/Documents/Extensions.cs index 05676fbd8..11e9f07c8 100644 --- a/MiniSpace.Services.Organizations/src/MiniSpace.Services.Organizations.Infrastructure/Mongo/Documents/Extensions.cs +++ b/MiniSpace.Services.Organizations/src/MiniSpace.Services.Organizations.Infrastructure/Mongo/Documents/Extensions.cs @@ -1,8 +1,11 @@ using MiniSpace.Services.Organizations.Application.DTO; using MiniSpace.Services.Organizations.Core.Entities; +using System.Diagnostics.CodeAnalysis; namespace MiniSpace.Services.Organizations.Infrastructure.Mongo.Documents { + [ExcludeFromCodeCoverage] + public static class Extensions { public static Organization AsEntity(this OrganizationDocument document) diff --git a/MiniSpace.Services.Organizations/src/MiniSpace.Services.Organizations.Infrastructure/Mongo/Documents/OrganizationDocument.cs b/MiniSpace.Services.Organizations/src/MiniSpace.Services.Organizations.Infrastructure/Mongo/Documents/OrganizationDocument.cs index 5e7a31dcb..375f3a0f8 100644 --- a/MiniSpace.Services.Organizations/src/MiniSpace.Services.Organizations.Infrastructure/Mongo/Documents/OrganizationDocument.cs +++ b/MiniSpace.Services.Organizations/src/MiniSpace.Services.Organizations.Infrastructure/Mongo/Documents/OrganizationDocument.cs @@ -1,8 +1,11 @@ using Convey.Types; using MiniSpace.Services.Organizations.Core.Entities; +using System.Diagnostics.CodeAnalysis; namespace MiniSpace.Services.Organizations.Infrastructure.Mongo.Documents { + [ExcludeFromCodeCoverage] + public class OrganizationDocument: IIdentifiable { public Guid Id { get; set; } diff --git a/MiniSpace.Services.Organizations/src/MiniSpace.Services.Organizations.Infrastructure/Mongo/Documents/OrganizerDocument.cs b/MiniSpace.Services.Organizations/src/MiniSpace.Services.Organizations.Infrastructure/Mongo/Documents/OrganizerDocument.cs index 8e6411cee..0345c012a 100644 --- a/MiniSpace.Services.Organizations/src/MiniSpace.Services.Organizations.Infrastructure/Mongo/Documents/OrganizerDocument.cs +++ b/MiniSpace.Services.Organizations/src/MiniSpace.Services.Organizations.Infrastructure/Mongo/Documents/OrganizerDocument.cs @@ -1,7 +1,9 @@ using Convey.Types; +using System.Diagnostics.CodeAnalysis; namespace MiniSpace.Services.Organizations.Infrastructure.Mongo.Documents { + [ExcludeFromCodeCoverage] public class OrganizerDocument : IIdentifiable { public Guid Id { get; set; } diff --git a/MiniSpace.Services.Organizations/src/MiniSpace.Services.Organizations.Infrastructure/Mongo/Queries/Handlers/GetAllChildrenOrganizationsHandler.cs b/MiniSpace.Services.Organizations/src/MiniSpace.Services.Organizations.Infrastructure/Mongo/Queries/Handlers/GetAllChildrenOrganizationsHandler.cs index 149cc4237..67babbb42 100644 --- a/MiniSpace.Services.Organizations/src/MiniSpace.Services.Organizations.Infrastructure/Mongo/Queries/Handlers/GetAllChildrenOrganizationsHandler.cs +++ b/MiniSpace.Services.Organizations/src/MiniSpace.Services.Organizations.Infrastructure/Mongo/Queries/Handlers/GetAllChildrenOrganizationsHandler.cs @@ -3,9 +3,11 @@ using MiniSpace.Services.Organizations.Application.Queries; using MiniSpace.Services.Organizations.Core.Entities; using MiniSpace.Services.Organizations.Infrastructure.Mongo.Documents; +using System.Diagnostics.CodeAnalysis; namespace MiniSpace.Services.Organizations.Infrastructure.Mongo.Queries.Handlers { + [ExcludeFromCodeCoverage] public class GetAllChildrenOrganizationsHandler : IQueryHandler> { private readonly IMongoRepository _repository; diff --git a/MiniSpace.Services.Organizations/src/MiniSpace.Services.Organizations.Infrastructure/Mongo/Queries/Handlers/GetChildrenOrganizationsHandler.cs b/MiniSpace.Services.Organizations/src/MiniSpace.Services.Organizations.Infrastructure/Mongo/Queries/Handlers/GetChildrenOrganizationsHandler.cs index 486ecff5e..28401dcdb 100644 --- a/MiniSpace.Services.Organizations/src/MiniSpace.Services.Organizations.Infrastructure/Mongo/Queries/Handlers/GetChildrenOrganizationsHandler.cs +++ b/MiniSpace.Services.Organizations/src/MiniSpace.Services.Organizations.Infrastructure/Mongo/Queries/Handlers/GetChildrenOrganizationsHandler.cs @@ -4,9 +4,11 @@ using MiniSpace.Services.Organizations.Application.Queries; using MiniSpace.Services.Organizations.Core.Entities; using MiniSpace.Services.Organizations.Infrastructure.Mongo.Documents; +using System.Diagnostics.CodeAnalysis; namespace MiniSpace.Services.Organizations.Infrastructure.Mongo.Queries.Handlers { + [ExcludeFromCodeCoverage] public class GetChildrenOrganizationsHandler : IQueryHandler> { private readonly IMongoRepository _repository; diff --git a/MiniSpace.Services.Organizations/src/MiniSpace.Services.Organizations.Infrastructure/Mongo/Queries/Handlers/GetOrganizationDetailsHandler.cs b/MiniSpace.Services.Organizations/src/MiniSpace.Services.Organizations.Infrastructure/Mongo/Queries/Handlers/GetOrganizationDetailsHandler.cs index 64ffcdb74..d78c2a003 100644 --- a/MiniSpace.Services.Organizations/src/MiniSpace.Services.Organizations.Infrastructure/Mongo/Queries/Handlers/GetOrganizationDetailsHandler.cs +++ b/MiniSpace.Services.Organizations/src/MiniSpace.Services.Organizations.Infrastructure/Mongo/Queries/Handlers/GetOrganizationDetailsHandler.cs @@ -4,9 +4,12 @@ using MiniSpace.Services.Organizations.Application.Queries; using MiniSpace.Services.Organizations.Infrastructure.Mongo.Documents; using MongoDB.Driver; +using System.Diagnostics.CodeAnalysis; + namespace MiniSpace.Services.Organizations.Infrastructure.Mongo.Queries.Handlers { + [ExcludeFromCodeCoverage] public class GetOrganizationDetailsHandler : IQueryHandler { private readonly IMongoRepository _repository; diff --git a/MiniSpace.Services.Organizations/src/MiniSpace.Services.Organizations.Infrastructure/Mongo/Queries/Handlers/GetOrganizationHandler.cs b/MiniSpace.Services.Organizations/src/MiniSpace.Services.Organizations.Infrastructure/Mongo/Queries/Handlers/GetOrganizationHandler.cs index 698b1db23..8978aa5c7 100644 --- a/MiniSpace.Services.Organizations/src/MiniSpace.Services.Organizations.Infrastructure/Mongo/Queries/Handlers/GetOrganizationHandler.cs +++ b/MiniSpace.Services.Organizations/src/MiniSpace.Services.Organizations.Infrastructure/Mongo/Queries/Handlers/GetOrganizationHandler.cs @@ -3,9 +3,12 @@ using MiniSpace.Services.Organizations.Application.DTO; using MiniSpace.Services.Organizations.Application.Queries; using MiniSpace.Services.Organizations.Infrastructure.Mongo.Documents; +using System.Diagnostics.CodeAnalysis; + namespace MiniSpace.Services.Organizations.Infrastructure.Mongo.Queries.Handlers { + [ExcludeFromCodeCoverage] public class GetOrganizationHandler : IQueryHandler { private readonly IMongoRepository _repository; diff --git a/MiniSpace.Services.Organizations/src/MiniSpace.Services.Organizations.Infrastructure/Mongo/Queries/Handlers/GetOrganizerOrganizationsHandler.cs b/MiniSpace.Services.Organizations/src/MiniSpace.Services.Organizations.Infrastructure/Mongo/Queries/Handlers/GetOrganizerOrganizationsHandler.cs index 78e29d923..e12bede29 100644 --- a/MiniSpace.Services.Organizations/src/MiniSpace.Services.Organizations.Infrastructure/Mongo/Queries/Handlers/GetOrganizerOrganizationsHandler.cs +++ b/MiniSpace.Services.Organizations/src/MiniSpace.Services.Organizations.Infrastructure/Mongo/Queries/Handlers/GetOrganizerOrganizationsHandler.cs @@ -5,9 +5,11 @@ using MiniSpace.Services.Organizations.Application.Queries; using MiniSpace.Services.Organizations.Core.Entities; using MiniSpace.Services.Organizations.Infrastructure.Mongo.Documents; +using System.Diagnostics.CodeAnalysis; namespace MiniSpace.Services.Organizations.Infrastructure.Mongo.Queries.Handlers { + [ExcludeFromCodeCoverage] public class GetOrganizerOrganizationsHandler : IQueryHandler> { private readonly IMongoRepository _repository; diff --git a/MiniSpace.Services.Organizations/src/MiniSpace.Services.Organizations.Infrastructure/Mongo/Queries/Handlers/GetRootOrganizationsHandler.cs b/MiniSpace.Services.Organizations/src/MiniSpace.Services.Organizations.Infrastructure/Mongo/Queries/Handlers/GetRootOrganizationsHandler.cs index 4376e5f64..05f940594 100644 --- a/MiniSpace.Services.Organizations/src/MiniSpace.Services.Organizations.Infrastructure/Mongo/Queries/Handlers/GetRootOrganizationsHandler.cs +++ b/MiniSpace.Services.Organizations/src/MiniSpace.Services.Organizations.Infrastructure/Mongo/Queries/Handlers/GetRootOrganizationsHandler.cs @@ -3,9 +3,11 @@ using MiniSpace.Services.Organizations.Application.DTO; using MiniSpace.Services.Organizations.Application.Queries; using MiniSpace.Services.Organizations.Infrastructure.Mongo.Documents; +using System.Diagnostics.CodeAnalysis; namespace MiniSpace.Services.Organizations.Infrastructure.Mongo.Queries.Handlers { + [ExcludeFromCodeCoverage] public class GetRootOrganizationsHandler : IQueryHandler> { private readonly IMongoRepository _repository; diff --git a/MiniSpace.Services.Organizations/src/MiniSpace.Services.Organizations.Infrastructure/Mongo/Repositories/OrganizationMongoRepository.cs b/MiniSpace.Services.Organizations/src/MiniSpace.Services.Organizations.Infrastructure/Mongo/Repositories/OrganizationMongoRepository.cs index 452841d0e..506c1853f 100644 --- a/MiniSpace.Services.Organizations/src/MiniSpace.Services.Organizations.Infrastructure/Mongo/Repositories/OrganizationMongoRepository.cs +++ b/MiniSpace.Services.Organizations/src/MiniSpace.Services.Organizations.Infrastructure/Mongo/Repositories/OrganizationMongoRepository.cs @@ -2,9 +2,11 @@ using MiniSpace.Services.Organizations.Core.Entities; using MiniSpace.Services.Organizations.Core.Repositories; using MiniSpace.Services.Organizations.Infrastructure.Mongo.Documents; +using System.Diagnostics.CodeAnalysis; namespace MiniSpace.Services.Organizations.Infrastructure.Mongo.Repositories { + [ExcludeFromCodeCoverage] public class OrganizationMongoRepository : IOrganizationRepository { private readonly IMongoRepository _repository; diff --git a/MiniSpace.Services.Organizations/src/MiniSpace.Services.Organizations.Infrastructure/Mongo/Repositories/OrganizerMongoRepository.cs b/MiniSpace.Services.Organizations/src/MiniSpace.Services.Organizations.Infrastructure/Mongo/Repositories/OrganizerMongoRepository.cs index bc7cc7973..88f148351 100644 --- a/MiniSpace.Services.Organizations/src/MiniSpace.Services.Organizations.Infrastructure/Mongo/Repositories/OrganizerMongoRepository.cs +++ b/MiniSpace.Services.Organizations/src/MiniSpace.Services.Organizations.Infrastructure/Mongo/Repositories/OrganizerMongoRepository.cs @@ -4,9 +4,11 @@ using MiniSpace.Services.Organizations.Core.Entities; using MiniSpace.Services.Organizations.Core.Repositories; using MiniSpace.Services.Organizations.Infrastructure.Mongo.Documents; +using System.Diagnostics.CodeAnalysis; namespace MiniSpace.Services.Organizations.Infrastructure.Mongo.Repositories { + [ExcludeFromCodeCoverage] public class OrganizerMongoRepository : IOrganizerRepository { private readonly IMongoRepository _repository; diff --git a/MiniSpace.Services.Organizations/src/MiniSpace.Services.Organizations.Infrastructure/Services/DateTimeProvider.cs b/MiniSpace.Services.Organizations/src/MiniSpace.Services.Organizations.Infrastructure/Services/DateTimeProvider.cs index 0da02bcd1..b3f41508c 100644 --- a/MiniSpace.Services.Organizations/src/MiniSpace.Services.Organizations.Infrastructure/Services/DateTimeProvider.cs +++ b/MiniSpace.Services.Organizations/src/MiniSpace.Services.Organizations.Infrastructure/Services/DateTimeProvider.cs @@ -1,7 +1,9 @@ using MiniSpace.Services.Organizations.Application.Services; +using System.Diagnostics.CodeAnalysis; namespace MiniSpace.Services.Organizations.Infrastructure.Services { + [ExcludeFromCodeCoverage] internal sealed class DateTimeProvider : IDateTimeProvider { public DateTime Now => DateTime.UtcNow; diff --git a/MiniSpace.Services.Organizations/src/MiniSpace.Services.Organizations.Infrastructure/Services/EventMapper.cs b/MiniSpace.Services.Organizations/src/MiniSpace.Services.Organizations.Infrastructure/Services/EventMapper.cs index 7b2ca7171..632373690 100644 --- a/MiniSpace.Services.Organizations/src/MiniSpace.Services.Organizations.Infrastructure/Services/EventMapper.cs +++ b/MiniSpace.Services.Organizations/src/MiniSpace.Services.Organizations.Infrastructure/Services/EventMapper.cs @@ -2,9 +2,11 @@ using MiniSpace.Services.Organizations.Application.Services; using MiniSpace.Services.Organizations.Core; using MiniSpace.Services.Organizations.Core.Events; +using System.Diagnostics.CodeAnalysis; namespace MiniSpace.Services.Organizations.Infrastructure.Services { + [ExcludeFromCodeCoverage] public class EventMapper : IEventMapper { public IEnumerable MapAll(IEnumerable events) diff --git a/MiniSpace.Services.Organizations/src/MiniSpace.Services.Organizations.Infrastructure/Services/MessageBroker.cs b/MiniSpace.Services.Organizations/src/MiniSpace.Services.Organizations.Infrastructure/Services/MessageBroker.cs index 2390268f8..0601896b1 100644 --- a/MiniSpace.Services.Organizations/src/MiniSpace.Services.Organizations.Infrastructure/Services/MessageBroker.cs +++ b/MiniSpace.Services.Organizations/src/MiniSpace.Services.Organizations.Infrastructure/Services/MessageBroker.cs @@ -6,9 +6,11 @@ using Microsoft.Extensions.Logging; using OpenTracing; using MiniSpace.Services.Organizations.Application.Services; +using System.Diagnostics.CodeAnalysis; namespace MiniSpace.Services.Organizations.Infrastructure.Services { + [ExcludeFromCodeCoverage] internal sealed class MessageBroker : IMessageBroker { private const string DefaultSpanContextHeader = "span_context"; diff --git a/MiniSpace.Services.Organizations/tests/MiniSpace.Services.Organizations.Application.UnitTests/Commands/Handlers/AddOrganizerToOrganizationHandlerTest.cs b/MiniSpace.Services.Organizations/tests/MiniSpace.Services.Organizations.Application.UnitTests/Commands/Handlers/AddOrganizerToOrganizationHandlerTest.cs new file mode 100644 index 000000000..2ece87ea9 --- /dev/null +++ b/MiniSpace.Services.Organizations/tests/MiniSpace.Services.Organizations.Application.UnitTests/Commands/Handlers/AddOrganizerToOrganizationHandlerTest.cs @@ -0,0 +1,177 @@ +using Xunit; +using Moq; +using System; +using System.Collections.Generic; +using System.Threading.Tasks; +using System.Threading; +using FluentAssertions; +using MiniSpace.Services.Organizations.Application.Commands.Handlers; +using MiniSpace.Services.Organizations.Core.Repositories; +using MiniSpace.Services.Organizations.Application.Services; +using MiniSpace.Services.Organizations.Core.Entities; +using MiniSpace.Services.Organizations.Infrastructure.Contexts; +using MiniSpace.Services.Organizations.Application.Commands; +using MiniSpace.Services.Organizations.Application.Exceptions; + +namespace MiniSpace.Services.Organizations.Application.UnitTests.Commands.Handlers +{ + public class AddOrganizerToOrganizationHandlerTest + { + private readonly AddOrganizerToOrganizationHandler _addOrganizerToOrganizationHandler; + private readonly Mock _organizationRepositoryMock; + private readonly Mock _organizerRepositoryMock; + private readonly Mock _appContextMock; + private readonly Mock _messageBrokerMock; + + public AddOrganizerToOrganizationHandlerTest() + { + _organizationRepositoryMock = new Mock(); + _organizerRepositoryMock = new Mock(); + _appContextMock = new Mock(); + _messageBrokerMock = new Mock(); + _addOrganizerToOrganizationHandler = new AddOrganizerToOrganizationHandler( + _organizationRepositoryMock.Object, + _organizerRepositoryMock.Object, + _appContextMock.Object, + _messageBrokerMock.Object + ); + } + + [Fact] + public async Task HandleAsync_WithValidOrganizationAndAuthorised_ShouldUpdateOrganization() + { + // Arrange + var rootOrganizationId = Guid.NewGuid(); + var organizationId = Guid.NewGuid(); + var organizerId = Guid.NewGuid(); + var comand = new AddOrganizerToOrganization(rootOrganizationId, organizationId, organizerId); + + var organization = new Organization(organizationId, "this", new List() { }, new List() { }); + var rootOrganization = new Organization(rootOrganizationId, "root", new List() { }, new List() { organization }); + var organizer = new Organizer(organizerId); + + + var identityContext = new IdentityContext(Guid.NewGuid().ToString(), "Admin", true, new Dictionary()); + + _appContextMock.Setup(ctx => ctx.Identity).Returns(identityContext); + _organizationRepositoryMock.Setup(repo => repo.GetAsync(comand.RootOrganizationId)).ReturnsAsync(rootOrganization); + _organizerRepositoryMock.Setup(repo => repo.GetAsync(comand.OrganizerId)).ReturnsAsync(organizer); + + var cancelationToken = new CancellationToken(); + + // Act + await _addOrganizerToOrganizationHandler.HandleAsync(comand, cancelationToken); + + // Assert + _organizationRepositoryMock.Verify(repo => repo.UpdateAsync(rootOrganization), Times.Once()); + } + + [Fact] + public async Task HandleAsync_WithoutAdminRole_ShouldThrowUnauthorizedAccessException() + { + // Arrange + var rootOrganizationId = Guid.NewGuid(); + var organizationId = Guid.NewGuid(); + var organizerId = Guid.NewGuid(); + var comand = new AddOrganizerToOrganization(rootOrganizationId, organizationId, organizerId); + + var organization = new Organization(organizationId, "this", new List() { }, new List() { }); + var rootOrganization = new Organization(rootOrganizationId, "root", new List() { }, new List() { organization }); + var organizer = new Organizer(organizerId); + + + var identityContext = new IdentityContext(Guid.NewGuid().ToString(), "", true, new Dictionary()); + + _appContextMock.Setup(ctx => ctx.Identity).Returns(identityContext); + _organizationRepositoryMock.Setup(repo => repo.GetAsync(comand.RootOrganizationId)).ReturnsAsync(rootOrganization); + _organizerRepositoryMock.Setup(repo => repo.GetAsync(comand.OrganizerId)).ReturnsAsync(organizer); + + var cancelationToken = new CancellationToken(); + + // Act & Assert + Func act = async () => await _addOrganizerToOrganizationHandler.HandleAsync(comand, cancelationToken); + await act.Should().ThrowAsync(); + } + + [Fact] + public async Task HandleAsync_InavalidOrganizationRoot_ShouldThrowRootOrganizationNotFoundException() + { + // Arrange + var rootOrganizationId = Guid.NewGuid(); + var organizationId = Guid.NewGuid(); + var organizerId = Guid.NewGuid(); + var comand = new AddOrganizerToOrganization(rootOrganizationId, organizationId, organizerId); + + var organization = new Organization(organizationId, "this", new List() { }, new List() { }); + var rootOrganization = new Organization(rootOrganizationId, "root", new List() { }, new List() { organization }); + var organizer = new Organizer(organizerId); + + + var identityContext = new IdentityContext(Guid.NewGuid().ToString(), "Admin", true, new Dictionary()); + + _appContextMock.Setup(ctx => ctx.Identity).Returns(identityContext); + _organizationRepositoryMock.Setup(repo => repo.GetAsync(comand.RootOrganizationId)).ReturnsAsync((Organization)null); + _organizerRepositoryMock.Setup(repo => repo.GetAsync(comand.OrganizerId)).ReturnsAsync(organizer); + + var cancelationToken = new CancellationToken(); + + // Act & Assert + Func act = async () => await _addOrganizerToOrganizationHandler.HandleAsync(comand, cancelationToken); + await act.Should().ThrowAsync(); + } + + [Fact] + public async Task HandleAsync_WithoutOrganizationInOrganizationRoot_ShouldThrowOrganizationNotFoundException() + { + // Arrange + var rootOrganizationId = Guid.NewGuid(); + var organizationId = Guid.NewGuid(); + var organizerId = Guid.NewGuid(); + var comand = new AddOrganizerToOrganization(rootOrganizationId, organizationId, organizerId); + + var organization = new Organization(organizationId, "this", new List() { }, new List() { }); + var rootOrganization = new Organization(rootOrganizationId, "root", new List() { }, new List() { }); + var organizer = new Organizer(organizerId); + + + var identityContext = new IdentityContext(Guid.NewGuid().ToString(), "Admin", true, new Dictionary()); + + _appContextMock.Setup(ctx => ctx.Identity).Returns(identityContext); + _organizationRepositoryMock.Setup(repo => repo.GetAsync(comand.RootOrganizationId)).ReturnsAsync(rootOrganization); + _organizerRepositoryMock.Setup(repo => repo.GetAsync(comand.OrganizerId)).ReturnsAsync(organizer); + + var cancelationToken = new CancellationToken(); + + // Act & Assert + Func act = async () => await _addOrganizerToOrganizationHandler.HandleAsync(comand, cancelationToken); + await act.Should().ThrowAsync(); + } + + [Fact] + public async Task HandleAsync_WithInvalidOrganizer_ShouldThrowOrganizerNotFoundException() + { + // Arrange + var rootOrganizationId = Guid.NewGuid(); + var organizationId = Guid.NewGuid(); + var organizerId = Guid.NewGuid(); + var comand = new AddOrganizerToOrganization(rootOrganizationId, organizationId, organizerId); + + var organization = new Organization(organizationId, "this", new List() { }, new List() { }); + var rootOrganization = new Organization(rootOrganizationId, "root", new List() { }, new List() { organization }); + var organizer = new Organizer(organizerId); + + + var identityContext = new IdentityContext(Guid.NewGuid().ToString(), "Admin", true, new Dictionary()); + + _appContextMock.Setup(ctx => ctx.Identity).Returns(identityContext); + _organizationRepositoryMock.Setup(repo => repo.GetAsync(comand.RootOrganizationId)).ReturnsAsync(rootOrganization); + _organizerRepositoryMock.Setup(repo => repo.GetAsync(comand.OrganizerId)).ReturnsAsync((Organizer)null); + + var cancelationToken = new CancellationToken(); + + // Act & Assert + Func act = async () => await _addOrganizerToOrganizationHandler.HandleAsync(comand, cancelationToken); + await act.Should().ThrowAsync(); + } + } +} diff --git a/MiniSpace.Services.Organizations/tests/MiniSpace.Services.Organizations.Application.UnitTests/Commands/Handlers/CreateOrganizationHandlerTest.cs b/MiniSpace.Services.Organizations/tests/MiniSpace.Services.Organizations.Application.UnitTests/Commands/Handlers/CreateOrganizationHandlerTest.cs new file mode 100644 index 000000000..408c1b7a2 --- /dev/null +++ b/MiniSpace.Services.Organizations/tests/MiniSpace.Services.Organizations.Application.UnitTests/Commands/Handlers/CreateOrganizationHandlerTest.cs @@ -0,0 +1,159 @@ +using Xunit; +using Moq; +using System; +using System.Collections.Generic; +using System.Threading.Tasks; +using System.Threading; +using FluentAssertions; +using MiniSpace.Services.Organizations.Application.Commands.Handlers; +using MiniSpace.Services.Organizations.Core.Repositories; +using MiniSpace.Services.Organizations.Application.Services; +using MiniSpace.Services.Organizations.Core.Entities; +using MiniSpace.Services.Organizations.Infrastructure.Contexts; +using MiniSpace.Services.Organizations.Application.Commands; +using MiniSpace.Services.Organizations.Application.Exceptions; + +namespace MiniSpace.Services.Organizations.Application.UnitTests.Commands.Handlers +{ + public class CreateOrganizationHandlerTest + { + private readonly CreateOrganizationHandler _createOrganizationHandler; + private readonly Mock _organizationRepositoryMock; + private readonly Mock _appContextMock; + private readonly Mock _messageBrokerMock; + + public CreateOrganizationHandlerTest() + { + _organizationRepositoryMock = new Mock(); + _appContextMock = new Mock(); + _messageBrokerMock = new Mock(); + _createOrganizationHandler = new CreateOrganizationHandler( + _organizationRepositoryMock.Object, + _appContextMock.Object, + _messageBrokerMock.Object + ); + } + + [Fact] + public async Task HandleAsync_WithValidorganizationAndAuthorised_ShouldUpdateRootOrganization() + { + // Arrange + var rootOrganizationId = Guid.NewGuid(); + var organizationId = Guid.NewGuid(); + var parentId = Guid.NewGuid(); + var comand = new CreateOrganization(organizationId, "this", rootOrganizationId, parentId); + + var organization = new Organization(organizationId, "this"); + var parent = new Organization(parentId, "parent", new List() { }, new List() { }); + var rootOrganization = new Organization(rootOrganizationId, "root", new List() { }, new List() { parent }); + var identityContext = new IdentityContext(Guid.NewGuid().ToString(), "Admin", true, new Dictionary()); + + _appContextMock.Setup(ctx => ctx.Identity).Returns(identityContext); + _organizationRepositoryMock.Setup(repo => repo.GetAsync(comand.RootId)).ReturnsAsync(rootOrganization); + + var cancelationToken = new CancellationToken(); + + // Act + await _createOrganizationHandler.HandleAsync(comand, cancelationToken); + + // Assert + _organizationRepositoryMock.Verify(repo => repo.UpdateAsync(rootOrganization), Times.Once()); + } + + [Fact] + public async Task HandleAsync_WithoutAdminRole_ShouldThrowUnauthorizedAccessException() + { + // Arrange + var rootOrganizationId = Guid.NewGuid(); + var organizationId = Guid.NewGuid(); + var parentId = Guid.NewGuid(); + var comand = new CreateOrganization(organizationId, "this", rootOrganizationId, parentId); + + var organization = new Organization(organizationId, "this"); + var parent = new Organization(parentId, "parent", new List() { }, new List() { }); + var rootOrganization = new Organization(rootOrganizationId, "root", new List() { }, new List() { parent }); + var identityContext = new IdentityContext(Guid.NewGuid().ToString(), "", true, new Dictionary()); + + _appContextMock.Setup(ctx => ctx.Identity).Returns(identityContext); + _organizationRepositoryMock.Setup(repo => repo.GetAsync(comand.RootId)).ReturnsAsync(rootOrganization); + + var cancelationToken = new CancellationToken(); + + // Act & Assert + Func act = async () => await _createOrganizationHandler.HandleAsync(comand, cancelationToken); + await act.Should().ThrowAsync(); + } + + [Fact] + public async Task HandleAsync_InavalidOrganizationRoot_ShouldThrowRootOrganizationNotFoundException() + { + // Arrange + var rootOrganizationId = Guid.NewGuid(); + var organizationId = Guid.NewGuid(); + var parentId = Guid.NewGuid(); + var comand = new CreateOrganization(organizationId, "this", rootOrganizationId, parentId); + + var organization = new Organization(organizationId, "this"); + var parent = new Organization(parentId, "parent", new List() { }, new List() { }); + var rootOrganization = new Organization(rootOrganizationId, "root", new List() { }, new List() { }); + var identityContext = new IdentityContext(Guid.NewGuid().ToString(), "Admin", true, new Dictionary()); + + _appContextMock.Setup(ctx => ctx.Identity).Returns(identityContext); + _organizationRepositoryMock.Setup(repo => repo.GetAsync(comand.RootId)).ReturnsAsync((Organization)null); + + var cancelationToken = new CancellationToken(); + + // Act & Assert + Func act = async () => await _createOrganizationHandler.HandleAsync(comand, cancelationToken); + await act.Should().ThrowAsync(); + } + + [Fact] + public async Task HandleAsync_WithoutOrganizationInOrganizationRoot_ShouldThrowParentOrganizationNotFoundException() + { + // Arrange + var rootOrganizationId = Guid.NewGuid(); + var organizationId = Guid.NewGuid(); + var parentId = Guid.NewGuid(); + var comand = new CreateOrganization(organizationId, "this", rootOrganizationId, parentId); + + var organization = new Organization(organizationId, "this"); + var parent = new Organization(parentId, "parent", new List() { }, new List() { }); + var rootOrganization = new Organization(rootOrganizationId, "root", new List() { }, new List() { }); + var identityContext = new IdentityContext(Guid.NewGuid().ToString(), "Admin", true, new Dictionary()); + + _appContextMock.Setup(ctx => ctx.Identity).Returns(identityContext); + _organizationRepositoryMock.Setup(repo => repo.GetAsync(comand.RootId)).ReturnsAsync(rootOrganization); + + var cancelationToken = new CancellationToken(); + + // Act & Assert + Func act = async () => await _createOrganizationHandler.HandleAsync(comand, cancelationToken); + await act.Should().ThrowAsync(); + } + + [Fact] + public async Task HandleAsync_WithInvalidName_ShouldThrowInvalidOrganizationNameException() + { + // Arrange + var rootOrganizationId = Guid.NewGuid(); + var organizationId = Guid.NewGuid(); + var parentId = Guid.NewGuid(); + var comand = new CreateOrganization(organizationId, "", rootOrganizationId, parentId); + + var organization = new Organization(organizationId, "this"); + var parent = new Organization(parentId, "parent", new List() { }, new List() { }); + var rootOrganization = new Organization(rootOrganizationId, "root", new List() { }, new List() { parent }); + var identityContext = new IdentityContext(Guid.NewGuid().ToString(), "Admin", true, new Dictionary()); + + _appContextMock.Setup(ctx => ctx.Identity).Returns(identityContext); + _organizationRepositoryMock.Setup(repo => repo.GetAsync(comand.RootId)).ReturnsAsync(rootOrganization); + + var cancelationToken = new CancellationToken(); + + // Act & Assert + Func act = async () => await _createOrganizationHandler.HandleAsync(comand, cancelationToken); + await act.Should().ThrowAsync(); + } + } +} diff --git a/MiniSpace.Services.Organizations/tests/MiniSpace.Services.Organizations.Application.UnitTests/Commands/Handlers/CreateRoorOrganizationHandlerTest.cs b/MiniSpace.Services.Organizations/tests/MiniSpace.Services.Organizations.Application.UnitTests/Commands/Handlers/CreateRoorOrganizationHandlerTest.cs new file mode 100644 index 000000000..6f4c88d12 --- /dev/null +++ b/MiniSpace.Services.Organizations/tests/MiniSpace.Services.Organizations.Application.UnitTests/Commands/Handlers/CreateRoorOrganizationHandlerTest.cs @@ -0,0 +1,95 @@ +using FluentAssertions; +using MiniSpace.Services.Organizations.Application.Commands; +using MiniSpace.Services.Organizations.Application.Commands.Handlers; +using MiniSpace.Services.Organizations.Application.Exceptions; +using MiniSpace.Services.Organizations.Application.Services; +using MiniSpace.Services.Organizations.Core.Entities; +using MiniSpace.Services.Organizations.Core.Repositories; +using MiniSpace.Services.Organizations.Infrastructure.Contexts; +using Moq; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading; +using System.Threading.Tasks; +using Xunit; + +namespace MiniSpace.Services.Organizations.Application.UnitTests.Commands.Handlers +{ + public class CreateRoorOrganizationHandlerTest + { + private readonly CreateRootOrganizationHandler _createRootOrganizationHandler; + private readonly Mock _organizationRepositoryMock; + private readonly Mock _appContextMock; + private readonly Mock _messageBrokerMock; + + public CreateRoorOrganizationHandlerTest() + { + _organizationRepositoryMock = new Mock(); + _appContextMock = new Mock(); + _messageBrokerMock = new Mock(); + _createRootOrganizationHandler = new CreateRootOrganizationHandler( + _organizationRepositoryMock.Object, + _appContextMock.Object, + _messageBrokerMock.Object + ); + } + + [Fact] + public async Task HandleAsync_WithValidOrganizationNameAndAuthorised_ShouldNotThrow() + { + // Arrange + var organizationId = Guid.NewGuid(); + var comand = new CreateRootOrganization(organizationId, "this"); + + var organization = new Organization(organizationId, "this"); + + var identityContext = new IdentityContext(Guid.NewGuid().ToString(), "Admin", true, new Dictionary()); + + _appContextMock.Setup(ctx => ctx.Identity).Returns(identityContext); + + var cancelationToken = new CancellationToken(); + + // Act & Assert + Func act = async () => await _createRootOrganizationHandler.HandleAsync(comand, cancelationToken); + await act.Should().NotThrowAsync(); + } + + [Fact] + public async Task HandleAsync_WithoutAdminRole_ShouldThrowUnauthorizedAccessException() + { + // Arrange + var organizationId = Guid.NewGuid(); + var comand = new CreateRootOrganization(organizationId, "this"); + + var identityContext = new IdentityContext(Guid.NewGuid().ToString(), "", true, new Dictionary()); + + _appContextMock.Setup(ctx => ctx.Identity).Returns(identityContext); + + var cancelationToken = new CancellationToken(); + + // Act & Assert + Func act = async () => await _createRootOrganizationHandler.HandleAsync(comand, cancelationToken); + await act.Should().ThrowAsync(); + } + + [Fact] + public async Task HandleAsync_InavalidOrganizationName_ShouldThrowInvalidOrganizationNameException() + { + // Arrange + var organizationId = Guid.NewGuid(); + var comand = new CreateRootOrganization(organizationId, ""); + + var identityContext = new IdentityContext(Guid.NewGuid().ToString(), "Admin", true, new Dictionary()); + + _appContextMock.Setup(ctx => ctx.Identity).Returns(identityContext); + + var cancelationToken = new CancellationToken(); + + // Act & Assert + Func act = async () => await _createRootOrganizationHandler.HandleAsync(comand, cancelationToken); + await act.Should().ThrowAsync (); + } + } +} diff --git a/MiniSpace.Services.Organizations/tests/MiniSpace.Services.Organizations.Application.UnitTests/Commands/Handlers/RemoveOrganizerFromOrganizationHandlerTest.cs b/MiniSpace.Services.Organizations/tests/MiniSpace.Services.Organizations.Application.UnitTests/Commands/Handlers/RemoveOrganizerFromOrganizationHandlerTest.cs new file mode 100644 index 000000000..598a8ce05 --- /dev/null +++ b/MiniSpace.Services.Organizations/tests/MiniSpace.Services.Organizations.Application.UnitTests/Commands/Handlers/RemoveOrganizerFromOrganizationHandlerTest.cs @@ -0,0 +1,182 @@ +using Xunit; +using Moq; +using System; +using System.Collections.Generic; +using System.Threading.Tasks; +using System.Threading; +using FluentAssertions; +using MiniSpace.Services.Organizations.Application.Commands.Handlers; +using MiniSpace.Services.Organizations.Core.Repositories; +using MiniSpace.Services.Organizations.Application.Services; +using MiniSpace.Services.Organizations.Core.Entities; +using MiniSpace.Services.Organizations.Infrastructure.Contexts; +using MiniSpace.Services.Organizations.Application.Commands; +using MiniSpace.Services.Organizations.Application.Exceptions; + +namespace MiniSpace.Services.Organizations.Application.UnitTests.Commands.Handlers +{ + public class RemoveOrganizerFromOrganizationHandlerTest + { + private readonly RemoveOrganizerFromOrganizationHandler _removeOrganizerFromOrganizationHandler; + private readonly Mock _organizationRepositoryMock; + private readonly Mock _organizerRepositoryMock; + private readonly Mock _appContextMock; + private readonly Mock _messageBrokerMock; + + public RemoveOrganizerFromOrganizationHandlerTest() + { + _organizationRepositoryMock = new Mock(); + _organizerRepositoryMock = new Mock(); + _appContextMock = new Mock(); + _messageBrokerMock = new Mock(); + _removeOrganizerFromOrganizationHandler = new RemoveOrganizerFromOrganizationHandler( + _organizationRepositoryMock.Object, + _organizerRepositoryMock.Object, + _appContextMock.Object, + _messageBrokerMock.Object + ); + } + + [Fact] + public async Task HandleAsync_WithValidOrganizationAndAuthorised_ShouldUpdateOrganization() + { + // Arrange + var rootOrganizationId = Guid.NewGuid(); + var organizationId = Guid.NewGuid(); + var organizerId = Guid.NewGuid(); + var comand = new RemoveOrganizerFromOrganization(rootOrganizationId, organizationId, organizerId); + + var organizer = new Organizer(organizerId); + var organization = new Organization(organizationId, "this", new List() { organizer }, new List() { }); + var rootOrganization = new Organization(rootOrganizationId, "root", new List() { }, new List() { organization }); + + + + var identityContext = new IdentityContext(Guid.NewGuid().ToString(), "Admin", true, new Dictionary()); + + _appContextMock.Setup(ctx => ctx.Identity).Returns(identityContext); + _organizationRepositoryMock.Setup(repo => repo.GetAsync(comand.RootOrganizationId)).ReturnsAsync(rootOrganization); + _organizerRepositoryMock.Setup(repo => repo.GetAsync(comand.OrganizerId)).ReturnsAsync(organizer); + + var cancelationToken = new CancellationToken(); + + // Act + await _removeOrganizerFromOrganizationHandler.HandleAsync(comand, cancelationToken); + + // Assert + _organizationRepositoryMock.Verify(repo => repo.UpdateAsync(rootOrganization), Times.Once()); + } + + [Fact] + public async Task HandleAsync_WithoutAdminRole_ShouldThrowUnauthorizedAccessException() + { + // Arrange + var rootOrganizationId = Guid.NewGuid(); + var organizationId = Guid.NewGuid(); + var organizerId = Guid.NewGuid(); + var comand = new RemoveOrganizerFromOrganization(rootOrganizationId, organizationId, organizerId); + + var organizer = new Organizer(organizerId); + var organization = new Organization(organizationId, "this", new List() { organizer }, new List() { }); + var rootOrganization = new Organization(rootOrganizationId, "root", new List() { }, new List() { organization }); + + + + var identityContext = new IdentityContext(Guid.NewGuid().ToString(), "", true, new Dictionary()); + + _appContextMock.Setup(ctx => ctx.Identity).Returns(identityContext); + _organizationRepositoryMock.Setup(repo => repo.GetAsync(comand.RootOrganizationId)).ReturnsAsync(rootOrganization); + _organizerRepositoryMock.Setup(repo => repo.GetAsync(comand.OrganizerId)).ReturnsAsync(organizer); + + var cancelationToken = new CancellationToken(); + + // Act & Assert + Func act = async () => await _removeOrganizerFromOrganizationHandler.HandleAsync(comand, cancelationToken); + await act.Should().ThrowAsync(); + } + + [Fact] + public async Task HandleAsync_InavalidOrganizationRoot_ShouldThrowRootOrganizationNotFoundException() + { + // Arrange + var rootOrganizationId = Guid.NewGuid(); + var organizationId = Guid.NewGuid(); + var organizerId = Guid.NewGuid(); + var comand = new RemoveOrganizerFromOrganization(rootOrganizationId, organizationId, organizerId); + + var organizer = new Organizer(organizerId); + var organization = new Organization(organizationId, "this", new List() { organizer }, new List() { }); + var rootOrganization = new Organization(rootOrganizationId, "root", new List() { }, new List() { organization }); + + + + var identityContext = new IdentityContext(Guid.NewGuid().ToString(), "Admin", true, new Dictionary()); + + _appContextMock.Setup(ctx => ctx.Identity).Returns(identityContext); + _organizationRepositoryMock.Setup(repo => repo.GetAsync(comand.RootOrganizationId)).ReturnsAsync((Organization)null); + _organizerRepositoryMock.Setup(repo => repo.GetAsync(comand.OrganizerId)).ReturnsAsync(organizer); + + var cancelationToken = new CancellationToken(); + + // Act & Assert + Func act = async () => await _removeOrganizerFromOrganizationHandler.HandleAsync(comand, cancelationToken); + await act.Should().ThrowAsync(); + } + + [Fact] + public async Task HandleAsync_WithoutOrganizationInOrganizationRoot_ShouldThrowOrganizationNotFoundException() + { + // Arrange + var rootOrganizationId = Guid.NewGuid(); + var organizationId = Guid.NewGuid(); + var organizerId = Guid.NewGuid(); + var comand = new RemoveOrganizerFromOrganization(rootOrganizationId, organizationId, organizerId); + + var organizer = new Organizer(organizerId); + var organization = new Organization(organizationId, "this", new List() { organizer }, new List() { }); + var rootOrganization = new Organization(rootOrganizationId, "root", new List() { }, new List() { }); + + + + var identityContext = new IdentityContext(Guid.NewGuid().ToString(), "Admin", true, new Dictionary()); + + _appContextMock.Setup(ctx => ctx.Identity).Returns(identityContext); + _organizationRepositoryMock.Setup(repo => repo.GetAsync(comand.RootOrganizationId)).ReturnsAsync(rootOrganization); + _organizerRepositoryMock.Setup(repo => repo.GetAsync(comand.OrganizerId)).ReturnsAsync(organizer); + + var cancelationToken = new CancellationToken(); + + // Act & Assert + Func act = async () => await _removeOrganizerFromOrganizationHandler.HandleAsync(comand, cancelationToken); + await act.Should().ThrowAsync(); + } + + [Fact] + public async Task HandleAsync_WithInvalidOrganizer_ShouldThrowOrganizerNotFoundException() + { + // Arrange + var rootOrganizationId = Guid.NewGuid(); + var organizationId = Guid.NewGuid(); + var organizerId = Guid.NewGuid(); + var comand = new RemoveOrganizerFromOrganization(rootOrganizationId, organizationId, organizerId); + + var organizer = new Organizer(organizerId); + var organization = new Organization(organizationId, "this", new List() { organizer }, new List() { }); + var rootOrganization = new Organization(rootOrganizationId, "root", new List() { }, new List() { organization }); + + + + var identityContext = new IdentityContext(Guid.NewGuid().ToString(), "Admin", true, new Dictionary()); + + _appContextMock.Setup(ctx => ctx.Identity).Returns(identityContext); + _organizationRepositoryMock.Setup(repo => repo.GetAsync(comand.RootOrganizationId)).ReturnsAsync(rootOrganization); + _organizerRepositoryMock.Setup(repo => repo.GetAsync(comand.OrganizerId)).ReturnsAsync((Organizer)null); + + var cancelationToken = new CancellationToken(); + + // Act & Assert + Func act = async () => await _removeOrganizerFromOrganizationHandler.HandleAsync(comand, cancelationToken); + await act.Should().ThrowAsync(); + } + } +} diff --git a/MiniSpace.Services.Organizations/tests/MiniSpace.Services.Organizations.Application.UnitTests/Events/External/Handlers/OrganizerRightRevokedHandlerTest.cs b/MiniSpace.Services.Organizations/tests/MiniSpace.Services.Organizations.Application.UnitTests/Events/External/Handlers/OrganizerRightRevokedHandlerTest.cs new file mode 100644 index 000000000..5b57ce214 --- /dev/null +++ b/MiniSpace.Services.Organizations/tests/MiniSpace.Services.Organizations.Application.UnitTests/Events/External/Handlers/OrganizerRightRevokedHandlerTest.cs @@ -0,0 +1,75 @@ +using Xunit; +using Moq; +using System; +using System.Collections.Generic; +using System.Threading.Tasks; +using System.Threading; +using FluentAssertions; +using MiniSpace.Services.Organizations.Application.Commands.Handlers; +using MiniSpace.Services.Organizations.Core.Repositories; +using MiniSpace.Services.Organizations.Application.Services; +using MiniSpace.Services.Organizations.Core.Entities; +using MiniSpace.Services.Organizations.Infrastructure.Contexts; +using MiniSpace.Services.Organizations.Application.Commands; +using MiniSpace.Services.Organizations.Application.Exceptions; +using MiniSpace.Services.Organizations.Application.Events.External.Handlers; +using MiniSpace.Services.Organizations.Application.Events.External; + +namespace MiniSpace.Services.Organizations.Application.UnitTests.Events.External.Handlers +{ + public class OrganizerRightRevokedHandlerTest + { + private readonly OrganizerRightsRevokedHandler _organizerRightsRevokedHandler; + private readonly Mock _organizerRepository; + private readonly Mock _organizationRepository; + + public OrganizerRightRevokedHandlerTest() + { + _organizerRepository = new Mock(); + _organizationRepository = new Mock(); + _organizerRightsRevokedHandler = new OrganizerRightsRevokedHandler(_organizerRepository.Object, _organizationRepository.Object); + } + + [Fact] + public async Task HandleAsync_WithValidOrganizer_ShouldNotThrow() + { + // Arrange + var userId = Guid.NewGuid(); + var @event = new OrganizerRightsRevoked(userId); + var organizer = new Organizer(userId); + var organization = new Organization(Guid.NewGuid(), "name", new List() { organizer }, new List() { }); + + _organizerRepository.Setup(repo => repo.GetAsync(@event.UserId)).ReturnsAsync(organizer); + _organizationRepository.Setup(repo => repo.GetOrganizerOrganizationsAsync(@event.UserId)).ReturnsAsync(new List() { organization }); + + var cancelationToken = new CancellationToken(); + + // Act + await _organizerRightsRevokedHandler.HandleAsync(@event, cancelationToken); + + // Arrange + _organizationRepository.Verify(repo => repo.UpdateAsync(organization), Times.Once()); + _organizerRepository.Verify(repo => repo.DeleteAsync(userId), Times.Once()); + } + + [Fact] + public async Task HandleAsync_WithInvalidOrganizer_ShouldThrowOrganizerNotFoundException() + { + // Arrange + var userId = Guid.NewGuid(); + var @event = new OrganizerRightsRevoked(userId); + var organizer = new Organizer(userId); + var organization = new Organization(Guid.NewGuid(), "name", new List() { organizer }, new List() { }); + + _organizerRepository.Setup(repo => repo.GetAsync(@event.UserId)).ReturnsAsync((Organizer)null); + _organizationRepository.Setup(repo => repo.GetOrganizerOrganizationsAsync(@event.UserId)).ReturnsAsync(new List() { organization }); + + var cancelationToken = new CancellationToken(); + + // Act & Assert + Func act = async () => await _organizerRightsRevokedHandler.HandleAsync(@event, cancelationToken); + await act.Should().ThrowAsync(); + } + + } +} diff --git a/MiniSpace.Services.Organizations/tests/MiniSpace.Services.Organizations.Application.UnitTests/Events/External/Handlers/OrganizerRightsGrantedHandlerTest.cs b/MiniSpace.Services.Organizations/tests/MiniSpace.Services.Organizations.Application.UnitTests/Events/External/Handlers/OrganizerRightsGrantedHandlerTest.cs new file mode 100644 index 000000000..7cf7ed768 --- /dev/null +++ b/MiniSpace.Services.Organizations/tests/MiniSpace.Services.Organizations.Application.UnitTests/Events/External/Handlers/OrganizerRightsGrantedHandlerTest.cs @@ -0,0 +1,63 @@ +using Xunit; +using Moq; +using System; +using System.Collections.Generic; +using System.Threading.Tasks; +using System.Threading; +using FluentAssertions; +using MiniSpace.Services.Organizations.Application.Commands.Handlers; +using MiniSpace.Services.Organizations.Core.Repositories; +using MiniSpace.Services.Organizations.Application.Services; +using MiniSpace.Services.Organizations.Core.Entities; +using MiniSpace.Services.Organizations.Infrastructure.Contexts; +using MiniSpace.Services.Organizations.Application.Commands; +using MiniSpace.Services.Organizations.Application.Exceptions; +using MiniSpace.Services.Organizations.Application.Events.External.Handlers; +using MiniSpace.Services.Organizations.Application.Events.External; + +namespace MiniSpace.Services.Organizations.Application.UnitTests.Events.External.Handlers +{ + public class OrganizerRightsGrantedHandlerTest + { + private readonly OrganizerRightsGrantedHandler _organizerRightsGrantedHandler; + private readonly Mock _organizerRepository; + + public OrganizerRightsGrantedHandlerTest() + { + _organizerRepository = new Mock(); + _organizerRightsGrantedHandler = new OrganizerRightsGrantedHandler(_organizerRepository.Object); + } + + [Fact] + public async Task HandleAsync_WithValidOrganizer_ShouldNotThrow() + { + // Arrange + var userId = Guid.NewGuid(); + var @event = new OrganizerRightsGranted(userId); + + _organizerRepository.Setup(repo => repo.ExistsAsync(userId)).ReturnsAsync(false); + + var cancelationToken = new CancellationToken(); + + // Act & Assert + Func act = async () => await _organizerRightsGrantedHandler.HandleAsync(@event, cancelationToken); + await act.Should().NotThrowAsync(); + } + + [Fact] + public async Task HandleAsync_WithInvalidOrganizer_ShouldThrowOrganizerAlreadyAddedException() + { + // Arrange + var userId = Guid.NewGuid(); + var @event = new OrganizerRightsGranted(userId); + + _organizerRepository.Setup(repo => repo.ExistsAsync(userId)).ReturnsAsync(true); + + var cancelationToken = new CancellationToken(); + + // Act & Assert + Func act = async () => await _organizerRightsGrantedHandler.HandleAsync(@event, cancelationToken); + await act.Should().ThrowAsync(); + } + } +} diff --git a/MiniSpace.Services.Organizations/tests/MiniSpace.Services.Organizations.Application.UnitTests/MiniSpace.Services.Organizations.Application.UnitTests.csproj b/MiniSpace.Services.Organizations/tests/MiniSpace.Services.Organizations.Application.UnitTests/MiniSpace.Services.Organizations.Application.UnitTests.csproj new file mode 100644 index 000000000..a38b990f2 --- /dev/null +++ b/MiniSpace.Services.Organizations/tests/MiniSpace.Services.Organizations.Application.UnitTests/MiniSpace.Services.Organizations.Application.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.Organizations/tests/MiniSpace.Services.Organizations.Core.UnitTests/Entities/AggregateIdTest.cs b/MiniSpace.Services.Organizations/tests/MiniSpace.Services.Organizations.Core.UnitTests/Entities/AggregateIdTest.cs new file mode 100644 index 000000000..846e08332 --- /dev/null +++ b/MiniSpace.Services.Organizations/tests/MiniSpace.Services.Organizations.Core.UnitTests/Entities/AggregateIdTest.cs @@ -0,0 +1,38 @@ +using MiniSpace.Services.Organizations.Core.Entities; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Xunit; + +namespace MiniSpace.Services.Organizations.Core.UnitTests.Entities +{ + public class AggregateIdTest + { + [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); + } + } +} diff --git a/MiniSpace.Services.Organizations/tests/MiniSpace.Services.Organizations.Core.UnitTests/Entities/OrganizationTest.cs b/MiniSpace.Services.Organizations/tests/MiniSpace.Services.Organizations.Core.UnitTests/Entities/OrganizationTest.cs new file mode 100644 index 000000000..0e6c30a6d --- /dev/null +++ b/MiniSpace.Services.Organizations/tests/MiniSpace.Services.Organizations.Core.UnitTests/Entities/OrganizationTest.cs @@ -0,0 +1,176 @@ +using MiniSpace.Services.Organizations.Core.Entities; +using MiniSpace.Services.Organizations.Core.Exceptions; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Xunit; + +namespace MiniSpace.Services.Organizations.Core.UnitTests.Entities +{ + public class OrganizationTest + { + [Fact] + public void AddOrganizer_OrganizorAdded_ShouldAddOrgizer() + { + // Arrange + var organizerId = Guid.NewGuid(); + var organiazation = new Organization(Guid.NewGuid(), "name"); + + // Act + organiazation.AddOrganizer(organizerId); + + // Assert + Assert.True(organiazation.Organizers.Any(x => x.Id == organizerId)); + } + + [Fact] + public void AddOrganizer_OrganizorOlreadyAdded_ShouldThrowOrganizerAlreadyAddedToOrganizationException() + { + // Arrange + var organizerId = Guid.NewGuid(); + var organiazation = new Organization(Guid.NewGuid(), "name"); + organiazation.AddOrganizer(organizerId); + + // Act & Assert + var act = new Action(() => organiazation.AddOrganizer(organizerId)); + Assert.Throws(act); + } + + [Fact] + public void RemoveOrganizer_OrganizorRemoved_ShouldRemoveOrganizer() + { + // Arrange + var organizerId = Guid.NewGuid(); + var organiazation = new Organization(Guid.NewGuid(), "name"); + organiazation.AddOrganizer(organizerId); + + // Act + organiazation.RemoveOrganizer(organizerId); + + // Assert + Assert.DoesNotContain(new Organizer(organizerId), organiazation.Organizers); + } + + [Fact] + public void RemoveOrganizer_OrganizorOlreadyRemoved_ShouldThrowOrganizerIsNotInOrganization() + { + // Arrange + var organizerId = Guid.NewGuid(); + var organiazation = new Organization(Guid.NewGuid(), "name"); + + // Act & Assert + var act = new Action(() => organiazation.RemoveOrganizer(organizerId)); + Assert.Throws(act); + } + + [Fact] + public void AddSubOrganization_AddOrganization_ShouldAddOrganization() + { + // Arrange + var organizationId = Guid.NewGuid(); + var organiazation = new Organization(organizationId, "name"); + var neworganization = new Organization(Guid.NewGuid(), "name"); + + // Act + organiazation.AddSubOrganization(neworganization); + + // Assert + Assert.Contains(neworganization, organiazation.SubOrganizations); + + } + + + [Fact] + public void GetSubOrganization_AskedAboutThemself_ShouldReturnThemself() + { + // Arrange + var organizationId = Guid.NewGuid(); + var organiazation = new Organization(organizationId, "name"); + + // Act + var result = organiazation.GetSubOrganization(organizationId); + + // Assert + Assert.Equal(result.Id.ToString(), organizationId.ToString()); + + } + + [Fact] + public void GetSubOrganization_DorsNotContainsOrganization_ShouldReturnNull() + { + // Arrange + var organizationId = Guid.NewGuid(); + var organiazation = new Organization(Guid.NewGuid(), "name"); + + // Act + var result = organiazation.GetSubOrganization(organizationId); + + // Assert + Assert.Null(result); + + } + + [Fact] + public void GetSubOrganization_ContainsOrganization_ShouldReturnOrganization() + { + // Arrange + var organizationId = Guid.NewGuid(); + var organiazation = new Organization(Guid.NewGuid(), "name"); + organiazation.AddSubOrganization(new Organization(organizationId, "name")); + + // Act + var result = organiazation.GetSubOrganization(organizationId); + + // Assert + Assert.Equal(result.Id.ToString(), organizationId.ToString()); + + } + + [Fact] + public void FindOrganizations_FindOrganizationOfAOrganizer_ShouldReturnAllOrganizerOrganizations() + { + // Arrange + var organizerId = Guid.NewGuid(); + var organizer = new Organizer(organizerId); + var organizers = new List() { organizer }; + var organizers2 = new List() { }; + var ch2_1 = new Organization(Guid.NewGuid(), "ch2_1", organizers, new List { }); + var ch1_1 = new Organization(Guid.NewGuid(), "ch1_1", organizers, new List { }); + var ch1_2 = new Organization(Guid.NewGuid(), "ch1_2", organizers2, new List { ch2_1}); + var root = new Organization(Guid.NewGuid(), "root", organizers, new List { ch1_1, ch1_2}); + + //Act + var result = Organization.FindOrganizations(organizerId, root); + + // Assert + Assert.Contains(root, result); + Assert.Contains(ch1_1, result); + Assert.DoesNotContain(ch1_2, result); + Assert.Contains(ch2_1, result); + } + + [Fact] + public void FindAllChildrenOrganizations_FromRoot_ShouldReturnFullOrganizationTree() + { + // Arrange + var organizerId = Guid.NewGuid(); + var organizer = new Organizer(organizerId); + var organizers = new List() { organizer }; + var ch2_1 = new Organization(Guid.NewGuid(), "ch2_1", organizers, new List { }); + var ch1_1 = new Organization(Guid.NewGuid(), "ch1_1", organizers, new List { }); + var ch1_2 = new Organization(Guid.NewGuid(), "ch1_1", organizers, new List { ch2_1 }); + var root = new Organization(Guid.NewGuid(), "root", organizers, new List { ch1_1, ch1_2 }); + + //Act + var result = Organization.FindOrganizations(organizerId, root); + + // Assert + Assert.Contains(root, result); + Assert.Contains(ch1_1, result); + Assert.Contains(ch1_2, result); + Assert.Contains(ch1_2, result); + } + } +} diff --git a/MiniSpace.Services.Organizations/tests/MiniSpace.Services.Organizations.Core.UnitTests/MiniSpace.Services.Organizations.Core.UnitTests.csproj b/MiniSpace.Services.Organizations/tests/MiniSpace.Services.Organizations.Core.UnitTests/MiniSpace.Services.Organizations.Core.UnitTests.csproj new file mode 100644 index 000000000..a38b990f2 --- /dev/null +++ b/MiniSpace.Services.Organizations/tests/MiniSpace.Services.Organizations.Core.UnitTests/MiniSpace.Services.Organizations.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 + + + + + + + + + +