// 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 Deltares.Dam.Data.CsvImporters;
using Deltares.Dam.Data.Importers;
using Deltares.Maps;
using NetTopologySuite.Geometries;
using NUnit.Framework;
using NSubstitute;
using DataAttribute = Deltares.Dam.Data.DataPlugins.Configuration.DataAttribute;
namespace Deltares.Dam.Tests.Importers
{
[TestFixture]
public class LocationShapeFileImporterTest
{
private ICollection locations;
private LocationShapeFileImporter importer;
private IFeatureRepository repository;
private Feature loc3, loc4;
[Test]
public void Importer_GivenEmptyLocationList_ShouldNotThrow()
{
Assert.DoesNotThrow(() =>
{
_ = new LocationShapeFileImporter(null, null);
});
}
[Test]
public void Importer_UsingValidConfiguredAttributed_NewLocationsAddedToImportedItems()
{
const string locAttributeName = "LID";
const string dridAttributeName = "DRID";
var configuredAttributes = new List
{
new DataAttribute
{
AttributeId = LocationShapeFileAttributeMap.LocationAttributeId,
AttributeName = locAttributeName
},
new DataAttribute
{
AttributeId = LocationShapeFileAttributeMap.DikeRingAttributeId,
AttributeName = dridAttributeName
}
};
importer = new LocationShapeFileImporter(locations, configuredAttributes)
{
Repository = repository
};
var l3 = Feature.Create(new Coordinate(1, 1));
l3.AddAttribute(locAttributeName, "loc3");
l3.AddAttribute(dridAttributeName, "ring5");
var l4 = Feature.Create(new Coordinate(2, 2));
l4.AddAttribute(locAttributeName, "loc4");
l4.AddAttribute(dridAttributeName, "ring5");
repository.Features.Returns(new[]
{
l3,
l4
});
importer.Import();
Assert.That(importer.ImportedItems.Count(), Is.EqualTo(4));
}
[Test]
public void Import_UsingValidRepository_NewLocationsAddedToImportedItems()
{
var configuredAttributes = new List
{
new DataAttribute
{
AttributeId = LocationShapeFileAttributeMap.LocationAttributeId
},
new DataAttribute
{
AttributeId = LocationShapeFileAttributeMap.DikeRingAttributeId
}
};
// Add existing location to test if that is handled correctly
var location = new CsvImporterLocations.LocationRecord
{
LocationId = "loc3",
DikeRingId = "ring5",
GeoX = 111.11,
GeoY = 222.22
};
locations = new List
{
location
};
importer = new LocationShapeFileImporter(locations, configuredAttributes)
{
Repository = repository
};
repository.Features.Returns(new[]
{
loc3,
loc4
});
importer.Import();
// ImportedItems should contain the total list of imported locations
// In this test a location is already in the locations list having same id as
// one feature returned by the feature repository
// This will cause that the importer overriders the existing location
// because of having the same name (id)
// Total should be 2
Assert.That(importer.ImportedItems.Count(), Is.EqualTo(2));
CsvImporterLocations.LocationRecord location2 = importer.ImportedItems.Single(l => l.LocationId == "loc3");
Assert.That(loc3.Geometry.Coordinate.X, Is.EqualTo(location2.GeoX));
Assert.That(loc3.Geometry.Coordinate.Y, Is.EqualTo(location2.GeoY));
}
#region Setup/Teardown
[SetUp]
public void FixtureSetup() {}
[SetUp]
public void TestSetup()
{
locations = new List
{
new CsvImporterLocations.LocationRecord
{
LocationId = "Location1"
},
new CsvImporterLocations.LocationRecord
{
LocationId = "Location2"
}
};
repository = Substitute.For();
LocationAttributeMapping lm = LocationShapeFileAttributeMap.GetAttributeMapping(LocationShapeFileAttributeMap.LocationAttributeId);
LocationAttributeMapping drm = LocationShapeFileAttributeMap.GetAttributeMapping(LocationShapeFileAttributeMap.DikeRingAttributeId);
loc3 = Feature.Create(new Coordinate(1, 1));
loc3.AddAttribute(lm.Name, "loc3");
loc3.AddAttribute(drm.Name, "ring5");
loc4 = Feature.Create(new Coordinate(2, 2));
loc4.AddAttribute(lm.Name, "loc4");
loc4.AddAttribute(drm.Name, "ring5");
}
[TearDown]
public void TestTearDown() {}
[TearDown]
public void FixtureTearDown() {}
#endregion
}
}