From 9fda556807fe6844c47b759c81a37fd0b71e30e4 Mon Sep 17 00:00:00 2001 From: ssinno28 Date: Tue, 21 Nov 2023 15:27:03 -0500 Subject: [PATCH] adding tests for middleware adding update method for multiple content items --- .../Controllers/BlogPostController.cs | 26 ++++++ .../Lucene.Net.IndexProvider.Tests.csproj | 2 + .../MiddlewareTests.cs | 92 +++++++++++++++++++ .../Interfaces/IIndexProvider.cs | 1 + .../Lucene.Net.IndexProvider.csproj | 2 +- .../LuceneIndexProvider.cs | 20 +++- 6 files changed, 141 insertions(+), 2 deletions(-) create mode 100644 Lucene.Net.IndexProvider.Tests/Controllers/BlogPostController.cs create mode 100644 Lucene.Net.IndexProvider.Tests/MiddlewareTests.cs diff --git a/Lucene.Net.IndexProvider.Tests/Controllers/BlogPostController.cs b/Lucene.Net.IndexProvider.Tests/Controllers/BlogPostController.cs new file mode 100644 index 0000000..6532dda --- /dev/null +++ b/Lucene.Net.IndexProvider.Tests/Controllers/BlogPostController.cs @@ -0,0 +1,26 @@ +using System.Collections.Generic; +using System.Threading.Tasks; +using Lucene.Net.IndexProvider.Interfaces; +using Lucene.Net.IndexProvider.Tests.Models; +using Microsoft.AspNetCore.Mvc; + +namespace Lucene.Net.IndexProvider.Tests.Controllers; + +[ApiController] +[Route("[controller]")] +public class BlogPostController : ControllerBase +{ + private readonly IIndexProvider _indexProvider; + + public BlogPostController(IIndexProvider indexProvider) + { + _indexProvider = indexProvider; + } + + [HttpPost] + public async Task Post(BlogPost dto) + { + await _indexProvider.Store(new List() { dto }, nameof(BlogPost)); + return dto; + } +} \ No newline at end of file diff --git a/Lucene.Net.IndexProvider.Tests/Lucene.Net.IndexProvider.Tests.csproj b/Lucene.Net.IndexProvider.Tests/Lucene.Net.IndexProvider.Tests.csproj index 725356b..6f6be19 100644 --- a/Lucene.Net.IndexProvider.Tests/Lucene.Net.IndexProvider.Tests.csproj +++ b/Lucene.Net.IndexProvider.Tests/Lucene.Net.IndexProvider.Tests.csproj @@ -13,6 +13,7 @@ + all runtime; build; native; contentfiles; analyzers; buildtransitive @@ -21,6 +22,7 @@ all runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/Lucene.Net.IndexProvider.Tests/MiddlewareTests.cs b/Lucene.Net.IndexProvider.Tests/MiddlewareTests.cs new file mode 100644 index 0000000..f2a7def --- /dev/null +++ b/Lucene.Net.IndexProvider.Tests/MiddlewareTests.cs @@ -0,0 +1,92 @@ +using System.Collections.Generic; +using System.IO; +using System.Net.Http; +using System.Reflection; +using System.Text; +using System.Text.Json; +using System.Threading.Tasks; +using AutoFixture; +using Lucene.Net.DocumentMapper.Helpers; +using Lucene.Net.IndexProvider.Helpers; +using Lucene.Net.IndexProvider.Interfaces; +using Lucene.Net.IndexProvider.Middleware; +using Lucene.Net.IndexProvider.Models; +using Lucene.Net.IndexProvider.Tests.Models; +using Lucene.Net.Util; +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore.TestHost; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; +using Moq; +using Xunit; + +namespace Lucene.Net.IndexProvider.Tests; + +public class MiddlewareTests +{ + private readonly TestServer _testServer; + private readonly IFixture _fixture = new Fixture(); + private string _settingsPath; + private string _indexPath; + private Mock _mockLocalIndexPathFactory; + private IIndexProvider _indexProvider; + + public MiddlewareTests() + { + _settingsPath = Path.GetFullPath(Path.Combine($"{Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)}", @"..\..\..\settings")); + _indexPath = $"{_settingsPath}\\PersonalBlog\\index"; + _mockLocalIndexPathFactory = new Mock(); + _mockLocalIndexPathFactory.Setup(x => x.GetLocalIndexPath()) + .Returns(_indexPath); + + var host = new WebHostBuilder() + .ConfigureServices(services => + { + services + .AddLuceneDocumentMapper() + .AddLuceneProvider() + .AddRouting() + .AddLogging(x => x.AddConsole()) + .AddHttpContextAccessor() + .AddControllers(); + + services.Add(new ServiceDescriptor(typeof(ILocalIndexPathFactory), _mockLocalIndexPathFactory.Object)); + }) + .Configure(app => + { + var configManager = app.ApplicationServices.GetService(); + configManager.AddConfiguration(new LuceneConfig() + { + Indexes = new[] { nameof(BlogPost) }, + BatchSize = 50000, + LuceneVersion = LuceneVersion.LUCENE_48 + }); + + _indexProvider = app.ApplicationServices.GetService(); + + app.UseMiddleware(); + app.UseRouting(); + app.UseEndpoints(endpoints => { endpoints.MapControllers(); }); + }); + + _testServer = new TestServer(host); + } + + [Fact] + public async Task BlogPostApi_Post_ProperlyClosesSession() + { + var blogPostDto = _fixture.Create(); + + var payload = JsonSerializer.Serialize(blogPostDto); + var testClient = _testServer.CreateClient(); + var response = + await testClient.PostAsync("/blogpost", new StringContent(payload, Encoding.UTF8, "application/json")); + + var sessionManager = _testServer.Services.GetService(); + Assert.Equal(0, sessionManager.ContextSessions.Count); + + var blogPost = _indexProvider.GetDocumentById(typeof(BlogPost), blogPostDto.Id); + Assert.NotNull(blogPost.Hit); + } +} \ No newline at end of file diff --git a/Lucene.Net.IndexProvider/Interfaces/IIndexProvider.cs b/Lucene.Net.IndexProvider/Interfaces/IIndexProvider.cs index a1966c0..7d38a85 100644 --- a/Lucene.Net.IndexProvider/Interfaces/IIndexProvider.cs +++ b/Lucene.Net.IndexProvider/Interfaces/IIndexProvider.cs @@ -17,6 +17,7 @@ public interface IIndexProvider Task Delete(Type contentType, string documentId); Task Delete(string documentId); Task Update(object contentItem, string id); + Task Update(IList contentItems); IndexResult GetDocumentById(Type contentType, string id); IndexResult GetDocumentById(string id); Task CheckHealth(); diff --git a/Lucene.Net.IndexProvider/Lucene.Net.IndexProvider.csproj b/Lucene.Net.IndexProvider/Lucene.Net.IndexProvider.csproj index 70f5dd6..d8ac43c 100644 --- a/Lucene.Net.IndexProvider/Lucene.Net.IndexProvider.csproj +++ b/Lucene.Net.IndexProvider/Lucene.Net.IndexProvider.csproj @@ -19,7 +19,7 @@ - 1.0.25 + 1.0.26 https://github.com/ssinno28/Lucene.Net.IndexProvider git A simple service that helps to abstract common operations when interacting with lucene.net indexes. diff --git a/Lucene.Net.IndexProvider/LuceneIndexProvider.cs b/Lucene.Net.IndexProvider/LuceneIndexProvider.cs index 0840867..02c1a67 100644 --- a/Lucene.Net.IndexProvider/LuceneIndexProvider.cs +++ b/Lucene.Net.IndexProvider/LuceneIndexProvider.cs @@ -38,7 +38,7 @@ public LuceneIndexProvider( IDocumentMapper mapper, ILoggerFactory loggerFactory, ILocalIndexPathFactory localIndexPathFactory, - IIndexSessionManager sessionManager, + IIndexSessionManager sessionManager, IIndexConfigurationManager configurationManager) { _mapper = mapper; @@ -313,6 +313,24 @@ public async Task GetByFilters(IList filters, ILis }); } + public async Task Update(IList contentItems) + { + foreach (var contentItem in contentItems) + { + var key = GetKeyName(contentItem.GetType()); + var idPropInfo = contentItem.GetType().GetProperties().First(x => x.Name.Equals(key)); + var id = idPropInfo.GetValue(contentItem).ToString(); + + var result = await Update(contentItem, id); + if (!result) + { + return false; + } + } + + return true; + } + /// /// Updates a single document based on the key supplied ///