From 870ca83ccc5462e4485846ee5ae7e653c7f2f103 Mon Sep 17 00:00:00 2001 From: Alejandro Sanchez Date: Fri, 31 May 2024 17:10:09 -0700 Subject: [PATCH 1/2] Test updates --- .../api/Services/AcquisitionFileService.cs | 4 - source/backend/api/Services/LeaseService.cs | 4 +- .../tests/core/Entities/NtsGeometryHelper.cs | 66 +++++ .../api/Controllers/LookupControllerTest.cs | 2 +- .../Services/AcquisitionFileServiceTest.cs | 105 ++++++-- .../CoordinateTransformServiceTest.cs | 54 +++- .../Services/DispositionFileServiceTest.cs | 11 +- .../unit/api/Services/LeaseServiceTest.cs | 234 ++++++++++++++++-- .../unit/api/Services/PropertyServiceTest.cs | 194 +++++++++++++++ .../api/Services/ResearchFileServiceTest.cs | 84 ++++++- 10 files changed, 713 insertions(+), 45 deletions(-) create mode 100644 source/backend/tests/core/Entities/NtsGeometryHelper.cs diff --git a/source/backend/api/Services/AcquisitionFileService.cs b/source/backend/api/Services/AcquisitionFileService.cs index dc4aa7ee40..ebf21e74d7 100644 --- a/source/backend/api/Services/AcquisitionFileService.cs +++ b/source/backend/api/Services/AcquisitionFileService.cs @@ -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; @@ -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; @@ -48,7 +46,6 @@ public AcquisitionFileService( IAcquisitionFilePropertyRepository acqFilePropertyRepository, IUserRepository userRepository, IPropertyRepository propertyRepository, - ICoordinateTransformService coordinateService, ILookupRepository lookupRepository, IEntityNoteRepository entityNoteRepository, IAcquisitionFileChecklistRepository checklistRepository, @@ -67,7 +64,6 @@ public AcquisitionFileService( _acquisitionFilePropertyRepository = acqFilePropertyRepository; _userRepository = userRepository; _propertyRepository = propertyRepository; - _coordinateService = coordinateService; _lookupRepository = lookupRepository; _entityNoteRepository = entityNoteRepository; _checklistRepository = checklistRepository; diff --git a/source/backend/api/Services/LeaseService.cs b/source/backend/api/Services/LeaseService.cs index 7da2cf2205..1366f85420 100644 --- a/source/backend/api/Services/LeaseService.cs +++ b/source/backend/api/Services/LeaseService.cs @@ -78,8 +78,6 @@ public PimsLease GetById(long leaseId) pimsUser.ThrowInvalidAccessToLeaseFile(_leaseRepository.GetNoTracking(leaseId).RegionCode); var lease = _leaseRepository.Get(leaseId); - - return lease; } @@ -441,7 +439,7 @@ private List GetActiveChecklistItemsForLease() List 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(), diff --git a/source/backend/tests/core/Entities/NtsGeometryHelper.cs b/source/backend/tests/core/Entities/NtsGeometryHelper.cs new file mode 100644 index 0000000000..313dfe07ee --- /dev/null +++ b/source/backend/tests/core/Entities/NtsGeometryHelper.cs @@ -0,0 +1,66 @@ +using NetTopologySuite.Geometries; +using Entity = Pims.Dal.Entities; + +namespace Pims.Core.Test +{ + /// + /// EntityHelper static class, provides helper methods to create test entities. + /// + public static partial class EntityHelper + { + /// + /// Creates a new instance of a Polygon. + /// + /// The target spatial reference Id (4396 = lat/lon), (3005 = BC ALBERS). + /// A polygon geometry instance. + 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); + } + + /// + /// Creates a new instance of a Polygon. + /// + /// An array without null elements, or an empty array, or null. + /// The target spatial reference Id (4396 = lat/lon), (3005 = BC ALBERS). + /// A polygon geometry instance. + public static Polygon CreatePolygon(Coordinate[] coordinates, int spatialReferenceId = 4396) + { + var gf = NetTopologySuite.NtsGeometryServices.Instance.CreateGeometryFactory(spatialReferenceId); + return gf.CreatePolygon(gf.CreateLinearRing(coordinates)); + } + + /// + /// Creates a geometric point object for the specified 'longitude' and 'latitude'. + /// + /// The x coordinate. + /// The y coordinate. + /// Spatial Reference Identifier (SRID) is a unique identifier associated with a specific coordinate system, tolerance, and resolution (default 4326). + /// A NetTopologySuite.Geometries.Point object. + public static Point CreatePoint(double longitude, double latitude, int spatialReferenceId) + { + return CreatePoint(new Coordinate(longitude, latitude), spatialReferenceId); + } + + /// + /// Creates a Point using the given Coordinate. A null coordinate creates an empty Geometry. + /// + /// a Coordinate, or null. + /// Spatial Reference Identifier (SRID) is a unique identifier associated with a specific coordinate system, tolerance, and resolution (default 4326). + /// A NetTopologySuite.Geometries.Point object. + 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); + } + } +} diff --git a/source/backend/tests/unit/api/Controllers/LookupControllerTest.cs b/source/backend/tests/unit/api/Controllers/LookupControllerTest.cs index f2050feadd..08294eb037 100644 --- a/source/backend/tests/unit/api/Controllers/LookupControllerTest.cs +++ b/source/backend/tests/unit/api/Controllers/LookupControllerTest.cs @@ -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"); diff --git a/source/backend/tests/unit/api/Services/AcquisitionFileServiceTest.cs b/source/backend/tests/unit/api/Services/AcquisitionFileServiceTest.cs index 24e2ee0f18..0e966431ee 100644 --- a/source/backend/tests/unit/api/Services/AcquisitionFileServiceTest.cs +++ b/source/backend/tests/unit/api/Services/AcquisitionFileServiceTest.cs @@ -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>(); + repository.Setup(x => x.GetPropertiesByAcquisitionFileId(It.IsAny())).Returns(new List()); + + // Act + Action act = () => service.GetProperties(1); + + // Assert + act.Should().Throw(); + } + + [Fact] + public void GetProperties_ByFileId_Success() + { + // Arrange + var service = this.CreateAcquisitionServiceWithPermissions(Permissions.AcquisitionFileView, Permissions.PropertyView); + + var acqFile = EntityHelper.CreateAcquisitionFile(); + + var repository = this._helper.GetService>(); + repository.Setup(x => x.GetRowVersion(It.IsAny())).Returns(1); + repository.Setup(x => x.GetById(It.IsAny())).Returns(acqFile); + + var propertyRepository = this._helper.GetService>(); + propertyRepository.Setup(x => x.GetPropertiesByAcquisitionFileId(It.IsAny())).Returns(new List()); + + // Act + var properties = service.GetProperties(1); + + // Assert + propertyRepository.Verify(x => x.GetPropertiesByAcquisitionFileId(It.IsAny()), 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>(); + repository.Setup(x => x.GetRowVersion(It.IsAny())).Returns(1); + repository.Setup(x => x.GetById(It.IsAny())).Returns(acqFile); + + var propertyRepository = this._helper.GetService>(); + propertyRepository.Setup(x => x.GetPropertiesByAcquisitionFileId(It.IsAny())) + .Returns(new List() { new() { Property = new() { Location = new Point(1, 1) } } }); + + var propertyService = this._helper.GetService>(); + propertyService.Setup(x => x.TransformAllPropertiesToLatLong(It.IsAny>())) + .Returns>(x => x); + + // Act + var properties = service.GetProperties(1); + + // Assert + propertyRepository.Verify(x => x.GetPropertiesByAcquisitionFileId(It.IsAny()), Times.Once); + propertyService.Verify(x => x.TransformAllPropertiesToLatLong(It.IsAny>()), Times.Once); + properties.FirstOrDefault().Property.Location.Coordinates.Should().BeEquivalentTo(new Coordinate[] { new Coordinate(1, 1) }); + } + + #endregion + #region UpdateProperties [Fact] public void UpdateProperties_Success() @@ -1220,6 +1292,9 @@ public void UpdateProperties_MatchProperties_Success() var userRepository = this._helper.GetService>(); userRepository.Setup(x => x.GetUserInfoByKeycloakUserId(It.IsAny())).Returns(EntityHelper.CreateUser(1, Guid.NewGuid(), "Test", regionCode: 1)); + var propertyService = this._helper.GetService>(); + propertyService.Setup(x => x.UpdateLocation(It.IsAny(), ref It.Ref.IsAny, It.IsAny>())); + var solver = this._helper.GetService>(); solver.Setup(x => x.CanEditProperties(It.IsAny())).Returns(true); @@ -1228,6 +1303,7 @@ public void UpdateProperties_MatchProperties_Success() // Assert filePropertyRepository.Verify(x => x.GetPropertiesByAcquisitionFileId(It.IsAny()), Times.Once); + propertyService.Verify(x => x.UpdateLocation(It.IsAny(), ref It.Ref.IsAny, It.IsAny>()), Times.Once); } [Fact] @@ -1255,20 +1331,19 @@ public void UpdateProperties_MatchProperties_NewProperty_Success() propertyRepository.Setup(x => x.GetByPid(It.IsAny(), true)).Throws(); propertyRepository.Setup(x => x.GetPropertyRegion(It.IsAny())).Returns(1); - var coordinateService = this._helper.GetService>(); - coordinateService.Setup(x => x.TransformCoordinates(It.IsAny(), It.IsAny(), It.IsAny())).Returns(new Coordinate(924046.3314288399, 1088892.9140135897)); - var propertyService = this._helper.GetService>(); - propertyService.Setup(x => x.PopulateNewProperty(It.IsAny(), It.IsAny(), It.IsAny())).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(), It.IsAny(), It.IsAny())).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>(); userRepository.Setup(x => x.GetUserInfoByKeycloakUserId(It.IsAny())).Returns(EntityHelper.CreateUser(1, Guid.NewGuid(), "Test", regionCode: 1)); @@ -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()), Times.Once); + propertyService.Verify(x => x.PopulateNewProperty(It.IsAny(), It.IsAny(), It.IsAny()), Times.Once); } [Fact] @@ -1388,6 +1464,7 @@ public void UpdateProperties_RemoveProperty_Success() var propertyRepository = this._helper.GetService>(); propertyRepository.Setup(x => x.GetByPid(It.IsAny(), false)).Throws(); propertyRepository.Setup(x => x.GetAllAssociationsById(It.IsAny())).Returns(property); + propertyRepository.Setup(x => x.GetAllAssociationsCountById(It.IsAny())).Returns(1); var userRepository = this._helper.GetService>(); userRepository.Setup(x => x.GetUserInfoByKeycloakUserId(It.IsAny())).Returns(EntityHelper.CreateUser("Test")); diff --git a/source/backend/tests/unit/api/Services/CoordinateTransformServiceTest.cs b/source/backend/tests/unit/api/Services/CoordinateTransformServiceTest.cs index 8fe6eda7e6..d59c77855d 100644 --- a/source/backend/tests/unit/api/Services/CoordinateTransformServiceTest.cs +++ b/source/backend/tests/unit/api/Services/CoordinateTransformServiceTest.cs @@ -28,7 +28,7 @@ public CoordinateTransformServiceTest() #region Tests [Fact] - public void Transform_Wgs84_BcAlbers() + public void TransformCoordinates_Wgs84_BcAlbers() { // Arrange var expected = new Coordinate(924303.6196359333, 1088419.4036716279); @@ -42,7 +42,7 @@ public void Transform_Wgs84_BcAlbers() } [Fact] - public void Transform_BcAlbers_Wgs84() + public void TransformCoordinates_BcAlbers_Wgs84() { // Arrange var expected = new Coordinate(-127.18432267731438, 54.793830114524795); @@ -56,15 +56,61 @@ public void Transform_BcAlbers_Wgs84() } [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(); + } + + + [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).Should().Be(new Coordinate(3021312.0903253276, 375417.28211033065)); + } + + [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).Should().Be(new Coordinate(-138.4471772534371, 44.199680362622246)); + } + + [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(() => this._service.TransformCoordinates(900913, 4326, location)); + act.Should().Throw(); } #endregion } diff --git a/source/backend/tests/unit/api/Services/DispositionFileServiceTest.cs b/source/backend/tests/unit/api/Services/DispositionFileServiceTest.cs index 9dad7f55ff..345cfb3c9d 100644 --- a/source/backend/tests/unit/api/Services/DispositionFileServiceTest.cs +++ b/source/backend/tests/unit/api/Services/DispositionFileServiceTest.cs @@ -356,7 +356,6 @@ public void Add_WithRetiredProperty_Should_Fail() #endregion #region Update - [Fact] public void Update_Should_Fail_NoPermission() { @@ -891,7 +890,9 @@ public void Update_Success_AddsNote() noteRepository.Verify(x => x.Add(It.Is(x => x.DispositionFileId == 1 && x.Note.NoteTxt == "Disposition File status changed from Closed to Active")), Times.Once); } + #endregion + #region UpdateProperties [Fact] public void UpdateProperties_Success() { @@ -944,11 +945,15 @@ public void UpdateProperties_MatchProperties_Success() var userRepository = this._helper.GetService>(); userRepository.Setup(x => x.GetUserInfoByKeycloakUserId(It.IsAny())).Returns(EntityHelper.CreateUser(1, Guid.NewGuid(), "Test", regionCode: 1)); + var propertyService = this._helper.GetService>(); + propertyService.Setup(x => x.UpdateLocation(It.IsAny(), ref It.Ref.IsAny, It.IsAny>())); + // Act service.UpdateProperties(dspFile, new List() { UserOverrideCode.AddLocationToProperty }); // Assert filePropertyRepository.Verify(x => x.GetPropertiesByDispositionFileId(It.IsAny()), Times.Once); + propertyService.Verify(x => x.UpdateLocation(It.IsAny(), ref It.Ref.IsAny, It.IsAny>()), Times.Once); } [Fact] @@ -1058,6 +1063,7 @@ public void UpdateProperties_MatchProperties_NewProperty_Success() updatedProperty.IsOwned.Should().Be(false); filePropertyRepository.Verify(x => x.GetPropertiesByDispositionFileId(It.IsAny()), Times.Once); + propertyService.Verify(x => x.PopulateNewProperty(It.IsAny(), It.IsAny(), It.IsAny()), Times.Once); } [Fact] @@ -1149,6 +1155,7 @@ public void UpdateProperties_RemoveProperty_Success() var propertyRepository = this._helper.GetService>(); propertyRepository.Setup(x => x.GetByPid(It.IsAny(), false)).Throws(); propertyRepository.Setup(x => x.GetAllAssociationsById(It.IsAny())).Returns(property); + propertyRepository.Setup(x => x.GetAllAssociationsCountById(It.IsAny())).Returns(1); var userRepository = this._helper.GetService>(); userRepository.Setup(x => x.GetUserInfoByKeycloakUserId(It.IsAny())).Returns(EntityHelper.CreateUser("Test")); @@ -1315,9 +1322,9 @@ public void UpdateProperties_WithRetiredProperty_Should_Fail() var ex = act.Should().Throw(); ex.WithMessage("Retired property can not be selected."); } - #endregion + #region GetTeamMembers [Fact] public void GetTeamMembers_Success() diff --git a/source/backend/tests/unit/api/Services/LeaseServiceTest.cs b/source/backend/tests/unit/api/Services/LeaseServiceTest.cs index e6e32d3403..4f7af99823 100644 --- a/source/backend/tests/unit/api/Services/LeaseServiceTest.cs +++ b/source/backend/tests/unit/api/Services/LeaseServiceTest.cs @@ -6,6 +6,7 @@ using Humanizer; using MapsterMapper; using Moq; +using NetTopologySuite.Geometries; using Pims.Api.Constants; using Pims.Api.Models.CodeTypes; using Pims.Api.Models.Concepts; @@ -50,7 +51,7 @@ public void Add_Success() // Arrange var lease = EntityHelper.CreateLease(1); lease.RegionCode = 1; - var user = new PimsUser(); + var user = EntityHelper.CreateUser("Test"); user.PimsRegionUsers.Add(new PimsRegionUser() { RegionCode = lease.RegionCode.Value }); var service = this.CreateLeaseService(Permissions.LeaseAdd); @@ -114,7 +115,7 @@ public void Add_InvalidAccessToLeaseFile() // Arrange var lease = EntityHelper.CreateLease(1); lease.RegionCode = 1; - var user = new PimsUser(); + var user = EntityHelper.CreateUser("Test"); user.PimsRegionUsers.Add(new PimsRegionUser() { RegionCode = 2 }); var service = this.CreateLeaseService(Permissions.LeaseAdd); @@ -137,7 +138,7 @@ public void Add_WithRetiredProperty_Should_Fail() // Arrange var lease = EntityHelper.CreateLease(1); lease.RegionCode = 1; - var user = new PimsUser(); + var user = EntityHelper.CreateUser("Test"); user.PimsRegionUsers.Add(new PimsRegionUser() { RegionCode = lease.RegionCode.Value }); PimsProperty retiredProperty = new PimsProperty() @@ -173,9 +174,122 @@ public void Add_WithRetiredProperty_Should_Fail() #endregion + #region Properties + [Fact] + public void GetProperties_ByFileId_NoPermission() + { + // Arrange + var service = this.CreateLeaseService(); + + var lease = EntityHelper.CreateLease(1); + lease.RegionCode = 1; + var user = EntityHelper.CreateUser("Test"); + user.PimsRegionUsers.Add(new PimsRegionUser() { RegionCode = lease.RegionCode.Value }); + + var leaseRepository = this._helper.GetService>(); + leaseRepository.Setup(x => x.GetNoTracking(It.IsAny())).Returns(lease); + + var propertyLeaseRepository = this._helper.GetService>(); + propertyLeaseRepository.Setup(x => x.GetAllByLeaseId(It.IsAny())).Returns(new List()); + + var userRepository = this._helper.GetService>(); + userRepository.Setup(x => x.GetByKeycloakUserId(It.IsAny())).Returns(user); + + // Act + Action act = () => service.GetPropertiesByLeaseId(1); + + // Assert + act.Should().Throw(); + } + + [Fact] + public void GetProperties_ByFileId_Success() + { + // Arrange + var service = this.CreateLeaseService(Permissions.LeaseView, Permissions.PropertyView); + + var lease = EntityHelper.CreateLease(1); + lease.RegionCode = 1; + var user = EntityHelper.CreateUser("Test"); + user.PimsRegionUsers.Add(new PimsRegionUser() { RegionCode = lease.RegionCode.Value }); + + var leaseRepository = this._helper.GetService>(); + leaseRepository.Setup(x => x.GetNoTracking(It.IsAny())).Returns(lease); + + var propertyLeaseRepository = this._helper.GetService>(); + propertyLeaseRepository.Setup(x => x.GetAllByLeaseId(It.IsAny())).Returns(new List()); + + var userRepository = this._helper.GetService>(); + userRepository.Setup(x => x.GetByKeycloakUserId(It.IsAny())).Returns(user); + + // Act + var properties = service.GetPropertiesByLeaseId(1); + + // Assert + propertyLeaseRepository.Verify(x => x.GetAllByLeaseId(It.IsAny()), Times.Once); + } + + [Fact] + public void GetProperties_ByFileId_Success_Reproject() + { + // Arrange + var service = this.CreateLeaseService(Permissions.LeaseView, Permissions.PropertyView); + + var lease = EntityHelper.CreateLease(1); + lease.RegionCode = 1; + var user = EntityHelper.CreateUser("Test"); + user.PimsRegionUsers.Add(new PimsRegionUser() { RegionCode = lease.RegionCode.Value }); + + var leaseRepository = this._helper.GetService>(); + leaseRepository.Setup(x => x.GetNoTracking(It.IsAny())).Returns(lease); + + var propertyLeaseRepository = this._helper.GetService>(); + propertyLeaseRepository.Setup(x => x.GetAllByLeaseId(It.IsAny())) + .Returns(new List() { new() { Property = new() { Location = new Point(1, 1) } } }); + + var userRepository = this._helper.GetService>(); + userRepository.Setup(x => x.GetByKeycloakUserId(It.IsAny())).Returns(user); + + var propertyService = this._helper.GetService>(); + propertyService.Setup(x => x.TransformAllPropertiesToLatLong(It.IsAny>())) + .Returns>(x => x); + + // Act + var properties = service.GetPropertiesByLeaseId(1); + + // Assert + propertyLeaseRepository.Verify(x => x.GetAllByLeaseId(It.IsAny()), Times.Once); + propertyService.Verify(x => x.TransformAllPropertiesToLatLong(It.IsAny>()), Times.Once); + properties.First().Property.Location.Coordinates.Should().BeEquivalentTo(new Coordinate[] { new Coordinate(1, 1) }); + } + + #endregion + #region Update [Fact] - public void Update_WithoutStatusNote() + public void Update_NoPermission() + { + // Arrange + var lease = EntityHelper.CreateLease(1); + + var service = this.CreateLeaseService(); + var leaseRepository = this._helper.GetService>(); + var propertyLeaseRepository = this._helper.GetService>(); + var userRepository = this._helper.GetService>(); + + leaseRepository.Setup(x => x.GetNoTracking(It.IsAny())).Returns(lease); + userRepository.Setup(x => x.GetByKeycloakUserId(It.IsAny())).Returns(EntityHelper.CreateUser("Test")); + + // Act + Action act = () => service.Update(lease, new List()); + + // Assert + act.Should().Throw(); + leaseRepository.Verify(x => x.Update(It.IsAny(), It.IsAny()), Times.Never); + } + + [Fact] + public void Update_Without_StatusNote() { // Arrange var service = this.CreateLeaseService(Permissions.LeaseEdit); @@ -196,7 +310,7 @@ public void Update_WithoutStatusNote() var userRepository = this._helper.GetService>(); leaseRepository.Setup(x => x.GetNoTracking(It.IsAny())).Returns(currentLeaseEntity); leaseRepository.Setup(x => x.Get(It.IsAny())).Returns(EntityHelper.CreateLease(1)); - userRepository.Setup(x => x.GetByKeycloakUserId(It.IsAny())).Returns(new PimsUser()); + userRepository.Setup(x => x.GetByKeycloakUserId(It.IsAny())).Returns(EntityHelper.CreateUser("Test")); var noteRepository = this._helper.GetService>(); @@ -208,7 +322,7 @@ public void Update_WithoutStatusNote() } [Fact] - public void Update_WithStatusNote() + public void Update_With_StatusNote() { // Arrange var service = this.CreateLeaseService(Permissions.LeaseEdit); @@ -231,7 +345,7 @@ public void Update_WithStatusNote() leaseRepository.Setup(x => x.GetNoTracking(It.IsAny())).Returns(currentLeaseEntity); leaseRepository.Setup(x => x.Get(It.IsAny())).Returns(EntityHelper.CreateLease(1)); - userRepository.Setup(x => x.GetByKeycloakUserId(It.IsAny())).Returns(new PimsUser()); + userRepository.Setup(x => x.GetByKeycloakUserId(It.IsAny())).Returns(EntityHelper.CreateUser("Test")); lookupRepository.Setup(x => x.GetAllLeaseStatusTypes()).Returns(new List() { new PimsLeaseStatusType() { @@ -255,7 +369,7 @@ public void Update_WithStatusNote() } [Fact] - public void Update_Properties_Success() + public void UpdateProperties_Success() { // Arrange var lease = EntityHelper.CreateLease(1); @@ -269,17 +383,21 @@ public void Update_Properties_Success() propertyRepository.Setup(x => x.GetByPid(It.IsAny(), true)).Returns(lease.PimsPropertyLeases.FirstOrDefault().Property); leaseRepository.Setup(x => x.GetNoTracking(It.IsAny())).Returns(lease); leaseRepository.Setup(x => x.Get(It.IsAny())).Returns(EntityHelper.CreateLease(1)); - userRepository.Setup(x => x.GetByKeycloakUserId(It.IsAny())).Returns(new PimsUser()); + userRepository.Setup(x => x.GetByKeycloakUserId(It.IsAny())).Returns(EntityHelper.CreateUser("Test")); + + var propertyService = this._helper.GetService>(); + propertyService.Setup(x => x.UpdateLocation(It.IsAny(), ref It.Ref.IsAny, It.IsAny>())); // Act var updatedLease = service.Update(lease, new List() { UserOverrideCode.AddLocationToProperty }); // Assert leaseRepository.Verify(x => x.Update(lease, false), Times.Once); + propertyService.Verify(x => x.UpdateLocation(It.IsAny(), ref It.Ref.IsAny, It.IsAny>()), Times.Once); } [Fact] - public void Update_Properties_WithRetiredProperty_Should_Fail() + public void UpdateProperties_WithRetiredProperty_Should_Fail() { // Arrange var lease = EntityHelper.CreateLease(1); @@ -300,7 +418,7 @@ public void Update_Properties_WithRetiredProperty_Should_Fail() propertyRepository.Setup(x => x.GetByPid(It.IsAny(), true)).Returns(property); leaseRepository.Setup(x => x.GetNoTracking(It.IsAny())).Returns(lease); leaseRepository.Setup(x => x.Get(It.IsAny())).Returns(EntityHelper.CreateLease(1)); - userRepository.Setup(x => x.GetByKeycloakUserId(It.IsAny())).Returns(new PimsUser()); + userRepository.Setup(x => x.GetByKeycloakUserId(It.IsAny())).Returns(EntityHelper.CreateUser("Test")); // Act Action act = () => service.Update(lease, new List() { UserOverrideCode.AddLocationToProperty }); @@ -311,7 +429,90 @@ public void Update_Properties_WithRetiredProperty_Should_Fail() } [Fact] - public void Update_Properties_Delete_Success() + public void UpdateProperties_MatchProperties_Success() + { + // Arrange + var lease = EntityHelper.CreateLease(1); + + var service = this.CreateLeaseService(Permissions.LeaseEdit, Permissions.LeaseView); + var leaseRepository = this._helper.GetService>(); + var propertyLeaseRepository = this._helper.GetService>(); + var propertyRepository = this._helper.GetService>(); + var userRepository = this._helper.GetService>(); + + propertyLeaseRepository.Setup(x => x.GetAllByLeaseId(It.IsAny())).Returns(lease.PimsPropertyLeases); + propertyRepository.Setup(x => x.GetByPid(It.IsAny(), true)).Returns(lease.PimsPropertyLeases.FirstOrDefault().Property); + leaseRepository.Setup(x => x.GetNoTracking(It.IsAny())).Returns(lease); + userRepository.Setup(x => x.GetByKeycloakUserId(It.IsAny())).Returns(EntityHelper.CreateUser("Test")); + + var propertyService = this._helper.GetService>(); + propertyService.Setup(x => x.UpdateLocation(It.IsAny(), ref It.Ref.IsAny, It.IsAny>())); + + // Act + var updatedLease = service.Update(lease, new List() { UserOverrideCode.AddLocationToProperty }); + + // Assert + leaseRepository.Verify(x => x.Update(lease, false), Times.Once); + propertyService.Verify(x => x.UpdateLocation(It.IsAny(), ref It.Ref.IsAny, It.IsAny>()), Times.Once); + } + + [Fact] + public void UpdateProperties_MatchProperties_NewProperty_Success() + { + // Arrange + var lease = EntityHelper.CreateLease(1); + + var service = this.CreateLeaseService(Permissions.LeaseEdit, Permissions.LeaseView, Permissions.PropertyAdd, Permissions.PropertyView); + var leaseRepository = this._helper.GetService>(); + var propertyLeaseRepository = this._helper.GetService>(); + var propertyRepository = this._helper.GetService>(); + var userRepository = this._helper.GetService>(); + + propertyLeaseRepository.Setup(x => x.GetAllByLeaseId(It.IsAny())).Returns(lease.PimsPropertyLeases); + propertyRepository.Setup(x => x.GetByPid(It.IsAny(), true)).Throws(); + leaseRepository.Setup(x => x.GetRowVersion(It.IsAny())).Returns(1); + leaseRepository.Setup(x => x.GetNoTracking(It.IsAny())).Returns(lease); + userRepository.Setup(x => x.GetByKeycloakUserId(It.IsAny())).Returns(EntityHelper.CreateUser("Test")); + + var propertyService = this._helper.GetService>(); + PimsProperty newProperty = null; + propertyService.Setup(x => x.PopulateNewProperty(It.IsAny(), It.IsAny(), It.IsAny())) + .Returns(newProperty) + .Callback((x, _, _) => + { + newProperty = x; + newProperty.Internal_Id = 0; + newProperty.PropertyClassificationTypeCode = "UNKNOWN"; + newProperty.PropertyDataSourceEffectiveDate = DateOnly.FromDateTime(System.DateTime.Now); + newProperty.PropertyDataSourceTypeCode = "PMBC"; + newProperty.PropertyTypeCode = "UNKNOWN"; + newProperty.PropertyStatusTypeCode = "UNKNOWN"; + newProperty.SurplusDeclarationTypeCode = "UNKNOWN"; + newProperty.RegionCode = 1; + }); + + + // Act + var updatedLease = service.Update(lease, new List() { UserOverrideCode.AddLocationToProperty }); + PimsPropertyLease updatedLeaseProperty = updatedLease.PimsPropertyLeases.First(); + + // Assert + // since this is a new property, the following default fields should be set. + var updatedProperty = updatedLeaseProperty.Property; + newProperty.PropertyClassificationTypeCode.Should().Be("UNKNOWN"); + newProperty.PropertyTypeCode.Should().Be("UNKNOWN"); + newProperty.PropertyStatusTypeCode.Should().Be("UNKNOWN"); + newProperty.SurplusDeclarationTypeCode.Should().Be("UNKNOWN"); + newProperty.PropertyDataSourceEffectiveDate.Should().Be(DateOnly.FromDateTime(DateTime.Now)); + newProperty.PropertyDataSourceTypeCode.Should().Be("PMBC"); + newProperty.IsOwned.Should().Be(false); + + leaseRepository.Verify(x => x.Update(lease, false), Times.Once); + propertyService.Verify(x => x.PopulateNewProperty(It.IsAny(), It.IsAny(), It.IsAny()), Times.Once); + } + + [Fact] + public void UpdateProperties_RemovePropertyFile_Success() { // Arrange var lease = EntityHelper.CreateLease(1); @@ -327,17 +528,18 @@ public void Update_Properties_Delete_Success() propertyRepository.Setup(x => x.GetAllAssociationsCountById(It.IsAny())).Returns(3); leaseRepository.Setup(x => x.GetNoTracking(It.IsAny())).Returns(lease); leaseRepository.Setup(x => x.Get(It.IsAny())).Returns(EntityHelper.CreateLease(1)); - userRepository.Setup(x => x.GetByKeycloakUserId(It.IsAny())).Returns(new PimsUser()); + userRepository.Setup(x => x.GetByKeycloakUserId(It.IsAny())).Returns(EntityHelper.CreateUser("Test")); // Act updatedLease = service.Update(updatedLease, new List()); // Assert + propertyLeaseRepository.Verify(x => x.UpdatePropertyLeases(It.IsAny(), It.IsAny>())); propertyRepository.Verify(x => x.Delete(It.IsAny()), Times.Never()); } [Fact] - public void Update_Properties_Delete_POI_Success() + public void UpdateProperties_RemoveProperty_Success() { // Arrange var lease = EntityHelper.CreateLease(1); @@ -353,14 +555,16 @@ public void Update_Properties_Delete_POI_Success() propertyLeaseRepository.Setup(x => x.GetAllByLeaseId(It.IsAny())).Returns(lease.PimsPropertyLeases); propertyRepository.Setup(x => x.GetByPid(It.IsAny(), false)).Returns(deletedProperty); propertyRepository.Setup(x => x.GetAllAssociationsById(It.IsAny())).Returns(lease.PimsPropertyLeases.FirstOrDefault().Property); + propertyRepository.Setup(x => x.GetAllAssociationsCountById(It.IsAny())).Returns(1); leaseRepository.Setup(x => x.GetNoTracking(It.IsAny())).Returns(lease); leaseRepository.Setup(x => x.Get(It.IsAny())).Returns(EntityHelper.CreateLease(1)); - userRepository.Setup(x => x.GetByKeycloakUserId(It.IsAny())).Returns(new PimsUser()); + userRepository.Setup(x => x.GetByKeycloakUserId(It.IsAny())).Returns(EntityHelper.CreateUser("Test")); // Act updatedLease = service.Update(updatedLease, new List()); // Assert + propertyLeaseRepository.Verify(x => x.UpdatePropertyLeases(It.IsAny(), It.IsAny>())); propertyRepository.Verify(x => x.Delete(deletedProperty), Times.Once); } diff --git a/source/backend/tests/unit/api/Services/PropertyServiceTest.cs b/source/backend/tests/unit/api/Services/PropertyServiceTest.cs index ac53ecef32..117e2ca11b 100644 --- a/source/backend/tests/unit/api/Services/PropertyServiceTest.cs +++ b/source/backend/tests/unit/api/Services/PropertyServiceTest.cs @@ -201,6 +201,200 @@ public void Update_Property_NoPermission() #endregion + #region UpdateLocation + [Fact] + public void UpdateLocation_Requires_UserOverride() + { + // Arrange + var property = EntityHelper.CreateProperty(200); + property.Location = null; + + var service = this.CreatePropertyServiceWithPermissions(Permissions.PropertyView, Permissions.PropertyEdit); + var repository = this._helper.GetService>(); + repository.Setup(x => x.Update(It.IsAny(), It.IsAny())).Returns(property); + + var coordinateService = this._helper.GetService>(); + coordinateService.Setup(x => x.TransformCoordinates(It.IsAny(), It.IsAny(), It.IsAny())).Returns(new Coordinate(14000, 9200)); + + var incomingProperty = new PimsProperty(); + incomingProperty.Description = "updated test"; + incomingProperty.Pid = 200; + incomingProperty.Location = EntityHelper.CreatePoint(-119, 53, SpatialReference.WGS84); + + // Act + Action act = () => service.UpdateLocation(incomingProperty, ref property, new List()); + + // Assert + var ex = act.Should().Throw(); + ex.Which.UserOverride.Should().Be(UserOverrideCode.AddLocationToProperty); + + coordinateService.Verify(x => x.TransformCoordinates(It.IsAny(), It.IsAny(), It.IsAny()), Times.Never); + repository.Verify(x => x.Update(It.IsAny(), It.IsAny()), Times.Never); + } + + [Fact] + public void UpdateLocation_Success() + { + // Arrange + var property = EntityHelper.CreateProperty(200); + property.Location = null; + + var service = this.CreatePropertyServiceWithPermissions(Permissions.PropertyView, Permissions.PropertyEdit); + var repository = this._helper.GetService>(); + repository.Setup(x => x.Update(It.IsAny(), It.IsAny())).Returns(property); + + var coordinateService = this._helper.GetService>(); + coordinateService.Setup(x => x.TransformCoordinates(It.IsAny(), It.IsAny(), It.IsAny())) + .Returns(new Coordinate(14000, 9200)); + + var incomingProperty = new PimsProperty(); + incomingProperty.Description = "updated test"; + incomingProperty.Pid = 200; + incomingProperty.Location = EntityHelper.CreatePoint(-119, 53, SpatialReference.WGS84); + + // Act + service.UpdateLocation(incomingProperty, ref property, new List() { UserOverrideCode.AddLocationToProperty }); + + // Assert + coordinateService.Verify(x => x.TransformCoordinates(It.IsAny(), It.IsAny(), It.IsAny()), Times.Once); + repository.Verify(x => x.Update(It.Is(p => p.Location.Coordinate.Equals(new Coordinate(14000, 9200))), It.IsAny()), Times.Once); + } + + [Fact] + public void UpdateLocation_Boundary_Success() + { + // Arrange + var property = EntityHelper.CreateProperty(200); + property.Location = null; + property.Boundary = null; + + var service = this.CreatePropertyServiceWithPermissions(Permissions.PropertyView, Permissions.PropertyEdit); + var repository = this._helper.GetService>(); + repository.Setup(x => x.Update(It.IsAny(), It.IsAny())).Returns(property); + + var coordinateService = this._helper.GetService>(); + coordinateService.Setup(x => x.TransformCoordinates(It.IsAny(), It.IsAny(), It.IsAny())) + .Returns(new Coordinate(14000, 9200)); + coordinateService.Setup(x => x.TransformGeometry(It.IsAny(), It.IsAny(), It.IsAny())) + .Callback((sourceSrid, targetSrid, boundary) => + { + // "apply" mock transformation + var polygon = boundary as Polygon; + polygon.SRID = targetSrid; + polygon.ExteriorRing.CoordinateSequence.SetX(0, 1000); + polygon.ExteriorRing.CoordinateSequence.SetY(0, 1000); + }); + + var incomingProperty = new PimsProperty(); + incomingProperty.Description = "updated test"; + incomingProperty.Pid = 200; + incomingProperty.Location = EntityHelper.CreatePoint(-119, 53, SpatialReference.WGS84); + incomingProperty.Boundary = EntityHelper.CreatePolygon(SpatialReference.WGS84); + + // Act + service.UpdateLocation(incomingProperty, ref property, new List() { UserOverrideCode.AddLocationToProperty }); + + // Assert + coordinateService.Verify(x => x.TransformCoordinates(It.IsAny(), It.IsAny(), It.IsAny()), Times.Once); + coordinateService.Verify(x => x.TransformGeometry(It.IsAny(), It.IsAny(), It.IsAny()), Times.Once); + repository.Verify(x => x.Update(property, true), Times.Once); + property.Location.Coordinate.Should().Be(new Coordinate(14000, 9200)); + property.Boundary.Should().BeOfType(); + var updatedBoundary = property.Boundary as Polygon; + updatedBoundary.ExteriorRing.GetCoordinateN(0).Should().Be(new Coordinate(1000, 1000)); + } + + #endregion + + #region PopulateNewProperty + [Fact] + public void PopulateNewProperty_Success() + { + // Arrange + var property = EntityHelper.CreateProperty(200); + property.Location = EntityHelper.CreatePoint(-119, 53, SpatialReference.WGS84); + property.Boundary = null; + + var service = this.CreatePropertyServiceWithPermissions(Permissions.PropertyView, Permissions.PropertyEdit); + + var lookupRepository = this._helper.GetService>(); + lookupRepository.Setup(x => x.GetAllCountries()).Returns(new[] { EntityHelper.CreateCountry(1, "CA") }); + lookupRepository.Setup(x => x.GetAllProvinces()).Returns(new[] { EntityHelper.CreateProvince(1, "BC") }); + + var coordinateService = this._helper.GetService>(); + coordinateService.Setup(x => x.TransformCoordinates(It.IsAny(), It.IsAny(), It.IsAny())) + .Returns(new Coordinate(14000, 9200)); + + // Act + service.PopulateNewProperty(property); + + // Assert + coordinateService.Verify(x => x.TransformCoordinates(It.IsAny(), It.IsAny(), It.IsAny()), Times.Once); + coordinateService.Verify(x => x.TransformGeometry(It.IsAny(), It.IsAny(), It.IsAny()), Times.Never); + + property.PropertyTypeCode.Should().Be("UNKNOWN"); + property.PropertyClassificationTypeCode.Should().Be("UNKNOWN"); + property.PropertyStatusTypeCode.Should().Be("UNKNOWN"); + property.SurplusDeclarationTypeCode.Should().Be("UNKNOWN"); + property.Location.Coordinate.Should().Be(new Coordinate(14000, 9200)); + property.Location.SRID.Should().Be(SpatialReference.BCALBERS); // Spatial reference should be in BC ALBERS for DB storage. + } + + [Fact] + public void PopulateNewProperty_Boundary_Success() + { + // Arrange + var property = EntityHelper.CreateProperty(200); + property.Location = EntityHelper.CreatePoint(-119, 53, SpatialReference.WGS84); + property.Boundary = EntityHelper.CreatePolygon(SpatialReference.WGS84); + + var service = this.CreatePropertyServiceWithPermissions(Permissions.PropertyView, Permissions.PropertyEdit); + + var lookupRepository = this._helper.GetService>(); + lookupRepository.Setup(x => x.GetAllCountries()).Returns(new[] { EntityHelper.CreateCountry(1, "CA") }); + lookupRepository.Setup(x => x.GetAllProvinces()).Returns(new[] { EntityHelper.CreateProvince(1, "BC") }); + + var coordinateService = this._helper.GetService>(); + coordinateService.Setup(x => x.TransformCoordinates(It.IsAny(), It.IsAny(), It.IsAny())) + .Returns(new Coordinate(14000, 9200)); + coordinateService.Setup(x => x.TransformGeometry(It.IsAny(), It.IsAny(), It.IsAny())) + .Callback((sourceSrid, targetSrid, boundary) => + { + // "apply" mock transformation + var polygon = boundary as Polygon; + polygon.SRID = targetSrid; + polygon.ExteriorRing.CoordinateSequence.SetX(0, 1000); + polygon.ExteriorRing.CoordinateSequence.SetY(0, 1000); + }); + + var incomingProperty = new PimsProperty(); + incomingProperty.Description = "updated test"; + incomingProperty.Pid = 200; + incomingProperty.Location = EntityHelper.CreatePoint(-119, 53, SpatialReference.WGS84); + incomingProperty.Boundary = EntityHelper.CreatePolygon(SpatialReference.WGS84); + + // Act + service.PopulateNewProperty(property); + + // Assert + coordinateService.Verify(x => x.TransformCoordinates(It.IsAny(), It.IsAny(), It.IsAny()), Times.Once); + coordinateService.Verify(x => x.TransformGeometry(It.IsAny(), It.IsAny(), It.IsAny()), Times.Once); + + property.PropertyTypeCode.Should().Be("UNKNOWN"); + property.PropertyClassificationTypeCode.Should().Be("UNKNOWN"); + property.PropertyStatusTypeCode.Should().Be("UNKNOWN"); + property.SurplusDeclarationTypeCode.Should().Be("UNKNOWN"); + property.Location.Coordinate.Should().Be(new Coordinate(14000, 9200)); + property.Location.SRID.Should().Be(SpatialReference.BCALBERS); // Spatial reference should be in BC ALBERS for DB storage. + property.Boundary.Should().BeOfType(); + + var updatedBoundary = property.Boundary as Polygon; + updatedBoundary.ExteriorRing.GetCoordinateN(0).Should().Be(new Coordinate(1000, 1000)); + updatedBoundary.SRID.Should().Be(SpatialReference.BCALBERS); // Spatial reference should be in BC ALBERS for DB storage. + } + + #endregion + #region Property Management [Fact] public void GetPropertyManagement_Success() diff --git a/source/backend/tests/unit/api/Services/ResearchFileServiceTest.cs b/source/backend/tests/unit/api/Services/ResearchFileServiceTest.cs index d1257f07ee..b44476527a 100644 --- a/source/backend/tests/unit/api/Services/ResearchFileServiceTest.cs +++ b/source/backend/tests/unit/api/Services/ResearchFileServiceTest.cs @@ -71,6 +71,78 @@ public void GetPage_NoPermission() #endregion + #region Properties + [Fact] + public void GetProperties_ByFileId_NoPermission() + { + // Arrange + var service = this.CreateResearchFileServiceWithPermissions(Permissions.ResearchFileView); + + var researchFile = EntityHelper.CreateResearchFile(1); + + var repository = this._helper.GetService>(); + repository.Setup(x => x.GetAllByResearchFileId(It.IsAny())).Returns(new List()); + + // Act + Action act = () => service.GetProperties(1); + + // Assert + act.Should().Throw(); + } + + [Fact] + public void GetProperties_ByFileId_Success() + { + // Arrange + var service = this.CreateResearchFileServiceWithPermissions(Permissions.ResearchFileView, Permissions.PropertyView); + + var researchFile = EntityHelper.CreateResearchFile(1); + + var repository = this._helper.GetService>(); + repository.Setup(x => x.GetRowVersion(It.IsAny())).Returns(1); + repository.Setup(x => x.GetById(It.IsAny())).Returns(researchFile); + + var propertyRepository = this._helper.GetService>(); + propertyRepository.Setup(x => x.GetAllByResearchFileId(It.IsAny())).Returns(new List()); + + // Act + var properties = service.GetProperties(1); + + // Assert + propertyRepository.Verify(x => x.GetAllByResearchFileId(It.IsAny()), Times.Once); + } + + [Fact] + public void GetProperties_ByFileId_Success_Reproject() + { + // Arrange + var service = this.CreateResearchFileServiceWithPermissions(Permissions.ResearchFileView, Permissions.PropertyView); + + var researchFile = EntityHelper.CreateResearchFile(1); + + var repository = this._helper.GetService>(); + repository.Setup(x => x.GetRowVersion(It.IsAny())).Returns(1); + repository.Setup(x => x.GetById(It.IsAny())).Returns(researchFile); + + var propertyRepository = this._helper.GetService>(); + propertyRepository.Setup(x => x.GetAllByResearchFileId(It.IsAny())) + .Returns(new List() { new() { Property = new() { Location = new Point(1, 1) } } }); + + var propertyService = this._helper.GetService>(); + propertyService.Setup(x => x.TransformAllPropertiesToLatLong(It.IsAny>())) + .Returns>(x => x); + + // Act + var properties = service.GetProperties(1); + + // Assert + propertyRepository.Verify(x => x.GetAllByResearchFileId(It.IsAny()), Times.Once); + propertyService.Verify(x => x.TransformAllPropertiesToLatLong(It.IsAny>()), Times.Once); + properties.FirstOrDefault().Property.Location.Coordinates.Should().BeEquivalentTo(new Coordinate[] { new Coordinate(1, 1) }); + } + + #endregion + #region UpdateProperties [Fact] public void UpdateProperties_Delete() @@ -121,11 +193,15 @@ public void UpdateProperties_MatchProperties_PID_Success() var filePropertyRepository = this._helper.GetService>(); filePropertyRepository.Setup(x => x.GetAllByResearchFileId(It.IsAny())).Returns(researchFile.PimsPropertyResearchFiles.ToList()); + var propertyService = this._helper.GetService>(); + propertyService.Setup(x => x.UpdateLocation(It.IsAny(), ref It.Ref.IsAny, It.IsAny>())); + // Act service.UpdateProperties(researchFile, new List() { UserOverrideCode.AddLocationToProperty }); // Assert filePropertyRepository.Verify(x => x.Add(It.IsAny()), Times.Once); + propertyService.Verify(x => x.UpdateLocation(It.IsAny(), ref It.Ref.IsAny, It.IsAny>()), Times.Once); } [Fact] @@ -151,11 +227,15 @@ public void UpdateProperties_MatchProperties_PIN_Success() var filePropertyRepository = this._helper.GetService>(); filePropertyRepository.Setup(x => x.GetAllByResearchFileId(It.IsAny())).Returns(researchFile.PimsPropertyResearchFiles.ToList()); + var propertyService = this._helper.GetService>(); + propertyService.Setup(x => x.UpdateLocation(It.IsAny(), ref It.Ref.IsAny, It.IsAny>())); + // Act service.UpdateProperties(researchFile, new List() { UserOverrideCode.AddLocationToProperty }); // Assert filePropertyRepository.Verify(x => x.Add(It.IsAny()), Times.Once); + propertyService.Verify(x => x.UpdateLocation(It.IsAny(), ref It.Ref.IsAny, It.IsAny>()), Times.Once); } [Fact] @@ -192,7 +272,7 @@ public void UpdateProperties_MatchProperties_PID_NewProperty_Success() SurplusDeclarationTypeCode = "UNKNOWN", RegionCode = 1 }); - + var propertyRepository = this._helper.GetService>(); propertyRepository.Setup(x => x.GetByPid(It.IsAny(), true)).Throws(); @@ -241,7 +321,7 @@ public void UpdateProperties_MatchProperties_PIN_NewProperty_Success() var propertyRepository = this._helper.GetService>(); propertyRepository.Setup(x => x.GetByPin(It.IsAny(), true)).Throws(); - + var propertyService = this._helper.GetService>(); propertyService.Setup(x => x.PopulateNewProperty(It.IsAny(), It.IsAny(), It.IsAny())).Returns(new PimsProperty() { From 795a25fd5f7d5ef4c897966638f75f4cab93c2eb Mon Sep 17 00:00:00 2001 From: Alejandro Date: Tue, 11 Jun 2024 15:56:50 -0700 Subject: [PATCH 2/2] Fix failing test --- .../CoordinateTransformServiceTest.cs | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/source/backend/tests/unit/api/Services/CoordinateTransformServiceTest.cs b/source/backend/tests/unit/api/Services/CoordinateTransformServiceTest.cs index d59c77855d..5046c6c370 100644 --- a/source/backend/tests/unit/api/Services/CoordinateTransformServiceTest.cs +++ b/source/backend/tests/unit/api/Services/CoordinateTransformServiceTest.cs @@ -31,28 +31,28 @@ public CoordinateTransformServiceTest() 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 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] @@ -81,7 +81,9 @@ public void TransformGeometry_Wgs84_BcAlbers() // Assert boundary.SRID.Should().Be(3005); - boundary.ExteriorRing.GetCoordinateN(0).Should().Be(new Coordinate(3021312.0903253276, 375417.28211033065)); + boundary.ExteriorRing.GetCoordinateN(0).X.Should().BeApproximately(3021312.09, 0.01d); + boundary.ExteriorRing.GetCoordinateN(0).Y.Should().BeApproximately(375417.28, 0.01d); + } [Fact] @@ -95,7 +97,8 @@ public void TransformGeometry_BcAlbers_Wgs84() // Assert boundary.SRID.Should().Be(4326); - boundary.ExteriorRing.GetCoordinateN(0).Should().Be(new Coordinate(-138.4471772534371, 44.199680362622246)); + boundary.ExteriorRing.GetCoordinateN(0).X.Should().BeApproximately(-138.45, 0.01d); + boundary.ExteriorRing.GetCoordinateN(0).Y.Should().BeApproximately(44.20, 0.01d); } [Fact]