// 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.Collections.Generic; using System.Linq; using NetTopologySuite.Geometries; using NUnit.Framework; using NSubstitute; namespace Deltares.Maps.Tests.Services { [TestFixture] public class PolygonAttributeImporterTest { private IFeatureRepository repository; [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) })); var geom = Feature.Create(square); geom.AddAttribute(attributeName, expected); geometryRepository.Add(geom); importer.Import(); Assert.That(target.Property1, Is.EqualTo(expected)); } [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) })); var geom = Feature.Create(square); geom.AddAttribute(attributeName, expected); geometryRepository.Add(geom); importer.Import(); Assert.That(target.Property1, Is.EqualTo(expected)); } [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) })); var geom = Feature.Create(square); geom.AddAttribute(attributeName, expected); geometryRepository.Add(geom); importer.Import(); Assert.That(target.Property1, Is.EqualTo(expected)); } [Test] 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 }; repository.SupportedAttributes.Returns(new List()); Assert.That(() => importer.Import(), Throws.InvalidOperationException); } [Test] 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 } } }; Assert.That(() => importer.Import(), Throws.InvalidOperationException); } [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) })); var feature = Feature.Create(square); const string testAttribute = "test"; feature.AddAttribute(testAttribute, new object()); repository.Query(Arg.Any()).Returns(new[] { feature }); repository.SupportedAttributes.Returns(new[] { testAttribute }); importer.Import(); Assert.That(importer.Errors.OfType().Count(), Is.EqualTo(1)); } class MockTarget { public double X { get; set; } public double Y { get; set; } public double Property1 { get; set; } } #region Setup [SetUp] public void FixtureSetup() { repository = Substitute.For(); } [TearDown] public void FixtureTearDown() {} [SetUp] public void TestSetup() {} [TearDown] public void TestTearDown() {} #endregion } }