Skip to content

Commit

Permalink
Add support for status transitions
Browse files Browse the repository at this point in the history
  • Loading branch information
TomW-Skyline committed Mar 5, 2024
1 parent e3b359d commit 9a942f8
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 0 deletions.
20 changes: 20 additions & 0 deletions Utils.DOM.Tests/Builders/DomBehaviorDefinitionBuilderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,25 @@ public void DomBehaviorDefinitionBuilder_WithStatuses()

definition.Statuses.Should().BeEquivalentTo(statuses);
}

[TestMethod]
public void DomBehaviorDefinitionBuilder_WithStatusTransitions()
{
var statusDraft = new DomStatus("draft", "Draft");
var statusConfirmed = new DomStatus("confirmed", "Confirmed");
var statusRunning = new DomStatus("running", "Running");

var transitions = new[]
{
new DomStatusTransition("draft_to_confirmed", statusDraft.Id, statusConfirmed.Id),
new DomStatusTransition("confirmed_to_running", statusConfirmed.Id, statusRunning.Id),
};

var definition = new DomBehaviorDefinitionBuilder()
.WithStatusTransitions(transitions)
.Build();

definition.StatusTransitions.Should().BeEquivalentTo(transitions);
}
}
}
17 changes: 17 additions & 0 deletions Utils.DOM/Builders/DomBehaviorDefinitionBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,23 @@ public T WithStatuses(IEnumerable<DomStatus> statuses)

return (T)this;
}

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

_definition.StatusTransitions = transitions.ToList();

return (T)this;
}
}

/// <summary>
Expand Down
42 changes: 42 additions & 0 deletions Utils.DOM/UnitTesting/DomSLNetMessageHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

using Skyline.DataMiner.Net;
using Skyline.DataMiner.Net.Apps.DataMinerObjectModel;
using Skyline.DataMiner.Net.Apps.DataMinerObjectModel.CustomMessages;
using Skyline.DataMiner.Net.Messages;
using Skyline.DataMiner.Net.Sections;

Expand Down Expand Up @@ -245,10 +246,51 @@ public bool TryHandleMessage(DMSMessage message, out DMSMessage response)

#endregion

#region Status Transition

case DomInstanceStatusTransitionRequestMessage request:
response = HandleDomInstanceStatusTransitionRequestMessage(request);
return true;

#endregion

default:
response = default;
return false;
}
}

private DMSMessage HandleDomInstanceStatusTransitionRequestMessage(DomInstanceStatusTransitionRequestMessage request)
{
if (!_instances.TryGetValue(request.DomInstanceId.Id, out var instance))
{
throw new InvalidOperationException($"Couldn't find instance with ID '{request.DomInstanceId.Id}'");
}

if (!_definitions.TryGetValue(instance.DomDefinitionId.Id, out var definition))
{
throw new InvalidOperationException($"Couldn't find definition with ID '{instance.DomDefinitionId.Id}'");
}

if (!_behaviorDefinitions.TryGetValue(definition.DomBehaviorDefinitionId.Id, out var behavior))
{
throw new InvalidOperationException($"Couldn't find behavior with ID '{definition.DomBehaviorDefinitionId.Id}'");
}

var transition = behavior.StatusTransitions.FirstOrDefault(x => String.Equals(x.Id, request.TransitionId));
if (transition == null)
{
throw new InvalidOperationException($"Couldn't find transition with ID '{request.TransitionId}'");
}

if (instance.StatusId != transition.FromStatusId)
{
throw new InvalidOperationException($"Instance doesn't have status '{transition.FromStatusId}'");
}

instance.StatusId = transition.ToStatusId;

return new DomInstanceStatusTransitionResponseMessage { DomInstance = instance };
}
}
}

0 comments on commit 9a942f8

Please sign in to comment.