Skip to content

Commit

Permalink
feat(backoffice): add dry run to merger + add error for combining and…
Browse files Browse the repository at this point in the history
… splitting same streetname
  • Loading branch information
ArneD committed Nov 14, 2024
1 parent 4cca1bb commit 3789e18
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public partial class StreetNameController
/// <param name="nisCode"></param>
/// <param name="persistentLocalIdGenerator"></param>
/// <param name="municipalityConsumerContext"></param>
/// <param name="dryRun"></param>
/// <param name="cancellationToken"></param>
[HttpPost("acties/voorstellen/gemeentefusie/{niscode}")]
[ProducesResponseType(StatusCodes.Status202Accepted)]
Expand All @@ -51,6 +52,7 @@ public async Task<IActionResult> ProposeForMunicipalityMerger(
[FromRoute(Name = "niscode")] string nisCode,
[FromServices] IPersistentLocalIdGenerator persistentLocalIdGenerator,
[FromServices] ConsumerContext municipalityConsumerContext,
[FromQuery(Name = "dry-run")] bool dryRun = false,
CancellationToken cancellationToken = default)
{
if (file == null || file.Length == 0)
Expand Down Expand Up @@ -187,6 +189,30 @@ public async Task<IActionResult> ProposeForMunicipalityMerger(
z.OldStreetNamePersistentLocalId,
oldMunicipalities.Single(x => x.NisCode == z.OldNisCode).MunicipalityId)).ToList());

//if merged streetnames are more than one for a streetname, then it's a merger (combination) of the streetnames
foreach (var combinedStreetName in streetNamesByNisCode.Where(x => x.Value.Count > 1))
{
//but if said streetname is also present in an other mergedstreetname, then it's also a split
//this combination is not supported
foreach (var mergedStreetName in combinedStreetName.Value)
{
if (streetNamesByNisCode.Any(x =>
x.Key != combinedStreetName.Key &&
x.Value.Any(y => y.StreetNamePersistentLocalId == mergedStreetName.StreetNamePersistentLocalId)))
{
errorMessages.Add($"Trying to combine and split streetname '{combinedStreetName.Key.StreetName}' is not supported");
}
}
}

if (errorMessages.Any())
{
return BadRequest(errorMessages.Distinct());
}

if (dryRun)
return NoContent();

var result = await _mediator
.Send(
new ProposeStreetNamesForMunicipalityMergerSqsRequest(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ await Controller.ProposeForMunicipalityMerger(
CsvHelpers.CreateFormFileFromString(CsvHelpers.Example),
"11001",
mockPersistentLocalIdGenerator.Object,
_municipalityConsumerContext,
CancellationToken.None);
_municipalityConsumerContext);
};

//Assert
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@ public void WithNoFormFile_ThenReturnsBadRequest()
null,
"bla",
Mock.Of<IPersistentLocalIdGenerator>(),
_municipalityConsumerContext,
CancellationToken.None).GetAwaiter().GetResult();
_municipalityConsumerContext).GetAwaiter().GetResult();

result.Should().BeOfType<BadRequestObjectResult>();
((BadRequestObjectResult)result).Value.Should().BeEquivalentTo("Please upload a CSV file.");
Expand All @@ -49,8 +48,7 @@ public void WithNoCsvExtension_ThenReturnsBadRequest()
CsvHelpers.CreateFormFileFromString("file", "content"),
"bla",
Mock.Of<IPersistentLocalIdGenerator>(),
_municipalityConsumerContext,
CancellationToken.None).GetAwaiter().GetResult();
_municipalityConsumerContext).GetAwaiter().GetResult();

result.Should().BeOfType<BadRequestObjectResult>();
((BadRequestObjectResult)result).Value.Should().BeEquivalentTo("Only CSV files are allowed.");
Expand All @@ -65,8 +63,7 @@ public void WithNoNisCode_ThenReturnsBadRequest()
"11001;123;;Name;HO"),
"bla",
Mock.Of<IPersistentLocalIdGenerator>(),
_municipalityConsumerContext,
CancellationToken.None).GetAwaiter().GetResult();
_municipalityConsumerContext).GetAwaiter().GetResult();

result.Should().BeOfType<BadRequestObjectResult>();
((BadRequestObjectResult)result).Value.Should().BeEquivalentTo(new[] { "NisCode is required at record number 1" });
Expand All @@ -82,8 +79,7 @@ public void WithDifferentNisCodeThanRoute_ThenReturnsBadRequest()
"11000;123;11001;Name;HO"),
nisCode,
Mock.Of<IPersistentLocalIdGenerator>(),
_municipalityConsumerContext,
CancellationToken.None).GetAwaiter().GetResult();
_municipalityConsumerContext).GetAwaiter().GetResult();

result.Should().BeOfType<BadRequestObjectResult>();
((BadRequestObjectResult)result).Value.Should().BeEquivalentTo(new[] { $"NisCode 11001 does not match the provided NisCode {nisCode} at record number 1" });
Expand All @@ -98,8 +94,7 @@ public void WithNoStreetName_ThenReturnsBadRequest()
"11000;123;NisCode;;HO"),
"NisCode",
Mock.Of<IPersistentLocalIdGenerator>(),
_municipalityConsumerContext,
CancellationToken.None).GetAwaiter().GetResult();
_municipalityConsumerContext).GetAwaiter().GetResult();

result.Should().BeOfType<BadRequestObjectResult>();
((BadRequestObjectResult)result).Value.Should().BeEquivalentTo(new[] { "StreetName is required at record number 1" });
Expand Down Expand Up @@ -129,15 +124,14 @@ public void WithDuplicateStreetName_ThenReturnsBadRequest(
$"11000;123;NisCode;{streetNameNameTwo};{homonymAdditionTwo}"),
"NisCode",
Mock.Of<IPersistentLocalIdGenerator>(),
_municipalityConsumerContext,
CancellationToken.None).GetAwaiter().GetResult();
_municipalityConsumerContext).GetAwaiter().GetResult();

result.Should().BeOfType<BadRequestObjectResult>();
((BadRequestObjectResult)result).Value.Should().BeEquivalentTo(new[] { "Duplicate record for streetName with persistent local id 123" });
}

[Fact]
public void WithValidCsv_ThenReturnsOk()
public void WithValidCsvAndDryRun_ThenReturnsNoContent()
{
var ticketId = Fixture.Create<Guid>();
var expectedLocationResult = new LocationResult(CreateTicketUri(ticketId));
Expand All @@ -163,7 +157,43 @@ public void WithValidCsv_ThenReturnsOk()
"11001",
mockPersistentLocalIdGenerator.Object,
_municipalityConsumerContext,
CancellationToken.None).GetAwaiter().GetResult();
dryRun:true).GetAwaiter().GetResult();

MockMediator.Verify(x =>
x.Send(
It.IsAny<ProposeStreetNamesForMunicipalityMergerSqsRequest>(),
It.IsAny<CancellationToken>()), Times.Never);

result.Should().BeOfType<NoContentResult>();
}

[Fact]
public void WithValidCsv_ThenReturnsOk()
{
var ticketId = Fixture.Create<Guid>();
var expectedLocationResult = new LocationResult(CreateTicketUri(ticketId));
MockMediatorResponse<ProposeStreetNamesForMunicipalityMergerSqsRequest, LocationResult>(expectedLocationResult);

var mockPersistentLocalIdGenerator = new Mock<IPersistentLocalIdGenerator>();
mockPersistentLocalIdGenerator.Setup(x => x.GenerateNextPersistentLocalId())
.Returns(new PersistentLocalId(1));

const string oldNisCode = "11000";
var oldMunicipalityId = Guid.NewGuid();
_municipalityConsumerContext.Add(new MunicipalityConsumerItem
{
MunicipalityId = oldMunicipalityId,
NisCode = oldNisCode
});
_municipalityConsumerContext.SaveChanges();

var result =
Controller.ProposeForMunicipalityMerger(
CsvHelpers.CreateFormFileFromString($"OUD NIS code;OUD straatnaamid;NIEUW NIS code;NIEUW straatnaam;NIEUW homoniemtoevoeging\n" +
$"{oldNisCode};123;11001;Street;HO\n{oldNisCode};456;11001;Name;NYM\n{oldNisCode};789;11001;Street;HO"),
"11001",
mockPersistentLocalIdGenerator.Object,
_municipalityConsumerContext).GetAwaiter().GetResult();

MockMediator.Verify(x =>
x.Send(
Expand All @@ -182,5 +212,52 @@ public void WithValidCsv_ThenReturnsOk()
acceptedResult.Location.Should().NotBeNull();
AssertLocation(acceptedResult.Location, ticketId);
}

[Fact]
public void WithCombinationAndSplitOfAStreetName_ThenReturnsBadRequest()
{
var ticketId = Fixture.Create<Guid>();
var expectedLocationResult = new LocationResult(CreateTicketUri(ticketId));
MockMediatorResponse<ProposeStreetNamesForMunicipalityMergerSqsRequest, LocationResult>(expectedLocationResult);

var mockPersistentLocalIdGenerator = new Mock<IPersistentLocalIdGenerator>();
mockPersistentLocalIdGenerator.Setup(x => x.GenerateNextPersistentLocalId())
.Returns(new PersistentLocalId(1));

const string oldNisCode = "71069";
var oldMunicipalityId = Guid.NewGuid();
_municipalityConsumerContext.Add(new MunicipalityConsumerItem
{
MunicipalityId = oldMunicipalityId,
NisCode = oldNisCode
});
_municipalityConsumerContext.SaveChanges();

/*
71069;117104;71071;Bakhuisstraat;
71069;117104;71071;Boomgaardstraat;
71069;117104;71071;Fruitstraat;
71069;117037;71071;Bakhuisstraat;
71069;117037;71071;De Schans;
71069;117037;71071;Keukenhof;
71069;117037;71071;Ravelijnstraat;
*/
var result =
Controller.ProposeForMunicipalityMerger(
CsvHelpers.CreateFormFileFromString($"OUD NIS code;OUD straatnaamid;NIEUW NIS code;NIEUW straatnaam;NIEUW homoniemtoevoeging\n" +
$"{oldNisCode};117104;71071;Bakhuisstraat;\n" +
$"{oldNisCode};117104;71071;Boomgaardstraat;\n" +
$"{oldNisCode};117104;71071;Fruitstraat;\n" +
$"{oldNisCode};117037;71071;Bakhuisstraat;\n" +
$"{oldNisCode};117037;71071;De Schans;\n" +
$"{oldNisCode};117037;71071;Keukenhof;\n" +
$"{oldNisCode};117037;71071;Ravelijnstraat;"),
"71071",
mockPersistentLocalIdGenerator.Object,
_municipalityConsumerContext).GetAwaiter().GetResult();

result.Should().BeOfType<BadRequestObjectResult>();
((BadRequestObjectResult)result).Value.Should().BeEquivalentTo(new[] {"Trying to combine and split streetname 'Bakhuisstraat' is not supported"});
}
}
}

0 comments on commit 3789e18

Please sign in to comment.