Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PSP-8194 Multipolygon test updates #4100

Merged
merged 2 commits into from
Jun 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions source/backend/api/Services/AcquisitionFileService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
using Pims.Dal.Entities.Extensions;
using Pims.Dal.Entities.Models;
using Pims.Dal.Exceptions;
using Pims.Dal.Helpers;
using Pims.Dal.Helpers.Extensions;
using Pims.Dal.Repositories;
using Pims.Dal.Security;
Expand All @@ -28,7 +27,6 @@ public class AcquisitionFileService : IAcquisitionFileService
private readonly IAcquisitionFilePropertyRepository _acquisitionFilePropertyRepository;
private readonly IUserRepository _userRepository;
private readonly IPropertyRepository _propertyRepository;
private readonly ICoordinateTransformService _coordinateService;
private readonly ILookupRepository _lookupRepository;
private readonly IEntityNoteRepository _entityNoteRepository;
private readonly IAcquisitionFileChecklistRepository _checklistRepository;
Expand All @@ -48,7 +46,6 @@ public AcquisitionFileService(
IAcquisitionFilePropertyRepository acqFilePropertyRepository,
IUserRepository userRepository,
IPropertyRepository propertyRepository,
ICoordinateTransformService coordinateService,
ILookupRepository lookupRepository,
IEntityNoteRepository entityNoteRepository,
IAcquisitionFileChecklistRepository checklistRepository,
Expand All @@ -67,7 +64,6 @@ public AcquisitionFileService(
_acquisitionFilePropertyRepository = acqFilePropertyRepository;
_userRepository = userRepository;
_propertyRepository = propertyRepository;
_coordinateService = coordinateService;
_lookupRepository = lookupRepository;
_entityNoteRepository = entityNoteRepository;
_checklistRepository = checklistRepository;
Expand Down
4 changes: 1 addition & 3 deletions source/backend/api/Services/LeaseService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,6 @@ public PimsLease GetById(long leaseId)
pimsUser.ThrowInvalidAccessToLeaseFile(_leaseRepository.GetNoTracking(leaseId).RegionCode);

var lease = _leaseRepository.Get(leaseId);


return lease;
}

Expand Down Expand Up @@ -441,7 +439,7 @@ private List<PimsLeaseChecklistItem> GetActiveChecklistItemsForLease()
List<PimsLeaseChecklistItem> chklistItems = new();
foreach (var itemType in _leaseRepository.GetAllChecklistItemTypes().Where(x => !x.IsExpiredType() && !x.IsDisabled))
{
PimsLeaseChecklistItem checklistItem = new ()
PimsLeaseChecklistItem checklistItem = new()
{
LeaseChklstItemTypeCode = itemType.LeaseChklstItemTypeCode,
LeaseChklstItemStatusTypeCode = LeaseChecklistItemStatusTypes.INCOMP.ToString(),
Expand Down
66 changes: 66 additions & 0 deletions source/backend/tests/core/Entities/NtsGeometryHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
using NetTopologySuite.Geometries;
using Entity = Pims.Dal.Entities;

namespace Pims.Core.Test
{
/// <summary>
/// EntityHelper static class, provides helper methods to create test entities.
/// </summary>
public static partial class EntityHelper
{
/// <summary>
/// Creates a new instance of a Polygon.
/// </summary>
/// <param name="spatialReferenceId">The target spatial reference Id (4396 = lat/lon), (3005 = BC ALBERS).</param>
/// <returns>A polygon geometry instance.</returns>
public static Polygon CreatePolygon(int spatialReferenceId = 4396)
{
return CreatePolygon(
new[]
{
new Coordinate(-100, 45),
new Coordinate(-98, 45),
new Coordinate(-99, 46),
new Coordinate(-100, 45),
},
spatialReferenceId);
}

/// <summary>
/// Creates a new instance of a Polygon.
/// </summary>
/// <param name="coordinates">An array without null elements, or an empty array, or null.</param>
/// <param name="spatialReferenceId">The target spatial reference Id (4396 = lat/lon), (3005 = BC ALBERS).</param>
/// <returns>A polygon geometry instance.</returns>
public static Polygon CreatePolygon(Coordinate[] coordinates, int spatialReferenceId = 4396)
{
var gf = NetTopologySuite.NtsGeometryServices.Instance.CreateGeometryFactory(spatialReferenceId);
return gf.CreatePolygon(gf.CreateLinearRing(coordinates));
}

/// <summary>
/// Creates a geometric point object for the specified 'longitude' and 'latitude'.
/// </summary>
/// <param name="longitude">The x coordinate.</param>
/// <param name="latitude">The y coordinate.</param>
/// <param name="spatialReferenceId">Spatial Reference Identifier (SRID) is a unique identifier associated with a specific coordinate system, tolerance, and resolution (default 4326).</param>
/// <returns>A NetTopologySuite.Geometries.Point object.</returns>
public static Point CreatePoint(double longitude, double latitude, int spatialReferenceId)
{
return CreatePoint(new Coordinate(longitude, latitude), spatialReferenceId);
}

/// <summary>
/// Creates a Point using the given Coordinate. A null coordinate creates an empty Geometry.
/// </summary>
/// <param name="coordinate">a Coordinate, or null.</param>
/// <param name="spatialReferenceId">Spatial Reference Identifier (SRID) is a unique identifier associated with a specific coordinate system, tolerance, and resolution (default 4326).</param>
/// <returns>A NetTopologySuite.Geometries.Point object.</returns>
public static Point CreatePoint(Coordinate coordinate, int spatialReferenceId)
{
// Spatial Reference Identifier (SRID) is a unique identifier associated with a specific coordinate system, tolerance, and resolution (default 4326)
var gf = NetTopologySuite.NtsGeometryServices.Instance.CreateGeometryFactory(spatialReferenceId);
return gf.CreatePoint(coordinate);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public void GetAll()
var classificationTypes = EntityHelper.CreatePropertyClassificationType("classification");
this._repository.Setup(m => m.GetAllPropertyClassificationTypes()).Returns(new[] { classificationTypes });

var countries = EntityHelper.CreateCountry(1, "CAN");
var countries = EntityHelper.CreateCountry(1, "CA");
this._repository.Setup(m => m.GetAllCountries()).Returns(new[] { countries });

var districts = EntityHelper.CreateDistrict(1, "district");
Expand Down
105 changes: 91 additions & 14 deletions source/backend/tests/unit/api/Services/AcquisitionFileServiceTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1103,6 +1103,78 @@ public void Update_NewTotalAllowableCompensation_Failure_LessThenCurrentFinancia
}
#endregion

#region Properties
[Fact]
public void GetProperties_ByFileId_NoPermission()
{
// Arrange
var service = this.CreateAcquisitionServiceWithPermissions(Permissions.AcquisitionFileView);

var acqFile = EntityHelper.CreateAcquisitionFile();

var repository = this._helper.GetService<Mock<IAcquisitionFilePropertyRepository>>();
repository.Setup(x => x.GetPropertiesByAcquisitionFileId(It.IsAny<long>())).Returns(new List<PimsPropertyAcquisitionFile>());

// Act
Action act = () => service.GetProperties(1);

// Assert
act.Should().Throw<NotAuthorizedException>();
}

[Fact]
public void GetProperties_ByFileId_Success()
{
// Arrange
var service = this.CreateAcquisitionServiceWithPermissions(Permissions.AcquisitionFileView, Permissions.PropertyView);

var acqFile = EntityHelper.CreateAcquisitionFile();

var repository = this._helper.GetService<Mock<IAcquisitionFileRepository>>();
repository.Setup(x => x.GetRowVersion(It.IsAny<long>())).Returns(1);
repository.Setup(x => x.GetById(It.IsAny<long>())).Returns(acqFile);

var propertyRepository = this._helper.GetService<Mock<IAcquisitionFilePropertyRepository>>();
propertyRepository.Setup(x => x.GetPropertiesByAcquisitionFileId(It.IsAny<long>())).Returns(new List<PimsPropertyAcquisitionFile>());

// Act
var properties = service.GetProperties(1);

// Assert
propertyRepository.Verify(x => x.GetPropertiesByAcquisitionFileId(It.IsAny<long>()), Times.Once);
}

[Fact]
public void GetProperties_ByFileId_Success_Reproject()
{
// Arrange
var service = this.CreateAcquisitionServiceWithPermissions(Permissions.AcquisitionFileView, Permissions.PropertyView);

var acqFile = EntityHelper.CreateAcquisitionFile();

var repository = this._helper.GetService<Mock<IAcquisitionFileRepository>>();
repository.Setup(x => x.GetRowVersion(It.IsAny<long>())).Returns(1);
repository.Setup(x => x.GetById(It.IsAny<long>())).Returns(acqFile);

var propertyRepository = this._helper.GetService<Mock<IAcquisitionFilePropertyRepository>>();
propertyRepository.Setup(x => x.GetPropertiesByAcquisitionFileId(It.IsAny<long>()))
.Returns(new List<PimsPropertyAcquisitionFile>() { new() { Property = new() { Location = new Point(1, 1) } } });

var propertyService = this._helper.GetService<Mock<IPropertyService>>();
propertyService.Setup(x => x.TransformAllPropertiesToLatLong(It.IsAny<List<PimsPropertyAcquisitionFile>>()))
.Returns<List<PimsPropertyAcquisitionFile>>(x => x);

// Act
var properties = service.GetProperties(1);

// Assert
propertyRepository.Verify(x => x.GetPropertiesByAcquisitionFileId(It.IsAny<long>()), Times.Once);
propertyService.Verify(x => x.TransformAllPropertiesToLatLong(It.IsAny<List<PimsPropertyAcquisitionFile>>()), Times.Once);
properties.FirstOrDefault().Property.Location.Coordinates.Should().BeEquivalentTo(new Coordinate[] { new Coordinate(1, 1) });
}

#endregion

#region UpdateProperties
[Fact]
public void UpdateProperties_Success()
Expand Down Expand Up @@ -1220,6 +1292,9 @@ public void UpdateProperties_MatchProperties_Success()
var userRepository = this._helper.GetService<Mock<IUserRepository>>();
userRepository.Setup(x => x.GetUserInfoByKeycloakUserId(It.IsAny<Guid>())).Returns(EntityHelper.CreateUser(1, Guid.NewGuid(), "Test", regionCode: 1));

var propertyService = this._helper.GetService<Mock<IPropertyService>>();
propertyService.Setup(x => x.UpdateLocation(It.IsAny<PimsProperty>(), ref It.Ref<PimsProperty>.IsAny, It.IsAny<IEnumerable<UserOverrideCode>>()));

var solver = this._helper.GetService<Mock<IAcquisitionStatusSolver>>();
solver.Setup(x => x.CanEditProperties(It.IsAny<AcquisitionStatusTypes?>())).Returns(true);

Expand All @@ -1228,6 +1303,7 @@ public void UpdateProperties_MatchProperties_Success()

// Assert
filePropertyRepository.Verify(x => x.GetPropertiesByAcquisitionFileId(It.IsAny<long>()), Times.Once);
propertyService.Verify(x => x.UpdateLocation(It.IsAny<PimsProperty>(), ref It.Ref<PimsProperty>.IsAny, It.IsAny<IEnumerable<UserOverrideCode>>()), Times.Once);
}

[Fact]
Expand Down Expand Up @@ -1255,20 +1331,19 @@ public void UpdateProperties_MatchProperties_NewProperty_Success()
propertyRepository.Setup(x => x.GetByPid(It.IsAny<int>(), true)).Throws<KeyNotFoundException>();
propertyRepository.Setup(x => x.GetPropertyRegion(It.IsAny<long>())).Returns(1);

var coordinateService = this._helper.GetService<Mock<ICoordinateTransformService>>();
coordinateService.Setup(x => x.TransformCoordinates(It.IsAny<int>(), It.IsAny<int>(), It.IsAny<Coordinate>())).Returns(new Coordinate(924046.3314288399, 1088892.9140135897));

var propertyService = this._helper.GetService<Mock<IPropertyService>>();
propertyService.Setup(x => x.PopulateNewProperty(It.IsAny<PimsProperty>(), It.IsAny<Boolean>(), It.IsAny<Boolean>())).Returns(new PimsProperty()
{
PropertyClassificationTypeCode = "UNKNOWN",
PropertyDataSourceEffectiveDate = DateOnly.FromDateTime(System.DateTime.Now),
PropertyDataSourceTypeCode = "PMBC",
PropertyTypeCode = "UNKNOWN",
PropertyStatusTypeCode = "UNKNOWN",
SurplusDeclarationTypeCode = "UNKNOWN",
RegionCode = 1,
});
propertyService.Setup(x => x.PopulateNewProperty(It.IsAny<PimsProperty>(), It.IsAny<Boolean>(), It.IsAny<Boolean>())).Returns(
new PimsProperty()
{
PropertyClassificationTypeCode = "UNKNOWN",
PropertyDataSourceEffectiveDate = DateOnly.FromDateTime(System.DateTime.Now),
PropertyDataSourceTypeCode = "PMBC",
PropertyTypeCode = "UNKNOWN",
PropertyStatusTypeCode = "UNKNOWN",
SurplusDeclarationTypeCode = "UNKNOWN",
RegionCode = 1,
}
);

var userRepository = this._helper.GetService<Mock<IUserRepository>>();
userRepository.Setup(x => x.GetUserInfoByKeycloakUserId(It.IsAny<Guid>())).Returns(EntityHelper.CreateUser(1, Guid.NewGuid(), "Test", regionCode: 1));
Expand All @@ -1288,9 +1363,10 @@ public void UpdateProperties_MatchProperties_NewProperty_Success()
updatedProperty.SurplusDeclarationTypeCode.Should().Be("UNKNOWN");
updatedProperty.PropertyDataSourceEffectiveDate.Should().Be(DateOnly.FromDateTime(DateTime.Now));
updatedProperty.PropertyDataSourceTypeCode.Should().Be("PMBC");
updatedProperty.IsOwned.Should().Be(false);
updatedProperty.IsOwned.Should().Be(false);

filePropertyRepository.Verify(x => x.GetPropertiesByAcquisitionFileId(It.IsAny<long>()), Times.Once);
propertyService.Verify(x => x.PopulateNewProperty(It.IsAny<PimsProperty>(), It.IsAny<Boolean>(), It.IsAny<Boolean>()), Times.Once);
}

[Fact]
Expand Down Expand Up @@ -1388,6 +1464,7 @@ public void UpdateProperties_RemoveProperty_Success()
var propertyRepository = this._helper.GetService<Mock<IPropertyRepository>>();
propertyRepository.Setup(x => x.GetByPid(It.IsAny<int>(), false)).Throws<KeyNotFoundException>();
propertyRepository.Setup(x => x.GetAllAssociationsById(It.IsAny<long>())).Returns(property);
propertyRepository.Setup(x => x.GetAllAssociationsCountById(It.IsAny<long>())).Returns(1);

var userRepository = this._helper.GetService<Mock<IUserRepository>>();
userRepository.Setup(x => x.GetUserInfoByKeycloakUserId(It.IsAny<Guid>())).Returns(EntityHelper.CreateUser("Test"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,43 +28,92 @@ public CoordinateTransformServiceTest()

#region Tests
[Fact]
public void Transform_Wgs84_BcAlbers()
public void TransformCoordinates_Wgs84_BcAlbers()
{
// Arrange
var expected = new Coordinate(924303.6196359333, 1088419.4036716279);
var location = new Coordinate(-127.18, 54.79);

// Act
var location = new Coordinate(-127.18, 54.79);
var actual = this._service.TransformCoordinates(4326, 3005, location);

// Assert
actual.Should().Be(expected);
actual.X.Should().BeApproximately(924303.62, 0.01d);
actual.Y.Should().BeApproximately(1088419.40, 0.01d);
}

[Fact]
public void Transform_BcAlbers_Wgs84()
public void TransformCoordinates_BcAlbers_Wgs84()
{
// Arrange
var expected = new Coordinate(-127.18432267731438, 54.793830114524795);
var location = new Coordinate(924033.50, 1088851.50);

// Act
var location = new Coordinate(924033.50, 1088851.50);
var actual = this._service.TransformCoordinates(3005, 4326, location);

// Assert
actual.Should().Be(expected);
actual.X.Should().BeApproximately(-127.18, 0.01d);
actual.Y.Should().BeApproximately(54.79, 0.01d);
}

[Fact]
public void Transform_Not_Supported()
public void TransformCoordinates_NotSupported()
{
// Arrange
var location = new Coordinate(924033.50, 1088851.50);

// Act
Action act = () => this._service.TransformCoordinates(900913, 4326, location);

// Assert
this._service.IsCoordinateSystemSupported(900913).Should().BeFalse();
act.Should().Throw<InvalidOperationException>();
}


[Fact]
public void TransformGeometry_Wgs84_BcAlbers()
{
// Arrange
var boundary = EntityHelper.CreatePolygon(4396);

// Act
this._service.TransformGeometry(4326, 3005, boundary);

// Assert
boundary.SRID.Should().Be(3005);
boundary.ExteriorRing.GetCoordinateN(0).X.Should().BeApproximately(3021312.09, 0.01d);
boundary.ExteriorRing.GetCoordinateN(0).Y.Should().BeApproximately(375417.28, 0.01d);

}

[Fact]
public void TransformGeometry_BcAlbers_Wgs84()
{
// Arrange
var boundary = EntityHelper.CreatePolygon(3005);

// Act
this._service.TransformGeometry(3005, 4326, boundary);

// Assert
boundary.SRID.Should().Be(4326);
boundary.ExteriorRing.GetCoordinateN(0).X.Should().BeApproximately(-138.45, 0.01d);
boundary.ExteriorRing.GetCoordinateN(0).Y.Should().BeApproximately(44.20, 0.01d);
}

[Fact]
public void TransformGeometry_NotSupported()
{
// Arrange
// Arrange
var boundary = EntityHelper.CreatePolygon(900913);

// Act
Action act = () => this._service.TransformGeometry(900913, 4326, boundary);

// Assert
this._service.IsCoordinateSystemSupported(900913).Should().BeFalse();
Assert.Throws<InvalidOperationException>(() => this._service.TransformCoordinates(900913, 4326, location));
act.Should().Throw<InvalidOperationException>();
}
#endregion
}
Expand Down
Loading
Loading