diff --git a/src/Childrens-Social-Care-CPD-Indexer.Tests/Core/ResourcesIndexerTest.cs b/src/Childrens-Social-Care-CPD-Indexer.Tests/Core/ResourcesIndexerTest.cs index e67dd66..fd27f3e 100644 --- a/src/Childrens-Social-Care-CPD-Indexer.Tests/Core/ResourcesIndexerTest.cs +++ b/src/Childrens-Social-Care-CPD-Indexer.Tests/Core/ResourcesIndexerTest.cs @@ -30,8 +30,7 @@ public async Task DeleteIndexAsync_Skips_Deletion_If_Index_Does_Not_Exist() // arrange var response = Substitute.For>(); response.HasValue.Returns(false); - _client.GetIndexAsync(Arg.Any(), Arg.Any()) - .Returns(Task.FromResult(response)); + _client.GetIndexAsync(Arg.Any(), Arg.Any()).Returns(Task.FromResult(response)); // act await _sut.DeleteIndexAsync("foo"); @@ -46,8 +45,7 @@ public async Task DeleteIndexAsync_Deletes_The_Index() // arrange var response = Substitute.For>(); response.HasValue.Returns(true); - _client.GetIndexAsync(Arg.Any(), Arg.Any()) - .Returns(Task.FromResult(response)); + _client.GetIndexAsync(Arg.Any(), Arg.Any()).Returns(Task.FromResult(response)); // act await _sut.DeleteIndexAsync("foo"); @@ -65,10 +63,8 @@ public async Task DeleteIndexAsync_Logs_Failure_To_Delete_Index() var deleteIndexResult = Substitute.For(); deleteIndexResult.IsError.Returns(true); - _client.GetIndexAsync(Arg.Any(), Arg.Any()) - .Returns(Task.FromResult(getIndexResult)); - _client.DeleteIndexAsync(Arg.Any(), Arg.Any()) - .Returns(Task.FromResult(deleteIndexResult)); + _client.GetIndexAsync(Arg.Any(), Arg.Any()).Returns(Task.FromResult(getIndexResult)); + _client.DeleteIndexAsync(Arg.Any(), Arg.Any()).Returns(Task.FromResult(deleteIndexResult)); // act await _sut.DeleteIndexAsync("foo"); @@ -81,6 +77,10 @@ public async Task DeleteIndexAsync_Logs_Failure_To_Delete_Index() public async Task CreateIndexAsync_Creates_The_Index() { // arrange + var getIndexResult = Substitute.For>(); + getIndexResult.HasValue.Returns(false); + _client.GetIndexAsync(Arg.Any(), Arg.Any()).Returns(Task.FromResult(getIndexResult)); + SearchIndex? searchIndex = null; await _client.CreateIndexAsync(Arg.Do(x => searchIndex = x), Arg.Any()); @@ -92,7 +92,23 @@ public async Task CreateIndexAsync_Creates_The_Index() searchIndex.Should().NotBeNull(); searchIndex!.Name.Should().Be("foo"); } - + + [Test] + public async Task CreateIndexAsync_Skips_Index_Creation() + { + // arrange + var getIndexResult = Substitute.For>(); + getIndexResult.HasValue.Returns(true); + _client.GetIndexAsync(Arg.Any(), Arg.Any()).Returns(Task.FromResult(getIndexResult)); + await _client.CreateIndexAsync(Arg.Any(), Arg.Any()); + + // act + await _sut.CreateIndexAsync("foo"); + + // assert + await _client.Received(0).CreateIndexAsync(Arg.Any(), Arg.Any()); + } + [Test] public async Task PopulateIndexAsync_Uploads_Documents() { diff --git a/src/Childrens-Social-Care-CPD-Indexer/Core/ResourcesIndexer.cs b/src/Childrens-Social-Care-CPD-Indexer/Core/ResourcesIndexer.cs index 17a671e..1c3124d 100644 --- a/src/Childrens-Social-Care-CPD-Indexer/Core/ResourcesIndexer.cs +++ b/src/Childrens-Social-Care-CPD-Indexer/Core/ResourcesIndexer.cs @@ -7,6 +7,13 @@ internal class ResourcesIndexer(SearchIndexClient searchIndexClient, IDocumentFe { public async Task CreateIndexAsync(string indexName, CancellationToken cancellationToken = default) { + var index = await searchIndexClient.GetIndexAsync(indexName, cancellationToken); + if (index.HasValue) + { + logger.LogInformation("Index already exists, skipping creation."); + return; + } + logger.LogInformation("Creating index..."); var fieldBuilder = new FieldBuilder(); var searchFields = fieldBuilder.Build(typeof(CpdDocument)); @@ -18,15 +25,10 @@ public async Task CreateIndexAsync(string indexName, CancellationToken cancellat public async Task DeleteIndexAsync(string indexName, CancellationToken cancellationToken = default) { logger.LogInformation("Deleting index..."); - var index = await searchIndexClient.GetIndexAsync(indexName, cancellationToken); - cancellationToken.ThrowIfCancellationRequested(); - if (index.HasValue) { var deleteResponse = await searchIndexClient.DeleteIndexAsync(indexName, cancellationToken); - cancellationToken.ThrowIfCancellationRequested(); - if (deleteResponse.IsError) { logger.LogError("Failed to delete the index"); diff --git a/src/Childrens-Social-Care-CPD-Indexer/IndexingService.cs b/src/Childrens-Social-Care-CPD-Indexer/IndexingService.cs index ce41d3b..b6b3a8c 100644 --- a/src/Childrens-Social-Care-CPD-Indexer/IndexingService.cs +++ b/src/Childrens-Social-Care-CPD-Indexer/IndexingService.cs @@ -14,9 +14,9 @@ public async Task StartAsync(CancellationToken cancellationToken) if (config.RecreateIndex) { await resourcesIndexer.DeleteIndexAsync(config.IndexName, cancellationToken); - await resourcesIndexer.CreateIndexAsync(config.IndexName, cancellationToken); } + await resourcesIndexer.CreateIndexAsync(config.IndexName, cancellationToken); await resourcesIndexer.PopulateIndexAsync(config.IndexName, config.BatchSize, cancellationToken); }