Skip to content

Commit

Permalink
Refactor ServiceBus converter & tests (#1716, #1723, #1727)
Browse files Browse the repository at this point in the history
  • Loading branch information
liliankasem committed Jul 26, 2023
1 parent f8959ed commit 1e2bb21
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,18 @@
using Azure.Messaging.ServiceBus;
using Microsoft.Azure.Functions.Worker.Converters;
using Microsoft.Azure.Functions.Worker.Core;
using Microsoft.Azure.Functions.Worker.Extensions;
using Microsoft.Azure.Functions.Worker.Extensions.Abstractions;
using Microsoft.Azure.Functions.Worker.Extensions.ServiceBus;

namespace Microsoft.Azure.Functions.Worker
{
/// <summary>
/// Converter to bind to <see cref="ServiceBusReceivedMessage" /> or <see cref="ServiceBusReceivedMessage[]" /> type parameters.
/// </summary>
[SupportsDeferredBinding]
[SupportedConverterType(typeof(ServiceBusReceivedMessage))]
[SupportedConverterType(typeof(ServiceBusReceivedMessage[]))]
[SupportedTargetType(typeof(ServiceBusReceivedMessage))]
[SupportedTargetType(typeof(ServiceBusReceivedMessage[]))]
internal class ServiceBusReceivedMessageConverter : IInputConverter
{
public ValueTask<ConversionResult> ConvertAsync(ConverterContext context)
Expand All @@ -26,7 +30,7 @@ public ValueTask<ConversionResult> ConvertAsync(ConverterContext context)
{
ModelBindingData binding => ConversionResult.Success(ConvertToServiceBusReceivedMessage(binding)),
// Only array collections are currently supported, which matches the behavior of the in-proc extension.
CollectionModelBindingData collection => ConversionResult.Success(collection.ModelBindingDataArray
CollectionModelBindingData collection => ConversionResult.Success(collection.ModelBindingData
.Select(ConvertToServiceBusReceivedMessage).ToArray()),
_ => ConversionResult.Unhandled()
};
Expand All @@ -40,16 +44,19 @@ public ValueTask<ConversionResult> ConvertAsync(ConverterContext context)

private ServiceBusReceivedMessage ConvertToServiceBusReceivedMessage(ModelBindingData binding)
{
if (binding?.Source is not Constants.BindingSource)
if (binding is null)
{
throw new InvalidOperationException(
$"Unexpected binding source. Only '{Constants.BindingSource}' is supported.");
throw new ArgumentNullException(nameof(binding));
}

if (binding.ContentType != Constants.BinaryContentType)
if (binding.Source is not Constants.BindingSource)
{
throw new InvalidOperationException(
$"Unexpected content-type. Only '{Constants.BinaryContentType}' is supported.");
throw new InvalidBindingSourceException(Constants.BindingSource);
}

if (binding.ContentType is not Constants.BinaryContentType)
{
throw new InvalidContentTypeException(Constants.BinaryContentType);
}

// The lock token is a 16 byte GUID
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

namespace Microsoft.Azure.Functions.Worker
{
[AllowConverterFallback(true)]
[ConverterFallbackBehavior(ConverterFallbackBehavior.Default)]
[InputConverter(typeof(ServiceBusReceivedMessageConverter))]
public sealed class ServiceBusTriggerAttribute : TriggerBindingAttribute, ISupportCardinality
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
</ItemGroup>

<ItemGroup>
<SharedReference Include="..\..\Worker.Extensions.Shared/Worker.Extensions.Shared.csproj" />
<SharedReference Include="..\..\Worker.Extensions.Shared\Worker.Extensions.Shared.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public async Task ConvertAsync_ReturnsFailure_WrongContentType()
Assert.Equal(ConversionStatus.Failed, result.Status);
var output = result.Value as ServiceBusReceivedMessage;
Assert.Null(output);
Assert.IsType<InvalidOperationException>(result.Error);
Assert.Equal("Unexpected content-type. Only 'application/octet-stream' is supported.", result.Error.Message);
}

[Fact]
Expand Down Expand Up @@ -117,7 +117,7 @@ public async Task ConvertAsync_Batch_ReturnsFailure_WrongContentType()
Assert.Equal(ConversionStatus.Failed, result.Status);
var output = result.Value as ServiceBusReceivedMessage[];
Assert.Null(output);
Assert.IsType<InvalidOperationException>(result.Error);
Assert.Equal("Unexpected content-type. Only 'application/octet-stream' is supported.", result.Error.Message);
}

[Fact]
Expand All @@ -140,7 +140,7 @@ public async Task ConvertAsync_ReturnsFailure_WrongSource()
Assert.Equal(ConversionStatus.Failed, result.Status);
var output = result.Value as ServiceBusReceivedMessage;
Assert.Null(output);
Assert.IsType<InvalidOperationException>(result.Error);
Assert.Equal("Unexpected binding source. Only 'AzureServiceBusReceivedMessage' is supported.", result.Error.Message);
}

[Fact]
Expand Down Expand Up @@ -168,7 +168,7 @@ public async Task ConvertAsync_Batch_ReturnsFailure_WrongSource()
Assert.Equal(ConversionStatus.Failed, result.Status);
var output = result.Value as ServiceBusReceivedMessage[];
Assert.Null(output);
Assert.IsType<InvalidOperationException>(result.Error);
Assert.Equal("Unexpected binding source. Only 'AzureServiceBusReceivedMessage' is supported.", result.Error.Message);
}

private static void AssertReceivedMessage(ServiceBusReceivedMessage output, Guid lockToken)
Expand Down

0 comments on commit 1e2bb21

Please sign in to comment.