// Copyright (C) Stichting Deltares 2017. 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 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;
#region Setup
[TestFixtureSetUp]
public void FixtureSetup()
{
mocks = new MockRepository();
repository = mocks.DynamicMock();
}
[TestFixtureTearDown]
public void FixtureTearDown()
{
}
[SetUp]
public void TestSetup()
{
}
[TearDown]
public void TestTearDown()
{
mocks.VerifyAll();
}
#endregion
class MockTarget
{
public double X { get; set; }
public double Y { get; set; }
public double Property1 { get; set; }
}
[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)
}));
Feature 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)
}));
Feature 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)
}));
Feature geom = Feature.Create(square);
geom.AddAttribute(attributeName, expected);
geometryRepository.Add(geom);
mocks.ReplayAll();
importer.Import();
Assert.AreEqual(expected, target.Property1);
}
[Test]
[ExpectedException(typeof(InvalidOperationException))]
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();
importer.Import();
}
[Test]
[ExpectedException(typeof(InvalidOperationException))]
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();
importer.Import();
}
[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)
}));
Feature 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());
}
}
}