From e3b359d7ab697e1c93a1292149c647845e84f075 Mon Sep 17 00:00:00 2001 From: Tom Waterbley Date: Tue, 27 Feb 2024 14:51:48 +0100 Subject: [PATCH] Better support for DomBehaviorDefinition --- .../DomBehaviorDefinitionBuilderTests.cs | 68 +++++++++ .../Builders/DomBehaviorDefinitionBuilder.cs | 138 ++++++++++++++++++ Utils.DOM/Builders/DomDefinitionBuilder.cs | 15 ++ Utils.DOM/UnitTesting/DomCacheMock.cs | 14 ++ Utils.DOM/UnitTesting/DomHelperMock.cs | 14 ++ .../UnitTesting/DomSLNetMessageHandler.cs | 45 ++++++ 6 files changed, 294 insertions(+) create mode 100644 Utils.DOM.Tests/Builders/DomBehaviorDefinitionBuilderTests.cs create mode 100644 Utils.DOM/Builders/DomBehaviorDefinitionBuilder.cs diff --git a/Utils.DOM.Tests/Builders/DomBehaviorDefinitionBuilderTests.cs b/Utils.DOM.Tests/Builders/DomBehaviorDefinitionBuilderTests.cs new file mode 100644 index 0000000..56488b4 --- /dev/null +++ b/Utils.DOM.Tests/Builders/DomBehaviorDefinitionBuilderTests.cs @@ -0,0 +1,68 @@ +namespace Utils.DOM.Tests +{ + using System; + + using FluentAssertions; + + using Microsoft.VisualStudio.TestTools.UnitTesting; + + using Skyline.DataMiner.Net.Apps.DataMinerObjectModel; + using Skyline.DataMiner.Net.Apps.DataMinerObjectModel.Status; + using Skyline.DataMiner.Utils.DOM.Builders; + + [TestClass] + public class DomBehaviorDefinitionBuilderTests + { + [TestMethod] + public void DomBehaviorDefinitionBuilder_WithID() + { + var id = new DomBehaviorDefinitionId(Guid.NewGuid()); + + var definition = new DomBehaviorDefinitionBuilder() + .WithID(id) + .Build(); + + definition.ID.Should().Be(id); + } + + [TestMethod] + public void DomBehaviorDefinitionBuilder_WithName() + { + var name = "My name"; + + var definition = new DomBehaviorDefinitionBuilder() + .WithName(name) + .Build(); + + definition.Name.Should().Be(name); + } + + [TestMethod] + public void DomBehaviorDefinitionBuilder_WithInitialStatusId() + { + var initialStatus = "draft"; + + var definition = new DomBehaviorDefinitionBuilder() + .WithInitialStatusId(initialStatus) + .Build(); + + definition.InitialStatusId.Should().Be(initialStatus); + } + + [TestMethod] + public void DomBehaviorDefinitionBuilder_WithStatuses() + { + var statuses = new[] + { + new DomStatus("draft", "Draft"), + new DomStatus("confirmed", "Confirmed"), + }; + + var definition = new DomBehaviorDefinitionBuilder() + .WithStatuses(statuses) + .Build(); + + definition.Statuses.Should().BeEquivalentTo(statuses); + } + } +} diff --git a/Utils.DOM/Builders/DomBehaviorDefinitionBuilder.cs b/Utils.DOM/Builders/DomBehaviorDefinitionBuilder.cs new file mode 100644 index 0000000..4d3a193 --- /dev/null +++ b/Utils.DOM/Builders/DomBehaviorDefinitionBuilder.cs @@ -0,0 +1,138 @@ +namespace Skyline.DataMiner.Utils.DOM.Builders +{ + using System; + using System.Collections.Generic; + using System.Linq; + + using Skyline.DataMiner.Net.Apps.DataMinerObjectModel; + using Skyline.DataMiner.Net.Apps.DataMinerObjectModel.Status; + using Skyline.DataMiner.Net.Apps.Sections.SectionDefinitions; + using Skyline.DataMiner.Net.Sections; + + /// + /// Represents a builder for creating instances of . + /// + /// The type of the derived builder class. + public class DomBehaviorDefinitionBuilder where T : DomBehaviorDefinitionBuilder + { + /// + /// The instance being built by the builder. + /// + protected readonly DomBehaviorDefinition _definition; + + /// + /// Initializes a new instance of the class. + /// + public DomBehaviorDefinitionBuilder() + { + _definition = new DomBehaviorDefinition(); + } + + /// + /// Initializes a new instance of the class with a specified . + /// + /// The existing DOM behavior definition to be used in the builder. + public DomBehaviorDefinitionBuilder(DomBehaviorDefinition definition) + { + _definition = definition ?? throw new ArgumentNullException(nameof(definition)); + } + + /// + /// Builds the . + /// + /// The built . + public DomBehaviorDefinition Build() + { + return _definition; + } + + /// + /// Sets the ID of the . + /// + /// The ID to set. + /// The builder instance. + public T WithID(DomBehaviorDefinitionId id) + { + _definition.ID = id ?? throw new ArgumentNullException(nameof(id)); + + return (T)this; + } + + /// + /// Sets the ID of the using a GUID. + /// + /// The GUID to set as the ID. + /// The builder instance. + public T WithID(Guid id) + { + return WithID(new DomBehaviorDefinitionId(id)); + } + + /// + /// Sets the name of the . + /// + /// The name to set. + /// The builder instance. + public T WithName(string name) + { + if (String.IsNullOrWhiteSpace(name)) + { + throw new ArgumentException($"'{nameof(name)}' cannot be null or whitespace.", nameof(name)); + } + + _definition.Name = name; + + return (T)this; + } + + /// + /// Sets the initial status ID. + /// + /// The initial status to set. + /// The builder instance. + public T WithInitialStatusId(string initialStatusId) + { + _definition.InitialStatusId = initialStatusId; + + return (T)this; + } + + /// + /// Sets the statuses. + /// + /// The statuses to set. + /// The builder instance. + public T WithStatuses(IEnumerable statuses) + { + if (statuses == null) + { + throw new ArgumentNullException(nameof(statuses)); + } + + _definition.Statuses = statuses.ToList(); + + return (T)this; + } + } + + /// + /// Represents a builder for creating instances of . + /// + public class DomBehaviorDefinitionBuilder : DomBehaviorDefinitionBuilder + { + /// + /// Initializes a new instance of the class. + /// + public DomBehaviorDefinitionBuilder() + { + } + + /// + /// Initializes a new instance of the class with a specified . + /// + /// The existing DOM behavior definition to be used in the builder. + public DomBehaviorDefinitionBuilder(DomBehaviorDefinition definition) : base(definition) + { + } + } +} \ No newline at end of file diff --git a/Utils.DOM/Builders/DomDefinitionBuilder.cs b/Utils.DOM/Builders/DomDefinitionBuilder.cs index b599402..c2b0f9b 100644 --- a/Utils.DOM/Builders/DomDefinitionBuilder.cs +++ b/Utils.DOM/Builders/DomDefinitionBuilder.cs @@ -94,6 +94,21 @@ public T WithDomBehaviorDefinition(DomBehaviorDefinitionId domBehaviorDefinition return (T)this; } + /// + /// Sets the DOM behavior definition of the . + /// + /// The DOM behavior definition to set. + /// The builder instance. + public T WithDomBehaviorDefinition(DomBehaviorDefinition definition) + { + if (definition == null) + { + throw new ArgumentNullException(nameof(definition)); + } + + return WithDomBehaviorDefinition(definition.ID); + } + /// /// Adds a section definition link to the . /// diff --git a/Utils.DOM/UnitTesting/DomCacheMock.cs b/Utils.DOM/UnitTesting/DomCacheMock.cs index eda27d9..cb67045 100644 --- a/Utils.DOM/UnitTesting/DomCacheMock.cs +++ b/Utils.DOM/UnitTesting/DomCacheMock.cs @@ -89,5 +89,19 @@ public void SetInstances(IEnumerable instances) _helper.SetInstances(instances); } + + /// + /// Sets the behavior definitions in the mock. + /// + /// The collection of DOM behavior definitions to set. + public void SetBehaviorDefinitions(IEnumerable definitions) + { + if (definitions == null) + { + throw new ArgumentNullException(nameof(definitions)); + } + + _helper.SetBehaviorDefinitions(definitions); + } } } diff --git a/Utils.DOM/UnitTesting/DomHelperMock.cs b/Utils.DOM/UnitTesting/DomHelperMock.cs index d8d1c32..5e6116b 100644 --- a/Utils.DOM/UnitTesting/DomHelperMock.cs +++ b/Utils.DOM/UnitTesting/DomHelperMock.cs @@ -91,5 +91,19 @@ public void SetInstances(IEnumerable instances) _messageHandler.SetInstances(instances); } + + /// + /// Sets the behavior definitions in the mock. + /// + /// The collection of DOM behavior definitions to set. + public void SetBehaviorDefinitions(IEnumerable definitions) + { + if (definitions == null) + { + throw new ArgumentNullException(nameof(definitions)); + } + + _messageHandler.SetBehaviorDefinitions(definitions); + } } } diff --git a/Utils.DOM/UnitTesting/DomSLNetMessageHandler.cs b/Utils.DOM/UnitTesting/DomSLNetMessageHandler.cs index 32c3525..c4bd640 100644 --- a/Utils.DOM/UnitTesting/DomSLNetMessageHandler.cs +++ b/Utils.DOM/UnitTesting/DomSLNetMessageHandler.cs @@ -18,6 +18,7 @@ public class DomSLNetMessageHandler private readonly ConcurrentDictionary _definitions = new ConcurrentDictionary(); private readonly ConcurrentDictionary _sectionDefinitions = new ConcurrentDictionary(); private readonly ConcurrentDictionary _instances = new ConcurrentDictionary(); + private readonly ConcurrentDictionary _behaviorDefinitions = new ConcurrentDictionary(); /// /// Initializes a new instance of the class. @@ -86,6 +87,26 @@ public void SetInstances(IEnumerable instances) } } + /// + /// Sets the DomBehaviorDefinitions for the handler. + /// + /// The collection of DomBehaviorDefinitions to set. + /// Thrown when the input collection of DomBehaviorDefinitions is null. + public void SetBehaviorDefinitions(IEnumerable definitions) + { + if (definitions == null) + { + throw new ArgumentNullException(nameof(definitions)); + } + + _behaviorDefinitions.Clear(); + + foreach (var definition in definitions) + { + _behaviorDefinitions.TryAdd(definition.ID.SafeId(), definition); + } + } + /// /// Handles an array of DMS messages, processing each message and returning an array of responses. /// @@ -200,6 +221,30 @@ public bool TryHandleMessage(DMSMessage message, out DMSMessage response) #endregion + #region BehaviorDefinitions + + case ManagerStoreReadRequest request: + var behaviorDefinitions = request.Query.ExecuteInMemory(_behaviorDefinitions.Values).ToList(); + response = new ManagerStoreCrudResponse(behaviorDefinitions); + return true; + + case ManagerStoreCreateRequest request: + _behaviorDefinitions[request.Object.ID.SafeId()] = request.Object; + response = new ManagerStoreCrudResponse(request.Object); + return true; + + case ManagerStoreUpdateRequest request: + _behaviorDefinitions[request.Object.ID.SafeId()] = request.Object; + response = new ManagerStoreCrudResponse(request.Object); + return true; + + case ManagerStoreDeleteRequest request: + _behaviorDefinitions.TryRemove(request.Object.ID.SafeId(), out _); + response = new ManagerStoreCrudResponse(request.Object); + return true; + + #endregion + default: response = default; return false;