// Copyright (C) Stichting Deltares 2025. All rights reserved. // // This file is part of the application DAM - UI. // // DAM - UI is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // This program 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 General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program. If not, see . // // All names, logos, and references to "Deltares" are registered trademarks of // Stichting Deltares and remain full property of Stichting Deltares at all times. // All rights reserved. using System.Collections.Generic; using System.Linq; using Deltares.Dam.Data; using Deltares.Dam.Data.Specifications; using Deltares.Standard.Specifications.Extensions; using NUnit.Framework; namespace Deltares.Standard.Specifications.Tests { /// /// Summary description for UnitTest1 /// [TestFixture] public class SpecifcationTest { /// /// Composite specifications test /// Define multiple specifications and compose them as one /// [Test] public void CompositionTest() { var m = new Model(); var c = new Cluster { Id = 1 }; var clusters = new List { c }; // Compose the specifications as one CompositeSpecification clusterValid = new ClusterValidIdSpecification(); // -> see domain model for definition ISpecification clusterInCollection = new ClusterInCollection(clusters); // -> see domain model for definition ISpecification validAndNotInCollection = clusterValid.And(clusterInCollection).Not(); const bool expected = false; bool actual = validAndNotInCollection.IsSatisfiedBy(c); Assert.That(actual, Is.EqualTo(expected)); } /// /// Parameterized specification /// [Test] public void ParameterizedSpecificationTest() { var m = new Model { Id = 2 }; // not very usefull in practice but for this test ok // just test to see if id equals 3. The model id is 2 so // the rule will not be satisfied var modelId = new ModelUniqueIdSpecification(3); Assert.That(modelId.IsSatisfiedBy(m), Is.False); // Changing the id to 3 and re-evaluate the rule // Now it will be satisfied by the candidate m.Id = 3; Assert.That(modelId.IsSatisfiedBy(m), Is.True); } /// /// Uses the LayerHasValidTopBottomValues specification for selecting /// [Test] public void SelectBySpecificationTest() { var c = new Cluster(); c.Layers.AddRange(new[] { new Layer { Bottom = 10, Top = 20 }, new Layer { Bottom = 20, Top = 40 }, new Layer { Bottom = 10, Top = 9 } // -> not satisfied by specification LayerHasValidTopBottomValues }); Layer[] validLayers = c.Layers.GetBySpecification(new LayerHasValidTopBottomValues()).ToArray(); Assert.That(validLayers, Has.Length.EqualTo(2)); // Change the state of the first layer and test again for validation validLayers[0].Top = 1; const bool expected = false; bool actual = validLayers[0].IsSatisfiedBySpecification(new LayerHasValidTopBottomValues()); Assert.That(actual, Is.EqualTo(expected)); } /// /// Test the min max attributes /// The soil is decorated with two attributes MinimumAttribute with value 20 /// and a MaximumAttribute with value -1 /// [Test] public void ValidateWithMinMaxAttributesTest() { var s = new Soil(); // -> GammaWet and GammaDry will be 0 (defaults) // Validator will return 2 items because 2 specifacations are not satisfied // GammaWet hase spec Minimum 20 and GammaDry has spec Maximum -1 IEnumerable vr = s.Validate(); Assert.That(vr, Is.Not.Null.And.Count.EqualTo(2)); } /// /// Tests the SpecificationAttribute for validating /// In the class Layer the LayerHasValidTopBottomValues (see the test domain model) specification used /// [Test] public void ValidateWithSpecificationAttributeTest() { // setup var l = new Layer { Bottom = 10, Top = 9, Name = "abcYz" }; // call IEnumerable vr = l.Validate(); // check Assert.That(vr, Is.Not.Null.And.Not.Empty); // <- should have at least one broken validation rule Assert.That((new LayerHasValidTopBottomValues()).Description, Is.EqualTo(vr.ElementAt(0).Text)); } } }