// 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 FeatureRepositoryExtensionsTest { private IFeatureRepository rep1, rep2; private LineString dikeRing; [Test] public void GetLineIntersectionPoints_OneValidProfileIntersects_ListContainsOneElement() { var profile1 = new LineString(new[] { new Coordinate(-3, 1), new Coordinate(0, 1) }); var profile2 = new LineString(new[] { new Coordinate(-3, -3), new Coordinate(0, -3) }); var geom1 = Feature.Create(profile1); var geom2 = Feature.Create(profile2); rep1.Features.Returns(new[] { geom1, geom2 }); var dikeGeom = Feature.Create(dikeRing); rep2.Query(Arg.Any()).Returns(new[] { dikeGeom }); IEnumerable> results = rep1.GetIntersectionPoints(rep2); Assert.That(results, Is.Not.Null); Assert.Multiple(() => { Assert.That(results.Count(), Is.EqualTo(2)); Assert.That(results.Any(r => r.Key == 1), Is.True); Assert.That(results.Any(r => r.Key == 0), Is.True); }); IntersectionResult result = results.GetResultsHavingCount(1).Single(); Assert.Multiple(() => { Assert.That(geom1.Id, Is.EqualTo(result.Source.Id)); Assert.That(dikeGeom.Id, Is.EqualTo(result.Target.Id)); Assert.That(result.IntersectionPoints.ElementAt(0).X, Is.EqualTo(-2).Within(0.0001)); Assert.That(result.IntersectionPoints.ElementAt(0).Y, Is.EqualTo(1).Within(0.0001)); }); } [Test] public void GetLineIntersectionPoints_TwoValidProfilesIntersects_ListContainsTwoElements() { var profile1 = new LineString(new[] { new Coordinate(-3, 1), new Coordinate(0, 1) }); var profile2 = new LineString(new[] { new Coordinate(-3, -1), new Coordinate(0, -1) }); var geom1 = Feature.Create(profile1); var geom2 = Feature.Create(profile2); rep1.Features.Returns(new[] { geom1, geom2 }); var dikeGeom = Feature.Create(dikeRing); rep2.Query(Arg.Any()).Returns(new[] { dikeGeom }); IEnumerable> results = rep1.GetIntersectionPoints(rep2); Assert.That(results, Is.Not.Null); Assert.Multiple(() => { Assert.That(results.Count(), Is.EqualTo(1)); Assert.That(results.Any(r => r.Key == 1), Is.True); Assert.That(results.GetResultsHavingCount(1).Count(), Is.EqualTo(2)); }); IntersectionResult result = results.ElementAt(0).First(); Assert.Multiple(() => { Assert.That(geom1.Id, Is.EqualTo(result.Source.Id)); Assert.That(dikeGeom.Id, Is.EqualTo(result.Target.Id)); Assert.That(result.IntersectionPoints.ElementAt(0).X, Is.EqualTo(-2).Within(0.0001)); Assert.That(result.IntersectionPoints.ElementAt(0).Y, Is.EqualTo(1).Within(0.0001)); }); } [Test] public void GetLineIntersectionPoints_ProfileIntersectsOnTwoSegements_ListContainsOneElementAndTwoIntersectionPoints() { var profile1 = new LineString(new[] { new Coordinate(-3, 1), new Coordinate(3, 1) }); var geom1 = Feature.Create(profile1); rep1.Features.Returns(new[] { geom1 }); var dikeGeom = Feature.Create(dikeRing); rep2.Query(Arg.Any()).Returns(new[] { dikeGeom }); IEnumerable> results = rep1.GetIntersectionPoints(rep2); Assert.That(results, Is.Not.Null); Assert.Multiple(() => { Assert.That(results.Count(), Is.EqualTo(1)); Assert.That(results.GetResultsHavingCount(2).Count(), Is.EqualTo(1)); }); IntersectionResult result = results.ElementAt(0).First(); Assert.Multiple(() => { Assert.That(geom1.Id, Is.EqualTo(result.Source.Id)); Assert.That(dikeGeom.Id, Is.EqualTo(result.Target.Id)); }); } [Test] public void GetLineIntersectionPoints_ProfileDoesNotIntersects_ListContainsOneElementAndZeroIntersectionPoints() { var profile1 = new LineString(new[] { new Coordinate(-3, -3), new Coordinate(3, -3) }); var geom1 = Feature.Create(profile1); rep1.Features.Returns(new[] { geom1 }); var dikeGeom = Feature.Create(dikeRing); rep2.Query(Arg.Any()).Returns(new[] { dikeGeom }); IEnumerable> results = rep1.GetIntersectionPoints(rep2); Assert.That(results, Is.Not.Null); Assert.Multiple(() => { Assert.That(results.Count(), Is.EqualTo(1)); Assert.That(results.GetResultsHavingCount(0).Count(), Is.EqualTo(1)); }); } #region Setup [SetUp] public void FixtureSetup() { } [TearDown] public void FixtureTearDown() {} [SetUp] public void TestSetup() { rep1 = Substitute.For(); rep2 = Substitute.For(); dikeRing = new LineString(new[] { new Coordinate(-2, 2), new Coordinate(2, 2), new Coordinate(2, -2), new Coordinate(-2, -2), new Coordinate(-2, 2) }); } [TearDown] public void TestTearDown() {} #endregion } }