Skip to content

Commit e996593

Browse files
committed
Add functional test for integration events. Implement IDisposable for EventBusRabbitMQ.
1 parent 33335eb commit e996593

12 files changed

Lines changed: 252 additions & 91 deletions

File tree

docker-compose-external.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,8 @@ services:
88
image: redis
99
ports:
1010
- "6379:6379"
11+
12+
rabbitmq:
13+
image: rabbitmq
14+
ports:
15+
- "5672:5672"

src/BuildingBlocks/EventBus/EventBusRabbitMQ/EventBusRabbitMQ.cs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
namespace Microsoft.eShopOnContainers.BuildingBlocks.EventBusRabbitMQ
1616
{
17-
public class EventBusRabbitMQ : IEventBus
17+
public class EventBusRabbitMQ : IEventBus, IDisposable
1818
{
1919
private readonly string _brokerName = "eshop_event_bus";
2020
private readonly string _connectionString;
@@ -31,6 +31,7 @@ public EventBusRabbitMQ(string connectionString)
3131
_handlers = new Dictionary<string, List<IIntegrationEventHandler>>();
3232
_eventTypes = new List<Type>();
3333
}
34+
3435
public void Publish(IntegrationEvent @event)
3536
{
3637
var eventName = @event.GetType().Name;
@@ -92,14 +93,24 @@ public void Unsubscribe<T>(IIntegrationEventHandler<T> handler) where T : Integr
9293
if (_handlers.Keys.Count == 0)
9394
{
9495
_queueName = string.Empty;
95-
_connection.Item1.Close();
96-
_connection.Item2.Close();
96+
_connection.Item1.Dispose();
97+
_connection.Item2.Dispose();
9798
}
9899

99100
}
100101
}
101102
}
102103

104+
public void Dispose()
105+
{
106+
if (_connection != null)
107+
{
108+
_handlers.Clear();
109+
_connection.Item1.Dispose();
110+
_connection.Item2.Dispose();
111+
}
112+
}
113+
103114
private IModel GetChannel()
104115
{
105116
if (_connection != null)
@@ -151,5 +162,6 @@ private async Task ProcessEvent(string eventName, string message)
151162
}
152163
}
153164
}
165+
154166
}
155167
}

src/Services/Basket/Basket.API/IntegrationEvents/EventHandling/ProductPriceChangedEventHandler.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public async Task Handle(ProductPriceChangedEvent @event)
2020
foreach (var id in userIds)
2121
{
2222
var basket = await _repository.GetBasket(id);
23-
await UpdateBasket(@event.ProductId, @event.NewPrice, basket);
23+
await UpdateBasket(@event.ProductId, @event.NewPrice, basket);
2424
}
2525
}
2626

@@ -31,9 +31,12 @@ private async Task UpdateBasket(int productId, decimal newPrice, CustomerBasket
3131
{
3232
foreach (var item in itemsToUpdate)
3333
{
34-
var originalPrice = item.UnitPrice;
35-
item.UnitPrice = newPrice;
36-
item.OldUnitPrice = originalPrice;
34+
if(item.UnitPrice != newPrice)
35+
{
36+
var originalPrice = item.UnitPrice;
37+
item.UnitPrice = newPrice;
38+
item.OldUnitPrice = originalPrice;
39+
}
3740
}
3841
await _repository.UpdateBasket(basket);
3942
}

src/Services/Basket/Basket.API/Startup.cs

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
using Microsoft.eShopOnContainers.Services.Basket.API.IntegrationEvents.Events;
1414
using Microsoft.eShopOnContainers.Services.Basket.API.IntegrationEvents.EventHandling;
1515
using Microsoft.eShopOnContainers.BuildingBlocks.EventBusRabbitMQ;
16+
using System;
1617

1718
namespace Microsoft.eShopOnContainers.Services.Basket.API
1819
{
@@ -80,13 +81,8 @@ public void ConfigureServices(IServiceCollection services)
8081

8182
var serviceProvider = services.BuildServiceProvider();
8283
var configuration = serviceProvider.GetRequiredService<IOptionsSnapshot<BasketSettings>>().Value;
83-
var eventBus = new EventBusRabbitMQ(configuration.EventBusConnection);
84-
services.AddSingleton<IEventBus>(eventBus);
84+
services.AddSingleton<IEventBus>(provider => new EventBusRabbitMQ(configuration.EventBusConnection));
8585

86-
87-
var catalogPriceHandler = serviceProvider.GetService<IIntegrationEventHandler<ProductPriceChangedEvent>>();
88-
eventBus.Subscribe<ProductPriceChangedEvent>(catalogPriceHandler);
89-
9086
}
9187

9288
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
@@ -100,20 +96,28 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerF
10096
// Use frameworks
10197
app.UseCors("CorsPolicy");
10298

103-
var identityUrl = Configuration.GetValue<string>("IdentityUrl");
104-
105-
app.UseIdentityServerAuthentication(new IdentityServerAuthenticationOptions
106-
{
107-
Authority = identityUrl.ToString(),
108-
ScopeName = "basket",
109-
RequireHttpsMetadata = false
110-
});
99+
ConfigureAuth(app);
111100

112101
app.UseMvcWithDefaultRoute();
113102

114103
app.UseSwagger()
115104
.UseSwaggerUi();
116105

106+
var catalogPriceHandler = app.ApplicationServices.GetService<IIntegrationEventHandler<ProductPriceChangedEvent>>();
107+
var eventBus = app.ApplicationServices.GetRequiredService<IEventBus>();
108+
eventBus.Subscribe<ProductPriceChangedEvent>(catalogPriceHandler);
109+
117110
}
111+
112+
protected virtual void ConfigureAuth(IApplicationBuilder app)
113+
{
114+
var identityUrl = Configuration.GetValue<string>("IdentityUrl");
115+
app.UseIdentityServerAuthentication(new IdentityServerAuthenticationOptions
116+
{
117+
Authority = identityUrl.ToString(),
118+
ScopeName = "basket",
119+
RequireHttpsMetadata = false
120+
});
121+
}
118122
}
119123
}

test/Services/FunctionalTests/FunctionalTests.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,16 @@
1313
</ItemGroup>
1414

1515
<ItemGroup>
16+
<ProjectReference Include="..\..\..\src\Services\Basket\Basket.API\Basket.API.csproj" />
1617
<ProjectReference Include="..\..\..\src\Services\Catalog\Catalog.API\Catalog.API.csproj" />
1718
<ProjectReference Include="..\..\..\src\Services\Ordering\Ordering.API\Ordering.API.csproj" />
1819
<ProjectReference Include="..\..\..\src\Web\WebMVC\WebMVC.csproj" />
1920
</ItemGroup>
2021

2122
<ItemGroup>
23+
<None Update="appsettings.json">
24+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
25+
</None>
2226
<None Update="settings.json">
2327
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
2428
</None>
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using Microsoft.AspNetCore.Hosting;
2+
using Microsoft.AspNetCore.TestHost;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.IO;
6+
using System.Text;
7+
8+
namespace FunctionalTests.Services.Basket
9+
{
10+
public class BasketScenariosBase
11+
{
12+
public TestServer CreateServer()
13+
{
14+
var webHostBuilder = new WebHostBuilder();
15+
webHostBuilder.UseContentRoot(Directory.GetCurrentDirectory());
16+
webHostBuilder.UseStartup<BasketTestsStartup>();
17+
18+
return new TestServer(webHostBuilder);
19+
}
20+
21+
public static class Get
22+
{
23+
public static string GetBasketByCustomer(string customerId)
24+
{
25+
return $"/{customerId}";
26+
}
27+
}
28+
29+
public static class Post
30+
{
31+
public static string CreateBasket = "/";
32+
}
33+
}
34+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using Microsoft.eShopOnContainers.Services.Basket.API;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Text;
5+
using Microsoft.AspNetCore.Hosting;
6+
using Microsoft.AspNetCore.Builder;
7+
using FunctionalTests.Middleware;
8+
9+
namespace FunctionalTests.Services.Basket
10+
{
11+
public class BasketTestsStartup : Startup
12+
{
13+
public BasketTestsStartup(IHostingEnvironment env) : base(env)
14+
{
15+
}
16+
17+
protected override void ConfigureAuth(IApplicationBuilder app)
18+
{
19+
if (Configuration["isTest"] == bool.TrueString.ToLowerInvariant())
20+
{
21+
app.UseMiddleware<AutoAuthorizeMiddleware>();
22+
}
23+
else
24+
{
25+
base.ConfigureAuth(app);
26+
}
27+
}
28+
}
29+
}

test/Services/FunctionalTests/Services/Catalog/CatalogScenarios.cs

Lines changed: 0 additions & 55 deletions
This file was deleted.

test/Services/FunctionalTests/Services/Catalog/CatalogScenariosBase.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ public static class Get
2424
{
2525
public static string Orders = "api/v1/orders";
2626

27+
public static string Items = "api/v1/catalog/items";
28+
2729
public static string ProductByName(string name)
2830
{
2931
return $"api/v1/catalog/items/withname/{name}";

test/Services/FunctionalTests/Services/Catalog/CatalogTestsStartup.cs

Lines changed: 0 additions & 15 deletions
This file was deleted.

0 commit comments

Comments
 (0)