Skip to content

Commit

Permalink
feat: add rename validation different municipalities GAWR-5383
Browse files Browse the repository at this point in the history
  • Loading branch information
ArneD committed Jan 16, 2024
1 parent 5adad7c commit 4cac530
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,9 @@ public sealed class RenameStreetNameRequest
[DataMember(Name = "DoelStraatnaamId", Order = 0)]
[JsonProperty(Required = Required.Always)]
public string DoelStraatnaamId { get; set; }

[IgnoreDataMember]
[JsonIgnore]
public int StreetNamePersistentLocalId { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ public static class DestinationStreetNameHasInvalidStatus

public static TicketError ToTicketError() => new TicketError(Message, Code);
}

public static class SourceAndDestinationStreetNameAreNotInSameMunicipality
{
public const string Code = "StraatnamenAndereGemeenten";
public const string Message = "De meegegeven straatnamen liggen in verschillende gemeenten.";

public static TicketError ToTicketError() => new TicketError(Message, Code);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public async Task<IActionResult> Rename(
[FromHeader(Name = "If-Match")] string? ifMatchHeaderValue,
CancellationToken cancellationToken = default)
{
request.StreetNamePersistentLocalId = persistentLocalId;
await validator.ValidateAndThrowAsync(request, cancellationToken);

try
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ namespace StreetNameRegistry.Api.BackOffice.Validators
using Abstractions.Requests;
using Abstractions.Validation;
using Be.Vlaanderen.Basisregisters.GrAr.Edit.Validators;
using Be.Vlaanderen.Basisregisters.GrAr.Legacy.Straatnaam;
using FluentValidation;

public sealed class RenameStreetNameRequestValidator : AbstractValidator<RenameStreetNameRequest>
Expand All @@ -27,6 +28,30 @@ public RenameStreetNameRequestValidator(BackOfficeContext backOfficeContext)

return municipalityIdByPersistentLocalId is not null;
})
.DependentRules(() =>
RuleFor(y => y.DoelStraatnaamId)
.MustAsync(async (request, straatNaamId, ct) =>
{
OsloPuriValidator.TryParseIdentifier(straatNaamId, out var persistentLocalIdAsString);

var persistentLocalId = int.Parse(persistentLocalIdAsString);

var municipalityIdByDestinationPersistentLocalId = await backOfficeContext
.MunicipalityIdByPersistentLocalId
.FindAsync(new object?[] { persistentLocalId }, cancellationToken: ct);

var municipalityIdBySourcePersistentLocalId = await backOfficeContext
.MunicipalityIdByPersistentLocalId
.FindAsync(new object?[] { request.StreetNamePersistentLocalId }, cancellationToken: ct);

return
municipalityIdBySourcePersistentLocalId != null
&& municipalityIdByDestinationPersistentLocalId != null
&& municipalityIdByDestinationPersistentLocalId.MunicipalityId == municipalityIdBySourcePersistentLocalId.MunicipalityId;
})
.WithMessage(ValidationErrors.RenameStreetName.SourceAndDestinationStreetNameAreNotInSameMunicipality.Message)
.WithErrorCode(ValidationErrors.RenameStreetName.SourceAndDestinationStreetNameAreNotInSameMunicipality.Code)
)
.WithMessage((_, straatNaamId) => ValidationErrors.Common.StreetNameInvalid.Message(straatNaamId))
.WithErrorCode(ValidationErrors.Common.StreetNameInvalid.Code))
.WithMessage(ValidationErrors.Common.StreetNameNotFound.Message)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,25 @@ public RenameStreetNameRequestValidatorTests()
[Fact]
public void GivenValidRequest_NoErrorsAreReturned()
{
var municipalityId = Guid.NewGuid();
var persistentLocalId = 10000;
_backOfficeContext.MunicipalityIdByPersistentLocalId.Add(new MunicipalityIdByPersistentLocalId(
persistentLocalId,
Guid.NewGuid(),
municipalityId,
"NISCODE"));
_backOfficeContext.SaveChanges();

var persistentLocalId2 = 10001;
_backOfficeContext.MunicipalityIdByPersistentLocalId.Add(new MunicipalityIdByPersistentLocalId(
persistentLocalId2,
municipalityId,
"NISCODE"));
_backOfficeContext.SaveChanges();

var result = _validator.TestValidate(new RenameStreetNameRequest
{
DoelStraatnaamId = $"https://data.vlaanderen.be/id/straatnaam/{persistentLocalId}"
DoelStraatnaamId = $"https://data.vlaanderen.be/id/straatnaam/{persistentLocalId}",
StreetNamePersistentLocalId = persistentLocalId2
});

result.ShouldNotHaveAnyValidationErrors();
Expand All @@ -52,7 +61,7 @@ public void GivenInvalidPuri_ReturnsExpectedError(string invalidPuri)
}

[Fact]
public void WithUnknownStreetName_ReturnsExpectedError()
public void GivenUnknownStreetName_ReturnsExpectedError()
{
var puri = "https://data.vlaanderen.be/id/straatnaam/123";
var result = _validator.TestValidate(new RenameStreetNameRequest
Expand All @@ -65,5 +74,31 @@ public void WithUnknownStreetName_ReturnsExpectedError()
.WithErrorCode("StraatnaamNietGekendValidatie");
}

[Fact]
public void GivenStreetNamesInDifferentMunicipalities_ReturnsExpectedError()
{
var persistentLocalId = 10000;
var persistentLocalId2 = 10001;
_backOfficeContext.MunicipalityIdByPersistentLocalId.Add(new MunicipalityIdByPersistentLocalId(
persistentLocalId,
Guid.NewGuid(),
"NISCODE"));

_backOfficeContext.MunicipalityIdByPersistentLocalId.Add(new MunicipalityIdByPersistentLocalId(
persistentLocalId2,
Guid.NewGuid(),
"NISCODE2"));
_backOfficeContext.SaveChanges();

var result = _validator.TestValidate(new RenameStreetNameRequest
{
DoelStraatnaamId = $"https://data.vlaanderen.be/id/straatnaam/{persistentLocalId}",
StreetNamePersistentLocalId = persistentLocalId2
});

result.ShouldHaveValidationErrorFor(nameof(RenameStreetNameRequest.DoelStraatnaamId))
.WithErrorMessage("De meegegeven straatnamen liggen in verschillende gemeenten.")
.WithErrorCode("StraatnamenAndereGemeenten");
}
}
}

0 comments on commit 4cac530

Please sign in to comment.