diff --git a/source/backend/api/Services/PropertyOperationService.cs b/source/backend/api/Services/PropertyOperationService.cs index a606b503fa..ae7dc8a37f 100644 --- a/source/backend/api/Services/PropertyOperationService.cs +++ b/source/backend/api/Services/PropertyOperationService.cs @@ -167,6 +167,11 @@ public IEnumerable ConsolidateProperty(IEnumerable operations) { + if (operations.Any(op => op.SourceProperty?.IsOwned != true)) + { + throw new BusinessRuleViolationException("All source properties must be owned."); + } + if (operations.Any(op => op.PropertyOperationNo != operations.FirstOrDefault().PropertyOperationNo)) { throw new BusinessRuleViolationException("All property operations must have matching operation numbers."); diff --git a/source/backend/tests/core/Entities/PropertyOperationHelper.cs b/source/backend/tests/core/Entities/PropertyOperationHelper.cs index 06ceee5d86..124c60a41a 100644 --- a/source/backend/tests/core/Entities/PropertyOperationHelper.cs +++ b/source/backend/tests/core/Entities/PropertyOperationHelper.cs @@ -27,8 +27,8 @@ public static Entity.PimsPropertyOperation CreatePropertyOperation(long? propert propertyOperation.PropertyOperationTypeCodeNavigation = operationType ?? new Entity.PimsPropertyOperationType() { PropertyOperationTypeCode = PropertyOperationTypes.SUBDIVIDE.ToString(), Description = "SUBDIVIDE", DbCreateUserid = "create user", DbLastUpdateUserid = "last user" }; propertyOperation.PropertyOperationNo = operationNo; propertyOperation.OperationDt = DateTime.UtcNow; - propertyOperation.SourceProperty = EntityHelper.CreateProperty(1); - propertyOperation.DestinationProperty = EntityHelper.CreateProperty(2); + propertyOperation.SourceProperty = EntityHelper.CreateProperty(1, isCoreInventory: true); + propertyOperation.DestinationProperty = EntityHelper.CreateProperty(2, isCoreInventory: true); return propertyOperation; } diff --git a/source/backend/tests/unit/api/Services/PropertyOperationServiceTest.cs b/source/backend/tests/unit/api/Services/PropertyOperationServiceTest.cs index e12137b77c..2727b94011 100644 --- a/source/backend/tests/unit/api/Services/PropertyOperationServiceTest.cs +++ b/source/backend/tests/unit/api/Services/PropertyOperationServiceTest.cs @@ -214,7 +214,7 @@ public void Subdivide_Success_SameSourceDestinationPid() // Arrange var service = this.CreateDispositionServiceWithPermissions(Permissions.PropertyEdit); var propertyService = this._helper.GetService>(); - var sameProperty = EntityHelper.CreateProperty(3); + var sameProperty = EntityHelper.CreateProperty(3, isCoreInventory: true); propertyService.Setup(x => x.GetById(It.IsAny())).Returns(sameProperty); propertyService.Setup(x => x.GetByPid(It.IsAny())).Returns(sameProperty); propertyService.Setup(x => x.RetireProperty(It.IsAny(), false)).Returns(sameProperty); @@ -400,6 +400,38 @@ public void Consolidate_Should_Fail_DestinationInInventory() exception.WithMessage("Consolidated child property may not be in the PIMS inventory unless also in the parent property list."); } + [Fact] + public void Consolidate_Should_Fail_NotOwnedSource() + { + // Arrange + var service = this.CreateDispositionServiceWithPermissions(Permissions.PropertyEdit); + var propertyService = this._helper.GetService>(); + var sameProperty = EntityHelper.CreateProperty(3); + var otherProperty = EntityHelper.CreateProperty(4); + propertyService.Setup(x => x.GetMultipleById(It.IsAny>())).Returns(new List { sameProperty, otherProperty }); + propertyService.Setup(x => x.GetByPid(It.IsAny())).Throws(new KeyNotFoundException()); + propertyService.Setup(x => x.RetireProperty(It.IsAny(), false)).Returns((PimsProperty p, bool b) => p); + propertyService.Setup(x => x.PopulateNewProperty(It.IsAny(), true, false)).Returns(sameProperty); + + var operationOne = EntityHelper.CreatePropertyOperation(); + operationOne.DestinationProperty.PropertyId = 0; + operationOne.SourceProperty.PropertyId = 5; + sameProperty.IsOwned = false; + operationOne.SourceProperty = sameProperty; + + var operations = new List() { operationOne, EntityHelper.CreatePropertyOperation() }; + + var repository = this._helper.GetService>(); + repository.Setup(x => x.AddRange(It.IsAny>())).Returns(operations); + + // Act + Action act = () => service.ConsolidateProperty(operations); + + // Assert + var exception = act.Should().Throw(); + exception.WithMessage("All source properties must be owned."); + } + [Fact] public void Consolidate_Success() { @@ -437,8 +469,8 @@ public void Consolidate_Success_SameSourceDestinationPid() // Arrange var service = this.CreateDispositionServiceWithPermissions(Permissions.PropertyEdit); var propertyService = this._helper.GetService>(); - var sameProperty = EntityHelper.CreateProperty(3); - var otherProperty = EntityHelper.CreateProperty(4); + var sameProperty = EntityHelper.CreateProperty(3, isCoreInventory: true); + var otherProperty = EntityHelper.CreateProperty(4, isCoreInventory: true); propertyService.Setup(x => x.GetMultipleById(It.IsAny>())).Returns(new List { sameProperty, otherProperty }); propertyService.Setup(x => x.GetByPid(It.IsAny())).Returns(sameProperty); propertyService.Setup(x => x.RetireProperty(It.IsAny(), false)).Returns((PimsProperty p, bool b) => p);