From 0eb7004b4fd4408c315884cea83f43164875c2fe Mon Sep 17 00:00:00 2001 From: psmagin Date: Fri, 2 Aug 2024 10:46:48 +0300 Subject: [PATCH] - add tests --- .../integration/ReindexKafkaListener.java | 4 +- ....java => ReindexOrchestrationService.java} | 2 +- .../reindex/jdbc/SubjectJdbcRepository.java | 6 -- .../integration/ReindexKafkaListenerIT.java | 4 +- .../ReindexOrchestrationServiceTest.java | 85 +++++++++++++++++++ .../reindex/ReindexRangeIndexServiceTest.java | 28 +++++- .../reindex/jdbc/SubjectJdbcRepositoryIT.java | 15 ++++ 7 files changed, 131 insertions(+), 13 deletions(-) rename src/main/java/org/folio/search/service/reindex/{ReindexService.java => ReindexOrchestrationService.java} (96%) create mode 100644 src/test/java/org/folio/search/service/reindex/ReindexOrchestrationServiceTest.java diff --git a/src/main/java/org/folio/search/integration/ReindexKafkaListener.java b/src/main/java/org/folio/search/integration/ReindexKafkaListener.java index 2692ab65f..3849c562f 100644 --- a/src/main/java/org/folio/search/integration/ReindexKafkaListener.java +++ b/src/main/java/org/folio/search/integration/ReindexKafkaListener.java @@ -4,7 +4,7 @@ import lombok.extern.log4j.Log4j2; import org.folio.search.model.event.ReindexRangeIndexEvent; import org.folio.search.service.consortium.ConsortiumTenantExecutor; -import org.folio.search.service.reindex.ReindexService; +import org.folio.search.service.reindex.ReindexOrchestrationService; import org.folio.search.utils.KafkaConstants; import org.folio.spring.service.SystemUserScopedExecutionService; import org.springframework.kafka.annotation.KafkaListener; @@ -15,7 +15,7 @@ @RequiredArgsConstructor public class ReindexKafkaListener { - private final ReindexService reindexService; + private final ReindexOrchestrationService reindexService; private final ConsortiumTenantExecutor executionService; private final SystemUserScopedExecutionService systemUserScopedExecutionService; diff --git a/src/main/java/org/folio/search/service/reindex/ReindexService.java b/src/main/java/org/folio/search/service/reindex/ReindexOrchestrationService.java similarity index 96% rename from src/main/java/org/folio/search/service/reindex/ReindexService.java rename to src/main/java/org/folio/search/service/reindex/ReindexOrchestrationService.java index d6f0d79d8..cd8101e3b 100644 --- a/src/main/java/org/folio/search/service/reindex/ReindexService.java +++ b/src/main/java/org/folio/search/service/reindex/ReindexOrchestrationService.java @@ -11,7 +11,7 @@ @Service @RequiredArgsConstructor -public class ReindexService { +public class ReindexOrchestrationService { private final ReindexRangeIndexService rangeIndexService; private final PrimaryResourceRepository elasticRepository; diff --git a/src/main/java/org/folio/search/service/reindex/jdbc/SubjectJdbcRepository.java b/src/main/java/org/folio/search/service/reindex/jdbc/SubjectJdbcRepository.java index 1ee7b8d66..81f540c5f 100644 --- a/src/main/java/org/folio/search/service/reindex/jdbc/SubjectJdbcRepository.java +++ b/src/main/java/org/folio/search/service/reindex/jdbc/SubjectJdbcRepository.java @@ -1,9 +1,6 @@ package org.folio.search.service.reindex.jdbc; -import com.fasterxml.jackson.core.type.TypeReference; -import com.fasterxml.jackson.databind.ObjectMapper; import java.util.HashMap; -import java.util.List; import java.util.Map; import org.folio.search.configuration.properties.ReindexConfigurationProperties; import org.folio.search.model.types.ReindexEntityType; @@ -16,9 +13,6 @@ @Repository public class SubjectJdbcRepository extends ReindexJdbcRepository { - public static final TypeReference>> VALUE_TYPE_REF = new TypeReference<>() { }; - private final ObjectMapper objectMapper = new ObjectMapper(); - protected SubjectJdbcRepository(JdbcTemplate jdbcTemplate, JsonConverter jsonConverter, FolioExecutionContext context, ReindexConfigurationProperties reindexConfigurationProperties) { super(jdbcTemplate, jsonConverter, context, reindexConfigurationProperties); diff --git a/src/test/java/org/folio/search/integration/ReindexKafkaListenerIT.java b/src/test/java/org/folio/search/integration/ReindexKafkaListenerIT.java index 7f7941220..f3b5aada7 100644 --- a/src/test/java/org/folio/search/integration/ReindexKafkaListenerIT.java +++ b/src/test/java/org/folio/search/integration/ReindexKafkaListenerIT.java @@ -25,7 +25,7 @@ import org.folio.search.configuration.kafka.ReindexRangeIndexEventKafkaConfiguration; import org.folio.search.model.event.ReindexRangeIndexEvent; import org.folio.search.service.consortium.ConsortiumTenantExecutor; -import org.folio.search.service.reindex.ReindexService; +import org.folio.search.service.reindex.ReindexOrchestrationService; import org.folio.spring.DefaultFolioExecutionContext; import org.folio.spring.FolioExecutionContext; import org.folio.spring.service.SystemUserScopedExecutionService; @@ -62,7 +62,7 @@ class ReindexKafkaListenerIT { @MockBean private SystemUserScopedExecutionService systemUserScopedExecutionService; @MockBean - private ReindexService reindexService; + private ReindexOrchestrationService reindexService; @Autowired private KafkaTemplate kafkaTemplate; diff --git a/src/test/java/org/folio/search/service/reindex/ReindexOrchestrationServiceTest.java b/src/test/java/org/folio/search/service/reindex/ReindexOrchestrationServiceTest.java new file mode 100644 index 000000000..649bbaae4 --- /dev/null +++ b/src/test/java/org/folio/search/service/reindex/ReindexOrchestrationServiceTest.java @@ -0,0 +1,85 @@ +package org.folio.search.service.reindex; + +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import java.util.List; +import java.util.Map; +import org.folio.search.domain.dto.FolioIndexOperationResponse; +import org.folio.search.domain.dto.ResourceEvent; +import org.folio.search.exception.ReindexException; +import org.folio.search.model.event.ReindexRangeIndexEvent; +import org.folio.search.model.index.SearchDocumentBody; +import org.folio.search.model.types.IndexActionType; +import org.folio.search.model.types.IndexingDataFormat; +import org.folio.search.repository.PrimaryResourceRepository; +import org.folio.search.service.converter.MultiTenantSearchDocumentConverter; +import org.folio.spring.testing.type.UnitTest; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; + +@UnitTest +@ExtendWith(MockitoExtension.class) +class ReindexOrchestrationServiceTest { + + @Mock + private ReindexRangeIndexService rangeIndexService; + @Mock + private PrimaryResourceRepository elasticRepository; + @Mock + private MultiTenantSearchDocumentConverter documentConverter; + + @InjectMocks + private ReindexOrchestrationService service; + + @Test + void process_shouldProcessSuccessfully() { + // Arrange + ReindexRangeIndexEvent event = new ReindexRangeIndexEvent(); + ResourceEvent resourceEvent = new ResourceEvent(); + FolioIndexOperationResponse folioIndexOperationResponse = + new FolioIndexOperationResponse().status(FolioIndexOperationResponse.StatusEnum.SUCCESS); + + when(rangeIndexService.fetchRecordRange(event)).thenReturn(List.of(resourceEvent)); + when(documentConverter.convert(List.of(resourceEvent))).thenReturn(Map.of("key", List.of(SearchDocumentBody.of(null, + IndexingDataFormat.JSON, resourceEvent, IndexActionType.INDEX)))); + when(elasticRepository.indexResources(any())).thenReturn(folioIndexOperationResponse); + + // Act + boolean result = service.process(event); + + // Assert + assertTrue(result); + verify(rangeIndexService).fetchRecordRange(event); + verify(documentConverter).convert(List.of(resourceEvent)); + verify(elasticRepository).indexResources(any()); + } + + @Test + void process_shouldThrowReindexException_whenElasticSearchReportsError() { + // Arrange + ReindexRangeIndexEvent event = new ReindexRangeIndexEvent(); + ResourceEvent resourceEvent = new ResourceEvent(); + FolioIndexOperationResponse folioIndexOperationResponse = new FolioIndexOperationResponse() + .status(FolioIndexOperationResponse.StatusEnum.ERROR) + .errorMessage("Error occurred during indexing."); + + when(rangeIndexService.fetchRecordRange(event)).thenReturn(List.of(resourceEvent)); + when(documentConverter.convert(List.of(resourceEvent))).thenReturn(Map.of("key", List.of(SearchDocumentBody.of(null, + IndexingDataFormat.JSON, resourceEvent, IndexActionType.INDEX)))); + when(elasticRepository.indexResources(any())).thenReturn(folioIndexOperationResponse); + + // Act & Assert + assertThrows(ReindexException.class, () -> service.process(event)); + + verify(rangeIndexService).fetchRecordRange(event); + verify(documentConverter).convert(List.of(resourceEvent)); + verify(elasticRepository).indexResources(any()); + } +} diff --git a/src/test/java/org/folio/search/service/reindex/ReindexRangeIndexServiceTest.java b/src/test/java/org/folio/search/service/reindex/ReindexRangeIndexServiceTest.java index 80fdd209b..473cc6dde 100644 --- a/src/test/java/org/folio/search/service/reindex/ReindexRangeIndexServiceTest.java +++ b/src/test/java/org/folio/search/service/reindex/ReindexRangeIndexServiceTest.java @@ -1,12 +1,17 @@ package org.folio.search.service.reindex; import static org.assertj.core.api.Assertions.assertThat; +import static org.folio.search.utils.SearchUtils.INSTANCE_RESOURCE; +import static org.folio.search.utils.TestConstants.TENANT_ID; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; import java.util.List; +import java.util.Map; +import java.util.UUID; import org.assertj.core.groups.Tuple; +import org.folio.search.domain.dto.ResourceEvent; import org.folio.search.model.event.ReindexRangeIndexEvent; import org.folio.search.model.reindex.UploadRangeEntity; import org.folio.search.model.types.ReindexEntityType; @@ -37,7 +42,7 @@ void setUp() { } @Test - void prepareAndSendIndexRanges_test(@Random UploadRangeEntity uploadRange) { + void prepareAndSendIndexRanges_positive(@Random UploadRangeEntity uploadRange) { // arrange when(repository.getUploadRanges(true)).thenReturn(List.of(uploadRange)); @@ -57,10 +62,29 @@ void prepareAndSendIndexRanges_test(@Random UploadRangeEntity uploadRange) { } @Test - void prepareAndSendIndexRanges_should_throw_exception_for_unknown_entity() { + void prepareAndSendIndexRanges_negative_shouldThrowExceptionForUnknownEntity() { // assert assertThrows(UnsupportedOperationException.class, () -> service.prepareAndSendIndexRanges(ReindexEntityType.SUBJECT)); } + @Test + void fetchRecordRange_positive() { + var indexEvent = new ReindexRangeIndexEvent(); + indexEvent.setId(UUID.randomUUID()); + indexEvent.setEntityType(ReindexEntityType.INSTANCE); + indexEvent.setTenant(TENANT_ID); + indexEvent.setLimit(10); + indexEvent.setOffset(50); + + Map mockRecord = Map.of("key", "val"); + when(repository.fetchBy(10, 50)).thenReturn(List.of(mockRecord)); + + var actual = service.fetchRecordRange(indexEvent); + + assertThat(actual) + .hasSize(1) + .extracting(ResourceEvent::getTenant, ResourceEvent::getNew, ResourceEvent::getResourceName) + .containsExactly(Tuple.tuple(TENANT_ID, mockRecord, INSTANCE_RESOURCE)); + } } diff --git a/src/test/java/org/folio/search/service/reindex/jdbc/SubjectJdbcRepositoryIT.java b/src/test/java/org/folio/search/service/reindex/jdbc/SubjectJdbcRepositoryIT.java index 6a429cec2..1ddbbf8f2 100644 --- a/src/test/java/org/folio/search/service/reindex/jdbc/SubjectJdbcRepositoryIT.java +++ b/src/test/java/org/folio/search/service/reindex/jdbc/SubjectJdbcRepositoryIT.java @@ -6,6 +6,7 @@ import static org.mockito.Mockito.when; import com.fasterxml.jackson.databind.ObjectMapper; +import java.util.List; import org.assertj.core.api.Condition; import org.folio.search.configuration.properties.ReindexConfigurationProperties; import org.folio.search.model.reindex.UploadRangeEntity; @@ -81,4 +82,18 @@ void getUploadRanges_returnList_whenNoUploadRangesAndNotPopulate() { .extracting(UploadRangeEntity::getLimit, UploadRangeEntity::getOffset) .containsExactly(tuple(5, 0), tuple(5, 5), tuple(5, 10), tuple(5, 15), tuple(1, 20)); } + + @Test + @Sql("/sql/populate-subjects.sql") + void fetchBy_returnListOfMaps() { + // act + var ranges = repository.fetchBy(10, 19); + + // assert + assertThat(ranges) + .hasSize(2) + .allMatch(map -> map.keySet().containsAll(List.of("id", "value", "authorityId", "instances"))) + .extracting("value", "authorityId") + .containsExactly(tuple("Alternative History", null), tuple("History", "79144653-7a98-4dfb-aa6a-13ad49e80952")); + } }