Skip to content

Commit

Permalink
- add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
psmagin committed Aug 2, 2024
1 parent a372753 commit 0eb7004
Show file tree
Hide file tree
Showing 7 changed files with 131 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -15,7 +15,7 @@
@RequiredArgsConstructor
public class ReindexKafkaListener {

private final ReindexService reindexService;
private final ReindexOrchestrationService reindexService;
private final ConsortiumTenantExecutor executionService;
private final SystemUserScopedExecutionService systemUserScopedExecutionService;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

@Service
@RequiredArgsConstructor
public class ReindexService {
public class ReindexOrchestrationService {

private final ReindexRangeIndexService rangeIndexService;
private final PrimaryResourceRepository elasticRepository;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -16,9 +13,6 @@
@Repository
public class SubjectJdbcRepository extends ReindexJdbcRepository {

public static final TypeReference<List<Map<String, Object>>> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -62,7 +62,7 @@ class ReindexKafkaListenerIT {
@MockBean
private SystemUserScopedExecutionService systemUserScopedExecutionService;
@MockBean
private ReindexService reindexService;
private ReindexOrchestrationService reindexService;
@Autowired
private KafkaTemplate<String, ReindexRangeIndexEvent> kafkaTemplate;

Expand Down
Original file line number Diff line number Diff line change
@@ -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());
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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));

Expand All @@ -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<String, Object> 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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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"));
}
}

0 comments on commit 0eb7004

Please sign in to comment.