Skip to content

Commit

Permalink
[Add] RequirementRuleChecker for MA-0610 and MA-0800
Browse files Browse the repository at this point in the history
  • Loading branch information
samatrhea committed Sep 1, 2019
1 parent 91a4fd6 commit fe086e9
Show file tree
Hide file tree
Showing 2 changed files with 218 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
// <copyright file="RequirementRuleCheckerTestFixture.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.EngineeringModelData;
using CDP4Common.SiteDirectoryData;
using CDP4Rules.Common;
using CDP4Rules.RuleCheckers;
using NUnit.Framework;

/// <summary>
/// Suite of tests for the <see cref="RequirementRuleChecker"/> class.
/// </summary>
[TestFixture]
public class RequirementRuleCheckerTestFixture
{
private RequirementRuleChecker requirementRuleChecker;

private RequirementsSpecification requirementsSpecification;

private Requirement requirement_1;

private Requirement requirement_2;

private Requirement requirement_3;

[SetUp]
public void SetUp()
{
this.requirementRuleChecker = new RequirementRuleChecker();

this.requirementsSpecification = new RequirementsSpecification();
this.requirement_1 = new Requirement { Iid = Guid.Parse("0816f4b2-7715-47be-88c1-514530bca0c2") };
this.requirement_2 = new Requirement { Iid = Guid.Parse("998f7f11-0153-4331-b7ee-33e36b278d3a") };
this.requirement_3 = new Requirement { Iid = Guid.Parse("ca3a7e32-4862-42c9-8435-b4f7dbdfac83") };

this.requirementsSpecification.Requirement.Add(this.requirement_1);
this.requirementsSpecification.Requirement.Add(this.requirement_2);
this.requirementsSpecification.Requirement.Add(this.requirement_3);
}

[Test]
public void Verify_when_Check_is_called_with_null_exception_is_thrown()
{
Assert.Throws<ArgumentNullException>(() => this.requirementRuleChecker.CheckWhetherTheRequirementShortNameIsUniqueInContainerRequirementsSpecification(null));
}

[Test]
public void Verify_when_Check_is_called_with_non_Category_exception_is_thrown()
{
var alias = new Alias();

Assert.Throws<ArgumentException>(() => this.requirementRuleChecker.CheckWhetherTheRequirementShortNameIsUniqueInContainerRequirementsSpecification(alias));
}

[Test]
public void Verify_that_when_requirement_ShortNames_are_not_unique_within_RequirementsSpecification_result_is_returned()
{
this.requirement_1.ShortName = "REQ-1";
this.requirement_2.ShortName = "REQ-1";
this.requirement_3.ShortName = "REQ-3";

var result = this.requirementRuleChecker.CheckWhetherTheRequirementShortNameIsUniqueInContainerRequirementsSpecification(this.requirement_1).Single();

Assert.That(result.Id, Is.EqualTo("MA-0800"));
Assert.That(result.Thing, Is.EqualTo(requirement_1));
Assert.That(result.Description, Is.EqualTo("The Requirement.ShortName is not unique within the container RequirementsSpecification: 998f7f11-0153-4331-b7ee-33e36b278d3a:REQ-1"));
Assert.That(result.Severity, Is.EqualTo(SeverityKind.Warning));
}

[Test]
public void Verify_that_when_requirement_ShortNames_are_unique_within_RequirementsSpecification_no_result_is_returned()
{
this.requirement_1.ShortName = "REQ-1";
this.requirement_2.ShortName = "REQ-2";
this.requirement_3.ShortName = "REQ-3";

var results = this.requirementRuleChecker.CheckWhetherTheRequirementShortNameIsUniqueInContainerRequirementsSpecification(this.requirement_1);

Assert.That(results, Is.Empty);
}
}
}
111 changes: 111 additions & 0 deletions CDP4Rules/RuleCheckers/RequirementRuleChecker.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
// <copyright file="RequirementRuleChecker.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.RuleCheckers
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using CDP4Common.CommonData;
using CDP4Common.EngineeringModelData;
using CDP4Rules.Common;

/// <summary>
/// The purpose of the <see cref="RequirementRuleChecker"/> is to execute the rules for instances of type <see cref="Requirement"/>
/// </summary>
[RuleChecker(typeof(Requirement))]
public class RequirementRuleChecker : RuleChecker
{
/// <summary>
/// Checks whether the <see cref="DefinedThing.ShortName"/> is unique within the container <see cref="RequirementsSpecification"/>
/// </summary>
/// <param name="thing">
/// The subject <see cref="Requirement"/>
/// </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="Requirement"/>
/// </exception>
[Rule("MA-0800")]
public IEnumerable<RuleCheckResult> CheckWhetherTheRequirementShortNameIsUniqueInContainerRequirementsSpecification(Thing thing)
{
var requirement = 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 requirementsSpecification = (RequirementsSpecification)thing.Container;

var duplicates = requirementsSpecification.Requirement.Where(r => r.ShortName == requirement.ShortName && r.Iid != requirement.Iid);

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

var result = new RuleCheckResult(thing, rule.Id, $"The Requirement.ShortName is not unique within the container RequirementsSpecification: {duplicateIdentifiers}:{duplicateShortNames}", SeverityKind.Warning);
results.Add(result);
}

return results;
}

/// <summary>
/// Verifies that the <see cref="Thing"/> is of the correct type
/// </summary>
/// <param name="thing">
/// the subject <see cref="Thing"/>
/// </param>
/// <returns>
/// an instance of <see cref="Requirement"/>
/// </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="Requirement"/>
/// </exception>
private Requirement VerifyThingArgument(Thing thing)
{
if (thing == null)
{
throw new ArgumentNullException($"The {nameof(thing)} may not be null");
}

var requirement = thing as Requirement;
if (requirement == null)
{
throw new ArgumentException($"{nameof(thing)} with Iid:{thing.Iid} is not an Requirement");
}

return requirement;
}
}
}

0 comments on commit fe086e9

Please sign in to comment.