Browse Source

Tests for serialization of rule state.

pull/959/head
Sebastian 3 years ago
parent
commit
27455f2439
  1. 10
      backend/src/Squidex.Domain.Apps.Core.Operations/HandleRules/RuleTypeProvider.cs
  2. 3
      backend/tests/Squidex.Domain.Apps.Core.Tests/Model/Rules/RuleTests.cs
  3. 2
      backend/tests/Squidex.Domain.Apps.Core.Tests/Operations/HandleRules/RuleServiceTests.cs
  4. 2
      backend/tests/Squidex.Domain.Apps.Entities.Tests/Rules/DomainObject/Guards/GuardRuleTests.cs
  5. 2
      backend/tests/Squidex.Domain.Apps.Entities.Tests/Rules/DomainObject/RuleDomainObjectTests.cs
  6. 20
      backend/tests/Squidex.Domain.Apps.Entities.Tests/Rules/DomainObject/RuleState.json
  7. 63
      backend/tests/Squidex.Domain.Apps.Entities.Tests/Rules/DomainObject/RuleStateTests.cs
  8. 20
      backend/tests/Squidex.Domain.Apps.Entities.Tests/Rules/DomainObject/RuleState_Old.json
  9. 1
      backend/tests/Squidex.Domain.Apps.Entities.Tests/Rules/RuleEnqueuerTests.cs
  10. 9
      backend/tests/Squidex.Domain.Apps.Entities.Tests/Squidex.Domain.Apps.Entities.Tests.csproj

10
backend/src/Squidex.Domain.Apps.Core.Operations/HandleRules/RuleTypeProvider.cs

@ -38,18 +38,18 @@ public sealed class RuleTypeProvider : ITypeProvider
}
}
public void Add<T>() where T : RuleAction
public RuleTypeProvider Add<T>() where T : RuleAction
{
Add(typeof(T));
return Add(typeof(T));
}
private void Add(Type actionType)
private RuleTypeProvider Add(Type actionType)
{
var metadata = actionType.GetCustomAttribute<RuleActionAttribute>();
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<T>(PropertyInfo property) where T : ValidationAttribute

3
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; }

2
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
{
}

2
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<TranslationsFixture>
private readonly NamedId<DomainId> schemaId = NamedId.Of(DomainId.NewGuid(), "my-schema");
private readonly IAppProvider appProvider = A.Fake<IAppProvider>();
[RuleAction]
public sealed record TestAction : RuleAction
{
public Uri Url { get; set; }

2
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<RuleDomainObject.State>
get => DomainId.Combine(AppId, ruleId);
}
[RuleAction]
public sealed record TestAction : RuleAction
{
public int Value { get; set; }

20
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
}

63
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<WebhookAction>());
}
[Fact]
public void Should_deserialize_state()
{
var json = File.ReadAllText("Rules/DomainObject/RuleState.json");
var deserialized = serializer.Deserialize<RuleDomainObject.State>(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<RuleDomainObject.State>(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<RuleDomainObject.State>(json), true);
Assert.Equal(json, serialized);
}
}

20
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
}

1
backend/tests/Squidex.Domain.Apps.Entities.Tests/Rules/RuleEnqueuerTests.cs

@ -31,6 +31,7 @@ public class RuleEnqueuerTests
private readonly NamedId<DomainId> appId = NamedId.Of(DomainId.NewGuid(), "my-app");
private readonly RuleEnqueuer sut;
[RuleAction]
public sealed record TestAction : RuleAction
{
public Uri Url { get; set; }

9
backend/tests/Squidex.Domain.Apps.Entities.Tests/Squidex.Domain.Apps.Entities.Tests.csproj

@ -79,11 +79,14 @@
<None Update="Assets\TestFiles\SampleVideo_1280x720_1mb.mp4">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Rules\DomainObject\RuleState_Old.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Schemas\DomainObject\SchemaState.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
<ItemGroup>
<Folder Include="Apps\Invitation\" />
<None Update="Rules\DomainObject\RuleState.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>
Loading…
Cancel
Save