// Copyright (C) Stichting Deltares 2016. All rights reserved. // // This file is part of the D-Soil Model application. // // The D-Soil Model application 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; using System.Collections.Generic; using System.Diagnostics; using System.Drawing; using System.IO; using System.Xml.Serialization; using Deltares.Geotechnics; using Deltares.Geotechnics.IO; using Deltares.Standard.EventPublisher; using Deltares.Standard.Language; using Deltares.Standard.TestUtils; using KellermanSoftware.CompareNetObjects; using NUnit.Framework; namespace Deltares.DSoilModel.Data.Tests { internal class SosSoilLayer1DTest { private const string TestDataFolder = @"..\..\Deltares.DSoilModel.Data.Tests\TestFiles"; private const string TestOutputFolder = TestDataFolder + @"SoilModelDatabaseTest\"; private bool oldIsDataEventPublishStopped; private LanguageType oldLanguage; [TestFixtureSetUp] public void FixtureSetup() { oldLanguage = LocalizationManager.CurrentLanguage; LocalizationManager.CurrentLanguage = LanguageType.Dutch; oldIsDataEventPublishStopped = DataEventPublisher.IsDataEventPublishStopped; DataEventPublisher.IsDataEventPublishStopped = true; if (!Directory.Exists(TestOutputFolder)) { Directory.CreateDirectory(TestOutputFolder); } } [TestFixtureTearDown] public void FixtureTearDown() { DataEventPublisher.IsDataEventPublishStopped = oldIsDataEventPublishStopped; LocalizationManager.CurrentLanguage = oldLanguage; } [Test] public void DefaultConstructorDefaultValues() { var sosLayer = new SosSoilLayer1D(); Assert.IsNull(sosLayer.MinimumTopLevel); Assert.IsNull(sosLayer.MaximumTopLevel); Assert.IsNullOrEmpty(sosLayer.RemarkTopLevel); Assert.IsNull(sosLayer.Soil); Assert.IsFalse(sosLayer.IsAquifer); Assert.AreEqual(0, sosLayer.TopLevel); } [Test] public void ReadSosLayerFromOldFileReturnsNullproperties() { using (var db = new SoilModelDatabase()) { db.Open(Path.Combine(TestDataFolder, "SoilSegments_version_1_3_3.soil")); var profiles = db.ReadSoilProfiles1D(); Assert.AreEqual(34, profiles.Count); foreach (var soilProfile in profiles) { foreach (var layer in soilProfile.Layers) { var sosLayer = layer as SosSoilLayer1D; Assert.NotNull(sosLayer); Assert.IsNull(sosLayer.MinimumTopLevel); Assert.IsNull(sosLayer.MaximumTopLevel); Assert.IsNullOrEmpty(sosLayer.RemarkTopLevel); } } } } [Test] public void StoreRestoreSosLayerInSoilModelDatabase() { // dummy profile with two SOS layers var sosProfile = new SoilProfile1D { Name = "TestSosProfile" }; var sosProfiles = new List { sosProfile }; var sosLayer = new SosSoilLayer1D { Name = "TestSos1", TopLevel = -10, MaximumTopLevel = -9, MinimumTopLevel = -11, RemarkTopLevel = "remark testSos1", Soil = new Soil("TestSoil1", Color.Beige) }; sosProfile.Layers.Add(sosLayer); sosLayer = new SosSoilLayer1D { Name = "TestSos2", TopLevel = -12, MaximumTopLevel = -11, MinimumTopLevel = -13, RemarkTopLevel = "remark testSos2", Soil = new Soil("TestSoil2", Color.Black) }; sosProfile.Layers.Add(sosLayer); // store var database = new SoilModelDatabase(); var fileName = TestOutputFolder + "TestSosSoilProfile1D.soil"; database.New(fileName); database.WriteSoilProfile1Ds(sosProfiles); database.Close(); //restore database = new SoilModelDatabase(); database.Open(fileName); var sosProfilesCopy = database.ReadSoilProfiles1D(); database.Close(); var co = new CompareLogic(); co.Config.MembersToIgnore = new List { "Id", "Name", "Soil", "Color", "SetCoordinate", "ChangeLog" }; var result = co.Compare(sosProfilesCopy, sosProfiles); Assert.AreEqual(0, result.Differences.Count, "SOSProfileLayer1D fails comparison: " + result.DifferencesString); } [Test] public void PerformanceOfReadingWtiSosData() { // purpose of this test was to measure and improve the database performance while working on issue DSB-264 string fileName = Path.Combine(@"..\..\..\data", @"D-Soil Model\SOS-data", "SosDatabase.soil"); var timer = new Stopwatch(); var database = new SoilModelDatabase(); database.Open(fileName); // note: timings are estimated for build server // soils timer.Restart(); var soils = database.ReadSoils().Soils; timer.Stop(); Console.WriteLine("{0} seconds for reading soils", timer.Elapsed.Seconds); Assert.LessOrEqual(timer.Elapsed.Seconds, 25); Assert.AreEqual(42, soils.Count); // soilprofiles timer.Restart(); var profiles = database.ReadSoilProfiles1D(); timer.Stop(); Console.WriteLine("{0} seconds for reading soil profiles", timer.Elapsed.Seconds); Assert.LessOrEqual(timer.Elapsed.Seconds, 20); // soilsegments timer.Restart(); var segments = database.ReadSoilSegments(); timer.Stop(); Console.WriteLine("{0} seconds for reading soil segments", timer.Elapsed.Seconds); Assert.LessOrEqual(timer.Elapsed.Seconds, 10); // sanity check ! Assert.AreEqual(5574, profiles.Count); foreach (var profile in profiles) { foreach (var layer in profile.Layers) { Assert.IsInstanceOf(typeof(SosSoilLayer1D), layer); var sosLayer = (SosSoilLayer1D)layer; Assert.NotNull(sosLayer.MaximumTopLevel); Assert.NotNull(sosLayer.MinimumTopLevel); } } } [Test] [Category(Categories.Slow)] public void GenerateWtiSosSoilFile() { // purpose here is to convert the WTI_SOS from CSV/SHP to .Soil format in a script, instead of having to use the D-SoilModel GUI for this // generate a .soil file containing the complete Wti SOS, and compare the input / output var sosFolder = Path.Combine(@"..\..\..\data", @"D-Soil Model\SOS-data"); // import SOS data var project = new DSoilModelProject(); Console.WriteLine("reading soil profiles CSV...."); project.ReadSoilProfiles1DFromFile(Path.Combine(sosFolder, "WTISOS_soilprofiles_short_code.csv")); Console.WriteLine("reading Segments CSV...."); DSoilModelIO.ReadCsvSegmentsFromFileAndAddToProject(Path.Combine(sosFolder, "WTOSOS_segments.csv"), project); Console.WriteLine("reading Segments SHP...."); DSoilModelIO.ReadSegmentShapefileAndAddToProject(Path.Combine(sosFolder, "WTI_SOS_alle_segmenten_def.shp"), project, "Segmentnr"); // write database Console.WriteLine("writing to database...."); var database = new SoilModelDatabase(); database.New(Path.Combine(sosFolder, "SosDatabase.soil")); database.WriteSoils(project.Soils.Soils); database.WriteSoilProfile1Ds(project.SoilProfiles1D); database.WriteSoilSegments(project.SoilSegments); database.Close(); // sanity check Console.WriteLine("sanity check...."); database = new SoilModelDatabase(); database.Open(Path.Combine(sosFolder, "SosDatabase.soil")); var profilesCopy = database.ReadSoilProfiles1D(); // change layer names to default foreach (var profile in profilesCopy) { foreach (var layer in profile.Layers) { layer.Name = "Laag 1D"; } } var co = new CompareLogic(); co.Config.ShowBreadcrumb = false; co.Config.CompareStaticFields = false; co.Config.CompareStaticProperties = false; co.Config.AttributesToIgnore = new List { typeof(XmlIgnoreAttribute) }; co.Config.MembersToIgnore = new List { "Id", "Soil", "SetCoordinate", "Color" }; // including the soils here takes a looong time to prove nothing.... var result = co.Compare(profilesCopy, project.SoilProfiles1D); Assert.AreEqual(0, result.Differences.Count, "SOSProfileLayer1D fails comparison: " + result.DifferencesString); Console.WriteLine("OK !"); } [Test] public void CloneTest() { var sosLayer1D = new SosSoilLayer1D() { Name = "TestSos1", TopLevel = 11, MaximumTopLevel = 12, MinimumTopLevel = 13, RemarkTopLevel = "remark testSos1", Soil = new Soil("TestSoil1", Color.Beige) }; var sosLayer1DClone = sosLayer1D.Clone(); var comparer = new CompareLogic(); comparer.Config.ShowBreadcrumb = false; comparer.Config.CompareStaticFields = false; comparer.Config.CompareStaticProperties = false; comparer.Config.AttributesToIgnore = new List { typeof(XmlIgnoreAttribute) }; var compareResult = comparer.Compare(sosLayer1D, sosLayer1DClone); Assert.IsTrue(compareResult.AreEqual, compareResult.DifferencesString); } [Test] public void AssignTest() { var sosLayer1 = new SosSoilLayer1D() { Name = "TestSos1", TopLevel = 11, MaximumTopLevel = 12, MinimumTopLevel = 13, RemarkTopLevel = "remark testSos1" }; var sosLayer2 = new SosSoilLayer1D() { Name = "TestSos2", TopLevel = 21, MaximumTopLevel = 22, MinimumTopLevel = 23, RemarkTopLevel = "remark testSos2" }; sosLayer2.Assign(sosLayer1); Assert.AreEqual("remark testSos1", sosLayer2.RemarkTopLevel); Assert.AreEqual(13, sosLayer2.MinimumTopLevel); Assert.AreEqual(12, sosLayer2.MaximumTopLevel); } } }