using System; using System.Collections.Generic; using System.Linq; using GeoAPI.Geometries; using NetTopologySuite.Geometries; using NUnit.Framework; using Rhino.Mocks; namespace Deltares.Maps.Tests.Services { [TestFixture] public class PolygonAttributeImporterTest { private MockRepository mocks; private IFeatureRepository repository; #region Setup [TestFixtureSetUp] public void FixtureSetup() { mocks = new MockRepository(); repository = mocks.DynamicMock(); } [TestFixtureTearDown] public void FixtureTearDown() { } [SetUp] public void TestSetup() { } [TearDown] public void TestTearDown() { mocks.VerifyAll(); } #endregion class MockTarget { public double X { get; set; } public double Y { get; set; } public double Property1 { get; set; } } [Test] public void Import_TargetPointIsInPolygon_TargetPropertyIsSet() { const string attributeName = "TrafLoad"; const double expected = 20; var target = new MockTarget { X = 0, Y = 0 }; var geometryRepository = new FeatureRepository(); var importer = new PolygonAttributeImporter { XGetter = t => t.X, YGetter = t => t.Y, Targets = new[] { target }, AttributeMappings = new[] { new AttributeMapping { Name = attributeName, Action = (t, value) => t.Property1 = (double)value } }, AttributeRepository = geometryRepository }; var square = new Polygon(new LinearRing(new[] { new Coordinate(-1, 1), new Coordinate(1, 1), new Coordinate(1, -1), new Coordinate(-1, -1), new Coordinate(-1, 1) })); Feature geom = Feature.Create(square); geom.AddAttribute(attributeName, expected); geometryRepository.Add(geom); mocks.ReplayAll(); importer.Import(); Assert.AreEqual(expected, target.Property1); } [Test] public void Import_TargetPointTouchesPolygon_TargetPropertyIsSet() { const string attributeName = "TrafLoad"; const double expected = 20; var target = new MockTarget { X = -1, Y = 1 }; var geometryRepository = new FeatureRepository(); var importer = new PolygonAttributeImporter { XGetter = t => t.X, YGetter = t => t.Y, Targets = new[] { target }, AttributeMappings = new[] { new AttributeMapping { Name = attributeName, Action = (t, value) => t.Property1 = (double)value } }, AttributeRepository = geometryRepository }; var square = new Polygon(new LinearRing(new[] { new Coordinate(-1, 1), new Coordinate(1, 1), new Coordinate(1, -1), new Coordinate(-1, -1), new Coordinate(-1, 1) })); Feature geom = Feature.Create(square); geom.AddAttribute(attributeName, expected); geometryRepository.Add(geom); mocks.ReplayAll(); importer.Import(); Assert.AreEqual(expected, target.Property1); } [Test] public void Import_TargetPointOutsidePolygon_TargetPropertyIsNotSet() { const string attributeName = "TrafLoad"; const double expected = -999; var target = new MockTarget { X = -2, Y = 2, Property1 = expected }; var geometryRepository = new FeatureRepository(); var importer = new PolygonAttributeImporter { XGetter = t => t.X, YGetter = t => t.Y, Targets = new[] { target }, AttributeMappings = new[] { new AttributeMapping { Name = attributeName, Action = (t, value) => t.Property1 = (double)value } }, AttributeRepository = geometryRepository }; var square = new Polygon(new LinearRing(new[] { new Coordinate(-1, 1), new Coordinate(1, 1), new Coordinate(1, -1), new Coordinate(-1, -1), new Coordinate(-1, 1) })); Feature geom = Feature.Create(square); geom.AddAttribute(attributeName, expected); geometryRepository.Add(geom); mocks.ReplayAll(); importer.Import(); Assert.AreEqual(expected, target.Property1); } [Test] [ExpectedException(typeof(InvalidOperationException))] public void Import_RepositoryContainsNoAttributes_Throws() { const string attributeName = "TrafLoad"; var target = new MockTarget { X = 0, Y = 0 }; var importer = new PolygonAttributeImporter { XGetter = t => t.X, YGetter = t => t.Y, Targets = new[] { target }, AttributeMappings = new[] { new AttributeMapping { Name = attributeName, Action = (t, value) => t.Property1 = (double)value } }, AttributeRepository = repository }; Expect.Call(repository.SupportedAttributes).Return(new List()); mocks.ReplayAll(); importer.Import(); } [Test] [ExpectedException(typeof(InvalidOperationException))] public void Import_RepositoryNotSet_Throws() { const string attributeName = "TrafLoad"; var target = new MockTarget { X = 0, Y = 0 }; var importer = new PolygonAttributeImporter { XGetter = t => t.X, YGetter = t => t.Y, Targets = new[] { target }, AttributeMappings = new[] { new AttributeMapping { Name = attributeName, Action = (t, value) => t.Property1 = (double)value } } }; mocks.ReplayAll(); importer.Import(); } [Test] public void Import_AttributeNameMissing_ErrorIsAddedToList() { const string attributeName = "TrafLoad"; var target = new MockTarget { X = 0, Y = 0 }; var importer = new PolygonAttributeImporter { XGetter = t => t.X, YGetter = t => t.Y, Targets = new[] { target }, AttributeMappings = new[] { new AttributeMapping { Name = attributeName, Action = (t, value) => t.Property1 = (double)value } }, AttributeRepository = repository }; var square = new Polygon(new LinearRing(new[] { new Coordinate(-1, 1), new Coordinate(1, 1), new Coordinate(1, -1), new Coordinate(-1, -1), new Coordinate(-1, 1) })); Feature feature = Feature.Create(square); const string testAttribute = "test"; feature.AddAttribute(testAttribute, new object()); //Expect.Call(repository.Features).Return(new[] { feature }); Expect.Call(repository.Query((IGeometry)null)).IgnoreArguments().Return(new[] { feature }); Expect.Call(repository.SupportedAttributes).Return(new[] { testAttribute }); mocks.ReplayAll(); importer.Import(); Assert.AreEqual(1, importer.Errors.OfType().Count()); } } }