diff --git a/backend/src/Squidex.Domain.Apps.Core.Operations/HandleRules/RuleTypeProvider.cs b/backend/src/Squidex.Domain.Apps.Core.Operations/HandleRules/RuleTypeProvider.cs index a8b2b09b2..db8592306 100644 --- a/backend/src/Squidex.Domain.Apps.Core.Operations/HandleRules/RuleTypeProvider.cs +++ b/backend/src/Squidex.Domain.Apps.Core.Operations/HandleRules/RuleTypeProvider.cs @@ -38,18 +38,18 @@ public sealed class RuleTypeProvider : ITypeProvider } } - public void Add() where T : RuleAction + public RuleTypeProvider Add() where T : RuleAction { - Add(typeof(T)); + return Add(typeof(T)); } - private void Add(Type actionType) + private RuleTypeProvider Add(Type actionType) { var metadata = actionType.GetCustomAttribute(); if (metadata == null) { - return; + return this; } var name = GetActionName(actionType); @@ -136,6 +136,8 @@ public sealed class RuleTypeProvider : ITypeProvider } actionTypes[name] = definition; + + return this; } private static T? GetDataAttribute(PropertyInfo property) where T : ValidationAttribute diff --git a/backend/tests/Squidex.Domain.Apps.Core.Tests/Model/Rules/RuleTests.cs b/backend/tests/Squidex.Domain.Apps.Core.Tests/Model/Rules/RuleTests.cs index c47f97fb9..08489c090 100644 --- a/backend/tests/Squidex.Domain.Apps.Core.Tests/Model/Rules/RuleTests.cs +++ b/backend/tests/Squidex.Domain.Apps.Core.Tests/Model/Rules/RuleTests.cs @@ -5,6 +5,7 @@ // All rights reserved. Licensed under the MIT license. // ========================================================================== +using Squidex.Domain.Apps.Core.HandleRules; using Squidex.Domain.Apps.Core.Rules; using Squidex.Domain.Apps.Core.Rules.Triggers; using Squidex.Domain.Apps.Core.TestHelpers; @@ -46,11 +47,13 @@ public class RuleTests } } + [RuleAction] public sealed record TestAction1 : RuleAction { public string Property { get; set; } } + [RuleAction] public sealed record TestAction2 : RuleAction { public string Property { get; set; } diff --git a/backend/tests/Squidex.Domain.Apps.Core.Tests/Operations/HandleRules/RuleServiceTests.cs b/backend/tests/Squidex.Domain.Apps.Core.Tests/Operations/HandleRules/RuleServiceTests.cs index bf0ed47e4..33b27d89e 100644 --- a/backend/tests/Squidex.Domain.Apps.Core.Tests/Operations/HandleRules/RuleServiceTests.cs +++ b/backend/tests/Squidex.Domain.Apps.Core.Tests/Operations/HandleRules/RuleServiceTests.cs @@ -40,10 +40,12 @@ public class RuleServiceTests { } + [RuleAction] public sealed record InvalidAction : RuleAction { } + [RuleAction] public sealed record ValidAction : RuleAction { } diff --git a/backend/tests/Squidex.Domain.Apps.Entities.Tests/Rules/DomainObject/Guards/GuardRuleTests.cs b/backend/tests/Squidex.Domain.Apps.Entities.Tests/Rules/DomainObject/Guards/GuardRuleTests.cs index f6ffdbfe4..e96ef72fb 100644 --- a/backend/tests/Squidex.Domain.Apps.Entities.Tests/Rules/DomainObject/Guards/GuardRuleTests.cs +++ b/backend/tests/Squidex.Domain.Apps.Entities.Tests/Rules/DomainObject/Guards/GuardRuleTests.cs @@ -5,6 +5,7 @@ // All rights reserved. Licensed under the MIT license. // ========================================================================== +using Squidex.Domain.Apps.Core.HandleRules; using Squidex.Domain.Apps.Core.Rules; using Squidex.Domain.Apps.Core.Rules.Triggers; using Squidex.Domain.Apps.Core.TestHelpers; @@ -23,6 +24,7 @@ public class GuardRuleTests : IClassFixture private readonly NamedId schemaId = NamedId.Of(DomainId.NewGuid(), "my-schema"); private readonly IAppProvider appProvider = A.Fake(); + [RuleAction] public sealed record TestAction : RuleAction { public Uri Url { get; set; } diff --git a/backend/tests/Squidex.Domain.Apps.Entities.Tests/Rules/DomainObject/RuleDomainObjectTests.cs b/backend/tests/Squidex.Domain.Apps.Entities.Tests/Rules/DomainObject/RuleDomainObjectTests.cs index f1cb7283d..94d4c6391 100644 --- a/backend/tests/Squidex.Domain.Apps.Entities.Tests/Rules/DomainObject/RuleDomainObjectTests.cs +++ b/backend/tests/Squidex.Domain.Apps.Entities.Tests/Rules/DomainObject/RuleDomainObjectTests.cs @@ -7,6 +7,7 @@ using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; +using Squidex.Domain.Apps.Core.HandleRules; using Squidex.Domain.Apps.Core.Rules; using Squidex.Domain.Apps.Core.Rules.Triggers; using Squidex.Domain.Apps.Entities.Rules.Commands; @@ -29,6 +30,7 @@ public class RuleDomainObjectTests : HandlerTestBase get => DomainId.Combine(AppId, ruleId); } + [RuleAction] public sealed record TestAction : RuleAction { public int Value { get; set; } diff --git a/backend/tests/Squidex.Domain.Apps.Entities.Tests/Rules/DomainObject/RuleState.json b/backend/tests/Squidex.Domain.Apps.Entities.Tests/Rules/DomainObject/RuleState.json new file mode 100644 index 000000000..b4827b1e2 --- /dev/null +++ b/backend/tests/Squidex.Domain.Apps.Entities.Tests/Rules/DomainObject/RuleState.json @@ -0,0 +1,20 @@ +{ + "appId": "06c68527-a2ca-4d84-91eb-a1707aa5b6fb,hotels", + "ruleDef": { + "trigger": { + "triggerType": "ManualTrigger" + }, + "action": { + "actionType": "Webhook", + "url": "http://squidex.io" + }, + "isEnabled": true + }, + "isDeleted": false, + "id": "265aaf1e-5dff-4c40-b1e6-1149f652cd30", + "createdBy": "subject:63761585e06d5466c71521b3", + "lastModifiedBy": "subject:63761585e06d5466c71521b3", + "created": "2022-12-05T11:00:23Z", + "lastModified": "2022-12-05T11:00:23Z", + "version": 0 +} diff --git a/backend/tests/Squidex.Domain.Apps.Entities.Tests/Rules/DomainObject/RuleStateTests.cs b/backend/tests/Squidex.Domain.Apps.Entities.Tests/Rules/DomainObject/RuleStateTests.cs new file mode 100644 index 000000000..faa88be8e --- /dev/null +++ b/backend/tests/Squidex.Domain.Apps.Entities.Tests/Rules/DomainObject/RuleStateTests.cs @@ -0,0 +1,63 @@ +// ========================================================================== +// Squidex Headless CMS +// ========================================================================== +// Copyright (c) Squidex UG (haftungsbeschraenkt) +// All rights reserved. Licensed under the MIT license. +// ========================================================================== + +using System.Text.Json.Serialization; +using Squidex.Domain.Apps.Core.HandleRules; +using Squidex.Domain.Apps.Core.Rules; +using Squidex.Domain.Apps.Core.TestHelpers; +using Squidex.Infrastructure.Json; + +namespace Squidex.Domain.Apps.Entities.Rules.DomainObject; + +public class RuleStateTests +{ + private readonly IJsonSerializer serializer = TestUtils.CreateSerializer(options => + { + options.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull; + }); + + [RuleAction] + public sealed record WebhookAction : RuleAction + { + public Uri Url { get; set; } + } + + static RuleStateTests() + { + TestUtils.TypeRegistry.Map(new RuleTypeProvider().Add()); + } + + [Fact] + public void Should_deserialize_state() + { + var json = File.ReadAllText("Rules/DomainObject/RuleState.json"); + + var deserialized = serializer.Deserialize(json); + + Assert.NotNull(deserialized); + } + + [Fact] + public void Should_deserialize_state_from_old_representation() + { + var json = File.ReadAllText("Rules/DomainObject/RuleState_Old.json"); + + var deserialized = serializer.Deserialize(json); + + Assert.NotNull(deserialized); + } + + [Fact] + public void Should_serialize_deserialize_state() + { + var json = File.ReadAllText("Rules/DomainObject/RuleState.json").CleanJson(); + + var serialized = serializer.Serialize(serializer.Deserialize(json), true); + + Assert.Equal(json, serialized); + } +} diff --git a/backend/tests/Squidex.Domain.Apps.Entities.Tests/Rules/DomainObject/RuleState_Old.json b/backend/tests/Squidex.Domain.Apps.Entities.Tests/Rules/DomainObject/RuleState_Old.json new file mode 100644 index 000000000..65fa5c9b3 --- /dev/null +++ b/backend/tests/Squidex.Domain.Apps.Entities.Tests/Rules/DomainObject/RuleState_Old.json @@ -0,0 +1,20 @@ +{ + "appId": "06c68527-a2ca-4d84-91eb-a1707aa5b6fb,hotels", + "ruleDef": { + "trigger": { + "$type": "ManualTrigger" + }, + "action": { + "$type": "WebhookAction", + "url": "http://squidex.io" + }, + "isEnabled": true + }, + "isDeleted": false, + "id": "265aaf1e-5dff-4c40-b1e6-1149f652cd30", + "createdBy": "subject:63761585e06d5466c71521b3", + "lastModifiedBy": "subject:63761585e06d5466c71521b3", + "created": "2022-12-05T11:00:23Z", + "lastModified": "2022-12-05T11:00:23Z", + "version": 0 +} \ No newline at end of file diff --git a/backend/tests/Squidex.Domain.Apps.Entities.Tests/Rules/RuleEnqueuerTests.cs b/backend/tests/Squidex.Domain.Apps.Entities.Tests/Rules/RuleEnqueuerTests.cs index d19f6b6fa..a7aa9d216 100644 --- a/backend/tests/Squidex.Domain.Apps.Entities.Tests/Rules/RuleEnqueuerTests.cs +++ b/backend/tests/Squidex.Domain.Apps.Entities.Tests/Rules/RuleEnqueuerTests.cs @@ -31,6 +31,7 @@ public class RuleEnqueuerTests private readonly NamedId appId = NamedId.Of(DomainId.NewGuid(), "my-app"); private readonly RuleEnqueuer sut; + [RuleAction] public sealed record TestAction : RuleAction { public Uri Url { get; set; } diff --git a/backend/tests/Squidex.Domain.Apps.Entities.Tests/Squidex.Domain.Apps.Entities.Tests.csproj b/backend/tests/Squidex.Domain.Apps.Entities.Tests/Squidex.Domain.Apps.Entities.Tests.csproj index 378a9547b..d3dd6bcea 100644 --- a/backend/tests/Squidex.Domain.Apps.Entities.Tests/Squidex.Domain.Apps.Entities.Tests.csproj +++ b/backend/tests/Squidex.Domain.Apps.Entities.Tests/Squidex.Domain.Apps.Entities.Tests.csproj @@ -79,11 +79,14 @@ PreserveNewest + + PreserveNewest + PreserveNewest - - - + + PreserveNewest + \ No newline at end of file