Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Feature/message broker refactor #17

Open
wants to merge 37 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
5ddf84d
Move into correct namespace
jordaanarmand Apr 21, 2020
e09daa8
Update IRabbitMessageBrokerClient.cs
jordaanarmand Apr 21, 2020
3e662ec
Add Messagebus subscribe helpers
jordaanarmand Apr 21, 2020
ba454c6
Update MessageBrokerSubscriptionBackgroundJob.cs
jordaanarmand Apr 21, 2020
a14f2d2
Update ServiceCollectionExtensions.cs
jordaanarmand Apr 21, 2020
07e0400
Work In Progress
jordaanarmand Apr 25, 2020
0f4892b
Delete CorsServiceCollectionExtensions.cs
jordaanarmand Apr 27, 2020
69e43e0
Create CorsServiceCollectionExtensions.cs
jordaanarmand Apr 27, 2020
b565b55
Update Startup.cs
jordaanarmand Apr 27, 2020
28d51ba
Added Calcs
jordaanarmand Apr 27, 2020
35081cc
Think its working
jordaanarmand Apr 29, 2020
0ef4fdd
Update MessageBrokerSubscriptionBackgroundJob.cs
jordaanarmand May 4, 2020
90a4a9a
Update OrdersController.cs
jordaanarmand May 4, 2020
22ae929
Wire up subscriptions
jordaanarmand May 4, 2020
937fd35
Add Order quantity
jordaanarmand May 4, 2020
8078eb2
fix event publisher wire ups
jordaanarmand May 4, 2020
7a15acf
Fix event publisher wireups
jordaanarmand May 4, 2020
8dfa8cd
Move EventSubscription into EventSubscriptionHandlers
jordaanarmand May 4, 2020
0eeaedc
Update OrderPlacedEventSubscriptionHandler.cs
jordaanarmand May 4, 2020
9527735
Update Startup.cs
jordaanarmand May 4, 2020
b0b50f4
Update MessageBrokerSubscriptionBackgroundJob.cs
jordaanarmand May 4, 2020
1cfeaea
Refactor messagebroker pub sub wireups
jordaanarmand May 4, 2020
2fa72a4
Add microservice integratontests for events
jordaanarmand May 5, 2020
272d3d9
WIP
jordaanarmand May 5, 2020
0b667c5
refactor and clean up tests
jordaanarmand May 6, 2020
26eea6e
Update OrdersControllerTests.cs
jordaanarmand May 6, 2020
5ac0e31
Addition of background processing
jordaanarmand May 6, 2020
9f6ff36
Update dotnetcore.yml
jordaanarmand May 6, 2020
c7cdf6e
Update JobScheduler.cs
jordaanarmand May 9, 2020
566b259
Update IBackgroundProcessingClient.cs
jordaanarmand May 9, 2020
fdf33b5
Storage wire up fix
jordaanarmand May 9, 2020
b26b905
Update BackgroundJobServerServiceCollectionExtensions.cs
jordaanarmand May 9, 2020
9abd49a
Update DatabaseServiceCollectionExtensions.cs
jordaanarmand May 9, 2020
ca46b6a
Update Microservice.Api.csproj
jordaanarmand May 9, 2020
0d6e90a
Install RabbitMqMessageBrokerExtensions
jordaanarmand Jun 13, 2020
9484b39
Update dockerfile
jordaanarmand Jun 13, 2020
0661690
Cleanup
jordaanarmand Jul 4, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/dotnetcore.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ jobs:
- name: Build
run: dotnet build --configuration Release --no-restore
- name: Install dotnet-ef tool
run: dotnet tool install --global dotnet-ef --version 3.1.0
run: dotnet tool install --global dotnet-ef
- name: Apply Migrations
run: dotnet ef database update -s Microservice.Api/Microservice.Api.csproj -p Microservice.Db/Microservice.Db.csproj -c MicroserviceDbContext
- name: Test
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ COPY ./Microservice.Api/*.csproj ./Microservice.Api/
COPY ./Microservice.Db/*.csproj ./Microservice.Db/
COPY ./Microservice.Logic/*.csproj ./Microservice.Logic/
COPY ./Microservice.HangfireBackgroundJobServer/*.csproj ./Microservice.HangfireBackgroundJobServer/
COPY ./Microservice.RabbitMessageBroker/*.csproj ./Microservice.RabbitMessageBroker/
COPY ./Microservice.RabbitMessageBrokerHelpers/*.csproj ./Microservice.RabbitMessageBrokerHelpers/
COPY ./Microservice.RabbitMessageBroker.Integration.Tests/*.csproj ./Microservice.RabbitMessageBroker.Integration.Tests/
COPY ./Microservice.Api.Integration.Tests/*.csproj ./Microservice.Api.Integration.Tests/
RUN dotnet restore
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,30 @@
using System;
using Microsoft.AspNetCore.Mvc.Testing;
using Microsoft.AspNetCore.Mvc.Testing;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting.Internal;
using System;
using System.Collections.Generic;
using System.IO;

namespace Microservice.Api.Integration.Tests.Infrastructure
{
public static class APIWebApplicationFactoryExtensions
{
public static WebApplicationFactory<TStartup> WithAppSettings<TStartup>(
this WebApplicationFactory<TStartup> factory,
IEnumerable<KeyValuePair<string, string>> customSettings = null)
where TStartup : class
=> factory.WithWebHostBuilder(x =>
{
var projDir = Directory.GetCurrentDirectory();
var configpath = Path.Combine(projDir, "appsettings.json");
x.ConfigureServices(c => c.AddSingleton(new HostingEnvironment {EnvironmentName = "Testing"}));
x.ConfigureAppConfiguration((c, d) => d.AddJsonFile(configpath));
if (customSettings != null)
x.ConfigureAppConfiguration((c, d) => d.AddInMemoryCollection(customSettings));
});

public static WebApplicationFactory<TStartup> Seed<TStartup, TDbContext>(
this WebApplicationFactory<TStartup> factory,
Action<TDbContext> seed)
Expand All @@ -22,5 +40,13 @@ public static WebApplicationFactory<TStartup> Seed<TStartup, TDbContext>(
}
return factory;
}

public static TService GetScopedService<TStartup, TService>(
this WebApplicationFactory<TStartup> factory)
where TStartup : class
=> factory.Server.Services.CreateScope().ServiceProvider.GetService<TService>();



}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using Microservice.Db;
using Microservice.RabbitMQMessageBrokerExtension;
using Microservice.RabbitMQMessageBrokerExtension.Configuration;
using Microsoft.AspNetCore.Mvc.Testing;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using NUnit.Framework;
using System.Net.Http;

namespace Microservice.Api.Integration.Tests.Infrastructure
{
public class BaseControllerTest
{
protected internal WebApplicationFactory<Startup> Factory;
protected internal HttpClient Client;
protected internal IRabbitMessageBrokerClient MessageBrokerClient;
public string BaseRoute { get; set; }

[OneTimeSetUp]
public void Setup()
{
Factory = new WebApplicationFactory<Startup>()
.WithWebHostBuilder(builder =>
{
builder.ConfigureServices(services =>
{
services.RemoveAll(typeof(DbContextOptions));
services.RemoveAll(typeof(MicroserviceDbContext));
services.AddDbContext<MicroserviceDbContext>();
services.AddRabbitMqMessageBroker(ConfigurationBuilderExtensions.GetCustomSection("RabbitMessageBrokerSettings"));
});
});

Client = Factory.CreateClient();
MessageBrokerClient = Factory.Services.GetService<IRabbitMessageBrokerClient>();
}

public string GetBaseRoute() => $"api/{BaseRoute}";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using Microservice.RabbitMQMessageBrokerExtension;
using Microsoft.AspNetCore.Mvc.Testing;
using Microsoft.AspNetCore.TestHost;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Moq;
using NUnit.Framework;

namespace Microservice.Api.Integration.Tests.Infrastructure
{
public class BaseEventSubscriptionTest
{
private MockRepository _mockRepository;
protected internal Mock<IRabbitMessageBrokerClient> MessageBrokerClient;
protected internal WebApplicationFactory<Startup> Factory;

[OneTimeSetUp]
public void Setup()
{
_mockRepository = new MockRepository(MockBehavior.Strict);
MessageBrokerClient = _mockRepository.Create<IRabbitMessageBrokerClient>(MockBehavior.Loose);
Factory = new WebApplicationFactory<Startup>()

.WithWebHostBuilder(x =>
{
x.ConfigureAppConfiguration((c, b) => { b.AddConfiguration(ConfigurationBuilderExtensions.GetConfigurationRoot()); });
x.ConfigureTestServices(s => { s.AddTransient(_ => MessageBrokerClient.Object); });
})
;
}
[TearDown]
public void TearDown()
{
_mockRepository.VerifyAll();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using Microsoft.Extensions.Configuration;
using System.IO;

namespace Microservice.Api.Integration.Tests.Infrastructure
{
public static class ConfigurationBuilderExtensions
{
public static IConfigurationSection GetCustomSection(string section)
{
var configBuilder = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: true);
var config = configBuilder.Build();

return config.GetSection(section);
}

public static IConfigurationRoot GetConfigurationRoot()
{
var configBuilder = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: true);

return configBuilder.Build();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public static Task<HttpResponseMessage> PostAsJsonAsync<T>(
return httpClient.PostAsync(url, content);
}

public static Task<HttpResponseMessage> PutAsJsonAsync<T>(
public static Task<HttpResponseMessage> PutAsJsonAsync<T>(
this HttpClient httpClient, string url, T data)
{
var dataAsString = JsonConvert.SerializeObject(data);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,38 @@
</PropertyGroup>

<ItemGroup>
<None Remove="appsettings.json" />
</ItemGroup>

<ItemGroup>
<Content Include="appsettings.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
<ExcludeFromSingleFile>true</ExcludeFromSingleFile>
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
</Content>
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microservice.RabbitMQMessageBrokerExtension" Version="1.3.18" />
<PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="3.1.3" />
<PackageReference Include="Microsoft.AspNetCore.JsonPatch" Version="3.1.3" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="3.1.3" />
<PackageReference Include="Microsoft.AspNetCore.TestHost" Version="3.1.3" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.1.3" />
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="3.1.3" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="3.1.3">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.Hosting" Version="3.1.3" />
<PackageReference Include="Moq" Version="4.14.1" />
<PackageReference Include="nunit" Version="3.12.0" />
<PackageReference Include="NUnit3TestAdapter" Version="3.16.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
<PackageReference Include="RabbitMQ.Client" Version="5.1.2" />
<PackageReference Include="RabbitMQ.Client" Version="6.1.0" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using Microservice.Api.Integration.Tests.Infrastructure;
using Microservice.Db;
using Microservice.Db.EntityModels;
using Microservice.Logic.Orders.Events;
using NUnit.Framework;
using System;
using System.Linq;
using System.Threading.Tasks;

namespace Microservice.Api.Integration.Tests
{
[TestFixture]
public class OrderPlacedEventSubscriptionHandlerTest: BaseEventSubscriptionTest
{
[Test]
public async Task Given_OrderPlacedSubscriptionEvent_Expect_OrderResponse_With_OrderPlacedEvent_Published()
{
// Arrange
var order = new Order
{
Id = 2,
Name = "product zero two",
Quantity = 5
};

var orderPlacedSubscriptionEvent = new OrderPlacedSubscriptionEvent
{
Id = order.Id,
Quantity = 2,
PersonId = 33
};

Factory.Seed<Startup, MicroserviceDbContext>(db =>
{
db.Clear();
db.Orders.Add(order);
});

// Act
var handler = MessageBrokerClient.Invocations.
First(i => (string)i.Arguments[0] == "OrderPlaced")
.Arguments[2] as Func<object, Task>;

await handler.Invoke(orderPlacedSubscriptionEvent);

var dbContext = Factory.GetScopedService<Startup, MicroserviceDbContext>();

var orderResult = dbContext.Orders.Find(order.Id);

// Assert handler
Assert.That(orderResult, Is.Not.Null);
Assert.That(orderResult.Quantity, Is.EqualTo(order.Quantity - orderPlacedSubscriptionEvent.Quantity));
}
}
}
Loading