Skip to content

Commit

Permalink
Bugfixes for heartbeats using MongoDB (#5337)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
3 people committed May 8, 2024
1 parent 33178f4 commit 016fa22
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 1 deletion.
7 changes: 7 additions & 0 deletions Elsa.sln
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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}
Expand Down
7 changes: 7 additions & 0 deletions src/modules/Elsa.MongoDb/Features/MongoDbFeature.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -64,6 +65,12 @@ private static void RegisterClassMaps()
.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)
Expand Down
2 changes: 1 addition & 1 deletion src/modules/Elsa.MongoDb/Modules/Runtime/KeyValueStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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 />
Expand Down
15 changes: 15 additions & 0 deletions test/unit/Elsa.MongoDb.UnitTests/Elsa.MongoDb.UnitTests.csproj
Original file line number Diff line number Diff line change
@@ -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>
36 changes: 36 additions & 0 deletions test/unit/Elsa.MongoDb.UnitTests/MongoKeyValueStoreTests.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
1 change: 1 addition & 0 deletions test/unit/Elsa.MongoDb.UnitTests/Usings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
global using Xunit;

0 comments on commit 016fa22

Please sign in to comment.