Skip to content

Commit

Permalink
- fix truncation + unit-tests-part1
Browse files Browse the repository at this point in the history
  • Loading branch information
psmagin committed Sep 4, 2024
1 parent 9f3dac3 commit 04d6ea0
Show file tree
Hide file tree
Showing 12 changed files with 80 additions and 112 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import org.folio.search.model.types.ReindexEntityType;
import org.folio.search.service.reindex.jdbc.MergeRangeRepository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Log4j2
@Service
Expand All @@ -37,14 +36,6 @@ public ReindexMergeRangeIndexService(List<MergeRangeRepository> repositories,
this.reindexConfig = reindexConfig;
}

@Transactional
public void deleteAllRangeRecords() {
for (ReindexEntityType entityType : ReindexEntityType.values()) {
repositories.get(entityType).truncate();
}
repositories.get(ReindexEntityType.INSTANCE).truncateMergeRanges();
}

public void saveMergeRanges(List<MergeRangeEntity> ranges) {
repositories.values().iterator().next().saveMergeRanges(ranges);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public CompletableFuture<Void> submitFullReindex(String tenantId) {
recreateIndex(reindexEntityType, tenantId);
}

mergeRangeService.deleteAllRangeRecords();
uploadRangeService.deleteAllRecords();
statusService.recreateMergeStatusRecords();

var future = CompletableFuture.runAsync(() -> {
Expand Down Expand Up @@ -129,7 +129,7 @@ private void recreateIndex(ReindexEntityType reindexEntityType, String tenantId)
indexService.dropIndex(resourceType, tenantId);
indexService.createIndex(resourceType, tenantId);
} catch (Exception e) {
log.warn("Index cannot be recreated for resource=%s".formatted(reindexEntityType), e);
log.warn("Index cannot be recreated for resource={}, message={}", reindexEntityType, e.getMessage());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.folio.search.service.reindex.jdbc.UploadRangeRepository;
import org.folio.spring.tools.kafka.FolioMessageProducer;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class ReindexUploadRangeIndexService {
Expand Down Expand Up @@ -61,6 +62,14 @@ public void updateFinishDate(ReindexRangeIndexEvent event) {
repository.setIndexRangeFinishDate(event.getId(), Timestamp.from(Instant.now()));
}


@Transactional
public void deleteAllRecords() {
for (ReindexEntityType entityType : ReindexEntityType.values()) {
repositories.get(entityType).truncate();
}
}

private List<ReindexRangeIndexEvent> prepareEvents(List<UploadRangeEntity> uploadRanges) {
return uploadRanges.stream()
.map(range -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,12 @@
import org.folio.spring.FolioExecutionContext;
import org.folio.spring.testing.type.UnitTest;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
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;

@Disabled //TODO fix
@UnitTest
@ExtendWith(MockitoExtension.class)
class ResourceFetchServiceTest {
Expand Down
30 changes: 2 additions & 28 deletions src/test/java/org/folio/search/service/ResourceServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,8 @@
import static org.folio.search.model.types.ResourceType.INSTANCE;
import static org.folio.search.utils.SearchResponseHelper.getErrorIndexOperationResponse;
import static org.folio.search.utils.SearchResponseHelper.getSuccessIndexOperationResponse;
import static org.folio.search.utils.TestConstants.INDEX_NAME;
import static org.folio.search.utils.TestConstants.RESOURCE_ID;
import static org.folio.search.utils.TestConstants.RESOURCE_ID_SECOND;
import static org.folio.search.utils.TestConstants.TENANT_ID;
import static org.folio.search.utils.TestConstants.indexName;
import static org.folio.search.utils.TestUtils.asJsonString;
import static org.folio.search.utils.TestUtils.mapOf;
import static org.folio.search.utils.TestUtils.randomId;
Expand Down Expand Up @@ -94,7 +91,7 @@ public void setUp() {
.thenAnswer(invocation -> ((Callable<?>) invocation.getArgument(1)).call());
lenient().when(indexNameProvider.getIndexName(any(ResourceEvent.class)))
.thenAnswer(invocation -> SearchUtils.getIndexName((ResourceEvent) invocation.getArgument(0)));
lenient().when(instanceEventPreProcessor.preProcess(any())).thenReturn(emptyList());
lenient().when(instanceEventPreProcessor.preProcess(any())).thenAnswer(invocation -> invocation.getArgument(0));
}

@Test
Expand All @@ -105,7 +102,6 @@ void indexResources_positive() {

when(searchDocumentConverter.convert(List.of(resourceEvent))).thenReturn(
mapOf(INSTANCE.getName(), List.of(searchBody)));
when(indexRepository.indexExists(indexName(TENANT_ID))).thenReturn(true);
when(primaryResourceRepository.indexResources(List.of(searchBody))).thenReturn(expectedResponse);
when(resourceDescriptionService.find(INSTANCE)).thenReturn(of(resourceDescription(INSTANCE)));

Expand All @@ -121,7 +117,6 @@ void indexResources_negative_failedResponse() {

when(searchDocumentConverter.convert(List.of(resourceEvent)))
.thenReturn(mapOf(INSTANCE.getName(), List.of(searchBody)));
when(indexRepository.indexExists(indexName(TENANT_ID))).thenReturn(true);
when(primaryResourceRepository.indexResources(List.of(searchBody))).thenReturn(expectedResponse);
when(resourceDescriptionService.find(INSTANCE)).thenReturn(of(resourceDescription(INSTANCE)));

Expand All @@ -139,7 +134,6 @@ void indexResources_positive_customResourceRepository() {
when(resourceDescriptionService.find(INSTANCE)).thenReturn(of(resourceDescriptionWithCustomRepository()));
when(searchDocumentConverter.convert(List.of(resourceEvent)))
.thenReturn(mapOf(INSTANCE.getName(), List.of(searchBody)));
when(indexRepository.indexExists(INDEX_NAME)).thenReturn(true);
when(resourceRepositoryBeans.containsKey(CUSTOM_REPOSITORY_NAME)).thenReturn(true);
when(resourceRepositoryBeans.get(CUSTOM_REPOSITORY_NAME)).thenReturn(customResourceRepository);
when(customResourceRepository.indexResources(List.of(searchBody))).thenReturn(expectedResponse);
Expand All @@ -152,7 +146,6 @@ void indexResources_positive_customResourceRepository() {
@Test
void indexResources_negative() {
var resourceEvents = List.of(resourceEvent(INSTANCE, mapOf("id", randomId())));
when(indexRepository.indexExists(indexName(TENANT_ID))).thenReturn(false);
when(primaryResourceRepository.indexResources(null)).thenReturn(getSuccessIndexOperationResponse());
when(searchDocumentConverter.convert(emptyList())).thenReturn(emptyMap());

Expand All @@ -173,8 +166,7 @@ void indexResourcesById_positive() {
var expectedResponse = getSuccessIndexOperationResponse();
var expectedDocuments = List.of(searchDocumentBody());

when(indexRepository.indexExists(indexName(TENANT_ID))).thenReturn(true);
when(resourceFetchService.fetchInstancesByIds(resourceEvents)).thenReturn(List.of(resourceEvent));
when(resourceFetchService.fetchInstancesByIds(any())).thenReturn(List.of(resourceEvent));
when(searchDocumentConverter.convert(List.of(resourceEvent)))
.thenReturn(mapOf(INSTANCE.getName(), expectedDocuments));
when(primaryResourceRepository.indexResources(expectedDocuments)).thenReturn(expectedResponse);
Expand All @@ -195,7 +187,6 @@ void indexResourcesById_positive_updateEvent() {
when(resourceFetchService.fetchInstancesByIds(List.of(resourceEvent))).thenReturn(List.of(fetchedEvent));
when(searchDocumentConverter.convert(List.of(fetchedEvent)))
.thenReturn(mapOf(INSTANCE.getName(), List.of(searchBody)));
when(indexRepository.indexExists(indexName(TENANT_ID))).thenReturn(true);
when(primaryResourceRepository.indexResources(List.of(searchBody))).thenReturn(expectedResponse);
when(resourceDescriptionService.find(INSTANCE)).thenReturn(of(resourceDescription(INSTANCE)));

Expand All @@ -217,7 +208,6 @@ void indexResourcesById_positive_moveDataBetweenInstances() {

when(resourceFetchService.fetchInstancesByIds(List.of(oldEvent, newEvent))).thenReturn(fetchedEvents);
when(searchDocumentConverter.convert(fetchedEvents)).thenReturn(mapOf(INSTANCE.getName(), searchBodies));
when(indexRepository.indexExists(indexName(TENANT_ID))).thenReturn(true);
when(primaryResourceRepository.indexResources(searchBodies)).thenReturn(expectedResponse);
when(resourceDescriptionService.find(INSTANCE)).thenReturn(of(resourceDescription(INSTANCE)));

Expand All @@ -233,7 +223,6 @@ void indexResourcesById_positive_deleteEvent() {
when(resourceFetchService.fetchInstancesByIds(emptyList())).thenReturn(emptyList());
when(searchDocumentConverter.convert(emptyList())).thenReturn(emptyMap());
when(searchDocumentConverter.convert(resourceEvents)).thenReturn(mapOf(INSTANCE.getName(), expectedDocuments));
when(indexRepository.indexExists(indexName(TENANT_ID))).thenReturn(true);

var expectedResponse = getSuccessIndexOperationResponse();
when(primaryResourceRepository.indexResources(expectedDocuments)).thenReturn(expectedResponse);
Expand All @@ -249,7 +238,6 @@ void indexResourcesById_negative_failedEvents() {
var expectedResponse = getErrorIndexOperationResponse("Bulk failed: errors: ['test-error']");
var expectedDocuments = List.of(searchDocumentBody());

when(indexRepository.indexExists(indexName(TENANT_ID))).thenReturn(true);
when(resourceFetchService.fetchInstancesByIds(resourceEvents)).thenReturn(List.of(resourceEvent));
when(searchDocumentConverter.convert(List.of(resourceEvent)))
.thenReturn(mapOf(INSTANCE.getName(), expectedDocuments));
Expand All @@ -271,20 +259,6 @@ void indexResourcesById_positive_null() {
assertThat(actual).isEqualTo(getSuccessIndexOperationResponse());
}

@Test
void indexResourcesById_negative_indexNotExist() {
var eventIds = List.of(resourceEvent(randomId(), INSTANCE, CREATE));

when(indexRepository.indexExists(indexName(TENANT_ID))).thenReturn(false);
when(resourceFetchService.fetchInstancesByIds(emptyList())).thenReturn(emptyList());
when(searchDocumentConverter.convert(emptyList())).thenReturn(emptyMap());
when(primaryResourceRepository.indexResources(null)).thenReturn(getSuccessIndexOperationResponse());

var actual = indexService.indexInstancesById(eventIds);

assertThat(actual).isEqualTo(getSuccessIndexOperationResponse());
}

private static ResourceDescription resourceDescriptionWithCustomRepository() {
var resourceIndexingConfiguration = new ResourceIndexingConfiguration();
resourceIndexingConfiguration.setResourceRepository(CUSTOM_REPOSITORY_NAME);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.folio.search.model.service.BrowseContext;
import org.folio.search.service.consortium.FeatureConfigServiceDecorator;
import org.folio.search.service.converter.ElasticsearchDocumentConverter;
import org.folio.search.utils.TestUtils;
import org.folio.spring.testing.type.UnitTest;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -72,6 +73,7 @@ void convert_positive_parameterized(@SuppressWarnings("unused") String name,
when(featureConfigService.isEnabled(BROWSE_CN_INTERMEDIATE_VALUES)).thenReturn(true);

var actual = resultConverter.convert(searchResponse, ctx, isBrowsingForward);
TestUtils.cleanupActual(actual);

assertThat(actual).isEqualTo(BrowseResult.of(100, expected));
verify(documentConverter)
Expand Down Expand Up @@ -116,6 +118,7 @@ void convert_positive_intermediateResultsNotPopulatedForBrowsingForward() {
when(featureConfigService.isEnabled(BROWSE_CN_INTERMEDIATE_VALUES)).thenReturn(false);

var actual = resultConverter.convert(searchResponse, forwardContext(), true);
TestUtils.cleanupActual(actual);

assertThat(actual).isEqualTo(BrowseResult.of(10, List.of(
cnBrowseItem(instance("B1", "B2", "C2"), "B1"),
Expand All @@ -136,6 +139,7 @@ void convert_positive_intermediateResultsNotPopulatedForBrowsingBackward() {
when(featureConfigService.isEnabled(BROWSE_CN_INTERMEDIATE_VALUES)).thenReturn(false);

var actual = resultConverter.convert(searchResponse, backwardContext(), false);
TestUtils.cleanupActual(actual);

assertThat(actual).isEqualTo(BrowseResult.of(10, List.of(
cnBrowseItem(instance("B1", "B2"), "B2"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import static org.folio.search.utils.SearchUtils.CALL_NUMBER_BROWSING_FIELD;
import static org.folio.search.utils.SearchUtils.SHELVING_ORDER_BROWSING_FIELD;
import static org.folio.search.utils.TestConstants.TENANT_ID;
import static org.folio.search.utils.TestUtils.cleanupActual;
import static org.folio.search.utils.TestUtils.cnBrowseItem;
import static org.folio.search.utils.TestUtils.getShelfKeyFromCallNumber;
import static org.mockito.ArgumentMatchers.any;
Expand Down Expand Up @@ -103,6 +104,7 @@ void browse_positive_around() {
BrowseResult.of(7, browseItems("C1", "C2", "C3", "C4", "C5", "C6", "C7")));

var actual = callNumberBrowseService.browse(request);
cleanupActual(actual);

assertThat(actual).isEqualTo(BrowseResult.of(11, "A3", "C2", List.of(
cnBrowseItem(instance("A3"), "A3"),
Expand All @@ -122,6 +124,7 @@ void browse_positive_aroundWithFoundAnchor() {
BrowseResult.of(2, browseItems("B", "C 11")));

var actual = callNumberBrowseService.browse(request);
cleanupActual(actual);

assertThat(actual).isEqualTo(BrowseResult.of(3, List.of(
cnBrowseItem(instance("A 11"), "A 11"),
Expand All @@ -142,6 +145,7 @@ void browse_positive_around_additionalRequest_when_emptyPrecedingResults() {
when(searchConfig.getMaxBrowseRequestOffset()).thenReturn(500L);

var actual = callNumberBrowseService.browse(request);
cleanupActual(actual);

assertThat(actual).isEqualTo(BrowseResult.of(2, List.of(
cnBrowseItem(instance("A"), "A"),
Expand All @@ -156,6 +160,7 @@ void browse_positive_around_emptySucceedingResults() {
contextAroundIncluding(), BrowseResult.of(1, browseItems("A 11", "A 12")), BrowseResult.empty());

var actual = callNumberBrowseService.browse(request);
cleanupActual(actual);

assertThat(actual).isEqualTo(BrowseResult.of(1, List.of(
cnBrowseItem(instance("A 11"), "A 11"),
Expand All @@ -173,6 +178,7 @@ void browse_positive_around_noHighlightMatch() {
BrowseResult.of(1, browseItems("C 11")));

var actual = callNumberBrowseService.browse(request);
cleanupActual(actual);

assertThat(actual).isEqualTo(BrowseResult.of(2, List.of(
cnBrowseItem(instance("A 11"), "A 11"),
Expand Down Expand Up @@ -222,6 +228,7 @@ void browse_positive_forward() {
BrowseResult.of(2, browseItems("C1", "C2")));

var actual = callNumberBrowseService.browse(request);
cleanupActual(actual);

assertThat(actual).isEqualTo(BrowseResult.of(2, "C1", null, List.of(
cnBrowseItem(instance("C1"), "C1"), cnBrowseItem(instance("C2"), "C2"))));
Expand All @@ -243,6 +250,7 @@ void browse_positive_forwardMultipleAnchors() {
BrowseResult.of(1, browseItems("B")));

var actual = callNumberBrowseService.browse(request);
cleanupActual(actual);

assertThat(actual).isEqualTo(BrowseResult.of(1, "B", null, List.of(
cnBrowseItem(instance("B"), "B"))));
Expand Down Expand Up @@ -298,6 +306,7 @@ void browse_positive_multipleAnchors() {
.thenReturn(succeedingResult);

var actual = callNumberBrowseService.browse(request);
cleanupActual(actual);

assertThat(actual).isEqualTo(BrowseResult.of(2, List.of(
cnBrowseItem(instance("A1"), "A1"),
Expand All @@ -320,6 +329,7 @@ void browse_positive_forwardWithNextValue() {
BrowseResult.of(5, browseItems("C1", "C2", "C3", "C4", "C5")));

var actual = callNumberBrowseService.browse(request);
cleanupActual(actual);

assertThat(actual).isEqualTo(BrowseResult.of(5, "C1", "C2", List.of(
cnBrowseItem(instance("C1"), "C1"), cnBrowseItem(instance("C2"), "C2"))));
Expand Down Expand Up @@ -356,6 +366,7 @@ void browse_positive_backward() {
BrowseResult.of(2, browseItems("A1", "A2")));

var actual = callNumberBrowseService.browse(request);
cleanupActual(actual);

assertThat(actual).isEqualTo(BrowseResult.of(2, null, "A2", List.of(
cnBrowseItem(instance("A1"), "A1"), cnBrowseItem(instance("A2"), "A2"))));
Expand Down Expand Up @@ -396,6 +407,7 @@ void browse_positive_backwardWithPrevValue() {
BrowseResult.of(5, browseItems("A1", "A2", "A3", "A4", "A5")));

var actual = callNumberBrowseService.browse(request);
cleanupActual(actual);

assertThat(actual).isEqualTo(BrowseResult.of(5, "A4", "A5", List.of(
cnBrowseItem(instance("A4"), "A4"), cnBrowseItem(instance("A5"), "A5"))));
Expand Down Expand Up @@ -471,6 +483,7 @@ void browse_positive_emptySucceedingResults() {
.thenReturn(succeedingResult);

var actual = callNumberBrowseService.browse(request);
cleanupActual(actual);

assertThat(actual).isEqualTo(BrowseResult.of(3, List.of(
cnBrowseItem(instance("A"), "A"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ void getAnchorSearchQuery_positive() {
}

private List<ContributorResource> contributors() {
return singletonList(new ContributorResource("name", "nameType", null, "auth",
return singletonList(new ContributorResource("id", "name", "nameType", "auth",
Set.of(
contributorInstance("ins1", "type1", false, "tenant1"),
contributorInstance("ins2", "type1", true, "tenant2"),
Expand Down
Loading

0 comments on commit 04d6ea0

Please sign in to comment.