-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #61 from Informatievlaanderen/feat/or-1244_add_uni…
…ttest_for_projection feat: or-1244 create repository for elastic (testablity)
- Loading branch information
Showing
7 changed files
with
176 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
src/AssociationRegistry.Public.Api/Projections/ElasticRepository.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
src/AssociationRegistry.Public.Api/Projections/IElasticRepository.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
...stry.Test/Projections.Tests/When_Saving_A_Document_To_Elastic/ElasticRepositoryFixture.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
...t/Projections.Tests/When_Saving_A_Document_To_Elastic/Given_A_Valid_VerenigingDocument.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>() } | ||
)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters