-
Notifications
You must be signed in to change notification settings - Fork 169
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding POCO converter for Timer extension (#2504)
* Adding POCO converter for Timer extension * Patch version bump and release notes. * Clean up solution file * Moved JsonSerializerOptions from Shared project to Timer project. * Defining converter on `TimerTriggerAttribute` & removefd TimerExtensionStartup. * Renamed test class name
- Loading branch information
Showing
8 changed files
with
169 additions
and
3 deletions.
There are no files selected for viewing
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
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
61 changes: 61 additions & 0 deletions
61
extensions/Worker.Extensions.Timer/src/Converters/TimerInfoConverter.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,61 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the MIT License. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using System.IO; | ||
using System.Text; | ||
using System.Text.Json; | ||
using System.Threading.Tasks; | ||
using Microsoft.Azure.Functions.Worker.Converters; | ||
|
||
namespace Microsoft.Azure.Functions.Worker.Extensions.Timer.Converters | ||
{ | ||
internal sealed class TimerInfoConverter : IInputConverter | ||
{ | ||
private static readonly JsonSerializerOptions _serializerOptions = new() | ||
{ | ||
PropertyNamingPolicy = JsonNamingPolicy.CamelCase, | ||
PropertyNameCaseInsensitive = true | ||
}; | ||
|
||
public async ValueTask<ConversionResult> ConvertAsync(ConverterContext context) | ||
{ | ||
if (context.TargetType != typeof(TimerInfo)) | ||
{ | ||
return ConversionResult.Unhandled(); | ||
} | ||
|
||
byte[]? bytes = null; | ||
|
||
if (context.Source is string sourceString) | ||
{ | ||
bytes = Encoding.UTF8.GetBytes(sourceString); | ||
} | ||
else if (context.Source is ReadOnlyMemory<byte> sourceMemory) | ||
{ | ||
bytes = sourceMemory.ToArray(); | ||
} | ||
|
||
if (bytes == null) | ||
{ | ||
return ConversionResult.Unhandled(); | ||
} | ||
|
||
return await GetConversionResultFromDeserialization(bytes); | ||
} | ||
|
||
private async Task<ConversionResult> GetConversionResultFromDeserialization(byte[] bytes) | ||
{ | ||
try | ||
{ | ||
using var stream = new MemoryStream(bytes); | ||
var deserializedObject = await JsonSerializer.DeserializeAsync<TimerInfo>(stream, _serializerOptions); | ||
return ConversionResult.Success(deserializedObject); | ||
} | ||
catch (Exception ex) | ||
{ | ||
return ConversionResult.Failed(ex); | ||
} | ||
} | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
extensions/Worker.Extensions.Timer/src/Properties/AssemblyInfo.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,6 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the MIT License. See License.txt in the project root for license information. | ||
|
||
using System.Runtime.CompilerServices; | ||
|
||
[assembly: InternalsVisibleTo("Microsoft.Azure.Functions.Worker.Extensions.Timer.Tests, PublicKey=00240000048000009400000006020000002400005253413100040000010001005148be37ac1d9f58bd40a2e472c9d380d635b6048278f7d47480b08c928858f0f7fe17a6e4ce98da0e7a7f0b8c308aecd9e9b02d7e9680a5b5b75ac7773cec096fbbc64aebd429e77cb5f89a569a79b28e9c76426783f624b6b70327eb37341eb498a2c3918af97c4860db6cdca4732787150841e395a29cfacb959c1fd971c1")] |
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
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
55 changes: 55 additions & 0 deletions
55
test/extensions/Worker.Extensions.Timer.Tests/TimerInfoConverterTests.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,55 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the MIT License. See License.txt in the project root for license information. | ||
|
||
using Microsoft.Azure.Functions.Worker; | ||
using Microsoft.Azure.Functions.Worker.Converters; | ||
using Microsoft.Azure.Functions.Worker.Extensions.Timer.Converters; | ||
using Microsoft.Azure.Functions.Worker.Http; | ||
using Moq; | ||
|
||
namespace Worker.Extensions.Timer.Tests | ||
{ | ||
public sealed class TimerInfoConverterTests | ||
{ | ||
[Fact] | ||
public async Task ConvertAsync_ShouldConvertJsonStringToTimerInfo() | ||
{ | ||
// The Source JSON contains properties which does not exist in the "TimerInfo" type (ex; Foo). | ||
const string timerTriggerSourceJson = @" | ||
{ | ||
""Foo"": ""Bar"", | ||
""Schedule"": { | ||
""adjustForDST"": true | ||
}, | ||
""scheduleStatus"": { | ||
""next"": ""2024-12-31T09:51:00-07:00"" | ||
}, | ||
""IsPastDue"": true | ||
}"; | ||
var converter = new TimerInfoConverter(); | ||
var contextMock = new Mock<ConverterContext>(); | ||
contextMock.SetupGet(c => c.TargetType).Returns(typeof(TimerInfo)); | ||
contextMock.SetupGet(c => c.Source).Returns(timerTriggerSourceJson); | ||
|
||
var result = await converter.ConvertAsync(contextMock.Object); | ||
|
||
Assert.Equal(ConversionStatus.Succeeded, result.Status); | ||
var timerInfo = Assert.IsType<TimerInfo>(result.Value); | ||
Assert.True(timerInfo.IsPastDue); | ||
Assert.Equal(DateTime.Parse("2024-12-31T09:51:00-07:00"), timerInfo?.ScheduleStatus?.Next); | ||
} | ||
|
||
[Fact] | ||
public async Task ConvertAsync_ShouldReturnUnhandledWhenTargetTypeIsNotTimerInfo() | ||
{ | ||
var converter = new TimerInfoConverter(); | ||
var contextMock = new Mock<ConverterContext>(); | ||
contextMock.SetupGet(c => c.TargetType).Returns(typeof(HttpRequestData)); | ||
contextMock.SetupGet(c => c.Source).Returns("{\"ScheduleStatus\": {\"Last\": \"2022-01-01T00:00:00Z\"}}"); | ||
|
||
var result = await converter.ConvertAsync(contextMock.Object); | ||
|
||
Assert.Equal(ConversionStatus.Unhandled, result.Status); | ||
} | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
test/extensions/Worker.Extensions.Timer.Tests/Worker.Extensions.Timer.Tests.csproj
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,32 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
<AssemblyName>Microsoft.Azure.Functions.Worker.Extensions.Timer.Tests</AssemblyName> | ||
<RootNamespace>Microsoft.Azure.Functions.Worker.Extensions.Timer.Tests</RootNamespace> | ||
<IsPackable>false</IsPackable> | ||
<IsTestProject>true</IsTestProject> | ||
<SignAssembly>True</SignAssembly> | ||
<AssemblyOriginatorKeyFile>..\..\..\key.snk</AssemblyOriginatorKeyFile> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="coverlet.collector" Version="6.0.0" /> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" /> | ||
<PackageReference Include="xunit" Version="2.5.3" /> | ||
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.3" /> | ||
<PackageReference Include="Moq" Version="4.20.70" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\..\extensions\Worker.Extensions.Http\src\Worker.Extensions.Http.csproj" /> | ||
<ProjectReference Include="..\..\..\extensions\Worker.Extensions.Timer\src\Worker.Extensions.Timer.csproj" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<Using Include="Xunit" /> | ||
</ItemGroup> | ||
|
||
</Project> |