diff --git a/Dfe.Data.Common.Infrastructure.CognitiveSearch/Filtering/LogicalOperators/Factories/ILogicalOperatorFactory.cs b/Dfe.Data.Common.Infrastructure.CognitiveSearch/Filtering/LogicalOperators/Factories/ILogicalOperatorFactory.cs index f360edc..250bb7b 100644 --- a/Dfe.Data.Common.Infrastructure.CognitiveSearch/Filtering/LogicalOperators/Factories/ILogicalOperatorFactory.cs +++ b/Dfe.Data.Common.Infrastructure.CognitiveSearch/Filtering/LogicalOperators/Factories/ILogicalOperatorFactory.cs @@ -7,17 +7,6 @@ /// public interface ILogicalOperatorFactory { - /// - /// Allows creation of an instance based on the type requested. - /// - /// - /// The concrete implementation type of requested. - /// - /// - /// The configured instance of the type. - /// - ILogicalOperator CreateLogicalOperator(Type logicalOperatorType); - /// /// Allows creation of an instance based on the type name requested. /// @@ -28,15 +17,4 @@ public interface ILogicalOperatorFactory /// The configured instance of the type. /// ILogicalOperator CreateLogicalOperator(string logicalOperatorName); - - /// - /// Allows creation of an instance based on the generic type specified. - /// - /// - /// The concrete type of requested. - /// - /// - /// The configured instance of the type. - /// - ILogicalOperator CreateLogicalOperator() where TLogicalOperator : ILogicalOperator; } diff --git a/Dfe.Data.Common.Infrastructure.CognitiveSearch/Filtering/LogicalOperators/Factories/LogicalOperatorFactory.cs b/Dfe.Data.Common.Infrastructure.CognitiveSearch/Filtering/LogicalOperators/Factories/LogicalOperatorFactory.cs index ac34542..57f0afe 100644 --- a/Dfe.Data.Common.Infrastructure.CognitiveSearch/Filtering/LogicalOperators/Factories/LogicalOperatorFactory.cs +++ b/Dfe.Data.Common.Infrastructure.CognitiveSearch/Filtering/LogicalOperators/Factories/LogicalOperatorFactory.cs @@ -41,30 +41,6 @@ public LogicalOperatorFactory(Dictionary> logical _logicalOperatorFactory = logicalOperatorFactory; } - /// - /// Allows creation of an instance based on the generic type specified. - /// - /// - /// The concrete type of requested. - /// - /// - /// The configured instance of the type. - /// - public ILogicalOperator CreateLogicalOperator() - where TLogicalOperator : ILogicalOperator => CreateLogicalOperator(typeof(TLogicalOperator)); - - /// - /// Allows creation of an instance based on the type requested. - /// - /// - /// The concrete implementation type of requested. - /// - /// - /// The configured instance of the type. - /// - public ILogicalOperator CreateLogicalOperator(Type logicalOperatorType) => CreateLogicalOperator(logicalOperatorName: logicalOperatorType.Name); - - /// /// Allows creation of an instance based on the type name requested. /// diff --git a/Dfe.Data.Common.Infrastructure.CognitiveSearch/Tests/CompositionRootTests.cs b/Dfe.Data.Common.Infrastructure.CognitiveSearch/Tests/CompositionRootTests.cs index ff9ccbe..d230be2 100644 --- a/Dfe.Data.Common.Infrastructure.CognitiveSearch/Tests/CompositionRootTests.cs +++ b/Dfe.Data.Common.Infrastructure.CognitiveSearch/Tests/CompositionRootTests.cs @@ -53,12 +53,12 @@ public async Task AddAzureSearchServices_RegistersAllDependencies() // act var response = - await searchByKeywordService + await searchByKeywordService! .SearchAsync( searchKeyword: "Test", searchIndex: "Index", searchOptions: new Azure.Search.Documents.SearchOptions() - ); + )!; response.Should().NotBeNull(); } @@ -78,7 +78,7 @@ public async Task AddAzureGeoLocationSearchServices_RegistersAllDependencies() IGeoLocationClientProvider mockGeoLocationClientProvider = GeoLocationClientProviderTestDouble - .CreateWithHttpStatusAndResponse(HttpStatusCode.OK, "{}"); + .CreateWithHttpStatusAndResponse(HttpStatusCode.OK, "{ }"); IServiceProvider? serviceProvider = _compositionRootServiceProvider? @@ -98,7 +98,7 @@ public async Task AddAzureGeoLocationSearchServices_RegistersAllDependencies() } [Fact] - public async Task AddAzureSearchFilterServices_RegistersAllDependencies() + public void AddAzureSearchFilterServices_RegistersAllDependencies() { Dictionary config = new() { { "FilterKeyToFilterExpressionMapOptions:FilterChainingLogicalOperator", "AndLogicalOperator" }, diff --git a/Dfe.Data.Common.Infrastructure.CognitiveSearch/Tests/ConfigurationStartupValidationTests.cs b/Dfe.Data.Common.Infrastructure.CognitiveSearch/Tests/ConfigurationStartupValidationTests.cs index 4a0d5f2..0d4e52f 100644 --- a/Dfe.Data.Common.Infrastructure.CognitiveSearch/Tests/ConfigurationStartupValidationTests.cs +++ b/Dfe.Data.Common.Infrastructure.CognitiveSearch/Tests/ConfigurationStartupValidationTests.cs @@ -147,7 +147,7 @@ public void AddAzureGeoLocationSearchServices_MissingGeoLocationOptions_MapsSubs } [Fact] - public async Task AddAzureSearchFilterServices_MissingFilterKeyToFilterExpressionMapOptions_FilterChainingLogicalOperator_ThrowOptionsValidationException() + public void AddAzureSearchFilterServices_MissingFilterKeyToFilterExpressionMapOptions_FilterChainingLogicalOperator_ThrowOptionsValidationException() { Dictionary config = new() { { "FilterKeyToFilterExpressionMapOptions:SearchFilterToExpressionMap:test-key:FilterExpressionKey", "SearchInFilterExpression" }, @@ -173,7 +173,7 @@ public async Task AddAzureSearchFilterServices_MissingFilterKeyToFilterExpressio } [Fact] - public async Task AddAzureSearchFilterServices_MissingFilterKeyToFilterExpressionMapOptions_SearchFilterToExpressionMap_ThrowOptionsValidationException() + public void AddAzureSearchFilterServices_MissingFilterKeyToFilterExpressionMapOptions_SearchFilterToExpressionMap_ThrowOptionsValidationException() { Dictionary config = new() { { "FilterKeyToFilterExpressionMapOptions:FilterChainingLogicalOperator", "AndLogicalOperator" } diff --git a/Dfe.Data.Common.Infrastructure.CognitiveSearch/Tests/Filtering/LogicalOperators/Factories/LogicalOperatorFactoryTests.cs b/Dfe.Data.Common.Infrastructure.CognitiveSearch/Tests/Filtering/LogicalOperators/Factories/LogicalOperatorFactoryTests.cs index 0630a80..c0ff748 100644 --- a/Dfe.Data.Common.Infrastructure.CognitiveSearch/Tests/Filtering/LogicalOperators/Factories/LogicalOperatorFactoryTests.cs +++ b/Dfe.Data.Common.Infrastructure.CognitiveSearch/Tests/Filtering/LogicalOperators/Factories/LogicalOperatorFactoryTests.cs @@ -1,6 +1,6 @@ using Dfe.Data.Common.Infrastructure.CognitiveSearch.Filtering.LogicalOperators; using Dfe.Data.Common.Infrastructure.CognitiveSearch.Filtering.LogicalOperators.Factories; -using Dfe.Data.Common.Infrastructure.CognitiveSearch.Tests.Filtering.FilterExpressions; +using Dfe.Data.Common.Infrastructure.CognitiveSearch.Tests.Filtering.TestDoubles; using FluentAssertions; using Xunit; @@ -12,12 +12,11 @@ public sealed class LogicalOperatorFactoryTests public void CreateLogicalOperator_WithValidOperatorName_ReturnsExpectedConfiguredOperatorFromfactory() { // arrange. - ILogicalOperatorFactory logicalOperatorFactory = - LogicalOperatorFactoryTestDouble.MockLogicalOperatorFactory(); + ILogicalOperatorFactory logicalOperatorFactory = new LogicalOperatorFactory(ILogicalOperators.Create()); // act. ILogicalOperator logicalOperator = - logicalOperatorFactory.CreateLogicalOperator("AndLogicalOperator"); + logicalOperatorFactory.CreateLogicalOperator("name1"); // assert. logicalOperator.Should().NotBeNull(); @@ -25,43 +24,25 @@ public void CreateLogicalOperator_WithValidOperatorName_ReturnsExpectedConfigure } [Fact] - public void CreateLogicalOperator_WithValidOperatorType_ReturnsExpectedConfiguredOperatorFromfactory() + public void CreateFilter_WithUnknownFilterName_ThrowsExpectedException() { // arrange. - ILogicalOperatorFactory logicalOperatorFactory = - LogicalOperatorFactoryTestDouble.MockLogicalOperatorFactory(); + ILogicalOperatorFactory logicalOperatorFactory = new LogicalOperatorFactory(ILogicalOperators.Create()); // act. - ILogicalOperator logicalOperator = - logicalOperatorFactory.CreateLogicalOperator(typeof(AndLogicalOperator)); - - // assert. - logicalOperator.Should().NotBeNull(); - logicalOperator.Should().BeOfType(); - } - - [Fact] - public void CreateLogicalOperator_WithValidOperatorGenricTemplateType_ReturnsExpectedConfiguredOperatorFromfactory() - { - // arrange. - ILogicalOperatorFactory logicalOperatorFactory = - LogicalOperatorFactoryTestDouble.MockLogicalOperatorFactoryFor(); - - // act. - ILogicalOperator logicalOperator = - logicalOperatorFactory.CreateLogicalOperator(); + Action failedCreateOperatorAction = () => + logicalOperatorFactory.CreateLogicalOperator(logicalOperatorName: "something else"); - // assert. - logicalOperator.Should().NotBeNull(); - logicalOperator.Should().BeOfType(); + //assert + ArgumentException exception = + Assert.Throws(failedCreateOperatorAction); } [Fact] public void CreateFilter_WithNullFilterName_ThrowsExpectedException() { // arrange. - ILogicalOperatorFactory logicalOperatorFactory = - LogicalOperatorFactoryTestDouble.MockLogicalOperatorFactory(); + ILogicalOperatorFactory logicalOperatorFactory = new LogicalOperatorFactory(ILogicalOperators.Create()); // act. Action failedCreateOperatorAction = () => @@ -78,9 +59,7 @@ public void CreateFilter_WithNullFilterName_ThrowsExpectedException() public void CreateFilter_WithEmptyFilterName_ThrowsExpectedException() { // arrange. - ILogicalOperatorFactory logicalOperatorFactory = - LogicalOperatorFactoryTestDouble.MockLogicalOperatorFactory(); - + ILogicalOperatorFactory logicalOperatorFactory = new LogicalOperatorFactory(ILogicalOperators.Create()); // act. Action failedCreateOperatorAction = () => logicalOperatorFactory.CreateLogicalOperator(logicalOperatorName: ""); @@ -96,8 +75,7 @@ public void CreateFilter_WithEmptyFilterName_ThrowsExpectedException() public void CreateFilter_WithWhitespaceFilterName_ThrowsExpectedException() { // arrange. - ILogicalOperatorFactory logicalOperatorFactory = - LogicalOperatorFactoryTestDouble.MockLogicalOperatorFactory(); + ILogicalOperatorFactory logicalOperatorFactory = new LogicalOperatorFactory(ILogicalOperators.Create()); // act. Action failedCreateOperatorAction = () => diff --git a/Dfe.Data.Common.Infrastructure.CognitiveSearch/Tests/Filtering/TestDoubles/ILogicalOperatorMock.cs b/Dfe.Data.Common.Infrastructure.CognitiveSearch/Tests/Filtering/TestDoubles/ILogicalOperatorMock.cs new file mode 100644 index 0000000..a042090 --- /dev/null +++ b/Dfe.Data.Common.Infrastructure.CognitiveSearch/Tests/Filtering/TestDoubles/ILogicalOperatorMock.cs @@ -0,0 +1,13 @@ +using Dfe.Data.Common.Infrastructure.CognitiveSearch.Filtering.LogicalOperators; + +namespace Dfe.Data.Common.Infrastructure.CognitiveSearch.Tests.Filtering.TestDoubles; + +public static class ILogicalOperators +{ + public static Dictionary> Create() + { + return new() { + { "name1", () => new AndLogicalOperator()} + }; + } +} diff --git a/Dfe.Data.Common.Infrastructure.CognitiveSearch/Tests/Filtering/TestDoubles/LogicalOperatorFactoryTestDouble.cs b/Dfe.Data.Common.Infrastructure.CognitiveSearch/Tests/Filtering/TestDoubles/LogicalOperatorFactoryTestDouble.cs index 072ef80..9adfdad 100644 --- a/Dfe.Data.Common.Infrastructure.CognitiveSearch/Tests/Filtering/TestDoubles/LogicalOperatorFactoryTestDouble.cs +++ b/Dfe.Data.Common.Infrastructure.CognitiveSearch/Tests/Filtering/TestDoubles/LogicalOperatorFactoryTestDouble.cs @@ -21,28 +21,6 @@ public static ILogicalOperatorFactory MockLogicalOperatorFactoryFor() .Returns((string type) => GetLogicalOperator(type)); - logicalOperatorFactoryMock - .Setup(logicalOperatorFactory => - logicalOperatorFactory - .CreateLogicalOperator(It.IsAny())) - .Returns((Type type) => - GetLogicalOperator(type.Name)); - - return logicalOperatorFactoryMock.Object; - } - - public static ILogicalOperatorFactory MockLogicalOperatorFactoryFor() - { - var logicalOperatorFactoryMock = LogicalOperatorFactoryMock(); - - logicalOperatorFactoryMock - .Setup(logicalOperatorFactory => - logicalOperatorFactory - .CreateLogicalOperator()) - .Returns(() => - GetLogicalOperator( - typeof(TFilterType).Name)); - return logicalOperatorFactoryMock.Object; }