using System; using System.Collections.Generic; using GeoAPI.Geometries; using NetTopologySuite.Geometries; using NUnit.Framework; using Rhino.Mocks; namespace Deltares.Maps.Tests.Domain { [TestFixture] public class CoverageFunctionTest { private MockRepository mocks; private IFeatureRepository repository; #region Setup [TestFixtureSetUp] public void FixtureSetup() { mocks = new MockRepository(); } [TestFixtureTearDown] public void FixtureTearDown() { } [SetUp] public void TestSetup() { repository = mocks.DynamicMock(); } [TearDown] public void TestTearDown() { mocks.VerifyAll(); } #endregion [Test] [ExpectedException(typeof(ArgumentException))] public void GetCoverageFunction_NoAttributesInList_Throws() { mocks.ReplayAll(); CoverageFunction.GetCoverageFunc(repository, new List()); } [Test] [ExpectedException(typeof(ArgumentException))] public void GetCoverageFunction_AttributeListContainsInvalidString_Throws() { mocks.ReplayAll(); CoverageFunction.GetCoverageFunc(repository, new List { "" }); } [Test] [ExpectedException(typeof(ArgumentNullException))] public void GetCoverageFunction_SupportedAttributeListIsNull_Throws() { mocks.ReplayAll(); repository.GetCoverageFunc("SomeAttribute", null); } [Test] [ExpectedException(typeof(ArgumentNullException))] public void GetCoverageFunction_RepositoryIsNull_Throws() { mocks.ReplayAll(); CoverageFunction.GetCoverageFunc(null, new[] { "SomeAttribute" }); } [Test] [ExpectedException(typeof(ArgumentNullException))] public void UsingCoverageFunction_AttributeNameIsNullOrEmpty_Throws() { mocks.ReplayAll(); Func func = CoverageFunction.GetCoverageFunc(repository, new[] { "SomeAttribute" }); func("", 0, 0); } [Test] public void CoverageFunction_AttributeNamesAndDifferentCase_ReturnsExpectedValue() { var lineString = new LineString(new[] { new Coordinate(1, 1), new Coordinate(1, 2) }); var rep = new FeatureRepository(); Feature geom = Feature.Create(lineString); rep.Add(geom); //Expect.Call(repository.Features).Return(new[] { geom }); const string attribute = "SomeAttribute"; const string expected = "test"; geom.AddAttribute(attribute, expected); //Expect.Call(repository.SupportedAttributes).Return(new[] { attribute }); Func func = rep.GetCoverageFunc(); var actual = (string)func(attribute.ToUpper(), 1, 1); Assert.IsNotNull(actual); Assert.AreEqual(expected, actual); actual = (string)func(attribute.ToLower(), 1, 1); Assert.IsNotNull(actual); Assert.AreEqual(expected, actual); mocks.ReplayAll(); } [Test] [ExpectedException(typeof(AttributeMissingException))] public void UsingCoverageFunction_AttributeNamenotContainedInAttributeList_Throws() { mocks.ReplayAll(); Func func = CoverageFunction.GetCoverageFunc(repository, new[] { "SomeAttribute" }); func("AnotherAttribute", 0, 0); } [Test] [ExpectedException(typeof(FeatureCoverageException), ExpectedMessage = "Multiple geometries found covering point (0 0)")] public void UsingCoverageFunction_MoreThenOneGeometyFoundAtGivenLocation_Throws() { 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); //Expect.Call(repository.Features).Return(new[] {geom, geom}); Expect.Call(repository.Query((IGeometry)null)).IgnoreArguments().Return(new[] { geom, geom }); const string attribute = "SomeAttribute"; Expect.Call(repository.SupportedAttributes).Return(new[] { attribute.ToUpper() }); mocks.ReplayAll(); Func func = repository.GetCoverageFunc(); func(attribute, 0, 0); } [Test] [ExpectedException(typeof(FeatureCoverageException), ExpectedMessage = "No geometries found covering point (10 10)")] public void UsingCoverageFunction_NoGeometyFoundAtGivenLocation_Throws() { 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) })); const string attribute = "SomeAttribute"; Expect.Call(repository.SupportedAttributes).Return(new[] { attribute.ToUpper() }); Feature geom = Feature.Create(square); //Expect.Call(repository.Features).Return(new[] { geom }); Expect.Call(repository.Query((IGeometry)null)).IgnoreArguments().Return(new[] { geom }); mocks.ReplayAll(); Func func = repository.GetCoverageFunc(); func("SomeAttribute", 10, 10); } [Test] public void UsingCoverageFunction_GeometyFoundAtGivenLocation_ReturnsAttributeValueAsDouble() { 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); //Expect.Call(repository.Features).Return(new[] { geom }); Expect.Call(repository.Query((IGeometry)null)).IgnoreArguments().Return(new[] { geom }); const string attribute = "SomeAttribute"; const double expected = 20; geom.AddAttribute(attribute, expected); Expect.Call(repository.SupportedAttributes).Return(new[] { attribute.ToUpper() }); mocks.ReplayAll(); Func func = repository.GetCoverageFunc(); var actual = (double)func(attribute, 0, 0); Assert.IsNotNull(actual); Assert.AreEqual(expected, actual, 0.0001); } [Test] public void UsingCoverageFunction_GeometyFoundAtGivenLocation_ReturnsAttributeValueAsStringType() { 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); //Expect.Call(repository.Features).Return(new[] { geom }); Expect.Call(repository.Query((IGeometry)null)).IgnoreArguments().Return(new[] { geom }); const string attribute = "SomeAttribute"; const string expected = "test"; geom.AddAttribute(attribute, expected); Expect.Call(repository.SupportedAttributes).Return(new[] { attribute }); mocks.ReplayAll(); Func func = repository.GetCoverageFunc(); var actual = (string)func(attribute, 0, 0); Assert.IsNotNull(actual); Assert.AreEqual(expected, actual); } [Test] public void UsingCoverageFunctionForGivenLocation_RepositoryContainsLinestringGeometryType_ReturnsValue() { var lineString = new LineString(new[] { new Coordinate(1, 1), new Coordinate(1, 2) }); Feature geom = Feature.Create(lineString); //Expect.Call(repository.Features).Return(new[] { geom }); Expect.Call(repository.Query((IGeometry)null)).IgnoreArguments().Return(new[] { geom }); const string attribute = "SomeAttribute"; const double expected = 5; geom.AddAttribute(attribute, expected); Expect.Call(repository.SupportedAttributes).Return(new[] { attribute }); mocks.ReplayAll(); Func func = repository.GetCoverageFunc(); var actual = (double)func(attribute, 1, 1); Assert.IsNotNull(actual); Assert.AreEqual(expected, actual); actual = (double)func(attribute, 1, 2); Assert.IsNotNull(actual); Assert.AreEqual(expected, actual); actual = (double)func(attribute, 1, 1.5); Assert.IsNotNull(actual); Assert.AreEqual(expected, actual); } [Test] public void UsingCoverageFunctionCreatedByGeometryRepository_GeometyFoundAtGivenLocation_ReturnsAttributeValue() { const string attribute = "SomeAttribute"; const double expected = 20; 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(attribute, expected); //Expect.Call(repository.Features).Return(new[] { geom }); Expect.Call(repository.Query((IGeometry)null)).IgnoreArguments().Return(new[] { geom }); Expect.Call(repository.SupportedAttributes).Return(new[] { attribute.ToUpper() }); mocks.ReplayAll(); Func func = repository.GetCoverageFunc(attribute); var actual = (double)func(0, 0); Assert.IsNotNull(actual); Assert.AreEqual(expected, actual, 0.0001); } } }