Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions SysML2.NET.Serializer.Json.Tests/ApiSerializerTestFixture.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
// -------------------------------------------------------------------------------------------------
// <copyright file="ApiSerializerTestFixture.cs" company="Starion Group S.A.">
//
//
// Copyright 2022-2025 Starion Group S.A.
//
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//
// http://www.apache.org/licenses/LICENSE-2.0
//
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
//
// </copyright>
// ------------------------------------------------------------------------------------------------

Expand Down Expand Up @@ -73,7 +73,7 @@ private void CreateTestData()
Created = new DateTime(1976, 8, 20, 0, 0, 0, DateTimeKind.Utc),
Description = "",
OwningProject = Guid.Parse("9b0e1914-3241-461e-b9ee-a3ff5120de4e"),
PreviousCommit = Guid.Empty,
PreviousCommits = [Guid.Parse("8d3c3f31-2b2f-4f4b-8f35-6b2e0f4a4a11")],
ResourceIdentifier = "http://www.stariongroup.eu/commit",
};

Expand Down
36 changes: 34 additions & 2 deletions SysML2.NET.Serializer.Json.Tests/DeSerializerTestFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,20 @@ public class DeSerializerTestFixture
/// </remarks>
private const int LargePayloadElementCount = 4000;

/// <summary>
/// A merge commit json object that names two immediately preceding commits
/// </summary>
private const string MergeCommit = """[{"@id":"6d7ad9fd-6520-4ff2-885b-8c5c129e6c27","@type":"Commit","created":"2023-03-13T17:53:59.111354-04:00","description":null,"owningProject":{"@id":"000e9890-6935-43e6-a5d7-5d7cac601f4c"},"previousCommit":[{"@id":"1e4c2a06-0f2f-4a3c-9d64-0f4b2f6d9a11"},{"@id":"2b7d5e18-6a4f-4d1e-8c32-7d5a1c8e4b22"}]}]""";

/// <summary>
/// The identifiers that the <see cref="MergeCommit"/> payload names as preceding commits
/// </summary>
private static readonly Guid[] MergeCommitPredecessors =
[
Guid.Parse("1e4c2a06-0f2f-4a3c-9d64-0f4b2f6d9a11"),
Guid.Parse("2b7d5e18-6a4f-4d1e-8c32-7d5a1c8e4b22")
];

private DeSerializer deSerializer;

[SetUp]
Expand Down Expand Up @@ -209,7 +223,7 @@ public void Verify_that_particular_project_and_commits_from_restapi_json_can_be_
using (Assert.EnterMultipleScope())
{
Assert.That(firstCommit.OwningProject, Is.EqualTo(Guid.Parse("000e9890-6935-43e6-a5d7-5d7cac601f4c")));
Assert.That(firstCommit.PreviousCommit, Is.EqualTo(Guid.Empty));
Assert.That(firstCommit.PreviousCommits, Has.Count.EqualTo(0));
Assert.That(firstCommit.Description, Is.Null);
Assert.That(firstCommit.Created, Is.EqualTo(DateTime.Parse("2023-03-13T17:53:59.111354-04:00", CultureInfo.InvariantCulture)));
}
Expand All @@ -232,12 +246,30 @@ public void Verify_that_particular_project_and_particular_commit_from_restapi_js
using (Assert.EnterMultipleScope())
{
Assert.That(firstCommit.OwningProject, Is.EqualTo(Guid.Parse("000e9890-6935-43e6-a5d7-5d7cac601f4c")));
Assert.That(firstCommit.PreviousCommit, Is.EqualTo(Guid.Empty));
Assert.That(firstCommit.PreviousCommits, Has.Count.EqualTo(0));
Assert.That(firstCommit.Description, Is.Null);
Assert.That(firstCommit.Created, Is.EqualTo(DateTime.Parse("2023-03-13T17:53:59.111354-04:00", CultureInfo.InvariantCulture)));
}
}

[Test]
[TestCase(false)]
[TestCase(true)]
public void Verify_that_a_merge_commit_from_restapi_json_can_be_deserialized(bool shouldDeserializeDerivedProperties)
{
using var stream = new MemoryStream(Encoding.UTF8.GetBytes(MergeCommit));

var data = this.deSerializer.DeSerialize(stream, SerializationModeKind.JSON, SerializationTargetKind.PSM, shouldDeserializeDerivedProperties);

var mergeCommit = data.OfType<Commit>().Single();

using (Assert.EnterMultipleScope())
{
Assert.That(mergeCommit.PreviousCommits, Is.EqualTo(MergeCommitPredecessors));
Assert.That(mergeCommit.OwningProject, Is.EqualTo(Guid.Parse("000e9890-6935-43e6-a5d7-5d7cac601f4c")));
}
}

[Test]
[TestCase(false)]
[TestCase(true)]
Expand Down
19 changes: 9 additions & 10 deletions SysML2.NET.Serializer.Json/PIM/CommitDeSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,18 +145,17 @@ internal static Commit DeSerialize(JsonElement jsonElement, SerializationModeKin

if (jsonElement.TryGetProperty("previousCommit"u8, out JsonElement previousCommitProperty))
{
if (previousCommitProperty.ValueKind == JsonValueKind.Null)
if (previousCommitProperty.ValueKind == JsonValueKind.Array)
{
dtoInstance.PreviousCommit = Guid.Empty;
}
else
{
if (previousCommitProperty.TryGetProperty("@id"u8, out JsonElement previousCommitPropertyIdProperty))
foreach (var previousCommitItem in previousCommitProperty.EnumerateArray())
{
var propertyValue = previousCommitPropertyIdProperty.GetString();
if (propertyValue != null)
if (previousCommitItem.TryGetProperty("@id"u8, out JsonElement previousCommitItemIdProperty))
{
dtoInstance.PreviousCommit = Guid.Parse(propertyValue);
var propertyValue = previousCommitItemIdProperty.GetString();
if (propertyValue != null)
{
dtoInstance.PreviousCommits.Add(Guid.Parse(propertyValue));
}
}
}
}
Expand All @@ -165,7 +164,7 @@ internal static Commit DeSerialize(JsonElement jsonElement, SerializationModeKin
{
if (logger.IsEnabled(LogLevel.Debug))
{
logger.LogDebug("the owningProject Json property was not found in the Commit: {Id}", dtoInstance.Id);
logger.LogDebug("the previousCommit Json property was not found in the Commit: {Id}", dtoInstance.Id);
}
}

Expand Down
14 changes: 11 additions & 3 deletions SysML2.NET.Serializer.Json/PIM/CommitSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,17 @@ internal static void Serialize(object obj, Utf8JsonWriter writer, SerializationM
writer.WriteString("@id"u8, commit.OwningProject);
writer.WriteEndObject();
writer.WriteString("resourceIdentifier"u8, commit.ResourceIdentifier);
writer.WriteStartObject("previousCommit"u8);
writer.WriteString("@id"u8, commit.PreviousCommit);
writer.WriteEndObject();
writer.WriteStartArray("previousCommit"u8);
if (commit.PreviousCommits != null)
{
foreach (var item in commit.PreviousCommits)
{
writer.WriteStartObject();
writer.WriteString("@id"u8, item);
writer.WriteEndObject();
}
}
writer.WriteEndArray();
writer.WriteEndObject();

break;
Expand Down
93 changes: 93 additions & 0 deletions SysML2.NET.Tests/PSM/DTO/MergeOutcomeTestFixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
// -------------------------------------------------------------------------------------------------
// <copyright file="MergeOutcomeTestFixture.cs" company="Starion Group S.A.">
//
// Copyright 2022-2025 Starion Group S.A.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// </copyright>
// ------------------------------------------------------------------------------------------------

namespace SysML2.NET.Tests.PSM.DTO
{
using System;

using NUnit.Framework;

using SysML2.NET.PSM.DTO;

[TestFixture]
public class MergeOutcomeTestFixture
{
private static readonly DataIdentity[] NoIdentities = [];

[Test]
public void VerifyMerged()
{
Assert.That(() => MergeOutcome.Merged(null), Throws.TypeOf<ArgumentNullException>());

var commit = new Commit { Id = Guid.NewGuid() };
var outcome = MergeOutcome.Merged(commit);

using (Assert.EnterMultipleScope())
{
Assert.That(outcome.Kind, Is.EqualTo(MergeOutcomeKind.Merged));
Assert.That(outcome.IsMerged, Is.True);
Assert.That(outcome.TryGetMergeCommit(out var held), Is.True);
Assert.That(held, Is.SameAs(commit));
Assert.That(outcome.TryGetConflict(out var conflict), Is.False);
Assert.That(conflict, Is.Empty);
}
}

[Test]
public void VerifyConflicted()
{
using (Assert.EnterMultipleScope())
{
Assert.That(() => MergeOutcome.Conflicted(null), Throws.TypeOf<ArgumentNullException>());
Assert.That(() => MergeOutcome.Conflicted(NoIdentities), Throws.TypeOf<ArgumentException>());
}

var identity = new DataIdentity { Id = Guid.NewGuid() };
var outcome = MergeOutcome.Conflicted([identity]);

using (Assert.EnterMultipleScope())
{
Assert.That(outcome.Kind, Is.EqualTo(MergeOutcomeKind.Conflicted));
Assert.That(outcome.IsMerged, Is.False);
Assert.That(outcome.TryGetConflict(out var held), Is.True);
Assert.That(held, Has.Count.EqualTo(1));
Assert.That(held[0], Is.SameAs(identity));
Assert.That(outcome.TryGetMergeCommit(out var commit), Is.False);
Assert.That(commit, Is.Null);
}
}

[Test]
public void VerifyMatch()
{
var merged = MergeOutcome.Merged(new Commit { Id = Guid.NewGuid() });
var conflicted = MergeOutcome.Conflicted([new DataIdentity { Id = Guid.NewGuid() }]);

using (Assert.EnterMultipleScope())
{
Assert.That(() => merged.Match<int>(null, conflict => conflict.Count), Throws.TypeOf<ArgumentNullException>());
Assert.That(() => merged.Match(commit => 1, null), Throws.TypeOf<ArgumentNullException>());
Assert.That(merged.Match(commit => "merged", conflict => "conflicted"), Is.EqualTo("merged"));
Assert.That(conflicted.Match(commit => "merged", conflict => "conflicted"), Is.EqualTo("conflicted"));
Assert.That(conflicted.Match(commit => 0, conflict => conflict.Count), Is.EqualTo(1));
}
}
}
}
119 changes: 119 additions & 0 deletions SysML2.NET.Tests/PSM/DTO/MergeResultMapperTestFixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
// -------------------------------------------------------------------------------------------------
// <copyright file="MergeResultMapperTestFixture.cs" company="Starion Group S.A.">
//
// Copyright 2022-2025 Starion Group S.A.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// </copyright>
// ------------------------------------------------------------------------------------------------

namespace SysML2.NET.Tests.PSM.DTO
{
using System;

using NUnit.Framework;

using SysML2.NET.Exceptions;
using SysML2.NET.PIM.DTO;
using SysML2.NET.PSM.DTO;

using Commit = SysML2.NET.PSM.DTO.Commit;
using DataIdentity = SysML2.NET.PSM.DTO.DataIdentity;

[TestFixture]
public class MergeResultMapperTestFixture
{
[Test]
public void VerifyToPsm()
{
using (Assert.EnterMultipleScope())
{
Assert.That(MergeResultMapper.ToPsm(null), Is.Null);
Assert.That(() => MergeResultMapper.ToPsm(new MergeResult()), Throws.TypeOf<IncompleteModelException>());
}

var mergeCommit = Guid.NewGuid();
var merged = MergeResultMapper.ToPsm(new PIM.DTO.MergeResult { MergeCommit = mergeCommit });

using (Assert.EnterMultipleScope())
{
Assert.That(merged.Kind, Is.EqualTo(MergeOutcomeKind.Merged));
Assert.That(merged.IsMerged, Is.True);
Assert.That(merged.TryGetMergeCommit(out var commit), Is.True);
Assert.That(merged.TryGetConflict(out var noConflict), Is.False);
Assert.That(commit.Id, Is.EqualTo(mergeCommit));
Assert.That(noConflict, Is.Empty);
}

var firstIdentity = Guid.NewGuid();
var secondIdentity = Guid.NewGuid();
var conflicted = MergeResultMapper.ToPsm(new PIM.DTO.MergeResult { Conflict = [firstIdentity, secondIdentity] });

using (Assert.EnterMultipleScope())
{
Assert.That(conflicted.Kind, Is.EqualTo(MergeOutcomeKind.Conflicted));
Assert.That(conflicted.IsMerged, Is.False);
Assert.That(conflicted.TryGetMergeCommit(out var absentCommit), Is.False);
Assert.That(absentCommit, Is.Null);
Assert.That(conflicted.TryGetConflict(out var conflict), Is.True);
Assert.That(conflict, Has.Count.EqualTo(2));
Assert.That(conflict[0].Id, Is.EqualTo(firstIdentity));
Assert.That(conflict[1].Id, Is.EqualTo(secondIdentity));
}

var both = MergeResultMapper.ToPsm(new PIM.DTO.MergeResult { MergeCommit = mergeCommit, Conflict = [firstIdentity] });

Assert.That(both.Kind, Is.EqualTo(MergeOutcomeKind.Conflicted));
}

[Test]
public void VerifyToPim()
{
Assert.That(MergeResultMapper.ToPim(null), Is.Null);

var mergeCommit = Guid.NewGuid();
var merged = MergeResultMapper.ToPim(MergeOutcome.Merged(new Commit { Id = mergeCommit }));

using (Assert.EnterMultipleScope())
{
Assert.That(merged.MergeCommit, Is.EqualTo(mergeCommit));
Assert.That(merged.Conflict, Has.Count.EqualTo(0));
}

var identity = Guid.NewGuid();
var conflicted = MergeResultMapper.ToPim(MergeOutcome.Conflicted([new DataIdentity { Id = identity }]));

using (Assert.EnterMultipleScope())
{
Assert.That(conflicted.MergeCommit, Is.EqualTo(Guid.Empty));
Assert.That(conflicted.Conflict, Is.EqualTo([identity]));
}
}

[Test]
public void VerifyRoundTrip()
{
var merged = new PIM.DTO.MergeResult { MergeCommit = Guid.NewGuid() };
var conflicted = new PIM.DTO.MergeResult { Conflict = [Guid.NewGuid(), Guid.NewGuid()] };

using (Assert.EnterMultipleScope())
{
Assert.That(MergeResultMapper.ToPim(MergeResultMapper.ToPsm(merged)).MergeCommit, Is.EqualTo(merged.MergeCommit));
Assert.That(MergeResultMapper.ToPim(MergeResultMapper.ToPsm(merged)).Conflict, Has.Count.EqualTo(0));
Assert.That(MergeResultMapper.ToPim(MergeResultMapper.ToPsm(conflicted)).Conflict, Is.EqualTo(conflicted.Conflict));
Assert.That(MergeResultMapper.ToPim(MergeResultMapper.ToPsm(conflicted)).MergeCommit, Is.EqualTo(Guid.Empty));
}
}
}
}
Loading
Loading