mirror of https://github.com/Squidex/squidex.git
11 changed files with 195 additions and 22 deletions
@ -0,0 +1,46 @@ |
|||||
|
// ==========================================================================
|
||||
|
// JsonRule.cs
|
||||
|
// Squidex Headless CMS
|
||||
|
// ==========================================================================
|
||||
|
// Copyright (c) Squidex Group
|
||||
|
// All rights reserved.
|
||||
|
// ==========================================================================
|
||||
|
|
||||
|
using Newtonsoft.Json; |
||||
|
using Squidex.Infrastructure.Reflection; |
||||
|
|
||||
|
namespace Squidex.Domain.Apps.Core.Rules.Json |
||||
|
{ |
||||
|
public sealed class JsonRule |
||||
|
{ |
||||
|
[JsonProperty] |
||||
|
public RuleTrigger Trigger { get; set; } |
||||
|
|
||||
|
[JsonProperty] |
||||
|
public RuleAction Action { get; set; } |
||||
|
|
||||
|
[JsonProperty] |
||||
|
public bool IsEnabled { get; set; } |
||||
|
|
||||
|
public JsonRule() |
||||
|
{ |
||||
|
} |
||||
|
|
||||
|
public JsonRule(Rule rule) |
||||
|
{ |
||||
|
SimpleMapper.Map(rule, this); |
||||
|
} |
||||
|
|
||||
|
public Rule ToRule() |
||||
|
{ |
||||
|
var rule = new Rule(Trigger, Action); |
||||
|
|
||||
|
if (!IsEnabled) |
||||
|
{ |
||||
|
rule.Disable(); |
||||
|
} |
||||
|
|
||||
|
return rule; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,27 @@ |
|||||
|
// ==========================================================================
|
||||
|
// RuleConverter.cs
|
||||
|
// Squidex Headless CMS
|
||||
|
// ==========================================================================
|
||||
|
// Copyright (c) Squidex Group
|
||||
|
// All rights reserved.
|
||||
|
// ==========================================================================
|
||||
|
|
||||
|
using System; |
||||
|
using Newtonsoft.Json; |
||||
|
using Squidex.Infrastructure.Json; |
||||
|
|
||||
|
namespace Squidex.Domain.Apps.Core.Rules.Json |
||||
|
{ |
||||
|
public sealed class RuleConverter : JsonClassConverter<Rule> |
||||
|
{ |
||||
|
protected override Rule ReadValue(JsonReader reader, JsonSerializer serializer) |
||||
|
{ |
||||
|
return serializer.Deserialize<JsonRule>(reader).ToRule(); |
||||
|
} |
||||
|
|
||||
|
protected override void WriteValue(JsonWriter writer, Rule value, JsonSerializer serializer) |
||||
|
{ |
||||
|
serializer.Serialize(writer, new JsonRule(value)); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,113 @@ |
|||||
|
// ==========================================================================
|
||||
|
// RuleTests.cs
|
||||
|
// Squidex Headless CMS
|
||||
|
// ==========================================================================
|
||||
|
// Copyright (c) Squidex Group
|
||||
|
// All rights reserved.
|
||||
|
// ==========================================================================
|
||||
|
|
||||
|
using System; |
||||
|
using FluentAssertions; |
||||
|
using Newtonsoft.Json; |
||||
|
using Newtonsoft.Json.Linq; |
||||
|
using Squidex.Domain.Apps.Core.Rules; |
||||
|
using Squidex.Domain.Apps.Core.Rules.Actions; |
||||
|
using Squidex.Domain.Apps.Core.Rules.Triggers; |
||||
|
using Xunit; |
||||
|
|
||||
|
namespace Squidex.Domain.Apps.Core.Model.Rules |
||||
|
{ |
||||
|
public class RuleTests |
||||
|
{ |
||||
|
private readonly JsonSerializer serializer = TestData.DefaultSerializer(); |
||||
|
private readonly Rule sut = new Rule(new ContentChangedTrigger(), new WebhookAction()); |
||||
|
|
||||
|
public sealed class OtherTrigger : RuleTrigger |
||||
|
{ |
||||
|
public override T Accept<T>(IRuleTriggerVisitor<T> visitor) |
||||
|
{ |
||||
|
throw new NotSupportedException(); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
public sealed class OtherAction : RuleAction |
||||
|
{ |
||||
|
public override T Accept<T>(IRuleActionVisitor<T> visitor) |
||||
|
{ |
||||
|
throw new NotSupportedException(); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void Should_create_with_trigger_and_action() |
||||
|
{ |
||||
|
var ruleTrigger = new ContentChangedTrigger(); |
||||
|
var ruleAction = new WebhookAction(); |
||||
|
|
||||
|
var newRule = new Rule(ruleTrigger, ruleAction); |
||||
|
|
||||
|
Assert.Equal(ruleTrigger, newRule.Trigger); |
||||
|
Assert.Equal(ruleAction, newRule.Action); |
||||
|
Assert.True(newRule.IsEnabled); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void Should_set_enabled_to_true_when_enabling() |
||||
|
{ |
||||
|
sut.Enable(); |
||||
|
|
||||
|
Assert.True(sut.IsEnabled); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void Should_set_enabled_to_false_when_disabling() |
||||
|
{ |
||||
|
sut.Enable(); |
||||
|
sut.Disable(); |
||||
|
|
||||
|
Assert.False(sut.IsEnabled); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void Should_replace_trigger_when_updating() |
||||
|
{ |
||||
|
var newTrigger = new ContentChangedTrigger(); |
||||
|
|
||||
|
sut.Update(newTrigger); |
||||
|
|
||||
|
Assert.Same(newTrigger, sut.Trigger); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void Should_throw_exception_when_new_trigger_has_other_type() |
||||
|
{ |
||||
|
Assert.Throws<ArgumentException>(() => sut.Update(new OtherTrigger())); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void Should_replace_action_when_updating() |
||||
|
{ |
||||
|
var newAction = new WebhookAction(); |
||||
|
|
||||
|
sut.Update(newAction); |
||||
|
|
||||
|
Assert.Same(newAction, sut.Action); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void Should_throw_exception_when_new_action_has_other_type() |
||||
|
{ |
||||
|
Assert.Throws<ArgumentException>(() => sut.Update(new OtherAction())); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void Should_serialize_and_deserialize() |
||||
|
{ |
||||
|
sut.Disable(); |
||||
|
|
||||
|
var appClients = JToken.FromObject(sut, serializer).ToObject<Rule>(serializer); |
||||
|
|
||||
|
appClients.ShouldBeEquivalentTo(sut); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue