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 89dd4c9
Show file tree
Hide file tree
Showing 20 changed files with 458 additions and 29 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 @@ -21,6 +21,7 @@
using HoofdactiviteitVerenigingsloket = AssociationRegistry.Admin.Schema.Detail.HoofdactiviteitVerenigingsloket;
using Locatie = AssociationRegistry.Admin.Schema.Detail.Locatie;
using Vertegenwoordiger = AssociationRegistry.Admin.Schema.Detail.Vertegenwoordiger;
using Werkingsgebied = AssociationRegistry.Admin.Schema.Detail.Werkingsgebied;

[UnitTest]
public class Given_FeitelijkeVerenigingWerdGeregistreerd
Expand Down Expand Up @@ -168,6 +169,17 @@ public void Then_it_creates_a_new_vereniging()
Code = h.Code,
Naam = h.Naam,
}).ToArray(),
Werkingsgebieden = feitelijkeVerenigingWerdGeregistreerd.Data.Werkingsgebieden!.Select(
h => new Werkingsgebied()
{
JsonLdMetadata = new JsonLdMetadata
{
Id = JsonLdType.Werkingsgebied.CreateWithIdValues(h.Code),
Type = JsonLdType.Werkingsgebied.Type,
},
Code = h.Code,
Naam = h.Naam,
}).ToArray(),
Sleutels = new Sleutel[]
{
new()
Expand Down
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
Expand Up @@ -137,7 +137,8 @@ private static void CustomizeFeitelijkeVerenigingWerdGeregistreerd(this IFixture
fixture.CreateMany<Registratiedata.Contactgegeven>().ToArray(),
fixture.CreateMany<Registratiedata.Locatie>().ToArray(),
fixture.CreateMany<Registratiedata.Vertegenwoordiger>().ToArray(),
fixture.CreateMany<Registratiedata.HoofdactiviteitVerenigingsloket>().ToArray()
fixture.CreateMany<Registratiedata.HoofdactiviteitVerenigingsloket>().ToArray(),
fixture.CreateMany<Registratiedata.Werkingsgebied>().ToArray()
)).OmitAutoProperties());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public static void CustomizeRegistratiedata(IFixture fixture)
fixture.CustomizeAdresId();
fixture.CustomizeLocatie();
fixture.CustomizeHoofdactiviteitVerenigingsloket();
fixture.CustomizeWerkingsgebieden();
fixture.CustomizeContactgegeven();
fixture.CustomizeDoelgroep();
}
Expand Down Expand Up @@ -45,6 +46,18 @@ private static void CustomizeHoofdactiviteitVerenigingsloket(this IFixture fixtu
}).OmitAutoProperties());
}

private static void CustomizeWerkingsgebieden(this IFixture fixture)
{
fixture.Customize<Registratiedata.Werkingsgebied>(
composer => composer.FromFactory(
() =>
{
var h = fixture.Create<Werkingsgebied>();
return new Registratiedata.Werkingsgebied(h.Code, h.Naam);
}).OmitAutoProperties());
}

private static void CustomizeLocatie(this IFixture fixture)
{
fixture.Customize<Registratiedata.Locatie>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public V076_AdresWerdGewijzigdInAdressenregister()
Locatie,
}.ToArray(),
HoofdactiviteitenVerenigingsloket = Array.Empty<Registratiedata.HoofdactiviteitVerenigingsloket>(),
Werkingsgebieden = Array.Empty<Registratiedata.Werkingsgebied>(),
Doelgroep = Registratiedata.Doelgroep.With(Doelgroep.Null),
Vertegenwoordigers = Array.Empty<Registratiedata.Vertegenwoordiger>(),
Contactgegevens = Array.Empty<Registratiedata.Contactgegeven>(),
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);
}
}
Loading

0 comments on commit 89dd4c9

Please sign in to comment.