Skip to content

Commit

Permalink
feat: or-2451 add werkingsgebieden to powerbi export
Browse files Browse the repository at this point in the history
  • Loading branch information
emalfroy committed Oct 24, 2024
1 parent d28b9ca commit a5f7223
Show file tree
Hide file tree
Showing 12 changed files with 394 additions and 27 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ public record PowerBiExportDocument : IVCode
public HoofdactiviteitVerenigingsloket[] HoofdactiviteitenVerenigingsloket { get; set; } =
Array.Empty<HoofdactiviteitVerenigingsloket>();

public Werkingsgebied[] Werkingsgebieden { get; set; } =
Array.Empty<Werkingsgebied>();

public bool IsUitgeschrevenUitPubliekeDatastroom { get; set; }
public string Bron { get; set; } = null!;
[Identity] public string VCode { get; init; } = null!;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace AssociationRegistry.Admin.Schema.PowerBiExport;

public record Werkingsgebied
{
public string Code { get; init; } = null!;
public string Naam { get; init; } = null!;
}
1 change: 1 addition & 0 deletions src/AssociationRegistry.PowerBi.ExportHost/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ private static void ConfigureServices(HostBuilderContext context, IServiceCollec
new Exporter(WellKnownFileNames.Basisgegevens, powerBiExportOptions.BucketName, new BasisgegevensRecordWriter(), sp.GetRequiredService<IAmazonS3>(), sp.GetRequiredService<ILogger<Exporter>>()),
new Exporter(WellKnownFileNames.Contactgegevens, powerBiExportOptions.BucketName, new ContactgegevensRecordWriter(), sp.GetRequiredService<IAmazonS3>(), sp.GetRequiredService<ILogger<Exporter>>()),
new Exporter(WellKnownFileNames.Hoofdactiviteiten, powerBiExportOptions.BucketName, new HoofdactiviteitenRecordWriter(), sp.GetRequiredService<IAmazonS3>(), sp.GetRequiredService<ILogger<Exporter>>()),
new Exporter(WellKnownFileNames.Werkingsgebieden, powerBiExportOptions.BucketName, new WerkingsgebiedenRecordWriter(), sp.GetRequiredService<IAmazonS3>(), sp.GetRequiredService<ILogger<Exporter>>()),
new Exporter(WellKnownFileNames.Locaties, powerBiExportOptions.BucketName, new LocatiesRecordWriter(), sp.GetRequiredService<IAmazonS3>(), sp.GetRequiredService<ILogger<Exporter>>()),
new Exporter(WellKnownFileNames.Historiek, powerBiExportOptions.BucketName, new HistoriekRecordWriter(), sp.GetRequiredService<IAmazonS3>(), sp.GetRequiredService<ILogger<Exporter>>()),
});
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace AssociationRegistry.PowerBi.ExportHost.Records;

using CsvHelper.Configuration.Attributes;

public record WerkingsgebiedenRecord(
[property: Name("code"), Index(0)] string Code,
[property: Name("naam"), Index(1)] string Naam,
[property: Name("vcode"), Index(2)] string VCode);
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ public class WellKnownFileNames
public const string Locaties = "Locaties.csv";
public const string Contactgegevens = "Contactgegevens.csv";
public const string Historiek = "Historiek.csv";
public const string Werkingsgebieden = "Werkingsgebieden.csv";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
namespace AssociationRegistry.PowerBi.ExportHost.Writers;

using Admin.Schema.PowerBiExport;
using Records;
using CsvHelper;

public class WerkingsgebiedenRecordWriter : IRecordWriter
{
public async Task Write(IEnumerable<PowerBiExportDocument> docs, IWriter csvWriter)
{
csvWriter.WriteHeader<WerkingsgebiedenRecord>();
await csvWriter.NextRecordAsync();

foreach (var vereniging in docs)
{
foreach (var werkgebied in vereniging.Werkingsgebieden)
{
csvWriter.WriteRecord(new WerkingsgebiedenRecord(
werkgebied.Code, werkgebied.Naam,
vereniging.VCode));

await csvWriter.NextRecordAsync();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,31 @@ await documentSession
powerBiExportDocument.HoofdactiviteitenVerenigingsloket.ShouldCompare(expectedHoofdactiviteiten);
}

[Fact]
public async Task ARecordIsStored_With_Werkingsgebieden()
{
await using var documentSession = _context
.Session;

var powerBiExportDocument =
await documentSession
.Query<PowerBiExportDocument>()
.SingleAsync(x => x.VCode == _scenario.VerenigingWerdGeregistreerd.VCode);

var expectedHoofdactiviteiten =
_scenario
.VerenigingWerdGeregistreerd
.Werkingsgebieden!
.Select(x => new Werkingsgebied()
{
Naam = x.Naam,
Code = x.Code,
})
.ToArray();

powerBiExportDocument.Werkingsgebieden.ShouldCompare(expectedHoofdactiviteiten);
}

[Fact]
public async Task ARecordIsStored_With_Historiek()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
namespace AssociationRegistry.Test.Admin.Api.Projections.V2.PowerBiExport;

using AssociationRegistry.Admin.Schema.PowerBiExport;
using FluentAssertions;
using KellermanSoftware.CompareNetObjects;
using Marten;
using Projections.PowerBiExport;
using ScenarioClassFixtures;
using Xunit;

[Collection(nameof(PowerBiExportContext))]
public class Given_WerkingsgebiedenWerdenGewijzigd : IClassFixture<WerkingsgebiedenWerdenGewijzigdScenario>
{
private readonly PowerBiExportContext _context;
private readonly WerkingsgebiedenWerdenGewijzigdScenario _scenario;

public Given_WerkingsgebiedenWerdenGewijzigd(
PowerBiExportContext context,
WerkingsgebiedenWerdenGewijzigdScenario scenario)
{
_context = context;
_scenario = scenario;
}

[Fact]
public async Task ARecordIsStored_With_Hoofdactiviteiten()
{
await using var documentSession = _context
.Session;

var powerBiExportDocument =
await documentSession
.Query<PowerBiExportDocument>()
.Where(w => w.VCode == _scenario.VerenigingWerdGeregistreerd.VCode)
.SingleAsync();

var expectedHoofdactiviteiten =
_scenario
.WerkingsgebiedenWerdenGewijzigd
.Werkingsgebieden
.Select(x => new Werkingsgebied()
{
Naam = x.Naam,
Code = x.Code,
})
.ToArray();

powerBiExportDocument.Werkingsgebieden.ShouldCompare(expectedHoofdactiviteiten);
}

[Fact]
public async Task ARecordIsStored_With_Historiek()
{
await using var documentSession = _context
.Session;

var powerBiExportDocument =
await documentSession
.Query<PowerBiExportDocument>()
.SingleAsync(w => w.VCode == _scenario.VerenigingWerdGeregistreerd.VCode);

powerBiExportDocument.VCode.Should().Be(_scenario.VerenigingWerdGeregistreerd.VCode);
powerBiExportDocument.Historiek.Should().NotBeEmpty();
powerBiExportDocument.Historiek.Should()
.ContainSingle(x => x.EventType == "WerkingsgebiedenWerdenGewijzigd");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
namespace AssociationRegistry.Test.Admin.Api.Projections.V2.PowerBiExport.ScenarioClassFixtures;

using AssociationRegistry.Events;
using AssociationRegistry.Test.Admin.Api.Projections.V2.PowerBiExport;
using AutoFixture;
using Projections.PowerBiExport;

public class WerkingsgebiedenWerdenGewijzigdScenario : ProjectionScenarioFixture<PowerBiExportContext>
{
public FeitelijkeVerenigingWerdGeregistreerd VerenigingWerdGeregistreerd { get; }
public WerkingsgebiedenWerdenGewijzigd WerkingsgebiedenWerdenGewijzigd { get; set; }

public WerkingsgebiedenWerdenGewijzigdScenario(PowerBiExportContext context): base(context)
{
VerenigingWerdGeregistreerd = AutoFixture.Create<FeitelijkeVerenigingWerdGeregistreerd>();

WerkingsgebiedenWerdenGewijzigd = AutoFixture.Create<WerkingsgebiedenWerdenGewijzigd>();
}

public override async Task Given()
{
await using var session = await Context.DocumentSession();

session.Events.Append(VerenigingWerdGeregistreerd.VCode,
VerenigingWerdGeregistreerd);
await session.SaveChangesAsync();
await using var session2 = await Context.DocumentSession();

session2.Events.Append(VerenigingWerdGeregistreerd.VCode,
WerkingsgebiedenWerdenGewijzigd);

await session2.SaveChangesAsync();

await Context.WaitForNonStaleProjectionDataAsync();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ namespace AssociationRegistry.Test.Common.AutoFixture;
using Vereniging.TelefoonNummers;
using Vereniging.Websites;
using HoofdactiviteitVerenigingsloket = Vereniging.HoofdactiviteitVerenigingsloket;
using Werkingsgebied = Vereniging.Werkingsgebied;

public static class AutoFixtureCustomizations
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
namespace AssociationRegistry.Test.PowerBi.ExportHost;

using Admin.Schema.PowerBiExport;
using Amazon.S3;
using Amazon.S3.Model;
using AssociationRegistry.PowerBi.ExportHost;
using AssociationRegistry.PowerBi.ExportHost.Writers;
using AutoFixture;
using Common.AutoFixture;
using FluentAssertions;
using Microsoft.Extensions.Logging.Abstractions;
using Moq;
using System.Net;
using System.Text;
using Xunit;

public class WerkingsgebiedenRecordWriterTests
{
private Stream _resultStream;
private readonly Fixture _fixture;
private readonly Mock<IAmazonS3> _s3ClientMock;

public WerkingsgebiedenRecordWriterTests()
{
_fixture = new Fixture().CustomizeDomain();
_s3ClientMock = SetupS3Client();
}

[Fact]
public async Task WithNoDocuments_ThenCsvExportsOnlyHeaders()
{
var docs = Array.Empty<PowerBiExportDocument>();

await Export(docs);

var actualResult = await GetActualResult();
actualResult.Should().BeEquivalentTo("code,naam,vcode\r\n");
}

[Fact]
public async Task WithMultipleDocuments_ThenCsvExportShouldExport()
{
var docs = _fixture.CreateMany<PowerBiExportDocument>();

await Export(docs);

var actualResult = await GetActualResult();
var expectedResult = GetExpectedResult(docs);

actualResult.Should().BeEquivalentTo(expectedResult);
}

private async Task<string> GetActualResult()
{
using var reader = new StreamReader(_resultStream, Encoding.UTF8);

var content = await reader.ReadToEndAsync();

return content;
}

private static string GetExpectedResult(IEnumerable<PowerBiExportDocument> docs)
{
var stringBuilder = new StringBuilder();
stringBuilder.Append("code,naam,vcode\r\n");

foreach (var doc in docs)
{
foreach (var werkgebied in doc.Werkingsgebieden)
{
stringBuilder.Append(
$"{werkgebied.Code},{werkgebied.Naam},{doc.VCode}\r\n");
}
}

return stringBuilder.ToString();
}

private Mock<IAmazonS3> SetupS3Client()
{
var s3ClientMock = new Mock<IAmazonS3>();

s3ClientMock.Setup(x => x.PutObjectAsync(It.IsAny<PutObjectRequest>(), default))
.Callback<PutObjectRequest, CancellationToken>((request, _) => _resultStream = request.InputStream)
.ReturnsAsync(new PutObjectResponse { HttpStatusCode = HttpStatusCode.OK });

return s3ClientMock;
}

private async Task Export(IEnumerable<PowerBiExportDocument> docs)
{
var exporter = new Exporter(WellKnownFileNames.Werkingsgebieden,
bucketName: "something",
new WerkingsgebiedenRecordWriter(),
_s3ClientMock.Object,
new NullLogger<Exporter>());

await exporter.Export(docs);
}
}

0 comments on commit a5f7223

Please sign in to comment.