using System.Collections.Generic; using System.Linq; using Deltares.Dam.Data; using Deltares.Dam.Data.DataPlugins.Configuration; using Deltares.Dam.Data.Importers; using Deltares.Maps; using GeoAPI.Geometries; using NUnit.Framework; using Rhino.Mocks; using Location = Deltares.Dam.Data.Location; namespace Deltares.Dam.Tests.Importers { [TestFixture] public class LocationShapeFileImporterTest { private ICollection locations; private LocationShapeFileImporter importer; private const string ShapeFileImportFolder = "TestFiles"; private MockRepository mocks; private IFeatureRepository repository; private Feature loc3, loc4; #region Setup/Teardown [TestFixtureSetUp] public void FixtureSetup() { mocks = new MockRepository(); } [SetUp] public void TestSetup() { this.locations = new List() { new Location{ Name= "Location1" }, new Location{ Name= "Location2" }, }; repository = mocks.DynamicMock(); var lm = LocationShapeFileAttributeMap.GetAttributeMapping(LocationShapeFileAttributeMap.LocationAttributeId); var dtm = LocationShapeFileAttributeMap.GetAttributeMapping(LocationShapeFileAttributeMap.DamTypeAttributeId); var drm = LocationShapeFileAttributeMap.GetAttributeMapping(LocationShapeFileAttributeMap.DikeRingAttributeId); loc3 = Feature.Create(new Coordinate(1, 1)); loc3.AddAttribute(lm.Name, "loc3"); loc3.AddAttribute(dtm.Name, "regional"); loc3.AddAttribute(drm.Name, "ring5"); loc4 = Feature.Create(new Coordinate(2, 2)); loc4.AddAttribute(lm.Name, "loc4"); loc4.AddAttribute(dtm.Name, "primary"); loc4.AddAttribute(drm.Name, "ring5"); } [TearDown] public void TestTearDown() { try { mocks.VerifyAll(); } finally { foreach (var location in locations) { location.Dispose(); } } } [TestFixtureTearDown] public void FixtureTearDown() { } #endregion [Test] public void Importer_GivenEmptyLocationList_ShouldNotThrow() { var invalidImporter = new LocationShapeFileImporter(null, null); mocks.ReplayAll(); } [Test] public void Importer_UsingValidCongiguredAttributed_NewLocationsAddedToImportedItems() { const string locAttributeName = "LID"; const string dtAttributeName = "DT"; const string dridAttributeName = "DRID"; var configuredAttributes = new List { new DataAttribute() { AttributeId = LocationShapeFileAttributeMap.LocationAttributeId, AttributeName = locAttributeName }, new DataAttribute() { AttributeId = LocationShapeFileAttributeMap.DikeRingAttributeId, AttributeName = dridAttributeName }, new DataAttribute() { AttributeId = LocationShapeFileAttributeMap.DamTypeAttributeId, AttributeName = dtAttributeName } }; this.importer = new LocationShapeFileImporter(locations, configuredAttributes) { Repository = repository }; var l3 = Feature.Create(new Coordinate(1, 1)); l3.AddAttribute(locAttributeName, "loc3"); l3.AddAttribute(dtAttributeName, "regional"); l3.AddAttribute(dridAttributeName, "ring5"); var l4 = Feature.Create(new Coordinate(2, 2)); l4.AddAttribute(locAttributeName, "loc4"); l4.AddAttribute(dtAttributeName, "primary"); l4.AddAttribute(dridAttributeName, "ring5"); Expect.Call(repository.Features).Return(new[] { l3, l4 }); mocks.ReplayAll(); importer.Import(); Assert.AreEqual(4, this.importer.ImportedItems.Count()); } [Test] public void Import_UsingValidRepository_NewLocationsAddedToImportedItems() { var configuredAttributes = new List { new DataAttribute() { AttributeId = LocationShapeFileAttributeMap.LocationAttributeId }, new DataAttribute() { AttributeId = LocationShapeFileAttributeMap.DikeRingAttributeId }, new DataAttribute() { AttributeId = LocationShapeFileAttributeMap.DamTypeAttributeId } }; // Add existing location to test if that is handled correctly using (var location = new Location() { Name = "loc3", DamType = DamType.Regional, DikeRingId = "ring5", XRd = 111.11, YRd = 222.22 }) { locations = new List() { location }; this.importer = new LocationShapeFileImporter(locations, configuredAttributes) { Repository = repository }; Expect.Call(repository.Features).Return(new[] { loc3, loc4 }); mocks.ReplayAll(); importer.Import(); // ImportedItems should contain the total list of imported locations // In this test a location is already in the locations list having same id as // one feature returned by the feature repository // This will cause that the importer overriders the existing location // because of having the same name (id) // Total should be 2 Assert.AreEqual(2, this.importer.ImportedItems.Count()); var location2 = importer.ImportedItems.Single(l => l.Name == "loc3"); foreach (var importedItem in importer.ImportedItems) { importedItem.Dispose(); } Assert.AreEqual(location2.XRd, loc3.Geometry.Coordinate.X); Assert.AreEqual(location2.YRd, loc3.Geometry.Coordinate.Y); } } } }