Bugfixes for heartbeats using MongoDB (#5337)

* Added failing unit test

* Added fix for error 'Instance property 'Id' is not defined for type 'Elsa.KeyValues.Entities.SerializedKeyValuePair' (Parameter 'propertyName')'

* Added Bson class map for SerializedKeyValuePair. Ignoring _id field mapping.

---------

Co-authored-by: Stephan Melzer <s.melzer@mera-petfood.com>
Co-authored-by: Sipke Schoorstra <sipkeschoorstra@outlook.com>
This commit is contained in:
Stephan Melzer 2024-05-08 20:19:40 +02:00 committed by GitHub
parent 33178f41c1
commit 016fa22145
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 67 additions and 1 deletions

View file

@ -352,6 +352,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "performance", "performance"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Elsa.Workflows.PerformanceTests", "test\performance\Elsa.Workflows.PerformanceTests\Elsa.Workflows.PerformanceTests.csproj", "{90CD37A9-C866-4D90-A3B1-8C87F53B845E}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Elsa.MongoDb.UnitTests", "test\unit\Elsa.MongoDb.UnitTests\Elsa.MongoDb.UnitTests.csproj", "{56CAA9F2-1882-4EFA-BAC0-9C3D804553F1}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -836,6 +838,10 @@ Global
{90CD37A9-C866-4D90-A3B1-8C87F53B845E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{90CD37A9-C866-4D90-A3B1-8C87F53B845E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{90CD37A9-C866-4D90-A3B1-8C87F53B845E}.Release|Any CPU.Build.0 = Release|Any CPU
{56CAA9F2-1882-4EFA-BAC0-9C3D804553F1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{56CAA9F2-1882-4EFA-BAC0-9C3D804553F1}.Debug|Any CPU.Build.0 = Debug|Any CPU
{56CAA9F2-1882-4EFA-BAC0-9C3D804553F1}.Release|Any CPU.ActiveCfg = Release|Any CPU
{56CAA9F2-1882-4EFA-BAC0-9C3D804553F1}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
@ -982,6 +988,7 @@ Global
{34BC9836-681D-43A5-BC39-E4FEE17FC528} = {08B41FFA-CEE3-46A7-B5C0-3EB65D37A16C}
{CBB515F3-A0EF-43B5-A907-FD4E652DD66E} = {90031D64-CA0F-46D0-9AF4-8DC023A5FFCD}
{90CD37A9-C866-4D90-A3B1-8C87F53B845E} = {CBB515F3-A0EF-43B5-A907-FD4E652DD66E}
{56CAA9F2-1882-4EFA-BAC0-9C3D804553F1} = {18453B51-25EB-4317-A4B3-B10518252E92}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {D4B5CEAA-7D70-4FCB-A68E-B03FBE5E0E5E}

View file

@ -1,6 +1,7 @@
using System.Text.Json;
using Elsa.Features.Abstractions;
using Elsa.Features.Services;
using Elsa.KeyValues.Entities;
using Elsa.MongoDb.Options;
using Elsa.MongoDb.Serializers;
using Elsa.Workflows.Memory;
@ -64,6 +65,12 @@ public class MongoDbFeature : FeatureBase
.MapIdProperty(b => b.BookmarkId)
.SetSerializer(new StringSerializer(BsonType.String));
});
BsonClassMap.RegisterClassMap<SerializedKeyValuePair>(map =>
{
map.AutoMap();
map.SetIgnoreExtraElements(true); // Needed for missing ID property
});
}
private static void TryRegisterSerializerOrSkipWhenExist(Type type, IBsonSerializer serializer)

View file

@ -16,7 +16,7 @@ public class MongoKeyValueStore(MongoDbStore<SerializedKeyValuePair> keyValueMon
/// <inheritdoc />
public Task SaveAsync(SerializedKeyValuePair keyValuePair, CancellationToken cancellationToken)
{
return keyValueMongoDbStore.SaveAsync(keyValuePair, cancellationToken);
return keyValueMongoDbStore.SaveAsync(keyValuePair, x => x.Key, cancellationToken);
}
/// <inheritdoc />

View file

@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="NSubstitute"/>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\..\src\modules\Elsa.MongoDb\Elsa.MongoDb.csproj" />
</ItemGroup>
</Project>

View file

@ -0,0 +1,36 @@
using Elsa.KeyValues.Entities;
using Elsa.MongoDb.Common;
using Elsa.MongoDb.Modules.Runtime;
using MongoDB.Driver;
using NSubstitute;
namespace Elsa.MongoDb.UnitTests;
public class MongoKeyValueStoreTests
{
private readonly MongoDbStore<SerializedKeyValuePair> _mongoDbStore;
public MongoKeyValueStoreTests()
{
var mongoCollectionMock = Substitute.For<IMongoCollection<SerializedKeyValuePair>>();
mongoCollectionMock.FindOneAndReplaceAsync(
Arg.Any<FilterDefinition<SerializedKeyValuePair>>(),
Arg.Any<SerializedKeyValuePair>(),
Arg.Any<FindOneAndReplaceOptions<SerializedKeyValuePair>>()
)
.Returns(new SerializedKeyValuePair());
_mongoDbStore = new MongoDbStore<SerializedKeyValuePair>(mongoCollectionMock);
}
[Fact(DisplayName = "When saving a SerializedKeyValuePair document, don't throw an exception of missing ID property")]
public async Task SaveAsync_WithSerializedKeyValuePairDocument_DoesNotThrowException()
{
var mongoKeyValueStore = new MongoKeyValueStore(_mongoDbStore);
var keyValuePair = new SerializedKeyValuePair();
var exception = await Record.ExceptionAsync(
async () => await mongoKeyValueStore.SaveAsync(keyValuePair, default));
Assert.Null(exception);
}
}

View file

@ -0,0 +1 @@
global using Xunit;