// Copyright (C) Stichting Deltares 2024. All rights reserved. // // This file is part of the application DAM - UI. // // DAM - UI is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program. If not, see . // // All names, logos, and references to "Deltares" are registered trademarks of // Stichting Deltares and remain full property of Stichting Deltares at all times. // All rights reserved. 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; [Test] public void GetCoverageFunction_NoAttributesInList_Throws() { mocks.ReplayAll(); Assert.That(() => CoverageFunction.GetCoverageFunc(repository, new List()), Throws.ArgumentException); } [Test] public void GetCoverageFunction_AttributeListContainsInvalidString_Throws() { mocks.ReplayAll(); Assert.That(() => CoverageFunction.GetCoverageFunc(repository, new List { "" }), Throws.ArgumentException); } [Test] public void GetCoverageFunction_SupportedAttributeListIsNull_Throws() { mocks.ReplayAll(); Assert.That(() => repository.GetCoverageFunc("SomeAttribute", null), Throws.ArgumentNullException); } [Test] public void GetCoverageFunction_RepositoryIsNull_Throws() { mocks.ReplayAll(); Assert.That(() => CoverageFunction.GetCoverageFunc(null, new[] { "SomeAttribute" }), Throws.ArgumentNullException); } [Test] public void UsingCoverageFunction_AttributeNameIsNullOrEmpty_Throws() { mocks.ReplayAll(); Func func = CoverageFunction.GetCoverageFunc(repository, new[] { "SomeAttribute" }); Assert.That(() => func("", 0, 0), Throws.ArgumentNullException); } [Test] public void CoverageFunction_AttributeNamesAndDifferentCase_ReturnsExpectedValue() { var lineString = new LineString(new[] { new Coordinate(1, 1), new Coordinate(1, 2) }); var rep = new FeatureRepository(); var 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.That(actual, Is.Not.Null); Assert.That(actual, Is.EqualTo(expected)); actual = (string) func(attribute.ToLower(), 1, 1); Assert.That(actual, Is.Not.Null); Assert.That(actual, Is.EqualTo(expected)); mocks.ReplayAll(); } [Test] public void UsingCoverageFunction_AttributeNamenotContainedInAttributeList_Throws() { mocks.ReplayAll(); Func func = CoverageFunction.GetCoverageFunc(repository, new[] { "SomeAttribute" }); Assert.That(() => func("AnotherAttribute", 0, 0), Throws.InstanceOf()); } [Test] 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) })); var 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(); Assert.That(() => func(attribute, 0, 0), Throws.InstanceOf().With.Message.EqualTo("Multiple geometries found covering point (0 0)")); } [Test] 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() }); var 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(); Assert.That(() => func("SomeAttribute", 10, 10), Throws.InstanceOf().With.Message.EqualTo("No geometries found covering point (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) })); var 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.That(actual, Is.EqualTo(expected).Within(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) })); var 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.That(actual, Is.Not.Null); Assert.That(actual, Is.EqualTo(expected)); } [Test] public void UsingCoverageFunctionForGivenLocation_RepositoryContainsLinestringGeometryType_ReturnsValue() { var lineString = new LineString(new[] { new Coordinate(1, 1), new Coordinate(1, 2) }); var 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.That(actual, Is.EqualTo(expected)); actual = (double) func(attribute, 1, 2); Assert.That(actual, Is.EqualTo(expected)); actual = (double) func(attribute, 1, 1.5); Assert.That(actual, Is.EqualTo(expected)); } [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) })); var 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.That(actual, Is.EqualTo(expected).Within(0.0001)); } #region Setup [SetUp] public void FixtureSetup() { mocks = new MockRepository(); } [TearDown] public void FixtureTearDown() {} [SetUp] public void TestSetup() { repository = mocks.DynamicMock(); } [TearDown] public void TestTearDown() { mocks.VerifyAll(); } #endregion } }