mirror of https://github.com/Squidex/squidex.git
13 changed files with 534 additions and 6 deletions
@ -0,0 +1,28 @@ |
|||
// ==========================================================================
|
|||
// AddPattern.cs
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex Group
|
|||
// All rights reserved.
|
|||
// ==========================================================================
|
|||
|
|||
using System; |
|||
|
|||
namespace Squidex.Domain.Apps.Entities.Apps.Commands |
|||
{ |
|||
public sealed class AddPattern : AppAggregateCommand |
|||
{ |
|||
public Guid Id { get; set; } |
|||
|
|||
public string Name { get; set; } |
|||
|
|||
public string Pattern { get; set; } |
|||
|
|||
public string DefaultMessage { get; set; } |
|||
|
|||
public AddPattern() |
|||
{ |
|||
Id = Guid.NewGuid(); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,17 @@ |
|||
// ==========================================================================
|
|||
// DeletePattern.cs
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex Group
|
|||
// All rights reserved.
|
|||
// ==========================================================================
|
|||
|
|||
using System; |
|||
|
|||
namespace Squidex.Domain.Apps.Entities.Apps.Commands |
|||
{ |
|||
public sealed class DeletePattern : AppAggregateCommand |
|||
{ |
|||
public Guid Id { get; set; } |
|||
} |
|||
} |
|||
@ -0,0 +1,23 @@ |
|||
// ==========================================================================
|
|||
// UpdatePattern.cs
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex Group
|
|||
// All rights reserved.
|
|||
// ==========================================================================
|
|||
|
|||
using System; |
|||
|
|||
namespace Squidex.Domain.Apps.Entities.Apps.Commands |
|||
{ |
|||
public sealed class UpdatePattern : AppAggregateCommand |
|||
{ |
|||
public Guid Id { get; set; } |
|||
|
|||
public string Name { get; set; } |
|||
|
|||
public string Pattern { get; set; } |
|||
|
|||
public string DefaultMessage { get; set; } |
|||
} |
|||
} |
|||
@ -0,0 +1,97 @@ |
|||
// ==========================================================================
|
|||
// GuardAppPattern.cs
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex Group
|
|||
// All rights reserved.
|
|||
// ==========================================================================
|
|||
using System; |
|||
using System.Linq; |
|||
using Squidex.Domain.Apps.Core.Apps; |
|||
using Squidex.Domain.Apps.Entities.Apps.Commands; |
|||
using Squidex.Infrastructure; |
|||
|
|||
namespace Squidex.Domain.Apps.Entities.Apps.Guards |
|||
{ |
|||
public static class GuardAppPattern |
|||
{ |
|||
public static void CanAdd(AppPatterns patterns, AddPattern command) |
|||
{ |
|||
Guard.NotNull(command, nameof(command)); |
|||
|
|||
Validate.It(() => "Cannot add pattern.", error => |
|||
{ |
|||
if (string.IsNullOrWhiteSpace(command.Name)) |
|||
{ |
|||
error(new ValidationError("Pattern name can not be empty.", nameof(command.Name))); |
|||
} |
|||
|
|||
if (patterns.Values.Any(x => x.Name.Equals(command.Name, StringComparison.OrdinalIgnoreCase))) |
|||
{ |
|||
error(new ValidationError("Pattern name is already assigned.", nameof(command.Name))); |
|||
} |
|||
|
|||
if (string.IsNullOrWhiteSpace(command.Pattern)) |
|||
{ |
|||
error(new ValidationError("Pattern can not be empty.", nameof(command.Pattern))); |
|||
} |
|||
else if (!command.Pattern.IsValidRegex()) |
|||
{ |
|||
error(new ValidationError("Pattern is not a valid regular expression.", nameof(command.Pattern))); |
|||
} |
|||
|
|||
if (patterns.Values.Any(x => x.Pattern == command.Pattern)) |
|||
{ |
|||
error(new ValidationError("Pattern already exists.", nameof(command.Pattern))); |
|||
} |
|||
}); |
|||
} |
|||
|
|||
public static void CanDelete(AppPatterns patterns, DeletePattern command) |
|||
{ |
|||
Guard.NotNull(command, nameof(command)); |
|||
|
|||
if (!patterns.ContainsKey(command.Id)) |
|||
{ |
|||
throw new DomainObjectNotFoundException(command.Id.ToString(), typeof(AppPattern)); |
|||
} |
|||
} |
|||
|
|||
public static void CanUpdate(AppPatterns patterns, UpdatePattern command) |
|||
{ |
|||
Guard.NotNull(command, nameof(command)); |
|||
|
|||
if (!patterns.ContainsKey(command.Id)) |
|||
{ |
|||
throw new DomainObjectNotFoundException(command.Id.ToString(), typeof(AppPattern)); |
|||
} |
|||
|
|||
Validate.It(() => "Cannot update pattern.", error => |
|||
{ |
|||
if (string.IsNullOrWhiteSpace(command.Name)) |
|||
{ |
|||
error(new ValidationError("Pattern name can not be empty.", nameof(command.Name))); |
|||
} |
|||
|
|||
if (patterns.Any(x => x.Key != command.Id && x.Value.Name.Equals(command.Name, StringComparison.OrdinalIgnoreCase))) |
|||
{ |
|||
error(new ValidationError("Pattern name is already assigned.", nameof(command.Name))); |
|||
} |
|||
|
|||
if (string.IsNullOrWhiteSpace(command.Pattern)) |
|||
{ |
|||
error(new ValidationError("Pattern can not be empty.", nameof(command.Pattern))); |
|||
} |
|||
else if (!command.Pattern.IsValidRegex()) |
|||
{ |
|||
error(new ValidationError("Pattern is not a valid regular expression.", nameof(command.Pattern))); |
|||
} |
|||
|
|||
if (patterns.Any(x => x.Key != command.Id && x.Value.Pattern == command.Pattern)) |
|||
{ |
|||
error(new ValidationError("Pattern already exists.", nameof(command.Pattern))); |
|||
} |
|||
}); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,169 @@ |
|||
// ==========================================================================
|
|||
// GuardAppPatternsTests.cs
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex Group
|
|||
// All rights reserved.
|
|||
// ==========================================================================
|
|||
|
|||
using System; |
|||
using Squidex.Domain.Apps.Core.Apps; |
|||
using Squidex.Domain.Apps.Entities.Apps.Commands; |
|||
using Squidex.Domain.Apps.Entities.Apps.Guards; |
|||
using Squidex.Infrastructure; |
|||
using Xunit; |
|||
|
|||
#pragma warning disable SA1310 // Field names must not contain underscore
|
|||
|
|||
namespace Squidex.Domain.Apps.Write.Apps.Guards |
|||
{ |
|||
public class GuardAppPatternsTests |
|||
{ |
|||
private readonly Guid patternId = Guid.NewGuid(); |
|||
private readonly AppPatterns patterns_0 = AppPatterns.Empty; |
|||
|
|||
[Fact] |
|||
public void CanAdd_should_throw_exception_if_name_empty() |
|||
{ |
|||
var command = new AddPattern { Id = patternId, Name = string.Empty, Pattern = ".*" }; |
|||
|
|||
Assert.Throws<ValidationException>(() => GuardAppPattern.CanAdd(patterns_0, command)); |
|||
} |
|||
|
|||
[Fact] |
|||
public void CanAdd_should_throw_exception_if_id_empty_guid() |
|||
{ |
|||
var command = new AddPattern { Name = string.Empty, Pattern = ".*" }; |
|||
|
|||
Assert.Throws<ValidationException>(() => GuardAppPattern.CanAdd(patterns_0, command)); |
|||
} |
|||
|
|||
[Fact] |
|||
public void CanAdd_should_throw_exception_if_pattern_empty() |
|||
{ |
|||
var command = new AddPattern { Id = patternId, Name = "any", Pattern = string.Empty }; |
|||
|
|||
Assert.Throws<ValidationException>(() => GuardAppPattern.CanAdd(patterns_0, command)); |
|||
} |
|||
|
|||
[Fact] |
|||
public void CanAdd_should_throw_exception_if_pattern_not_valid() |
|||
{ |
|||
var command = new AddPattern { Id = patternId, Name = "any", Pattern = "[0-9{1}" }; |
|||
|
|||
Assert.Throws<ValidationException>(() => GuardAppPattern.CanAdd(patterns_0, command)); |
|||
} |
|||
|
|||
[Fact] |
|||
public void CanAdd_should_throw_exception_if_name_exists() |
|||
{ |
|||
var patterns_1 = patterns_0.Add(Guid.NewGuid(), "any", "[a-z]", "Message"); |
|||
|
|||
var command = new AddPattern { Id = patternId, Name = "any", Pattern = ".*" }; |
|||
|
|||
Assert.Throws<ValidationException>(() => GuardAppPattern.CanAdd(patterns_1, command)); |
|||
} |
|||
|
|||
[Fact] |
|||
public void CanAdd_should_not_throw_exception_if_success() |
|||
{ |
|||
var command = new AddPattern { Id = patternId, Name = "any", Pattern = ".*" }; |
|||
|
|||
GuardAppPattern.CanAdd(patterns_0, command); |
|||
} |
|||
|
|||
[Fact] |
|||
public void CanDelete_should_throw_exception_if_pattern_not_found() |
|||
{ |
|||
var command = new DeletePattern { Id = patternId }; |
|||
|
|||
Assert.Throws<DomainObjectNotFoundException>(() => GuardAppPattern.CanDelete(patterns_0, command)); |
|||
} |
|||
|
|||
[Fact] |
|||
public void CanDelete_should_not_throw_exception_if_success() |
|||
{ |
|||
var patterns_1 = patterns_0.Add(patternId, "any", ".*", "Message"); |
|||
|
|||
var command = new DeletePattern { Id = patternId }; |
|||
|
|||
GuardAppPattern.CanDelete(patterns_1, command); |
|||
} |
|||
|
|||
[Fact] |
|||
public void CanUpdate_should_throw_exception_if_name_empty() |
|||
{ |
|||
var patterns_1 = patterns_0.Add(patternId, "any", ".*", "Message"); |
|||
|
|||
var command = new UpdatePattern { Id = patternId, Name = string.Empty, Pattern = ".*" }; |
|||
|
|||
Assert.Throws<ValidationException>(() => GuardAppPattern.CanUpdate(patterns_1, command)); |
|||
} |
|||
|
|||
[Fact] |
|||
public void CanUpdate_should_throw_exception_if_pattern_empty() |
|||
{ |
|||
var patterns_1 = patterns_0.Add(patternId, "any", ".*", "Message"); |
|||
|
|||
var command = new UpdatePattern { Id = patternId, Name = "any", Pattern = string.Empty }; |
|||
|
|||
Assert.Throws<ValidationException>(() => GuardAppPattern.CanUpdate(patterns_1, command)); |
|||
} |
|||
|
|||
[Fact] |
|||
public void CanUpdate_should_throw_exception_if_pattern_not_valid() |
|||
{ |
|||
var patterns_1 = patterns_0.Add(patternId, "any", ".*", "Message"); |
|||
|
|||
var command = new UpdatePattern { Id = patternId, Name = "any", Pattern = "[0-9{1}" }; |
|||
|
|||
Assert.Throws<ValidationException>(() => GuardAppPattern.CanUpdate(patterns_1, command)); |
|||
} |
|||
|
|||
[Fact] |
|||
public void CanUpdate_should_throw_exception_if_name_exists() |
|||
{ |
|||
var id1 = Guid.NewGuid(); |
|||
var id2 = Guid.NewGuid(); |
|||
|
|||
var patterns_1 = patterns_0.Add(id1, "Pattern1", "[0-5]", "Message"); |
|||
var patterns_2 = patterns_1.Add(id2, "Pattern2", "[0-4]", "Message"); |
|||
|
|||
var command = new UpdatePattern { Id = id2, Name = "Pattern1", Pattern = "[0-4]" }; |
|||
|
|||
Assert.Throws<ValidationException>(() => GuardAppPattern.CanUpdate(patterns_2, command)); |
|||
} |
|||
|
|||
[Fact] |
|||
public void CanUpdate_should_throw_exception_if_pattern_exists() |
|||
{ |
|||
var id1 = Guid.NewGuid(); |
|||
var id2 = Guid.NewGuid(); |
|||
|
|||
var patterns_1 = patterns_0.Add(id1, "Pattern1", "[0-5]", "Message"); |
|||
var patterns_2 = patterns_1.Add(id2, "Pattern2", "[0-4]", "Message"); |
|||
|
|||
var command = new UpdatePattern { Id = id2, Name = "Pattern2", Pattern = "[0-5]" }; |
|||
|
|||
Assert.Throws<ValidationException>(() => GuardAppPattern.CanUpdate(patterns_2, command)); |
|||
} |
|||
|
|||
[Fact] |
|||
public void CanUpdate_should_throw_exception_if_pattern_does_not_exists() |
|||
{ |
|||
var command = new UpdatePattern { Id = patternId, Name = "Pattern1", Pattern = ".*" }; |
|||
|
|||
Assert.Throws<DomainObjectNotFoundException>(() => GuardAppPattern.CanUpdate(patterns_0, command)); |
|||
} |
|||
|
|||
[Fact] |
|||
public void CanUpdate_should_not_throw_exception_if_pattern_exist_with_valid_command() |
|||
{ |
|||
var patterns_1 = patterns_0.Add(patternId, "any", ".*", "Message"); |
|||
|
|||
var command = new UpdatePattern { Id = patternId, Name = "Pattern1", Pattern = ".*" }; |
|||
|
|||
GuardAppPattern.CanUpdate(patterns_1, command); |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue