Skip to content

Commit

Permalink
[Implement] ParameterTypeRuleChecker for MA-0700 and MA-0710
Browse files Browse the repository at this point in the history
[Implement] CategoryRuleChecker for MA-0720 and MA-0730

[Implement] ParameterTypeRuleChecker for MA-0740

[Implement] QuantityKindRuleChecker for MA-0750
  • Loading branch information
samatrhea committed Sep 1, 2019
1 parent df31e7e commit 91a4fd6
Show file tree
Hide file tree
Showing 8 changed files with 540 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,25 @@ public class CategoryRuleCheckerTestFixture
{
private CategoryRuleChecker categoryRuleChecker;

private SiteReferenceDataLibrary siteReferenceDataLibrary;
private Category category_1;
private Category category_2;
private Category category_3;

[SetUp]
public void SetUp()
{
this.categoryRuleChecker = new CategoryRuleChecker();

this.siteReferenceDataLibrary = new SiteReferenceDataLibrary();

this.category_1 = new Category { Iid = Guid.Parse("da4d1677-3f02-4272-965e-7a21f6b14b60") };
this.category_2 = new Category { Iid = Guid.Parse("f3813784-8420-452f-9f85-2c82756a4fa3") };
this.category_3 = new Category { Iid = Guid.Parse("82b811af-5e67-4304-9dd7-cb7b9d8a5a07") };

this.siteReferenceDataLibrary.DefinedCategory.Add(this.category_1);
this.siteReferenceDataLibrary.DefinedCategory.Add(this.category_2);
this.siteReferenceDataLibrary.DefinedCategory.Add(this.category_3);
}

[Test]
Expand Down Expand Up @@ -96,5 +111,35 @@ public void Verify_that_when_referenced_categories_are_in_the_chain_of_Rdls_no_r

Assert.That(results, Is.Empty);
}

[Test]
public void Verify_that_when_A_Category_ShortName_is_not_unique_in_the_containing_RDL_a_result_is_returned()
{
this.category_1.ShortName = "CAT";
this.category_2.ShortName = "CAT";
this.category_3.ShortName = "CAT3";

var result = this.categoryRuleChecker.CheckWhetherTheCategoryShortNameIsUniqueInTheContainerRdl(this.category_1).Single();

Assert.That(result.Id, Is.EqualTo("MA-0720"));
Assert.That(result.Description, Is.EqualTo("The Category does not have a unique ShortNames in the container Reference Data Library - f3813784-8420-452f-9f85-2c82756a4fa3:CAT"));
Assert.That(result.Thing, Is.EqualTo(this.category_1));
Assert.That(result.Severity, Is.EqualTo(SeverityKind.Error));
}

[Test]
public void Verify_that_when_A_Category_Name_is_not_unique_in_the_containing_RDL_a_result_is_returned()
{
this.category_1.Name = "CAT";
this.category_2.Name = "CAT";
this.category_3.Name = "CAT3";

var result = this.categoryRuleChecker.CheckWhetherTheCategoryNameIsUniqueInTheContainerRdl(this.category_1).Single();

Assert.That(result.Id, Is.EqualTo("MA-0730"));
Assert.That(result.Description, Is.EqualTo("The Category does not have a unique Names in the container Reference Data Library - f3813784-8420-452f-9f85-2c82756a4fa3:CAT"));
Assert.That(result.Thing, Is.EqualTo(this.category_1));
Assert.That(result.Severity, Is.EqualTo(SeverityKind.Error));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -214,5 +214,33 @@ public void Verify_that_when_DeprecatableThing_Is_Deprecated_no_result_is_return

Assert.That(results, Is.Empty);
}

[Test]
public void Verify_that_The_ParameterTypeComponent_has_a_valid_scale()
{
var scale = new RatioScale();
var textParameterType = new TextParameterType();
var simpleQuantityKind = new SimpleQuantityKind();

this.parameterTypeComponent.Scale = scale;
this.parameterTypeComponent.ParameterType = textParameterType;

var result = this.parameterTypeComponentRuleChecker.ChecksWhetherTheParameterTypeComponentHasAValidScale(this.parameterTypeComponent).Single();

Assert.That(result.Id, Is.EqualTo("MA-0740"));
Assert.That(result.Description, Is.EqualTo("When the referenced ParameterType is a not QuantityKind, the ParameterTypeComponent.Scale must be null"));
Assert.That(result.Thing, Is.EqualTo(this.parameterTypeComponent));
Assert.That(result.Severity, Is.EqualTo(SeverityKind.Error));

this.parameterTypeComponent.Scale = null;
this.parameterTypeComponent.ParameterType = simpleQuantityKind;

result = this.parameterTypeComponentRuleChecker.ChecksWhetherTheParameterTypeComponentHasAValidScale(this.parameterTypeComponent).Single();

Assert.That(result.Id, Is.EqualTo("MA-0740"));
Assert.That(result.Description, Is.EqualTo("When the referenced ParameterType is a QuantityKind, the ParameterTypeComponent.Scale may not be null"));
Assert.That(result.Thing, Is.EqualTo(this.parameterTypeComponent));
Assert.That(result.Severity, Is.EqualTo(SeverityKind.Error));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
// <copyright file="ParameterTypeRuleCheckerTestFixture.cs" company="RHEA System S.A.">
// Copyright (c) 2015-2019 RHEA System S.A.
//
// Author: Sam Gerené
//
// This file is part of CDP4-SDK Community Edition
//
// The CDP4-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-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 CDP4Rules.NetCore.Tests.RuleCheckers
{
using System;
using System.Linq;
using CDP4Common.CommonData;
using CDP4Common.SiteDirectoryData;
using CDP4Rules.Common;
using CDP4Rules.RuleCheckers;
using NUnit.Framework;

/// <summary>
/// Suite of tests for the <see cref="ParameterTypeRuleChecker"/> class.
/// </summary>
[TestFixture]
public class ParameterTypeRuleCheckerTestFixture
{
private ParameterTypeRuleChecker parameterTypeRuleChecker;

private SiteReferenceDataLibrary siteReferenceDataLibrary;

private TextParameterType parameterType_1;

private TextParameterType parameterType_2;

private TextParameterType parameterType_3;

[SetUp]
public void SetUp()
{
this.parameterTypeRuleChecker = new ParameterTypeRuleChecker();

this.siteReferenceDataLibrary = new SiteReferenceDataLibrary();
this.parameterType_1 = new TextParameterType { Iid = Guid.Parse("d501f2e8-896f-4027-b0e4-c0fe6b8507b5") };
this.parameterType_2 = new TextParameterType { Iid = Guid.Parse("6fb0c4a0-fd3f-444e-9efb-dad00f08cf2f") };
this.parameterType_3 = new TextParameterType { Iid = Guid.Parse("9bf61f1e-65ea-4dc1-9199-41cda57aba4e") };

this.siteReferenceDataLibrary.ParameterType.Add(this.parameterType_1);
this.siteReferenceDataLibrary.ParameterType.Add(this.parameterType_2);
this.siteReferenceDataLibrary.ParameterType.Add(this.parameterType_3);
}

[Test]
public void Verify_that_when_thing_is_null_exception_is_thrown()
{
Assert.Throws<ArgumentNullException>(() => this.parameterTypeRuleChecker.CheckWhetherTheParameterTypeShortNameIsUniqueInTheContainerRdl(null));
}

[Test]
public void Verify_that_when_thing_is_not_a_ParameterType_exception_is_thrown()
{
var alias = new Alias();
Assert.Throws<ArgumentException>(() => this.parameterTypeRuleChecker.CheckWhetherTheParameterTypeShortNameIsUniqueInTheContainerRdl(alias));
}

[Test]
public void Verify_that_when_A_ParameterType_Shortname_is_not_unique_in_the_containing_RDL_a_result_is_returned()
{
this.parameterType_1.ShortName = "TEXT";
this.parameterType_2.ShortName = "TEXT";
this.parameterType_3.ShortName = "OTHERTEXT";

var result = this.parameterTypeRuleChecker.CheckWhetherTheParameterTypeShortNameIsUniqueInTheContainerRdl(this.parameterType_1).Single();

Assert.That(result.Id, Is.EqualTo("MA-0700"));
Assert.That(result.Description, Is.EqualTo("The ParameterType does not have a unique ShortNames in the container Reference Data Library - 6fb0c4a0-fd3f-444e-9efb-dad00f08cf2f:TEXT"));
Assert.That(result.Thing, Is.EqualTo(this.parameterType_1));
Assert.That(result.Severity, Is.EqualTo(SeverityKind.Error));
}

[Test]
public void Verify_that_when_A_ParameterType_Symbol_is_not_unique_in_the_containing_RDL_a_result_is_returned()
{
this.parameterType_1.Symbol = "TEXT";
this.parameterType_2.Symbol = "TEXT";
this.parameterType_3.Symbol = "OTHERTEXT";

var result = this.parameterTypeRuleChecker.CheckWhetherTheParameterTypeSymbolIsUniqueInTheContainerRdl(this.parameterType_1).Single();

Assert.That(result.Id, Is.EqualTo("MA-0710"));
Assert.That(result.Description, Is.EqualTo("The ParameterType does not have a unique Symbols in the container Reference Data Library - 6fb0c4a0-fd3f-444e-9efb-dad00f08cf2f:TEXT"));
Assert.That(result.Thing, Is.EqualTo(this.parameterType_1));
Assert.That(result.Severity, Is.EqualTo(SeverityKind.Error));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -170,5 +170,45 @@ public void Verify_that_when_DeprecatableThing_Is_Deprecated_no_result_is_return

Assert.That(results, Is.Empty);
}

[Test]
public void Verify_that_when_the_DefaultScale_is_not_in_the_PossibleScales_a_result_is_returned()
{
var ratioScale_1 = new RatioScale { Iid = Guid.Parse("9fd6eb4f-5fb8-469f-8798-20bf4dd84e95"), ShortName = "scale_1" };
var ratioScale_2 = new RatioScale { Iid = Guid.Parse("eed266d7-3ce5-4117-8ca4-2db316ec9cc6"), ShortName = "scale_2" };
var ratioScale_3 = new RatioScale { Iid = Guid.Parse("918abf39-87af-4305-91e8-28c17ab274b2"), ShortName = "scale_3" };

var sqk = new SimpleQuantityKind();
sqk.PossibleScale.Add(ratioScale_1);
sqk.PossibleScale.Add(ratioScale_2);

sqk.DefaultScale = ratioScale_3;

var result = this.quantityKindRuleChecker.ChecksWhetherReferencedDefaultScaleIsInTheCollectionOfPossibleScales(sqk).Single();

Assert.That(result.Id, Is.EqualTo("MA-0750"));
Assert.That(result.Description, Is.EqualTo("The QuantityKind.DefaultScale 918abf39-87af-4305-91e8-28c17ab274b2:scale_3 is not in the list of QuantityKind.PossibleScale"));
Assert.That(result.Thing, Is.EqualTo(sqk));
Assert.That(result.Severity, Is.EqualTo(SeverityKind.Error));
}
[Test]
public void Verify_that_when_the_DefaultScale_is_in_the_PossibleScales_no_result_is_returned()
{
var ratioScale_1 = new RatioScale { Iid = Guid.Parse("9fd6eb4f-5fb8-469f-8798-20bf4dd84e95"), ShortName = "scale_1" };
var ratioScale_2 = new RatioScale { Iid = Guid.Parse("eed266d7-3ce5-4117-8ca4-2db316ec9cc6"), ShortName = "scale_2" };
var ratioScale_3 = new RatioScale { Iid = Guid.Parse("918abf39-87af-4305-91e8-28c17ab274b2"), ShortName = "scale_3" };

var sqk = new SimpleQuantityKind();
sqk.PossibleScale.Add(ratioScale_1);
sqk.PossibleScale.Add(ratioScale_2);
sqk.PossibleScale.Add(ratioScale_3);

sqk.DefaultScale = ratioScale_3;

var results = this.quantityKindRuleChecker.ChecksWhetherReferencedDefaultScaleIsInTheCollectionOfPossibleScales(sqk);

Assert.That(results, Is.Empty);
}

}
}
80 changes: 80 additions & 0 deletions CDP4Rules/RuleCheckers/CategoryRuleChecker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,86 @@ namespace CDP4Rules.RuleCheckers
[RuleChecker(typeof(Category))]
public class CategoryRuleChecker : RuleChecker
{
/// <summary>
/// Checks whether the <see cref="Category.ShortName"/> is case-sensitive unique within its containing <see cref="ReferenceDataLibrary"/>
/// </summary>
/// <param name="thing">
/// The subject <see cref="Category"/>
/// </param>
/// <returns>
/// An <see cref="IEnumerable{RuleCheckResult}"/> which is empty when no rule violations are encountered.
/// </returns>
/// <exception cref="ArgumentNullException">
/// thrown when <paramref name="thing"/> is null
/// </exception>
/// <exception cref="ArgumentException">
/// thrown when <paramref name="thing"/> is not an <see cref="Category"/>
/// </exception>
[Rule("MA-0720")]
public IEnumerable<RuleCheckResult> CheckWhetherTheCategoryShortNameIsUniqueInTheContainerRdl(Thing thing)
{
var category = this.VerifyThingArgument(thing);

var results = new List<RuleCheckResult>();
var ruleAttribute = System.Reflection.MethodBase.GetCurrentMethod().GetCustomAttribute<RuleAttribute>();
var rule = StaticRuleProvider.QueryRules().Single(r => r.Id == ruleAttribute.Id);

var referenceDataLibrary = (ReferenceDataLibrary)thing.Container;

var duplicates = referenceDataLibrary.DefinedCategory.Where(p => p.ShortName == category.ShortName && p.Iid != category.Iid);

if (duplicates.Any())
{
var duplicateIdentifiers = string.Join(",", duplicates.Select(p => p.Iid));
var duplicateShortNames = string.Join(",", duplicates.Select(p => p.ShortName));

var result = new RuleCheckResult(thing, rule.Id, $"The Category does not have a unique ShortNames in the container Reference Data Library - {duplicateIdentifiers}:{duplicateShortNames}", SeverityKind.Error);
results.Add(result);
}

return results;
}

/// <summary>
/// Checks whether the <see cref="Category.Name"/> is case-sensitive unique within its containing <see cref="ReferenceDataLibrary"/>
/// </summary>
/// <param name="thing">
/// The subject <see cref="Category"/>
/// </param>
/// <returns>
/// An <see cref="IEnumerable{RuleCheckResult}"/> which is empty when no rule violations are encountered.
/// </returns>
/// <exception cref="ArgumentNullException">
/// thrown when <paramref name="thing"/> is null
/// </exception>
/// <exception cref="ArgumentException">
/// thrown when <paramref name="thing"/> is not an <see cref="Category"/>
/// </exception>
[Rule("MA-0730")]
public IEnumerable<RuleCheckResult> CheckWhetherTheCategoryNameIsUniqueInTheContainerRdl(Thing thing)
{
var category = this.VerifyThingArgument(thing);

var results = new List<RuleCheckResult>();
var ruleAttribute = System.Reflection.MethodBase.GetCurrentMethod().GetCustomAttribute<RuleAttribute>();
var rule = StaticRuleProvider.QueryRules().Single(r => r.Id == ruleAttribute.Id);

var referenceDataLibrary = (ReferenceDataLibrary)thing.Container;

var duplicates = referenceDataLibrary.DefinedCategory.Where(p => p.Name == category.Name && p.Iid != category.Iid);

if (duplicates.Any())
{
var duplicateIdentifiers = string.Join(",", duplicates.Select(p => p.Iid));
var duplicateShortNames = string.Join(",", duplicates.Select(p => p.Name));

var result = new RuleCheckResult(thing, rule.Id, $"The Category does not have a unique Names in the container Reference Data Library - {duplicateIdentifiers}:{duplicateShortNames}", SeverityKind.Error);
results.Add(result);
}

return results;
}

/// <summary>
/// Checks whether a referenced <see cref="Category"/> is the in chain of Reference Data Libraries
/// </summary>
Expand Down
Loading

0 comments on commit 91a4fd6

Please sign in to comment.