Skip to content

Commit

Permalink
ensure that all subdivisions/consolidations only allow for owned sour…
Browse files Browse the repository at this point in the history
…ce properties.
  • Loading branch information
devinleighsmith committed Apr 8, 2024
1 parent c845829 commit 3739606
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 5 deletions.
5 changes: 5 additions & 0 deletions source/backend/api/Services/PropertyOperationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,11 @@ public IEnumerable<PimsPropertyOperation> ConsolidateProperty(IEnumerable<PimsPr

private static void CommonPropertyOperationValidation(IEnumerable<PimsPropertyOperation> 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.");
Expand Down
4 changes: 2 additions & 2 deletions source/backend/tests/core/Entities/PropertyOperationHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ public void Subdivide_Success_SameSourceDestinationPid()
// Arrange
var service = this.CreateDispositionServiceWithPermissions(Permissions.PropertyEdit);
var propertyService = this._helper.GetService<Mock<IPropertyService>>();
var sameProperty = EntityHelper.CreateProperty(3);
var sameProperty = EntityHelper.CreateProperty(3, isCoreInventory: true);
propertyService.Setup(x => x.GetById(It.IsAny<long>())).Returns(sameProperty);
propertyService.Setup(x => x.GetByPid(It.IsAny<string>())).Returns(sameProperty);
propertyService.Setup(x => x.RetireProperty(It.IsAny<PimsProperty>(), false)).Returns(sameProperty);
Expand Down Expand Up @@ -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<Mock<IPropertyService>>();
var sameProperty = EntityHelper.CreateProperty(3);
var otherProperty = EntityHelper.CreateProperty(4);
propertyService.Setup(x => x.GetMultipleById(It.IsAny<List<long>>())).Returns(new List<PimsProperty> { sameProperty, otherProperty });
propertyService.Setup(x => x.GetByPid(It.IsAny<string>())).Throws(new KeyNotFoundException());
propertyService.Setup(x => x.RetireProperty(It.IsAny<PimsProperty>(), false)).Returns((PimsProperty p, bool b) => p);
propertyService.Setup(x => x.PopulateNewProperty(It.IsAny<PimsProperty>(), 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<PimsPropertyOperation>() { operationOne, EntityHelper.CreatePropertyOperation() };

var repository = this._helper.GetService<Mock<IPropertyOperationRepository>>();
repository.Setup(x => x.AddRange(It.IsAny<List<PimsPropertyOperation>>())).Returns(operations);

// Act
Action act = () => service.ConsolidateProperty(operations);

// Assert
var exception = act.Should().Throw<BusinessRuleViolationException>();
exception.WithMessage("All source properties must be owned.");
}

[Fact]
public void Consolidate_Success()
{
Expand Down Expand Up @@ -437,8 +469,8 @@ public void Consolidate_Success_SameSourceDestinationPid()
// Arrange
var service = this.CreateDispositionServiceWithPermissions(Permissions.PropertyEdit);
var propertyService = this._helper.GetService<Mock<IPropertyService>>();
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<List<long>>())).Returns(new List<PimsProperty> { sameProperty, otherProperty });
propertyService.Setup(x => x.GetByPid(It.IsAny<string>())).Returns(sameProperty);
propertyService.Setup(x => x.RetireProperty(It.IsAny<PimsProperty>(), false)).Returns((PimsProperty p, bool b) => p);
Expand Down

0 comments on commit 3739606

Please sign in to comment.