// Copyright (C) Stichting Deltares 2025. 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 NetTopologySuite.Geometries;
using NSubstitute;
using NUnit.Framework;
namespace Deltares.Maps.Tests.Domain
{
[TestFixture]
public class CoverageFunctionTest
{
private IFeatureRepository repository;
#region Setup
[SetUp]
public void TestSetup()
{
repository = Substitute.For();
}
#endregion
[Test]
public void GetCoverageFunction_NoAttributesInList_Throws()
{
Assert.That(() => CoverageFunction.GetCoverageFunc(repository, new List()), Throws.ArgumentException);
}
[Test]
public void GetCoverageFunction_AttributeListContainsInvalidString_Throws()
{
Assert.That(() => CoverageFunction.GetCoverageFunc(repository, new List
{
""
}), Throws.ArgumentException);
}
[Test]
public void GetCoverageFunction_SupportedAttributeListIsNull_Throws()
{
Assert.That(() => repository.GetCoverageFunc("SomeAttribute", null), Throws.ArgumentNullException);
}
[Test]
public void GetCoverageFunction_RepositoryIsNull_Throws()
{
Assert.That(() => CoverageFunction.GetCoverageFunc(null, new[]
{
"SomeAttribute"
}), Throws.ArgumentNullException);
}
[Test]
public void UsingCoverageFunction_AttributeNameIsNullOrEmpty_Throws()
{
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);
const string attribute = "SomeAttribute";
const string expected = "test";
geom.AddAttribute(attribute, expected);
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));
}
[Test]
public void UsingCoverageFunction_AttributeNamenotContainedInAttributeList_Throws()
{
Func func = CoverageFunction.GetCoverageFunc(repository, new[]
{
"SomeAttribute"
});
Assert.That(() => func("AnotherAttribute", 0, 0), Throws.InstanceOf());
}
[Test]
public void UsingCoverageFunction_MoreThenOneGeometryFoundAtGivenLocation_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);
repository.Query(Arg.Any()).Returns(new[]
{
geom,
geom
});
const string attribute = "SomeAttribute";
repository.SupportedAttributes.Returns(new[]
{
attribute.ToUpper()
});
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_NoGeometryFoundAtGivenLocation_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";
repository.SupportedAttributes.Returns(new[]
{
attribute.ToUpper()
});
var geom = Feature.Create(square);
repository.Query(Arg.Any()).Returns(new[]
{
geom
});
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_GeometryFoundAtGivenLocation_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);
repository.Query(Arg.Any()).Returns(new[]
{
geom
});
const string attribute = "SomeAttribute";
const double expected = 20;
geom.AddAttribute(attribute, expected);
repository.SupportedAttributes.Returns(new[]
{
attribute.ToUpper()
});
Func func = repository.GetCoverageFunc();
var actual = (double) func(attribute, 0, 0);
Assert.That(actual, Is.EqualTo(expected).Within(0.0001));
}
[Test]
public void UsingCoverageFunction_GeometryFoundAtGivenLocation_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);
repository.Query(Arg.Any()).Returns(new[]
{
geom
});
const string attribute = "SomeAttribute";
const string expected = "test";
geom.AddAttribute(attribute, expected);
repository.SupportedAttributes.Returns(new[]
{
attribute
});
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);
repository.Query(Arg.Any()).Returns(new[]
{
geom
});
const string attribute = "SomeAttribute";
const double expected = 5;
geom.AddAttribute(attribute, expected);
repository.SupportedAttributes.Returns(new[]
{
attribute
});
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_GeometryFoundAtGivenLocation_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);
repository.Query(Arg.Any()).Returns(new[]
{
geom
});
repository.SupportedAttributes.Returns(new[]
{
attribute.ToUpper()
});
Func func = repository.GetCoverageFunc(attribute);
var actual = (double) func(0, 0);
Assert.That(actual, Is.EqualTo(expected).Within(0.0001));
}
}
}