From 13710659e781eeb66bb201516ef92655595d931a Mon Sep 17 00:00:00 2001 From: jvandaal Date: Thu, 24 Oct 2024 15:26:21 +0200 Subject: [PATCH] refactor: propose all streetnames for municipality merger within a single transaction --- ...eStreetNameForMunicipalityMergerHandler.cs | 64 +++---- ...Controller-ProposeForMunicipalityMerger.cs | 1 - .../ProposeStreetNameForMunicipalityMerger.cs | 67 ------- ...ProposeStreetNamesForMunicipalityMerger.cs | 93 +++++++++ .../StreetNameCommandHandlerModule.cs | 20 +- ...reetNameForMunicipalityMergerExtensions.cs | 118 ++++++------ .../GivenMunicipality.cs | 181 +++++++++++------- ...roposeForMunicipalityMergerHandlerTests.cs | 31 +-- 8 files changed, 307 insertions(+), 268 deletions(-) delete mode 100644 src/StreetNameRegistry/Municipality/Commands/ProposeStreetNameForMunicipalityMerger.cs create mode 100644 src/StreetNameRegistry/Municipality/Commands/ProposeStreetNamesForMunicipalityMerger.cs diff --git a/src/StreetNameRegistry.Api.BackOffice.Handlers.Lambda/Handlers/ProposeStreetNameForMunicipalityMergerHandler.cs b/src/StreetNameRegistry.Api.BackOffice.Handlers.Lambda/Handlers/ProposeStreetNameForMunicipalityMergerHandler.cs index 1957fff20..711a87460 100644 --- a/src/StreetNameRegistry.Api.BackOffice.Handlers.Lambda/Handlers/ProposeStreetNameForMunicipalityMergerHandler.cs +++ b/src/StreetNameRegistry.Api.BackOffice.Handlers.Lambda/Handlers/ProposeStreetNameForMunicipalityMergerHandler.cs @@ -7,7 +7,6 @@ namespace StreetNameRegistry.Api.BackOffice.Handlers.Lambda.Handlers using Be.Vlaanderen.Basisregisters.Sqs.Lambda.Infrastructure; using Be.Vlaanderen.Basisregisters.Sqs.Responses; using Microsoft.Extensions.Configuration; - using Microsoft.Extensions.Logging; using Municipality; using Municipality.Commands; using Municipality.Exceptions; @@ -17,7 +16,6 @@ namespace StreetNameRegistry.Api.BackOffice.Handlers.Lambda.Handlers public sealed class ProposeStreetNameForMunicipalityMergerHandler : StreetNameLambdaHandler { private readonly BackOfficeContext _backOfficeContext; - private readonly ILogger _logger; public ProposeStreetNameForMunicipalityMergerHandler( IConfiguration configuration, @@ -25,8 +23,7 @@ public ProposeStreetNameForMunicipalityMergerHandler( ITicketing ticketing, IScopedIdempotentCommandHandler idempotentCommandHandler, BackOfficeContext backOfficeContext, - IMunicipalities municipalities, - ILoggerFactory loggerFactory + IMunicipalities municipalities ) : base( configuration, @@ -36,54 +33,46 @@ ILoggerFactory loggerFactory idempotentCommandHandler) { _backOfficeContext = backOfficeContext; - _logger = loggerFactory.CreateLogger(GetType()); } protected override async Task InnerHandle( ProposeStreetNamesForMunicipalityMergerLambdaRequest request, CancellationToken cancellationToken) { - var commands = await BuildCommands(request, cancellationToken); + var command = await BuildCommand(request, cancellationToken); - foreach (var command in commands) + try { - _logger.LogDebug($"Handling {command.GetType().FullName}"); - - try - { - await IdempotentCommandHandler.Dispatch( - command.CreateCommandId(), - command, - request.Metadata!, - cancellationToken: cancellationToken); - _logger.LogDebug($"Handled {command.GetType().FullName}"); - } - catch (IdempotencyException) - { - // Idempotent: Do Nothing return last etag - _logger.LogDebug($"Skipped due to idempotency {command.GetType().FullName}"); - } + await IdempotentCommandHandler.Dispatch( + command.CreateCommandId(), + command, + request.Metadata!, + cancellationToken: cancellationToken); + } + catch (IdempotencyException) + { + // Idempotent: do nothing return last etag } var etagResponses = new List(); - foreach (var command in commands) + foreach (var streetName in command.StreetNames) { await _backOfficeContext .AddIdempotentMunicipalityStreetNameIdRelation( - command.PersistentLocalId, + streetName.PersistentLocalId, request.MunicipalityId(), request.NisCode, cancellationToken); - var lastHash = await GetStreetNameHash(request.MunicipalityId(), command.PersistentLocalId, cancellationToken); - etagResponses.Add(new ETagResponse(string.Format(DetailUrlFormat, command.PersistentLocalId), lastHash)); + var lastHash = await GetStreetNameHash(request.MunicipalityId(), streetName.PersistentLocalId, cancellationToken); + etagResponses.Add(new ETagResponse(string.Format(DetailUrlFormat, streetName.PersistentLocalId), lastHash)); } return etagResponses; } - private async Task> BuildCommands( + private async Task BuildCommand( ProposeStreetNamesForMunicipalityMergerLambdaRequest request, CancellationToken cancellationToken) { @@ -96,7 +85,7 @@ private async Task> BuildCommands( new MunicipalityStreamId(new MunicipalityId(municipalityId)), cancellationToken)); } - return request.StreetNames + var streetNames = request.StreetNames .Select(streetName => { var desiredStatus = streetName.MergedStreetNames @@ -111,18 +100,21 @@ private async Task> BuildCommands( ? StreetNameStatus.Current : StreetNameStatus.Proposed; - return new ProposeStreetNameForMunicipalityMerger( - request.MunicipalityId(), + return new ProposeStreetNamesForMunicipalityMerger.StreetNameToPropose( desiredStatus, - new Names(new[] { new StreetNameName(streetName.StreetName, Language.Dutch) }), + new Names([new StreetNameName(streetName.StreetName, Language.Dutch)]), streetName.HomonymAddition is not null - ? new HomonymAdditions(new[] { new StreetNameHomonymAddition(streetName.HomonymAddition, Language.Dutch) }) - : null, + ? new HomonymAdditions([new StreetNameHomonymAddition(streetName.HomonymAddition, Language.Dutch)]) + : [], new PersistentLocalId(streetName.NewPersistentLocalId), - streetName.MergedStreetNames.Select(x => new PersistentLocalId(x.StreetNamePersistentLocalId)).ToList(), - request.Provenance); + streetName.MergedStreetNames.Select(x => new PersistentLocalId(x.StreetNamePersistentLocalId)).ToList()); }) .ToList(); + + return new ProposeStreetNamesForMunicipalityMerger( + request.MunicipalityId(), + streetNames, + request.Provenance); } protected override TicketError? InnerMapDomainException(DomainException exception, diff --git a/src/StreetNameRegistry.Api.BackOffice/StreetNameController-ProposeForMunicipalityMerger.cs b/src/StreetNameRegistry.Api.BackOffice/StreetNameController-ProposeForMunicipalityMerger.cs index 2c85fdd09..8e182cbb4 100644 --- a/src/StreetNameRegistry.Api.BackOffice/StreetNameController-ProposeForMunicipalityMerger.cs +++ b/src/StreetNameRegistry.Api.BackOffice/StreetNameController-ProposeForMunicipalityMerger.cs @@ -11,7 +11,6 @@ namespace StreetNameRegistry.Api.BackOffice using Abstractions.Validation; using Be.Vlaanderen.Basisregisters.Api.Exceptions; using Be.Vlaanderen.Basisregisters.Auth.AcmIdm; - using Be.Vlaanderen.Basisregisters.GrAr.Edit.Validators; using Be.Vlaanderen.Basisregisters.GrAr.Provenance; using Be.Vlaanderen.Basisregisters.Sqs.Exceptions; using Consumer; diff --git a/src/StreetNameRegistry/Municipality/Commands/ProposeStreetNameForMunicipalityMerger.cs b/src/StreetNameRegistry/Municipality/Commands/ProposeStreetNameForMunicipalityMerger.cs deleted file mode 100644 index c17fa826b..000000000 --- a/src/StreetNameRegistry/Municipality/Commands/ProposeStreetNameForMunicipalityMerger.cs +++ /dev/null @@ -1,67 +0,0 @@ -namespace StreetNameRegistry.Municipality.Commands -{ - using System; - using System.Collections.Generic; - using Be.Vlaanderen.Basisregisters.Generators.Guid; - using Be.Vlaanderen.Basisregisters.GrAr.Provenance; - using Be.Vlaanderen.Basisregisters.Utilities; - - public sealed class ProposeStreetNameForMunicipalityMerger : IHasCommandProvenance - { - private static readonly Guid Namespace = new Guid("eaf63006-10de-403f-a692-084707cc5ed4"); - - public MunicipalityId MunicipalityId { get; } - public StreetNameStatus DesiredStatus { get; } - public Names StreetNameNames { get; } - public HomonymAdditions HomonymAdditions { get; } - public PersistentLocalId PersistentLocalId { get; } - public List MergedStreetNamePersistentLocalIds { get; } - public Provenance Provenance { get; } - - public ProposeStreetNameForMunicipalityMerger( - MunicipalityId municipalityId, - StreetNameStatus desiredStatus, - Names streetNameNames, - HomonymAdditions? homonymAdditions, - PersistentLocalId persistentLocalId, - List mergedStreetNamePersistentLocalIds, - Provenance provenance) - { - MunicipalityId = municipalityId; - DesiredStatus = desiredStatus; - StreetNameNames = streetNameNames; - HomonymAdditions = homonymAdditions ?? []; - PersistentLocalId = persistentLocalId; - MergedStreetNamePersistentLocalIds = mergedStreetNamePersistentLocalIds; - Provenance = provenance; - } - - public Guid CreateCommandId() - => Deterministic.Create(Namespace, $"ProposeStreetNameForMunicipalityMerger-{ToString()}"); - - public override string? ToString() - => ToStringBuilder.ToString(IdentityFields()); - - private IEnumerable IdentityFields() - { - yield return MunicipalityId; - yield return PersistentLocalId; - yield return DesiredStatus; - - foreach (var streetNameName in StreetNameNames) - { - yield return streetNameName; - } - - foreach (var homonymAddition in HomonymAdditions) - { - yield return homonymAddition; - } - - foreach (var mergedStreetNamePersistentLocalId in MergedStreetNamePersistentLocalIds) - { - yield return mergedStreetNamePersistentLocalId; - } - } - } -} diff --git a/src/StreetNameRegistry/Municipality/Commands/ProposeStreetNamesForMunicipalityMerger.cs b/src/StreetNameRegistry/Municipality/Commands/ProposeStreetNamesForMunicipalityMerger.cs new file mode 100644 index 000000000..521382d87 --- /dev/null +++ b/src/StreetNameRegistry/Municipality/Commands/ProposeStreetNamesForMunicipalityMerger.cs @@ -0,0 +1,93 @@ +namespace StreetNameRegistry.Municipality.Commands +{ + using System; + using System.Collections.Generic; + using System.Linq; + using Be.Vlaanderen.Basisregisters.Generators.Guid; + using Be.Vlaanderen.Basisregisters.GrAr.Provenance; + using Be.Vlaanderen.Basisregisters.Utilities; + + public sealed class ProposeStreetNamesForMunicipalityMerger : IHasCommandProvenance + { + private static readonly Guid Namespace = new Guid("eaf63006-10de-403f-a692-084707cc5ed4"); + + public MunicipalityId MunicipalityId { get; } + public List StreetNames { get; } + public Provenance Provenance { get; } + + public ProposeStreetNamesForMunicipalityMerger( + MunicipalityId municipalityId, + List streetNames, + Provenance provenance) + { + MunicipalityId = municipalityId; + StreetNames = streetNames; + Provenance = provenance; + } + + public Guid CreateCommandId() + => Deterministic.Create(Namespace, $"ProposeStreetNamesForMunicipalityMerger-{ToString()}"); + + public override string? ToString() + => ToStringBuilder.ToString(IdentityFields()); + + private IEnumerable IdentityFields() + { + yield return MunicipalityId; + + foreach (var streetNameName in StreetNames.SelectMany(x => x.IdentityFields())) + { + yield return streetNameName; + } + + foreach (var field in Provenance.GetIdentityFields()) + { + yield return field; + } + } + + public sealed class StreetNameToPropose + { + public StreetNameStatus DesiredStatus { get; } + public Names StreetNameNames { get; } + public HomonymAdditions HomonymAdditions { get; } + public PersistentLocalId PersistentLocalId { get; } + public List MergedStreetNamePersistentLocalIds { get; } + + public StreetNameToPropose( + StreetNameStatus desiredStatus, + Names streetNameNames, + HomonymAdditions homonymAdditions, + PersistentLocalId persistentLocalId, + List mergedStreetNamePersistentLocalIds) + { + DesiredStatus = desiredStatus; + StreetNameNames = streetNameNames; + HomonymAdditions = homonymAdditions; + PersistentLocalId = persistentLocalId; + MergedStreetNamePersistentLocalIds = mergedStreetNamePersistentLocalIds; + } + + internal IEnumerable IdentityFields() + { + yield return PersistentLocalId; + yield return DesiredStatus; + + foreach (var streetNameName in StreetNameNames) + { + yield return streetNameName; + } + + foreach (var homonymAddition in HomonymAdditions) + { + yield return homonymAddition; + } + + foreach (var mergedStreetNamePersistentLocalId in MergedStreetNamePersistentLocalIds) + { + yield return mergedStreetNamePersistentLocalId; + } + } + } + } +} diff --git a/src/StreetNameRegistry/Municipality/StreetNameCommandHandlerModule.cs b/src/StreetNameRegistry/Municipality/StreetNameCommandHandlerModule.cs index 0c07d01e6..31aa6ea7a 100644 --- a/src/StreetNameRegistry/Municipality/StreetNameCommandHandlerModule.cs +++ b/src/StreetNameRegistry/Municipality/StreetNameCommandHandlerModule.cs @@ -33,19 +33,23 @@ public StreetNameCommandHandlerModule( municipality.ProposeStreetName(message.Command.StreetNameNames, message.Command.PersistentLocalId); }); - For() + For() .AddSqlStreamStore(getStreamStore, getUnitOfWork, eventMapping, eventSerializer, getSnapshotStore) - .AddEventHash(getUnitOfWork) + .AddEventHash(getUnitOfWork) .AddProvenance(getUnitOfWork, provenanceFactory) .Handle(async (message, ct) => { var municipality = await getMunicipalities().GetAsync(new MunicipalityStreamId(message.Command.MunicipalityId), ct); - municipality.ProposeStreetNameForMunicipalityMerger( - message.Command.DesiredStatus, - message.Command.StreetNameNames, - message.Command.HomonymAdditions, - message.Command.PersistentLocalId, - message.Command.MergedStreetNamePersistentLocalIds); + + foreach (var streetName in message.Command.StreetNames) + { + municipality.ProposeStreetNameForMunicipalityMerger( + streetName.DesiredStatus, + streetName.StreetNameNames, + streetName.HomonymAdditions, + streetName.PersistentLocalId, + streetName.MergedStreetNamePersistentLocalIds); + } }); For() diff --git a/test/StreetNameRegistry.Tests/AggregateTests/Extensions/ProposeStreetNameForMunicipalityMergerExtensions.cs b/test/StreetNameRegistry.Tests/AggregateTests/Extensions/ProposeStreetNameForMunicipalityMergerExtensions.cs index c08947c10..5c9c1aa1d 100644 --- a/test/StreetNameRegistry.Tests/AggregateTests/Extensions/ProposeStreetNameForMunicipalityMergerExtensions.cs +++ b/test/StreetNameRegistry.Tests/AggregateTests/Extensions/ProposeStreetNameForMunicipalityMergerExtensions.cs @@ -4,98 +4,92 @@ using global::AutoFixture; using Municipality; using Municipality.Commands; + using StreetNameToPropose = Municipality.Commands.ProposeStreetNamesForMunicipalityMerger.StreetNameToPropose; public static class ProposeStreetNameForMunicipalityMergerExtensions { - public static ProposeStreetNameForMunicipalityMerger WithMunicipalityId( - this ProposeStreetNameForMunicipalityMerger command, MunicipalityId municipalityId) + public static ProposeStreetNamesForMunicipalityMerger WithMunicipalityId( + this ProposeStreetNamesForMunicipalityMerger command, MunicipalityId municipalityId) { - return new ProposeStreetNameForMunicipalityMerger( + return new ProposeStreetNamesForMunicipalityMerger( municipalityId, - command.DesiredStatus, - command.StreetNameNames, - command.HomonymAdditions, - command.PersistentLocalId, - command.MergedStreetNamePersistentLocalIds, + command.StreetNames, command.Provenance); } - public static ProposeStreetNameForMunicipalityMerger WithRandomStreetName( - this ProposeStreetNameForMunicipalityMerger command, Fixture fixture) + public static ProposeStreetNamesForMunicipalityMerger WithStreetNames( + this ProposeStreetNamesForMunicipalityMerger command, List streetNames) { - return new ProposeStreetNameForMunicipalityMerger( + return new ProposeStreetNamesForMunicipalityMerger( command.MunicipalityId, - command.DesiredStatus, - new Names(new List { fixture.Create() }), - command.HomonymAdditions, - command.PersistentLocalId, - command.MergedStreetNamePersistentLocalIds, + streetNames, command.Provenance); } - public static ProposeStreetNameForMunicipalityMerger WithStreetNameNames( - this ProposeStreetNameForMunicipalityMerger command, Names names) + public static StreetNameToPropose WithRandomStreetName( + this StreetNameToPropose streetName, Fixture fixture) { - return new ProposeStreetNameForMunicipalityMerger( - command.MunicipalityId, - command.DesiredStatus, + return new StreetNameToPropose( + streetName.DesiredStatus, + new Names(new List { fixture.Create() }), + streetName.HomonymAdditions, + streetName.PersistentLocalId, + streetName.MergedStreetNamePersistentLocalIds); + } + + public static StreetNameToPropose WithStreetNameNames( + this StreetNameToPropose streetName, Names names) + { + return new StreetNameToPropose( + streetName.DesiredStatus, names, - command.HomonymAdditions, - command.PersistentLocalId, - command.MergedStreetNamePersistentLocalIds, - command.Provenance); + streetName.HomonymAdditions, + streetName.PersistentLocalId, + streetName.MergedStreetNamePersistentLocalIds); } - public static ProposeStreetNameForMunicipalityMerger WithHomonymAdditions( - this ProposeStreetNameForMunicipalityMerger command, HomonymAdditions homonymAdditions) + public static StreetNameToPropose WithHomonymAdditions( + this StreetNameToPropose streetName, HomonymAdditions homonymAdditions) { - return new ProposeStreetNameForMunicipalityMerger( - command.MunicipalityId, - command.DesiredStatus, - command.StreetNameNames, + return new StreetNameToPropose( + streetName.DesiredStatus, + streetName.StreetNameNames, homonymAdditions, - command.PersistentLocalId, - command.MergedStreetNamePersistentLocalIds, - command.Provenance); + streetName.PersistentLocalId, + streetName.MergedStreetNamePersistentLocalIds); } - public static ProposeStreetNameForMunicipalityMerger WithPersistentLocalId( - this ProposeStreetNameForMunicipalityMerger command, PersistentLocalId persistentLocalId) + public static StreetNameToPropose WithPersistentLocalId( + this StreetNameToPropose streetName, PersistentLocalId persistentLocalId) { - return new ProposeStreetNameForMunicipalityMerger( - command.MunicipalityId, - command.DesiredStatus, - command.StreetNameNames, - command.HomonymAdditions, + return new StreetNameToPropose( + streetName.DesiredStatus, + streetName.StreetNameNames, + streetName.HomonymAdditions, persistentLocalId, - command.MergedStreetNamePersistentLocalIds, - command.Provenance); + streetName.MergedStreetNamePersistentLocalIds); } - public static ProposeStreetNameForMunicipalityMerger WithMergedStreetNamePersistentIds( - this ProposeStreetNameForMunicipalityMerger command, List mergedStreetNamePersistentIds) + public static StreetNameToPropose WithMergedStreetNamePersistentIds( + this StreetNameToPropose streetName, List mergedStreetNamePersistentIds) { - return new ProposeStreetNameForMunicipalityMerger( - command.MunicipalityId, - command.DesiredStatus, - command.StreetNameNames, - command.HomonymAdditions, - command.PersistentLocalId, - mergedStreetNamePersistentIds, - command.Provenance); + return new StreetNameToPropose( + streetName.DesiredStatus, + streetName.StreetNameNames, + streetName.HomonymAdditions, + streetName.PersistentLocalId, + mergedStreetNamePersistentIds); } - public static ProposeStreetNameForMunicipalityMerger WithDesiredStatus( - this ProposeStreetNameForMunicipalityMerger command, StreetNameStatus desiredStatus) + public static StreetNameToPropose WithDesiredStatus( + this StreetNameToPropose streetName, StreetNameStatus desiredStatus) { - return new ProposeStreetNameForMunicipalityMerger( - command.MunicipalityId, + return new StreetNameToPropose( desiredStatus, - command.StreetNameNames, - command.HomonymAdditions, - command.PersistentLocalId, - command.MergedStreetNamePersistentLocalIds, - command.Provenance); + streetName.StreetNameNames, + streetName.HomonymAdditions, + streetName.PersistentLocalId, + streetName.MergedStreetNamePersistentLocalIds); } } } diff --git a/test/StreetNameRegistry.Tests/AggregateTests/WhenProposingStreetNameForMunicipalityMerger/GivenMunicipality.cs b/test/StreetNameRegistry.Tests/AggregateTests/WhenProposingStreetNameForMunicipalityMerger/GivenMunicipality.cs index 735ba602d..74b08819c 100644 --- a/test/StreetNameRegistry.Tests/AggregateTests/WhenProposingStreetNameForMunicipalityMerger/GivenMunicipality.cs +++ b/test/StreetNameRegistry.Tests/AggregateTests/WhenProposingStreetNameForMunicipalityMerger/GivenMunicipality.cs @@ -18,6 +18,7 @@ namespace StreetNameRegistry.Tests.AggregateTests.WhenProposingStreetNameForMuni using Testing; using Xunit; using Xunit.Abstractions; + using StreetNameToPropose = Municipality.Commands.ProposeStreetNamesForMunicipalityMerger.StreetNameToPropose; public sealed class GivenMunicipality : StreetNameRegistryTest { @@ -41,10 +42,12 @@ public void ThenStreetNameWasProposedForMunicipalityMerger() Fixture.Register(() => Language.Dutch); Fixture.Register(() => Taal.NL); - var command = Fixture.Create() + var streetName = Fixture.Create() .WithDesiredStatus(StreetNameStatus.Current) - .WithStreetNameNames([new(Fixture.Create(), Language.Dutch)]) - .WithHomonymAdditions([new (new string(Fixture.CreateMany(5).ToArray()), Language.Dutch)]); + .WithStreetNameNames([new StreetNameName(Fixture.Create(), Language.Dutch)]) + .WithHomonymAdditions([new StreetNameHomonymAddition(new string(Fixture.CreateMany(5).ToArray()), Language.Dutch)]); + var command = Fixture.Create() + .WithStreetNames([streetName]); //Act, assert Assert(new Scenario() @@ -56,11 +59,11 @@ public void ThenStreetNameWasProposedForMunicipalityMerger() new StreetNameWasProposedForMunicipalityMerger( _municipalityId, Fixture.Create(), - command.DesiredStatus, - command.StreetNameNames, - command.HomonymAdditions, - command.PersistentLocalId, - command.MergedStreetNamePersistentLocalIds)))); + streetName.DesiredStatus, + streetName.StreetNameNames, + streetName.HomonymAdditions, + streetName.PersistentLocalId, + streetName.MergedStreetNamePersistentLocalIds)))); } [Theory] @@ -71,9 +74,12 @@ public void WithExistingStreetNameName_ThenThrowsStreetNameNameAlreadyExistsExce var streetNameWasProposed = Fixture.Create() .WithNames(new Names(new List { new(name, Language.Dutch) })); - var command = Fixture.Create() - .WithDesiredStatus(StreetNameStatus.Current) - .WithStreetNameNames(new Names(new List { new(newName, Language.Dutch) })); + var command = Fixture.Create() + .WithStreetNames([ + Fixture.Create() + .WithDesiredStatus(StreetNameStatus.Current) + .WithStreetNameNames(new Names(new List { new(newName, Language.Dutch) })) + ]); Assert(new Scenario() .Given(_streamId, @@ -89,8 +95,10 @@ public void WithExistingPersistentLocalId_ThenThrowsStreetNamePersistentLocalIdA { var streetNameWasProposed = Fixture.Create(); - var command = Fixture.Create() - .WithPersistentLocalId(new PersistentLocalId(streetNameWasProposed.PersistentLocalId)); + var command = Fixture.Create() + .WithStreetNames([ + Fixture.Create().WithPersistentLocalId(new PersistentLocalId(streetNameWasProposed.PersistentLocalId)) + ]); Assert(new Scenario() .Given(_streamId, @@ -109,8 +117,9 @@ public void WithExistingRetiredStreetName_ThenStreetNameWasProposed() Fixture.Register(() => Language.Dutch); Fixture.Register(() => Taal.NL); - var command = Fixture.Create() - .WithDesiredStatus(StreetNameStatus.Current); + var streetName = Fixture.Create().WithDesiredStatus(StreetNameStatus.Current); + var command = Fixture.Create() + .WithStreetNames([streetName]); Assert(new Scenario() .Given(_streamId, @@ -122,11 +131,11 @@ public void WithExistingRetiredStreetName_ThenStreetNameWasProposed() new StreetNameWasProposedForMunicipalityMerger( _municipalityId, Fixture.Create(), - command.DesiredStatus, - command.StreetNameNames, - command.HomonymAdditions, - command.PersistentLocalId, - command.MergedStreetNamePersistentLocalIds)))); + streetName.DesiredStatus, + streetName.StreetNameNames, + streetName.HomonymAdditions, + streetName.PersistentLocalId, + streetName.MergedStreetNamePersistentLocalIds)))); } [Fact] @@ -140,8 +149,9 @@ public void WithExistingRejectedStreetName_ThenStreetNameWasProposed() var streetNameWasMigrated = Fixture.Create(); - var command = Fixture.Create() - .WithDesiredStatus(StreetNameStatus.Current); + var streetName = Fixture.Create().WithDesiredStatus(StreetNameStatus.Current); + var command = Fixture.Create() + .WithStreetNames([streetName]); Assert(new Scenario() .Given(_streamId, @@ -153,11 +163,11 @@ public void WithExistingRejectedStreetName_ThenStreetNameWasProposed() new StreetNameWasProposedForMunicipalityMerger( _municipalityId, Fixture.Create(), - command.DesiredStatus, - command.StreetNameNames, - command.HomonymAdditions, - command.PersistentLocalId, - command.MergedStreetNamePersistentLocalIds)))); + streetName.DesiredStatus, + streetName.StreetNameNames, + streetName.HomonymAdditions, + streetName.PersistentLocalId, + streetName.MergedStreetNamePersistentLocalIds)))); } [Fact] @@ -173,8 +183,9 @@ public void WithExistingRemovedStreetName_ThenStreetNameWasProposed() .WithIsRemoved() .Build(); - var command = Fixture.Create() - .WithDesiredStatus(StreetNameStatus.Current); + var streetName = Fixture.Create().WithDesiredStatus(StreetNameStatus.Current); + var command = Fixture.Create() + .WithStreetNames([streetName]); Assert(new Scenario() .Given(_streamId, @@ -186,11 +197,11 @@ public void WithExistingRemovedStreetName_ThenStreetNameWasProposed() new StreetNameWasProposedForMunicipalityMerger( _municipalityId, Fixture.Create(), - command.DesiredStatus, - command.StreetNameNames, - command.HomonymAdditions, - command.PersistentLocalId, - command.MergedStreetNamePersistentLocalIds)))); + streetName.DesiredStatus, + streetName.StreetNameNames, + streetName.HomonymAdditions, + streetName.PersistentLocalId, + streetName.MergedStreetNamePersistentLocalIds)))); } [Fact] @@ -200,9 +211,12 @@ public void WithOneExistingStreetNameAndOneNew_ThenThrowsStreetNameNameAlreadyEx var newStreetNameName = Fixture.Create(); Fixture.Register(() => new Names { existingStreetNameName }); - var command = Fixture.Create() - .WithStreetNameNames([existingStreetNameName, newStreetNameName]) - .WithDesiredStatus(StreetNameStatus.Current); + var command = Fixture.Create() + .WithStreetNames([ + Fixture.Create() + .WithStreetNameNames([existingStreetNameName, newStreetNameName]) + .WithDesiredStatus(StreetNameStatus.Current) + ]); Assert(new Scenario() .Given(_streamId, @@ -224,9 +238,11 @@ public void WithNoConflictingStreetNames_ThenStreetNameWasProposed() var streetNameWasProposed = Fixture.Create(); - var command = Fixture.Create() + var streetName = Fixture.Create() .WithStreetNameNames([newStreetNameName]) .WithDesiredStatus(StreetNameStatus.Current); + var command = Fixture.Create() + .WithStreetNames([streetName]); Assert(new Scenario() .Given(_streamId, @@ -238,17 +254,17 @@ public void WithNoConflictingStreetNames_ThenStreetNameWasProposed() new StreetNameWasProposedForMunicipalityMerger( _municipalityId, Fixture.Create(), - command.DesiredStatus, - command.StreetNameNames, - command.HomonymAdditions, - command.PersistentLocalId, - command.MergedStreetNamePersistentLocalIds)))); + streetName.DesiredStatus, + streetName.StreetNameNames, + streetName.HomonymAdditions, + streetName.PersistentLocalId, + streetName.MergedStreetNamePersistentLocalIds)))); } [Fact] public void WithMunicipalityRetired_ThenThrowsMunicipalityHasInvalidStatusException() { - var command = Fixture.Create(); + var command = Fixture.Create(); Assert(new Scenario() .Given(_streamId, @@ -270,9 +286,12 @@ public void WithOfficialLanguageDutchAndProposedLanguageIsFrench_ThenThrowsStree new(Fixture.Create(), Language.French) }; - var command = Fixture.Create() - .WithStreetNameNames(names) - .WithDesiredStatus(StreetNameStatus.Current); + var command = Fixture.Create() + .WithStreetNames([ + Fixture.Create() + .WithStreetNameNames(names) + .WithDesiredStatus(StreetNameStatus.Current) + ]); Assert(new Scenario() .Given(_streamId, @@ -295,9 +314,12 @@ public void WithFacilityLanguageFrenchAndProposedLanguageIsDutch_ThenThrowsStree new(Fixture.Create(), Language.Dutch) }; - var command = Fixture.Create() - .WithStreetNameNames(names) - .WithDesiredStatus(StreetNameStatus.Current); + var command = Fixture.Create() + .WithStreetNames([ + Fixture.Create() + .WithStreetNameNames(names) + .WithDesiredStatus(StreetNameStatus.Current) + ]); Assert(new Scenario() .Given(_streamId, @@ -323,9 +345,12 @@ public void new(Fixture.Create(), Language.French) }; - var command = Fixture.Create() - .WithDesiredStatus(StreetNameStatus.Current) - .WithStreetNameNames(names); + var command = Fixture.Create() + .WithStreetNames([ + Fixture.Create() + .WithDesiredStatus(StreetNameStatus.Current) + .WithStreetNameNames(names) + ]); Assert(new Scenario() .Given(_streamId, @@ -348,8 +373,8 @@ public void WithExistingStreetNameAndNoHomonymAdditions_ThenThrowsStreetNameName .WithStatus(StreetNameStatus.Current) .Build(); - var command = Fixture.Create() - .WithDesiredStatus(StreetNameStatus.Current); + var command = Fixture.Create() + .WithStreetNames([Fixture.Create().WithDesiredStatus(StreetNameStatus.Current)]); Assert(new Scenario() .Given(_streamId, @@ -373,9 +398,12 @@ public void WithExistingStreetNameAndExistingHomonymAdditions_ThenStreetNameWasP .WithHomonymAdditions([new("test", Language.Dutch)]) .Build(); - var command = Fixture.Create() - .WithDesiredStatus(StreetNameStatus.Current) - .WithHomonymAdditions([new("test", Language.Dutch)]); + var command = Fixture.Create() + .WithStreetNames([ + Fixture.Create() + .WithDesiredStatus(StreetNameStatus.Current) + .WithHomonymAdditions([new("test", Language.Dutch)]) + ]); Assert(new Scenario() .Given(_streamId, @@ -399,8 +427,9 @@ public void WithExistingStreetNameAndHomonymAdditions_ThenStreetNameWasProposed( .WithHomonymAdditions([new("test", Language.Dutch)]) .Build(); - var command = Fixture.Create() - .WithDesiredStatus(StreetNameStatus.Current); + var streetName = Fixture.Create().WithDesiredStatus(StreetNameStatus.Current); + var command = Fixture.Create() + .WithStreetNames([streetName]); Assert(new Scenario() .Given(_streamId, @@ -412,11 +441,11 @@ public void WithExistingStreetNameAndHomonymAdditions_ThenStreetNameWasProposed( new StreetNameWasProposedForMunicipalityMerger( _municipalityId, Fixture.Create(), - command.DesiredStatus, - command.StreetNameNames, - command.HomonymAdditions, - command.PersistentLocalId, - command.MergedStreetNamePersistentLocalIds)))); + streetName.DesiredStatus, + streetName.StreetNameNames, + streetName.HomonymAdditions, + streetName.PersistentLocalId, + streetName.MergedStreetNamePersistentLocalIds)))); } [Fact] @@ -431,10 +460,13 @@ public void WithEmptyMergedStreetNamePersistentLocalIds_ThenThrowsMergedStreetNa .WithStatus(StreetNameStatus.Current) .Build(); - var command = Fixture.Create() - .WithDesiredStatus(StreetNameStatus.Current) - .WithRandomStreetName(Fixture) - .WithMergedStreetNamePersistentIds([]); + var command = Fixture.Create() + .WithStreetNames([ + Fixture.Create() + .WithDesiredStatus(StreetNameStatus.Current) + .WithRandomStreetName(Fixture) + .WithMergedStreetNamePersistentIds([]) + ]); Assert(new Scenario() .Given(_streamId, @@ -457,10 +489,13 @@ public void WithDuplicateMergedStreetNamePersistentLocalIds_ThenThrowsMergedStre .WithStatus(StreetNameStatus.Current) .Build(); - var command = Fixture.Create() - .WithDesiredStatus(StreetNameStatus.Current) - .WithRandomStreetName(Fixture) - .WithMergedStreetNamePersistentIds([new(5), new(5)]); + var command = Fixture.Create() + .WithStreetNames([ + Fixture.Create() + .WithDesiredStatus(StreetNameStatus.Current) + .WithRandomStreetName(Fixture) + .WithMergedStreetNamePersistentIds([new(5), new(5)]) + ]); Assert(new Scenario() .Given(_streamId, diff --git a/test/StreetNameRegistry.Tests/BackOffice/Lambda/WhenProposingStreetNameForMunicipalityMerger/SqsStreetNameProposeForMunicipalityMergerHandlerTests.cs b/test/StreetNameRegistry.Tests/BackOffice/Lambda/WhenProposingStreetNameForMunicipalityMerger/SqsStreetNameProposeForMunicipalityMergerHandlerTests.cs index 6df824cfc..6719bbf22 100644 --- a/test/StreetNameRegistry.Tests/BackOffice/Lambda/WhenProposingStreetNameForMunicipalityMerger/SqsStreetNameProposeForMunicipalityMergerHandlerTests.cs +++ b/test/StreetNameRegistry.Tests/BackOffice/Lambda/WhenProposingStreetNameForMunicipalityMerger/SqsStreetNameProposeForMunicipalityMergerHandlerTests.cs @@ -13,7 +13,6 @@ namespace StreetNameRegistry.Tests.BackOffice.Lambda.WhenProposingStreetNameForM using FluentAssertions; using global::AutoFixture; using Microsoft.Extensions.Configuration; - using Microsoft.Extensions.Logging.Abstractions; using Moq; using Municipality; using Municipality.Exceptions; @@ -72,8 +71,7 @@ public async Task ThenTheStreetNameIsProposed() ticketing.Object, new FakeScopedIdemponentCommandHandler(() => new IdempotentCommandHandler(Container.Resolve(), _idempotencyContext)), _backOfficeContext, - Container.Resolve(), - new NullLoggerFactory()); + Container.Resolve()); //Act await handler.Handle(new ProposeStreetNamesForMunicipalityMergerLambdaRequest(newMunicipalityId, @@ -125,8 +123,7 @@ public async Task WhenStreetNameNameAlreadyExistsException_ThenTicketingErrorIsE ticketing.Object, MockExceptionScopedIdempotentCommandHandler(() => new StreetNameNameAlreadyExistsException(streetname)).Object, _backOfficeContext, - Mock.Of(), - new NullLoggerFactory()); + Mock.Of()); await sut.Handle(new ProposeStreetNamesForMunicipalityMergerLambdaRequest(newMunicipalityId, new ProposeStreetNamesForMunicipalityMergerSqsRequest @@ -173,8 +170,7 @@ public async Task WhenMunicipalityHasInvalidStatusException_ThenTicketingErrorIs ticketing.Object, MockExceptionScopedIdempotentCommandHandler().Object, _backOfficeContext, - Mock.Of(), - new NullLoggerFactory()); + Mock.Of()); await sut.Handle(new ProposeStreetNamesForMunicipalityMergerLambdaRequest(newMunicipalityId, new ProposeStreetNamesForMunicipalityMergerSqsRequest @@ -220,8 +216,7 @@ public async Task WhenStreetNameNameLanguageIsNotSupportedException_ThenTicketin ticketing.Object, MockExceptionScopedIdempotentCommandHandler().Object, _backOfficeContext, - Mock.Of(), - new NullLoggerFactory()); + Mock.Of()); // Act var municipalityId = Guid.NewGuid().ToString(); @@ -270,8 +265,7 @@ public async Task WhenStreetNameIsMissingALanguageException_ThenTicketingErrorIs ticketing.Object, MockExceptionScopedIdempotentCommandHandler().Object, _backOfficeContext, - Mock.Of(), - new NullLoggerFactory()); + Mock.Of()); // Act await sut.Handle(new ProposeStreetNamesForMunicipalityMergerLambdaRequest(newMunicipalityId, @@ -320,8 +314,7 @@ public async Task WhenMergedStreetNamePersistentLocalIdsAreMissingException_Then ticketing.Object, MockExceptionScopedIdempotentCommandHandler().Object, _backOfficeContext, - Mock.Of(), - new NullLoggerFactory()); + Mock.Of()); // Act await sut.Handle(new ProposeStreetNamesForMunicipalityMergerLambdaRequest(newMunicipalityId, @@ -370,8 +363,7 @@ public async Task WhenMergedStreetNamePersistentLocalIdsAreNotUniqueException_Th ticketing.Object, MockExceptionScopedIdempotentCommandHandler().Object, _backOfficeContext, - Mock.Of(), - new NullLoggerFactory()); + Mock.Of()); // Act await sut.Handle(new ProposeStreetNamesForMunicipalityMergerLambdaRequest(newMunicipalityId, @@ -420,8 +412,7 @@ public async Task WhenStreetNameHasInvalidDesiredStatusException_ThenTicketingEr ticketing.Object, MockExceptionScopedIdempotentCommandHandler().Object, _backOfficeContext, - Mock.Of(), - new NullLoggerFactory()); + Mock.Of()); // Act await sut.Handle(new ProposeStreetNamesForMunicipalityMergerLambdaRequest(newMunicipalityId, @@ -491,8 +482,7 @@ public async Task WhenIdempotencyException_ThenTicketingCompleteIsExpected() ticketing.Object, MockExceptionScopedIdempotentCommandHandler(() => new IdempotencyException(string.Empty)).Object, _backOfficeContext, - municipalities, - new NullLoggerFactory()); + municipalities); // Act await sut.Handle(new ProposeStreetNamesForMunicipalityMergerLambdaRequest(newMunicipalityId, @@ -558,8 +548,7 @@ public async Task GivenRetryingRequest_ThenTicketingCompleteIsExpected() ticketing.Object, new FakeScopedIdemponentCommandHandler(() => new IdempotentCommandHandler(Container.Resolve(), _idempotencyContext)), _backOfficeContext, - municipalities, - new NullLoggerFactory()); + municipalities); var request = new ProposeStreetNamesForMunicipalityMergerLambdaRequest(newMunicipalityId, new ProposeStreetNamesForMunicipalityMergerSqsRequest