Skip to content

Commit

Permalink
[ADD] ResolveName extension Method for EnumerationParameterType; fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
lxatstariongroup authored Feb 15, 2024
1 parent 914b08e commit da814f6
Show file tree
Hide file tree
Showing 3 changed files with 241 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="EnumerationParameterTypeExtensionsTestFixture.cs" company="RHEA System S.A.">
// Copyright (c) 2015-2024 RHEA System S.A.
//
// Author: Sam Gerené, Alex Vorobiev, Alexander van Delft, Nathanael Smiechowski,
// Antoine Théate, Omar Elebiary, Jaime Bernar
//
// This file is part of CDP4-COMET-SDK Community Edition
//
// The CDP4-COMET-SDK Community Edition is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 3 of the License, or (at your option) any later version.
//
// The CDP4-COMET-SDK Community Edition is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this program; if not, write to the Free Software Foundation,
// Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
// </copyright>
// --------------------------------------------------------------------------------------------------------------------

namespace CDP4Common.NetCore.Tests.Extensions
{
using System;
using System.Collections.Generic;
using System.Linq;

using CDP4Common.Exceptions;
using CDP4Common.Extensions;
using CDP4Common.SiteDirectoryData;

using NUnit.Framework;

/// <summary>
/// Suite of tests for the <see cref="EnumerationParameterTypeExtensions"/> class
/// </summary>
[TestFixture]
public class EnumerationParameterTypeExtensionsTestFixture
{
private EnumerationParameterType enumerationParameterType;
private EnumerationValueDefinition enumerationValueDefinition1;
private EnumerationValueDefinition enumerationValueDefinition2;
private EnumerationValueDefinition enumerationValueDefinition3;

[SetUp]
public void SetUp()
{
this.enumerationParameterType = new EnumerationParameterType(Guid.NewGuid(), null, null);
this.enumerationValueDefinition1 = new EnumerationValueDefinition(Guid.NewGuid(), null, null) {Name = "Value 1", ShortName = "val1"};
this.enumerationValueDefinition2 = new EnumerationValueDefinition(Guid.NewGuid(), null, null) { Name = "Value 2", ShortName = "val2" };
this.enumerationValueDefinition3 = new EnumerationValueDefinition(Guid.NewGuid(), null, null) { Name = "Value 3", ShortName = "val3" };

this.enumerationParameterType.ValueDefinition.Add(this.enumerationValueDefinition1);
this.enumerationParameterType.ValueDefinition.Add(this.enumerationValueDefinition2);
this.enumerationParameterType.ValueDefinition.Add(this.enumerationValueDefinition3);
}

[Test]
public void VerifyExpressionStringsAtThingLevel()
{
Assert.That(this.enumerationParameterType.ResolveNames("-"), Is.EquivalentTo(new List<string> { "-" }));
Assert.That(this.enumerationParameterType.ResolveNames("val1"), Is.EquivalentTo(new List<string> { "Value 1" }));
Assert.That(this.enumerationParameterType.ResolveNames("val1|val2"), Is.EquivalentTo(new List<string> { "Value 1", "Value 2" }));
Assert.That(this.enumerationParameterType.ResolveNames("val1|val3"), Is.EquivalentTo(new List<string> { "Value 1", "Value 3" }));
Assert.That(() => this.enumerationParameterType.ResolveNames("val1|val4"), Throws.TypeOf<Cdp4ModelValidationException>());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="EnumerationParameterTypeExtensionsTestFixture.cs" company="RHEA System S.A.">
// Copyright (c) 2015-2024 RHEA System S.A.
//
// Author: Sam Gerené, Alex Vorobiev, Alexander van Delft, Nathanael Smiechowski,
// Antoine Théate, Omar Elebiary, Jaime Bernar
//
// This file is part of CDP4-COMET-SDK Community Edition
//
// The CDP4-COMET-SDK Community Edition is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 3 of the License, or (at your option) any later version.
//
// The CDP4-COMET-SDK Community Edition is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this program; if not, write to the Free Software Foundation,
// Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
// </copyright>
// --------------------------------------------------------------------------------------------------------------------

namespace CDP4Common.Tests.Extensions
{
using System;
using System.Collections.Generic;

using CDP4Common.Exceptions;
using CDP4Common.Extensions;
using CDP4Common.SiteDirectoryData;

using NUnit.Framework;

/// <summary>
/// Suite of tests for the <see cref="EnumerationParameterTypeExtensions"/> class
/// </summary>
[TestFixture]
public class EnumerationParameterTypeExtensionsTestFixture
{
private EnumerationParameterType enumerationParameterType;
private EnumerationValueDefinition enumerationValueDefinition1;
private EnumerationValueDefinition enumerationValueDefinition2;
private EnumerationValueDefinition enumerationValueDefinition3;

[SetUp]
public void SetUp()
{
this.enumerationParameterType = new EnumerationParameterType(Guid.NewGuid(), null, null);
this.enumerationValueDefinition1 = new EnumerationValueDefinition(Guid.NewGuid(), null, null) {Name = "Value 1", ShortName = "val1"};
this.enumerationValueDefinition2 = new EnumerationValueDefinition(Guid.NewGuid(), null, null) { Name = "Value 2", ShortName = "val2" };
this.enumerationValueDefinition3 = new EnumerationValueDefinition(Guid.NewGuid(), null, null) { Name = "Value 3", ShortName = "val3" };

this.enumerationParameterType.ValueDefinition.Add(this.enumerationValueDefinition1);
this.enumerationParameterType.ValueDefinition.Add(this.enumerationValueDefinition2);
this.enumerationParameterType.ValueDefinition.Add(this.enumerationValueDefinition3);
}

[Test]
public void VerifyExpressionStringsAtThingLevel()
{
Assert.That(this.enumerationParameterType.ResolveNames("-"), Is.EquivalentTo(new List<string> { "-" }));
Assert.That(this.enumerationParameterType.ResolveNames("val1"), Is.EquivalentTo(new List<string> { "Value 1" }));
Assert.That(this.enumerationParameterType.ResolveNames("val1|val2"), Is.EquivalentTo(new List<string> { "Value 1", "Value 2" }));
Assert.That(this.enumerationParameterType.ResolveNames("val1|val3"), Is.EquivalentTo(new List<string> { "Value 1", "Value 3" }));
Assert.That(() => this.enumerationParameterType.ResolveNames("val1|val4"), Throws.TypeOf<Cdp4ModelValidationException>());
}
}
}
98 changes: 98 additions & 0 deletions CDP4Common/Extensions/EnumerationParameterTypeExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="EnumerationParameterTypeExtensions.cs" company="RHEA System S.A.">
// Copyright (c) 2015-2024 RHEA System S.A.
//
// Author: Sam Gerené, Alex Vorobiev, Alexander van Delft, Nathanael Smiechowski,
// Antoine Théate, Omar Elebiary, Jaime Bernar
//
// This file is part of CDP4-COMET SDK Community Edition
//
// The CDP4-COMET SDK Community Edition is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 3 of the License, or (at your option) any later version.
//
// The CDP4-COMET SDK Community Edition is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this program; if not, write to the Free Software Foundation,
// Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
// </copyright>
// --------------------------------------------------------------------------------------------------------------------

namespace CDP4Common.Extensions
{
using System.Collections.Generic;
using System.Linq;

using CDP4Common.EngineeringModelData;
using CDP4Common.Exceptions;
using CDP4Common.Helpers;
using CDP4Common.SiteDirectoryData;

/// <summary>
/// This class contains methods for specific ParametricConstraint related functionality
/// </summary>
public static class EnumerationParameterTypeExtensions
{
/// <summary>
/// Resolves the names of a <see cref="Parameter"/> of type <see cref="EnumerationParameterType"/>, based on the strng representation
/// according to a <see cref="ParameterValueSet"/>
/// </summary>
/// <param name="enumerationParameterType">The <see cref="EnumerationParameterType"/> for which the names will be resolved</param>
/// <param name="value">The value to resolve as an object</param>
/// <returns>A collection of <see cref="string"/>s that represent the Name(s) of the selected <see cref="EnumerationValueDefinition"/>s</returns>
/// <exception cref="Cdp4ModelValidationException">When a specific ShortName is not found in the <paramref name="value"/></exception>
public static IEnumerable<string> ResolveNames(this EnumerationParameterType enumerationParameterType, object value)
{
var result = new List<string>();

if (value is string stringValue)
{
if (stringValue == "-")
{
result.Add(stringValue);
}
else
{
var values = stringValue.Split(Constants.MultiValueEnumSeparator);

foreach (var enumerationValue in values)
{
var name =
enumerationParameterType.ValueDefinition
.SingleOrDefault(x => x.ShortName == enumerationValue.Trim())?
.Name;

if (name == null)
{
var joinedShortnames = string.Empty;
var sortedItems = enumerationParameterType.ValueDefinition.SortedItems.Values;

foreach (var enumerationValueDefinition in sortedItems)
{
if (joinedShortnames == string.Empty)
{
joinedShortnames = enumerationValueDefinition.ShortName;
}
else
{
joinedShortnames = joinedShortnames + ", " + enumerationValueDefinition.ShortName;
}
}

throw new Cdp4ModelValidationException($"The {enumerationParameterType.Name} Enumeration Parametertype does not contain the following value definition {enumerationValue}, allowed values are: {joinedShortnames}");
}

result.Add(name);
}
}
}

return result;
}
}
}

0 comments on commit da814f6

Please sign in to comment.