From da814f64d39011503796be03249226953fe09e7f Mon Sep 17 00:00:00 2001 From: Alexander van Delft <56023674+lxatrhea@users.noreply.github.com> Date: Thu, 15 Feb 2024 17:34:01 +0100 Subject: [PATCH] [ADD] ResolveName extension Method for EnumerationParameterType; fixes #310 --- ...ationParameterTypeExtensionsTestFixture.cs | 72 ++++++++++++++ ...ationParameterTypeExtensionsTestFixture.cs | 71 ++++++++++++++ .../EnumerationParameterTypeExtensions.cs | 98 +++++++++++++++++++ 3 files changed, 241 insertions(+) create mode 100644 CDP4Common.NetCore.Tests/Extensions/EnumerationParameterTypeExtensionsTestFixture.cs create mode 100644 CDP4Common.Tests/Extensions/EnumerationParameterTypeExtensionsTestFixture.cs create mode 100644 CDP4Common/Extensions/EnumerationParameterTypeExtensions.cs diff --git a/CDP4Common.NetCore.Tests/Extensions/EnumerationParameterTypeExtensionsTestFixture.cs b/CDP4Common.NetCore.Tests/Extensions/EnumerationParameterTypeExtensionsTestFixture.cs new file mode 100644 index 00000000..705cae54 --- /dev/null +++ b/CDP4Common.NetCore.Tests/Extensions/EnumerationParameterTypeExtensionsTestFixture.cs @@ -0,0 +1,72 @@ +// -------------------------------------------------------------------------------------------------------------------- +// +// 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. +// +// -------------------------------------------------------------------------------------------------------------------- + +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; + + /// + /// Suite of tests for the class + /// + [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 { "-" })); + Assert.That(this.enumerationParameterType.ResolveNames("val1"), Is.EquivalentTo(new List { "Value 1" })); + Assert.That(this.enumerationParameterType.ResolveNames("val1|val2"), Is.EquivalentTo(new List { "Value 1", "Value 2" })); + Assert.That(this.enumerationParameterType.ResolveNames("val1|val3"), Is.EquivalentTo(new List { "Value 1", "Value 3" })); + Assert.That(() => this.enumerationParameterType.ResolveNames("val1|val4"), Throws.TypeOf()); + } + } +} diff --git a/CDP4Common.Tests/Extensions/EnumerationParameterTypeExtensionsTestFixture.cs b/CDP4Common.Tests/Extensions/EnumerationParameterTypeExtensionsTestFixture.cs new file mode 100644 index 00000000..155bda11 --- /dev/null +++ b/CDP4Common.Tests/Extensions/EnumerationParameterTypeExtensionsTestFixture.cs @@ -0,0 +1,71 @@ +// -------------------------------------------------------------------------------------------------------------------- +// +// 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. +// +// -------------------------------------------------------------------------------------------------------------------- + +namespace CDP4Common.Tests.Extensions +{ + using System; + using System.Collections.Generic; + + using CDP4Common.Exceptions; + using CDP4Common.Extensions; + using CDP4Common.SiteDirectoryData; + + using NUnit.Framework; + + /// + /// Suite of tests for the class + /// + [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 { "-" })); + Assert.That(this.enumerationParameterType.ResolveNames("val1"), Is.EquivalentTo(new List { "Value 1" })); + Assert.That(this.enumerationParameterType.ResolveNames("val1|val2"), Is.EquivalentTo(new List { "Value 1", "Value 2" })); + Assert.That(this.enumerationParameterType.ResolveNames("val1|val3"), Is.EquivalentTo(new List { "Value 1", "Value 3" })); + Assert.That(() => this.enumerationParameterType.ResolveNames("val1|val4"), Throws.TypeOf()); + } + } +} diff --git a/CDP4Common/Extensions/EnumerationParameterTypeExtensions.cs b/CDP4Common/Extensions/EnumerationParameterTypeExtensions.cs new file mode 100644 index 00000000..159f79ad --- /dev/null +++ b/CDP4Common/Extensions/EnumerationParameterTypeExtensions.cs @@ -0,0 +1,98 @@ +// -------------------------------------------------------------------------------------------------------------------- +// +// 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. +// +// -------------------------------------------------------------------------------------------------------------------- + +namespace CDP4Common.Extensions +{ + using System.Collections.Generic; + using System.Linq; + + using CDP4Common.EngineeringModelData; + using CDP4Common.Exceptions; + using CDP4Common.Helpers; + using CDP4Common.SiteDirectoryData; + + /// + /// This class contains methods for specific ParametricConstraint related functionality + /// + public static class EnumerationParameterTypeExtensions + { + /// + /// Resolves the names of a of type , based on the strng representation + /// according to a + /// + /// The for which the names will be resolved + /// The value to resolve as an object + /// A collection of s that represent the Name(s) of the selected s + /// When a specific ShortName is not found in the + public static IEnumerable ResolveNames(this EnumerationParameterType enumerationParameterType, object value) + { + var result = new List(); + + 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; + } + } +}