Skip to content
Merged
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
Original file line number Diff line number Diff line change
@@ -1,38 +1,88 @@
// -------------------------------------------------------------------------------------------------
// -------------------------------------------------------------------------------------------------
// <copyright file="EndFeatureMembershipExtensionsTestFixture.cs" company="Starion Group S.A.">
//
//
// Copyright 2022-2026 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.Extend
{
using System;

using NUnit.Framework;

using SysML2.NET.Core.POCO.Core.Features;
using SysML2.NET.Core.POCO.Root.Elements;
using SysML2.NET.Core.POCO.Root.Namespaces;
using SysML2.NET.Exceptions;
using SysML2.NET.Extensions;

using Type = SysML2.NET.Core.POCO.Core.Types.Type;

[TestFixture]
public class EndFeatureMembershipExtensionsTestFixture
{
[Test]
public void ComputeOwnedMemberFeature_ThrowsNotSupportedException()
public void VerifyComputeOwnedMemberFeature()
{
Assert.That(() => ((IEndFeatureMembership)null).ComputeOwnedMemberFeature(), Throws.TypeOf<NotSupportedException>());
Assert.That(() => ((IEndFeatureMembership)null).ComputeOwnedMemberFeature(), Throws.TypeOf<ArgumentNullException>());

// Empty OwnedRelatedElement → [1..1] violation: throws IncompleteModelException.
var endFeatureMembership = new EndFeatureMembership();

Assert.That(() => endFeatureMembership.ComputeOwnedMemberFeature(), Throws.TypeOf<IncompleteModelException>());

// Single IFeature wired via the public API → returned.
var owningType = new Type();
var feature = new Feature();

owningType.AssignOwnership(endFeatureMembership, feature);

Assert.That(endFeatureMembership.ComputeOwnedMemberFeature(), Is.SameAs(feature));

// Two IFeatures in OwnedRelatedElement → [1..1] violation: throws IncompleteModelException.
var twoFeatureMembership = new EndFeatureMembership();
var firstFeature = new Feature();
var secondFeature = new Feature();

((IContainedRelationship)twoFeatureMembership).OwnedRelatedElement.Add(firstFeature);
((IContainedRelationship)twoFeatureMembership).OwnedRelatedElement.Add(secondFeature);

Assert.That(() => twoFeatureMembership.ComputeOwnedMemberFeature(), Throws.TypeOf<IncompleteModelException>());

// Mixed-type owned related elements: exactly one IFeature alongside a non-IFeature (Namespace).
// The RequireSingleOfType<IFeature> projection MUST pick out the IFeature regardless of its position
// (this is the core robustness guarantee — never positionally index the unfiltered collection).
var mixedMembership = new EndFeatureMembership();
var siblingNonFeature = new Namespace();
var mixedFeature = new Feature();

((IContainedRelationship)mixedMembership).OwnedRelatedElement.Add(siblingNonFeature);
((IContainedRelationship)mixedMembership).OwnedRelatedElement.Add(mixedFeature);

Assert.That(mixedMembership.ComputeOwnedMemberFeature(), Is.SameAs(mixedFeature));

// OwnedRelatedElement populated with non-IFeature element(s) only → no IFeature match:
// [1..1] violation, throws IncompleteModelException.
var nonFeatureMembership = new EndFeatureMembership();
var nonFeatureElement = new Namespace();

((IContainedRelationship)nonFeatureMembership).OwnedRelatedElement.Add(nonFeatureElement);

Assert.That(() => nonFeatureMembership.ComputeOwnedMemberFeature(), Throws.TypeOf<IncompleteModelException>());
}
}
}
16 changes: 7 additions & 9 deletions SysML2.NET/Extend/EndFeatureMembershipExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,8 @@
namespace SysML2.NET.Core.POCO.Core.Features
{
using System;
using System.Collections.Generic;

using SysML2.NET.Core.Root.Namespaces;
using SysML2.NET.Core.POCO.Core.Types;
using SysML2.NET.Core.POCO.Root.Annotations;
using SysML2.NET.Core.POCO.Root.Elements;
using SysML2.NET.Core.POCO.Root.Namespaces;
using SysML2.NET.Extensions;

/// <summary>
/// The <see cref="EndFeatureMembershipExtensions"/> class provides extensions methods for
Expand All @@ -44,11 +39,14 @@
/// <returns>
/// the computed result
/// </returns>
[System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
internal static IFeature ComputeOwnedMemberFeature(this IEndFeatureMembership endFeatureMembershipSubject)
{
throw new NotSupportedException("Create a GitHub issue when this method is required");
}
if (endFeatureMembershipSubject == null)
{
throw new ArgumentNullException(nameof(endFeatureMembershipSubject));
}

Check warning on line 47 in SysML2.NET/Extend/EndFeatureMembershipExtensions.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use 'ArgumentNullException.ThrowIfNull' instead of explicitly throwing a new exception instance

See more on https://sonarcloud.io/project/issues?id=STARIONGROUP_SysML2.NET&issues=AZ5kmdSfUIizL7HCkA5B&open=AZ5kmdSfUIizL7HCkA5B&pullRequest=265

return endFeatureMembershipSubject.OwnedRelatedElement.RequireSingleOfType<IFeature>(nameof(endFeatureMembershipSubject));
}
}
}
Loading