From 8bccf09eb3c8e42ae2356a27599bd87d02a3a00b Mon Sep 17 00:00:00 2001 From: Rik De Peuter Date: Wed, 29 May 2024 11:42:07 +0200 Subject: [PATCH] fix: remove all duplicate address links when detaching (#785) --- src/ParcelRegistry/Parcel/Parcel_State.cs | 10 +++++----- .../GivenAddressAttached.cs | 4 ++++ .../GivenAddressAttached.cs | 4 ++++ .../GivenAddressAttached.cs | 4 ++++ .../WhenDetachingParcelAddress/GivenAddressAttached.cs | 4 ++++ .../WhenReaddressingAddresses/GivenParcelExists.cs | 3 +++ .../GivenParcelExistsWithAttachedAddresses.cs | 2 +- .../WhenRetiringParcel/GivenParcelIsRemoved.cs | 6 ------ 8 files changed, 25 insertions(+), 12 deletions(-) diff --git a/src/ParcelRegistry/Parcel/Parcel_State.cs b/src/ParcelRegistry/Parcel/Parcel_State.cs index 86e3093d..557ec900 100644 --- a/src/ParcelRegistry/Parcel/Parcel_State.cs +++ b/src/ParcelRegistry/Parcel/Parcel_State.cs @@ -113,28 +113,28 @@ private void When(ParcelAddressWasAttachedV2 @event) private void When(ParcelAddressWasDetachedV2 @event) { - _addressPersistentLocalIds.Remove(new AddressPersistentLocalId(@event.AddressPersistentLocalId)); + _addressPersistentLocalIds.RemoveAll(x => x == new AddressPersistentLocalId(@event.AddressPersistentLocalId)); _lastEvent = @event; } private void When(ParcelAddressWasDetachedBecauseAddressWasRemoved @event) { - _addressPersistentLocalIds.Remove(new AddressPersistentLocalId(@event.AddressPersistentLocalId)); + _addressPersistentLocalIds.RemoveAll(x => x == new AddressPersistentLocalId(@event.AddressPersistentLocalId)); _lastEvent = @event; } private void When(ParcelAddressWasDetachedBecauseAddressWasRejected @event) { - _addressPersistentLocalIds.Remove(new AddressPersistentLocalId(@event.AddressPersistentLocalId)); + _addressPersistentLocalIds.RemoveAll(x => x == new AddressPersistentLocalId(@event.AddressPersistentLocalId)); _lastEvent = @event; } private void When(ParcelAddressWasDetachedBecauseAddressWasRetired @event) { - _addressPersistentLocalIds.Remove(new AddressPersistentLocalId(@event.AddressPersistentLocalId)); + _addressPersistentLocalIds.RemoveAll(x => x == new AddressPersistentLocalId(@event.AddressPersistentLocalId)); _lastEvent = @event; } @@ -151,7 +151,7 @@ private void When(ParcelAddressesWereReaddressed @event) { foreach (var addressPersistentLocalId in @event.DetachedAddressPersistentLocalIds) { - _addressPersistentLocalIds.Remove(new AddressPersistentLocalId(addressPersistentLocalId)); + _addressPersistentLocalIds.RemoveAll(x => x == new AddressPersistentLocalId(addressPersistentLocalId)); } foreach (var addressPersistentLocalId in @event.AttachedAddressPersistentLocalIds) diff --git a/test/ParcelRegistry.Tests/AggregateTests/WhenDetachingAddressBecauseAddressWasRejected/GivenAddressAttached.cs b/test/ParcelRegistry.Tests/AggregateTests/WhenDetachingAddressBecauseAddressWasRejected/GivenAddressAttached.cs index 784d6a80..acf45c40 100644 --- a/test/ParcelRegistry.Tests/AggregateTests/WhenDetachingAddressBecauseAddressWasRejected/GivenAddressAttached.cs +++ b/test/ParcelRegistry.Tests/AggregateTests/WhenDetachingAddressBecauseAddressWasRejected/GivenAddressAttached.cs @@ -1,6 +1,7 @@ namespace ParcelRegistry.Tests.AggregateTests.WhenDetachingAddressBecauseAddressWasRejected { using System.Collections.Generic; + using System.Linq; using Autofac; using AutoFixture; using BackOffice; @@ -67,9 +68,12 @@ public void StateCheck() var parcelWasMigrated = new ParcelWasMigratedBuilder(Fixture) .WithStatus(ParcelStatus.Realized) .WithAddress(addressPersistentLocalId) + .WithAddress(addressPersistentLocalId) .WithAddress(456) .Build(); + parcelWasMigrated.AddressPersistentLocalIds.Count(x => x == addressPersistentLocalId).Should().Be(2); + var parcelAddressWasDetachedV2 = new ParcelAddressWasDetachedBecauseAddressWasRejectedBuilder(Fixture) .WithAddress(addressPersistentLocalId) .Build(); diff --git a/test/ParcelRegistry.Tests/AggregateTests/WhenDetachingAddressBecauseAddressWasRemoved/GivenAddressAttached.cs b/test/ParcelRegistry.Tests/AggregateTests/WhenDetachingAddressBecauseAddressWasRemoved/GivenAddressAttached.cs index d5d3fba4..3d90ada8 100644 --- a/test/ParcelRegistry.Tests/AggregateTests/WhenDetachingAddressBecauseAddressWasRemoved/GivenAddressAttached.cs +++ b/test/ParcelRegistry.Tests/AggregateTests/WhenDetachingAddressBecauseAddressWasRemoved/GivenAddressAttached.cs @@ -1,6 +1,7 @@ namespace ParcelRegistry.Tests.AggregateTests.WhenDetachingAddressBecauseAddressWasRemoved { using System.Collections.Generic; + using System.Linq; using Autofac; using AutoFixture; using BackOffice; @@ -66,9 +67,12 @@ public void StateCheck() var parcelWasMigrated = new ParcelWasMigratedBuilder(Fixture) .WithStatus(ParcelStatus.Realized) .WithAddress(addressPersistentLocalId) + .WithAddress(addressPersistentLocalId) .WithAddress(456) .Build(); + parcelWasMigrated.AddressPersistentLocalIds.Count(x => x == addressPersistentLocalId).Should().Be(2); + var parcelAddressWasDetachedV2 = new ParcelAddressWasDetachedBecauseAddressWasRemovedBuilder(Fixture) .WithAddress(addressPersistentLocalId) .Build(); diff --git a/test/ParcelRegistry.Tests/AggregateTests/WhenDetachingAddressBecauseAddressWasRetired/GivenAddressAttached.cs b/test/ParcelRegistry.Tests/AggregateTests/WhenDetachingAddressBecauseAddressWasRetired/GivenAddressAttached.cs index 3b79a43a..df1cf4cf 100644 --- a/test/ParcelRegistry.Tests/AggregateTests/WhenDetachingAddressBecauseAddressWasRetired/GivenAddressAttached.cs +++ b/test/ParcelRegistry.Tests/AggregateTests/WhenDetachingAddressBecauseAddressWasRetired/GivenAddressAttached.cs @@ -1,6 +1,7 @@ namespace ParcelRegistry.Tests.AggregateTests.WhenDetachingAddressBecauseAddressWasRetired { using System.Collections.Generic; + using System.Linq; using Autofac; using AutoFixture; using BackOffice; @@ -66,9 +67,12 @@ public void StateCheck() var parcelWasMigrated = new ParcelWasMigratedBuilder(Fixture) .WithStatus(ParcelStatus.Realized) .WithAddress(addressPersistentLocalId) + .WithAddress(addressPersistentLocalId) .WithAddress(456) .Build(); + parcelWasMigrated.AddressPersistentLocalIds.Count(x => x == addressPersistentLocalId).Should().Be(2); + var parcelAddressWasDetachedV2 = new ParcelAddressWasDetachedBecauseAddressWasRetiredBuilder(Fixture) .WithAddress(addressPersistentLocalId) .Build(); diff --git a/test/ParcelRegistry.Tests/AggregateTests/WhenDetachingParcelAddress/GivenAddressAttached.cs b/test/ParcelRegistry.Tests/AggregateTests/WhenDetachingParcelAddress/GivenAddressAttached.cs index 5efd7f1c..7e5d344a 100644 --- a/test/ParcelRegistry.Tests/AggregateTests/WhenDetachingParcelAddress/GivenAddressAttached.cs +++ b/test/ParcelRegistry.Tests/AggregateTests/WhenDetachingParcelAddress/GivenAddressAttached.cs @@ -1,6 +1,7 @@ namespace ParcelRegistry.Tests.AggregateTests.WhenDetachingParcelAddress { using System.Collections.Generic; + using System.Linq; using Autofac; using AutoFixture; using BackOffice; @@ -66,9 +67,12 @@ public void StateCheck() var parcelWasMigrated = new ParcelWasMigratedBuilder(Fixture) .WithStatus(ParcelStatus.Realized) .WithAddress(addressPersistentLocalId) + .WithAddress(addressPersistentLocalId) .WithAddress(456) .Build(); + parcelWasMigrated.AddressPersistentLocalIds.Count(x => x == addressPersistentLocalId).Should().Be(2); + var parcelAddressWasDetachedV2 = new ParcelAddressWasDetachedV2Builder(Fixture) .WithAddress(addressPersistentLocalId) .Build(); diff --git a/test/ParcelRegistry.Tests/AggregateTests/WhenReaddressingAddresses/GivenParcelExists.cs b/test/ParcelRegistry.Tests/AggregateTests/WhenReaddressingAddresses/GivenParcelExists.cs index d1b6e5f4..a37cead1 100644 --- a/test/ParcelRegistry.Tests/AggregateTests/WhenReaddressingAddresses/GivenParcelExists.cs +++ b/test/ParcelRegistry.Tests/AggregateTests/WhenReaddressingAddresses/GivenParcelExists.cs @@ -194,9 +194,12 @@ public void StateCheck() var parcelWasMigrated = new ParcelWasMigratedBuilder(Fixture) .WithStatus(ParcelStatus.Realized) .WithAddress(sourceAddressPersistentLocalId) + .WithAddress(sourceAddressPersistentLocalId) .WithAddress(otherAddressPersistentLocalId) .Build(); + parcelWasMigrated.AddressPersistentLocalIds.Count(x => x == sourceAddressPersistentLocalId).Should().Be(2); + var @event = new ParcelAddressesWereReaddressed( Fixture.Create(), Fixture.Create(), diff --git a/test/ParcelRegistry.Tests/AggregateTests/WhenRetiringParcel/GivenParcelExistsWithAttachedAddresses.cs b/test/ParcelRegistry.Tests/AggregateTests/WhenRetiringParcel/GivenParcelExistsWithAttachedAddresses.cs index ff81c104..bc22c4a5 100644 --- a/test/ParcelRegistry.Tests/AggregateTests/WhenRetiringParcel/GivenParcelExistsWithAttachedAddresses.cs +++ b/test/ParcelRegistry.Tests/AggregateTests/WhenRetiringParcel/GivenParcelExistsWithAttachedAddresses.cs @@ -1,4 +1,4 @@ -namespace ParcelRegistry.Tests.AggregateTests.WhenRetiringParcel +namespace ParcelRegistry.Tests.AggregateTests.WhenRetiringParcel { using AutoFixture; using Be.Vlaanderen.Basisregisters.AggregateSource; diff --git a/test/ParcelRegistry.Tests/AggregateTests/WhenRetiringParcel/GivenParcelIsRemoved.cs b/test/ParcelRegistry.Tests/AggregateTests/WhenRetiringParcel/GivenParcelIsRemoved.cs index 65eece86..60cc0f1f 100644 --- a/test/ParcelRegistry.Tests/AggregateTests/WhenRetiringParcel/GivenParcelIsRemoved.cs +++ b/test/ParcelRegistry.Tests/AggregateTests/WhenRetiringParcel/GivenParcelIsRemoved.cs @@ -1,19 +1,13 @@ namespace ParcelRegistry.Tests.AggregateTests.WhenRetiringParcel { - using System.Collections.Generic; - using Api.BackOffice.Abstractions.Extensions; using AutoFixture; using Be.Vlaanderen.Basisregisters.AggregateSource.Testing; - using Be.Vlaanderen.Basisregisters.GrAr.Provenance; using Builders; using Fixtures; using Parcel; - using Parcel.Commands; - using Parcel.Events; using Parcel.Exceptions; using Xunit; using Xunit.Abstractions; - using ParcelId = ParcelRegistry.Legacy.ParcelId; using ParcelStatus = Parcel.ParcelStatus; public class GivenParcelIsRemoved : ParcelRegistryTest