// 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 GeoAPI.Geometries; using NetTopologySuite.Geometries; using NUnit.Framework; using Rhino.Mocks; namespace Deltares.Maps.Tests.Services { [TestFixture] public class FeatueRepositoryExtensionsTest { private MockRepository mocks; 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); Expect.Call(rep1.Features).Return(new[] { geom1, geom2 }); var dikeGeom = Feature.Create(dikeRing); Expect.Call(rep2.Query((IFeature) null)).IgnoreArguments().Return(new[] { dikeGeom }); mocks.ReplayAll(); IEnumerable> results = rep1.GetIntersectionPoints(rep2); Assert.IsNotNull(results); Assert.That(results.Count(), Is.EqualTo(2)); Assert.IsTrue(results.Any(r => r.Key == 1)); Assert.IsTrue(results.Any(r => r.Key == 0)); IntersectionResult result = results.GetResultsHavingCount(1).Single(); 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); Expect.Call(rep1.Features).Return(new[] { geom1, geom2 }); var dikeGeom = Feature.Create(dikeRing); Expect.Call(rep2.Query((IFeature) null)).IgnoreArguments().Return(new[] { dikeGeom }); mocks.ReplayAll(); IEnumerable> results = rep1.GetIntersectionPoints(rep2); Assert.IsNotNull(results); Assert.That(results.Count(), Is.EqualTo(1)); Assert.IsTrue(results.Any(r => r.Key == 1)); Assert.That(results.GetResultsHavingCount(1).Count(), Is.EqualTo(2)); IntersectionResult result = results.ElementAt(0).First(); 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); Expect.Call(rep1.Features).Return(new[] { geom1 }); var dikeGeom = Feature.Create(dikeRing); Expect.Call(rep2.Query((IFeature) null)).IgnoreArguments().Return(new[] { dikeGeom }); mocks.ReplayAll(); IEnumerable> results = rep1.GetIntersectionPoints(rep2); Assert.IsNotNull(results); Assert.That(results.Count(), Is.EqualTo(1)); Assert.That(results.GetResultsHavingCount(2).Count(), Is.EqualTo(1)); IntersectionResult result = results.ElementAt(0).First(); 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); Expect.Call(rep1.Features).Return(new[] { geom1 }); var dikeGeom = Feature.Create(dikeRing); Expect.Call(rep2.Query((IFeature) null)).IgnoreArguments().Return(new[] { dikeGeom }); mocks.ReplayAll(); IEnumerable> results = rep1.GetIntersectionPoints(rep2); Assert.IsNotNull(results); Assert.That(results.Count(), Is.EqualTo(1)); Assert.That(results.GetResultsHavingCount(0).Count(), Is.EqualTo(1)); } #region Setup [SetUp] public void FixtureSetup() { mocks = new MockRepository(); } [TearDown] public void FixtureTearDown() {} [SetUp] public void TestSetup() { rep1 = mocks.DynamicMock(); rep2 = mocks.DynamicMock(); 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() { mocks.VerifyAll(); } #endregion } }