Skip to content

Commit

Permalink
test the service registrations in the composition root for the applic…
Browse files Browse the repository at this point in the history
…ation tier (#53)

* test the service registrations in the composition root for the application tier

* warnings
  • Loading branch information
CathLass authored Oct 1, 2024
1 parent e2804b3 commit aa30e30
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 11 deletions.
2 changes: 2 additions & 0 deletions Dfe.Data.SearchPrototype/Dfe.Data.SearchPrototype.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@


<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="8.0.2" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Options" Version="8.0.2" />
</ItemGroup>
Expand Down
7 changes: 0 additions & 7 deletions Dfe.Data.SearchPrototype/Infrastructure/CompositionRoot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,6 @@ public static void AddCognitiveSearchAdaptorServices(this IServiceCollection ser
.GetSection(nameof(AzureSearchOptions))
.Bind(settings));

services.AddOptions<SearchByKeywordCriteria>()
.Configure<IConfiguration>(
(settings, configuration) =>
configuration
.GetSection(nameof(SearchByKeywordCriteria))
.Bind(settings));

services.AddScoped(typeof(ISearchServiceAdapter), typeof(CognitiveSearchServiceAdapter<DataTransferObjects.Establishment>));
services.AddSingleton(typeof(IMapper<Pageable<SearchResult<DataTransferObjects.Establishment>>, EstablishmentResults>), typeof(PageableSearchResultsToEstablishmentResultsMapper));
services.AddSingleton<IMapper<Dictionary<string, IList<Azure.Search.Documents.Models.FacetResult>>, EstablishmentFacets>, AzureFacetResultToEstablishmentFacetsMapper>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

namespace Dfe.Data.SearchPrototype.Infrastructure.Tests.Integration;

public class CognitiveSearchServiceAdaptorIntegrationTests :IClassFixture<ConfigBuilder>, IClassFixture<CompositionRootServiceProvider>
public class CompositionRootTests :IClassFixture<ConfigBuilder>, IClassFixture<CompositionRootServiceProvider>
{
private IConfiguration _configuration;
private IServiceProvider _serviceProvider;
Expand All @@ -17,7 +17,7 @@ public class CognitiveSearchServiceAdaptorIntegrationTests :IClassFixture<Config
{"AzureSearchOptions:SearchIndex", "establishments" }
};

public CognitiveSearchServiceAdaptorIntegrationTests(ConfigBuilder configBuilder, CompositionRootServiceProvider serviceProvider)
public CompositionRootTests(ConfigBuilder configBuilder, CompositionRootServiceProvider serviceProvider)
{
_configuration = configBuilder.SetupConfiguration(InMemoryConfig);
_serviceProvider = serviceProvider.SetUpServiceProvider(_configuration);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using Dfe.Data.SearchPrototype.Common.CleanArchitecture.Application.UseCase;
using Dfe.Data.SearchPrototype.SearchForEstablishments.ByKeyword.Usecase;
using Dfe.Data.SearchPrototype.SearchForEstablishments.Models;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

namespace Dfe.Data.SearchPrototype.SearchForEstablishments;
Expand All @@ -17,17 +19,25 @@ public static class CompositionRoot
/// <param name="services">
/// The originating application services onto which to register the search dependencies.
/// </param>
/// <param name="configuration">
/// The originating configuration block from which to derive use-case settings.
/// </param>
/// <exception cref="ArgumentNullException">
/// The exception thrown if no valid <see cref="IServiceCollection"/> is provisioned.
/// </exception>
public static void AddSearchForEstablishmentServices(this IServiceCollection services)
public static void AddSearchForEstablishmentServices(this IServiceCollection services, IConfiguration configuration)
{
if (services is null)
{
throw new ArgumentNullException(nameof(services),
"A service collection is required to configure the search by keyword use-case.");
}

services.AddOptions<SearchByKeywordCriteria>()
.Configure<IConfiguration>(
(settings, configuration) =>
configuration
.GetSection(nameof(SearchByKeywordCriteria))
.Bind(settings));
services.AddScoped<IUseCase<SearchByKeywordRequest, SearchByKeywordResponse>, SearchByKeywordUseCase>();
}
}
35 changes: 35 additions & 0 deletions Dfe.Data.SearchPrototype/Tests/Integration/CompositionRootTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using Dfe.Data.SearchPrototype.Common.CleanArchitecture.Application.UseCase;
using Dfe.Data.SearchPrototype.SearchForEstablishments.ByKeyword.Usecase;
using Dfe.Data.SearchPrototype.Tests.Integration.TestHarness;
using FluentAssertions;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Xunit;

namespace Dfe.Data.SearchPrototype.Tests.Integration;

public class CompositionRootTests : IClassFixture<CompositionRootServiceProvider>, IClassFixture<ConfigBuilder>
{
private Dictionary<string, string?> InMemoryConfig => new() {
{ "SearchByKeywordCriteria:SearchFields:0", "ESTABLISHMENTNAME" },
{ "SearchByKeywordCriteria:Facets:0", "FACET1"}
};
private readonly IConfiguration _config;
private readonly IServiceProvider _compositionRootServiceProvider;

public CompositionRootTests(CompositionRootServiceProvider serviceProvider, ConfigBuilder configBuilder)
{
_config = configBuilder.SetupConfiguration(InMemoryConfig);
_compositionRootServiceProvider = serviceProvider.SetUpServiceProvider(_config);
}

[Fact]
public async Task AddSearchForEstablishmentServices_CanCreateUsecase()
{
var usecase = _compositionRootServiceProvider.GetRequiredService<IUseCase<SearchByKeywordRequest, SearchByKeywordResponse>>();

var response = await usecase.HandleRequest(new SearchByKeywordRequest("searchkeyword"));

response.Should().NotBeNull();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using Dfe.Data.SearchPrototype.Infrastructure;
using Dfe.Data.SearchPrototype.Infrastructure.DataTransferObjects;
using Dfe.Data.SearchPrototype.SearchForEstablishments;
using Dfe.Data.SearchPrototype.SearchForEstablishments.ByKeyword.ServiceAdapters;
using Dfe.Data.SearchPrototype.Tests.SearchForEstablishments.ByKeyword.TestDoubles;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Moq;

namespace Dfe.Data.SearchPrototype.Tests.Integration.TestHarness;

public class CompositionRootServiceProvider
{
public IServiceProvider SetUpServiceProvider(IConfiguration config)
{
IServiceCollection services = new ServiceCollection();
services.AddSingleton<IConfiguration>(config);

services.AddSearchForEstablishmentServices(config);

services.AddScoped<ISearchServiceAdapter>(provider => SearchServiceAdapterTestDouble.MockFor(SearchResultsTestDouble.CreateWithNoResults()));
return services.BuildServiceProvider();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using Microsoft.Extensions.Configuration;

namespace Dfe.Data.SearchPrototype.Tests.Integration.TestHarness;

public class ConfigBuilder
{
public IConfiguration SetupConfiguration(Dictionary<string, string?> options)
{
var configBuilder = new ConfigurationBuilder();

configBuilder.AddInMemoryCollection(options);
return configBuilder.Build();
}
}

0 comments on commit aa30e30

Please sign in to comment.