// Copyright (C) Stichting Deltares 2023. 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 PolygonAttributeImporterTest
{
private MockRepository mocks;
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);
mocks.ReplayAll();
importer.Import();
Assert.AreEqual(expected, target.Property1);
}
[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);
mocks.ReplayAll();
importer.Import();
Assert.AreEqual(expected, target.Property1);
}
[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);
mocks.ReplayAll();
importer.Import();
Assert.AreEqual(expected, target.Property1);
}
[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
};
Expect.Call(repository.SupportedAttributes).Return(new List());
mocks.ReplayAll();
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
}
}
};
mocks.ReplayAll();
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());
//Expect.Call(repository.Features).Return(new[] { feature });
Expect.Call(repository.Query((IGeometry) null)).IgnoreArguments().Return(new[]
{
feature
});
Expect.Call(repository.SupportedAttributes).Return(new[]
{
testAttribute
});
mocks.ReplayAll();
importer.Import();
Assert.AreEqual(1, importer.Errors.OfType().Count());
}
class MockTarget
{
public double X { get; set; }
public double Y { get; set; }
public double Property1 { get; set; }
}
#region Setup
[SetUp]
public void FixtureSetup()
{
mocks = new MockRepository();
repository = mocks.DynamicMock();
}
[TearDown]
public void FixtureTearDown() {}
[SetUp]
public void TestSetup() {}
[TearDown]
public void TestTearDown()
{
mocks.VerifyAll();
}
#endregion
}
}