// Copyright (C) Stichting Deltares 2023. 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.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.AreEqual(expected, actual); } /// /// 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.IsFalse(modelId.IsSatisfiedBy(m)); // Changing the id to 3 and re-evaluate the rule // Now it will be satisfied by the candidate m.Id = 3; Assert.IsTrue(modelId.IsSatisfiedBy(m)); } /// /// 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.IsTrue(validLayers != null && validLayers.Length == 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.AreEqual(expected, actual); } /// /// 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 var vr = s.Validate(); Assert.IsTrue(vr != null && vr.Count() == 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 var vr = l.Validate(); // check Assert.IsTrue(vr.Any()); // <- should have at least one broken validation rule Assert.AreEqual(vr.ElementAt(0).Text, (new LayerHasValidTopBottomValues()).Description); } } }