Skip to content

Commit 287b4bf

Browse files
committed
test: add queue storage DI tests
1 parent 52b1264 commit 287b4bf

4 files changed

Lines changed: 215 additions & 0 deletions

File tree

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace Savvyio.Extensions.DependencyInjection.QueueStorage.Assets
2+
{
3+
public struct BusMarker
4+
{
5+
}
6+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace Savvyio.Extensions.DependencyInjection.QueueStorage.Assets
2+
{
3+
public struct QueueMarker
4+
{
5+
}
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<RootNamespace>Savvyio.Extensions.DependencyInjection.QueueStorage</RootNamespace>
4+
</PropertyGroup>
5+
<ItemGroup>
6+
<ProjectReference Include="..\..\src\Savvyio.Extensions.DependencyInjection.QueueStorage\Savvyio.Extensions.DependencyInjection.QueueStorage.csproj" />
7+
<ProjectReference Include="..\..\src\Savvyio.Extensions.Newtonsoft.Json\Savvyio.Extensions.Newtonsoft.Json.csproj" />
8+
<ProjectReference Include="..\..\src\Savvyio.Extensions.Text.Json\Savvyio.Extensions.Text.Json.csproj" />
9+
</ItemGroup>
10+
<ItemGroup>
11+
<Folder Include="Assets\" />
12+
</ItemGroup>
13+
</Project>
Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
using System;
2+
using Azure;
3+
using Codebelt.Extensions.Xunit;
4+
using Microsoft.Extensions.DependencyInjection;
5+
using Savvyio.Commands;
6+
using Savvyio.EventDriven;
7+
using Savvyio.Extensions.DependencyInjection.Messaging;
8+
using Savvyio.Extensions.DependencyInjection.QueueStorage;
9+
using Savvyio.Extensions.DependencyInjection.QueueStorage.Assets;
10+
using DICommands = Savvyio.Extensions.DependencyInjection.QueueStorage.Commands;
11+
using DIEvent = Savvyio.Extensions.DependencyInjection.QueueStorage.EventDriven;
12+
using Savvyio.Extensions.Newtonsoft.Json;
13+
using Savvyio.Extensions.QueueStorage;
14+
using CoreCommands = Savvyio.Extensions.QueueStorage.Commands;
15+
using CoreEvent = Savvyio.Extensions.QueueStorage.EventDriven;
16+
using Savvyio.Extensions.Text.Json;
17+
using Savvyio.Messaging;
18+
using Xunit;
19+
using Xunit.Abstractions;
20+
using Xunit.Sdk;
21+
22+
namespace Savvyio.Extensions.DependencyInjection.QueueStorage
23+
{
24+
public class ServiceCollectionExtensionsTest : Test
25+
{
26+
public ServiceCollectionExtensionsTest(ITestOutputHelper output) : base(output)
27+
{
28+
}
29+
30+
[Fact]
31+
public void AddAzureCommandQueue_ShouldAddDefaultImplementation()
32+
{
33+
Action<AzureQueueOptions> azureSetup = o =>
34+
{
35+
o.StorageAccountName = "account";
36+
o.QueueName = "queue";
37+
o.SasCredential = new AzureSasCredential("sas");
38+
};
39+
40+
var services = new ServiceCollection();
41+
services.AddMarshaller<NewtonsoftJsonMarshaller>();
42+
services.AddAzureCommandQueue(azureSetup);
43+
var provider = services.BuildServiceProvider();
44+
45+
Assert.IsType<CoreCommands.AzureCommandQueue>(provider.GetRequiredService<IPointToPointChannel<ICommand>>());
46+
Assert.IsType<CoreCommands.AzureCommandQueue>(provider.GetRequiredService<ISender<ICommand>>());
47+
Assert.IsType<CoreCommands.AzureCommandQueue>(provider.GetRequiredService<IReceiver<ICommand>>());
48+
49+
var opt1 = new AzureQueueOptions();
50+
var opt2 = new AzureQueueOptions();
51+
var opt3 = new AzureQueueOptions();
52+
53+
azureSetup(opt1);
54+
provider.GetRequiredService<Action<AzureQueueOptions>>()(opt2);
55+
56+
Assert.Equivalent(azureSetup, provider.GetRequiredService<Action<AzureQueueOptions>>());
57+
Assert.Equivalent(opt1, opt2);
58+
59+
Assert.Throws<EquivalentException>(() => Assert.Equivalent(opt1, opt3));
60+
Assert.Throws<EquivalentException>(() => Assert.Equivalent(opt2, opt3));
61+
}
62+
63+
[Fact]
64+
public void AddAzureCommandQueue_ShouldAddDefaultImplementationWithMarker()
65+
{
66+
Action<AzureQueueOptions<QueueMarker>> azureSetup = o =>
67+
{
68+
o.StorageAccountName = "account";
69+
o.QueueName = "queue";
70+
o.SasCredential = new AzureSasCredential("sas");
71+
};
72+
73+
var services = new ServiceCollection();
74+
services.AddMarshaller<JsonMarshaller>();
75+
services.AddAzureCommandQueue(azureSetup);
76+
var provider = services.BuildServiceProvider();
77+
78+
Assert.IsType<DICommands.AzureCommandQueue<QueueMarker>>(provider.GetRequiredService<IPointToPointChannel<ICommand, QueueMarker>>());
79+
Assert.IsType<DICommands.AzureCommandQueue<QueueMarker>>(provider.GetRequiredService<ISender<ICommand, QueueMarker>>());
80+
Assert.IsType<DICommands.AzureCommandQueue<QueueMarker>>(provider.GetRequiredService<IReceiver<ICommand, QueueMarker>>());
81+
82+
var opt1 = new AzureQueueOptions<QueueMarker>();
83+
var opt2 = new AzureQueueOptions<QueueMarker>();
84+
var opt3 = new AzureQueueOptions<QueueMarker>();
85+
86+
azureSetup(opt1);
87+
provider.GetRequiredService<Action<AzureQueueOptions<QueueMarker>>>()(opt2);
88+
89+
Assert.Equivalent(azureSetup, provider.GetRequiredService<Action<AzureQueueOptions<QueueMarker>>>());
90+
Assert.Equivalent(opt1, opt2);
91+
92+
Assert.Throws<EquivalentException>(() => Assert.Equivalent(opt1, opt3));
93+
Assert.Throws<EquivalentException>(() => Assert.Equivalent(opt2, opt3));
94+
}
95+
96+
[Fact]
97+
public void AddAzureEventBus_ShouldAddDefaultImplementation()
98+
{
99+
Action<AzureQueueOptions> azureQueueSetup = o =>
100+
{
101+
o.StorageAccountName = "account";
102+
o.QueueName = "queue";
103+
o.SasCredential = new AzureSasCredential("sas");
104+
};
105+
Action<CoreEvent.AzureEventBusOptions> azureEventBusSetup = o =>
106+
{
107+
o.TopicEndpoint = new Uri("https://example.com");
108+
o.SasCredential = new AzureSasCredential("sas");
109+
};
110+
111+
var services = new ServiceCollection();
112+
services.AddMarshaller<NewtonsoftJsonMarshaller>();
113+
services.AddAzureEventBus(azureQueueSetup, azureEventBusSetup);
114+
var provider = services.BuildServiceProvider();
115+
116+
Assert.IsType<CoreEvent.AzureEventBus>(provider.GetRequiredService<IPublishSubscribeChannel<IIntegrationEvent>>());
117+
Assert.IsType<CoreEvent.AzureEventBus>(provider.GetRequiredService<IPublisher<IIntegrationEvent>>());
118+
Assert.IsType<CoreEvent.AzureEventBus>(provider.GetRequiredService<ISubscriber<IIntegrationEvent>>());
119+
120+
var q1 = new AzureQueueOptions();
121+
var q2 = new AzureQueueOptions();
122+
var q3 = new AzureQueueOptions();
123+
var b1 = new CoreEvent.AzureEventBusOptions();
124+
var b2 = new CoreEvent.AzureEventBusOptions();
125+
var b3 = new CoreEvent.AzureEventBusOptions();
126+
127+
azureQueueSetup(q1);
128+
provider.GetRequiredService<Action<AzureQueueOptions>>()(q2);
129+
azureEventBusSetup(b1);
130+
provider.GetRequiredService<Action<CoreEvent.AzureEventBusOptions>>()(b2);
131+
132+
Assert.Equivalent(azureQueueSetup, provider.GetRequiredService<Action<AzureQueueOptions>>());
133+
Assert.Equivalent(azureEventBusSetup, provider.GetRequiredService<Action<CoreEvent.AzureEventBusOptions>>());
134+
Assert.Equivalent(q1, q2);
135+
Assert.Equivalent(b1, b2);
136+
137+
Assert.Throws<EquivalentException>(() => Assert.Equivalent(q1, q3));
138+
Assert.Throws<EquivalentException>(() => Assert.Equivalent(q2, q3));
139+
Assert.Throws<EquivalentException>(() => Assert.Equivalent(b1, b3));
140+
Assert.Throws<EquivalentException>(() => Assert.Equivalent(b2, b3));
141+
}
142+
143+
[Fact]
144+
public void AddAzureEventBus_ShouldAddDefaultImplementationWithMarker()
145+
{
146+
Action<AzureQueueOptions<BusMarker>> azureQueueSetup = o =>
147+
{
148+
o.StorageAccountName = "account";
149+
o.QueueName = "queue";
150+
o.SasCredential = new AzureSasCredential("sas");
151+
};
152+
Action<DIEvent.AzureEventBusOptions<BusMarker>> azureEventBusSetup = o =>
153+
{
154+
o.TopicEndpoint = new Uri("https://example.com");
155+
o.SasCredential = new AzureSasCredential("sas");
156+
};
157+
158+
var services = new ServiceCollection();
159+
services.AddMarshaller<JsonMarshaller>();
160+
services.AddAzureEventBus(azureQueueSetup, azureEventBusSetup);
161+
var provider = services.BuildServiceProvider();
162+
163+
Assert.IsType<DIEvent.AzureEventBus<BusMarker>>(provider.GetRequiredService<IPublishSubscribeChannel<IIntegrationEvent, BusMarker>>());
164+
Assert.IsType<DIEvent.AzureEventBus<BusMarker>>(provider.GetRequiredService<IPublisher<IIntegrationEvent, BusMarker>>());
165+
Assert.IsType<DIEvent.AzureEventBus<BusMarker>>(provider.GetRequiredService<ISubscriber<IIntegrationEvent, BusMarker>>());
166+
167+
var q1 = new AzureQueueOptions<BusMarker>();
168+
var q2 = new AzureQueueOptions<BusMarker>();
169+
var q3 = new AzureQueueOptions<BusMarker>();
170+
var b1 = new DIEvent.AzureEventBusOptions<BusMarker>();
171+
var b2 = new DIEvent.AzureEventBusOptions<BusMarker>();
172+
var b3 = new DIEvent.AzureEventBusOptions<BusMarker>();
173+
174+
azureQueueSetup(q1);
175+
provider.GetRequiredService<Action<AzureQueueOptions<BusMarker>>>()(q2);
176+
azureEventBusSetup(b1);
177+
provider.GetRequiredService<Action<DIEvent.AzureEventBusOptions<BusMarker>>>()(b2);
178+
179+
Assert.Equivalent(azureQueueSetup, provider.GetRequiredService<Action<AzureQueueOptions<BusMarker>>>());
180+
Assert.Equivalent(azureEventBusSetup, provider.GetRequiredService<Action<DIEvent.AzureEventBusOptions<BusMarker>>>());
181+
Assert.Equivalent(q1, q2);
182+
Assert.Equivalent(b1, b2);
183+
184+
Assert.Throws<EquivalentException>(() => Assert.Equivalent(q1, q3));
185+
Assert.Throws<EquivalentException>(() => Assert.Equivalent(q2, q3));
186+
Assert.Throws<EquivalentException>(() => Assert.Equivalent(b1, b3));
187+
Assert.Throws<EquivalentException>(() => Assert.Equivalent(b2, b3));
188+
}
189+
}
190+
}

0 commit comments

Comments
 (0)