Skip to content

Commit

Permalink
Better support for DomBehaviorDefinition
Browse files Browse the repository at this point in the history
  • Loading branch information
TomW-Skyline committed Feb 27, 2024
1 parent 7aba921 commit e3b359d
Show file tree
Hide file tree
Showing 6 changed files with 294 additions and 0 deletions.
68 changes: 68 additions & 0 deletions Utils.DOM.Tests/Builders/DomBehaviorDefinitionBuilderTests.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
138 changes: 138 additions & 0 deletions Utils.DOM/Builders/DomBehaviorDefinitionBuilder.cs
Original file line number Diff line number Diff line change
@@ -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;

/// <summary>
/// Represents a builder for creating instances of <see cref="DomBehaviorDefinition"/>.
/// </summary>
/// <typeparam name="T">The type of the derived builder class.</typeparam>
public class DomBehaviorDefinitionBuilder<T> where T : DomBehaviorDefinitionBuilder<T>
{
/// <summary>
/// The <see cref="DomBehaviorDefinition"/> instance being built by the builder.
/// </summary>
protected readonly DomBehaviorDefinition _definition;

/// <summary>
/// Initializes a new instance of the <see cref="DomBehaviorDefinitionBuilder{T}"/> class.
/// </summary>
public DomBehaviorDefinitionBuilder()
{
_definition = new DomBehaviorDefinition();
}

/// <summary>
/// Initializes a new instance of the <see cref="DomBehaviorDefinitionBuilder{T}"/> class with a specified <see cref="DomBehaviorDefinition"/>.
/// </summary>
/// <param name="definition">The existing DOM behavior definition to be used in the builder.</param>
public DomBehaviorDefinitionBuilder(DomBehaviorDefinition definition)
{
_definition = definition ?? throw new ArgumentNullException(nameof(definition));
}

/// <summary>
/// Builds the <see cref="DomBehaviorDefinition"/>.
/// </summary>
/// <returns>The built <see cref="DomBehaviorDefinition"/>.</returns>
public DomBehaviorDefinition Build()
{
return _definition;
}

/// <summary>
/// Sets the ID of the <see cref="DomBehaviorDefinition"/>.
/// </summary>
/// <param name="id">The ID to set.</param>
/// <returns>The builder instance.</returns>
public T WithID(DomBehaviorDefinitionId id)
{
_definition.ID = id ?? throw new ArgumentNullException(nameof(id));

return (T)this;
}

/// <summary>
/// Sets the ID of the <see cref="DomBehaviorDefinition"/> using a GUID.
/// </summary>
/// <param name="id">The GUID to set as the ID.</param>
/// <returns>The builder instance.</returns>
public T WithID(Guid id)
{
return WithID(new DomBehaviorDefinitionId(id));
}

/// <summary>
/// Sets the name of the <see cref="DomBehaviorDefinition"/>.
/// </summary>
/// <param name="name">The name to set.</param>
/// <returns>The builder instance.</returns>
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;
}

/// <summary>
/// Sets the initial status ID.
/// </summary>
/// <param name="initialStatusId">The initial status to set.</param>
/// <returns>The builder instance.</returns>
public T WithInitialStatusId(string initialStatusId)
{
_definition.InitialStatusId = initialStatusId;

return (T)this;
}

/// <summary>
/// Sets the statuses.
/// </summary>
/// <param name="statuses">The statuses to set.</param>
/// <returns>The builder instance.</returns>
public T WithStatuses(IEnumerable<DomStatus> statuses)
{
if (statuses == null)
{
throw new ArgumentNullException(nameof(statuses));
}

_definition.Statuses = statuses.ToList();

return (T)this;
}
}

/// <summary>
/// Represents a builder for creating instances of <see cref="DomBehaviorDefinition"/>.
/// </summary>
public class DomBehaviorDefinitionBuilder : DomBehaviorDefinitionBuilder<DomBehaviorDefinitionBuilder>
{
/// <summary>
/// Initializes a new instance of the <see cref="DomBehaviorDefinitionBuilder"/> class.
/// </summary>
public DomBehaviorDefinitionBuilder()
{
}

/// <summary>
/// Initializes a new instance of the <see cref="DomBehaviorDefinitionBuilder"/> class with a specified <see cref="DomBehaviorDefinition"/>.
/// </summary>
/// <param name="definition">The existing DOM behavior definition to be used in the builder.</param>
public DomBehaviorDefinitionBuilder(DomBehaviorDefinition definition) : base(definition)
{
}
}
}
15 changes: 15 additions & 0 deletions Utils.DOM/Builders/DomDefinitionBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,21 @@ public T WithDomBehaviorDefinition(DomBehaviorDefinitionId domBehaviorDefinition
return (T)this;
}

/// <summary>
/// Sets the DOM behavior definition of the <see cref="DomDefinition"/>.
/// </summary>
/// <param name="definition">The DOM behavior definition to set.</param>
/// <returns>The builder instance.</returns>
public T WithDomBehaviorDefinition(DomBehaviorDefinition definition)
{
if (definition == null)
{
throw new ArgumentNullException(nameof(definition));
}

return WithDomBehaviorDefinition(definition.ID);
}

/// <summary>
/// Adds a section definition link to the <see cref="DomDefinition"/>.
/// </summary>
Expand Down
14 changes: 14 additions & 0 deletions Utils.DOM/UnitTesting/DomCacheMock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,5 +89,19 @@ public void SetInstances(IEnumerable<DomInstance> instances)

_helper.SetInstances(instances);
}

/// <summary>
/// Sets the behavior definitions in the mock.
/// </summary>
/// <param name="definitions">The collection of DOM behavior definitions to set.</param>
public void SetBehaviorDefinitions(IEnumerable<DomBehaviorDefinition> definitions)
{
if (definitions == null)
{
throw new ArgumentNullException(nameof(definitions));
}

_helper.SetBehaviorDefinitions(definitions);
}
}
}
14 changes: 14 additions & 0 deletions Utils.DOM/UnitTesting/DomHelperMock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,5 +91,19 @@ public void SetInstances(IEnumerable<DomInstance> instances)

_messageHandler.SetInstances(instances);
}

/// <summary>
/// Sets the behavior definitions in the mock.
/// </summary>
/// <param name="definitions">The collection of DOM behavior definitions to set.</param>
public void SetBehaviorDefinitions(IEnumerable<DomBehaviorDefinition> definitions)
{
if (definitions == null)
{
throw new ArgumentNullException(nameof(definitions));
}

_messageHandler.SetBehaviorDefinitions(definitions);
}
}
}
45 changes: 45 additions & 0 deletions Utils.DOM/UnitTesting/DomSLNetMessageHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public class DomSLNetMessageHandler
private readonly ConcurrentDictionary<Guid, DomDefinition> _definitions = new ConcurrentDictionary<Guid, DomDefinition>();
private readonly ConcurrentDictionary<Guid, SectionDefinition> _sectionDefinitions = new ConcurrentDictionary<Guid, SectionDefinition>();
private readonly ConcurrentDictionary<Guid, DomInstance> _instances = new ConcurrentDictionary<Guid, DomInstance>();
private readonly ConcurrentDictionary<Guid, DomBehaviorDefinition> _behaviorDefinitions = new ConcurrentDictionary<Guid, DomBehaviorDefinition>();

/// <summary>
/// Initializes a new instance of the <see cref="DomSLNetMessageHandler"/> class.
Expand Down Expand Up @@ -86,6 +87,26 @@ public void SetInstances(IEnumerable<DomInstance> instances)
}
}

/// <summary>
/// Sets the DomBehaviorDefinitions for the handler.
/// </summary>
/// <param name="definitions">The collection of DomBehaviorDefinitions to set.</param>
/// <exception cref="ArgumentNullException">Thrown when the input collection of DomBehaviorDefinitions is null.</exception>
public void SetBehaviorDefinitions(IEnumerable<DomBehaviorDefinition> definitions)
{
if (definitions == null)
{
throw new ArgumentNullException(nameof(definitions));
}

_behaviorDefinitions.Clear();

foreach (var definition in definitions)
{
_behaviorDefinitions.TryAdd(definition.ID.SafeId(), definition);
}
}

/// <summary>
/// Handles an array of DMS messages, processing each message and returning an array of responses.
/// </summary>
Expand Down Expand Up @@ -200,6 +221,30 @@ public bool TryHandleMessage(DMSMessage message, out DMSMessage response)

#endregion

#region BehaviorDefinitions

case ManagerStoreReadRequest<DomBehaviorDefinition> request:
var behaviorDefinitions = request.Query.ExecuteInMemory(_behaviorDefinitions.Values).ToList();
response = new ManagerStoreCrudResponse<DomBehaviorDefinition>(behaviorDefinitions);
return true;

case ManagerStoreCreateRequest<DomBehaviorDefinition> request:
_behaviorDefinitions[request.Object.ID.SafeId()] = request.Object;
response = new ManagerStoreCrudResponse<DomBehaviorDefinition>(request.Object);
return true;

case ManagerStoreUpdateRequest<DomBehaviorDefinition> request:
_behaviorDefinitions[request.Object.ID.SafeId()] = request.Object;
response = new ManagerStoreCrudResponse<DomBehaviorDefinition>(request.Object);
return true;

case ManagerStoreDeleteRequest<DomBehaviorDefinition> request:
_behaviorDefinitions.TryRemove(request.Object.ID.SafeId(), out _);
response = new ManagerStoreCrudResponse<DomBehaviorDefinition>(request.Object);
return true;

#endregion

default:
response = default;
return false;
Expand Down

0 comments on commit e3b359d

Please sign in to comment.