using System; using System.Collections.Generic; using System.IO; using System.Linq; using Core.Common.Base; using Core.Common.TestUtils; using NUnit.Framework; using Rhino.Mocks; using Ringtoets.Piping.Data; using Ringtoets.Piping.Plugin.FileImporter; using WtiFormsResources = Ringtoets.Piping.Forms.Properties.Resources; using ApplicationResources = Ringtoets.Piping.Plugin.Properties.Resources; using WtiIOResources = Ringtoets.Piping.IO.Properties.Resources; namespace Ringtoets.Piping.Plugin.Test.FileImporter { [TestFixture] public class PipingSoilProfilesImporterTest { private readonly string testDataPath = TestHelper.GetTestDataPath(TestDataPath.Ringtoets.Piping.IO, "PipingSoilProfilesReader"); private int progress; [SetUp] public void SetUp() { progress = 0; } [Test] public void DefaultConstructor_ExpectedValues() { // Prepare var expectedFileFilter = String.Format("{0} {1} (*.soil)|*.soil", WtiFormsResources.PipingSoilProfilesCollection_DisplayName, ApplicationResources.Soil_file_name); // Call var importer = new PipingSoilProfilesImporter(); // Assert Assert.IsInstanceOf(importer); Assert.AreEqual(WtiFormsResources.PipingSoilProfilesCollection_DisplayName, importer.Name); Assert.AreEqual(ApplicationResources.Wti_application_name, importer.Category); Assert.AreEqual(16, importer.Image.Width); Assert.AreEqual(16, importer.Image.Height); CollectionAssert.AreEqual(new[] { typeof(ICollection) }, importer.SupportedItemTypes); Assert.IsFalse(importer.CanImportOnRootLevel); Assert.AreEqual(expectedFileFilter, importer.FileFilter); Assert.IsNull(importer.TargetDataDirectory); Assert.IsFalse(importer.ShouldCancel); Assert.IsNull(importer.ProgressChanged); Assert.IsFalse(importer.OpenViewAfterImport); } [Test] public void CanImportOn_TargetIsCollectionOfPipingSoilProfile_ReturnTrue() { // Setup var mocks = new MockRepository(); var validTarget = mocks.StrictMock>(); mocks.ReplayAll(); var importer = new PipingSoilProfilesImporter(); // Call var importAllowed = importer.CanImportOn(validTarget); // Assert Assert.IsTrue(importAllowed); mocks.VerifyAll(); // Expect no calls on mocks. } [Test] public void CanImportOn_InvalidTarget_ReturnFalse() { // Setup var mocks = new MockRepository(); var invalidTarget = mocks.StrictMock>(); mocks.ReplayAll(); var importer = new PipingSoilProfilesImporter(); // Call var importAllowed = importer.CanImportOn(invalidTarget); // Assert Assert.IsFalse(importAllowed); mocks.VerifyAll(); // Expect no calls on mocks. } [Test] public void ImportItem_FromNonExistingFile_LogError() { // Setup var file = "nonexisting.soil"; string validFilePath = Path.Combine(testDataPath, file); var mocks = new MockRepository(); var observer = mocks.StrictMock(); mocks.ReplayAll(); var observableList = new ObservableList(); observableList.Attach(observer); var importer = new PipingSoilProfilesImporter { ProgressChanged = IncrementProgress }; // Precondition CollectionAssert.IsEmpty(observableList); Assert.IsTrue(importer.CanImportOn(observableList)); object importedItem = null; // Call Action call = () => importedItem = importer.ImportItem(validFilePath, observableList); // Assert TestHelper.AssertLogMessages(call, messages => { string[] messageArray = messages.ToArray(); var message = string.Format(ApplicationResources.PipingSoilProfilesImporter_Critical_error_reading_File_0_Cause_1_, validFilePath, String.Empty); StringAssert.StartsWith(message, messageArray[0]); }); Assert.AreSame(observableList, importedItem); CollectionAssert.IsEmpty(observableList); Assert.AreEqual(1, progress); mocks.VerifyAll(); // 'observer' should not be notified } [Test] public void ImportItem_FromInvalidFileName_LogError() { // Setup var file = "/"; string validFilePath = Path.Combine(testDataPath, file); var mocks = new MockRepository(); var observer = mocks.StrictMock(); mocks.ReplayAll(); var observableList = new ObservableList(); observableList.Attach(observer); var importer = new PipingSoilProfilesImporter { ProgressChanged = IncrementProgress }; // Precondition CollectionAssert.IsEmpty(observableList); Assert.IsTrue(importer.CanImportOn(observableList)); object importedItem = null; // Call Action call = () => importedItem = importer.ImportItem(validFilePath, observableList); // Assert TestHelper.AssertLogMessages(call, messages => { string[] messageArray = messages.ToArray(); var message = string.Format(ApplicationResources.PipingSoilProfilesImporter_Critical_error_reading_File_0_Cause_1_, validFilePath, String.Empty); StringAssert.StartsWith(message, messageArray[0]); }); Assert.AreSame(observableList, importedItem); CollectionAssert.IsEmpty(observableList); Assert.AreEqual(1, progress); mocks.VerifyAll(); // 'observer' should not be notified } [Test] public void ImportItem_ImportingToValidTargetWithValidFile_ImportSoilProfilesToCollection() { // Setup string validFilePath = Path.Combine(testDataPath, "complete.soil"); var piping = new PipingFailureMechanism(); var importer = new PipingSoilProfilesImporter { ProgressChanged = IncrementProgress }; var importTarget = piping.SoilProfiles; // Precondition Assert.IsTrue(importer.CanImportOn(importTarget)); // Call var importedItem = importer.ImportItem(validFilePath, importTarget); // Assert Assert.AreSame(importTarget, importedItem); Assert.AreEqual(28, progress); } [Test] public void ImportItem_ImportingToValidTargetWithValidFileWhileShouldCancelTrue_CancelImportAndLog() { // Setup string validFilePath = Path.Combine(testDataPath, "complete.soil"); var mocks = new MockRepository(); var observer = mocks.StrictMock(); mocks.ReplayAll(); var observableList = new ObservableList(); observableList.Attach(observer); var importer = new PipingSoilProfilesImporter { ProgressChanged = IncrementProgress }; // Precondition CollectionAssert.IsEmpty(observableList); Assert.IsTrue(importer.CanImportOn(observableList)); Assert.IsTrue(File.Exists(validFilePath)); importer.ShouldCancel = true; object importedItem = null; // Call Action call = () => importedItem = importer.ImportItem(validFilePath, observableList); // Assert TestHelper.AssertLogMessageIsGenerated(call, ApplicationResources.PipingSoilProfilesImporter_ImportItem_Import_cancelled, 1); Assert.AreSame(observableList, importedItem); CollectionAssert.IsEmpty(observableList); Assert.AreEqual(1, progress); mocks.VerifyAll(); // 'observer' should not be notified } [Test] public void ImportItem_ImportingToValidTargetWithEmptyFile_AbortImportAndLog() { // Setup string corruptPath = Path.Combine(testDataPath, "empty.soil"); var mocks = new MockRepository(); var observer = mocks.StrictMock(); mocks.ReplayAll(); var importer = new PipingSoilProfilesImporter { ProgressChanged = IncrementProgress }; var observableSoilProfileList = new ObservableList(); observableSoilProfileList.Attach(observer); object importedItem = null; // Call Action call = () => importedItem = importer.ImportItem(corruptPath, observableSoilProfileList); // Assert var internalErrorMessage = string.Format(WtiIOResources.Error_SoilProfile_read_from_database, Path.GetFileName(corruptPath)); var expectedLogMessage = string.Format(ApplicationResources.PipingSoilProfilesImporter_Critical_error_reading_File_0_Cause_1_, corruptPath, internalErrorMessage); TestHelper.AssertLogMessageIsGenerated(call, expectedLogMessage, 1); Assert.AreSame(observableSoilProfileList, importedItem); CollectionAssert.IsEmpty(observableSoilProfileList, "No items should be added to collection when import is aborted."); Assert.AreEqual(1, progress); mocks.VerifyAll(); // Expect no calls on 'observer' } [Test] public void ImportItem_ImportingToValidTargetWithProfileContainingInvalidAtX_SkipImportAndLog() { // Setup string corruptPath = Path.Combine(testDataPath, "invalidAtX2dProperty.soil"); var mocks = new MockRepository(); var observer = mocks.StrictMock(); observer.Expect(o => o.UpdateObserver()); mocks.ReplayAll(); var importer = new PipingSoilProfilesImporter { ProgressChanged = IncrementProgress }; var observableSoilProfileList = new ObservableList(); observableSoilProfileList.Attach(observer); object importedItem = null; // Call Action call = () => importedItem = importer.ImportItem(corruptPath, observableSoilProfileList); // Assert var internalErrorMessage = string.Format(WtiIOResources.PipingSoilProfileReader_Profile_0_has_invalid_value_on_column_1_, "Profile","IntersectionX"); var expectedLogMessage = string.Format(ApplicationResources.PipingSoilProfilesImporter_ReadSoilProfiles_File_0_Message_1_, corruptPath, internalErrorMessage); TestHelper.AssertLogMessageIsGenerated(call, expectedLogMessage, 1); Assert.AreSame(observableSoilProfileList, importedItem); Assert.AreEqual(1, observableSoilProfileList.Count); Assert.AreEqual(4, progress); mocks.VerifyAll(); } [Test] public void ImportItem_ImportingToValidTargetWithProfileContainingInvalidParameterValue_ZeroForValue() { // Setup string corruptPath = Path.Combine(testDataPath, "incorrectValue2dProperty.soil"); var mocks = new MockRepository(); var observer = mocks.StrictMock(); observer.Expect(o => o.UpdateObserver()); mocks.ReplayAll(); var importer = new PipingSoilProfilesImporter { ProgressChanged = IncrementProgress }; var observableSoilProfileList = new ObservableList(); observableSoilProfileList.Attach(observer); // Call var importedItem = importer.ImportItem(corruptPath, observableSoilProfileList); Assert.AreSame(observableSoilProfileList, importedItem); Assert.AreEqual(0.0, observableSoilProfileList[0].Layers.ElementAt(0).DryUnitWeight); Assert.AreEqual(0.0, observableSoilProfileList[1].Layers.ElementAt(1).DryUnitWeight); Assert.AreEqual(2, observableSoilProfileList.Count); Assert.AreEqual(4, progress); mocks.VerifyAll(); } private void IncrementProgress(string a, int b, int c) { progress++; } } }