using System; using System.Reflection; using Deltares.Geotechnics; using Deltares.Probabilistic; using Deltares.Standard; using Deltares.Standard.EventPublisher; using Deltares.Standard.Reflection; using NUnit.Framework; namespace Deltares.DSoilModel.Data.Tests { [TestFixture] public class DSoilModelTransformerTests { private bool oldIsDataEventPublishStopped; [TestFixtureSetUp] public void FixtureSetup() { oldIsDataEventPublishStopped = DataEventPublisher.IsDataEventPublishStopped; DataEventPublisher.IsDataEventPublishStopped = true; TransformerManager.Transformer = new DSoilModelTransformer(); } [TestFixtureTearDown] public void FixtureTearDown() { DataEventPublisher.IsDataEventPublishStopped = oldIsDataEventPublishStopped; } private Stochast FindAutoStochastProperty(object obj, string property) { var instance = TransformerManager.Transformer as DSoilModelTransformer; return (Stochast)instance.GetType().GetMethod("FindAutoStochastProperty", BindingFlags.NonPublic | BindingFlags.Instance).Invoke(instance, new[] {obj, property}); } [TestCase(typeof(Soil))] [TestCase(typeof(PreConsolidationStress))] public void TestFindAutoStochastProperty(Type objType) { var obj = Activator.CreateInstance(objType); var stochastProperties = PropertyInfoSupport.GetPropertiesOfType(obj.GetType(), typeof (Stochast)); foreach (BindPropertyInfo propertyInfo in stochastProperties) { var stochast = (Stochast) propertyInfo.GetValue(obj, null); Assert.AreSame(stochast, FindAutoStochastProperty(obj, stochast.AssociatedPropertyName)); } } [Test] public void FindAutoStochastPropertyTest_ReturnsNullForUnknownProperty() { Assert.IsNull(FindAutoStochastProperty(new SosSoilLayer1D(), "Property")); Assert.IsNull(FindAutoStochastProperty(new SosSoilLayer1D(), null)); } [Test] [TestCase(typeof(Soil), "Cohesion")] [TestCase(typeof(Soil), "BeddingAngle")] [TestCase(typeof(PreConsolidationStress), "StressValue")] public void TestStochasticPropertiesUncoupled(Type objType, string propertyName) { var obj = Activator.CreateInstance(objType); var deterministicProperty = obj.GetType().GetProperty(propertyName); var stochasticProperty = FindAutoStochastProperty(obj, propertyName); // reusing method that we already test var changedValue = 123.456; deterministicProperty.SetValue(obj, changedValue, null); Assert.AreEqual(changedValue, deterministicProperty.GetValue(obj, null)); // deterministic property indeed changed Assert.AreNotEqual(changedValue, stochasticProperty.Mean); // but stochastic property remained its mean value } [Test] public void GetTransformedValueTest_ReturnsInitialValueIfNotSupported() { var transformer = new DSoilModelTransformer(); Assert.AreEqual(44, transformer.GetTransformedValue(new SosSoilLayer1D(), "Property", 44)); Assert.AreEqual(54, transformer.SetTransformedValue(new SosSoilLayer1D(), "Property", 54)); } } }