From bef70f9144aa29deb6bb47af13ac07bbc9a9765d Mon Sep 17 00:00:00 2001 From: Arne Dumarey Date: Fri, 25 Oct 2024 10:43:54 +0200 Subject: [PATCH] fix: correct streetname when language is not present --- .../Municipality/MunicipalityStreetName.cs | 7 +++- .../GivenMunicipality.cs | 36 +++++++++++++++++++ .../GivenMunicipality.cs | 36 +++++++++++++++++++ 3 files changed, 78 insertions(+), 1 deletion(-) diff --git a/src/StreetNameRegistry/Municipality/MunicipalityStreetName.cs b/src/StreetNameRegistry/Municipality/MunicipalityStreetName.cs index fc23ea9a3..bab1b96b6 100644 --- a/src/StreetNameRegistry/Municipality/MunicipalityStreetName.cs +++ b/src/StreetNameRegistry/Municipality/MunicipalityStreetName.cs @@ -107,7 +107,12 @@ public void CorrectNames(Names namesToCorrect, Action x.Language == correctedName.Language); + var originalName = Names.SingleOrDefault(x => x.Language == correctedName.Language); + + // This should never happen in the normal flow, but with CRAB migration there are streetnames without names in supported every language + if(originalName is null) + throw new StreetNameNameCorrectionExceededCharacterChangeLimitException(correctedName.Name); + var changeDifference = LevenshteinDistanceCalculator.CalculatePercentage(correctedName.Name, originalName.Name); if (changeDifference > CorrectionChangeLimitPercentage) diff --git a/test/StreetNameRegistry.Tests/AggregateTests/WhenChangingStreetNameName/GivenMunicipality.cs b/test/StreetNameRegistry.Tests/AggregateTests/WhenChangingStreetNameName/GivenMunicipality.cs index d36fd6514..0d98a2f6d 100644 --- a/test/StreetNameRegistry.Tests/AggregateTests/WhenChangingStreetNameName/GivenMunicipality.cs +++ b/test/StreetNameRegistry.Tests/AggregateTests/WhenChangingStreetNameName/GivenMunicipality.cs @@ -219,6 +219,42 @@ public void WithNoChanges_ThenNothing() .ThenNone()); } + [Fact] + public void WithOneAdded_ThenOnlyOneStreetNameNameWasChanged() + { + var command = Fixture.Create() + .WithStreetNameNames(new Names()) + //.WithStreetNameName(new StreetNameName("Kapelstraat", Language.Dutch)) + .WithStreetNameName(new StreetNameName("Rue de la Chapelle", Language.French)); + + var dutchLanguageWasAdded = new MunicipalityOfficialLanguageWasAddedBuilder(Fixture) + .Build(); + + var frenchLanguageWasAdded = new MunicipalityOfficialLanguageWasAddedBuilder(Fixture) + .WithLanguage(Language.French) + .Build(); + + var streetNameWasProposedV2 = new StreetNameWasProposedV2Builder(Fixture) + .WithNisCode(new NisCode("abc")) + .WithNames(new Names(new[] + { + new StreetNameName("Kapelstraat", Language.Dutch), + })) + .Build(); + + // Act, assert + Assert(new Scenario() + .Given(_streamId, + Fixture.Create(), + Fixture.Create(), + dutchLanguageWasAdded, + frenchLanguageWasAdded, + streetNameWasProposedV2) + .When(command) + .Then(new Fact(_streamId, new StreetNameNamesWereChanged( + _municipalityId, command.PersistentLocalId, new Names(new[] { new StreetNameName("Rue de la Chapelle", Language.French) }))))); + } + [Fact] public void WithOneChange_ThenOnlyOneStreetNameNameWasChanged() { diff --git a/test/StreetNameRegistry.Tests/AggregateTests/WhenCorrectingStreetNameName/GivenMunicipality.cs b/test/StreetNameRegistry.Tests/AggregateTests/WhenCorrectingStreetNameName/GivenMunicipality.cs index fe9084b41..d6273d519 100644 --- a/test/StreetNameRegistry.Tests/AggregateTests/WhenCorrectingStreetNameName/GivenMunicipality.cs +++ b/test/StreetNameRegistry.Tests/AggregateTests/WhenCorrectingStreetNameName/GivenMunicipality.cs @@ -329,6 +329,42 @@ public void WithInvalidLanguage_ThenThrowsStreetNameNameLanguageNotSupportedExce $"The language '{Language.Dutch}' is not an official or facility language of municipality '{_municipalityId}'."))); } + [Fact] + public void WithFacilityLanguageNotInProposedStreetName_ThenThrowsStreetNameNameLanguageNotSupportedException() + { + var invalidStreetName = new StreetNameName("Rue de la Chapelle", Language.French); + var streetNameNames = new Names(new[] + { + new StreetNameName("Kapelstraat", Language.Dutch), + invalidStreetName + }); + + var command = Fixture.Create() + .WithStreetNameNames(streetNameNames); + + var languageWasAdded = new MunicipalityOfficialLanguageWasAddedBuilder(Fixture) + .WithLanguage(Language.Dutch) + .Build(); + + var facilityLanguageWasAdded = Fixture.Create() + .WithLanguage(Language.French) + .ToEvent(); + + var streetNameWasProposedV2 = Fixture.Create() + .WithNames(new Names([new StreetNameName("Kapestraat", Language.Dutch)])); + + // Act, assert + Assert(new Scenario() + .Given(_streamId, + Fixture.Create(), + Fixture.Create(), + languageWasAdded, + facilityLanguageWasAdded, + streetNameWasProposedV2) + .When(command) + .Throws(new StreetNameNameCorrectionExceededCharacterChangeLimitException(invalidStreetName.Name))); + } + [Fact] public void WithNoChanges_ThenNothing() {