From 7a78361da75d9e0de87af8eb288e74871417bf12 Mon Sep 17 00:00:00 2001 From: olegkiprik Date: Sat, 18 May 2024 21:02:32 +0200 Subject: [PATCH 01/24] (#149) Create the test project --- MiniSpace.Services.Posts/.vscode/launch.json | 35 ++++++++++++++++ MiniSpace.Services.Posts/.vscode/tasks.json | 41 +++++++++++++++++++ .../MiniSpace.Services.Posts.Api/Program.cs | 1 + .../MiniSpace.Services.Posts.UnitTests.csproj | 30 ++++++++++++++ .../UnitTest1.cs | 13 ++++++ MiniSpace.Services.Posts/src/src.sln | 6 +++ 6 files changed, 126 insertions(+) create mode 100644 MiniSpace.Services.Posts/.vscode/launch.json create mode 100644 MiniSpace.Services.Posts/.vscode/tasks.json create mode 100644 MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.UnitTests/MiniSpace.Services.Posts.UnitTests.csproj create mode 100644 MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.UnitTests/UnitTest1.cs diff --git a/MiniSpace.Services.Posts/.vscode/launch.json b/MiniSpace.Services.Posts/.vscode/launch.json new file mode 100644 index 000000000..bc94964dc --- /dev/null +++ b/MiniSpace.Services.Posts/.vscode/launch.json @@ -0,0 +1,35 @@ +{ + "version": "0.2.0", + "configurations": [ + { + // Use IntelliSense to find out which attributes exist for C# debugging + // Use hover for the description of the existing attributes + // For further information visit https://github.com/dotnet/vscode-csharp/blob/main/debugger-launchjson.md. + "name": ".NET Core Launch (web)", + "type": "coreclr", + "request": "launch", + "preLaunchTask": "build", + // If you have changed target frameworks, make sure to update the program path. + "program": "${workspaceFolder}/src/MiniSpace.Services.Posts.Api/bin/Debug/net8.0/MiniSpace.Services.Posts.Api.dll", + "args": [], + "cwd": "${workspaceFolder}/src/MiniSpace.Services.Posts.Api", + "stopAtEntry": false, + // Enable launching a web browser when ASP.NET Core starts. For more information: https://aka.ms/VSCode-CS-LaunchJson-WebBrowser + "serverReadyAction": { + "action": "openExternally", + "pattern": "\\bNow listening on:\\s+(https?://\\S+)" + }, + "env": { + "ASPNETCORE_ENVIRONMENT": "Development" + }, + "sourceFileMap": { + "/Views": "${workspaceFolder}/Views" + } + }, + { + "name": ".NET Core Attach", + "type": "coreclr", + "request": "attach" + } + ] +} \ No newline at end of file diff --git a/MiniSpace.Services.Posts/.vscode/tasks.json b/MiniSpace.Services.Posts/.vscode/tasks.json new file mode 100644 index 000000000..292ce3988 --- /dev/null +++ b/MiniSpace.Services.Posts/.vscode/tasks.json @@ -0,0 +1,41 @@ +{ + "version": "2.0.0", + "tasks": [ + { + "label": "build", + "command": "dotnet", + "type": "process", + "args": [ + "build", + "${workspaceFolder}/src/MiniSpace.Services.Posts.Api/MiniSpace.Services.Posts.Api.csproj", + "/property:GenerateFullPaths=true", + "/consoleloggerparameters:NoSummary;ForceNoAlign" + ], + "problemMatcher": "$msCompile" + }, + { + "label": "publish", + "command": "dotnet", + "type": "process", + "args": [ + "publish", + "${workspaceFolder}/src/MiniSpace.Services.Posts.Api/MiniSpace.Services.Posts.Api.csproj", + "/property:GenerateFullPaths=true", + "/consoleloggerparameters:NoSummary;ForceNoAlign" + ], + "problemMatcher": "$msCompile" + }, + { + "label": "watch", + "command": "dotnet", + "type": "process", + "args": [ + "watch", + "run", + "--project", + "${workspaceFolder}/src/MiniSpace.Services.Posts.Api/MiniSpace.Services.Posts.Api.csproj" + ], + "problemMatcher": "$msCompile" + } + ] +} \ No newline at end of file diff --git a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Api/Program.cs b/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Api/Program.cs index bdbec8ae4..67be1e502 100644 --- a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Api/Program.cs +++ b/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Api/Program.cs @@ -1,3 +1,4 @@ +using System; using System.Collections.Generic; using System.Threading.Tasks; using Convey; diff --git a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.UnitTests/MiniSpace.Services.Posts.UnitTests.csproj b/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.UnitTests/MiniSpace.Services.Posts.UnitTests.csproj new file mode 100644 index 000000000..ca7bb7cf1 --- /dev/null +++ b/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.UnitTests/MiniSpace.Services.Posts.UnitTests.csproj @@ -0,0 +1,30 @@ + + + + net8.0 + enable + disable + + false + true + + + + + + + + + + + + + + + + + + + + + diff --git a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.UnitTests/UnitTest1.cs b/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.UnitTests/UnitTest1.cs new file mode 100644 index 000000000..4a5128760 --- /dev/null +++ b/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.UnitTests/UnitTest1.cs @@ -0,0 +1,13 @@ +using MiniSpace.Services.Posts.Api; + +namespace MiniSpace.Services.Posts.UnitTests; + +public class UnitTest1 +{ + [Fact] + public void Test1() + { + int sum = 2 + 3; + Assert.Equal(5, sum); + } +} \ No newline at end of file diff --git a/MiniSpace.Services.Posts/src/src.sln b/MiniSpace.Services.Posts/src/src.sln index 4f4c608ce..d069185eb 100644 --- a/MiniSpace.Services.Posts/src/src.sln +++ b/MiniSpace.Services.Posts/src/src.sln @@ -11,6 +11,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MiniSpace.Services.Posts.Ap EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MiniSpace.Services.Posts.Application", "MiniSpace.Services.Posts.Application\MiniSpace.Services.Posts.Application.csproj", "{4D79B5C0-6E85-44B6-9BF4-E36822BF711C}" EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MiniSpace.Services.Posts.UnitTests", "MiniSpace.Services.Posts.UnitTests\MiniSpace.Services.Posts.UnitTests.csproj", "{26DA1AF1-E17D-47EB-B36A-C4C31E59B3D9}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -33,6 +35,10 @@ Global {4D79B5C0-6E85-44B6-9BF4-E36822BF711C}.Debug|Any CPU.Build.0 = Debug|Any CPU {4D79B5C0-6E85-44B6-9BF4-E36822BF711C}.Release|Any CPU.ActiveCfg = Release|Any CPU {4D79B5C0-6E85-44B6-9BF4-E36822BF711C}.Release|Any CPU.Build.0 = Release|Any CPU + {26DA1AF1-E17D-47EB-B36A-C4C31E59B3D9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {26DA1AF1-E17D-47EB-B36A-C4C31E59B3D9}.Debug|Any CPU.Build.0 = Debug|Any CPU + {26DA1AF1-E17D-47EB-B36A-C4C31E59B3D9}.Release|Any CPU.ActiveCfg = Release|Any CPU + {26DA1AF1-E17D-47EB-B36A-C4C31E59B3D9}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE From 83ddce6f57a3687264fb0a7b5e5abd59213fd7d5 Mon Sep 17 00:00:00 2001 From: olegkiprik Date: Sat, 18 May 2024 21:05:45 +0200 Subject: [PATCH 02/24] (#149) Delete .vscode directory --- MiniSpace.Services.Posts/.gitignore | 3 ++ MiniSpace.Services.Posts/.vscode/launch.json | 35 ----------------- MiniSpace.Services.Posts/.vscode/tasks.json | 41 -------------------- 3 files changed, 3 insertions(+), 76 deletions(-) delete mode 100644 MiniSpace.Services.Posts/.vscode/launch.json delete mode 100644 MiniSpace.Services.Posts/.vscode/tasks.json diff --git a/MiniSpace.Services.Posts/.gitignore b/MiniSpace.Services.Posts/.gitignore index c1aafb511..fffd35c7b 100644 --- a/MiniSpace.Services.Posts/.gitignore +++ b/MiniSpace.Services.Posts/.gitignore @@ -3,6 +3,9 @@ ## ## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore +# VSCode +.vscode/ + # User-specific files *.suo *.user diff --git a/MiniSpace.Services.Posts/.vscode/launch.json b/MiniSpace.Services.Posts/.vscode/launch.json deleted file mode 100644 index bc94964dc..000000000 --- a/MiniSpace.Services.Posts/.vscode/launch.json +++ /dev/null @@ -1,35 +0,0 @@ -{ - "version": "0.2.0", - "configurations": [ - { - // Use IntelliSense to find out which attributes exist for C# debugging - // Use hover for the description of the existing attributes - // For further information visit https://github.com/dotnet/vscode-csharp/blob/main/debugger-launchjson.md. - "name": ".NET Core Launch (web)", - "type": "coreclr", - "request": "launch", - "preLaunchTask": "build", - // If you have changed target frameworks, make sure to update the program path. - "program": "${workspaceFolder}/src/MiniSpace.Services.Posts.Api/bin/Debug/net8.0/MiniSpace.Services.Posts.Api.dll", - "args": [], - "cwd": "${workspaceFolder}/src/MiniSpace.Services.Posts.Api", - "stopAtEntry": false, - // Enable launching a web browser when ASP.NET Core starts. For more information: https://aka.ms/VSCode-CS-LaunchJson-WebBrowser - "serverReadyAction": { - "action": "openExternally", - "pattern": "\\bNow listening on:\\s+(https?://\\S+)" - }, - "env": { - "ASPNETCORE_ENVIRONMENT": "Development" - }, - "sourceFileMap": { - "/Views": "${workspaceFolder}/Views" - } - }, - { - "name": ".NET Core Attach", - "type": "coreclr", - "request": "attach" - } - ] -} \ No newline at end of file diff --git a/MiniSpace.Services.Posts/.vscode/tasks.json b/MiniSpace.Services.Posts/.vscode/tasks.json deleted file mode 100644 index 292ce3988..000000000 --- a/MiniSpace.Services.Posts/.vscode/tasks.json +++ /dev/null @@ -1,41 +0,0 @@ -{ - "version": "2.0.0", - "tasks": [ - { - "label": "build", - "command": "dotnet", - "type": "process", - "args": [ - "build", - "${workspaceFolder}/src/MiniSpace.Services.Posts.Api/MiniSpace.Services.Posts.Api.csproj", - "/property:GenerateFullPaths=true", - "/consoleloggerparameters:NoSummary;ForceNoAlign" - ], - "problemMatcher": "$msCompile" - }, - { - "label": "publish", - "command": "dotnet", - "type": "process", - "args": [ - "publish", - "${workspaceFolder}/src/MiniSpace.Services.Posts.Api/MiniSpace.Services.Posts.Api.csproj", - "/property:GenerateFullPaths=true", - "/consoleloggerparameters:NoSummary;ForceNoAlign" - ], - "problemMatcher": "$msCompile" - }, - { - "label": "watch", - "command": "dotnet", - "type": "process", - "args": [ - "watch", - "run", - "--project", - "${workspaceFolder}/src/MiniSpace.Services.Posts.Api/MiniSpace.Services.Posts.Api.csproj" - ], - "problemMatcher": "$msCompile" - } - ] -} \ No newline at end of file From 5001127463dd8c7ce88454cea2c58679e2a7f527 Mon Sep 17 00:00:00 2001 From: olegkiprik Date: Sun, 19 May 2024 20:03:15 +0200 Subject: [PATCH 03/24] (#149) Update the project structure --- .../MiniSpace.Services.Posts.sln | 21 +++++++++++++ .../UnitTest1.cs | 13 -------- MiniSpace.Services.Posts/src/src.sln | 6 ---- ...rvices.Posts.Application.UnitTests.csproj} | 8 ++--- ...Space.Services.Posts.Core.UnitTests.csproj | 30 +++++++++++++++++++ ...ices.Posts.Infrastructure.UnitTests.csproj | 30 +++++++++++++++++++ 6 files changed, 85 insertions(+), 23 deletions(-) delete mode 100644 MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.UnitTests/UnitTest1.cs rename MiniSpace.Services.Posts/{src/MiniSpace.Services.Posts.UnitTests/MiniSpace.Services.Posts.UnitTests.csproj => tests/MiniSpace.Services.Posts.Application.UnitTests/MiniSpace.Services.Posts.Application.UnitTests.csproj} (57%) create mode 100644 MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Core.UnitTests/MiniSpace.Services.Posts.Core.UnitTests.csproj create mode 100644 MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Infrastructure.UnitTests/MiniSpace.Services.Posts.Infrastructure.UnitTests.csproj diff --git a/MiniSpace.Services.Posts/MiniSpace.Services.Posts.sln b/MiniSpace.Services.Posts/MiniSpace.Services.Posts.sln index 468fc6e92..031553aa5 100644 --- a/MiniSpace.Services.Posts/MiniSpace.Services.Posts.sln +++ b/MiniSpace.Services.Posts/MiniSpace.Services.Posts.sln @@ -13,6 +13,12 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MiniSpace.Services.Posts.Co EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MiniSpace.Services.Posts.Infrastructure", "src\MiniSpace.Services.Posts.Infrastructure\MiniSpace.Services.Posts.Infrastructure.csproj", "{515F6D6B-FB63-40F7-8173-2B94031F29B1}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MiniSpace.Services.Posts.Application.UnitTests", "tests\MiniSpace.Services.Posts.Application.UnitTests\MiniSpace.Services.Posts.Application.UnitTests.csproj", "{63DF71AF-1D31-4A4D-8127-9B8EB359889D}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MiniSpace.Services.Posts.Core.UnitTests", "tests\MiniSpace.Services.Posts.Core.UnitTests\MiniSpace.Services.Posts.Core.UnitTests.csproj", "{38556B4C-90E1-453F-8BA7-C0699577A521}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MiniSpace.Services.Posts.Infrastructure.UnitTests", "tests\MiniSpace.Services.Posts.Infrastructure.UnitTests\MiniSpace.Services.Posts.Infrastructure.UnitTests.csproj", "{641E0BF5-8BAA-4ECC-9EA7-18CA3D763166}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -38,11 +44,26 @@ Global {515F6D6B-FB63-40F7-8173-2B94031F29B1}.Debug|Any CPU.Build.0 = Debug|Any CPU {515F6D6B-FB63-40F7-8173-2B94031F29B1}.Release|Any CPU.ActiveCfg = Release|Any CPU {515F6D6B-FB63-40F7-8173-2B94031F29B1}.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 + {38556B4C-90E1-453F-8BA7-C0699577A521}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {38556B4C-90E1-453F-8BA7-C0699577A521}.Debug|Any CPU.Build.0 = Debug|Any CPU + {38556B4C-90E1-453F-8BA7-C0699577A521}.Release|Any CPU.ActiveCfg = Release|Any CPU + {38556B4C-90E1-453F-8BA7-C0699577A521}.Release|Any CPU.Build.0 = Release|Any CPU + {641E0BF5-8BAA-4ECC-9EA7-18CA3D763166}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {641E0BF5-8BAA-4ECC-9EA7-18CA3D763166}.Debug|Any CPU.Build.0 = Debug|Any CPU + {641E0BF5-8BAA-4ECC-9EA7-18CA3D763166}.Release|Any CPU.ActiveCfg = Release|Any CPU + {641E0BF5-8BAA-4ECC-9EA7-18CA3D763166}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(NestedProjects) = preSolution {5D62C437-6137-4000-A90A-6549835F593B} = {16C8A8E2-B637-4A8B-828F-876523BCC79F} {A17406AF-E11D-421B-9913-9512BB9B3333} = {16C8A8E2-B637-4A8B-828F-876523BCC79F} {0FD8C55D-00F5-4059-889A-D223C7D1275C} = {16C8A8E2-B637-4A8B-828F-876523BCC79F} {515F6D6B-FB63-40F7-8173-2B94031F29B1} = {16C8A8E2-B637-4A8B-828F-876523BCC79F} + {63DF71AF-1D31-4A4D-8127-9B8EB359889D} = {16C8A8E2-B637-4A8B-828F-876523BCC79F} + {38556B4C-90E1-453F-8BA7-C0699577A521} = {16C8A8E2-B637-4A8B-828F-876523BCC79F} + {641E0BF5-8BAA-4ECC-9EA7-18CA3D763166} = {16C8A8E2-B637-4A8B-828F-876523BCC79F} EndGlobalSection EndGlobal diff --git a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.UnitTests/UnitTest1.cs b/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.UnitTests/UnitTest1.cs deleted file mode 100644 index 4a5128760..000000000 --- a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.UnitTests/UnitTest1.cs +++ /dev/null @@ -1,13 +0,0 @@ -using MiniSpace.Services.Posts.Api; - -namespace MiniSpace.Services.Posts.UnitTests; - -public class UnitTest1 -{ - [Fact] - public void Test1() - { - int sum = 2 + 3; - Assert.Equal(5, sum); - } -} \ No newline at end of file diff --git a/MiniSpace.Services.Posts/src/src.sln b/MiniSpace.Services.Posts/src/src.sln index d069185eb..4f4c608ce 100644 --- a/MiniSpace.Services.Posts/src/src.sln +++ b/MiniSpace.Services.Posts/src/src.sln @@ -11,8 +11,6 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MiniSpace.Services.Posts.Ap EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MiniSpace.Services.Posts.Application", "MiniSpace.Services.Posts.Application\MiniSpace.Services.Posts.Application.csproj", "{4D79B5C0-6E85-44B6-9BF4-E36822BF711C}" EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MiniSpace.Services.Posts.UnitTests", "MiniSpace.Services.Posts.UnitTests\MiniSpace.Services.Posts.UnitTests.csproj", "{26DA1AF1-E17D-47EB-B36A-C4C31E59B3D9}" -EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -35,10 +33,6 @@ Global {4D79B5C0-6E85-44B6-9BF4-E36822BF711C}.Debug|Any CPU.Build.0 = Debug|Any CPU {4D79B5C0-6E85-44B6-9BF4-E36822BF711C}.Release|Any CPU.ActiveCfg = Release|Any CPU {4D79B5C0-6E85-44B6-9BF4-E36822BF711C}.Release|Any CPU.Build.0 = Release|Any CPU - {26DA1AF1-E17D-47EB-B36A-C4C31E59B3D9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {26DA1AF1-E17D-47EB-B36A-C4C31E59B3D9}.Debug|Any CPU.Build.0 = Debug|Any CPU - {26DA1AF1-E17D-47EB-B36A-C4C31E59B3D9}.Release|Any CPU.ActiveCfg = Release|Any CPU - {26DA1AF1-E17D-47EB-B36A-C4C31E59B3D9}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.UnitTests/MiniSpace.Services.Posts.UnitTests.csproj b/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Application.UnitTests/MiniSpace.Services.Posts.Application.UnitTests.csproj similarity index 57% rename from MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.UnitTests/MiniSpace.Services.Posts.UnitTests.csproj rename to MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Application.UnitTests/MiniSpace.Services.Posts.Application.UnitTests.csproj index ca7bb7cf1..f17a4d184 100644 --- a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.UnitTests/MiniSpace.Services.Posts.UnitTests.csproj +++ b/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Application.UnitTests/MiniSpace.Services.Posts.Application.UnitTests.csproj @@ -21,10 +21,10 @@ - - - - + + + + diff --git a/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Core.UnitTests/MiniSpace.Services.Posts.Core.UnitTests.csproj b/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Core.UnitTests/MiniSpace.Services.Posts.Core.UnitTests.csproj new file mode 100644 index 000000000..f17a4d184 --- /dev/null +++ b/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Core.UnitTests/MiniSpace.Services.Posts.Core.UnitTests.csproj @@ -0,0 +1,30 @@ + + + + net8.0 + enable + disable + + false + true + + + + + + + + + + + + + + + + + + + + + diff --git a/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Infrastructure.UnitTests/MiniSpace.Services.Posts.Infrastructure.UnitTests.csproj b/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Infrastructure.UnitTests/MiniSpace.Services.Posts.Infrastructure.UnitTests.csproj new file mode 100644 index 000000000..f17a4d184 --- /dev/null +++ b/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Infrastructure.UnitTests/MiniSpace.Services.Posts.Infrastructure.UnitTests.csproj @@ -0,0 +1,30 @@ + + + + net8.0 + enable + disable + + false + true + + + + + + + + + + + + + + + + + + + + + From 6a6bd7ff8872315275438e5b67a850b5f5218313 Mon Sep 17 00:00:00 2001 From: olegkiprik Date: Sun, 19 May 2024 21:25:56 +0200 Subject: [PATCH 04/24] (#149) Prepare the project for implementing tests --- .../Services/MessageBroker.cs | 2 + .../Handlers/ChangePostStateHandlerTest.cs | 46 ++++++ .../Handlers/CreatePostHandlerTest.cs | 50 ++++++ .../Handlers/DeletePostHandlerTest.cs | 45 ++++++ .../Handlers/UpdatePostHandlerTest.cs | 48 ++++++ .../Handlers/UpdatePostStateHandlerTest.cs | 42 ++++++ .../Handlers/EventCreatedHandlerTest.cs | 40 +++++ .../Handlers/EventDeletedHandlerTest.cs | 41 +++++ ...ervices.Posts.Application.UnitTests.csproj | 20 ++- .../Entities/AggregatedIdTest.cs | 52 +++++++ ...Space.Services.Posts.Core.UnitTests.csproj | 20 ++- ...ices.Posts.Infrastructure.UnitTests.csproj | 20 ++- .../Services/MessageBrokerTest.cs | 142 ++++++++++++++++++ 13 files changed, 544 insertions(+), 24 deletions(-) create mode 100644 MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Application.UnitTests/Commands/Handlers/ChangePostStateHandlerTest.cs create mode 100644 MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Application.UnitTests/Commands/Handlers/CreatePostHandlerTest.cs create mode 100644 MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Application.UnitTests/Commands/Handlers/DeletePostHandlerTest.cs create mode 100644 MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Application.UnitTests/Commands/Handlers/UpdatePostHandlerTest.cs create mode 100644 MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Application.UnitTests/Commands/Handlers/UpdatePostStateHandlerTest.cs create mode 100644 MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Application.UnitTests/Events/External/Handlers/EventCreatedHandlerTest.cs create mode 100644 MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Application.UnitTests/Events/External/Handlers/EventDeletedHandlerTest.cs create mode 100644 MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Core.UnitTests/Entities/AggregatedIdTest.cs create mode 100644 MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Infrastructure.UnitTests/Services/MessageBrokerTest.cs diff --git a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Infrastructure/Services/MessageBroker.cs b/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Infrastructure/Services/MessageBroker.cs index 83b388249..b4ed67764 100644 --- a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Infrastructure/Services/MessageBroker.cs +++ b/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Infrastructure/Services/MessageBroker.cs @@ -6,7 +6,9 @@ using Microsoft.Extensions.Logging; using OpenTracing; using MiniSpace.Services.Posts.Application.Services; +using System.Runtime.CompilerServices; +[assembly: InternalsVisibleTo("MiniSpace.Services.Posts.Infrastructure.UnitTests")] namespace MiniSpace.Services.Posts.Infrastructure.Services { internal sealed class MessageBroker : IMessageBroker diff --git a/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Application.UnitTests/Commands/Handlers/ChangePostStateHandlerTest.cs b/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Application.UnitTests/Commands/Handlers/ChangePostStateHandlerTest.cs new file mode 100644 index 000000000..5759250fa --- /dev/null +++ b/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Application.UnitTests/Commands/Handlers/ChangePostStateHandlerTest.cs @@ -0,0 +1,46 @@ +using Xunit; +using Moq; +using System; +using System.Collections.Generic; +using System.Threading.Tasks; +using System.Linq; +using System.Text; +using MiniSpace.Services.Posts.Application.Events; +using MiniSpace.Services.Posts.Application.Exceptions; +using MiniSpace.Services.Posts.Application.Services; +using MiniSpace.Services.Posts.Core.Entities; +using MiniSpace.Services.Posts.Core.Repositories; +using MiniSpace.Services.Posts.Application.Commands.Handlers; +using MiniSpace.Services.Posts.Application.Commands; +using MiniSpace.Services.Posts.Infrastructure.Contexts; +using Convey.CQRS.Commands; +using System.Threading; +using System.Security.Claims; +using FluentAssertions; + +namespace MiniSpace.Services.Posts.Application.UnitTests.Commands.Handlers { + public class ChangePostStateHandlerTest { + private readonly ChangePostStateHandler _changePostStateHandler; + private readonly Mock _postRepositoryMock; + private readonly Mock _dateTimeProviderMock; + private readonly Mock _messageBrokerMock; + private readonly Mock _appContextMock; + + public ChangePostStateHandlerTest() { + _postRepositoryMock = new(); + _messageBrokerMock = new(); + _changePostStateHandler = new ChangePostStateHandler(_postRepositoryMock.Object, + _appContextMock.Object, + _dateTimeProviderMock.Object, + _messageBrokerMock.Object + ); + } + + [Fact] + public async Task HandleAsync_XXX_ShouldXXX() { + + } + + + } +} \ No newline at end of file diff --git a/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Application.UnitTests/Commands/Handlers/CreatePostHandlerTest.cs b/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Application.UnitTests/Commands/Handlers/CreatePostHandlerTest.cs new file mode 100644 index 000000000..d984149e5 --- /dev/null +++ b/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Application.UnitTests/Commands/Handlers/CreatePostHandlerTest.cs @@ -0,0 +1,50 @@ +using Xunit; +using Moq; +using System; +using System.Collections.Generic; +using System.Threading.Tasks; +using System.Linq; +using System.Text; +using MiniSpace.Services.Posts.Application.Events; +using MiniSpace.Services.Posts.Application.Exceptions; +using MiniSpace.Services.Posts.Application.Services; +using MiniSpace.Services.Posts.Core.Entities; +using MiniSpace.Services.Posts.Core.Repositories; +using MiniSpace.Services.Posts.Application.Commands.Handlers; +using MiniSpace.Services.Posts.Application.Commands; +using MiniSpace.Services.Posts.Infrastructure.Contexts; +using Convey.CQRS.Commands; +using System.Threading; +using System.Security.Claims; +using FluentAssertions; + +namespace MiniSpace.Services.Posts.Application.UnitTests.Commands.Handlers { + public class CreatePostHandlerTest { + private readonly CreatePostHandler _createPostHandler; + private readonly Mock _postRepositoryMock; + private readonly Mock _eventRepositoryMock; + private readonly Mock _dateTimeProviderMock; + private readonly Mock _messageBrokerMock; + private readonly Mock _appContextMock; + + public CreatePostHandlerTest() { + _postRepositoryMock = new(); + _eventRepositoryMock = new(); + _dateTimeProviderMock = new(); + _messageBrokerMock = new(); + _appContextMock = new(); + _createPostHandler = new CreatePostHandler(_postRepositoryMock.Object, + _eventRepositoryMock.Object, + _dateTimeProviderMock.Object, + _messageBrokerMock.Object, + _appContextMock.Object); + } + + [Fact] + public async Task HandleAsync_XXX_ShouldXXX() { + + } + + + } +} \ No newline at end of file diff --git a/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Application.UnitTests/Commands/Handlers/DeletePostHandlerTest.cs b/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Application.UnitTests/Commands/Handlers/DeletePostHandlerTest.cs new file mode 100644 index 000000000..6b06caf56 --- /dev/null +++ b/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Application.UnitTests/Commands/Handlers/DeletePostHandlerTest.cs @@ -0,0 +1,45 @@ +using Xunit; +using Moq; +using System; +using System.Collections.Generic; +using System.Threading.Tasks; +using System.Linq; +using System.Text; +using MiniSpace.Services.Posts.Application.Events; +using MiniSpace.Services.Posts.Application.Exceptions; +using MiniSpace.Services.Posts.Application.Services; +using MiniSpace.Services.Posts.Core.Entities; +using MiniSpace.Services.Posts.Core.Repositories; +using MiniSpace.Services.Posts.Application.Commands.Handlers; +using MiniSpace.Services.Posts.Application.Commands; +using MiniSpace.Services.Posts.Infrastructure.Contexts; +using Convey.CQRS.Commands; +using System.Threading; +using System.Security.Claims; +using FluentAssertions; + +namespace MiniSpace.Services.Posts.Application.UnitTests.Commands.Handlers { + public class DeletePostHandlerTest { + private readonly DeletePostHandler _deletePostHandler; + private readonly Mock _postRepositoryMock; + private readonly Mock _messageBrokerMock; + private readonly Mock _appContextMock; + + public DeletePostHandlerTest() { + _postRepositoryMock = new(); + _messageBrokerMock = new(); + _appContextMock = new(); + _deletePostHandler = new DeletePostHandler(_postRepositoryMock.Object, + _appContextMock.Object, + _messageBrokerMock.Object + ); + } + + [Fact] + public async Task HandleAsync_XXX_ShouldXXX() { + + } + + + } +} \ No newline at end of file diff --git a/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Application.UnitTests/Commands/Handlers/UpdatePostHandlerTest.cs b/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Application.UnitTests/Commands/Handlers/UpdatePostHandlerTest.cs new file mode 100644 index 000000000..06c9896cf --- /dev/null +++ b/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Application.UnitTests/Commands/Handlers/UpdatePostHandlerTest.cs @@ -0,0 +1,48 @@ +using Xunit; +using Moq; +using System; +using System.Collections.Generic; +using System.Threading.Tasks; +using System.Linq; +using System.Text; +using MiniSpace.Services.Posts.Application.Events; +using MiniSpace.Services.Posts.Application.Exceptions; +using MiniSpace.Services.Posts.Application.Services; +using MiniSpace.Services.Posts.Core.Entities; +using MiniSpace.Services.Posts.Core.Repositories; +using MiniSpace.Services.Posts.Application.Commands.Handlers; +using MiniSpace.Services.Posts.Application.Commands; +using MiniSpace.Services.Posts.Infrastructure.Contexts; +using Convey.CQRS.Commands; +using System.Threading; +using System.Security.Claims; +using FluentAssertions; + +namespace MiniSpace.Services.Posts.Application.UnitTests.Commands.Handlers { + public class UpdatePostHandlerTest { + private readonly UpdatePostHandler _updatePostHandler; + private readonly Mock _postRepositoryMock; + private readonly Mock _dateTimeProviderMock; + private readonly Mock _messageBrokerMock; + private readonly Mock _appContextMock; + + public UpdatePostHandlerTest() { + _postRepositoryMock = new(); + _dateTimeProviderMock = new(); + _messageBrokerMock = new(); + _appContextMock = new(); + _updatePostHandler = new UpdatePostHandler(_postRepositoryMock.Object, + _appContextMock.Object, + _messageBrokerMock.Object, + _dateTimeProviderMock.Object + ); + } + + [Fact] + public async Task HandleAsync_XXX_ShouldXXX() { + + } + + + } +} \ No newline at end of file diff --git a/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Application.UnitTests/Commands/Handlers/UpdatePostStateHandlerTest.cs b/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Application.UnitTests/Commands/Handlers/UpdatePostStateHandlerTest.cs new file mode 100644 index 000000000..576e23499 --- /dev/null +++ b/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Application.UnitTests/Commands/Handlers/UpdatePostStateHandlerTest.cs @@ -0,0 +1,42 @@ +using Xunit; +using Moq; +using System; +using System.Collections.Generic; +using System.Threading.Tasks; +using System.Linq; +using System.Text; +using MiniSpace.Services.Posts.Application.Events; +using MiniSpace.Services.Posts.Application.Exceptions; +using MiniSpace.Services.Posts.Application.Services; +using MiniSpace.Services.Posts.Core.Entities; +using MiniSpace.Services.Posts.Core.Repositories; +using MiniSpace.Services.Posts.Application.Commands.Handlers; +using MiniSpace.Services.Posts.Application.Commands; +using MiniSpace.Services.Posts.Infrastructure.Contexts; +using Convey.CQRS.Commands; +using System.Threading; +using System.Security.Claims; +using FluentAssertions; + +namespace MiniSpace.Services.Posts.Application.UnitTests.Commands.Handlers { + public class UpdatePostStateHandlerTest { + private readonly UpdatePostsStateHandler _updatePostStateHandler; + private readonly Mock _postRepositoryMock; + private readonly Mock _messageBrokerMock; + + public UpdatePostStateHandlerTest() { + _postRepositoryMock = new(); + _messageBrokerMock = new(); + _updatePostStateHandler = new UpdatePostsStateHandler(_postRepositoryMock.Object, + _messageBrokerMock.Object + ); + } + + [Fact] + public async Task HandleAsync_XXX_ShouldXXX() { + + } + + + } +} \ No newline at end of file diff --git a/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Application.UnitTests/Events/External/Handlers/EventCreatedHandlerTest.cs b/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Application.UnitTests/Events/External/Handlers/EventCreatedHandlerTest.cs new file mode 100644 index 000000000..fa711e1c5 --- /dev/null +++ b/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Application.UnitTests/Events/External/Handlers/EventCreatedHandlerTest.cs @@ -0,0 +1,40 @@ +using Xunit; +using Moq; +using System; +using System.Collections.Generic; +using System.Threading.Tasks; +using MiniSpace.Services.Posts.Application.Exceptions; +using MiniSpace.Services.Posts.Application.Services; +using MiniSpace.Services.Posts.Core.Entities; +using MiniSpace.Services.Posts.Core.Repositories; +using MiniSpace.Services.Posts.Application.Commands.Handlers; +using MiniSpace.Services.Posts.Application.Commands; +using MiniSpace.Services.Posts.Infrastructure.Contexts; +using System.Threading; +using FluentAssertions; +using MiniSpace.Services.Posts.Application.Events.External.Handlers; +using MiniSpace.Services.Posts.Application.Events.External; +using System.ComponentModel.Design; +using Convey.CQRS.Commands; + +namespace MiniSpace.Services.Posts.Application.UnitTests.Events.External.Handlers +{ + public class EventCreatedHandlerTest + { + private readonly EventCreatedHandler _eventDeletedHandler; + private readonly Mock _postRepositoryMock; + private readonly Mock _eventRepositoryMock; + private readonly Mock _commandDispatcherMock; + + public EventCreatedHandlerTest() + { + _postRepositoryMock = new Mock(); + _eventDeletedHandler = new EventCreatedHandler(_eventRepositoryMock.Object); + } + + [Fact] + public async Task HandleAsync_XXX_ShouldXXX() { + + } + } +} \ No newline at end of file diff --git a/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Application.UnitTests/Events/External/Handlers/EventDeletedHandlerTest.cs b/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Application.UnitTests/Events/External/Handlers/EventDeletedHandlerTest.cs new file mode 100644 index 000000000..064d08a70 --- /dev/null +++ b/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Application.UnitTests/Events/External/Handlers/EventDeletedHandlerTest.cs @@ -0,0 +1,41 @@ +using Xunit; +using Moq; +using System; +using System.Collections.Generic; +using System.Threading.Tasks; +using MiniSpace.Services.Posts.Application.Exceptions; +using MiniSpace.Services.Posts.Application.Services; +using MiniSpace.Services.Posts.Core.Entities; +using MiniSpace.Services.Posts.Core.Repositories; +using MiniSpace.Services.Posts.Application.Commands.Handlers; +using MiniSpace.Services.Posts.Application.Commands; +using MiniSpace.Services.Posts.Infrastructure.Contexts; +using System.Threading; +using FluentAssertions; +using MiniSpace.Services.Posts.Application.Events.External.Handlers; +using MiniSpace.Services.Posts.Application.Events.External; +using System.ComponentModel.Design; +using Convey.CQRS.Commands; + +namespace MiniSpace.Services.Posts.Application.UnitTests.Events.External.Handlers +{ + public class EventDeletedHandlerTest + { + private readonly EventDeletedHandler _eventDeletedHandler; + private readonly Mock _postRepositoryMock; + private readonly Mock _eventRepositoryMock; + private readonly Mock _commandDispatcherMock; + + public EventDeletedHandlerTest() + { + _postRepositoryMock = new Mock(); + _eventDeletedHandler = new EventDeletedHandler(_eventRepositoryMock.Object, + _postRepositoryMock.Object, _commandDispatcherMock.Object); + } + + [Fact] + public async Task HandleAsync_XXX_ShouldXXX() { + + } + } +} \ No newline at end of file diff --git a/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Application.UnitTests/MiniSpace.Services.Posts.Application.UnitTests.csproj b/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Application.UnitTests/MiniSpace.Services.Posts.Application.UnitTests.csproj index f17a4d184..59068af34 100644 --- a/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Application.UnitTests/MiniSpace.Services.Posts.Application.UnitTests.csproj +++ b/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Application.UnitTests/MiniSpace.Services.Posts.Application.UnitTests.csproj @@ -10,14 +10,18 @@ - - - - - - - - + + + + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + diff --git a/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Core.UnitTests/Entities/AggregatedIdTest.cs b/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Core.UnitTests/Entities/AggregatedIdTest.cs new file mode 100644 index 000000000..5908458bb --- /dev/null +++ b/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Core.UnitTests/Entities/AggregatedIdTest.cs @@ -0,0 +1,52 @@ +using Xunit; +using Moq; +using System; +using System.Collections.Generic; +using System.Threading.Tasks; +using System.Linq; +using System.Text; +using MiniSpace.Services.Posts.Application.Events; +using MiniSpace.Services.Posts.Application.Exceptions; +using MiniSpace.Services.Posts.Application.Services; +using MiniSpace.Services.Posts.Core.Entities; +using MiniSpace.Services.Posts.Core.Repositories; +using MiniSpace.Services.Posts.Application.Commands.Handlers; +using MiniSpace.Services.Posts.Application.Commands; +using MiniSpace.Services.Posts.Infrastructure.Contexts; +using Convey.CQRS.Commands; +using System.Threading; +using System.Security.Claims; +using FluentAssertions; +using MiniSpace.Services.Posts.Core.Exceptions; +using Microsoft.AspNetCore.DataProtection.KeyManagement; + +namespace MiniSpace.Services.Posts.Core.UnitTests.Entities +{ + public class AggregatedIdTest + { + [Fact] + public void AggregateId_CreatedTwice_ShouldBeDifferent() + { + // Arrange & Act + var id1 = new AggregateId(); + var id2 = new AggregateId(); + + // Assert + Assert.NotEqual(id1.Value, id2.Value); + } + + [Fact] + public void AggregateId_CreatedTwiceSameGuid_ShouldBeSame() + { + // Arrange + var id = Guid.NewGuid(); + + // Act + var id1 = new AggregateId(id); + var id2 = new AggregateId(id); + + // Assert + Assert.Equal(id1.Value, id2.Value); + } + } +} \ No newline at end of file diff --git a/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Core.UnitTests/MiniSpace.Services.Posts.Core.UnitTests.csproj b/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Core.UnitTests/MiniSpace.Services.Posts.Core.UnitTests.csproj index f17a4d184..59068af34 100644 --- a/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Core.UnitTests/MiniSpace.Services.Posts.Core.UnitTests.csproj +++ b/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Core.UnitTests/MiniSpace.Services.Posts.Core.UnitTests.csproj @@ -10,14 +10,18 @@ - - - - - - - - + + + + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + diff --git a/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Infrastructure.UnitTests/MiniSpace.Services.Posts.Infrastructure.UnitTests.csproj b/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Infrastructure.UnitTests/MiniSpace.Services.Posts.Infrastructure.UnitTests.csproj index f17a4d184..59068af34 100644 --- a/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Infrastructure.UnitTests/MiniSpace.Services.Posts.Infrastructure.UnitTests.csproj +++ b/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Infrastructure.UnitTests/MiniSpace.Services.Posts.Infrastructure.UnitTests.csproj @@ -10,14 +10,18 @@ - - - - - - - - + + + + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + diff --git a/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Infrastructure.UnitTests/Services/MessageBrokerTest.cs b/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Infrastructure.UnitTests/Services/MessageBrokerTest.cs new file mode 100644 index 000000000..6c9f6d58e --- /dev/null +++ b/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Infrastructure.UnitTests/Services/MessageBrokerTest.cs @@ -0,0 +1,142 @@ +using Xunit; +using Moq; +using Convey.CQRS.Events; +using System; +using System.Collections.Generic; +using System.Threading.Tasks; +using Convey.MessageBrokers; +using Convey.MessageBrokers.Outbox; +using Convey.MessageBrokers.RabbitMQ; +using Microsoft.AspNetCore.Http; +using Microsoft.Extensions.Logging; +using OpenTracing; +using MiniSpace.Services.Posts.Infrastructure.Services; +using MiniSpace.Services.Posts.Application.Services; +using MiniSpace.Services.Posts.Application.Events; + +namespace MiniSpace.Services.Posts.Infrastructure.UnitTests.Services +{ + public class MessageBrokerTest + { + private readonly MessageBroker _messageBroker; + private readonly Mock _mockBusPublisher; + private readonly Mock _mockMessageOutbox; + private readonly Mock _mockContextAccessor; + private readonly Mock _mockHttpContextAccessor; + private readonly Mock _mockMessagePropertiesAccessor; + private readonly Mock _mockTracer; + private readonly Mock> _mockLogger; + + public MessageBrokerTest() + { + _mockBusPublisher = new Mock(); + _mockMessageOutbox = new Mock(); + _mockContextAccessor = new Mock(); + _mockHttpContextAccessor = new Mock(); + _mockMessagePropertiesAccessor = new Mock(); + _mockTracer = new Mock(); + _mockLogger = new Mock>(); + + _messageBroker = new MessageBroker(_mockBusPublisher.Object, _mockMessageOutbox.Object, _mockContextAccessor.Object, + _mockHttpContextAccessor.Object, _mockMessagePropertiesAccessor.Object, new RabbitMqOptions(), + _mockTracer.Object, _mockLogger.Object); + } + + [Fact] + public async Task PublishAsync_WithEventsAndOutboxDisabled_PublishesEvents() + { + //Arrange + var events = new List + { + new PostCreated(Guid.NewGuid()) + }; + _mockMessageOutbox.Setup(x => x.Enabled).Returns(false); + + //Act + await _messageBroker.PublishAsync(events); + + //Assert + _mockMessageOutbox.Verify( + x => x.SendAsync(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), + It.IsAny(), It.IsAny(), It.IsAny>()), + Times.Never + ); + _mockBusPublisher.Verify( + x => x.PublishAsync(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), + It.IsAny(), It.IsAny>()), Times.Exactly(events.Count) + ); + } + + [Fact] + public async Task PublishAsync_WithEventsAndOutboxEnabled_SendsMessagesToOutbox() + { + //Arrange + var events = new List + { + new PostCreated(Guid.NewGuid()) + }; + _mockMessageOutbox.Setup(x => x.Enabled).Returns(true); + + //Act + await _messageBroker.PublishAsync(events); + + //Assert + _mockMessageOutbox.Verify( + x => x.SendAsync(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), + It.IsAny(), It.IsAny(), It.IsAny>()), + Times.Exactly(events.Count) + ); + _mockBusPublisher.Verify( + x => x.PublishAsync(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), + It.IsAny(), It.IsAny>()), Times.Never + ); + } + + [Fact] + public async Task PublishAsync_WithoutEvents_Returns() + { + //Arrange + List events = null; + + //Act + await _messageBroker.PublishAsync(events); + + //Assert + _mockMessageOutbox.Verify( + x => x.SendAsync(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), + It.IsAny(), It.IsAny(), It.IsAny>()), + Times.Never + ); + _mockBusPublisher.Verify( + x => x.PublishAsync(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), + It.IsAny(), It.IsAny>()), Times.Never + ); + } + + [Fact] + public async Task PublishAsync_WithNullEventAndOutboxDisabled_PublishesOneLessEvent() + { + //Arrange + var events = new List + { + new PostCreated(Guid.NewGuid()), + null + }; + _mockMessageOutbox.Setup(x => x.Enabled).Returns(false); + + //Act + await _messageBroker.PublishAsync(events); + + //Assert + _mockMessageOutbox.Verify( + x => x.SendAsync(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), + It.IsAny(), It.IsAny(), It.IsAny>()), + Times.Never + ); + _mockBusPublisher.Verify( + x => x.PublishAsync(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), + It.IsAny(), It.IsAny>()), Times.Exactly(events.Count - 1) + ); + } + } +} \ No newline at end of file From d3fc2edfa5ee5ac7681b06e2809a0688772b172b Mon Sep 17 00:00:00 2001 From: olegkiprik Date: Sun, 19 May 2024 23:21:48 +0200 Subject: [PATCH 05/24] (#149) Add the first test --- .../Contexts/IdentityContext.cs | 2 ++ .../Handlers/ChangePostStateHandlerTest.cs | 6 ++--- .../Handlers/CreatePostHandlerTest.cs | 23 ++++++++++++++++--- .../Handlers/DeletePostHandlerTest.cs | 6 ++--- .../Handlers/UpdatePostHandlerTest.cs | 6 ++--- .../Handlers/UpdatePostStateHandlerTest.cs | 6 ++--- .../Handlers/EventCreatedHandlerTest.cs | 6 ++--- .../Handlers/EventDeletedHandlerTest.cs | 6 ++--- 8 files changed, 40 insertions(+), 21 deletions(-) diff --git a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Infrastructure/Contexts/IdentityContext.cs b/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Infrastructure/Contexts/IdentityContext.cs index eed550c5a..0c9418efe 100644 --- a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Infrastructure/Contexts/IdentityContext.cs +++ b/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Infrastructure/Contexts/IdentityContext.cs @@ -1,5 +1,7 @@ using MiniSpace.Services.Posts.Application; +using System.Runtime.CompilerServices; +[assembly: InternalsVisibleTo("MiniSpace.Services.Posts.Application.UnitTests")] namespace MiniSpace.Services.Posts.Infrastructure.Contexts { internal class IdentityContext : IIdentityContext diff --git a/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Application.UnitTests/Commands/Handlers/ChangePostStateHandlerTest.cs b/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Application.UnitTests/Commands/Handlers/ChangePostStateHandlerTest.cs index 5759250fa..8e5291bf2 100644 --- a/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Application.UnitTests/Commands/Handlers/ChangePostStateHandlerTest.cs +++ b/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Application.UnitTests/Commands/Handlers/ChangePostStateHandlerTest.cs @@ -36,10 +36,10 @@ public ChangePostStateHandlerTest() { ); } - [Fact] - public async Task HandleAsync_XXX_ShouldXXX() { + // [Fact] + // public async Task HandleAsync_XXX_ShouldXXX() { - } + // } } diff --git a/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Application.UnitTests/Commands/Handlers/CreatePostHandlerTest.cs b/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Application.UnitTests/Commands/Handlers/CreatePostHandlerTest.cs index d984149e5..a42bc6155 100644 --- a/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Application.UnitTests/Commands/Handlers/CreatePostHandlerTest.cs +++ b/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Application.UnitTests/Commands/Handlers/CreatePostHandlerTest.cs @@ -41,10 +41,27 @@ public CreatePostHandlerTest() { } [Fact] - public async Task HandleAsync_XXX_ShouldXXX() { + public async Task HandleAsync_WithNonPermittedIdentity_ShouldThrowUnauthorizedPostCreationAttemptException() { + // Arrange + var eventId = Guid.NewGuid(); + var contextId = Guid.NewGuid(); + var postId = Guid.NewGuid(); + var studentId = Guid.NewGuid(); + var cancelationToken = new CancellationToken(); + var state = State.Published; - } + var @event = new Event(eventId, contextId); + var command = new CreatePost(postId, eventId, contextId, "Post", "Media Content", + nameof(state), DateTime.Today); + + _eventRepositoryMock.Setup(repo => repo.GetAsync(eventId)).ReturnsAsync(@event); + _appContextMock.Setup(ctx => ctx.Identity.IsAuthenticated).Returns(true); + _appContextMock.Setup(ctx => ctx.Identity.Id).Should().NotBeEquivalentTo(command.OrganizerId); + _appContextMock.Setup(ctx => ctx.Identity.IsAdmin).Returns(false); - + // Act & Assert + Func act = async () => await _createPostHandler.HandleAsync(command, cancelationToken); + await act.Should().ThrowAsync(); + } } } \ No newline at end of file diff --git a/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Application.UnitTests/Commands/Handlers/DeletePostHandlerTest.cs b/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Application.UnitTests/Commands/Handlers/DeletePostHandlerTest.cs index 6b06caf56..152478ae3 100644 --- a/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Application.UnitTests/Commands/Handlers/DeletePostHandlerTest.cs +++ b/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Application.UnitTests/Commands/Handlers/DeletePostHandlerTest.cs @@ -35,10 +35,10 @@ public DeletePostHandlerTest() { ); } - [Fact] - public async Task HandleAsync_XXX_ShouldXXX() { + // [Fact] + // public async Task HandleAsync_XXX_ShouldXXX() { - } + // } } diff --git a/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Application.UnitTests/Commands/Handlers/UpdatePostHandlerTest.cs b/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Application.UnitTests/Commands/Handlers/UpdatePostHandlerTest.cs index 06c9896cf..4aaf6eab6 100644 --- a/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Application.UnitTests/Commands/Handlers/UpdatePostHandlerTest.cs +++ b/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Application.UnitTests/Commands/Handlers/UpdatePostHandlerTest.cs @@ -38,10 +38,10 @@ public UpdatePostHandlerTest() { ); } - [Fact] - public async Task HandleAsync_XXX_ShouldXXX() { + // [Fact] + // public async Task HandleAsync_XXX_ShouldXXX() { - } + // } } diff --git a/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Application.UnitTests/Commands/Handlers/UpdatePostStateHandlerTest.cs b/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Application.UnitTests/Commands/Handlers/UpdatePostStateHandlerTest.cs index 576e23499..73ec8f17d 100644 --- a/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Application.UnitTests/Commands/Handlers/UpdatePostStateHandlerTest.cs +++ b/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Application.UnitTests/Commands/Handlers/UpdatePostStateHandlerTest.cs @@ -32,10 +32,10 @@ public UpdatePostStateHandlerTest() { ); } - [Fact] - public async Task HandleAsync_XXX_ShouldXXX() { + // [Fact] + // public async Task HandleAsync_XXX_ShouldXXX() { - } + // } } diff --git a/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Application.UnitTests/Events/External/Handlers/EventCreatedHandlerTest.cs b/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Application.UnitTests/Events/External/Handlers/EventCreatedHandlerTest.cs index fa711e1c5..a238b499a 100644 --- a/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Application.UnitTests/Events/External/Handlers/EventCreatedHandlerTest.cs +++ b/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Application.UnitTests/Events/External/Handlers/EventCreatedHandlerTest.cs @@ -32,9 +32,9 @@ public EventCreatedHandlerTest() _eventDeletedHandler = new EventCreatedHandler(_eventRepositoryMock.Object); } - [Fact] - public async Task HandleAsync_XXX_ShouldXXX() { + // [Fact] + // public async Task HandleAsync_XXX_ShouldXXX() { - } + // } } } \ No newline at end of file diff --git a/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Application.UnitTests/Events/External/Handlers/EventDeletedHandlerTest.cs b/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Application.UnitTests/Events/External/Handlers/EventDeletedHandlerTest.cs index 064d08a70..4bce3b4a3 100644 --- a/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Application.UnitTests/Events/External/Handlers/EventDeletedHandlerTest.cs +++ b/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Application.UnitTests/Events/External/Handlers/EventDeletedHandlerTest.cs @@ -33,9 +33,9 @@ public EventDeletedHandlerTest() _postRepositoryMock.Object, _commandDispatcherMock.Object); } - [Fact] - public async Task HandleAsync_XXX_ShouldXXX() { + // [Fact] + // public async Task HandleAsync_XXX_ShouldXXX() { - } + // } } } \ No newline at end of file From 4dd08e21552b278f5946749874e39a7032ce4bf3 Mon Sep 17 00:00:00 2001 From: olegkiprik Date: Mon, 20 May 2024 04:02:46 +0200 Subject: [PATCH 06/24] (#149) Add tests for command and event handlers --- .../Handlers/ChangePostStateHandlerTest.cs | 219 +++++++++++++++++- .../Handlers/CreatePostHandlerTest.cs | 189 ++++++++++++++- .../Handlers/DeletePostHandlerTest.cs | 157 ++++++++++++- .../Handlers/UpdatePostHandlerTest.cs | 169 +++++++++++++- .../Handlers/UpdatePostStateHandlerTest.cs | 42 ---- .../Handlers/EventCreatedHandlerTest.cs | 37 ++- .../Handlers/EventDeletedHandlerTest.cs | 52 ++++- .../Entities/PostTest.cs | 67 ++++++ 8 files changed, 866 insertions(+), 66 deletions(-) delete mode 100644 MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Application.UnitTests/Commands/Handlers/UpdatePostStateHandlerTest.cs create mode 100644 MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Core.UnitTests/Entities/PostTest.cs diff --git a/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Application.UnitTests/Commands/Handlers/ChangePostStateHandlerTest.cs b/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Application.UnitTests/Commands/Handlers/ChangePostStateHandlerTest.cs index 8e5291bf2..2d56a5abf 100644 --- a/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Application.UnitTests/Commands/Handlers/ChangePostStateHandlerTest.cs +++ b/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Application.UnitTests/Commands/Handlers/ChangePostStateHandlerTest.cs @@ -17,6 +17,9 @@ using System.Threading; using System.Security.Claims; using FluentAssertions; +using Microsoft.OpenApi.Extensions; +using MiniSpace.Services.Posts.Core.Exceptions; +using System.Diagnostics.Eventing.Reader; namespace MiniSpace.Services.Posts.Application.UnitTests.Commands.Handlers { public class ChangePostStateHandlerTest { @@ -29,6 +32,8 @@ public class ChangePostStateHandlerTest { public ChangePostStateHandlerTest() { _postRepositoryMock = new(); _messageBrokerMock = new(); + _dateTimeProviderMock = new(); + _appContextMock = new(); _changePostStateHandler = new ChangePostStateHandler(_postRepositoryMock.Object, _appContextMock.Object, _dateTimeProviderMock.Object, @@ -36,11 +41,217 @@ public ChangePostStateHandlerTest() { ); } - // [Fact] - // public async Task HandleAsync_XXX_ShouldXXX() { + [Fact] + public async Task HandleAsync_WithValidParametersAndStateToBePublished_ShouldNotThrowException() { + // Arrange + var contextId = Guid.NewGuid(); + var postId = Guid.NewGuid(); + var cancelationToken = new CancellationToken(); + var state = State.ToBePublished; + var eventId = Guid.NewGuid(); - // } + var command = new ChangePostState(postId, + state.GetDisplayName(), DateTime.Today); - + var identityContext = new IdentityContext(contextId.ToString(), "", true, default); + _appContextMock.Setup(ctx => ctx.Identity).Returns(identityContext); + + var post = Post.Create(new AggregateId(postId), eventId, contextId, "Text", "Media content", DateTime.Today, + state, DateTime.Today); + + _postRepositoryMock.Setup(repo => repo.GetAsync(command.PostId)).ReturnsAsync(post); + + // Act & Assert + Func act = async () => await _changePostStateHandler.HandleAsync(command, cancelationToken); + await act.Should().NotThrowAsync(); + } + + [Fact] + public async Task HandleAsync_WithNullPost_ShouldThrowPostNotFoundException() { + // Arrange + var contextId = Guid.NewGuid(); + var postId = Guid.NewGuid(); + var cancelationToken = new CancellationToken(); + var state = State.ToBePublished; + var eventId = Guid.NewGuid(); + + var command = new ChangePostState(postId, + state.GetDisplayName(), DateTime.Today); + + var identityContext = new IdentityContext(contextId.ToString(), "", true, default); + _appContextMock.Setup(ctx => ctx.Identity).Returns(identityContext); + + _postRepositoryMock.Setup(repo => repo.GetAsync(command.PostId)).ReturnsAsync((Post)null); + + // Act & Assert + Func act = async () => await _changePostStateHandler.HandleAsync(command, cancelationToken); + await act.Should().ThrowAsync(); + } + + [Fact] + public async Task HandleAsync_WithNonAuthenticated_ShouldNotThrowException() { + // Arrange + var contextId = Guid.NewGuid(); + var postId = Guid.NewGuid(); + var cancelationToken = new CancellationToken(); + var state = State.ToBePublished; + var eventId = Guid.NewGuid(); + + var command = new ChangePostState(postId, + state.GetDisplayName(), DateTime.Today); + + var identityContext = new IdentityContext(contextId.ToString(), "", false, default); + _appContextMock.Setup(ctx => ctx.Identity).Returns(identityContext); + + var post = Post.Create(new AggregateId(postId), eventId, contextId, "Text", "Media content", DateTime.Today, + state, DateTime.Today); + + _postRepositoryMock.Setup(repo => repo.GetAsync(command.PostId)).ReturnsAsync(post); + + // Act & Assert + Func act = async () => await _changePostStateHandler.HandleAsync(command, cancelationToken); + await act.Should().NotThrowAsync(); + } + + [Fact] + public async Task HandleAsync_WithNonPermittedIdentity_ShouldThrowUnauthorizedPostAccessException() { + // Arrange + var contextId = Guid.NewGuid(); + var postId = Guid.NewGuid(); + var cancelationToken = new CancellationToken(); + var state = State.ToBePublished; + var eventId = Guid.NewGuid(); + + Guid differentOrganizer; + do { + differentOrganizer = Guid.NewGuid(); + } while (differentOrganizer == contextId); + + var command = new ChangePostState(postId, + state.GetDisplayName(), DateTime.Today); + + var post = Post.Create(new AggregateId(postId), eventId, differentOrganizer, + "Text", "Media content", DateTime.Today, + state, DateTime.Today); + + var identityContext = new IdentityContext(contextId.ToString(), "not admin", true, default); + _appContextMock.Setup(ctx => ctx.Identity).Returns(identityContext); + + _postRepositoryMock.Setup(repo => repo.GetAsync(command.PostId)).ReturnsAsync(post); + + // Act & Assert + Func act = async () => await _changePostStateHandler.HandleAsync(command, cancelationToken); + await act.Should().ThrowAsync(); + } + + [Fact] + public async Task HandleAsync_WithIdentityAsAdminAndForeignPost_ShouldNotThrowException() { + // Arrange + var contextId = Guid.NewGuid(); + var postId = Guid.NewGuid(); + var cancelationToken = new CancellationToken(); + var state = State.ToBePublished; + var eventId = Guid.NewGuid(); + + Guid differentOrganizer; + do { + differentOrganizer = Guid.NewGuid(); + } while (differentOrganizer == contextId); + + var command = new ChangePostState(postId, + state.GetDisplayName(), DateTime.Today); + + var post = Post.Create(new AggregateId(postId), eventId, differentOrganizer, + "Text", "Media content", DateTime.Today, + state, DateTime.Today); + + var identityContext = new IdentityContext(contextId.ToString(), "Admin", true, default); + _appContextMock.Setup(ctx => ctx.Identity).Returns(identityContext); + + _postRepositoryMock.Setup(repo => repo.GetAsync(command.PostId)).ReturnsAsync(post); + + // Act & Assert + Func act = async () => await _changePostStateHandler.HandleAsync(command, cancelationToken); + await act.Should().NotThrowAsync(); + } + + [Fact] + public async Task HandleAsync_WithInvalidState_ShouldThrowInvalidPostStateException() { + // Arrange + var contextId = Guid.NewGuid(); + var postId = Guid.NewGuid(); + var cancelationToken = new CancellationToken(); + var state = "a"; + var eventId = Guid.NewGuid(); + + var command = new ChangePostState(postId, + state, DateTime.Today); + + var post = Post.Create(new AggregateId(postId), eventId, contextId, + "Text", "Media content", DateTime.Today, + State.ToBePublished, DateTime.Today); + + var identityContext = new IdentityContext(contextId.ToString(), "", true, default); + _appContextMock.Setup(ctx => ctx.Identity).Returns(identityContext); + + _postRepositoryMock.Setup(repo => repo.GetAsync(command.PostId)).ReturnsAsync(post); + + // Act & Assert + Func act = async () => await _changePostStateHandler.HandleAsync(command, cancelationToken); + await act.Should().ThrowAsync(); + } + + [Fact] + public async Task HandleAsync_WithSameState_ShouldThrowPostStateAlreadySetException() { + // Arrange + var contextId = Guid.NewGuid(); + var postId = Guid.NewGuid(); + var cancelationToken = new CancellationToken(); + var eventId = Guid.NewGuid(); + var state = State.Published; + + var command = new ChangePostState(postId, + state.GetDisplayName(), DateTime.Today); + + var post = Post.Create(new AggregateId(postId), eventId, contextId, + "Text", "Media content", DateTime.Today, + state, DateTime.Today); + + var identityContext = new IdentityContext(contextId.ToString(), "", true, default); + _appContextMock.Setup(ctx => ctx.Identity).Returns(identityContext); + + _postRepositoryMock.Setup(repo => repo.GetAsync(command.PostId)).ReturnsAsync(post); + + // Act & Assert + Func act = async () => await _changePostStateHandler.HandleAsync(command, cancelationToken); + await act.Should().ThrowAsync(); + } + + [Fact] + public async Task HandleAsync_WithStateToBePublishedAndNullPublishDate_ShouldThrowPublishDateNullException() { + // Arrange + var contextId = Guid.NewGuid(); + var postId = Guid.NewGuid(); + var cancelationToken = new CancellationToken(); + var eventId = Guid.NewGuid(); + var state = State.ToBePublished; + DateTime? publishDate = null; + + var command = new ChangePostState(postId, + state.GetDisplayName(), publishDate); + + var post = Post.Create(new AggregateId(postId), eventId, contextId, + "Text", "Media content", DateTime.Today, + state, DateTime.Today); + + var identityContext = new IdentityContext(contextId.ToString(), "", true, default); + _appContextMock.Setup(ctx => ctx.Identity).Returns(identityContext); + + _postRepositoryMock.Setup(repo => repo.GetAsync(command.PostId)).ReturnsAsync(post); + + // Act & Assert + Func act = async () => await _changePostStateHandler.HandleAsync(command, cancelationToken); + await act.Should().ThrowAsync(); + } } } \ No newline at end of file diff --git a/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Application.UnitTests/Commands/Handlers/CreatePostHandlerTest.cs b/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Application.UnitTests/Commands/Handlers/CreatePostHandlerTest.cs index a42bc6155..640968726 100644 --- a/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Application.UnitTests/Commands/Handlers/CreatePostHandlerTest.cs +++ b/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Application.UnitTests/Commands/Handlers/CreatePostHandlerTest.cs @@ -2,7 +2,6 @@ using Moq; using System; using System.Collections.Generic; -using System.Threading.Tasks; using System.Linq; using System.Text; using MiniSpace.Services.Posts.Application.Events; @@ -17,6 +16,8 @@ using System.Threading; using System.Security.Claims; using FluentAssertions; +using MiniSpace.Services.Posts.Core.Exceptions; +using Microsoft.OpenApi.Extensions; namespace MiniSpace.Services.Posts.Application.UnitTests.Commands.Handlers { public class CreatePostHandlerTest { @@ -40,6 +41,77 @@ public CreatePostHandlerTest() { _appContextMock.Object); } + [Fact] + public async Task HandleAsync_WithValidParametersAndStatePublished_ShouldNotThrowException() { + // Arrange + var eventId = Guid.NewGuid(); + var contextId = Guid.NewGuid(); + var postId = Guid.NewGuid(); + var studentId = Guid.NewGuid(); + var cancelationToken = new CancellationToken(); + var state = State.Published.GetDisplayName(); + + var @event = new Event(eventId, contextId); + var command = new CreatePost(postId, eventId, contextId, "Post", "Media Content", + state, DateTime.Today); + + var identityContext = new IdentityContext(contextId.ToString(), "", true, default); + + _eventRepositoryMock.Setup(repo => repo.GetAsync(eventId)).ReturnsAsync(@event); + _appContextMock.Setup(ctx => ctx.Identity).Returns(identityContext); + + // Act & Assert + Func act = async () => await _createPostHandler.HandleAsync(command, cancelationToken); + await act.Should().NotThrowAsync(); + } + + [Fact] + public async Task HandleAsync_WithNonAuthenticated_ShouldNotThrowException() { + // Arrange + var eventId = Guid.NewGuid(); + var contextId = Guid.NewGuid(); + var postId = Guid.NewGuid(); + var studentId = Guid.NewGuid(); + var cancelationToken = new CancellationToken(); + var state = State.Published.GetDisplayName(); + + var @event = new Event(eventId, contextId); + var command = new CreatePost(postId, eventId, contextId, "Post", "Media Content", + state, DateTime.Today); + + var isAuthenticated = false; + + var identityContext = new IdentityContext(contextId.ToString(), "", isAuthenticated, default); + + _eventRepositoryMock.Setup(repo => repo.GetAsync(eventId)).ReturnsAsync(@event); + _appContextMock.Setup(ctx => ctx.Identity).Returns(identityContext); + + // Act & Assert + Func act = async () => await _createPostHandler.HandleAsync(command, cancelationToken); + await act.Should().NotThrowAsync(); + } + + [Fact] + public async Task HandleAsync_WithNullEvent_ShouldThrowEventNotFoundException() { + // Arrange + var eventId = Guid.NewGuid(); + var contextId = Guid.NewGuid(); + var postId = Guid.NewGuid(); + var studentId = Guid.NewGuid(); + var state = State.Published; + var cancelationToken = new CancellationToken(); + var command = new CreatePost(postId, eventId, contextId, "Post", "Media Content", + state.GetDisplayName(), DateTime.Today); + + _eventRepositoryMock.Setup(repo => repo.GetAsync(eventId)).ReturnsAsync((Event)null); + + // Act + Func act = async () => await _createPostHandler.HandleAsync(command, cancelationToken); + + // Assert + await act.Should().ThrowAsync(); + } + [Fact] public async Task HandleAsync_WithNonPermittedIdentity_ShouldThrowUnauthorizedPostCreationAttemptException() { // Arrange @@ -50,18 +122,125 @@ public async Task HandleAsync_WithNonPermittedIdentity_ShouldThrowUnauthorizedPo var cancelationToken = new CancellationToken(); var state = State.Published; + Guid differentOrganizer; + do { + differentOrganizer = Guid.NewGuid(); + } while (differentOrganizer == contextId); + var @event = new Event(eventId, contextId); + var command = new CreatePost(postId, eventId, differentOrganizer, "Post", "Media Content", + state.GetDisplayName(), DateTime.Today); + + var identityContext = new IdentityContext(contextId.ToString(), "", true, default); + + _eventRepositoryMock.Setup(repo => repo.GetAsync(eventId)).ReturnsAsync(@event); + _appContextMock.Setup(ctx => ctx.Identity).Returns(identityContext); + + // Act & Assert + Func act = async () => await _createPostHandler.HandleAsync(command, cancelationToken); + await act.Should().ThrowAsync(); + } + + [Fact] + public async Task HandleAsync_WithIdentityNotRelatedToEvent_ShouldThrowUnauthorizedPostCreationAttemptException() { + // Arrange + var eventId = Guid.NewGuid(); + var contextId = Guid.NewGuid(); + var postId = Guid.NewGuid(); + var studentId = Guid.NewGuid(); + var cancelationToken = new CancellationToken(); + var state = State.Published; + + Guid differentOrganizer; + do { + differentOrganizer = Guid.NewGuid(); + } while (differentOrganizer == contextId); + + var @event = new Event(eventId, differentOrganizer); var command = new CreatePost(postId, eventId, contextId, "Post", "Media Content", - nameof(state), DateTime.Today); + state.GetDisplayName(), DateTime.Today); + + var identityContext = new IdentityContext(contextId.ToString(), "", true, default); _eventRepositoryMock.Setup(repo => repo.GetAsync(eventId)).ReturnsAsync(@event); - _appContextMock.Setup(ctx => ctx.Identity.IsAuthenticated).Returns(true); - _appContextMock.Setup(ctx => ctx.Identity.Id).Should().NotBeEquivalentTo(command.OrganizerId); - _appContextMock.Setup(ctx => ctx.Identity.IsAdmin).Returns(false); + _appContextMock.Setup(ctx => ctx.Identity).Returns(identityContext); // Act & Assert Func act = async () => await _createPostHandler.HandleAsync(command, cancelationToken); await act.Should().ThrowAsync(); } + + [Fact] + public async Task HandleAsync_WithInvalidStateName_ShouldThrowInvalidPostStateException() { + // Arrange + var eventId = Guid.NewGuid(); + var contextId = Guid.NewGuid(); + var postId = Guid.NewGuid(); + var studentId = Guid.NewGuid(); + var cancelationToken = new CancellationToken(); + var state = "a"; + + var @event = new Event(eventId, contextId); + var command = new CreatePost(postId, eventId, contextId, "Post", "Media Content", + state, DateTime.Today); + + var identityContext = new IdentityContext(contextId.ToString(), "", true, default); + + _eventRepositoryMock.Setup(repo => repo.GetAsync(eventId)).ReturnsAsync(@event); + _appContextMock.Setup(ctx => ctx.Identity).Returns(identityContext); + + // Act & Assert + Func act = async () => await _createPostHandler.HandleAsync(command, cancelationToken); + await act.Should().ThrowAsync(); + } + + [Fact] + public async Task HandleAsync_WithNewStateReported_ShouldThrowNotAllowedPostStateException() { + // Arrange + var eventId = Guid.NewGuid(); + var contextId = Guid.NewGuid(); + var postId = Guid.NewGuid(); + var studentId = Guid.NewGuid(); + var cancelationToken = new CancellationToken(); + var state = State.Reported.GetDisplayName(); + + var @event = new Event(eventId, contextId); + var command = new CreatePost(postId, eventId, contextId, "Post", "Media Content", + state, DateTime.Today); + + var identityContext = new IdentityContext(contextId.ToString(), "", true, default); + + _eventRepositoryMock.Setup(repo => repo.GetAsync(eventId)).ReturnsAsync(@event); + _appContextMock.Setup(ctx => ctx.Identity).Returns(identityContext); + + // Act & Assert + Func act = async () => await _createPostHandler.HandleAsync(command, cancelationToken); + await act.Should().ThrowAsync(); + } + + [Fact] + public async Task HandleAsync_WithStateToBePublishedAndNullPublishDate_ShouldThrowPublishDateNullException() { + // Arrange + var eventId = Guid.NewGuid(); + var contextId = Guid.NewGuid(); + var postId = Guid.NewGuid(); + var studentId = Guid.NewGuid(); + var cancelationToken = new CancellationToken(); + var state = State.ToBePublished.GetDisplayName(); + DateTime? publishDate = null; + + var @event = new Event(eventId, contextId); + var command = new CreatePost(postId, eventId, contextId, "Post", "Media Content", state, + publishDate); + + var identityContext = new IdentityContext(contextId.ToString(), "", true, default); + + _eventRepositoryMock.Setup(repo => repo.GetAsync(eventId)).ReturnsAsync(@event); + _appContextMock.Setup(ctx => ctx.Identity).Returns(identityContext); + + // Act & Assert + Func act = async () => await _createPostHandler.HandleAsync(command, cancelationToken); + await act.Should().ThrowAsync(); + } } } \ No newline at end of file diff --git a/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Application.UnitTests/Commands/Handlers/DeletePostHandlerTest.cs b/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Application.UnitTests/Commands/Handlers/DeletePostHandlerTest.cs index 152478ae3..57d901ea0 100644 --- a/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Application.UnitTests/Commands/Handlers/DeletePostHandlerTest.cs +++ b/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Application.UnitTests/Commands/Handlers/DeletePostHandlerTest.cs @@ -35,11 +35,160 @@ public DeletePostHandlerTest() { ); } - // [Fact] - // public async Task HandleAsync_XXX_ShouldXXX() { + [Fact] + public async Task HandleAsync_WithValidParametersAndStatePublished_ShouldNotThrowException() { + // Arrange + var eventId = Guid.NewGuid(); + var contextId = Guid.NewGuid(); + var postId = Guid.NewGuid(); + var studentId = Guid.NewGuid(); + var cancelationToken = new CancellationToken(); + var state = State.Published; - // } + var @event = new Event(eventId, contextId); + var command = new DeletePost(postId); - + var post = Post.Create(new AggregateId(postId), eventId, contextId, + "Text", "Media content", DateTime.Today, + state, DateTime.Today); + + var identityContext = new IdentityContext(contextId.ToString(), "", true, default); + + _appContextMock.Setup(ctx => ctx.Identity).Returns(identityContext); + _postRepositoryMock.Setup(repo => repo.GetAsync(postId)).ReturnsAsync(post); + + // Act & Assert + Func act = async () => await _deletePostHandler.HandleAsync(command, cancelationToken); + await act.Should().NotThrowAsync(); + } + + [Fact] + public async Task HandleAsync_WithNullPost_ShouldThrowPostNotFoundException() { + // Arrange + var postId = Guid.NewGuid(); + var cancelationToken = new CancellationToken(); + var command = new DeletePost(postId); + + _postRepositoryMock.Setup(repo => repo.GetAsync(postId)).ReturnsAsync((Post)null); + + // Act & Assert + Func act = async () => await _deletePostHandler.HandleAsync(command, cancelationToken); + await act.Should().ThrowAsync(); + } + + [Fact] + public async Task HandleAsync_WithNonPermittedIdentity_ShouldThrowUnauthorizedPostAccessException() { + // Arrange + var contextId = Guid.NewGuid(); + var postId = Guid.NewGuid(); + var cancelationToken = new CancellationToken(); + var state = State.ToBePublished; + var eventId = Guid.NewGuid(); + + Guid differentOrganizer; + do { + differentOrganizer = Guid.NewGuid(); + } while (differentOrganizer == contextId); + + var command = new DeletePost(postId); + + var post = Post.Create(new AggregateId(postId), eventId, differentOrganizer, + "Text", "Media content", DateTime.Today, + state, DateTime.Today); + + var identityContext = new IdentityContext(contextId.ToString(), "not admin", true, default); + _appContextMock.Setup(ctx => ctx.Identity).Returns(identityContext); + + _postRepositoryMock.Setup(repo => repo.GetAsync(command.PostId)).ReturnsAsync(post); + + // Act & Assert + Func act = async () => await _deletePostHandler.HandleAsync(command, cancelationToken); + await act.Should().ThrowAsync(); + } + + [Fact] + public async Task HandleAsync_WithIdentityAsAdminAndForeignPost_ShouldNotThrowException() { + // Arrange + var contextId = Guid.NewGuid(); + var postId = Guid.NewGuid(); + var cancelationToken = new CancellationToken(); + var state = State.ToBePublished; + var eventId = Guid.NewGuid(); + + Guid differentOrganizer; + do { + differentOrganizer = Guid.NewGuid(); + } while (differentOrganizer == contextId); + + var command = new DeletePost(postId); + + var post = Post.Create(new AggregateId(postId), eventId, differentOrganizer, + "Text", "Media content", DateTime.Today, + state, DateTime.Today); + + var identityContext = new IdentityContext(contextId.ToString(), "Admin", true, default); + _appContextMock.Setup(ctx => ctx.Identity).Returns(identityContext); + + _postRepositoryMock.Setup(repo => repo.GetAsync(command.PostId)).ReturnsAsync(post); + + // Act & Assert + Func act = async () => await _deletePostHandler.HandleAsync(command, cancelationToken); + await act.Should().NotThrowAsync(); + } + + [Fact] + public async Task HandleAsync_WithIdentityAsAdminAndReportedPost_ShouldNotThrowException() { + // Arrange + var contextId = Guid.NewGuid(); + var postId = Guid.NewGuid(); + var cancelationToken = new CancellationToken(); + var state = State.Reported; + var eventId = Guid.NewGuid(); + + Guid differentOrganizer; + do { + differentOrganizer = Guid.NewGuid(); + } while (differentOrganizer == contextId); + + var command = new DeletePost(postId); + + var post = Post.Create(new AggregateId(postId), eventId, differentOrganizer, + "Text", "Media content", DateTime.Today, + state, DateTime.Today); + + var identityContext = new IdentityContext(contextId.ToString(), "Admin", true, default); + _appContextMock.Setup(ctx => ctx.Identity).Returns(identityContext); + + _postRepositoryMock.Setup(repo => repo.GetAsync(command.PostId)).ReturnsAsync(post); + + // Act & Assert + Func act = async () => await _deletePostHandler.HandleAsync(command, cancelationToken); + await act.Should().NotThrowAsync(); + } + + [Fact] + public async Task HandleAsync_WithIdentityAsNotAdminAndReportedPost_ShouldThrowUnauthorizedPostOperationException() { + // Arrange + var contextId = Guid.NewGuid(); + var postId = Guid.NewGuid(); + var cancelationToken = new CancellationToken(); + var state = State.Reported; + var eventId = Guid.NewGuid(); + + var command = new DeletePost(postId); + + var post = Post.Create(new AggregateId(postId), eventId, contextId, + "Text", "Media content", DateTime.Today, + state, DateTime.Today); + + var identityContext = new IdentityContext(contextId.ToString(), "not admin", true, default); + _appContextMock.Setup(ctx => ctx.Identity).Returns(identityContext); + + _postRepositoryMock.Setup(repo => repo.GetAsync(command.PostId)).ReturnsAsync(post); + + // Act & Assert + Func act = async () => await _deletePostHandler.HandleAsync(command, cancelationToken); + await act.Should().ThrowAsync(); + } } } \ No newline at end of file diff --git a/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Application.UnitTests/Commands/Handlers/UpdatePostHandlerTest.cs b/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Application.UnitTests/Commands/Handlers/UpdatePostHandlerTest.cs index 4aaf6eab6..8b85274bf 100644 --- a/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Application.UnitTests/Commands/Handlers/UpdatePostHandlerTest.cs +++ b/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Application.UnitTests/Commands/Handlers/UpdatePostHandlerTest.cs @@ -38,11 +38,172 @@ public UpdatePostHandlerTest() { ); } - // [Fact] - // public async Task HandleAsync_XXX_ShouldXXX() { + [Fact] + public async Task HandleAsync_WithValidParametersAndStatePublished_ShouldNotThrowException() { + // Arrange + var eventId = Guid.NewGuid(); + var contextId = Guid.NewGuid(); + var postId = Guid.NewGuid(); + var studentId = Guid.NewGuid(); + var cancelationToken = new CancellationToken(); + var state = State.Published; + var textContent = "a"; + var mediaContent = "a"; - // } + var @event = new Event(eventId, contextId); + var command = new UpdatePost(postId, textContent, mediaContent); - + var post = Post.Create(new AggregateId(postId), eventId, contextId, + "Text", "Media content", DateTime.Today, + state, DateTime.Today); + + var identityContext = new IdentityContext(contextId.ToString(), "", true, default); + + _appContextMock.Setup(ctx => ctx.Identity).Returns(identityContext); + _postRepositoryMock.Setup(repo => repo.GetAsync(postId)).ReturnsAsync(post); + + // Act & Assert + Func act = async () => await _updatePostHandler.HandleAsync(command, cancelationToken); + await act.Should().NotThrowAsync(); + } + + [Fact] + public async Task HandleAsync_WithNullPost_ShouldThrowPostNotFoundException() { + // Arrange + var postId = Guid.NewGuid(); + var cancelationToken = new CancellationToken(); + var textContent = "a"; + var mediaContent = "a"; + var command = new UpdatePost(postId, textContent, mediaContent); + + _postRepositoryMock.Setup(repo => repo.GetAsync(postId)).ReturnsAsync((Post)null); + + // Act & Assert + Func act = async () => await _updatePostHandler.HandleAsync(command, cancelationToken); + await act.Should().ThrowAsync(); + } + + [Fact] + public async Task HandleAsync_WithNonPermittedIdentity_ShouldThrowUnauthorizedPostAccessException() { + // Arrange + var contextId = Guid.NewGuid(); + var postId = Guid.NewGuid(); + var cancelationToken = new CancellationToken(); + var state = State.ToBePublished; + var eventId = Guid.NewGuid(); + var textContent = "a"; + var mediaContent = "a"; + + Guid differentOrganizer; + do { + differentOrganizer = Guid.NewGuid(); + } while (differentOrganizer == contextId); + + var command = new UpdatePost(postId, textContent, mediaContent); + + var post = Post.Create(new AggregateId(postId), eventId, differentOrganizer, + "Text", "Media content", DateTime.Today, + state, DateTime.Today); + + var identityContext = new IdentityContext(contextId.ToString(), "not admin", true, default); + _appContextMock.Setup(ctx => ctx.Identity).Returns(identityContext); + + _postRepositoryMock.Setup(repo => repo.GetAsync(command.PostId)).ReturnsAsync(post); + + // Act & Assert + Func act = async () => await _updatePostHandler.HandleAsync(command, cancelationToken); + await act.Should().ThrowAsync(); + } + + [Fact] + public async Task HandleAsync_WithIdentityAsAdminAndForeignPost_ShouldNotThrowException() { + // Arrange + var contextId = Guid.NewGuid(); + var postId = Guid.NewGuid(); + var cancelationToken = new CancellationToken(); + var state = State.ToBePublished; + var eventId = Guid.NewGuid(); + var textContent = "a"; + var mediaContent = "a"; + + Guid differentOrganizer; + do { + differentOrganizer = Guid.NewGuid(); + } while (differentOrganizer == contextId); + + var command = new UpdatePost(postId, textContent, mediaContent); + + var post = Post.Create(new AggregateId(postId), eventId, differentOrganizer, + "Text", "Media content", DateTime.Today, + state, DateTime.Today); + + var identityContext = new IdentityContext(contextId.ToString(), "Admin", true, default); + _appContextMock.Setup(ctx => ctx.Identity).Returns(identityContext); + + _postRepositoryMock.Setup(repo => repo.GetAsync(command.PostId)).ReturnsAsync(post); + + // Act & Assert + Func act = async () => await _updatePostHandler.HandleAsync(command, cancelationToken); + await act.Should().NotThrowAsync(); + } + + [Fact] + public async Task HandleAsync_WithIdentityAsAdminAndReportedPost_ShouldNotThrowException() { + // Arrange + var contextId = Guid.NewGuid(); + var postId = Guid.NewGuid(); + var cancelationToken = new CancellationToken(); + var state = State.Reported; + var eventId = Guid.NewGuid(); + var textContent = "a"; + var mediaContent = "a"; + + Guid differentOrganizer; + do { + differentOrganizer = Guid.NewGuid(); + } while (differentOrganizer == contextId); + + var command = new UpdatePost(postId, textContent, mediaContent); + + var post = Post.Create(new AggregateId(postId), eventId, differentOrganizer, + "Text", "Media content", DateTime.Today, + state, DateTime.Today); + + var identityContext = new IdentityContext(contextId.ToString(), "Admin", true, default); + _appContextMock.Setup(ctx => ctx.Identity).Returns(identityContext); + + _postRepositoryMock.Setup(repo => repo.GetAsync(command.PostId)).ReturnsAsync(post); + + // Act & Assert + Func act = async () => await _updatePostHandler.HandleAsync(command, cancelationToken); + await act.Should().NotThrowAsync(); + } + + [Fact] + public async Task HandleAsync_WithIdentityAsNotAdminAndReportedPost_ShouldThrowUnauthorizedPostOperationException() { + // Arrange + var contextId = Guid.NewGuid(); + var postId = Guid.NewGuid(); + var cancelationToken = new CancellationToken(); + var state = State.Reported; + var eventId = Guid.NewGuid(); + var textContent = "a"; + var mediaContent = "a"; + + var command = new UpdatePost(postId, textContent, mediaContent); + + var post = Post.Create(new AggregateId(postId), eventId, contextId, + "Text", "Media content", DateTime.Today, + state, DateTime.Today); + + var identityContext = new IdentityContext(contextId.ToString(), "not admin", true, default); + _appContextMock.Setup(ctx => ctx.Identity).Returns(identityContext); + + _postRepositoryMock.Setup(repo => repo.GetAsync(command.PostId)).ReturnsAsync(post); + + // Act & Assert + Func act = async () => await _updatePostHandler.HandleAsync(command, cancelationToken); + await act.Should().ThrowAsync(); + } } } \ No newline at end of file diff --git a/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Application.UnitTests/Commands/Handlers/UpdatePostStateHandlerTest.cs b/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Application.UnitTests/Commands/Handlers/UpdatePostStateHandlerTest.cs deleted file mode 100644 index 73ec8f17d..000000000 --- a/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Application.UnitTests/Commands/Handlers/UpdatePostStateHandlerTest.cs +++ /dev/null @@ -1,42 +0,0 @@ -using Xunit; -using Moq; -using System; -using System.Collections.Generic; -using System.Threading.Tasks; -using System.Linq; -using System.Text; -using MiniSpace.Services.Posts.Application.Events; -using MiniSpace.Services.Posts.Application.Exceptions; -using MiniSpace.Services.Posts.Application.Services; -using MiniSpace.Services.Posts.Core.Entities; -using MiniSpace.Services.Posts.Core.Repositories; -using MiniSpace.Services.Posts.Application.Commands.Handlers; -using MiniSpace.Services.Posts.Application.Commands; -using MiniSpace.Services.Posts.Infrastructure.Contexts; -using Convey.CQRS.Commands; -using System.Threading; -using System.Security.Claims; -using FluentAssertions; - -namespace MiniSpace.Services.Posts.Application.UnitTests.Commands.Handlers { - public class UpdatePostStateHandlerTest { - private readonly UpdatePostsStateHandler _updatePostStateHandler; - private readonly Mock _postRepositoryMock; - private readonly Mock _messageBrokerMock; - - public UpdatePostStateHandlerTest() { - _postRepositoryMock = new(); - _messageBrokerMock = new(); - _updatePostStateHandler = new UpdatePostsStateHandler(_postRepositoryMock.Object, - _messageBrokerMock.Object - ); - } - - // [Fact] - // public async Task HandleAsync_XXX_ShouldXXX() { - - // } - - - } -} \ No newline at end of file diff --git a/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Application.UnitTests/Events/External/Handlers/EventCreatedHandlerTest.cs b/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Application.UnitTests/Events/External/Handlers/EventCreatedHandlerTest.cs index a238b499a..4880ded06 100644 --- a/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Application.UnitTests/Events/External/Handlers/EventCreatedHandlerTest.cs +++ b/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Application.UnitTests/Events/External/Handlers/EventCreatedHandlerTest.cs @@ -22,19 +22,44 @@ namespace MiniSpace.Services.Posts.Application.UnitTests.Events.External.Handler public class EventCreatedHandlerTest { private readonly EventCreatedHandler _eventDeletedHandler; - private readonly Mock _postRepositoryMock; private readonly Mock _eventRepositoryMock; - private readonly Mock _commandDispatcherMock; public EventCreatedHandlerTest() { - _postRepositoryMock = new Mock(); + _eventRepositoryMock = new(); _eventDeletedHandler = new EventCreatedHandler(_eventRepositoryMock.Object); } - // [Fact] - // public async Task HandleAsync_XXX_ShouldXXX() { + [Fact] + public async Task HandleAsync_ValidData_ShouldNotThrowExeption() + { + // Arrange + var eventId = Guid.NewGuid(); + var organizerId = Guid.NewGuid(); + var @event = new EventCreated(eventId, organizerId); + + _eventRepositoryMock.Setup(repo => repo.ExistsAsync(eventId)) + .ReturnsAsync(false); + + // Act & Assert + Func act = async () => await _eventDeletedHandler.HandleAsync(@event); + await act.Should().NotThrowAsync(); + } - // } + [Fact] + public async Task HandleAsync_EventAlreadyCreated_ShouldThrowEventAlreadyExistsException() + { + // Arrange + var eventId = Guid.NewGuid(); + var organizerId = Guid.NewGuid(); + var @event = new EventCreated(eventId, organizerId); + + _eventRepositoryMock.Setup(repo => repo.ExistsAsync(eventId)) + .ReturnsAsync(true); + + // Act & Assert + Func act = async () => await _eventDeletedHandler.HandleAsync(@event); + await act.Should().ThrowAsync(); + } } } \ No newline at end of file diff --git a/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Application.UnitTests/Events/External/Handlers/EventDeletedHandlerTest.cs b/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Application.UnitTests/Events/External/Handlers/EventDeletedHandlerTest.cs index 4bce3b4a3..5139880ce 100644 --- a/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Application.UnitTests/Events/External/Handlers/EventDeletedHandlerTest.cs +++ b/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Application.UnitTests/Events/External/Handlers/EventDeletedHandlerTest.cs @@ -28,14 +28,64 @@ public class EventDeletedHandlerTest public EventDeletedHandlerTest() { + _eventRepositoryMock = new(); + _commandDispatcherMock = new(); _postRepositoryMock = new Mock(); _eventDeletedHandler = new EventDeletedHandler(_eventRepositoryMock.Object, _postRepositoryMock.Object, _commandDispatcherMock.Object); } + // TODO: connect ICommandDispatcher with IPostRepository + // [Fact] - // public async Task HandleAsync_XXX_ShouldXXX() { + // public async Task HandleAsync_ValidData_ShouldNotThrowException() + // { + // // Arrange + // var socialEventId = Guid.NewGuid(); + // var organizerId = Guid.NewGuid(); + // var socialEvent = new Event(socialEventId, organizerId); + // var serviceEvent = new EventDeleted(socialEventId); + // var postId1 = Guid.NewGuid(); + // var postId2 = Guid.NewGuid(); + // var post1 = Post.Create(new AggregateId(postId1), socialEventId, Guid.NewGuid(), + // "a", "a", DateTime.Today, + // State.Published, DateTime.Today); + // var post2 = Post.Create(new AggregateId(postId2), socialEventId, Guid.NewGuid(), + // "a", "a", DateTime.Today, + // State.Published, DateTime.Today); + // var postsList = new List { post1, post2 }; + + // _postRepositoryMock.Setup(repo => repo.GetByEventIdAsync(socialEventId)) + // .ReturnsAsync(postsList.AsEnumerable()); + + // var cancellationToken = new CancellationToken(); + + // _eventRepositoryMock.Setup(repo => repo.ExistsAsync(socialEventId)).ReturnsAsync(true); + // _eventRepositoryMock.Setup(repo => repo.GetAsync(socialEventId)).ReturnsAsync(socialEvent); + + // // Act + // await _eventDeletedHandler.HandleAsync(serviceEvent, cancellationToken); + // // Assert + // _postRepositoryMock.Verify(repo => repo.DeleteAsync(postId1), Times.Once()); + // _postRepositoryMock.Verify(repo => repo.DeleteAsync(postId2), Times.Once()); // } + + [Fact] + public async Task HandleAsync_NullEvent_ShouldThrowEventNotFoundException() + { + // Arrange + var eventId = Guid.NewGuid(); + var cancelationToken = new CancellationToken(); + var @event = new EventDeleted(eventId); + + _eventRepositoryMock.Setup(repo => repo.GetAsync(eventId)).ReturnsAsync((Event)null); + + // Act + Func act = async () => await _eventDeletedHandler.HandleAsync(@event, cancelationToken); + + // Assert + await act.Should().ThrowAsync(); + } } } \ No newline at end of file diff --git a/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Core.UnitTests/Entities/PostTest.cs b/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Core.UnitTests/Entities/PostTest.cs new file mode 100644 index 000000000..790ded83c --- /dev/null +++ b/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Core.UnitTests/Entities/PostTest.cs @@ -0,0 +1,67 @@ +using Xunit; +using Moq; +using System; +using System.Collections.Generic; +using System.Threading.Tasks; +using System.Linq; +using System.Text; +using MiniSpace.Services.Posts.Application.Events; +using MiniSpace.Services.Posts.Application.Exceptions; +using MiniSpace.Services.Posts.Application.Services; +using MiniSpace.Services.Posts.Core.Entities; +using MiniSpace.Services.Posts.Core.Repositories; +using MiniSpace.Services.Posts.Application.Commands.Handlers; +using MiniSpace.Services.Posts.Application.Commands; +using MiniSpace.Services.Posts.Infrastructure.Contexts; +using Convey.CQRS.Commands; +using System.Threading; +using System.Security.Claims; +using FluentAssertions; +using MiniSpace.Services.Posts.Core.Exceptions; +using Microsoft.AspNetCore.DataProtection.KeyManagement; + +namespace MiniSpace.Services.Posts.Core.UnitTests.Entities { + public class PostTest + { + // TODO: prevent exception leakage + + // [Fact] + // public void Update_WithWhitespace_ShouldThrowInvalidPostTextContentException() { + // // Arrange + // var id = new AggregateId(); + // string textContent = " "; + // var post = Post.Create(id, default, default, default, default, default, default, default); + // // Act & Assert + // Action act = () => { post.Update(textContent, "a", default); }; + // var ex = Record.Exception(act); + // Assert.NotNull(ex); + // Assert.IsType(ex); + // } + + // [Fact] + // public void Update_WithNullTextContent_ShouldThrowInvalidPostTextContentException() { + // // Arrange + // var id = new AggregateId(); + // string textContent = null; + // var post = Post.Create(id, default, default, default, default, default, default, default); + // // Act & Assert + // Action act = () => { post.Update(textContent, "a", default); }; + // var ex = Record.Exception(act); + // Assert.NotNull(ex); + // Assert.IsType(ex); + // } + + // [Fact] + // public void Update_WithTooLongTextContent_ShouldThrowInvalidPostTextContentException() { + // // Arrange + // var id = new AggregateId(); + // string textContent = new('a', 100000); + // var post = Post.Create(id, default, default, default, default, default, default, default); + // // Act & Assert + // Action act = () => { post.Update(textContent, "a", default); }; + // var ex = Record.Exception(act); + // Assert.NotNull(ex); + // Assert.IsType(ex); + // } + } +} \ No newline at end of file From faa45954df8b812a89cc88df12e3db5e324cfeb0 Mon Sep 17 00:00:00 2001 From: olegkiprik Date: Mon, 20 May 2024 07:41:05 +0200 Subject: [PATCH 07/24] (#149) Add test that checks repo update --- .../Handlers/UpdatePostHandlerTest.cs | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Application.UnitTests/Commands/Handlers/UpdatePostHandlerTest.cs b/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Application.UnitTests/Commands/Handlers/UpdatePostHandlerTest.cs index 8b85274bf..88f7a12fa 100644 --- a/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Application.UnitTests/Commands/Handlers/UpdatePostHandlerTest.cs +++ b/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Application.UnitTests/Commands/Handlers/UpdatePostHandlerTest.cs @@ -67,6 +67,36 @@ public async Task HandleAsync_WithValidParametersAndStatePublished_ShouldNotThro await act.Should().NotThrowAsync(); } + [Fact] + public async Task HandleAsync_WithValidParametersAndStatePublished_ShouldUpdateRepository() { + // Arrange + var eventId = Guid.NewGuid(); + var contextId = Guid.NewGuid(); + var postId = Guid.NewGuid(); + var studentId = Guid.NewGuid(); + var cancelationToken = new CancellationToken(); + var state = State.Published; + var textContent = "a"; + var mediaContent = "a"; + + var @event = new Event(eventId, contextId); + var command = new UpdatePost(postId, textContent, mediaContent); + + var post = Post.Create(new AggregateId(postId), eventId, contextId, + "Text", "Media content", DateTime.Today, + state, DateTime.Today); + + var identityContext = new IdentityContext(contextId.ToString(), "", true, default); + + _appContextMock.Setup(ctx => ctx.Identity).Returns(identityContext); + _postRepositoryMock.Setup(repo => repo.GetAsync(postId)).ReturnsAsync(post); + + // Act & Assert + Func act = async () => await _updatePostHandler.HandleAsync(command, cancelationToken); + await act(); + _postRepositoryMock.Verify(repo => repo.UpdateAsync(post), Times.Once()); + } + [Fact] public async Task HandleAsync_WithNullPost_ShouldThrowPostNotFoundException() { // Arrange From 7809d9915f5d28e72ca9194f90b82ab1cfcb23e4 Mon Sep 17 00:00:00 2001 From: Andrii Voznesenskyi Date: Mon, 20 May 2024 18:12:05 +0200 Subject: [PATCH 08/24] (#149) udpat the test-build script --- .github/workflows/build_microservices.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/build_microservices.yml b/.github/workflows/build_microservices.yml index 2d60a9355..145cfc683 100644 --- a/.github/workflows/build_microservices.yml +++ b/.github/workflows/build_microservices.yml @@ -12,6 +12,8 @@ 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.Posts/src/MiniSpace.Services.Posts.Api' + test_dir: 'MiniSpace.Services.Posts/tests' - project: 'MiniSpace.Web/src/MiniSpace.Web' steps: - uses: actions/checkout@v2 From f0b78c975d4f9a58e5a65f36bfab5f1e0a75b3b5 Mon Sep 17 00:00:00 2001 From: Andrii Voznesenskyi Date: Mon, 20 May 2024 18:16:17 +0200 Subject: [PATCH 09/24] (#149) udpdate the deployment script --- .github/workflows/cloud_deploy.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/cloud_deploy.yml b/.github/workflows/cloud_deploy.yml index 5b7f897c1..5a289e1e8 100644 --- a/.github/workflows/cloud_deploy.yml +++ b/.github/workflows/cloud_deploy.yml @@ -3,10 +3,10 @@ name: Deploy to Cloud on: push: branches: - - dev # dev for the test purposes here + - main # dev for the test purposes here pull_request: branches: - - dev + - main jobs: deploy: From 310d4e69a9f0208137b7db4bd46ea6352b8ec4b1 Mon Sep 17 00:00:00 2001 From: olegkiprik Date: Sun, 26 May 2024 19:28:07 +0200 Subject: [PATCH 10/24] (#149) Application: add Queries and Extensions test coverage exclusions --- .../src/MiniSpace.Services.Posts.Application/Extensions.cs | 2 ++ .../Queries/GetOrganizerPosts.cs | 2 ++ .../src/MiniSpace.Services.Posts.Application/Queries/GetPost.cs | 2 ++ .../MiniSpace.Services.Posts.Application/Queries/GetPosts.cs | 2 ++ 4 files changed, 8 insertions(+) diff --git a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Application/Extensions.cs b/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Application/Extensions.cs index bca122302..e75cdd897 100644 --- a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Application/Extensions.cs +++ b/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Application/Extensions.cs @@ -1,9 +1,11 @@ +using System.Diagnostics.CodeAnalysis; using Convey; using Convey.CQRS.Commands; using Convey.CQRS.Events; namespace MiniSpace.Services.Posts.Application { + [ExcludeFromCodeCoverage] public static class Extensions { public static IConveyBuilder AddApplication(this IConveyBuilder builder) diff --git a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Application/Queries/GetOrganizerPosts.cs b/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Application/Queries/GetOrganizerPosts.cs index 480b6361c..468acc48d 100644 --- a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Application/Queries/GetOrganizerPosts.cs +++ b/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Application/Queries/GetOrganizerPosts.cs @@ -1,9 +1,11 @@ using System.Collections; +using System.Diagnostics.CodeAnalysis; using Convey.CQRS.Queries; using MiniSpace.Services.Posts.Application.Dto; namespace MiniSpace.Services.Posts.Application.Queries { + [ExcludeFromCodeCoverage] public class GetOrganizerPosts : IQuery> { public Guid OrganizerId { get; set; } diff --git a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Application/Queries/GetPost.cs b/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Application/Queries/GetPost.cs index 3524d49c6..fde0324d4 100644 --- a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Application/Queries/GetPost.cs +++ b/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Application/Queries/GetPost.cs @@ -1,8 +1,10 @@ +using System.Diagnostics.CodeAnalysis; using Convey.CQRS.Queries; using MiniSpace.Services.Posts.Application.Dto; namespace MiniSpace.Services.Posts.Application.Queries { + [ExcludeFromCodeCoverage] public class GetPost : IQuery { public Guid PostId { get; set; } diff --git a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Application/Queries/GetPosts.cs b/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Application/Queries/GetPosts.cs index 1efeae6f9..a06a7d95f 100644 --- a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Application/Queries/GetPosts.cs +++ b/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Application/Queries/GetPosts.cs @@ -1,8 +1,10 @@ +using System.Diagnostics.CodeAnalysis; using Convey.CQRS.Queries; using MiniSpace.Services.Posts.Application.Dto; namespace MiniSpace.Services.Posts.Application.Queries { + [ExcludeFromCodeCoverage] public class GetPosts : IQuery> { public Guid EventId { get; set; } From d4621419529c5c8d7ad72f8a6ec7e84ee6132eda Mon Sep 17 00:00:00 2001 From: olegkiprik Date: Sun, 26 May 2024 19:48:58 +0200 Subject: [PATCH 11/24] (#149) Core: test InvalidAggregatedException, InvalidPostPublishDateException and InvalidPostTextContentException --- .../Entities/AggregatedIdTest.cs | 5 ++ .../Entities/PostTest.cs | 82 ++++++++++--------- 2 files changed, 50 insertions(+), 37 deletions(-) diff --git a/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Core.UnitTests/Entities/AggregatedIdTest.cs b/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Core.UnitTests/Entities/AggregatedIdTest.cs index 5908458bb..e8f4e9c27 100644 --- a/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Core.UnitTests/Entities/AggregatedIdTest.cs +++ b/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Core.UnitTests/Entities/AggregatedIdTest.cs @@ -48,5 +48,10 @@ public void AggregateId_CreatedTwiceSameGuid_ShouldBeSame() // Assert Assert.Equal(id1.Value, id2.Value); } + + [Fact] + public void AggregateId_CreateWithEmptyGuid_ShouldThrowInvalidAggregateIdException() { + Assert.Throws(() => { var id = new AggregateId(Guid.Empty); }); + } } } \ No newline at end of file diff --git a/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Core.UnitTests/Entities/PostTest.cs b/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Core.UnitTests/Entities/PostTest.cs index 790ded83c..988c319d9 100644 --- a/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Core.UnitTests/Entities/PostTest.cs +++ b/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Core.UnitTests/Entities/PostTest.cs @@ -23,45 +23,53 @@ namespace MiniSpace.Services.Posts.Core.UnitTests.Entities { public class PostTest { - // TODO: prevent exception leakage + [Fact] + public void Create_WithWhitespace_ShouldThrowInvalidPostTextContentException() { + // Arrange + var id = new AggregateId(); + string textContent = " "; + + // Act & Assert + Assert.Throws(() => { + Post.Create(id, default, default, textContent, default, default, default, default); + }); + } - // [Fact] - // public void Update_WithWhitespace_ShouldThrowInvalidPostTextContentException() { - // // Arrange - // var id = new AggregateId(); - // string textContent = " "; - // var post = Post.Create(id, default, default, default, default, default, default, default); - // // Act & Assert - // Action act = () => { post.Update(textContent, "a", default); }; - // var ex = Record.Exception(act); - // Assert.NotNull(ex); - // Assert.IsType(ex); - // } + [Fact] + public void Create_WithNullTextContent_ShouldThrowInvalidPostTextContentException() { + // Arrange + var id = new AggregateId(); + string textContent = null; + + // Act & Assert + Assert.Throws(() => { + Post.Create(id, default, default, textContent, default, default, default, default); + }); + } - // [Fact] - // public void Update_WithNullTextContent_ShouldThrowInvalidPostTextContentException() { - // // Arrange - // var id = new AggregateId(); - // string textContent = null; - // var post = Post.Create(id, default, default, default, default, default, default, default); - // // Act & Assert - // Action act = () => { post.Update(textContent, "a", default); }; - // var ex = Record.Exception(act); - // Assert.NotNull(ex); - // Assert.IsType(ex); - // } + [Fact] + public void Create_WithTooLongTextContent_ShouldThrowInvalidPostTextContentException() { + // Arrange + var id = new AggregateId(); + string textContent = new('a', 100000); + + // Act & Assert + Assert.Throws(() => { + Post.Create(id, default, default, textContent, default, default, default, default); + }); + } - // [Fact] - // public void Update_WithTooLongTextContent_ShouldThrowInvalidPostTextContentException() { - // // Arrange - // var id = new AggregateId(); - // string textContent = new('a', 100000); - // var post = Post.Create(id, default, default, default, default, default, default, default); - // // Act & Assert - // Action act = () => { post.Update(textContent, "a", default); }; - // var ex = Record.Exception(act); - // Assert.NotNull(ex); - // Assert.IsType(ex); - // } + [Fact] + public void CheckPublishDate_WithInappropriateDateTime_ShouldThrowInvalidPostPublishDateException() { + // Arrange + var id = new AggregateId(); + string textContent = new('a', 100); + var post = Post.Create(id, default, default, textContent, default, DateTime.Now, default, DateTime.Now); + + // Act & Assert + Assert.Throws(() => { + post.SetToBePublished(new DateTime(2000, 1, 1, 1, 1, 1), DateTime.Now); + }); + } } } \ No newline at end of file From ee15935f9d70cd75c61089f48415b93196625939 Mon Sep 17 00:00:00 2001 From: olegkiprik Date: Sun, 26 May 2024 20:02:49 +0200 Subject: [PATCH 12/24] (#149) Infrastructure: add extension test coverage exclusion --- .../src/MiniSpace.Services.Posts.Infrastructure/Extensions.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Infrastructure/Extensions.cs b/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Infrastructure/Extensions.cs index 0a91bf72a..f5367dbbd 100644 --- a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Infrastructure/Extensions.cs +++ b/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Infrastructure/Extensions.cs @@ -41,9 +41,11 @@ using MiniSpace.Services.Posts.Infrastructure.Mongo.Repositories; using MiniSpace.Services.Posts.Infrastructure.Services; using MiniSpace.Services.Posts.Infrastructure.Services.Workers; +using System.Diagnostics.CodeAnalysis; namespace MiniSpace.Services.Posts.Infrastructure { + [ExcludeFromCodeCoverage] public static class Extensions { public static IConveyBuilder AddInfrastructure(this IConveyBuilder builder) From 52c9cd0fd67266fe0121e22297ebb93a2e38ae3f Mon Sep 17 00:00:00 2001 From: olegkiprik Date: Sun, 26 May 2024 20:07:37 +0200 Subject: [PATCH 13/24] (#149) Infrastructure: add Context test coverage exclusions --- .../Contexts/AppContext.cs | 2 ++ .../Contexts/AppContextFactory.cs | 2 ++ .../Contexts/CorrelationContext.cs | 3 +++ .../Contexts/IdentityContext.cs | 2 ++ 4 files changed, 9 insertions(+) diff --git a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Infrastructure/Contexts/AppContext.cs b/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Infrastructure/Contexts/AppContext.cs index 62d23d593..958479ae4 100644 --- a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Infrastructure/Contexts/AppContext.cs +++ b/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Infrastructure/Contexts/AppContext.cs @@ -1,7 +1,9 @@ +using System.Diagnostics.CodeAnalysis; using MiniSpace.Services.Posts.Application; namespace MiniSpace.Services.Posts.Infrastructure.Contexts { + [ExcludeFromCodeCoverage] internal class AppContext : IAppContext { public string RequestId { get; } diff --git a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Infrastructure/Contexts/AppContextFactory.cs b/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Infrastructure/Contexts/AppContextFactory.cs index ac0ff8648..65b8aed07 100644 --- a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Infrastructure/Contexts/AppContextFactory.cs +++ b/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Infrastructure/Contexts/AppContextFactory.cs @@ -2,9 +2,11 @@ using Microsoft.AspNetCore.Http; using Newtonsoft.Json; using MiniSpace.Services.Posts.Application; +using System.Diagnostics.CodeAnalysis; namespace MiniSpace.Services.Posts.Infrastructure.Contexts { + [ExcludeFromCodeCoverage] internal sealed class AppContextFactory : IAppContextFactory { private readonly ICorrelationContextAccessor _contextAccessor; diff --git a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Infrastructure/Contexts/CorrelationContext.cs b/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Infrastructure/Contexts/CorrelationContext.cs index 191f9e53d..23753504a 100644 --- a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Infrastructure/Contexts/CorrelationContext.cs +++ b/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Infrastructure/Contexts/CorrelationContext.cs @@ -1,5 +1,8 @@ +using System.Diagnostics.CodeAnalysis; + namespace MiniSpace.Services.Posts.Infrastructure.Contexts { + [ExcludeFromCodeCoverage] internal class CorrelationContext { public string CorrelationId { get; set; } diff --git a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Infrastructure/Contexts/IdentityContext.cs b/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Infrastructure/Contexts/IdentityContext.cs index 0c9418efe..6f5735e24 100644 --- a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Infrastructure/Contexts/IdentityContext.cs +++ b/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Infrastructure/Contexts/IdentityContext.cs @@ -1,9 +1,11 @@ using MiniSpace.Services.Posts.Application; +using System.Diagnostics.CodeAnalysis; using System.Runtime.CompilerServices; [assembly: InternalsVisibleTo("MiniSpace.Services.Posts.Application.UnitTests")] namespace MiniSpace.Services.Posts.Infrastructure.Contexts { + [ExcludeFromCodeCoverage] internal class IdentityContext : IIdentityContext { public Guid Id { get; } From c88b7b7d2048c8331dd6158d2da32d229688593e Mon Sep 17 00:00:00 2001 From: olegkiprik Date: Sun, 26 May 2024 20:09:07 +0200 Subject: [PATCH 14/24] (#149) Infrastructure: remove IdentityContext test coverage exclusion --- .../Contexts/IdentityContext.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Infrastructure/Contexts/IdentityContext.cs b/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Infrastructure/Contexts/IdentityContext.cs index 6f5735e24..9d615dc00 100644 --- a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Infrastructure/Contexts/IdentityContext.cs +++ b/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Infrastructure/Contexts/IdentityContext.cs @@ -5,7 +5,6 @@ [assembly: InternalsVisibleTo("MiniSpace.Services.Posts.Application.UnitTests")] namespace MiniSpace.Services.Posts.Infrastructure.Contexts { - [ExcludeFromCodeCoverage] internal class IdentityContext : IIdentityContext { public Guid Id { get; } From e8785798c978f5c168757b0b49b3d96f48776aec Mon Sep 17 00:00:00 2001 From: olegkiprik Date: Sun, 26 May 2024 21:41:26 +0200 Subject: [PATCH 15/24] (#149) Delete draft comments --- .../Handlers/EventDeletedHandlerTest.cs | 36 ------------------- 1 file changed, 36 deletions(-) diff --git a/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Application.UnitTests/Events/External/Handlers/EventDeletedHandlerTest.cs b/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Application.UnitTests/Events/External/Handlers/EventDeletedHandlerTest.cs index 5139880ce..d8e372e1f 100644 --- a/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Application.UnitTests/Events/External/Handlers/EventDeletedHandlerTest.cs +++ b/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Application.UnitTests/Events/External/Handlers/EventDeletedHandlerTest.cs @@ -35,42 +35,6 @@ public EventDeletedHandlerTest() _postRepositoryMock.Object, _commandDispatcherMock.Object); } - // TODO: connect ICommandDispatcher with IPostRepository - - // [Fact] - // public async Task HandleAsync_ValidData_ShouldNotThrowException() - // { - // // Arrange - // var socialEventId = Guid.NewGuid(); - // var organizerId = Guid.NewGuid(); - // var socialEvent = new Event(socialEventId, organizerId); - // var serviceEvent = new EventDeleted(socialEventId); - // var postId1 = Guid.NewGuid(); - // var postId2 = Guid.NewGuid(); - // var post1 = Post.Create(new AggregateId(postId1), socialEventId, Guid.NewGuid(), - // "a", "a", DateTime.Today, - // State.Published, DateTime.Today); - // var post2 = Post.Create(new AggregateId(postId2), socialEventId, Guid.NewGuid(), - // "a", "a", DateTime.Today, - // State.Published, DateTime.Today); - // var postsList = new List { post1, post2 }; - - // _postRepositoryMock.Setup(repo => repo.GetByEventIdAsync(socialEventId)) - // .ReturnsAsync(postsList.AsEnumerable()); - - // var cancellationToken = new CancellationToken(); - - // _eventRepositoryMock.Setup(repo => repo.ExistsAsync(socialEventId)).ReturnsAsync(true); - // _eventRepositoryMock.Setup(repo => repo.GetAsync(socialEventId)).ReturnsAsync(socialEvent); - - // // Act - // await _eventDeletedHandler.HandleAsync(serviceEvent, cancellationToken); - - // // Assert - // _postRepositoryMock.Verify(repo => repo.DeleteAsync(postId1), Times.Once()); - // _postRepositoryMock.Verify(repo => repo.DeleteAsync(postId2), Times.Once()); - // } - [Fact] public async Task HandleAsync_NullEvent_ShouldThrowEventNotFoundException() { From cf80f063dbba271c7b9acc7c12ccc69df725c848 Mon Sep 17 00:00:00 2001 From: olegkiprik Date: Sat, 25 May 2024 20:07:14 +0200 Subject: [PATCH 16/24] (#149) conflics resolve --- .../src/MiniSpace.Services.Posts.Application/Dto/PostDto.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Application/Dto/PostDto.cs b/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Application/Dto/PostDto.cs index 8545cc0b6..04cc193ea 100644 --- a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Application/Dto/PostDto.cs +++ b/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Application/Dto/PostDto.cs @@ -1,7 +1,8 @@ -using MiniSpace.Services.Posts.Core.Entities; +using System.Diagnostics.CodeAnalysis; namespace MiniSpace.Services.Posts.Application.Dto { + [ExcludeFromCodeCoverage] public class PostDto { public Guid Id { get; set; } From 0cf95686d9bf4a80219ff3121c9ced48ea0365b7 Mon Sep 17 00:00:00 2001 From: olegkiprik Date: Sat, 25 May 2024 20:21:46 +0200 Subject: [PATCH 17/24] (#149) exclude logger related files from Infrastructure --- .../Exceptions/ExceptionToMessageMapper.cs | 2 ++ .../Exceptions/ExceptionToResponseMapper.cs | 2 ++ .../Logging/Extensions.cs | 2 ++ .../Logging/MessageToLogTemplateMapper.cs | 2 ++ 4 files changed, 8 insertions(+) diff --git a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Infrastructure/Exceptions/ExceptionToMessageMapper.cs b/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Infrastructure/Exceptions/ExceptionToMessageMapper.cs index 21453a8b5..caa07a45b 100644 --- a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Infrastructure/Exceptions/ExceptionToMessageMapper.cs +++ b/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Infrastructure/Exceptions/ExceptionToMessageMapper.cs @@ -1,3 +1,4 @@ +using System.Diagnostics.CodeAnalysis; using Convey.MessageBrokers.RabbitMQ; using MiniSpace.Services.Posts.Application.Commands; using MiniSpace.Services.Posts.Application.Events.Rejected; @@ -6,6 +7,7 @@ namespace MiniSpace.Services.Posts.Infrastructure.Exceptions { + [ExcludeFromCodeCoverage] internal sealed class ExceptionToMessageMapper : IExceptionToMessageMapper { public object Map(Exception exception, object message) diff --git a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Infrastructure/Exceptions/ExceptionToResponseMapper.cs b/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Infrastructure/Exceptions/ExceptionToResponseMapper.cs index 70c051e7b..42e3230dd 100644 --- a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Infrastructure/Exceptions/ExceptionToResponseMapper.cs +++ b/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Infrastructure/Exceptions/ExceptionToResponseMapper.cs @@ -1,4 +1,5 @@ using System.Collections.Concurrent; +using System.Diagnostics.CodeAnalysis; using System.Net; using Convey; using Convey.WebApi.Exceptions; @@ -7,6 +8,7 @@ namespace MiniSpace.Services.Posts.Infrastructure.Exceptions { + [ExcludeFromCodeCoverage] internal sealed class ExceptionToResponseMapper : IExceptionToResponseMapper { private static readonly ConcurrentDictionary Codes = new ConcurrentDictionary(); diff --git a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Infrastructure/Logging/Extensions.cs b/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Infrastructure/Logging/Extensions.cs index 5e5cd3f91..a1d2c95aa 100644 --- a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Infrastructure/Logging/Extensions.cs +++ b/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Infrastructure/Logging/Extensions.cs @@ -1,3 +1,4 @@ +using System.Diagnostics.CodeAnalysis; using Convey; using Convey.Logging.CQRS; using Microsoft.Extensions.DependencyInjection; @@ -5,6 +6,7 @@ namespace MiniSpace.Services.Posts.Infrastructure.Logging { + [ExcludeFromCodeCoverage] internal static class Extensions { public static IConveyBuilder AddHandlersLogging(this IConveyBuilder builder) diff --git a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Infrastructure/Logging/MessageToLogTemplateMapper.cs b/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Infrastructure/Logging/MessageToLogTemplateMapper.cs index f060a7f43..a3aa98da6 100644 --- a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Infrastructure/Logging/MessageToLogTemplateMapper.cs +++ b/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Infrastructure/Logging/MessageToLogTemplateMapper.cs @@ -1,3 +1,4 @@ +using System.Diagnostics.CodeAnalysis; using Convey.Logging.CQRS; using MiniSpace.Services.Posts.Application.Commands; using MiniSpace.Services.Posts.Application.Events; @@ -5,6 +6,7 @@ namespace MiniSpace.Services.Posts.Infrastructure.Logging { + [ExcludeFromCodeCoverage] internal sealed class MessageToLogTemplateMapper : IMessageToLogTemplateMapper { private static IReadOnlyDictionary MessageTemplates From 17f4fa7867368e07cc27e00607195269f8ac8893 Mon Sep 17 00:00:00 2001 From: olegkiprik Date: Sat, 25 May 2024 23:50:20 +0200 Subject: [PATCH 18/24] (#149) Add additional test coverage exclusions --- .../Mongo/Queries/Handlers/GetOrganizerPostsHandler.cs | 4 +++- .../Mongo/Queries/Handlers/GetPostHandler.cs | 2 ++ .../Mongo/Queries/Handlers/GetPostsHandler.cs | 2 ++ .../MiniSpace.Services.Posts.Application.UnitTests.csproj | 1 - .../MiniSpace.Services.Posts.Core.UnitTests.csproj | 1 - .../MiniSpace.Services.Posts.Infrastructure.UnitTests.csproj | 1 - 6 files changed, 7 insertions(+), 4 deletions(-) diff --git a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Infrastructure/Mongo/Queries/Handlers/GetOrganizerPostsHandler.cs b/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Infrastructure/Mongo/Queries/Handlers/GetOrganizerPostsHandler.cs index 4f84c7cb3..6ccc9cb59 100644 --- a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Infrastructure/Mongo/Queries/Handlers/GetOrganizerPostsHandler.cs +++ b/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Infrastructure/Mongo/Queries/Handlers/GetOrganizerPostsHandler.cs @@ -1,4 +1,5 @@ -using Convey.CQRS.Queries; +using System.Diagnostics.CodeAnalysis; +using Convey.CQRS.Queries; using Convey.Persistence.MongoDB; using MiniSpace.Services.Posts.Application; using MiniSpace.Services.Posts.Application.Dto; @@ -9,6 +10,7 @@ namespace MiniSpace.Services.Posts.Infrastructure.Mongo.Queries.Handlers { + [ExcludeFromCodeCoverage] public class GetOrganizerPostsHandler : IQueryHandler> { private readonly IMongoRepository _postRepository; diff --git a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Infrastructure/Mongo/Queries/Handlers/GetPostHandler.cs b/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Infrastructure/Mongo/Queries/Handlers/GetPostHandler.cs index 4a6447b53..4e88709a2 100644 --- a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Infrastructure/Mongo/Queries/Handlers/GetPostHandler.cs +++ b/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Infrastructure/Mongo/Queries/Handlers/GetPostHandler.cs @@ -1,3 +1,4 @@ +using System.Diagnostics.CodeAnalysis; using Convey.CQRS.Queries; using Convey.Persistence.MongoDB; using MiniSpace.Services.Posts.Application.Dto; @@ -6,6 +7,7 @@ namespace MiniSpace.Services.Posts.Infrastructure.Mongo.Queries.Handlers { + [ExcludeFromCodeCoverage] public class GetPostHandler : IQueryHandler { private readonly IMongoRepository _repository; diff --git a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Infrastructure/Mongo/Queries/Handlers/GetPostsHandler.cs b/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Infrastructure/Mongo/Queries/Handlers/GetPostsHandler.cs index b75fa383c..38085f674 100644 --- a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Infrastructure/Mongo/Queries/Handlers/GetPostsHandler.cs +++ b/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Infrastructure/Mongo/Queries/Handlers/GetPostsHandler.cs @@ -1,3 +1,4 @@ +using System.Diagnostics.CodeAnalysis; using Convey.CQRS.Queries; using Convey.Persistence.MongoDB; using MiniSpace.Services.Posts.Application.Dto; @@ -9,6 +10,7 @@ namespace MiniSpace.Services.Posts.Infrastructure.Mongo.Queries.Handlers { + [ExcludeFromCodeCoverage] public class GetPostsHandler : IQueryHandler> { private readonly IMongoRepository _postRepository; diff --git a/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Application.UnitTests/MiniSpace.Services.Posts.Application.UnitTests.csproj b/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Application.UnitTests/MiniSpace.Services.Posts.Application.UnitTests.csproj index 59068af34..3a7933449 100644 --- a/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Application.UnitTests/MiniSpace.Services.Posts.Application.UnitTests.csproj +++ b/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Application.UnitTests/MiniSpace.Services.Posts.Application.UnitTests.csproj @@ -25,7 +25,6 @@ - diff --git a/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Core.UnitTests/MiniSpace.Services.Posts.Core.UnitTests.csproj b/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Core.UnitTests/MiniSpace.Services.Posts.Core.UnitTests.csproj index 59068af34..3a7933449 100644 --- a/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Core.UnitTests/MiniSpace.Services.Posts.Core.UnitTests.csproj +++ b/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Core.UnitTests/MiniSpace.Services.Posts.Core.UnitTests.csproj @@ -25,7 +25,6 @@ - diff --git a/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Infrastructure.UnitTests/MiniSpace.Services.Posts.Infrastructure.UnitTests.csproj b/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Infrastructure.UnitTests/MiniSpace.Services.Posts.Infrastructure.UnitTests.csproj index 59068af34..3a7933449 100644 --- a/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Infrastructure.UnitTests/MiniSpace.Services.Posts.Infrastructure.UnitTests.csproj +++ b/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Infrastructure.UnitTests/MiniSpace.Services.Posts.Infrastructure.UnitTests.csproj @@ -25,7 +25,6 @@ - From 560d174c852d5d6298605b44eec3f43458cd1330 Mon Sep 17 00:00:00 2001 From: olegkiprik Date: Sat, 25 May 2024 23:51:33 +0200 Subject: [PATCH 19/24] (#149) Add additional test coverage exclusions --- .../src/MiniSpace.Services.Posts.Api/Program.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Api/Program.cs b/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Api/Program.cs index 8e78a520c..d13754769 100644 --- a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Api/Program.cs +++ b/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Api/Program.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; using System.Threading.Tasks; using Convey; using Convey.Logging; @@ -19,6 +20,7 @@ namespace MiniSpace.Services.Posts.Api { + [ExcludeFromCodeCoverage] public class Program { public static async Task Main(string[] args) From bbde62329b14a76451f5a2246139a6a9726ef555 Mon Sep 17 00:00:00 2001 From: olegkiprik Date: Sun, 26 May 2024 16:52:34 +0200 Subject: [PATCH 20/24] (#149) Add event rejected test coverage exclusions --- .../Events/Rejected/ChangePostStateRejected.cs | 2 ++ .../Events/Rejected/CreatePostRejected.cs | 2 ++ .../Events/Rejected/DeletePostRejected.cs | 2 ++ .../Events/Rejected/UpdatePostRejected.cs | 2 ++ 4 files changed, 8 insertions(+) diff --git a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Application/Events/Rejected/ChangePostStateRejected.cs b/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Application/Events/Rejected/ChangePostStateRejected.cs index 8415fb2dc..b7db43226 100644 --- a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Application/Events/Rejected/ChangePostStateRejected.cs +++ b/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Application/Events/Rejected/ChangePostStateRejected.cs @@ -1,7 +1,9 @@ +using System.Diagnostics.CodeAnalysis; using Convey.CQRS.Events; namespace MiniSpace.Services.Posts.Application.Events.Rejected { + [ExcludeFromCodeCoverage] public class ChangePostStateRejected : IRejectedEvent { public Guid PostId { get; } diff --git a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Application/Events/Rejected/CreatePostRejected.cs b/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Application/Events/Rejected/CreatePostRejected.cs index 6bbd8a7eb..198e1f322 100644 --- a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Application/Events/Rejected/CreatePostRejected.cs +++ b/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Application/Events/Rejected/CreatePostRejected.cs @@ -1,7 +1,9 @@ +using System.Diagnostics.CodeAnalysis; using Convey.CQRS.Events; namespace MiniSpace.Services.Posts.Application.Events.Rejected { + [ExcludeFromCodeCoverage] public class CreatePostRejected : IRejectedEvent { public Guid PostId { get; } diff --git a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Application/Events/Rejected/DeletePostRejected.cs b/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Application/Events/Rejected/DeletePostRejected.cs index 93c3b9c60..b4e5879f7 100644 --- a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Application/Events/Rejected/DeletePostRejected.cs +++ b/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Application/Events/Rejected/DeletePostRejected.cs @@ -1,7 +1,9 @@ +using System.Diagnostics.CodeAnalysis; using Convey.CQRS.Events; namespace MiniSpace.Services.Posts.Application.Events.Rejected { + [ExcludeFromCodeCoverage] public class DeletePostRejected : IRejectedEvent { public Guid PostId { get; } diff --git a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Application/Events/Rejected/UpdatePostRejected.cs b/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Application/Events/Rejected/UpdatePostRejected.cs index f3638136e..883e2d6cb 100644 --- a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Application/Events/Rejected/UpdatePostRejected.cs +++ b/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Application/Events/Rejected/UpdatePostRejected.cs @@ -1,7 +1,9 @@ +using System.Diagnostics.CodeAnalysis; using Convey.CQRS.Events; namespace MiniSpace.Services.Posts.Application.Events.Rejected { + [ExcludeFromCodeCoverage] public class UpdatePostRejected : IRejectedEvent { public Guid PostId { get; } From 663ca7d2e67d261b5a51c98823bd1ff6e295ae93 Mon Sep 17 00:00:00 2001 From: olegkiprik Date: Sun, 26 May 2024 18:37:54 +0200 Subject: [PATCH 21/24] (#149) Add post state updater worker tests --- .../Workers/PostStateUpdaterWorker.cs | 2 +- .../Workers/PostStateUpdaterWorkerTest.cs | 95 +++++++++++++++++++ 2 files changed, 96 insertions(+), 1 deletion(-) create mode 100644 MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Infrastructure.UnitTests/Services/Workers/PostStateUpdaterWorkerTest.cs diff --git a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Infrastructure/Services/Workers/PostStateUpdaterWorker.cs b/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Infrastructure/Services/Workers/PostStateUpdaterWorker.cs index 9ec664240..f0d0b579e 100644 --- a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Infrastructure/Services/Workers/PostStateUpdaterWorker.cs +++ b/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Infrastructure/Services/Workers/PostStateUpdaterWorker.cs @@ -11,7 +11,7 @@ public class PostStateUpdaterWorker: BackgroundService private readonly IMessageBroker _messageBroker; private readonly ICommandDispatcher _commandDispatcher; private readonly IDateTimeProvider _dateTimeProvider; - private const int MinutesInterval = 5; + public const int MinutesInterval = 5; public PostStateUpdaterWorker(IMessageBroker messageBroker, ICommandDispatcher commandDispatcher, IDateTimeProvider dateTimeProvider) diff --git a/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Infrastructure.UnitTests/Services/Workers/PostStateUpdaterWorkerTest.cs b/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Infrastructure.UnitTests/Services/Workers/PostStateUpdaterWorkerTest.cs new file mode 100644 index 000000000..2258332b0 --- /dev/null +++ b/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Infrastructure.UnitTests/Services/Workers/PostStateUpdaterWorkerTest.cs @@ -0,0 +1,95 @@ +using Xunit; +using Moq; +using Convey.CQRS.Events; +using System; +using System.Collections.Generic; +using System.Threading.Tasks; +using Convey.MessageBrokers; +using Convey.MessageBrokers.Outbox; +using Convey.MessageBrokers.RabbitMQ; +using Microsoft.AspNetCore.Http; +using Microsoft.Extensions.Logging; +using OpenTracing; +using MiniSpace.Services.Posts.Infrastructure.Services; +using MiniSpace.Services.Posts.Application.Services; +using MiniSpace.Services.Posts.Application.Events; +using MiniSpace.Services.Posts.Infrastructure.Services.Workers; +using Convey.CQRS.Commands; +using Microsoft.VisualStudio.TestPlatform.Common.Utilities; +using App.Metrics.Timer; +using MiniSpace.Services.Posts.Application.Commands; + +namespace MiniSpace.Services.Posts.Infrastructure.UnitTests.Services.Workers +{ + public class PostStateUpdaterWorkerTest + { + private readonly PostStateUpdaterWorker _postStateUpdaterWorker; + private readonly Mock _messageBrokerMock; + private readonly Mock _commandDispatcherMock; + private readonly Mock _dateTimeProviderMock; + + public PostStateUpdaterWorkerTest() + { + _messageBrokerMock = new Mock(); + _commandDispatcherMock = new Mock(); + _dateTimeProviderMock = new Mock(); + _postStateUpdaterWorker = new PostStateUpdaterWorker(_messageBrokerMock.Object, + _commandDispatcherMock.Object, _dateTimeProviderMock.Object); + } + + [Fact] + public async Task ExecuteAsync_WithDefaultParameters_ShouldPublishStarted() + { + // Arrange + CancellationToken cancellationToken = new CancellationToken(); + + // Act + await _postStateUpdaterWorker.StartAsync(cancellationToken); + await Task.Delay(1000); + + // Assert + _messageBrokerMock.Verify(broker => + broker.PublishAsync(It.IsAny()), + Times.Once()); + } + + [Fact] + public async Task ExecuteAsync_WithCancelRequested_ShouldPublishStopped() + { + // Arrange + CancellationToken cancellationToken = new CancellationToken(); + + // Act + await _postStateUpdaterWorker.StartAsync(cancellationToken); + await Task.Delay(1000); + await _postStateUpdaterWorker.StopAsync(cancellationToken); + + // Assert + _messageBrokerMock.Verify(broker => + broker.PublishAsync(It.IsAny()), + Times.Once()); + } + + [Fact] + public async Task ExecuteAsync_WithTimeSetToInvokeCommandDispatcherSendAsync_ShouldCommandDispatcherSendAsync() + { + CancellationToken cancellationToken = new CancellationToken(); + var nowMock0 = new DateTime(2024, 5, 26, 18, PostStateUpdaterWorker.MinutesInterval * 1, 0); + var nowMock1 = new DateTime(2024, 5, 26, 18, PostStateUpdaterWorker.MinutesInterval * 2, 0); + + _dateTimeProviderMock.Setup(prov => prov.Now).Returns(nowMock0); + await _postStateUpdaterWorker.StartAsync(cancellationToken); + await Task.Delay(1000); + await _postStateUpdaterWorker.StopAsync(cancellationToken); + + _dateTimeProviderMock.Setup(prov => prov.Now).Returns(nowMock1); + await _postStateUpdaterWorker.StartAsync(cancellationToken); + await Task.Delay(1000); + await _postStateUpdaterWorker.StopAsync(cancellationToken); + + _commandDispatcherMock.Verify(broker => + broker.SendAsync(It.IsAny(), It.IsAny()), + Times.Exactly(2)); + } + } +} \ No newline at end of file From 3d235b627bdf3e5117df35e75b4576ae97454dfd Mon Sep 17 00:00:00 2001 From: olegkiprik Date: Sun, 26 May 2024 19:14:16 +0200 Subject: [PATCH 22/24] (#149) Add UpdatePostsStateHandlerTest --- .../Handlers/UpdatePostsStateHandlerTest.cs | 86 +++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Application.UnitTests/Commands/Handlers/UpdatePostsStateHandlerTest.cs diff --git a/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Application.UnitTests/Commands/Handlers/UpdatePostsStateHandlerTest.cs b/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Application.UnitTests/Commands/Handlers/UpdatePostsStateHandlerTest.cs new file mode 100644 index 000000000..891006e41 --- /dev/null +++ b/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Application.UnitTests/Commands/Handlers/UpdatePostsStateHandlerTest.cs @@ -0,0 +1,86 @@ +using Xunit; +using Moq; +using System; +using System.Collections.Generic; +using System.Threading.Tasks; +using System.Linq; +using System.Text; +using MiniSpace.Services.Posts.Application.Events; +using MiniSpace.Services.Posts.Application.Exceptions; +using MiniSpace.Services.Posts.Application.Services; +using MiniSpace.Services.Posts.Core.Entities; +using MiniSpace.Services.Posts.Core.Repositories; +using MiniSpace.Services.Posts.Application.Commands.Handlers; +using MiniSpace.Services.Posts.Application.Commands; +using MiniSpace.Services.Posts.Infrastructure.Contexts; +using Convey.CQRS.Commands; +using System.Threading; +using System.Security.Claims; +using FluentAssertions; + +namespace MiniSpace.Services.Posts.Application.UnitTests.Commands.Handlers { + public class UpdatePostsStateHandlerTest { + private readonly UpdatePostsStateHandler _updatePostStateHandler; + private readonly Mock _postRepositoryMock; + private readonly Mock _messageBrokerMock; + + public UpdatePostsStateHandlerTest() { + _postRepositoryMock = new(); + _messageBrokerMock = new(); + _updatePostStateHandler = new UpdatePostsStateHandler(_postRepositoryMock.Object, + _messageBrokerMock.Object + ); + } + + [Fact] + public async Task HandleAsync_WithValidParametersAndStatePublished_ShouldNotThrowException() { + // Arrange + var eventId = Guid.NewGuid(); + var contextId = Guid.NewGuid(); + var postId = Guid.NewGuid(); + var cancelationToken = new CancellationToken(); + var state = State.Published; + + var command = new UpdatePostsState(DateTime.Today); + + var post = Post.Create(new AggregateId(postId), eventId, contextId, + "Text", "Media content", DateTime.Today, + state, DateTime.Today); + + _postRepositoryMock.Setup(repo => repo.GetAsync(postId)).ReturnsAsync(post); + + // Act & Assert + Func act = async () => await _updatePostStateHandler.HandleAsync(command, cancelationToken); + await act.Should().NotThrowAsync(); + } + + [Fact] + public async Task HandleAsync_WithValidParametersAndStatePublished_ShouldUpdateRepository() { + // Arrange + var eventId = Guid.NewGuid(); + var contextId = Guid.NewGuid(); + var postId = Guid.NewGuid(); + var cancelationToken = new CancellationToken(); + var state = State.ToBePublished; + + var command = new UpdatePostsState(new DateTime(2024, 1, 1, 0, 0, 0)); + + var post = Post.Create(new AggregateId(postId), eventId, contextId, + "Text", "Media content", new DateTime(2000, 1, 1, 0, 0, 0), + state, new DateTime(2000, 1, 1, 0, 0, 0)); + + var postList = new List + { + post + }; + + _postRepositoryMock.Setup(repo => repo.GetToUpdateAsync()).ReturnsAsync(postList.AsEnumerable()); + _postRepositoryMock.Setup(repo => repo.GetAsync(postId)).ReturnsAsync(post); + + // Act & Assert + Func act = async () => await _updatePostStateHandler.HandleAsync(command, cancelationToken); + await act(); + _postRepositoryMock.Verify(repo => repo.UpdateAsync(post), Times.Once()); + } + } +} \ No newline at end of file From ae72fe14be619b87402e0e85f46f93f357b1ff65 Mon Sep 17 00:00:00 2001 From: olegkiprik Date: Sun, 26 May 2024 19:23:24 +0200 Subject: [PATCH 23/24] (#149) Add ChangePostStateHandler message broker test --- .../Handlers/ChangePostStateHandlerTest.cs | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Application.UnitTests/Commands/Handlers/ChangePostStateHandlerTest.cs b/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Application.UnitTests/Commands/Handlers/ChangePostStateHandlerTest.cs index 2d56a5abf..527b93781 100644 --- a/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Application.UnitTests/Commands/Handlers/ChangePostStateHandlerTest.cs +++ b/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Application.UnitTests/Commands/Handlers/ChangePostStateHandlerTest.cs @@ -41,6 +41,34 @@ public ChangePostStateHandlerTest() { ); } + [Fact] + public async Task HandleAsync_WithValidParametersAndStateToBePublished_ShouldPublishAsync() { + // Arrange + var contextId = Guid.NewGuid(); + var postId = Guid.NewGuid(); + var cancelationToken = new CancellationToken(); + var state = State.ToBePublished; + var eventId = Guid.NewGuid(); + + var command = new ChangePostState(postId, + state.GetDisplayName(), DateTime.Today); + + var identityContext = new IdentityContext(contextId.ToString(), "", true, default); + _appContextMock.Setup(ctx => ctx.Identity).Returns(identityContext); + + var post = Post.Create(new AggregateId(postId), eventId, contextId, "Text", "Media content", DateTime.Today, + state, DateTime.Today); + + _postRepositoryMock.Setup(repo => repo.GetAsync(command.PostId)).ReturnsAsync(post); + + // Act + Func act = async () => await _changePostStateHandler.HandleAsync(command, cancelationToken); + await act(); + + // Assert + _messageBrokerMock.Verify(broker => broker.PublishAsync(It.IsAny()), Times.Once()); + } + [Fact] public async Task HandleAsync_WithValidParametersAndStateToBePublished_ShouldNotThrowException() { // Arrange From 4b8ca3c38d1932b8f6098cb4fb9f99b9aed2f5c8 Mon Sep 17 00:00:00 2001 From: olegkiprik Date: Sun, 26 May 2024 22:40:46 +0200 Subject: [PATCH 24/24] (#149) Update tests --- .../MiniSpace.Services.Posts.Api/Program.cs | 9 +- .../appsettings.docker.json | 4 +- .../appsettings.local.json | 6 +- .../Commands/CreatePost.cs | 8 +- .../Commands/Handlers/CreatePostHandler.cs | 15 +-- .../Commands/Handlers/UpdatePostHandler.cs | 10 +- .../Commands/SearchPosts.cs | 11 --- .../Commands/UpdatePost.cs | 6 +- .../Dto/PageableDto.cs | 9 -- .../Dto/PostDto.cs | 22 +---- .../Dto/SortDto.cs | 8 -- .../Dto/StudentEventsDto.cs | 9 -- .../Handlers/MediaFileDeletedHandler.cs | 33 ------- .../Events/External/MediaFileDeleted.cs | 20 ---- .../Events/PostCreated.cs | 4 +- .../Events/PostUpdated.cs | 4 +- .../Rejected/ChangePostStateRejected.cs | 2 + .../Events/Rejected/CreatePostRejected.cs | 2 + .../Events/Rejected/DeletePostRejected.cs | 2 + .../Events/Rejected/UpdatePostRejected.cs | 2 + .../InvalidNumberOfPostMediaFilesException.cs | 16 ---- .../Exceptions/InvalidPostIdException.cs | 12 --- .../UnauthorizedPostSearchException.cs | 15 --- .../Extensions.cs | 2 + .../Queries/GetOrganizerPosts.cs | 2 + .../Queries/GetPost.cs | 2 + .../Queries/GetPosts.cs | 2 + .../Clients/IStudentsServiceClient.cs | 9 -- .../Services/IPostsService.cs | 11 --- .../Entities/Post.cs | 28 ++---- ...idStudentServiceClientResponseException.cs | 14 --- .../Exceptions/MediaFileNotFoundException.cs | 16 ---- .../Repositories/IPostRepository.cs | 4 - .../Wrappers/PagedResponse.cs | 28 ------ .../Wrappers/Response.cs | 10 -- .../Contexts/AppContext.cs | 2 + .../Contexts/AppContextFactory.cs | 2 + .../Contexts/CorrelationContext.cs | 3 + .../Contexts/IdentityContext.cs | 1 + .../OutboxCommandHandlerDecorator.cs | 2 + .../Decorators/OutboxEventHandlerDecorator.cs | 2 + .../Exceptions/ExceptionToMessageMapper.cs | 2 + .../Exceptions/ExceptionToResponseMapper.cs | 2 + .../Extensions.cs | 6 +- .../Logging/Extensions.cs | 2 + .../Logging/MessageToLogTemplateMapper.cs | 2 + .../Mongo/Documents/EventDocument.cs | 2 + .../Mongo/Documents/Extensions.cs | 8 +- .../Mongo/Documents/PostDocument.cs | 4 +- .../Handlers/GetOrganizerPostsHandler.cs | 4 +- .../Mongo/Queries/Handlers/GetPostHandler.cs | 2 + .../Mongo/Queries/Handlers/GetPostsHandler.cs | 2 + .../Repositories/EventMongoRepository.cs | 2 + .../Mongo/Repositories/Extensions.cs | 83 ---------------- .../Mongo/Repositories/PostMongoRepository.cs | 31 +----- .../Services/Clients/StudentsServiceClient.cs | 24 ----- .../Services/PostsService.cs | 58 ----------- .../Workers/PostStateUpdaterWorker.cs | 2 +- .../Handlers/ChangePostStateHandlerTest.cs | 28 ++++++ .../Handlers/UpdatePostsStateHandlerTest.cs | 86 +++++++++++++++++ .../Handlers/EventDeletedHandlerTest.cs | 36 ------- ...ervices.Posts.Application.UnitTests.csproj | 1 - .../Entities/AggregatedIdTest.cs | 5 + .../Entities/PostTest.cs | 82 ++++++++-------- ...Space.Services.Posts.Core.UnitTests.csproj | 1 - ...ices.Posts.Infrastructure.UnitTests.csproj | 1 - .../Workers/PostStateUpdaterWorkerTest.cs | 95 +++++++++++++++++++ 67 files changed, 349 insertions(+), 591 deletions(-) delete mode 100644 MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Application/Commands/SearchPosts.cs delete mode 100644 MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Application/Dto/PageableDto.cs delete mode 100644 MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Application/Dto/SortDto.cs delete mode 100644 MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Application/Dto/StudentEventsDto.cs delete mode 100644 MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Application/Events/External/Handlers/MediaFileDeletedHandler.cs delete mode 100644 MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Application/Events/External/MediaFileDeleted.cs delete mode 100644 MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Application/Exceptions/InvalidNumberOfPostMediaFilesException.cs delete mode 100644 MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Application/Exceptions/InvalidPostIdException.cs delete mode 100644 MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Application/Exceptions/UnauthorizedPostSearchException.cs delete mode 100644 MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Application/Services/Clients/IStudentsServiceClient.cs delete mode 100644 MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Application/Services/IPostsService.cs delete mode 100644 MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Core/Exceptions/InvalidStudentServiceClientResponseException.cs delete mode 100644 MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Core/Exceptions/MediaFileNotFoundException.cs delete mode 100644 MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Core/Wrappers/PagedResponse.cs delete mode 100644 MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Core/Wrappers/Response.cs delete mode 100644 MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Infrastructure/Mongo/Repositories/Extensions.cs delete mode 100644 MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Infrastructure/Services/Clients/StudentsServiceClient.cs delete mode 100644 MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Infrastructure/Services/PostsService.cs create mode 100644 MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Application.UnitTests/Commands/Handlers/UpdatePostsStateHandlerTest.cs create mode 100644 MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Infrastructure.UnitTests/Services/Workers/PostStateUpdaterWorkerTest.cs diff --git a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Api/Program.cs b/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Api/Program.cs index 8e78a520c..cc5bf0b76 100644 --- a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Api/Program.cs +++ b/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Api/Program.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; using System.Threading.Tasks; using Convey; using Convey.Logging; @@ -14,11 +15,11 @@ using MiniSpace.Services.Posts.Application.Commands; using MiniSpace.Services.Posts.Application.Dto; using MiniSpace.Services.Posts.Application.Queries; -using MiniSpace.Services.Posts.Application.Services; using MiniSpace.Services.Posts.Infrastructure; namespace MiniSpace.Services.Posts.Api { + [ExcludeFromCodeCoverage] public class Program { public static async Task Main(string[] args) @@ -31,12 +32,6 @@ public static async Task Main(string[] args) .Build()) .Configure(app => app .UseInfrastructure() - .UseEndpoints(endpoints => endpoints - .Post("posts/search", async (cmd, ctx) => - { - var pagedResult = await ctx.RequestServices.GetService().BrowsePostsAsync(cmd); - await ctx.Response.WriteJsonAsync(pagedResult); - })) .UseDispatcherEndpoints(endpoints => endpoints .Get("", ctx => ctx.Response.WriteAsync(ctx.RequestServices.GetService().Name)) .Get("posts/{postId}") diff --git a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Api/appsettings.docker.json b/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Api/appsettings.docker.json index 18539c4d2..07c6d2bc2 100644 --- a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Api/appsettings.docker.json +++ b/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Api/appsettings.docker.json @@ -22,9 +22,7 @@ "httpClient": { "type": "fabio", "retries": 3, - "services": { - "students": "students-service" - } + "services": {} }, "jwt": { "certificate": { diff --git a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Api/appsettings.local.json b/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Api/appsettings.local.json index 535b691a9..29f59a9ca 100644 --- a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Api/appsettings.local.json +++ b/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Api/appsettings.local.json @@ -21,11 +21,9 @@ "service": "posts-service" }, "httpClient": { - "type": "direct", + "type": "fabio", "retries": 3, - "services": { - "students": "http://localhost:5007" - }, + "services": {}, "requestMasking": { "enabled": true, "maskTemplate": "*****" diff --git a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Application/Commands/CreatePost.cs b/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Application/Commands/CreatePost.cs index fdb630f37..0291c545f 100644 --- a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Application/Commands/CreatePost.cs +++ b/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Application/Commands/CreatePost.cs @@ -8,18 +8,18 @@ public class CreatePost : ICommand public Guid EventId { get; } public Guid OrganizerId { get; } public string TextContent { get; } - public IEnumerable MediaFiles { get; } + public string MediaContent { get; } public string State { get; } public DateTime? PublishDate { get; } public CreatePost(Guid postId, Guid eventId, Guid organizerId, string textContent, - IEnumerable mediaFiles, string state, DateTime? publishDate) + string mediaContent, string state, DateTime? publishDate) { - PostId = postId; + PostId = postId == Guid.Empty ? Guid.NewGuid() : postId; EventId = eventId; OrganizerId = organizerId; TextContent = textContent; - MediaFiles = mediaFiles; + MediaContent = mediaContent; State = state; PublishDate = publishDate; } diff --git a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Application/Commands/Handlers/CreatePostHandler.cs b/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Application/Commands/Handlers/CreatePostHandler.cs index 44811ab9b..132d00a84 100644 --- a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Application/Commands/Handlers/CreatePostHandler.cs +++ b/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Application/Commands/Handlers/CreatePostHandler.cs @@ -39,22 +39,11 @@ public async Task HandleAsync(CreatePost command, CancellationToken cancellation { throw new UnauthorizedPostCreationAttemptException(identity.Id, command.EventId); } - - if(command.PostId == Guid.Empty || await _postRepository.ExistsAsync(command.PostId)) - { - throw new InvalidPostIdException(command.PostId); - } if (!Enum.TryParse(command.State, true, out var newState)) { throw new InvalidPostStateException(command.State); } - - var mediaFiles = command.MediaFiles.ToList(); - if(mediaFiles.Count > 3) - { - throw new InvalidNumberOfPostMediaFilesException(command.PostId, mediaFiles.Count); - } switch (newState) { @@ -65,10 +54,10 @@ public async Task HandleAsync(CreatePost command, CancellationToken cancellation } var post = Post.Create(command.PostId, command.EventId, command.OrganizerId, command.TextContent, - command.MediaFiles, _dateTimeProvider.Now, newState, command.PublishDate); + command.MediaContent, _dateTimeProvider.Now, newState, command.PublishDate); await _postRepository.AddAsync(post); - await _messageBroker.PublishAsync(new PostCreated(command.PostId, post.MediaFiles)); + await _messageBroker.PublishAsync(new PostCreated(command.PostId)); } } } diff --git a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Application/Commands/Handlers/UpdatePostHandler.cs b/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Application/Commands/Handlers/UpdatePostHandler.cs index ee725ab9e..f14af1fab 100644 --- a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Application/Commands/Handlers/UpdatePostHandler.cs +++ b/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Application/Commands/Handlers/UpdatePostHandler.cs @@ -42,16 +42,10 @@ public async Task HandleAsync(UpdatePost command, CancellationToken cancellation throw new UnauthorizedPostOperationException(command.PostId, identity.Id); } - var mediaFiles = command.MediaFiles.ToList(); - if(mediaFiles.Count > 3) - { - throw new InvalidNumberOfPostMediaFilesException(post.Id, mediaFiles.Count); - } - - post.Update(command.TextContent, command.MediaFiles, _dateTimeProvider.Now); + post.Update(command.TextContent, command.MediaContent, _dateTimeProvider.Now); await _postRepository.UpdateAsync(post); - await _messageBroker.PublishAsync(new PostUpdated(command.PostId, post.MediaFiles)); + await _messageBroker.PublishAsync(new PostUpdated(command.PostId)); } } } diff --git a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Application/Commands/SearchPosts.cs b/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Application/Commands/SearchPosts.cs deleted file mode 100644 index e6776a5a0..000000000 --- a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Application/Commands/SearchPosts.cs +++ /dev/null @@ -1,11 +0,0 @@ -using Convey.CQRS.Commands; -using MiniSpace.Services.Posts.Application.Dto; - -namespace MiniSpace.Services.Posts.Application.Commands -{ - public class SearchPosts : ICommand - { - public Guid StudentId { get; set; } - public PageableDto Pageable { get; set; } - } -} \ No newline at end of file diff --git a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Application/Commands/UpdatePost.cs b/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Application/Commands/UpdatePost.cs index e180055a3..00f6d6f40 100644 --- a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Application/Commands/UpdatePost.cs +++ b/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Application/Commands/UpdatePost.cs @@ -6,13 +6,13 @@ public class UpdatePost : ICommand { public Guid PostId { get; } public string TextContent { get; } - public IEnumerable MediaFiles { get; } + public string MediaContent { get; } - public UpdatePost(Guid postId, string textContent, IEnumerable mediaFiles) + public UpdatePost(Guid postId, string textContent, string mediaContent) { PostId = postId; TextContent = textContent; - MediaFiles = mediaFiles; + MediaContent = mediaContent; } } } diff --git a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Application/Dto/PageableDto.cs b/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Application/Dto/PageableDto.cs deleted file mode 100644 index 9d6928419..000000000 --- a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Application/Dto/PageableDto.cs +++ /dev/null @@ -1,9 +0,0 @@ -namespace MiniSpace.Services.Posts.Application.Dto -{ - public class PageableDto - { - public int Page { get; set; } - public int Size { get; set; } - public SortDto Sort { get; set; } - } -} \ No newline at end of file diff --git a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Application/Dto/PostDto.cs b/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Application/Dto/PostDto.cs index 8545cc0b6..d99a1238e 100644 --- a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Application/Dto/PostDto.cs +++ b/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Application/Dto/PostDto.cs @@ -1,34 +1,18 @@ -using MiniSpace.Services.Posts.Core.Entities; +using System.Diagnostics.CodeAnalysis; namespace MiniSpace.Services.Posts.Application.Dto { + [ExcludeFromCodeCoverage] public class PostDto { public Guid Id { get; set; } public Guid EventId { get; set; } public Guid OrganizerId { get; set; } public string TextContent { get; set; } - public IEnumerable MediaFiles { get; set; } + public string MediaContent { get; set; } public string State { get; set; } public DateTime? PublishDate { get; set; } public DateTime CreatedAt { get; set; } public DateTime? UpdatedAt { get; set; } - - public PostDto() - { - } - - public PostDto(Post post) - { - Id = post.Id; - EventId = post.EventId; - OrganizerId = post.OrganizerId; - TextContent = post.TextContent; - MediaFiles = post.MediaFiles; - State = post.State.ToString(); - PublishDate = post.PublishDate; - CreatedAt = post.CreatedAt; - UpdatedAt = post.UpdatedAt; - } } } diff --git a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Application/Dto/SortDto.cs b/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Application/Dto/SortDto.cs deleted file mode 100644 index 0fa08bb89..000000000 --- a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Application/Dto/SortDto.cs +++ /dev/null @@ -1,8 +0,0 @@ -namespace MiniSpace.Services.Posts.Application.Dto -{ - public class SortDto - { - public IEnumerable SortBy { get; set; } - public string Direction { get; set; } - } -} \ No newline at end of file diff --git a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Application/Dto/StudentEventsDto.cs b/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Application/Dto/StudentEventsDto.cs deleted file mode 100644 index 2f27c2fa3..000000000 --- a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Application/Dto/StudentEventsDto.cs +++ /dev/null @@ -1,9 +0,0 @@ -namespace MiniSpace.Services.Posts.Application.Dto -{ - public class StudentEventsDto - { - public Guid StudentId { get; set; } - public IEnumerable InterestedInEvents { get; set; } - public IEnumerable SignedUpEvents { get; set; } - } -} \ No newline at end of file diff --git a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Application/Events/External/Handlers/MediaFileDeletedHandler.cs b/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Application/Events/External/Handlers/MediaFileDeletedHandler.cs deleted file mode 100644 index 971421190..000000000 --- a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Application/Events/External/Handlers/MediaFileDeletedHandler.cs +++ /dev/null @@ -1,33 +0,0 @@ -using Convey.CQRS.Events; -using MiniSpace.Services.Posts.Application.Services; -using MiniSpace.Services.Posts.Core.Repositories; - -namespace MiniSpace.Services.Posts.Application.Events.External.Handlers -{ - public class MediaFileDeletedHandler: IEventHandler - { - private readonly IPostRepository _postRepository; - private readonly IDateTimeProvider _dateTimeProvider; - - public MediaFileDeletedHandler(IPostRepository postRepository, IDateTimeProvider dateTimeProvider) - { - _postRepository = postRepository; - _dateTimeProvider = dateTimeProvider; - } - - public async Task HandleAsync(MediaFileDeleted @event, CancellationToken cancellationToken) - { - if(@event.Source.ToLowerInvariant() != "post") - { - return; - } - - var post = await _postRepository.GetAsync(@event.SourceId); - if(post != null) - { - post.RemoveMediaFile(@event.MediaFileId, _dateTimeProvider.Now); - await _postRepository.UpdateAsync(post); - } - } - } -} \ No newline at end of file diff --git a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Application/Events/External/MediaFileDeleted.cs b/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Application/Events/External/MediaFileDeleted.cs deleted file mode 100644 index f79ca8647..000000000 --- a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Application/Events/External/MediaFileDeleted.cs +++ /dev/null @@ -1,20 +0,0 @@ -using Convey.CQRS.Events; -using Convey.MessageBrokers; - -namespace MiniSpace.Services.Posts.Application.Events.External -{ - [Message("mediafiles")] - public class MediaFileDeleted: IEvent - { - public Guid MediaFileId { get; } - public Guid SourceId { get; } - public string Source { get; } - - public MediaFileDeleted(Guid mediaFileId, Guid sourceId, string source) - { - MediaFileId = mediaFileId; - SourceId = sourceId; - Source = source; - } - } -} \ No newline at end of file diff --git a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Application/Events/PostCreated.cs b/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Application/Events/PostCreated.cs index bf44c36ed..6675de454 100644 --- a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Application/Events/PostCreated.cs +++ b/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Application/Events/PostCreated.cs @@ -5,12 +5,10 @@ namespace MiniSpace.Services.Posts.Application.Events public class PostCreated : IEvent { public Guid PostId { get; } - public IEnumerable MediaFilesIds { get; } - public PostCreated(Guid postId, IEnumerable mediaFilesIds) + public PostCreated(Guid postId) { PostId = postId; - MediaFilesIds = mediaFilesIds; } } } diff --git a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Application/Events/PostUpdated.cs b/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Application/Events/PostUpdated.cs index e1a6a3d95..f6471e54f 100644 --- a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Application/Events/PostUpdated.cs +++ b/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Application/Events/PostUpdated.cs @@ -5,12 +5,10 @@ namespace MiniSpace.Services.Posts.Application.Events public class PostUpdated : IEvent { public Guid PostId { get; } - public IEnumerable MediaFilesIds { get; } - public PostUpdated(Guid postId, IEnumerable mediaFilesIds) + public PostUpdated(Guid postId) { PostId = postId; - MediaFilesIds = mediaFilesIds; } } } diff --git a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Application/Events/Rejected/ChangePostStateRejected.cs b/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Application/Events/Rejected/ChangePostStateRejected.cs index 8415fb2dc..b7db43226 100644 --- a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Application/Events/Rejected/ChangePostStateRejected.cs +++ b/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Application/Events/Rejected/ChangePostStateRejected.cs @@ -1,7 +1,9 @@ +using System.Diagnostics.CodeAnalysis; using Convey.CQRS.Events; namespace MiniSpace.Services.Posts.Application.Events.Rejected { + [ExcludeFromCodeCoverage] public class ChangePostStateRejected : IRejectedEvent { public Guid PostId { get; } diff --git a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Application/Events/Rejected/CreatePostRejected.cs b/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Application/Events/Rejected/CreatePostRejected.cs index 6bbd8a7eb..198e1f322 100644 --- a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Application/Events/Rejected/CreatePostRejected.cs +++ b/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Application/Events/Rejected/CreatePostRejected.cs @@ -1,7 +1,9 @@ +using System.Diagnostics.CodeAnalysis; using Convey.CQRS.Events; namespace MiniSpace.Services.Posts.Application.Events.Rejected { + [ExcludeFromCodeCoverage] public class CreatePostRejected : IRejectedEvent { public Guid PostId { get; } diff --git a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Application/Events/Rejected/DeletePostRejected.cs b/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Application/Events/Rejected/DeletePostRejected.cs index 93c3b9c60..b4e5879f7 100644 --- a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Application/Events/Rejected/DeletePostRejected.cs +++ b/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Application/Events/Rejected/DeletePostRejected.cs @@ -1,7 +1,9 @@ +using System.Diagnostics.CodeAnalysis; using Convey.CQRS.Events; namespace MiniSpace.Services.Posts.Application.Events.Rejected { + [ExcludeFromCodeCoverage] public class DeletePostRejected : IRejectedEvent { public Guid PostId { get; } diff --git a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Application/Events/Rejected/UpdatePostRejected.cs b/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Application/Events/Rejected/UpdatePostRejected.cs index f3638136e..883e2d6cb 100644 --- a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Application/Events/Rejected/UpdatePostRejected.cs +++ b/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Application/Events/Rejected/UpdatePostRejected.cs @@ -1,7 +1,9 @@ +using System.Diagnostics.CodeAnalysis; using Convey.CQRS.Events; namespace MiniSpace.Services.Posts.Application.Events.Rejected { + [ExcludeFromCodeCoverage] public class UpdatePostRejected : IRejectedEvent { public Guid PostId { get; } diff --git a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Application/Exceptions/InvalidNumberOfPostMediaFilesException.cs b/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Application/Exceptions/InvalidNumberOfPostMediaFilesException.cs deleted file mode 100644 index 481fd5d4e..000000000 --- a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Application/Exceptions/InvalidNumberOfPostMediaFilesException.cs +++ /dev/null @@ -1,16 +0,0 @@ -namespace MiniSpace.Services.Posts.Application.Exceptions -{ - public class InvalidNumberOfPostMediaFilesException : AppException - { - public override string Code { get; } = "invalid_number_of_post_media_files"; - public Guid PostId { get; } - public int MediaSizeNumber { get; } - - public InvalidNumberOfPostMediaFilesException(Guid postId, int mediaFilesNumber) - : base($"Invalid media files number: {mediaFilesNumber} for post with ID: '{postId}'. It should be less or equal 3.") - { - PostId = postId; - MediaSizeNumber = mediaFilesNumber; - } - } -} \ No newline at end of file diff --git a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Application/Exceptions/InvalidPostIdException.cs b/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Application/Exceptions/InvalidPostIdException.cs deleted file mode 100644 index 13b2d64b0..000000000 --- a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Application/Exceptions/InvalidPostIdException.cs +++ /dev/null @@ -1,12 +0,0 @@ -namespace MiniSpace.Services.Posts.Application.Exceptions -{ - public class InvalidPostIdException : AppException - { - public Guid PostId { get; } - - public InvalidPostIdException(Guid postId) : base($"Invalid post id: {postId}") - { - PostId = postId; - } - } -} \ No newline at end of file diff --git a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Application/Exceptions/UnauthorizedPostSearchException.cs b/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Application/Exceptions/UnauthorizedPostSearchException.cs deleted file mode 100644 index 49464f74c..000000000 --- a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Application/Exceptions/UnauthorizedPostSearchException.cs +++ /dev/null @@ -1,15 +0,0 @@ -namespace MiniSpace.Services.Posts.Application.Exceptions -{ - public class UnauthorizedPostSearchException : AppException - { - public override string Code { get; } = "unauthorized_post_search"; - public Guid StudentId { get; } - public Guid IdentityId { get; } - - public UnauthorizedPostSearchException(Guid studentId, Guid identityId) : base("Unauthorized post search.") - { - StudentId = studentId; - IdentityId = identityId; - } - } -} \ No newline at end of file diff --git a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Application/Extensions.cs b/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Application/Extensions.cs index bca122302..e75cdd897 100644 --- a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Application/Extensions.cs +++ b/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Application/Extensions.cs @@ -1,9 +1,11 @@ +using System.Diagnostics.CodeAnalysis; using Convey; using Convey.CQRS.Commands; using Convey.CQRS.Events; namespace MiniSpace.Services.Posts.Application { + [ExcludeFromCodeCoverage] public static class Extensions { public static IConveyBuilder AddApplication(this IConveyBuilder builder) diff --git a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Application/Queries/GetOrganizerPosts.cs b/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Application/Queries/GetOrganizerPosts.cs index 480b6361c..468acc48d 100644 --- a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Application/Queries/GetOrganizerPosts.cs +++ b/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Application/Queries/GetOrganizerPosts.cs @@ -1,9 +1,11 @@ using System.Collections; +using System.Diagnostics.CodeAnalysis; using Convey.CQRS.Queries; using MiniSpace.Services.Posts.Application.Dto; namespace MiniSpace.Services.Posts.Application.Queries { + [ExcludeFromCodeCoverage] public class GetOrganizerPosts : IQuery> { public Guid OrganizerId { get; set; } diff --git a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Application/Queries/GetPost.cs b/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Application/Queries/GetPost.cs index 3524d49c6..fde0324d4 100644 --- a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Application/Queries/GetPost.cs +++ b/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Application/Queries/GetPost.cs @@ -1,8 +1,10 @@ +using System.Diagnostics.CodeAnalysis; using Convey.CQRS.Queries; using MiniSpace.Services.Posts.Application.Dto; namespace MiniSpace.Services.Posts.Application.Queries { + [ExcludeFromCodeCoverage] public class GetPost : IQuery { public Guid PostId { get; set; } diff --git a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Application/Queries/GetPosts.cs b/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Application/Queries/GetPosts.cs index 1efeae6f9..a06a7d95f 100644 --- a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Application/Queries/GetPosts.cs +++ b/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Application/Queries/GetPosts.cs @@ -1,8 +1,10 @@ +using System.Diagnostics.CodeAnalysis; using Convey.CQRS.Queries; using MiniSpace.Services.Posts.Application.Dto; namespace MiniSpace.Services.Posts.Application.Queries { + [ExcludeFromCodeCoverage] public class GetPosts : IQuery> { public Guid EventId { get; set; } diff --git a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Application/Services/Clients/IStudentsServiceClient.cs b/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Application/Services/Clients/IStudentsServiceClient.cs deleted file mode 100644 index 849818345..000000000 --- a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Application/Services/Clients/IStudentsServiceClient.cs +++ /dev/null @@ -1,9 +0,0 @@ -using MiniSpace.Services.Posts.Application.Dto; - -namespace MiniSpace.Services.Posts.Application.Services.Clients -{ - public interface IStudentsServiceClient - { - Task GetAsync(Guid id); - } -} \ No newline at end of file diff --git a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Application/Services/IPostsService.cs b/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Application/Services/IPostsService.cs deleted file mode 100644 index 6c2dc71ce..000000000 --- a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Application/Services/IPostsService.cs +++ /dev/null @@ -1,11 +0,0 @@ -using MiniSpace.Services.Posts.Application.Commands; -using MiniSpace.Services.Posts.Application.Dto; -using MiniSpace.Services.Posts.Core.Wrappers; - -namespace MiniSpace.Services.Posts.Application.Services -{ - public interface IPostsService - { - Task>> BrowsePostsAsync(SearchPosts command); - } -} \ No newline at end of file diff --git a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Core/Entities/Post.cs b/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Core/Entities/Post.cs index ced44e44f..d97c788c3 100644 --- a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Core/Entities/Post.cs +++ b/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Core/Entities/Post.cs @@ -7,20 +7,20 @@ public class Post : AggregateRoot public Guid EventId { get; private set; } public Guid OrganizerId { get; private set; } public string TextContent { get; private set; } - public IEnumerable MediaFiles { get; private set; } + public string MediaContent { get; private set; } public State State { get; private set; } public DateTime? PublishDate { get; private set; } public DateTime CreatedAt { get; private set; } public DateTime? UpdatedAt { get; private set; } - public Post(Guid id, Guid eventId, Guid organizerId, string textContent, IEnumerable mediaFiles, - DateTime createdAt, State state, DateTime? publishDate, DateTime? updatedAt = null) + public Post(Guid id, Guid eventId, Guid organizerId, string textContent, + string mediaContent, DateTime createdAt, State state, DateTime? publishDate, DateTime? updatedAt = null) { Id = id; EventId = eventId; OrganizerId = organizerId; TextContent = textContent; - MediaFiles = mediaFiles; + MediaContent = mediaContent; CreatedAt = createdAt; UpdatedAt = updatedAt; State = state; @@ -38,7 +38,7 @@ public void SetToBePublished(DateTime publishDate, DateTime now) public void SetPublished(DateTime now) { State = State.Published; - PublishDate = now; + PublishDate = null; UpdatedAt = now; } @@ -68,32 +68,22 @@ public bool UpdateState(DateTime now) } public static Post Create(AggregateId id, Guid eventId, Guid studentId, string textContent, - IEnumerable mediaFiles, DateTime createdAt, State state, DateTime? publishDate) + string mediaContent, DateTime createdAt, State state, DateTime? publishDate) { CheckTextContent(id, textContent); - return new Post(id, eventId, studentId, textContent, mediaFiles, createdAt, state, - publishDate ?? createdAt); + return new Post(id, eventId, studentId, textContent, mediaContent, createdAt, state, publishDate); } - public void Update(string textContent, IEnumerable mediaFiles, DateTime now) + public void Update(string textContent, string mediaContent, DateTime now) { CheckTextContent(Id, textContent); TextContent = textContent; - MediaFiles = mediaFiles; + MediaContent = mediaContent; UpdatedAt = now; } - public void RemoveMediaFile(Guid mediaFileId, DateTime now) - { - var mediaFile = MediaFiles.SingleOrDefault(mf => mf == mediaFileId); - if (mediaFile == Guid.Empty) - { - throw new MediaFileNotFoundException(mediaFileId, Id); - } - } - private static void CheckTextContent(AggregateId id, string textContent) { if (string.IsNullOrWhiteSpace(textContent) || textContent.Length > 5000) diff --git a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Core/Exceptions/InvalidStudentServiceClientResponseException.cs b/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Core/Exceptions/InvalidStudentServiceClientResponseException.cs deleted file mode 100644 index c76e981b4..000000000 --- a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Core/Exceptions/InvalidStudentServiceClientResponseException.cs +++ /dev/null @@ -1,14 +0,0 @@ -namespace MiniSpace.Services.Posts.Core.Exceptions -{ - public class InvalidStudentServiceClientResponseException : DomainException - { - public override string Code { get; } = "invalid_student_service_client_response"; - public Guid StudentId { get; } - - public InvalidStudentServiceClientResponseException(Guid studentId) - : base($"Invalid student service client response for student with ID: '{studentId}'.") - { - StudentId = studentId; - } - } -} diff --git a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Core/Exceptions/MediaFileNotFoundException.cs b/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Core/Exceptions/MediaFileNotFoundException.cs deleted file mode 100644 index 4a68fdb63..000000000 --- a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Core/Exceptions/MediaFileNotFoundException.cs +++ /dev/null @@ -1,16 +0,0 @@ -namespace MiniSpace.Services.Posts.Core.Exceptions -{ - public class MediaFileNotFoundException : DomainException - { - public override string Code { get; } = "media_file_not_found"; - public Guid MediaFileId { get; } - public Guid PostId { get; } - - public MediaFileNotFoundException(Guid mediaFileId, Guid postId) - : base($"Media file with ID: '{mediaFileId}' for post with ID: {postId} was not found.") - { - MediaFileId = mediaFileId; - PostId = postId; - } - } -} \ No newline at end of file diff --git a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Core/Repositories/IPostRepository.cs b/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Core/Repositories/IPostRepository.cs index f2c68e399..cb9a727d7 100644 --- a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Core/Repositories/IPostRepository.cs +++ b/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Core/Repositories/IPostRepository.cs @@ -1,5 +1,4 @@ using MiniSpace.Services.Posts.Core.Entities; -using MiniSpace.Services.Posts.Core.Wrappers; namespace MiniSpace.Services.Posts.Core.Repositories { @@ -11,8 +10,5 @@ public interface IPostRepository Task AddAsync(Post post); Task UpdateAsync(Post post); Task DeleteAsync(Guid id); - Task ExistsAsync(Guid id); - Task<(IEnumerable posts, int pageNumber,int pageSize, int totalPages, int totalElements)> BrowseCommentsAsync(int pageNumber, int pageSize, - IEnumerable eventsIds, IEnumerable sortBy, string direction); } } diff --git a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Core/Wrappers/PagedResponse.cs b/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Core/Wrappers/PagedResponse.cs deleted file mode 100644 index bc82a2d4a..000000000 --- a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Core/Wrappers/PagedResponse.cs +++ /dev/null @@ -1,28 +0,0 @@ -namespace MiniSpace.Services.Posts.Core.Wrappers -{ - public class PagedResponse : Response - { - public int TotalPages { get; } - public int TotalElements { get; } - public int Size { get; } - public int Number { get; } - public bool First { get; } - public bool Last { get; } - public bool Empty { get; } - - public PagedResponse(T content, int pageNumber, int pageSize, int totalPages, int totalElements) - { - Content = content; - TotalPages = totalPages; - TotalElements = totalElements; - Size = pageSize; - Number = pageNumber; - First = pageNumber == 0; - Last = pageNumber == totalPages - 1; - Empty = totalElements == 0; - Succeeded = true; - Errors = null; - Message = null; - } - } -} \ No newline at end of file diff --git a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Core/Wrappers/Response.cs b/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Core/Wrappers/Response.cs deleted file mode 100644 index 2de1fbc49..000000000 --- a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Core/Wrappers/Response.cs +++ /dev/null @@ -1,10 +0,0 @@ -namespace MiniSpace.Services.Posts.Core.Wrappers -{ - public class Response - { - public T Content { get; set; } - public bool Succeeded { get; set; } - public string[] Errors { get; set; } - public string Message { get; set; } - } -} \ No newline at end of file diff --git a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Infrastructure/Contexts/AppContext.cs b/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Infrastructure/Contexts/AppContext.cs index 62d23d593..958479ae4 100644 --- a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Infrastructure/Contexts/AppContext.cs +++ b/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Infrastructure/Contexts/AppContext.cs @@ -1,7 +1,9 @@ +using System.Diagnostics.CodeAnalysis; using MiniSpace.Services.Posts.Application; namespace MiniSpace.Services.Posts.Infrastructure.Contexts { + [ExcludeFromCodeCoverage] internal class AppContext : IAppContext { public string RequestId { get; } diff --git a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Infrastructure/Contexts/AppContextFactory.cs b/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Infrastructure/Contexts/AppContextFactory.cs index ac0ff8648..65b8aed07 100644 --- a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Infrastructure/Contexts/AppContextFactory.cs +++ b/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Infrastructure/Contexts/AppContextFactory.cs @@ -2,9 +2,11 @@ using Microsoft.AspNetCore.Http; using Newtonsoft.Json; using MiniSpace.Services.Posts.Application; +using System.Diagnostics.CodeAnalysis; namespace MiniSpace.Services.Posts.Infrastructure.Contexts { + [ExcludeFromCodeCoverage] internal sealed class AppContextFactory : IAppContextFactory { private readonly ICorrelationContextAccessor _contextAccessor; diff --git a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Infrastructure/Contexts/CorrelationContext.cs b/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Infrastructure/Contexts/CorrelationContext.cs index 191f9e53d..23753504a 100644 --- a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Infrastructure/Contexts/CorrelationContext.cs +++ b/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Infrastructure/Contexts/CorrelationContext.cs @@ -1,5 +1,8 @@ +using System.Diagnostics.CodeAnalysis; + namespace MiniSpace.Services.Posts.Infrastructure.Contexts { + [ExcludeFromCodeCoverage] internal class CorrelationContext { public string CorrelationId { get; set; } diff --git a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Infrastructure/Contexts/IdentityContext.cs b/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Infrastructure/Contexts/IdentityContext.cs index 0c9418efe..9d615dc00 100644 --- a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Infrastructure/Contexts/IdentityContext.cs +++ b/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Infrastructure/Contexts/IdentityContext.cs @@ -1,4 +1,5 @@ using MiniSpace.Services.Posts.Application; +using System.Diagnostics.CodeAnalysis; using System.Runtime.CompilerServices; [assembly: InternalsVisibleTo("MiniSpace.Services.Posts.Application.UnitTests")] diff --git a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Infrastructure/Decorators/OutboxCommandHandlerDecorator.cs b/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Infrastructure/Decorators/OutboxCommandHandlerDecorator.cs index fe8411803..240cacbc4 100644 --- a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Infrastructure/Decorators/OutboxCommandHandlerDecorator.cs +++ b/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Infrastructure/Decorators/OutboxCommandHandlerDecorator.cs @@ -1,3 +1,4 @@ +using System.Diagnostics.CodeAnalysis; using Convey.CQRS.Commands; using Convey.MessageBrokers; using Convey.MessageBrokers.Outbox; @@ -5,6 +6,7 @@ namespace MiniSpace.Services.Posts.Infrastructure.Decorators { + [ExcludeFromCodeCoverage] [Decorator] internal sealed class OutboxCommandHandlerDecorator : ICommandHandler where TCommand : class, ICommand diff --git a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Infrastructure/Decorators/OutboxEventHandlerDecorator.cs b/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Infrastructure/Decorators/OutboxEventHandlerDecorator.cs index f98c19ea0..ac562b6ed 100644 --- a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Infrastructure/Decorators/OutboxEventHandlerDecorator.cs +++ b/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Infrastructure/Decorators/OutboxEventHandlerDecorator.cs @@ -1,3 +1,4 @@ +using System.Diagnostics.CodeAnalysis; using Convey.CQRS.Events; using Convey.MessageBrokers; using Convey.MessageBrokers.Outbox; @@ -5,6 +6,7 @@ namespace MiniSpace.Services.Posts.Infrastructure.Decorators { + [ExcludeFromCodeCoverage] [Decorator] internal sealed class OutboxEventHandlerDecorator : IEventHandler where TEvent : class, IEvent diff --git a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Infrastructure/Exceptions/ExceptionToMessageMapper.cs b/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Infrastructure/Exceptions/ExceptionToMessageMapper.cs index 21453a8b5..caa07a45b 100644 --- a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Infrastructure/Exceptions/ExceptionToMessageMapper.cs +++ b/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Infrastructure/Exceptions/ExceptionToMessageMapper.cs @@ -1,3 +1,4 @@ +using System.Diagnostics.CodeAnalysis; using Convey.MessageBrokers.RabbitMQ; using MiniSpace.Services.Posts.Application.Commands; using MiniSpace.Services.Posts.Application.Events.Rejected; @@ -6,6 +7,7 @@ namespace MiniSpace.Services.Posts.Infrastructure.Exceptions { + [ExcludeFromCodeCoverage] internal sealed class ExceptionToMessageMapper : IExceptionToMessageMapper { public object Map(Exception exception, object message) diff --git a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Infrastructure/Exceptions/ExceptionToResponseMapper.cs b/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Infrastructure/Exceptions/ExceptionToResponseMapper.cs index 70c051e7b..42e3230dd 100644 --- a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Infrastructure/Exceptions/ExceptionToResponseMapper.cs +++ b/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Infrastructure/Exceptions/ExceptionToResponseMapper.cs @@ -1,4 +1,5 @@ using System.Collections.Concurrent; +using System.Diagnostics.CodeAnalysis; using System.Net; using Convey; using Convey.WebApi.Exceptions; @@ -7,6 +8,7 @@ namespace MiniSpace.Services.Posts.Infrastructure.Exceptions { + [ExcludeFromCodeCoverage] internal sealed class ExceptionToResponseMapper : IExceptionToResponseMapper { private static readonly ConcurrentDictionary Codes = new ConcurrentDictionary(); diff --git a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Infrastructure/Extensions.cs b/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Infrastructure/Extensions.cs index 0a91bf72a..da4d595bf 100644 --- a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Infrastructure/Extensions.cs +++ b/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Infrastructure/Extensions.cs @@ -25,13 +25,11 @@ using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.DependencyInjection; -using MiniSpace.Services.Events.Infrastructure.Services.Clients; using Newtonsoft.Json; using MiniSpace.Services.Posts.Application; using MiniSpace.Services.Posts.Application.Commands; using MiniSpace.Services.Posts.Application.Events.External; using MiniSpace.Services.Posts.Application.Services; -using MiniSpace.Services.Posts.Application.Services.Clients; using MiniSpace.Services.Posts.Core.Repositories; using MiniSpace.Services.Posts.Infrastructure.Contexts; using MiniSpace.Services.Posts.Infrastructure.Decorators; @@ -41,9 +39,11 @@ using MiniSpace.Services.Posts.Infrastructure.Mongo.Repositories; using MiniSpace.Services.Posts.Infrastructure.Services; using MiniSpace.Services.Posts.Infrastructure.Services.Workers; +using System.Diagnostics.CodeAnalysis; namespace MiniSpace.Services.Posts.Infrastructure { + [ExcludeFromCodeCoverage] public static class Extensions { public static IConveyBuilder AddInfrastructure(this IConveyBuilder builder) @@ -54,8 +54,6 @@ public static IConveyBuilder AddInfrastructure(this IConveyBuilder builder) builder.Services.AddSingleton(); builder.Services.AddTransient(); builder.Services.AddTransient(); - builder.Services.AddTransient(); - builder.Services.AddTransient(); builder.Services.AddTransient(ctx => ctx.GetRequiredService().Create()); builder.Services.TryDecorate(typeof(ICommandHandler<>), typeof(OutboxCommandHandlerDecorator<>)); builder.Services.TryDecorate(typeof(IEventHandler<>), typeof(OutboxEventHandlerDecorator<>)); diff --git a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Infrastructure/Logging/Extensions.cs b/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Infrastructure/Logging/Extensions.cs index 5e5cd3f91..a1d2c95aa 100644 --- a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Infrastructure/Logging/Extensions.cs +++ b/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Infrastructure/Logging/Extensions.cs @@ -1,3 +1,4 @@ +using System.Diagnostics.CodeAnalysis; using Convey; using Convey.Logging.CQRS; using Microsoft.Extensions.DependencyInjection; @@ -5,6 +6,7 @@ namespace MiniSpace.Services.Posts.Infrastructure.Logging { + [ExcludeFromCodeCoverage] internal static class Extensions { public static IConveyBuilder AddHandlersLogging(this IConveyBuilder builder) diff --git a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Infrastructure/Logging/MessageToLogTemplateMapper.cs b/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Infrastructure/Logging/MessageToLogTemplateMapper.cs index f060a7f43..a3aa98da6 100644 --- a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Infrastructure/Logging/MessageToLogTemplateMapper.cs +++ b/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Infrastructure/Logging/MessageToLogTemplateMapper.cs @@ -1,3 +1,4 @@ +using System.Diagnostics.CodeAnalysis; using Convey.Logging.CQRS; using MiniSpace.Services.Posts.Application.Commands; using MiniSpace.Services.Posts.Application.Events; @@ -5,6 +6,7 @@ namespace MiniSpace.Services.Posts.Infrastructure.Logging { + [ExcludeFromCodeCoverage] internal sealed class MessageToLogTemplateMapper : IMessageToLogTemplateMapper { private static IReadOnlyDictionary MessageTemplates diff --git a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Infrastructure/Mongo/Documents/EventDocument.cs b/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Infrastructure/Mongo/Documents/EventDocument.cs index 47b8b3b05..be59fb7b1 100644 --- a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Infrastructure/Mongo/Documents/EventDocument.cs +++ b/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Infrastructure/Mongo/Documents/EventDocument.cs @@ -1,7 +1,9 @@ +using System.Diagnostics.CodeAnalysis; using Convey.Types; namespace MiniSpace.Services.Posts.Infrastructure.Mongo.Documents { + [ExcludeFromCodeCoverage] public class EventDocument : IIdentifiable { public Guid Id { get; set; } diff --git a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Infrastructure/Mongo/Documents/Extensions.cs b/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Infrastructure/Mongo/Documents/Extensions.cs index f02e7606b..11e99a7bc 100644 --- a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Infrastructure/Mongo/Documents/Extensions.cs +++ b/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Infrastructure/Mongo/Documents/Extensions.cs @@ -1,13 +1,15 @@ +using System.Diagnostics.CodeAnalysis; using MiniSpace.Services.Posts.Application.Dto; using MiniSpace.Services.Posts.Core.Entities; namespace MiniSpace.Services.Posts.Infrastructure.Mongo.Documents { + [ExcludeFromCodeCoverage] public static class Extensions { public static Post AsEntity(this PostDocument document) => new Post(document.Id, document.EventId, document.OrganizerId, document.TextContent, - document.MediaFiles, document.CreatedAt, document.State, document.PublishDate, document.UpdatedAt); + document.MediaContent, document.CreatedAt, document.State, document.PublishDate, document.UpdatedAt); public static PostDocument AsDocument(this Post entity) => new PostDocument() @@ -16,7 +18,7 @@ public static PostDocument AsDocument(this Post entity) EventId = entity.EventId, OrganizerId = entity.OrganizerId, TextContent = entity.TextContent, - MediaFiles = entity.MediaFiles, + MediaContent = entity.MediaContent, CreatedAt = entity.CreatedAt, UpdatedAt = entity.UpdatedAt, State = entity.State, @@ -30,7 +32,7 @@ public static PostDto AsDto(this PostDocument document) EventId = document.EventId, OrganizerId = document.OrganizerId, TextContent = document.TextContent, - MediaFiles = document.MediaFiles, + MediaContent = document.MediaContent, CreatedAt = document.CreatedAt, UpdatedAt = document.UpdatedAt, State = document.State.ToString().ToLowerInvariant(), diff --git a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Infrastructure/Mongo/Documents/PostDocument.cs b/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Infrastructure/Mongo/Documents/PostDocument.cs index 12af21c96..64fae7354 100644 --- a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Infrastructure/Mongo/Documents/PostDocument.cs +++ b/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Infrastructure/Mongo/Documents/PostDocument.cs @@ -1,15 +1,17 @@ +using System.Diagnostics.CodeAnalysis; using Convey.Types; using MiniSpace.Services.Posts.Core.Entities; namespace MiniSpace.Services.Posts.Infrastructure.Mongo.Documents { + [ExcludeFromCodeCoverage] public class PostDocument : IIdentifiable { public Guid Id { get; set; } public Guid EventId { get; set; } public Guid OrganizerId { get; set; } public string TextContent { get; set; } - public IEnumerable MediaFiles { get; set; } + public string MediaContent { get; set; } public State State { get; set; } public DateTime? PublishDate { get; set; } public DateTime CreatedAt { get; set; } diff --git a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Infrastructure/Mongo/Queries/Handlers/GetOrganizerPostsHandler.cs b/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Infrastructure/Mongo/Queries/Handlers/GetOrganizerPostsHandler.cs index 4f84c7cb3..6ccc9cb59 100644 --- a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Infrastructure/Mongo/Queries/Handlers/GetOrganizerPostsHandler.cs +++ b/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Infrastructure/Mongo/Queries/Handlers/GetOrganizerPostsHandler.cs @@ -1,4 +1,5 @@ -using Convey.CQRS.Queries; +using System.Diagnostics.CodeAnalysis; +using Convey.CQRS.Queries; using Convey.Persistence.MongoDB; using MiniSpace.Services.Posts.Application; using MiniSpace.Services.Posts.Application.Dto; @@ -9,6 +10,7 @@ namespace MiniSpace.Services.Posts.Infrastructure.Mongo.Queries.Handlers { + [ExcludeFromCodeCoverage] public class GetOrganizerPostsHandler : IQueryHandler> { private readonly IMongoRepository _postRepository; diff --git a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Infrastructure/Mongo/Queries/Handlers/GetPostHandler.cs b/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Infrastructure/Mongo/Queries/Handlers/GetPostHandler.cs index 4a6447b53..4e88709a2 100644 --- a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Infrastructure/Mongo/Queries/Handlers/GetPostHandler.cs +++ b/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Infrastructure/Mongo/Queries/Handlers/GetPostHandler.cs @@ -1,3 +1,4 @@ +using System.Diagnostics.CodeAnalysis; using Convey.CQRS.Queries; using Convey.Persistence.MongoDB; using MiniSpace.Services.Posts.Application.Dto; @@ -6,6 +7,7 @@ namespace MiniSpace.Services.Posts.Infrastructure.Mongo.Queries.Handlers { + [ExcludeFromCodeCoverage] public class GetPostHandler : IQueryHandler { private readonly IMongoRepository _repository; diff --git a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Infrastructure/Mongo/Queries/Handlers/GetPostsHandler.cs b/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Infrastructure/Mongo/Queries/Handlers/GetPostsHandler.cs index b75fa383c..38085f674 100644 --- a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Infrastructure/Mongo/Queries/Handlers/GetPostsHandler.cs +++ b/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Infrastructure/Mongo/Queries/Handlers/GetPostsHandler.cs @@ -1,3 +1,4 @@ +using System.Diagnostics.CodeAnalysis; using Convey.CQRS.Queries; using Convey.Persistence.MongoDB; using MiniSpace.Services.Posts.Application.Dto; @@ -9,6 +10,7 @@ namespace MiniSpace.Services.Posts.Infrastructure.Mongo.Queries.Handlers { + [ExcludeFromCodeCoverage] public class GetPostsHandler : IQueryHandler> { private readonly IMongoRepository _postRepository; diff --git a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Infrastructure/Mongo/Repositories/EventMongoRepository.cs b/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Infrastructure/Mongo/Repositories/EventMongoRepository.cs index 687e76e5f..d98f1633b 100644 --- a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Infrastructure/Mongo/Repositories/EventMongoRepository.cs +++ b/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Infrastructure/Mongo/Repositories/EventMongoRepository.cs @@ -1,3 +1,4 @@ +using System.Diagnostics.CodeAnalysis; using Convey.Persistence.MongoDB; using MiniSpace.Services.Posts.Core.Entities; using MiniSpace.Services.Posts.Core.Repositories; @@ -5,6 +6,7 @@ namespace MiniSpace.Services.Posts.Infrastructure.Mongo.Repositories { + [ExcludeFromCodeCoverage] public class EventMongoRepository : IEventRepository { private readonly IMongoRepository _repository; diff --git a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Infrastructure/Mongo/Repositories/Extensions.cs b/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Infrastructure/Mongo/Repositories/Extensions.cs deleted file mode 100644 index c201f56a4..000000000 --- a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Infrastructure/Mongo/Repositories/Extensions.cs +++ /dev/null @@ -1,83 +0,0 @@ -using System.Collections; -using MiniSpace.Services.Posts.Core.Entities; -using MiniSpace.Services.Posts.Infrastructure.Mongo.Documents; -using MongoDB.Driver; - -namespace MiniSpace.Services.Posts.Infrastructure.Mongo.Repositories -{ - public static class Extensions - { - private static readonly FilterDefinitionBuilder FilterDefinitionBuilder = Builders.Filter; - public static async Task<(int totalPages, int totalElements, IReadOnlyList data)> AggregateByPage( - this IMongoCollection collection, - FilterDefinition filterDefinition, - SortDefinition sortDefinition, - int page, - int pageSize) - { - var countFacet = AggregateFacet.Create("count", - PipelineDefinition.Create(new[] - { - PipelineStageDefinitionBuilder.Count() - })); - - var dataFacet = AggregateFacet.Create("data", - PipelineDefinition.Create(new[] - { - PipelineStageDefinitionBuilder.Sort(sortDefinition), - PipelineStageDefinitionBuilder.Skip((page - 1) * pageSize), - PipelineStageDefinitionBuilder.Limit(pageSize), - })); - - - var aggregation = await collection.Aggregate() - .Match(filterDefinition) - .Facet(countFacet, dataFacet) - .ToListAsync(); - - var count = aggregation.First() - .Facets.First(x => x.Name == "count") - .Output() - ?.FirstOrDefault() - ?.Count; - - if (count == null) - { - return (0, 0, Array.Empty()); - } - var totalPages = (int)Math.Ceiling((double)count / pageSize); - - var data = aggregation.First() - .Facets.First(x => x.Name == "data") - .Output(); - - return (totalPages, (int)count, data); - } - - public static FilterDefinition ToFilterDefinition(IEnumerable eventsIds) - { - var filterDefinition = FilterDefinitionBuilder.Empty; - - filterDefinition &= FilterDefinitionBuilder.In(p => p.EventId, eventsIds); - filterDefinition &= FilterDefinitionBuilder.Eq(p => p.State, State.Published); - - return filterDefinition; - } - - public static SortDefinition ToSortDefinition(IEnumerable sortByArguments, string direction) - { - var sort = sortByArguments.ToList(); - if(sort.Count == 0) - { - sort.Add("PublishDate"); - } - var sortDefinitionBuilder = Builders.Sort; - var sortDefinition = sort - .Select(sortBy => direction == "asc" - ? sortDefinitionBuilder.Ascending(sortBy) - : sortDefinitionBuilder.Descending(sortBy)); - var sortCombined = sortDefinitionBuilder.Combine(sortDefinition); - return sortCombined; - } - } -} \ No newline at end of file diff --git a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Infrastructure/Mongo/Repositories/PostMongoRepository.cs b/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Infrastructure/Mongo/Repositories/PostMongoRepository.cs index feb7e0619..17f9eb807 100644 --- a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Infrastructure/Mongo/Repositories/PostMongoRepository.cs +++ b/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Infrastructure/Mongo/Repositories/PostMongoRepository.cs @@ -1,4 +1,4 @@ -using System.Collections; +using System.Diagnostics.CodeAnalysis; using Convey.Persistence.MongoDB; using MiniSpace.Services.Posts.Core.Entities; using MiniSpace.Services.Posts.Core.Repositories; @@ -8,6 +8,7 @@ namespace MiniSpace.Services.Posts.Infrastructure.Mongo.Repositories { + [ExcludeFromCodeCoverage] public class PostMongoRepository : IPostRepository { private readonly IMongoRepository _repository; @@ -47,33 +48,5 @@ public Task UpdateAsync(Post post) public Task DeleteAsync(Guid id) => _repository.DeleteAsync(id); - - public Task ExistsAsync(Guid id) - => _repository.ExistsAsync(p => p.Id == id); - - private async Task<(int totalPages, int totalElements, IEnumerable data)> BrowseAsync( - FilterDefinition filterDefinition, SortDefinition sortDefinition, - int pageNumber, int pageSize) - { - var pagedEvents = await _repository.Collection.AggregateByPage( - filterDefinition, - sortDefinition, - pageNumber, - pageSize); - - return pagedEvents; - } - - public async Task<(IEnumerable posts, int pageNumber,int pageSize, int totalPages, int totalElements)> BrowseCommentsAsync(int pageNumber, int pageSize, - IEnumerable eventsIds, IEnumerable sortBy, string direction) - { - var filterDefinition = Extensions.ToFilterDefinition(eventsIds); - var sortDefinition = Extensions.ToSortDefinition(sortBy, direction); - - var pagedEvents = await BrowseAsync(filterDefinition, sortDefinition, pageNumber, pageSize); - - return (pagedEvents.data.Select(e => e.AsEntity()), pageNumber, pageSize, - pagedEvents.totalPages, pagedEvents.totalElements); - } } } diff --git a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Infrastructure/Services/Clients/StudentsServiceClient.cs b/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Infrastructure/Services/Clients/StudentsServiceClient.cs deleted file mode 100644 index d58c856c5..000000000 --- a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Infrastructure/Services/Clients/StudentsServiceClient.cs +++ /dev/null @@ -1,24 +0,0 @@ -using System; -using System.Threading.Tasks; -using Convey.HTTP; -using MiniSpace.Services.Posts.Application.Dto; -using MiniSpace.Services.Posts.Application.Services.Clients; - -namespace MiniSpace.Services.Events.Infrastructure.Services.Clients -{ - public class StudentsServiceClient : IStudentsServiceClient - { - private readonly IHttpClient _httpClient; - private readonly string _url; - - public StudentsServiceClient(IHttpClient httpClient, HttpClientOptions options) - { - _httpClient = httpClient; - _url = options.Services["students"]; - } - - public Task GetAsync(Guid id) - => _httpClient.GetAsync($"{_url}/students/{id}/events"); - - } -} \ No newline at end of file diff --git a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Infrastructure/Services/PostsService.cs b/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Infrastructure/Services/PostsService.cs deleted file mode 100644 index 1cce33758..000000000 --- a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Infrastructure/Services/PostsService.cs +++ /dev/null @@ -1,58 +0,0 @@ -using MiniSpace.Services.Posts.Application; -using MiniSpace.Services.Posts.Application.Commands; -using MiniSpace.Services.Posts.Application.Dto; -using MiniSpace.Services.Posts.Application.Exceptions; -using MiniSpace.Services.Posts.Application.Services; -using MiniSpace.Services.Posts.Application.Services.Clients; -using MiniSpace.Services.Posts.Core.Wrappers; -using MiniSpace.Services.Posts.Core.Entities; -using MiniSpace.Services.Posts.Core.Exceptions; -using MiniSpace.Services.Posts.Core.Repositories; -using MiniSpace.Services.Posts.Infrastructure.Mongo.Documents; - -namespace MiniSpace.Services.Posts.Infrastructure.Services -{ - public class PostsService : IPostsService - { - private readonly IPostRepository _postRepository; - private readonly IStudentsServiceClient _studentsServiceClient; - private readonly IAppContext _appContext; - - public PostsService(IPostRepository postRepository, IStudentsServiceClient studentsServiceClient, - IAppContext appContext) - { - _postRepository = postRepository; - _studentsServiceClient = studentsServiceClient; - _appContext = appContext; - } - - public async Task>> BrowsePostsAsync(SearchPosts command) - { - var identity = _appContext.Identity; - if (identity.IsAuthenticated && identity.Id != command.StudentId) - { - throw new UnauthorizedPostSearchException(command.StudentId, identity.Id); - } - - var studentEvents = await _studentsServiceClient.GetAsync(command.StudentId); - if (studentEvents is null) - { - throw new InvalidStudentServiceClientResponseException(command.StudentId); - } - - var eventsIds = studentEvents.InterestedInEvents.Union(studentEvents.SignedUpEvents).ToList(); - - var pageNumber = command.Pageable.Page < 1 ? 1 : command.Pageable.Page; - var pageSize = command.Pageable.Size > 10 ? 10 : command.Pageable.Size; - - var result = await _postRepository.BrowseCommentsAsync( - pageNumber, pageSize, eventsIds, command.Pageable.Sort.SortBy, command.Pageable.Sort.Direction); - - var pagedEvents = new PagedResponse>( - result.posts.Select(p => new PostDto(p)), - result.pageNumber, result.pageSize, result.totalPages, result.totalElements); - - return pagedEvents; - } - } -} \ No newline at end of file diff --git a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Infrastructure/Services/Workers/PostStateUpdaterWorker.cs b/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Infrastructure/Services/Workers/PostStateUpdaterWorker.cs index 9ec664240..f0d0b579e 100644 --- a/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Infrastructure/Services/Workers/PostStateUpdaterWorker.cs +++ b/MiniSpace.Services.Posts/src/MiniSpace.Services.Posts.Infrastructure/Services/Workers/PostStateUpdaterWorker.cs @@ -11,7 +11,7 @@ public class PostStateUpdaterWorker: BackgroundService private readonly IMessageBroker _messageBroker; private readonly ICommandDispatcher _commandDispatcher; private readonly IDateTimeProvider _dateTimeProvider; - private const int MinutesInterval = 5; + public const int MinutesInterval = 5; public PostStateUpdaterWorker(IMessageBroker messageBroker, ICommandDispatcher commandDispatcher, IDateTimeProvider dateTimeProvider) diff --git a/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Application.UnitTests/Commands/Handlers/ChangePostStateHandlerTest.cs b/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Application.UnitTests/Commands/Handlers/ChangePostStateHandlerTest.cs index 2d56a5abf..527b93781 100644 --- a/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Application.UnitTests/Commands/Handlers/ChangePostStateHandlerTest.cs +++ b/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Application.UnitTests/Commands/Handlers/ChangePostStateHandlerTest.cs @@ -41,6 +41,34 @@ public ChangePostStateHandlerTest() { ); } + [Fact] + public async Task HandleAsync_WithValidParametersAndStateToBePublished_ShouldPublishAsync() { + // Arrange + var contextId = Guid.NewGuid(); + var postId = Guid.NewGuid(); + var cancelationToken = new CancellationToken(); + var state = State.ToBePublished; + var eventId = Guid.NewGuid(); + + var command = new ChangePostState(postId, + state.GetDisplayName(), DateTime.Today); + + var identityContext = new IdentityContext(contextId.ToString(), "", true, default); + _appContextMock.Setup(ctx => ctx.Identity).Returns(identityContext); + + var post = Post.Create(new AggregateId(postId), eventId, contextId, "Text", "Media content", DateTime.Today, + state, DateTime.Today); + + _postRepositoryMock.Setup(repo => repo.GetAsync(command.PostId)).ReturnsAsync(post); + + // Act + Func act = async () => await _changePostStateHandler.HandleAsync(command, cancelationToken); + await act(); + + // Assert + _messageBrokerMock.Verify(broker => broker.PublishAsync(It.IsAny()), Times.Once()); + } + [Fact] public async Task HandleAsync_WithValidParametersAndStateToBePublished_ShouldNotThrowException() { // Arrange diff --git a/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Application.UnitTests/Commands/Handlers/UpdatePostsStateHandlerTest.cs b/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Application.UnitTests/Commands/Handlers/UpdatePostsStateHandlerTest.cs new file mode 100644 index 000000000..891006e41 --- /dev/null +++ b/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Application.UnitTests/Commands/Handlers/UpdatePostsStateHandlerTest.cs @@ -0,0 +1,86 @@ +using Xunit; +using Moq; +using System; +using System.Collections.Generic; +using System.Threading.Tasks; +using System.Linq; +using System.Text; +using MiniSpace.Services.Posts.Application.Events; +using MiniSpace.Services.Posts.Application.Exceptions; +using MiniSpace.Services.Posts.Application.Services; +using MiniSpace.Services.Posts.Core.Entities; +using MiniSpace.Services.Posts.Core.Repositories; +using MiniSpace.Services.Posts.Application.Commands.Handlers; +using MiniSpace.Services.Posts.Application.Commands; +using MiniSpace.Services.Posts.Infrastructure.Contexts; +using Convey.CQRS.Commands; +using System.Threading; +using System.Security.Claims; +using FluentAssertions; + +namespace MiniSpace.Services.Posts.Application.UnitTests.Commands.Handlers { + public class UpdatePostsStateHandlerTest { + private readonly UpdatePostsStateHandler _updatePostStateHandler; + private readonly Mock _postRepositoryMock; + private readonly Mock _messageBrokerMock; + + public UpdatePostsStateHandlerTest() { + _postRepositoryMock = new(); + _messageBrokerMock = new(); + _updatePostStateHandler = new UpdatePostsStateHandler(_postRepositoryMock.Object, + _messageBrokerMock.Object + ); + } + + [Fact] + public async Task HandleAsync_WithValidParametersAndStatePublished_ShouldNotThrowException() { + // Arrange + var eventId = Guid.NewGuid(); + var contextId = Guid.NewGuid(); + var postId = Guid.NewGuid(); + var cancelationToken = new CancellationToken(); + var state = State.Published; + + var command = new UpdatePostsState(DateTime.Today); + + var post = Post.Create(new AggregateId(postId), eventId, contextId, + "Text", "Media content", DateTime.Today, + state, DateTime.Today); + + _postRepositoryMock.Setup(repo => repo.GetAsync(postId)).ReturnsAsync(post); + + // Act & Assert + Func act = async () => await _updatePostStateHandler.HandleAsync(command, cancelationToken); + await act.Should().NotThrowAsync(); + } + + [Fact] + public async Task HandleAsync_WithValidParametersAndStatePublished_ShouldUpdateRepository() { + // Arrange + var eventId = Guid.NewGuid(); + var contextId = Guid.NewGuid(); + var postId = Guid.NewGuid(); + var cancelationToken = new CancellationToken(); + var state = State.ToBePublished; + + var command = new UpdatePostsState(new DateTime(2024, 1, 1, 0, 0, 0)); + + var post = Post.Create(new AggregateId(postId), eventId, contextId, + "Text", "Media content", new DateTime(2000, 1, 1, 0, 0, 0), + state, new DateTime(2000, 1, 1, 0, 0, 0)); + + var postList = new List + { + post + }; + + _postRepositoryMock.Setup(repo => repo.GetToUpdateAsync()).ReturnsAsync(postList.AsEnumerable()); + _postRepositoryMock.Setup(repo => repo.GetAsync(postId)).ReturnsAsync(post); + + // Act & Assert + Func act = async () => await _updatePostStateHandler.HandleAsync(command, cancelationToken); + await act(); + _postRepositoryMock.Verify(repo => repo.UpdateAsync(post), Times.Once()); + } + } +} \ No newline at end of file diff --git a/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Application.UnitTests/Events/External/Handlers/EventDeletedHandlerTest.cs b/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Application.UnitTests/Events/External/Handlers/EventDeletedHandlerTest.cs index 5139880ce..d8e372e1f 100644 --- a/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Application.UnitTests/Events/External/Handlers/EventDeletedHandlerTest.cs +++ b/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Application.UnitTests/Events/External/Handlers/EventDeletedHandlerTest.cs @@ -35,42 +35,6 @@ public EventDeletedHandlerTest() _postRepositoryMock.Object, _commandDispatcherMock.Object); } - // TODO: connect ICommandDispatcher with IPostRepository - - // [Fact] - // public async Task HandleAsync_ValidData_ShouldNotThrowException() - // { - // // Arrange - // var socialEventId = Guid.NewGuid(); - // var organizerId = Guid.NewGuid(); - // var socialEvent = new Event(socialEventId, organizerId); - // var serviceEvent = new EventDeleted(socialEventId); - // var postId1 = Guid.NewGuid(); - // var postId2 = Guid.NewGuid(); - // var post1 = Post.Create(new AggregateId(postId1), socialEventId, Guid.NewGuid(), - // "a", "a", DateTime.Today, - // State.Published, DateTime.Today); - // var post2 = Post.Create(new AggregateId(postId2), socialEventId, Guid.NewGuid(), - // "a", "a", DateTime.Today, - // State.Published, DateTime.Today); - // var postsList = new List { post1, post2 }; - - // _postRepositoryMock.Setup(repo => repo.GetByEventIdAsync(socialEventId)) - // .ReturnsAsync(postsList.AsEnumerable()); - - // var cancellationToken = new CancellationToken(); - - // _eventRepositoryMock.Setup(repo => repo.ExistsAsync(socialEventId)).ReturnsAsync(true); - // _eventRepositoryMock.Setup(repo => repo.GetAsync(socialEventId)).ReturnsAsync(socialEvent); - - // // Act - // await _eventDeletedHandler.HandleAsync(serviceEvent, cancellationToken); - - // // Assert - // _postRepositoryMock.Verify(repo => repo.DeleteAsync(postId1), Times.Once()); - // _postRepositoryMock.Verify(repo => repo.DeleteAsync(postId2), Times.Once()); - // } - [Fact] public async Task HandleAsync_NullEvent_ShouldThrowEventNotFoundException() { diff --git a/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Application.UnitTests/MiniSpace.Services.Posts.Application.UnitTests.csproj b/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Application.UnitTests/MiniSpace.Services.Posts.Application.UnitTests.csproj index 59068af34..3a7933449 100644 --- a/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Application.UnitTests/MiniSpace.Services.Posts.Application.UnitTests.csproj +++ b/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Application.UnitTests/MiniSpace.Services.Posts.Application.UnitTests.csproj @@ -25,7 +25,6 @@ - diff --git a/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Core.UnitTests/Entities/AggregatedIdTest.cs b/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Core.UnitTests/Entities/AggregatedIdTest.cs index 5908458bb..e8f4e9c27 100644 --- a/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Core.UnitTests/Entities/AggregatedIdTest.cs +++ b/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Core.UnitTests/Entities/AggregatedIdTest.cs @@ -48,5 +48,10 @@ public void AggregateId_CreatedTwiceSameGuid_ShouldBeSame() // Assert Assert.Equal(id1.Value, id2.Value); } + + [Fact] + public void AggregateId_CreateWithEmptyGuid_ShouldThrowInvalidAggregateIdException() { + Assert.Throws(() => { var id = new AggregateId(Guid.Empty); }); + } } } \ No newline at end of file diff --git a/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Core.UnitTests/Entities/PostTest.cs b/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Core.UnitTests/Entities/PostTest.cs index 790ded83c..988c319d9 100644 --- a/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Core.UnitTests/Entities/PostTest.cs +++ b/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Core.UnitTests/Entities/PostTest.cs @@ -23,45 +23,53 @@ namespace MiniSpace.Services.Posts.Core.UnitTests.Entities { public class PostTest { - // TODO: prevent exception leakage + [Fact] + public void Create_WithWhitespace_ShouldThrowInvalidPostTextContentException() { + // Arrange + var id = new AggregateId(); + string textContent = " "; + + // Act & Assert + Assert.Throws(() => { + Post.Create(id, default, default, textContent, default, default, default, default); + }); + } - // [Fact] - // public void Update_WithWhitespace_ShouldThrowInvalidPostTextContentException() { - // // Arrange - // var id = new AggregateId(); - // string textContent = " "; - // var post = Post.Create(id, default, default, default, default, default, default, default); - // // Act & Assert - // Action act = () => { post.Update(textContent, "a", default); }; - // var ex = Record.Exception(act); - // Assert.NotNull(ex); - // Assert.IsType(ex); - // } + [Fact] + public void Create_WithNullTextContent_ShouldThrowInvalidPostTextContentException() { + // Arrange + var id = new AggregateId(); + string textContent = null; + + // Act & Assert + Assert.Throws(() => { + Post.Create(id, default, default, textContent, default, default, default, default); + }); + } - // [Fact] - // public void Update_WithNullTextContent_ShouldThrowInvalidPostTextContentException() { - // // Arrange - // var id = new AggregateId(); - // string textContent = null; - // var post = Post.Create(id, default, default, default, default, default, default, default); - // // Act & Assert - // Action act = () => { post.Update(textContent, "a", default); }; - // var ex = Record.Exception(act); - // Assert.NotNull(ex); - // Assert.IsType(ex); - // } + [Fact] + public void Create_WithTooLongTextContent_ShouldThrowInvalidPostTextContentException() { + // Arrange + var id = new AggregateId(); + string textContent = new('a', 100000); + + // Act & Assert + Assert.Throws(() => { + Post.Create(id, default, default, textContent, default, default, default, default); + }); + } - // [Fact] - // public void Update_WithTooLongTextContent_ShouldThrowInvalidPostTextContentException() { - // // Arrange - // var id = new AggregateId(); - // string textContent = new('a', 100000); - // var post = Post.Create(id, default, default, default, default, default, default, default); - // // Act & Assert - // Action act = () => { post.Update(textContent, "a", default); }; - // var ex = Record.Exception(act); - // Assert.NotNull(ex); - // Assert.IsType(ex); - // } + [Fact] + public void CheckPublishDate_WithInappropriateDateTime_ShouldThrowInvalidPostPublishDateException() { + // Arrange + var id = new AggregateId(); + string textContent = new('a', 100); + var post = Post.Create(id, default, default, textContent, default, DateTime.Now, default, DateTime.Now); + + // Act & Assert + Assert.Throws(() => { + post.SetToBePublished(new DateTime(2000, 1, 1, 1, 1, 1), DateTime.Now); + }); + } } } \ No newline at end of file diff --git a/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Core.UnitTests/MiniSpace.Services.Posts.Core.UnitTests.csproj b/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Core.UnitTests/MiniSpace.Services.Posts.Core.UnitTests.csproj index 59068af34..3a7933449 100644 --- a/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Core.UnitTests/MiniSpace.Services.Posts.Core.UnitTests.csproj +++ b/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Core.UnitTests/MiniSpace.Services.Posts.Core.UnitTests.csproj @@ -25,7 +25,6 @@ - diff --git a/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Infrastructure.UnitTests/MiniSpace.Services.Posts.Infrastructure.UnitTests.csproj b/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Infrastructure.UnitTests/MiniSpace.Services.Posts.Infrastructure.UnitTests.csproj index 59068af34..3a7933449 100644 --- a/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Infrastructure.UnitTests/MiniSpace.Services.Posts.Infrastructure.UnitTests.csproj +++ b/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Infrastructure.UnitTests/MiniSpace.Services.Posts.Infrastructure.UnitTests.csproj @@ -25,7 +25,6 @@ - diff --git a/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Infrastructure.UnitTests/Services/Workers/PostStateUpdaterWorkerTest.cs b/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Infrastructure.UnitTests/Services/Workers/PostStateUpdaterWorkerTest.cs new file mode 100644 index 000000000..2258332b0 --- /dev/null +++ b/MiniSpace.Services.Posts/tests/MiniSpace.Services.Posts.Infrastructure.UnitTests/Services/Workers/PostStateUpdaterWorkerTest.cs @@ -0,0 +1,95 @@ +using Xunit; +using Moq; +using Convey.CQRS.Events; +using System; +using System.Collections.Generic; +using System.Threading.Tasks; +using Convey.MessageBrokers; +using Convey.MessageBrokers.Outbox; +using Convey.MessageBrokers.RabbitMQ; +using Microsoft.AspNetCore.Http; +using Microsoft.Extensions.Logging; +using OpenTracing; +using MiniSpace.Services.Posts.Infrastructure.Services; +using MiniSpace.Services.Posts.Application.Services; +using MiniSpace.Services.Posts.Application.Events; +using MiniSpace.Services.Posts.Infrastructure.Services.Workers; +using Convey.CQRS.Commands; +using Microsoft.VisualStudio.TestPlatform.Common.Utilities; +using App.Metrics.Timer; +using MiniSpace.Services.Posts.Application.Commands; + +namespace MiniSpace.Services.Posts.Infrastructure.UnitTests.Services.Workers +{ + public class PostStateUpdaterWorkerTest + { + private readonly PostStateUpdaterWorker _postStateUpdaterWorker; + private readonly Mock _messageBrokerMock; + private readonly Mock _commandDispatcherMock; + private readonly Mock _dateTimeProviderMock; + + public PostStateUpdaterWorkerTest() + { + _messageBrokerMock = new Mock(); + _commandDispatcherMock = new Mock(); + _dateTimeProviderMock = new Mock(); + _postStateUpdaterWorker = new PostStateUpdaterWorker(_messageBrokerMock.Object, + _commandDispatcherMock.Object, _dateTimeProviderMock.Object); + } + + [Fact] + public async Task ExecuteAsync_WithDefaultParameters_ShouldPublishStarted() + { + // Arrange + CancellationToken cancellationToken = new CancellationToken(); + + // Act + await _postStateUpdaterWorker.StartAsync(cancellationToken); + await Task.Delay(1000); + + // Assert + _messageBrokerMock.Verify(broker => + broker.PublishAsync(It.IsAny()), + Times.Once()); + } + + [Fact] + public async Task ExecuteAsync_WithCancelRequested_ShouldPublishStopped() + { + // Arrange + CancellationToken cancellationToken = new CancellationToken(); + + // Act + await _postStateUpdaterWorker.StartAsync(cancellationToken); + await Task.Delay(1000); + await _postStateUpdaterWorker.StopAsync(cancellationToken); + + // Assert + _messageBrokerMock.Verify(broker => + broker.PublishAsync(It.IsAny()), + Times.Once()); + } + + [Fact] + public async Task ExecuteAsync_WithTimeSetToInvokeCommandDispatcherSendAsync_ShouldCommandDispatcherSendAsync() + { + CancellationToken cancellationToken = new CancellationToken(); + var nowMock0 = new DateTime(2024, 5, 26, 18, PostStateUpdaterWorker.MinutesInterval * 1, 0); + var nowMock1 = new DateTime(2024, 5, 26, 18, PostStateUpdaterWorker.MinutesInterval * 2, 0); + + _dateTimeProviderMock.Setup(prov => prov.Now).Returns(nowMock0); + await _postStateUpdaterWorker.StartAsync(cancellationToken); + await Task.Delay(1000); + await _postStateUpdaterWorker.StopAsync(cancellationToken); + + _dateTimeProviderMock.Setup(prov => prov.Now).Returns(nowMock1); + await _postStateUpdaterWorker.StartAsync(cancellationToken); + await Task.Delay(1000); + await _postStateUpdaterWorker.StopAsync(cancellationToken); + + _commandDispatcherMock.Verify(broker => + broker.SendAsync(It.IsAny(), It.IsAny()), + Times.Exactly(2)); + } + } +} \ No newline at end of file