mirror of https://github.com/Squidex/squidex.git
10 changed files with 394 additions and 0 deletions
@ -0,0 +1,62 @@ |
|||
// ==========================================================================
|
|||
// AppPattern.cs
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex Group
|
|||
// All rights reserved.
|
|||
// ==========================================================================
|
|||
|
|||
using System; |
|||
using System.Diagnostics.Contracts; |
|||
using Squidex.Infrastructure; |
|||
|
|||
namespace Squidex.Domain.Apps.Core.Apps |
|||
{ |
|||
public class AppPattern |
|||
{ |
|||
private readonly Guid id; |
|||
private readonly string name; |
|||
private readonly string pattern; |
|||
private readonly string defaultMessage; |
|||
|
|||
public Guid Id |
|||
{ |
|||
get { return id; } |
|||
} |
|||
|
|||
public string Name |
|||
{ |
|||
get { return name; } |
|||
} |
|||
|
|||
public string Pattern |
|||
{ |
|||
get { return pattern; } |
|||
} |
|||
|
|||
public string DefaultMessage |
|||
{ |
|||
get { return defaultMessage; } |
|||
} |
|||
|
|||
public AppPattern(Guid id, string name, string pattern, string defaultMessage) |
|||
{ |
|||
Guard.NotNullOrEmpty(name, nameof(name)); |
|||
Guard.NotNullOrEmpty(pattern, nameof(pattern)); |
|||
|
|||
this.id = id; |
|||
this.name = name; |
|||
this.pattern = pattern; |
|||
this.defaultMessage = defaultMessage; |
|||
} |
|||
|
|||
[Pure] |
|||
public AppPattern Update(string name, string pattern, string defaultMessage) |
|||
{ |
|||
Guard.NotNullOrEmpty(name, nameof(name)); |
|||
Guard.NotNullOrEmpty(pattern, nameof(pattern)); |
|||
|
|||
return new AppPattern(this.id, name, pattern, defaultMessage); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,57 @@ |
|||
// ==========================================================================
|
|||
// AppPatterns.cs
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex Group
|
|||
// All rights reserved.
|
|||
// ==========================================================================
|
|||
using System; |
|||
using System.Collections.Immutable; |
|||
using System.Diagnostics.Contracts; |
|||
using Squidex.Infrastructure; |
|||
|
|||
namespace Squidex.Domain.Apps.Core.Apps |
|||
{ |
|||
public sealed class AppPatterns : DictionaryWrapper<Guid, AppPattern> |
|||
{ |
|||
public static readonly AppPatterns Empty = new AppPatterns(); |
|||
|
|||
private AppPatterns() |
|||
: base(ImmutableDictionary<Guid, AppPattern>.Empty) |
|||
{ |
|||
} |
|||
|
|||
public AppPatterns(ImmutableDictionary<Guid, AppPattern> inner) |
|||
: base(inner) |
|||
{ |
|||
} |
|||
|
|||
[Pure] |
|||
public AppPatterns Add(Guid id, string name, string pattern, string defaultMessage) |
|||
{ |
|||
var newPattern = new AppPattern(id, name, pattern, defaultMessage); |
|||
|
|||
return new AppPatterns(Inner.Add(id, newPattern)); |
|||
} |
|||
|
|||
[Pure] |
|||
public AppPatterns Remove(Guid id) |
|||
{ |
|||
return new AppPatterns(Inner.Remove(id)); |
|||
} |
|||
|
|||
[Pure] |
|||
public AppPatterns Update(Guid id, string name, string pattern, string defaultMessage) |
|||
{ |
|||
Guard.NotNullOrEmpty(name, nameof(name)); |
|||
Guard.NotNullOrEmpty(pattern, nameof(pattern)); |
|||
|
|||
if (!TryGetValue(id, out var appPattern)) |
|||
{ |
|||
return this; |
|||
} |
|||
|
|||
return new AppPatterns(Inner.SetItem(id, appPattern.Update(name, pattern, defaultMessage))); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,37 @@ |
|||
// ==========================================================================
|
|||
// AppPatternsConverter.cs
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex Group
|
|||
// All rights reserved.
|
|||
// ==========================================================================
|
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Collections.Immutable; |
|||
using Newtonsoft.Json; |
|||
using Squidex.Infrastructure.Json; |
|||
|
|||
namespace Squidex.Domain.Apps.Core.Apps.Json |
|||
{ |
|||
public sealed class AppPatternsConverter : JsonClassConverter<AppPatterns> |
|||
{ |
|||
protected override void WriteValue(JsonWriter writer, AppPatterns value, JsonSerializer serializer) |
|||
{ |
|||
var json = new Dictionary<Guid, JsonAppPattern>(value.Count); |
|||
|
|||
foreach (var client in value) |
|||
{ |
|||
json.Add(client.Key, new JsonAppPattern(client.Value)); |
|||
} |
|||
|
|||
serializer.Serialize(writer, json); |
|||
} |
|||
|
|||
protected override AppPatterns ReadValue(JsonReader reader, Type objectType, JsonSerializer serializer) |
|||
{ |
|||
var json = serializer.Deserialize<Dictionary<Guid, JsonAppPattern>>(reader); |
|||
|
|||
return new AppPatterns(json.ToImmutableDictionary(x => x.Key, x => x.Value.ToPattern())); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,42 @@ |
|||
// ==========================================================================
|
|||
// JsonAppPattern.cs
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex Group
|
|||
// All rights reserved.
|
|||
// ==========================================================================
|
|||
using System; |
|||
using Newtonsoft.Json; |
|||
using Squidex.Infrastructure.Reflection; |
|||
|
|||
namespace Squidex.Domain.Apps.Core.Apps.Json |
|||
{ |
|||
public class JsonAppPattern |
|||
{ |
|||
[JsonProperty] |
|||
public Guid Id { get; set; } |
|||
|
|||
[JsonProperty] |
|||
public string Name { get; set; } |
|||
|
|||
[JsonProperty] |
|||
public string Pattern { get; set; } |
|||
|
|||
[JsonProperty] |
|||
public string DefaultMessage { get; set; } |
|||
|
|||
public JsonAppPattern() |
|||
{ |
|||
} |
|||
|
|||
public JsonAppPattern(AppPattern pattern) |
|||
{ |
|||
SimpleMapper.Map(pattern, this); |
|||
} |
|||
|
|||
public AppPattern ToPattern() |
|||
{ |
|||
return new AppPattern(Id, Name, Pattern, DefaultMessage); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,25 @@ |
|||
// ==========================================================================
|
|||
// AppPatternAdded.cs
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex Group
|
|||
// All rights reserved.
|
|||
// ==========================================================================
|
|||
|
|||
using System; |
|||
using Squidex.Infrastructure.EventSourcing; |
|||
|
|||
namespace Squidex.Domain.Apps.Events.Apps |
|||
{ |
|||
[EventType(nameof(AppPatternAdded))] |
|||
public sealed class AppPatternAdded : AppEvent |
|||
{ |
|||
public Guid Id { get; set; } |
|||
|
|||
public string Name { get; set; } |
|||
|
|||
public string Pattern { get; set; } |
|||
|
|||
public string DefaultMessage { get; set; } |
|||
} |
|||
} |
|||
@ -0,0 +1,21 @@ |
|||
// ==========================================================================
|
|||
// AppPatternDeleted.cs
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex Group
|
|||
// All rights reserved.
|
|||
// ==========================================================================
|
|||
|
|||
using System; |
|||
using Squidex.Infrastructure.EventSourcing; |
|||
|
|||
namespace Squidex.Domain.Apps.Events.Apps |
|||
{ |
|||
[EventType(nameof(AppPatternDeleted))] |
|||
public sealed class AppPatternDeleted : AppEvent |
|||
{ |
|||
public Guid Id { get; set; } |
|||
|
|||
public string Name { get; set; } |
|||
} |
|||
} |
|||
@ -0,0 +1,25 @@ |
|||
// ==========================================================================
|
|||
// AppPatternUpdated.cs
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex Group
|
|||
// All rights reserved.
|
|||
// ==========================================================================
|
|||
|
|||
using System; |
|||
using Squidex.Infrastructure.EventSourcing; |
|||
|
|||
namespace Squidex.Domain.Apps.Events.Apps |
|||
{ |
|||
[EventType(nameof(AppPatternUpdated))] |
|||
public sealed class AppPatternUpdated : AppEvent |
|||
{ |
|||
public Guid Id { get; set; } |
|||
|
|||
public string Name { get; set; } |
|||
|
|||
public string Pattern { get; set; } |
|||
|
|||
public string DefaultMessage { get; set; } |
|||
} |
|||
} |
|||
@ -0,0 +1,44 @@ |
|||
// ==========================================================================
|
|||
// AppClientJsonTests.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.Apps; |
|||
using Xunit; |
|||
|
|||
namespace Squidex.Domain.Apps.Core.Model.Apps |
|||
{ |
|||
public class AppPatternJsonTests |
|||
{ |
|||
private readonly JsonSerializer serializer = TestData.DefaultSerializer(); |
|||
|
|||
[Fact] |
|||
public void Should_serialize_and_deserialize() |
|||
{ |
|||
var patterns = AppPatterns.Empty; |
|||
|
|||
var guid1 = Guid.NewGuid(); |
|||
var guid2 = Guid.NewGuid(); |
|||
var guid3 = Guid.NewGuid(); |
|||
|
|||
patterns = patterns.Add(guid1, "Name1", "Pattern1", "Default"); |
|||
patterns = patterns.Add(guid2, "Name2", "Pattern2", "Default"); |
|||
patterns = patterns.Add(guid3, "Name3", "Pattern3", "Default"); |
|||
|
|||
patterns = patterns.Update(guid2, "Name2 Update", "Pattern2 Update", "Default2"); |
|||
|
|||
patterns = patterns.Remove(guid1); |
|||
|
|||
var appPatterns = JToken.FromObject(patterns, serializer).ToObject<AppPatterns>(serializer); |
|||
|
|||
appPatterns.ShouldBeEquivalentTo(patterns); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,80 @@ |
|||
// ==========================================================================
|
|||
// AppClientsTests.cs
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex Group
|
|||
// All rights reserved.
|
|||
// ==========================================================================
|
|||
|
|||
using System; |
|||
using System.Linq; |
|||
using FluentAssertions; |
|||
using Squidex.Domain.Apps.Core.Apps; |
|||
using Xunit; |
|||
|
|||
#pragma warning disable SA1310 // Field names must not contain underscore
|
|||
|
|||
namespace Squidex.Domain.Apps.Core.Model.Apps |
|||
{ |
|||
public class AppPatternsTests |
|||
{ |
|||
private readonly AppPatterns defaultPatterns; |
|||
private readonly Guid firstId = Guid.NewGuid(); |
|||
private readonly Guid id = Guid.NewGuid(); |
|||
|
|||
public AppPatternsTests() |
|||
{ |
|||
defaultPatterns = AppPatterns.Empty.Add(firstId, "Default", "Default Pattern", "Message"); |
|||
|
|||
id = Guid.NewGuid(); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Should_add_pattern() |
|||
{ |
|||
var patterns = defaultPatterns.Add(id, "NewPattern", "New Pattern", "Message"); |
|||
|
|||
patterns[id].ShouldBeEquivalentTo(new AppPattern(id, "NewPattern", "New Pattern", "Message")); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Should_throw_exception_if_add_pattern_with_same_id() |
|||
{ |
|||
var patterns = defaultPatterns.Add(id, "NewPattern", "New Pattern", "Message"); |
|||
|
|||
Assert.Throws<ArgumentException>(() => patterns.Add(id, "NewPattern", "New Pattern", "Message")); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Should_update_pattern() |
|||
{ |
|||
var patterns = defaultPatterns.Update(firstId, "UpdatePattern", "Update Pattern", "Message"); |
|||
|
|||
patterns[firstId].ShouldBeEquivalentTo(new AppPattern(firstId, "UpdatePattern", "Update Pattern", "Message")); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Should_return_same_patterns_if_pattern_not_found() |
|||
{ |
|||
var patterns = defaultPatterns.Update(id, "NewPattern", "NewPattern", "Message"); |
|||
|
|||
Assert.Same(defaultPatterns, patterns); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Should_remove_pattern() |
|||
{ |
|||
var patterns = defaultPatterns.Remove(firstId); |
|||
|
|||
Assert.Empty(patterns); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Should_do_nothing_if_remove_pattern_not_found() |
|||
{ |
|||
var patterns = defaultPatterns.Remove(id); |
|||
|
|||
Assert.NotSame(defaultPatterns, patterns); |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue