-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from GodelTech/feature/init
Feature/init
- Loading branch information
Showing
10 changed files
with
303 additions
and
19 deletions.
There are no files selected for viewing
15 changes: 15 additions & 0 deletions
15
src/GodelTech.Messaging.AzureServiceBus/AzureServiceBusOptions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
using System.Collections.Generic; | ||
|
||
namespace GodelTech.Messaging.AzureServiceBus | ||
{ | ||
/// <summary> | ||
/// Azure Service Bus options | ||
/// </summary> | ||
public class AzureServiceBusOptions | ||
{ | ||
/// <summary> | ||
/// Queue dictionary that stores key value pair of internal queue key and Azure queue name. | ||
/// </summary> | ||
public IDictionary<string, string> Queues { get; set; } | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
src/GodelTech.Messaging.AzureServiceBus/AzureServiceBusSender.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
using System; | ||
using System.Text.Json; | ||
using System.Threading.Tasks; | ||
using Azure.Messaging.ServiceBus; | ||
using Microsoft.Extensions.Options; | ||
|
||
namespace GodelTech.Messaging.AzureServiceBus | ||
{ | ||
/// <summary> | ||
/// Azure Service Bus sender | ||
/// </summary> | ||
public class AzureServiceBusSender | ||
{ | ||
private readonly ServiceBusClient _serviceBusClient; | ||
private readonly AzureServiceBusOptions _azureServiceBusOptions; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="AzureServiceBusSender"/> class. | ||
/// </summary> | ||
/// <param name="serviceBusClient">Azure Service Bus client.</param> | ||
/// <param name="azureServiceBusOptions">Azure Service Bus options.</param> | ||
public AzureServiceBusSender(ServiceBusClient serviceBusClient, IOptions<AzureServiceBusOptions> azureServiceBusOptions) | ||
{ | ||
if (azureServiceBusOptions == null) throw new ArgumentNullException(nameof(azureServiceBusOptions)); | ||
|
||
_serviceBusClient = serviceBusClient; | ||
_azureServiceBusOptions = azureServiceBusOptions.Value; | ||
} | ||
|
||
/// <summary> | ||
/// Asynchronously sends TModel object as JSON to Azure Service Bus queue. | ||
/// Queue is select by key from options. | ||
/// </summary> | ||
/// <typeparam name="TModel">The type of the T model.</typeparam> | ||
/// <param name="queueKey">Queue key.</param> | ||
/// <param name="model">The model.</param> | ||
/// <exception cref="ArgumentOutOfRangeException">No queue found with provided key.</exception> | ||
public async Task SendAsync<TModel>(string queueKey, TModel model) | ||
where TModel : class | ||
{ | ||
if (!_azureServiceBusOptions.Queues.TryGetValue(queueKey, out var queueName)) throw new ArgumentOutOfRangeException(nameof(queueKey), queueKey, "No queue found with provided key."); | ||
|
||
var sender = _serviceBusClient.CreateSender(queueName); | ||
|
||
var messageToSend = new ServiceBusMessage(JsonSerializer.Serialize(model)); | ||
|
||
await sender.SendMessageAsync(messageToSend); | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
37 changes: 37 additions & 0 deletions
37
src/GodelTech.Messaging.AzureServiceBus/DependencyInjection/ServiceCollectionExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
using System; | ||
using Azure.Messaging.ServiceBus; | ||
using GodelTech.Messaging.AzureServiceBus; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace Microsoft.AspNetCore.Builder | ||
{ | ||
/// <summary> | ||
/// Extensions to register AzureServiceBusSender with the service collection. | ||
/// </summary> | ||
public static class ServiceCollectionExtensions | ||
{ | ||
/// <summary> | ||
/// Register AzureServiceBusSender with the service collection. | ||
/// </summary> | ||
/// <param name="services">Service collection.</param> | ||
/// <param name="connectionString">Connection string to Azure Service Bus.</param> | ||
/// <param name="optionsAction">Azure Service Bus options action.</param> | ||
/// <returns></returns> | ||
public static IServiceCollection AddAzureServiceBusSender( | ||
this IServiceCollection services, | ||
string connectionString, | ||
Action<AzureServiceBusOptions> optionsAction) | ||
{ | ||
// ServiceBusClient | ||
services.AddTransient(provider => new ServiceBusClient(connectionString)); | ||
|
||
// Options | ||
services.Configure(optionsAction); | ||
|
||
// AzureServiceBusSender | ||
services.AddTransient<AzureServiceBusSender>(); | ||
|
||
return services; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
139 changes: 139 additions & 0 deletions
139
test/GodelTech.Messaging.AzureServiceBus.Tests/AzureServiceBusSenderTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text.Json; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Azure.Messaging.ServiceBus; | ||
using GodelTech.Messaging.AzureServiceBus.Tests.Fakes; | ||
using Microsoft.Extensions.Options; | ||
using Moq; | ||
using Xunit; | ||
|
||
namespace GodelTech.Messaging.AzureServiceBus.Tests | ||
{ | ||
public class AzureServiceBusSenderTests | ||
{ | ||
private readonly Mock<ServiceBusClient> _mockServiceBusClient; | ||
|
||
public AzureServiceBusSenderTests() | ||
{ | ||
_mockServiceBusClient = new Mock<ServiceBusClient>(MockBehavior.Strict); | ||
} | ||
|
||
[Fact] | ||
public void Constructor_ThrowsArgumentNullException() | ||
{ | ||
// Arrange & Act & Assert | ||
var exception = Assert.Throws<ArgumentNullException>( | ||
() => new AzureServiceBusSender(_mockServiceBusClient.Object, null) | ||
); | ||
|
||
Assert.Equal("azureServiceBusOptions", exception.ParamName); | ||
} | ||
|
||
[Fact] | ||
public async Task SendAsync_ThrowsArgumentOutOfRangeException() | ||
{ | ||
// Arrange | ||
var model = new List<FakeModel>(); | ||
|
||
var azureServiceBusOptions = new AzureServiceBusOptions | ||
{ | ||
Queues = new Dictionary<string, string> | ||
{ | ||
{ | ||
"OtherInternalKey", | ||
"OtherAzureServiceBusQueueName" | ||
} | ||
} | ||
}; | ||
|
||
var mockAzureServiceBusOptions = new Mock<IOptions<AzureServiceBusOptions>>(); | ||
mockAzureServiceBusOptions | ||
.Setup(x => x.Value) | ||
.Returns(azureServiceBusOptions); | ||
|
||
var sender = new AzureServiceBusSender(_mockServiceBusClient.Object, mockAzureServiceBusOptions.Object); | ||
|
||
var expectedException = new ArgumentOutOfRangeException($"queueKey", "InternalKey", "No queue found with provided key."); | ||
|
||
// Act & Assert | ||
var exception = | ||
await Assert.ThrowsAsync<ArgumentOutOfRangeException>( | ||
() => | ||
sender.SendAsync("InternalKey", model) | ||
); | ||
|
||
Assert.IsType<ArgumentOutOfRangeException>(exception); | ||
Assert.Equal("queueKey", exception.ParamName); | ||
Assert.Equal("InternalKey", exception.ActualValue); | ||
Assert.Equal(expectedException.Message, exception.Message); | ||
} | ||
|
||
[Fact] | ||
public async Task SendAsync_Success() | ||
{ | ||
// Arrange | ||
var model = new List<FakeModel> | ||
{ | ||
new FakeModel | ||
{ | ||
Id = 1, | ||
Name = "TestName" | ||
} | ||
}; | ||
|
||
var serializedModel = JsonSerializer.Serialize(model); | ||
|
||
var azureServiceBusOptions = new AzureServiceBusOptions | ||
{ | ||
Queues = new Dictionary<string, string> | ||
{ | ||
{ | ||
"InternalKey", | ||
"AzureServiceBusQueueName" | ||
} | ||
} | ||
}; | ||
|
||
var mockAzureServiceBusOptions = new Mock<IOptions<AzureServiceBusOptions>>(); | ||
mockAzureServiceBusOptions | ||
.Setup(x => x.Value) | ||
.Returns(azureServiceBusOptions); | ||
|
||
var sender = new AzureServiceBusSender(_mockServiceBusClient.Object, mockAzureServiceBusOptions.Object); | ||
|
||
var mockServiceBusSender = new Mock<ServiceBusSender>(MockBehavior.Strict); | ||
mockServiceBusSender | ||
.Setup( | ||
x => x.SendMessageAsync( | ||
It.Is<ServiceBusMessage>( | ||
serviceBusMessage => | ||
serviceBusMessage.Body.ToString() == serializedModel | ||
), | ||
It.Is<CancellationToken>( | ||
cancellationToken => cancellationToken == default | ||
) | ||
) | ||
) | ||
.Returns(Task.CompletedTask) | ||
.Verifiable(); | ||
|
||
_mockServiceBusClient | ||
.Setup( | ||
x => x.CreateSender( | ||
It.Is<string>(queueOrTopicName => queueOrTopicName == "AzureServiceBusQueueName") | ||
) | ||
) | ||
.Returns(mockServiceBusSender.Object) | ||
.Verifiable(); | ||
|
||
// Act | ||
await sender.SendAsync("InternalKey", model); | ||
|
||
// Assert | ||
_mockServiceBusClient.VerifyAll(); | ||
mockServiceBusSender.VerifyAll(); | ||
} | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
...h.Messaging.AzureServiceBus.Tests/DependencyInjection/ServiceCollectionExtensionsTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using Azure.Messaging.ServiceBus; | ||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Options; | ||
using Xunit; | ||
|
||
namespace GodelTech.Messaging.AzureServiceBus.Tests.DependencyInjection | ||
{ | ||
public class ServiceCollectionExtensionsTests | ||
{ | ||
[Fact] | ||
public void AddAzureServiceBusSender_Success() | ||
{ | ||
// Arrange | ||
const string connectionString = "Endpoint=sb://test.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=YourAccessKey"; | ||
var queues = new Dictionary<string, string> | ||
{ | ||
{ | ||
"InternalKey", | ||
"AzureServiceBusQueueName" | ||
} | ||
}; | ||
Action<AzureServiceBusOptions> optionsAction = options => | ||
{ | ||
options.Queues = queues; | ||
}; | ||
|
||
var services = new ServiceCollection(); | ||
|
||
// Act | ||
services.AddAzureServiceBusSender(connectionString, optionsAction); | ||
|
||
// Assert | ||
var provider = services.BuildServiceProvider(); | ||
|
||
var resultRequiredService = provider.GetRequiredService<ServiceBusClient>(); | ||
Assert.NotNull(resultRequiredService); | ||
|
||
var resultOptionsAction = provider.GetRequiredService<IOptions<AzureServiceBusOptions>>(); | ||
Assert.NotNull(resultOptionsAction); | ||
Assert.NotNull(resultOptionsAction.Value); | ||
Assert.Equal(queues, resultOptionsAction.Value.Queues); | ||
|
||
var resultAzureServiceBusSender = provider.GetRequiredService<AzureServiceBusSender>(); | ||
Assert.NotNull(resultAzureServiceBusSender); | ||
} | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
test/GodelTech.Messaging.AzureServiceBus.Tests/Fakes/FakeModel.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
namespace GodelTech.Messaging.AzureServiceBus.Tests.Fakes | ||
{ | ||
public class FakeModel | ||
{ | ||
public int Id { get; set; } | ||
|
||
public string Name { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 0 additions & 13 deletions
13
test/GodelTech.Messaging.AzureServiceBus.Tests/UnitTest1.cs
This file was deleted.
Oops, something went wrong.