Index: Ringtoets/Common/test/Ringtoets.Common.IO.Test/Configurations/Export/CalculationConfigurationWriterTest.cs =================================================================== diff -u -rb3b6c13cf736c134476b3db34281332d01ca86b1 -re507c88f2863ff7bc93505caf71c5f8025fb48c0 --- Ringtoets/Common/test/Ringtoets.Common.IO.Test/Configurations/Export/CalculationConfigurationWriterTest.cs (.../CalculationConfigurationWriterTest.cs) (revision b3b6c13cf736c134476b3db34281332d01ca86b1) +++ Ringtoets/Common/test/Ringtoets.Common.IO.Test/Configurations/Export/CalculationConfigurationWriterTest.cs (.../CalculationConfigurationWriterTest.cs) (revision e507c88f2863ff7bc93505caf71c5f8025fb48c0) @@ -22,17 +22,15 @@ using System; using System.Collections.Generic; using System.IO; +using System.Linq; using System.Xml; -using Core.Common.Base.Data; -using Core.Common.IO.Exceptions; using Core.Common.TestUtil; using NUnit.Framework; using Rhino.Mocks; using Ringtoets.Common.Data.Calculation; -using Ringtoets.Common.Data.DikeProfiles; -using Ringtoets.Common.Data.Probabilistics; -using Ringtoets.Common.Data.TestUtil; +using Ringtoets.Common.IO.Configurations; using Ringtoets.Common.IO.Configurations.Export; +using Ringtoets.Common.IO.Configurations.Helpers; using Ringtoets.Common.IO.TestUtil; namespace Ringtoets.Common.IO.Test.Configurations.Export @@ -41,28 +39,30 @@ public class CalculationConfigurationWriterTest : CustomCalculationConfigurationWriterDesignGuidelinesTestFixture< TestCalculationConfigurationWriter, - TestCalculation> + TestConfigurationItem> { [Test] - public void WriteDistribution_WithoutDistributions_ArgumentNullException() + [TestCaseSource(nameof(GetCalculationConfigurations))] + public void Write_DifferentCalculationAndCalculationGroupConfigurations_ValidFile(IEnumerable configuration, string expectedFileContentsFileName) { // Setup - string filePath = TestHelper.GetScratchPadPath(nameof(WriteDistribution_WithoutDistributions_ArgumentNullException)); + string filePath = TestHelper.GetScratchPadPath("test.xml"); + string expectedXmlFilePath = TestHelper.GetTestDataPath( + TestDataPath.Ringtoets.Common.IO, + Path.Combine(nameof(CalculationConfigurationWriter), expectedFileContentsFileName)); try { - using (XmlWriter xmlWriter = CreateXmlWriter(filePath)) - { - var writer = new TestCalculationConfigurationWriter(); + // Call + new TestCalculationConfigurationWriter(filePath).Write(configuration); - // Call - Assert.Throws(() => writer.PublicWriteDistributions(null, xmlWriter)); - } - // Assert + Assert.IsTrue(File.Exists(filePath)); + string actualXml = File.ReadAllText(filePath); + string expectedXml = File.ReadAllText(expectedXmlFilePath); - Assert.IsEmpty(actualXml); + Assert.AreEqual(expectedXml, actualXml); } finally { @@ -71,340 +71,365 @@ } [Test] - public void WriteDistribution_EmptyDistributions_NothingWrittenToFile() + public void WriteDistributionWhenAvailable_MeanStandardDeviationStochastConfigurationWriterNull_ThrowsArgumentNullException() { + // Call + TestDelegate test = () => ExposedCalculationConfigurationWriter.PublicWriteDistributionWhenAvailable( + null, + "some name", + null); + + // Assert + var exception = Assert.Throws(test); + Assert.AreEqual("writer", exception.ParamName); + } + + [Test] + public void WriteDistributionWhenAvailable_MeanStandardDeviationStochastConfigurationDistributionNameNull_ThrowsArgumentNullException() + { // Setup - string filePath = TestHelper.GetScratchPadPath(nameof(WriteDistribution_EmptyDistributions_NothingWrittenToFile)); + var mocks = new MockRepository(); + var xmlWriter = mocks.StrictMock(); + mocks.ReplayAll(); - try - { - using (XmlWriter xmlWriter = CreateXmlWriter(filePath)) - { - var writer = new TestCalculationConfigurationWriter(); + // Call + TestDelegate test = () => ExposedCalculationConfigurationWriter.PublicWriteDistributionWhenAvailable( + xmlWriter, + null, + null); - // Call - writer.PublicWriteDistributions(new Dictionary(), xmlWriter); - } + // Assert + var exception = Assert.Throws(test); + Assert.AreEqual("distributionName", exception.ParamName); + } - // Assert - string actualXml = File.ReadAllText(filePath); + [Test] + public void WriteDistributionWhenAvailable_MeanStandardDeviationStochastConfigurationNull_WriterNotCalled() + { + // Setup + var mocks = new MockRepository(); + var xmlWriter = mocks.StrictMock(); + mocks.ReplayAll(); - Assert.IsEmpty(actualXml); - } - finally - { - File.Delete(filePath); - } + // Call + ExposedCalculationConfigurationWriter.PublicWriteDistributionWhenAvailable( + xmlWriter, + "some name", + null); + + // Assert + mocks.VerifyAll(); } [Test] - public void WriteDistribution_WithDistributions_WritesEachDistributionAsElement() + public void WriteDistributionWhenAvailable_MeanStandardDeviationStochastConfigurationSet_WriterCalledWithExpectedParameters() { // Setup - string filePath = TestHelper.GetScratchPadPath(nameof(WriteDistribution_WithDistributions_WritesEachDistributionAsElement)); - string expectedXmlFilePath = TestHelper.GetTestDataPath( - TestDataPath.Ringtoets.Common.IO, - Path.Combine(nameof(CalculationConfigurationWriter), "distributions.xml")); + const string name = "some name"; + var configuration = new StochastConfiguration(); - var distributions = new Dictionary - { - { - "normal", new NormalDistribution - { - Mean = (RoundedDouble) 0.2, - StandardDeviation = (RoundedDouble) 0.1 - } - }, - { - "lognormal", new LogNormalDistribution - { - Mean = (RoundedDouble) 0.4, - StandardDeviation = (RoundedDouble) 0.3 - } - } - }; + var mocks = new MockRepository(); + var xmlWriter = mocks.StrictMock(); + xmlWriter.Expect(w => w.WriteDistribution(name, configuration)); + mocks.ReplayAll(); - try - { - using (XmlWriter xmlWriter = CreateXmlWriter(filePath)) - { - var writer = new TestCalculationConfigurationWriter(); + // Call + ExposedCalculationConfigurationWriter.PublicWriteDistributionWhenAvailable( + xmlWriter, + name, + configuration); - // Call - writer.PublicWriteDistributions(distributions, xmlWriter); - } + // Assert + mocks.VerifyAll(); + } - // Assert - string actualXml = File.ReadAllText(filePath); - string expectedXml = File.ReadAllText(expectedXmlFilePath); + [Test] + public void WriteDistributionWhenAvailable_StochastConfigurationWriterNull_ThrowsArgumentNullException() + { + // Call + TestDelegate test = () => ExposedCalculationConfigurationWriter.PublicWriteDistributionWhenAvailable( + null, + "some name", + null); - Assert.AreEqual(expectedXml, actualXml); - } - finally - { - File.Delete(filePath); - } + // Assert + var exception = Assert.Throws(test); + Assert.AreEqual("writer", exception.ParamName); } [Test] - public void WriteVariationCoefficientDistribution_WithoutVariationCoefficientDistributions_ArgumentNullException() + public void WriteDistributionWhenAvailable_StochastConfigurationDistributionNameNull_ThrowsArgumentNullException() { // Setup - string filePath = TestHelper.GetScratchPadPath( - nameof(WriteVariationCoefficientDistribution_WithoutVariationCoefficientDistributions_ArgumentNullException)); + var mocks = new MockRepository(); + var xmlWriter = mocks.StrictMock(); + mocks.ReplayAll(); - try - { - using (XmlWriter xmlWriter = CreateXmlWriter(filePath)) - { - var writer = new TestCalculationConfigurationWriter(); + // Call + TestDelegate test = () => ExposedCalculationConfigurationWriter.PublicWriteDistributionWhenAvailable( + xmlWriter, + null, + null); - // Call - Assert.Throws(() => writer.PublicWriteDistributions(null, xmlWriter)); - } + // Assert + var exception = Assert.Throws(test); + Assert.AreEqual("distributionName", exception.ParamName); + } - // Assert - string actualXml = File.ReadAllText(filePath); + [Test] + public void WriteDistributionWhenAvailable_StochastConfigurationNull_WriterNotCalled() + { + // Setup + var mocks = new MockRepository(); + var xmlWriter = mocks.StrictMock(); + mocks.ReplayAll(); - Assert.IsEmpty(actualXml); - } - finally - { - File.Delete(filePath); - } + // Call + ExposedCalculationConfigurationWriter.PublicWriteDistributionWhenAvailable( + xmlWriter, + "some name", + null); + + // Assert + mocks.VerifyAll(); } [Test] - public void WriteVariationCoefficientDistribution_EmptyVariationCoefficientDistributions_NothingWrittenToFile() + public void WriteDistributionWhenAvailable_StochastConfigurationSet_WriterCalledWithExpectedParameters() { // Setup - string filePath = TestHelper.GetScratchPadPath( - nameof(WriteVariationCoefficientDistribution_EmptyVariationCoefficientDistributions_NothingWrittenToFile)); + const string name = "some name"; + var configuration = new StochastConfiguration(); - try - { - using (XmlWriter xmlWriter = CreateXmlWriter(filePath)) - { - var writer = new TestCalculationConfigurationWriter(); + var mocks = new MockRepository(); + var xmlWriter = mocks.StrictMock(); + xmlWriter.Expect(w => w.WriteDistribution(name, configuration)); + mocks.ReplayAll(); - // Call - writer.PublicWriteDistributions(new Dictionary(), xmlWriter); - } + // Call + ExposedCalculationConfigurationWriter.PublicWriteDistributionWhenAvailable( + xmlWriter, + name, + configuration); - // Assert - string actualXml = File.ReadAllText(filePath); + // Assert + mocks.VerifyAll(); + } - Assert.IsEmpty(actualXml); - } - finally - { - File.Delete(filePath); - } + [Test] + public void WriteElementWhenContentAvailable_StringWriterNull_ThrowsArgumentNullException() + { + // Call + TestDelegate test = () => ExposedCalculationConfigurationWriter.PublicWriteElementWhenContentAvailable( + null, + "some name", + (string) null); + + // Assert + var exception = Assert.Throws(test); + Assert.AreEqual("writer", exception.ParamName); } [Test] - public void WriteVariationCoefficientDistribution_WithVariationCoefficientDistributions_WritesEachDistributionAsElement() + public void WriteElementWhenContentAvailable_StringElementNameNull_ThrowsArgumentNullException() { // Setup - string filePath = TestHelper.GetScratchPadPath( - nameof(WriteVariationCoefficientDistribution_WithVariationCoefficientDistributions_WritesEachDistributionAsElement)); - string expectedXmlFilePath = TestHelper.GetTestDataPath( - TestDataPath.Ringtoets.Common.IO, - Path.Combine(nameof(CalculationConfigurationWriter), "variationCoefficientDistributions.xml")); + var mocks = new MockRepository(); + var xmlWriter = mocks.StrictMock(); + mocks.ReplayAll(); - var distributions = new Dictionary - { - { - "normal", new VariationCoefficientNormalDistribution - { - Mean = (RoundedDouble) 0.2, - CoefficientOfVariation = (RoundedDouble) 0.1 - } - }, - { - "lognormal", new VariationCoefficientLogNormalDistribution - { - Mean = (RoundedDouble) 0.4, - CoefficientOfVariation = (RoundedDouble) 0.3 - } - } - }; + // Call + TestDelegate test = () => ExposedCalculationConfigurationWriter.PublicWriteElementWhenContentAvailable( + xmlWriter, + null, + (string) null); - try - { - using (XmlWriter xmlWriter = CreateXmlWriter(filePath)) - { - var writer = new TestCalculationConfigurationWriter(); + // Assert + var exception = Assert.Throws(test); + Assert.AreEqual("elementName", exception.ParamName); + } - // Call - writer.PublicWriteVariationCoefficientDistributions(distributions, xmlWriter); - } + [Test] + public void WriteElementWhenContentAvailable_StringNull_WriterNotCalled() + { + // Setup + var mocks = new MockRepository(); + var xmlWriter = mocks.StrictMock(); + mocks.ReplayAll(); - // Assert - string actualXml = File.ReadAllText(filePath); - string expectedXml = File.ReadAllText(expectedXmlFilePath); + // Call + ExposedCalculationConfigurationWriter.PublicWriteElementWhenContentAvailable( + xmlWriter, + "some name", + (string) null); - Assert.AreEqual(expectedXml, actualXml); - } - finally - { - File.Delete(filePath); - } + // Assert + mocks.VerifyAll(); } [Test] - public void WriteBreakWaterProperties_BreakWaterNull_NothingWrittenToFile() + public void WriteElementWhenContentAvailable_StringSet_WriterCalledWithExpectedParameters() { // Setup - string filePath = TestHelper.GetScratchPadPath( - $"{nameof(WriteBreakWaterProperties_WithBreakWater_WritesPropertiesToFile)}.xml"); + const string name = "some name"; + const string value = "some value"; - try - { - using (XmlWriter xmlWriter = CreateXmlWriter(filePath)) - { - var writer = new TestCalculationConfigurationWriter(); + var mocks = new MockRepository(); + var xmlWriter = mocks.StrictMock(); + xmlWriter.Expect(w => w.WriteElementString(name, value)); + mocks.ReplayAll(); - // Call - writer.PublicWriteBreakWaterProperties(null, xmlWriter); - } + // Call + ExposedCalculationConfigurationWriter.PublicWriteElementWhenContentAvailable( + xmlWriter, + name, + value); - // Assert - string actualXml = File.ReadAllText(filePath); + // Assert + mocks.VerifyAll(); + } - Assert.IsEmpty(actualXml); - } - finally - { - File.Delete(filePath); - } + [Test] + public void WriteElementWhenContentAvailable_DoubleWriterNull_ThrowsArgumentNullException() + { + // Call + TestDelegate test = () => ExposedCalculationConfigurationWriter.PublicWriteElementWhenContentAvailable( + null, + "some name", + (double?) null); + + // Assert + var exception = Assert.Throws(test); + Assert.AreEqual("writer", exception.ParamName); } [Test] - [TestCase(BreakWaterType.Wall, 26.3, "breakWaterWall.xml")] - [TestCase(BreakWaterType.Caisson, 1.5, "breakWaterCaisson.xml")] - [TestCase(BreakWaterType.Dam, -55.1, "breakWaterDam.xml")] - public void WriteBreakWaterProperties_WithBreakWater_WritesPropertiesToFile( - BreakWaterType type, - double height, - string expectedContentFilePath) + public void WriteElementWhenContentAvailable_DoubleElementNameNull_ThrowsArgumentNullException() { // Setup - string filePath = TestHelper.GetScratchPadPath( - $"{nameof(WriteBreakWaterProperties_WithBreakWater_WritesPropertiesToFile)} {type}"); + var mocks = new MockRepository(); + var xmlWriter = mocks.StrictMock(); + mocks.ReplayAll(); - string expectedXmlFilePath = TestHelper.GetTestDataPath( - TestDataPath.Ringtoets.Common.IO, - Path.Combine(nameof(CalculationConfigurationWriter), expectedContentFilePath)); + // Call + TestDelegate test = () => ExposedCalculationConfigurationWriter.PublicWriteElementWhenContentAvailable( + xmlWriter, + null, + (double?) null); - var breakWater = new BreakWater(type, (RoundedDouble) height); + // Assert + var exception = Assert.Throws(test); + Assert.AreEqual("elementName", exception.ParamName); + } - try - { - using (XmlWriter xmlWriter = CreateXmlWriter(filePath)) - { - var writer = new TestCalculationConfigurationWriter(); + [Test] + public void WriteElementWhenContentAvailable_DoubleNull_WriterNotCalled() + { + // Setup + var mocks = new MockRepository(); + var xmlWriter = mocks.StrictMock(); + mocks.ReplayAll(); - // Call - writer.PublicWriteBreakWaterProperties(breakWater, xmlWriter); - } + // Call + ExposedCalculationConfigurationWriter.PublicWriteElementWhenContentAvailable( + xmlWriter, + "some name", + (double?) null); - // Assert - string actualXml = File.ReadAllText(filePath); - string expectedXml = File.ReadAllText(expectedXmlFilePath); - - Assert.AreEqual(expectedXml, actualXml); - } - finally - { - File.Delete(filePath); - } + // Assert + mocks.VerifyAll(); } [Test] - public void Write_CalculationOfTypeOtherThanGiven_ThrowsCriticalFileWriteExceptionWithInnerArgumentException() + public void WriteElementWhenContentAvailable_DoubleSet_WriterCalledWithExpectedParameters() { // Setup + const string name = "some name"; + const double value = 3.2; + var mocks = new MockRepository(); - var calculation = mocks.Stub(); + var xmlWriter = mocks.StrictMock(); + xmlWriter.Expect(w => w.WriteElementString(name, XmlConvert.ToString(value))); mocks.ReplayAll(); - string filePath = TestHelper.GetScratchPadPath("test.xml"); + // Call + ExposedCalculationConfigurationWriter.PublicWriteElementWhenContentAvailable( + xmlWriter, + name, + value); - try - { - // Call - TestDelegate test = () => new TestCalculationConfigurationWriter().Write(new[] - { - calculation - }, filePath); - - // Assert - var exception = Assert.Throws(test); - Exception innerException = exception.InnerException; - Assert.IsNotNull(innerException); - Assert.AreEqual($"Cannot write calculation of type '{calculation.GetType()}' using this writer.", innerException.Message); - } - finally - { - File.Delete(filePath); - } + // Assert mocks.VerifyAll(); } [Test] - [TestCaseSource(nameof(GetCalculationConfigurations))] - public void Write_DifferentCalculationAndCalculationGroupConfigurations_ValidFile(IEnumerable configuration, string expectedFileContentsFileName) + public void WriteWaveReductionWhenAvailable_WriterNull_ThrowsArgumentNullException() { - // Setup - string filePath = TestHelper.GetScratchPadPath("test.xml"); - string expectedXmlFilePath = TestHelper.GetTestDataPath( - TestDataPath.Ringtoets.Common.IO, - Path.Combine(nameof(CalculationConfigurationWriter), expectedFileContentsFileName)); + // Call + TestDelegate test = () => ExposedCalculationConfigurationWriter.PublicWriteWaveReductionWhenAvailable( + null, + null); - try - { - // Call - new TestCalculationConfigurationWriter().Write(configuration, filePath); + // Assert + var exception = Assert.Throws(test); + Assert.AreEqual("writer", exception.ParamName); + } - // Assert - Assert.IsTrue(File.Exists(filePath)); + [Test] + public void WriteWaveReductionWhenAvailable_WaveReductionConfigurationNull_WriterNotCalled() + { + // Setup + var mocks = new MockRepository(); + var xmlWriter = mocks.StrictMock(); + mocks.ReplayAll(); - string actualXml = File.ReadAllText(filePath); - string expectedXml = File.ReadAllText(expectedXmlFilePath); + // Call + ExposedCalculationConfigurationWriter.PublicWriteWaveReductionWhenAvailable( + xmlWriter, + null); - Assert.AreEqual(expectedXml, actualXml); - } - finally - { - File.Delete(filePath); - } + // Assert + mocks.VerifyAll(); } - private static XmlWriter CreateXmlWriter(string filePath) + [Test] + public void WriteWaveReductionWhenAvailable_WaveReductionConfigurationSet_WriterCalledWithExpectedParameters() { - return XmlWriter.Create(filePath, new XmlWriterSettings - { - Indent = true, - ConformanceLevel = ConformanceLevel.Fragment - }); + // Setup + var configuration = new WaveReductionConfiguration(); + + var mocks = new MockRepository(); + var xmlWriter = mocks.StrictMock(); + xmlWriter.Expect(w => w.WriteWaveReduction(configuration)); + mocks.ReplayAll(); + + // Call + ExposedCalculationConfigurationWriter.PublicWriteWaveReductionWhenAvailable( + xmlWriter, + configuration); + + // Assert + mocks.VerifyAll(); } private static IEnumerable GetCalculationConfigurations() { - var calculation1 = new TestCalculation("calculation1"); - var calculation2 = new TestCalculation("calculation2"); - - var calculationGroup1 = new CalculationGroup("group1", false); - var calculationGroup2 = new CalculationGroup("group2", false) + var calculation1 = new TestConfigurationItem { - Children = - { - calculation2, - calculationGroup1 - } + Name = "calculation1" }; + var calculation2 = new TestConfigurationItem + { + Name = "calculation2" + }; + var calculationGroup1 = new CalculationGroupConfiguration("group1", Enumerable.Empty()); + var calculationGroup2 = new CalculationGroupConfiguration("group2", new IConfigurationItem[] + { + calculation2, + calculationGroup1 + }); + yield return new TestCaseData( new[] { @@ -420,48 +445,56 @@ "singleCalculation.xml") .SetName("Single calculation"); yield return new TestCaseData( - new ICalculationBase[] + new IConfigurationItem[] { calculationGroup1, calculation1 }, "calculationGroupAndCalculation.xml") .SetName("Calculation group and calculation"); yield return new TestCaseData( - new ICalculationBase[] + new IConfigurationItem[] { calculation1, calculationGroup2 }, "calculationAndGroupWithNesting.xml") .SetName("Calculation and group with nesting"); } - } - public class TestCalculationConfigurationWriter : CalculationConfigurationWriter - { - public const string CalculationElementTag = "calculation"; - - public void PublicWriteDistributions(IDictionary distributions, XmlWriter writer) + protected override TestCalculationConfigurationWriter CreateWriterInstance(string filePath) { - WriteDistributions(distributions, writer); + return new TestCalculationConfigurationWriter(filePath); } - public void PublicWriteVariationCoefficientDistributions( - IDictionary distributions, - XmlWriter writer) + private class ExposedCalculationConfigurationWriter : CalculationConfigurationWriter { - WriteVariationCoefficientDistributions(distributions, writer); - } + public ExposedCalculationConfigurationWriter(string filePath) : base(filePath) {} - public void PublicWriteBreakWaterProperties(BreakWater breakWater, XmlWriter writer) - { - WriteBreakWaterProperties(breakWater, writer); - } + public static void PublicWriteDistributionWhenAvailable(XmlWriter writer, string distributionName, StochastConfiguration configuration) + { + WriteDistributionWhenAvailable(writer, distributionName, configuration); + } - protected override void WriteCalculation(TestCalculation calculation, XmlWriter writer) - { - writer.WriteElementString(CalculationElementTag, calculation.Name); + public static void PublicWriteElementWhenContentAvailable(XmlWriter writer, string elementName, string elementContent) + { + WriteElementWhenContentAvailable(writer, elementName, elementContent); + } + + public static void PublicWriteElementWhenContentAvailable(XmlWriter writer, string elementName, double? elementContent) + { + WriteElementWhenContentAvailable(writer, elementName, elementContent); + } + + public static void PublicWriteWaveReductionWhenAvailable(XmlWriter writer, WaveReductionConfiguration configuration) + { + WriteWaveReductionWhenAvailable(writer, configuration); + } + + protected override void WriteCalculation(TestConfigurationItem calculation, XmlWriter writer) + { + throw new NotImplementedException(); + } } } } \ No newline at end of file