Skip to content

Commit 0ca3f2c

Browse files
authored
Merge pull request #11 from Morvie/RabbitMQ
Implemented consumer with RabbitMQ🐇
2 parents b60c958 + 31c1c25 commit 0ca3f2c

File tree

25 files changed

+376
-39
lines changed

25 files changed

+376
-39
lines changed

.github/workflows/dotnet.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ jobs:
2525
uses: actions/upload-artifact@v3
2626
with:
2727
name: Artifact for Build job results
28-
path: ./build
28+
path: bin/
2929
retention-days: 7
3030

3131
test:
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Worker">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net6.0</TargetFramework>
5+
<Nullable>enable</Nullable>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<UserSecretsId>dotnet-Example.WorkerService-843c6a94-61b1-4ede-b903-f41f2649058a</UserSecretsId>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="Microsoft.Extensions.Hosting" Version="6.0.1" />
12+
</ItemGroup>
13+
</Project>

Example.WorkerService/Program.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using Example.WorkerService;
2+
3+
IHost host = Host.CreateDefaultBuilder(args)
4+
.ConfigureServices(services =>
5+
{
6+
services.AddHostedService<Worker>();
7+
})
8+
.Build();
9+
10+
await host.RunAsync();
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"profiles": {
3+
"Example.WorkerService": {
4+
"commandName": "Project",
5+
"dotnetRunMessages": true,
6+
"environmentVariables": {
7+
"DOTNET_ENVIRONMENT": "Development"
8+
}
9+
}
10+
}
11+
}

Example.WorkerService/Worker.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
namespace Example.WorkerService;
2+
3+
public class Worker : BackgroundService
4+
{
5+
private readonly ILogger<Worker> _logger;
6+
7+
public Worker(ILogger<Worker> logger)
8+
{
9+
_logger = logger;
10+
}
11+
12+
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
13+
{
14+
while (!stoppingToken.IsCancellationRequested)
15+
{
16+
_logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now);
17+
await Task.Delay(1000, stoppingToken);
18+
}
19+
}
20+
}

Forums.API.sln

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,18 @@ VisualStudioVersion = 17.3.32804.467
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{E55534E5-3740-433D-9286-C73BB94B80A7}"
77
EndProject
8-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Forums", "Forums\Forums.csproj", "{B32A812D-6725-4E30-A6A3-75C746BAE10E}"
8+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Forums", "Forums\Forums.csproj", "{B32A812D-6725-4E30-A6A3-75C746BAE10E}"
99
EndProject
10-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ForumsService.Application", "ForumsService.API\ForumsService.Application.csproj", "{3277C567-8A09-4BBB-9588-96DFBF1A5D93}"
10+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ForumsService.Application", "ForumsService.API\ForumsService.Application.csproj", "{3277C567-8A09-4BBB-9588-96DFBF1A5D93}"
1111
EndProject
12-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ForumsService.Domain", "ForumsService.Domain\ForumsService.Domain.csproj", "{890E3992-0D7E-4090-B7ED-658A2EA9207D}"
12+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ForumsService.Domain", "ForumsService.Domain\ForumsService.Domain.csproj", "{890E3992-0D7E-4090-B7ED-658A2EA9207D}"
1313
EndProject
14-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ForumsService.Infrastructure", "ForumsService.Infrastructure\ForumsService.Infrastructure.csproj", "{24838E55-59E1-4B37-A8CA-740AE56265BA}"
14+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ForumsService.Infrastructure", "ForumsService.Infrastructure\ForumsService.Infrastructure.csproj", "{24838E55-59E1-4B37-A8CA-740AE56265BA}"
1515
EndProject
1616
Project("{E53339B2-1760-4266-BCC7-CA923CBCF16C}") = "docker-compose", "docker-compose.dcproj", "{4FEA7726-C49A-4E6D-8A14-FD2BF2E35187}"
1717
EndProject
18+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Forums.Test", "Forums.Test\Forums.Test.csproj", "{99AE3B8A-8D77-49AB-AD8E-A33EE3A9384E}"
19+
EndProject
1820
Global
1921
GlobalSection(SolutionConfigurationPlatforms) = preSolution
2022
Debug|Any CPU = Debug|Any CPU
@@ -41,6 +43,10 @@ Global
4143
{4FEA7726-C49A-4E6D-8A14-FD2BF2E35187}.Debug|Any CPU.Build.0 = Debug|Any CPU
4244
{4FEA7726-C49A-4E6D-8A14-FD2BF2E35187}.Release|Any CPU.ActiveCfg = Release|Any CPU
4345
{4FEA7726-C49A-4E6D-8A14-FD2BF2E35187}.Release|Any CPU.Build.0 = Release|Any CPU
46+
{99AE3B8A-8D77-49AB-AD8E-A33EE3A9384E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
47+
{99AE3B8A-8D77-49AB-AD8E-A33EE3A9384E}.Debug|Any CPU.Build.0 = Debug|Any CPU
48+
{99AE3B8A-8D77-49AB-AD8E-A33EE3A9384E}.Release|Any CPU.ActiveCfg = Release|Any CPU
49+
{99AE3B8A-8D77-49AB-AD8E-A33EE3A9384E}.Release|Any CPU.Build.0 = Release|Any CPU
4450
EndGlobalSection
4551
GlobalSection(SolutionProperties) = preSolution
4652
HideSolutionNode = FALSE

Forums.Test/Forums.Test.csproj

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,30 +11,39 @@
1111
<ItemGroup>
1212
<PackageReference Include="AutoFixture" Version="4.17.0" />
1313
<PackageReference Include="AutoMapper" Version="12.0.0" />
14-
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="6.0.8" />
15-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.1" />
14+
<PackageReference Include="Bogus" Version="34.0.2" />
15+
<PackageReference Include="MassTransit.AspNetCore" Version="7.3.1" />
16+
<PackageReference Include="MassTransit.RabbitMQ" Version="8.0.8" />
17+
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="6.0.11" />
18+
<PackageReference Include="Microsoft.Data.Sqlite.Core" Version="7.0.0" />
19+
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="6.0.11" />
20+
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite.Core" Version="6.0.11" />
1621
<PackageReference Include="Moq" Version="4.18.2" />
17-
<PackageReference Include="MSTest.TestFramework" Version="2.2.10" />
18-
<PackageReference Include="Testcontainers" Version="2.1.0" />
22+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.2.0" />
23+
<PackageReference Include="Respawn" Version="4.0.0" />
24+
<PackageReference Include="TestContainers.Container.Database.MsSql" Version="1.5.0" />
1925
<PackageReference Include="xunit" Version="2.4.2" />
2026
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
21-
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
22-
<PrivateAssets>all</PrivateAssets>
23-
</PackageReference>
24-
<PackageReference Include="coverlet.collector" Version="3.1.0">
25-
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
26-
<PrivateAssets>all</PrivateAssets>
27-
</PackageReference>
28-
<PackageReference Include="coverlet.msbuild" Version="3.0.3">
29-
<PrivateAssets>all</PrivateAssets>
30-
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
31-
</PackageReference>
27+
28+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
29+
<PrivateAssets>all</PrivateAssets>
30+
</PackageReference>
31+
<PackageReference Include="coverlet.collector" Version="3.1.0">
32+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
33+
<PrivateAssets>all</PrivateAssets>
34+
</PackageReference>
35+
<PackageReference Include="coverlet.msbuild" Version="3.0.3">
36+
<PrivateAssets>all</PrivateAssets>
37+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
38+
</PackageReference>
39+
3240

3341
</ItemGroup>
3442

3543
<ItemGroup>
3644
<ProjectReference Include="..\ForumsService.Domain\ForumsService.Domain.csproj" />
3745
<ProjectReference Include="..\ForumsService.Infrastructure\ForumsService.Infrastructure.csproj" />
46+
<ProjectReference Include="..\Forums\Forums.csproj" />
3847
</ItemGroup>
3948

4049
</Project>
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
using AutoMapper;
2+
using Forums.Controllers;
3+
using Forums.Models;
4+
using ForumsService.Application.Command.CreateForum;
5+
using ForumsService.Domain.Entities;
6+
using ForumsService.Infrastructure.Repository;
7+
using MediatR;
8+
using Microsoft;
9+
using Microsoft.AspNetCore.Mvc;
10+
using Moq;
11+
using System;
12+
using System.Collections.Generic;
13+
using System.Linq;
14+
using System.Text;
15+
using System.Threading.Tasks;
16+
17+
namespace Forums.Test.IntegrationTest
18+
{
19+
public class ForumsRepositoryTests
20+
{
21+
[Fact]
22+
public async Task ReturnJsonResult()
23+
{
24+
var mediator = new Mock<IMediator>();
25+
var command = new CreateForumCommand("Title","Description",new Guid(),DateTime.Now,false,1346,54326);
26+
var calculatePriceResult = new ForumsDTO { Id= new Guid(), DateOfAdded = DateTime.Now, Description = "Test", Amountoflikes = 123, MovieId = 2364,Ownership= new Guid(), Reported = false, Title ="Hello!"};
27+
mediator.Setup(x => x.Send(command, default(CancellationToken))).ReturnsAsync(calculatePriceResult);
28+
var controller = new ForumController(mediator.Object);
29+
30+
var result = await controller.Create(command);
31+
32+
Assert.IsAssignableFrom<OkObjectResult>(result);
33+
Assert.Equal(calculatePriceResult, ((OkObjectResult)result).Value);
34+
}
35+
36+
[Fact]
37+
public async Task CallMediatorWithCorrectParameters()
38+
{
39+
var mediator = new Mock<IMediator>();
40+
var command = new CreateForumCommand("Title", "Description", new Guid(), DateTime.Now, false, 1346, 54326);
41+
var controller = new ForumController(mediator.Object);
42+
43+
await controller.Create(command);
44+
45+
mediator.Verify(x => x.Send(command, default(CancellationToken)), Times.Once());
46+
}
47+
}
48+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
using Bogus;
2+
using ForumsService.Domain.Entities;
3+
using ForumsService.Infrastructure.Context;
4+
using Microsoft.Data.Sqlite;
5+
using Microsoft.EntityFrameworkCore;
6+
using System;
7+
using System.Data.Common;
8+
9+
namespace Forums.Test.IntegrationTest
10+
{
11+
public class SharedDatabaseFixture: IDisposable
12+
{
13+
private static readonly object _lock = new object();
14+
private static bool _databaseInitialized;
15+
private string dbName = "TestDatabase.db";
16+
public SharedDatabaseFixture()
17+
{
18+
Connection = new SqliteConnection($"Filename={dbName}");
19+
Seed();
20+
Connection.Open();
21+
}
22+
public DbConnection Connection
23+
{
24+
get;
25+
}
26+
public ForumDbContext CreateContext(DbTransaction? transaction = null)
27+
{
28+
var context = new ForumDbContext(new DbContextOptionsBuilder<ForumDbContext>().UseSqlite(Connection).Options);
29+
if (transaction != null)
30+
{
31+
context.Database.UseTransaction(transaction);
32+
}
33+
return context;
34+
}
35+
private void Seed()
36+
{
37+
lock (_lock)
38+
{
39+
if (!_databaseInitialized)
40+
{
41+
using (var context = CreateContext())
42+
{
43+
context.Database.EnsureDeleted();
44+
context.Database.EnsureCreated();
45+
SeedData(context);
46+
}
47+
_databaseInitialized = true;
48+
}
49+
}
50+
}
51+
private void SeedData(ForumDbContext context)
52+
{
53+
var id = new Guid("1c565daf - 3eaa - 4b60 - bc11 - d0a96fce249e");
54+
var fakeForums = new Faker<ForumsDTO>()
55+
.RuleFor(o => o.Id, f => id)
56+
.RuleFor(o => o.Description, f => "This is a movie test description!")
57+
.RuleFor(o => o.Title, f => "Test forum")
58+
.RuleFor(o => o.Ownership, f => id)
59+
.RuleFor(o => o.MovieId, f => f.Random.Int(10000, 99999))
60+
.RuleFor(o => o.DateOfAdded, f => DateTime.Now)
61+
.RuleFor(o => o.Amountoflikes, f => f.Random.Int(0, 1000))
62+
.RuleFor(o => o.Reported, f => false);
63+
var products = fakeForums.Generate(10);
64+
context.AddRange(products);
65+
context.SaveChanges();
66+
}
67+
public void Dispose() => Connection.Dispose();
68+
}
69+
}

Forums.Test/UnitTest/QueryTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public class QueryTests
1717
public async Task GetAllForums()
1818
{
1919
//Arrange
20-
var handler = new GetAllForumsHandler(_mockRepo.Object);
20+
var handler = new CreateForumHandler(_mockRepo.Object);
2121

2222
//Act
2323
var result = await handler.Handle(new GetAllForumsQuery(), CancellationToken.None);

0 commit comments

Comments
 (0)