Skip to content

Commit

Permalink
fix(extract): add missing version update in extract + add tests
Browse files Browse the repository at this point in the history
GAWR-6683
  • Loading branch information
ArneD committed Nov 26, 2024
1 parent bc57f09 commit 6275610
Show file tree
Hide file tree
Showing 5 changed files with 173 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,11 @@ await backOfficeContext.ParcelAddressRelations.AddAsync(
await backOfficeContext.SaveChangesAsync(cancellationToken);
});

When<Envelope<ParcelAddressesWereReaddressed>>((_, _, _) => Task.CompletedTask);
When<Envelope<ParcelWasImported>>(DoNothing);
When<Envelope<ParcelGeometryWasChanged>>(DoNothing);
When<Envelope<ParcelAddressesWereReaddressed>>(DoNothing);
When<Envelope<ParcelWasRetiredV2>>(DoNothing);
When<Envelope<ParcelWasCorrectedFromRetiredToRealized>>(DoNothing);
}

private static async Task DelayProjection<TMessage>(Envelope<TMessage> envelope, int delayInSeconds, CancellationToken cancellationToken)
Expand All @@ -171,5 +175,7 @@ private static async Task DelayProjection<TMessage>(Envelope<TMessage> envelope,
await Task.Delay(TimeSpan.FromSeconds(delayInSeconds - differenceInSeconds), cancellationToken);
}
}

private static Task DoNothing<T>(BackOfficeProjectionsContext context, Envelope<T> envelope, CancellationToken ct) where T: IMessage => Task.CompletedTask;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,17 @@ await context.FindAndUpdateParcelExtract(
},
ct);
});

When<Envelope<ParcelAddressesWereReaddressed>>(async (context, message, ct) =>
{
await context.FindAndUpdateParcelExtract(
message.Message.ParcelId,
parcel =>
{
UpdateVersie(parcel, message.Message.Provenance.Timestamp);
},
ct);
});
}

private void SetDelete(ParcelExtractItem parcel)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ namespace ParcelRegistry.Projections.Extract.ParcelLinkExtract
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Be.Vlaanderen.Basisregisters.EventHandling;
using Be.Vlaanderen.Basisregisters.GrAr.Extracts;
using Be.Vlaanderen.Basisregisters.ProjectionHandling.Connector;
using Be.Vlaanderen.Basisregisters.ProjectionHandling.SqlStreamStore;
Expand Down Expand Up @@ -149,6 +150,11 @@ await context
await AddParcelLink(context, message.Message.ParcelId, message.Message.CaPaKey, addressPersistentLocalId, ct);
}
});

When<Envelope<ParcelGeometryWasChanged>>(DoNothing);
When<Envelope<ParcelWasRetiredV2>>(DoNothing);
When<Envelope<ParcelWasCorrectedFromRetiredToRealized>>(DoNothing);
When<Envelope<ParcelWasImported>>(DoNothing);
}

private static async Task RemoveParcelLink(
Expand Down Expand Up @@ -197,5 +203,7 @@ await context
}, ct);
}
}

private static Task DoNothing<T>(ExtractContext context, Envelope<T> envelope, CancellationToken ct) where T: IMessage => Task.CompletedTask;
}
}
4 changes: 4 additions & 0 deletions test/ParcelRegistry.Tests/ParcelRegistry.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@
<ProjectReference Include="..\..\src\ParcelRegistry.Api.BackOffice\ParcelRegistry.Api.BackOffice.csproj" />
<ProjectReference Include="..\..\src\ParcelRegistry.Consumer.Address\ParcelRegistry.Consumer.Address.csproj" />
<ProjectReference Include="..\..\src\ParcelRegistry.Importer.Grb\ParcelRegistry.Importer.Grb.csproj" />
<ProjectReference Include="..\..\src\ParcelRegistry.Producer.Snapshot.Oslo\ParcelRegistry.Producer.Snapshot.Oslo.csproj" />
<ProjectReference Include="..\..\src\ParcelRegistry.Producer\ParcelRegistry.Producer.csproj" />
<ProjectReference Include="..\..\src\ParcelRegistry.Projections.BackOffice\ParcelRegistry.Projections.BackOffice.csproj" />
<ProjectReference Include="..\..\src\ParcelRegistry.Projections.Extract\ParcelRegistry.Projections.Extract.csproj" />
<ProjectReference Include="..\..\src\ParcelRegistry.Projections.Integration\ParcelRegistry.Projections.Integration.csproj" />
<ProjectReference Include="..\..\src\ParcelRegistry.Projections.LastChangedList\ParcelRegistry.Projections.LastChangedList.csproj" />
<ProjectReference Include="..\..\src\ParcelRegistry.Projections.Legacy\ParcelRegistry.Projections.Legacy.csproj" />
<ProjectReference Include="..\..\src\ParcelRegistry\ParcelRegistry.csproj" />
</ItemGroup>
Expand Down
143 changes: 143 additions & 0 deletions test/ParcelRegistry.Tests/ProjectionsHandlesEventsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
namespace ParcelRegistry.Tests
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Text;
using Api.BackOffice.Abstractions;
using Be.Vlaanderen.Basisregisters.EventHandling;
using Be.Vlaanderen.Basisregisters.GrAr.Oslo.SnapshotProducer;
using Be.Vlaanderen.Basisregisters.MessageHandling.Kafka.Producer;
using Be.Vlaanderen.Basisregisters.ProjectionHandling.Connector;
using Be.Vlaanderen.Basisregisters.ProjectionHandling.LastChangedList;
using Be.Vlaanderen.Basisregisters.Testing.Infrastructure.Events;
using FluentAssertions;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Options;
using Moq;
using NetTopologySuite.IO;
using Parcel.Events;
using Producer;
using Producer.Snapshot.Oslo;
using Projections.BackOffice;
using Projections.Extract;
using Projections.Extract.ParcelExtract;
using Projections.Extract.ParcelLinkExtract;
using Projections.Integration;
using Projections.Integration.Infrastructure;
using Projections.Integration.ParcelLatestItemV2;
using Projections.Integration.ParcelVersion;
using Projections.LastChangedList;
using Projections.Legacy;
using Projections.Legacy.ParcelDetail;
using Projections.Legacy.ParcelSyndication;
using SqlStreamStore;
using Xunit;

public sealed class ProjectionsHandlesEventsTests
{
private readonly IEnumerable<Type> _eventsToExclude = new[] { typeof(ParcelSnapshotV2) };
private readonly IList<Type> _eventTypes;

public ProjectionsHandlesEventsTests()
{
_eventTypes = DiscoverEventTypes();
}

private IList<Type> DiscoverEventTypes()
{
var domainAssembly = AppDomain.CurrentDomain.GetAssemblies()
.FirstOrDefault(a => InfrastructureEventsTests.GetAssemblyTypesSafe(a)
.Any(t => t.Name == "DomainAssemblyMarker"));

if (domainAssembly == null)
{
return Enumerable.Empty<Type>().ToList();
}

return domainAssembly.GetTypes()
.Where(t => t is { IsClass: true, Namespace: not null }
&& IsEventNamespace(t)
&& IsNotCompilerGenerated(t)
&& t.GetCustomAttributes(typeof(EventNameAttribute), true).Length != 0)
.Except(_eventsToExclude)
.ToList();
}

private static bool IsEventNamespace(Type t) => t.Namespace?.EndsWith("Parcel.Events") ?? false;
private static bool IsNotCompilerGenerated(MemberInfo t) => Attribute.GetCustomAttribute(t, typeof(CompilerGeneratedAttribute)) == null;

[Theory]
[MemberData(nameof(GetProjectionsToTest))]
public void ProjectionsHandleEvents<T>(List<ConnectedProjection<T>> projectionsToTest)
{
AssertHandleEvents(projectionsToTest);
}

public static IEnumerable<object[]> GetProjectionsToTest()
{
yield return [new List<ConnectedProjection<LegacyContext>>
{
new ParcelDetailProjections(),
new ParcelSyndicationProjections(),
}];

yield return [new List<ConnectedProjection<LastChangedListContext>>
{
new LastChangedListProjections(Mock.Of<ICacheValidator>())
}];

yield return [new List<ConnectedProjection<IntegrationContext>>
{
new ParcelLatestItemV2Projections(Mock.Of<IOptions<IntegrationOptions>>()),
new ParcelVersionProjections(Mock.Of<IAddressRepository>(), Mock.Of<IOptions<IntegrationOptions>>())
}];

yield return [new List<ConnectedProjection<ExtractContext>>
{
new ParcelExtractProjections(new OptionsWrapper<ExtractConfig>(new ExtractConfig()), Encoding.UTF8),
new ParcelLinkExtractProjections(Encoding.UTF8)
}];

var inMemorySettings = new Dictionary<string, string>
{
{ "DelayInSeconds", "10" }
};
var configurationRoot = new ConfigurationBuilder()
.AddInMemoryCollection(inMemorySettings)
.Build();

yield return [new List<ConnectedProjection<BackOfficeProjectionsContext>>
{
new BackOfficeProjections(Mock.Of<IDbContextFactory<BackOfficeContext>>(), configurationRoot)
}];

yield return [new List<ConnectedProjection<ParcelRegistry.Producer.Snapshot.Oslo.ProducerContext>>
{
new ProducerProjections(Mock.Of<IProducer>(), Mock.Of<ISnapshotManager>(), Mock.Of<IOsloProxy>())
}];

yield return [new List<ConnectedProjection<ParcelRegistry.Producer.ProducerContext>>
{
new ProducerMigrateProjections(Mock.Of<IProducer>())
}];
}

private void AssertHandleEvents<T>(List<ConnectedProjection<T>> projectionsToTest, IList<Type>? eventsToExclude = null)
{
var eventsToCheck = _eventTypes.Except(eventsToExclude ?? Enumerable.Empty<Type>()).ToList();
foreach (var projection in projectionsToTest)
{
projection.Handlers.Should().NotBeEmpty();
foreach (var eventType in eventsToCheck)
{
var messageType = projection.Handlers.Any(x => x.Message.GetGenericArguments().First() == eventType);
messageType.Should().BeTrue($"The event {eventType.Name} is not handled by the projection {projection.GetType().Name}");
}
}
}
}
}

0 comments on commit 6275610

Please sign in to comment.