Skip to content

Commit

Permalink
Merge pull request #61 from Informatievlaanderen/feat/or-1244_add_uni…
Browse files Browse the repository at this point in the history
…ttest_for_projection

feat: or-1244 create repository for elastic (testablity)
  • Loading branch information
W0dan authored Oct 19, 2022
2 parents ce6107e + a347b2f commit 4ec880c
Show file tree
Hide file tree
Showing 7 changed files with 176 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,30 @@ namespace AssociationRegistry.Public.Api.Projections;

using System.Linq;
using Events;
using Nest;
using SearchVerenigingen;

public class ElasticEventHandler
{
private readonly IElasticClient _elasticClient;
private readonly IElasticRepository _elasticRepository;
private readonly IVerenigingBrolFeeder _brolFeeder;

public ElasticEventHandler(IElasticClient elasticClient, IVerenigingBrolFeeder brolFeeder)
public ElasticEventHandler(IElasticRepository elasticRepository, IVerenigingBrolFeeder brolFeeder)
{
_elasticClient = elasticClient;
_elasticRepository = elasticRepository;
_brolFeeder = brolFeeder;
}

public void HandleEvent(VerenigingWerdGeregistreerd message)
{
var document = new VerenigingDocument(
message.VCode,
message.Naam,
_brolFeeder.KorteNaam,
_brolFeeder.Hoofdlocatie,
_brolFeeder.Locaties.ToArray(),
_brolFeeder.Hoofdactiviteiten,
_brolFeeder.Doelgroep,
_brolFeeder.Activiteiten.ToArray());
var response = _elasticClient.IndexDocument(document);

if (!response.IsValid)
throw new IndexDocumentFailed(response.DebugInformation);
}
=> _elasticRepository.Save(
new VerenigingDocument(
message.VCode,
message.Naam,
_brolFeeder.KorteNaam,
_brolFeeder.Hoofdlocatie,
_brolFeeder.Locaties.ToArray(),
_brolFeeder.Hoofdactiviteiten,
_brolFeeder.Doelgroep,
_brolFeeder.Activiteiten.ToArray()
)
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
namespace AssociationRegistry.Public.Api.Projections;

using Nest;

public class ElasticRepository : IElasticRepository
{
private readonly IElasticClient _elasticClient;

public ElasticRepository(IElasticClient elasticClient)
{
_elasticClient = elasticClient;
}

public void Save<TDocument>(TDocument document)
where TDocument : class
{
var response = _elasticClient.IndexDocument(document);

if (!response.IsValid)
{
// todo: log ? (should never happen in test/staging/production)
throw new IndexDocumentFailed(response.DebugInformation);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace AssociationRegistry.Public.Api.Projections;

public interface IElasticRepository
{
void Save<TDocument>(TDocument document)
where TDocument : class;
}
2 changes: 1 addition & 1 deletion src/AssociationRegistry.Public.Api/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public IServiceProvider ConfigureServices(IServiceCollection services)

opts.Events.StreamIdentity = StreamIdentity.AsString;

var esEventHandler = new ElasticEventHandler(elasticClient, new VerenigingBrolFeeder());
var esEventHandler = new ElasticEventHandler(new ElasticRepository(elasticClient), new VerenigingBrolFeeder());

opts.Projections.Add(
new MartenSubscription(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
namespace AssociationRegistry.Test.Projections.Tests.When_Saving_A_Document_To_Elastic;

using System.Reflection;
using AssociationRegistry.Public.Api;
using AssociationRegistry.Public.Api.Projections;
using AssociationRegistry.Public.Api.SearchVerenigingen;
using Microsoft.Extensions.Configuration;
using Nest;

public abstract class ElasticRepositoryFixture : IDisposable
{
private readonly string _identifier;
private readonly IConfigurationRoot _configurationRoot;
private readonly ElasticClient _elasticClient;
public ElasticRepository ElasticRepository { get; }

private string VerenigingenIndexName
=> _configurationRoot["ElasticClientOptions:Indices:Verenigingen"];

protected ElasticRepositoryFixture(string identifier)
{
_identifier += "_" + identifier.ToLowerInvariant();
GoToRootDirectory();

_configurationRoot = SetConfigurationRoot();

_elasticClient = ConfigureElasticClient();

ElasticRepository = new ElasticRepository(_elasticClient);
}

private static void GoToRootDirectory()
{
var maybeRootDirectory = Directory
.GetParent(typeof(Startup).GetTypeInfo().Assembly.Location)?.Parent?.Parent?.Parent?.FullName;
if (maybeRootDirectory is not { } rootDirectory)
throw new NullReferenceException("Root directory cannot be null");
Directory.SetCurrentDirectory(rootDirectory);
}

private IConfigurationRoot SetConfigurationRoot()
{
var builder = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: true)
.AddJsonFile($"appsettings.{Environment.MachineName.ToLowerInvariant()}.json", optional: true);
var tempConfiguration = builder.Build();
tempConfiguration["PostgreSQLOptions:database"] += _identifier;
tempConfiguration["ElasticClientOptions:Indices:Verenigingen"] += _identifier;
return tempConfiguration;
}

private ElasticClient ConfigureElasticClient()
{
var settings = new ConnectionSettings(new Uri(_configurationRoot["ElasticClientOptions:Uri"]))
.BasicAuthentication(
_configurationRoot["ElasticClientOptions:Username"],
_configurationRoot["ElasticClientOptions:Password"])
.DefaultMappingFor(
typeof(VerenigingDocument),
descriptor => descriptor.IndexName(VerenigingenIndexName))
.EnableDebugMode();

var client = new ElasticClient(settings);
if (client.Indices.Exists(VerenigingenIndexName).Exists)
client.Indices.Delete(VerenigingenIndexName);

client.Indices.Create(
VerenigingenIndexName,
c => c
.Map<VerenigingDocument>(
m => m
.AutoMap<VerenigingDocument>()));

client.Indices.Refresh(Indices.All);
return client;
}

public void Dispose()
{
GC.SuppressFinalize(this);

_elasticClient.Indices.Delete(VerenigingenIndexName);
_elasticClient.Indices.Refresh(Indices.All);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
namespace AssociationRegistry.Test.Projections.Tests.When_Saving_A_Document_To_Elastic;

using AssociationRegistry.Public.Api.SearchVerenigingen;
using AutoFixture;
using Xunit;

public class Given_A_Valid_VerenigingDocument_Fixture : ElasticRepositoryFixture
{
public Given_A_Valid_VerenigingDocument_Fixture() : base(nameof(Given_A_Valid_VerenigingDocument_Fixture))
{
}
}

public class Given_A_Valid_VerenigingDocument : IClassFixture<Given_A_Valid_VerenigingDocument_Fixture>
{
private readonly Given_A_Valid_VerenigingDocument_Fixture _classFixture;

public Given_A_Valid_VerenigingDocument(Given_A_Valid_VerenigingDocument_Fixture classFixture)
{
_classFixture = classFixture;
}

[Fact]
public void Then_it_does_not_throw_an_exception()
{
var fixture = new Fixture();

_classFixture.ElasticRepository
.Save(
new VerenigingDocument(
fixture.Create<string>(),
fixture.Create<string>(),
fixture.Create<string>(),
fixture.Create<string>(),
new[] { fixture.Create<string>() },
new[] { fixture.Create<string>() },
fixture.Create<string>(),
new[] { fixture.Create<string>() }
));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ private ElasticClient ConfigureElasticClient()

private DocumentStore ConfigureDocumentStore()
{
var esEventHandler = new ElasticEventHandler(_elasticClient, new BrolFeederStub());
var esEventHandler = new ElasticEventHandler(new ElasticRepository(_elasticClient), new BrolFeederStub());

return DocumentStore.For(
opts =>
Expand Down

0 comments on commit 4ec880c

Please sign in to comment.