Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions src/main/java/org/folio/bulkops/util/FqmContentFetcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;
Expand Down Expand Up @@ -159,7 +160,7 @@ public InputStream contents(

List<String> entityJsonKeys = getEntityJsonKeys(entityType);

boolean isCentralTenant = consortiaService.isTenantCentral(folioExecutionContext.getTenantId());
var tenantId = folioExecutionContext.getTenantId();

List<List<UUID>> chunks =
IntStream.range(0, uuids.size())
Expand All @@ -170,14 +171,21 @@ public InputStream contents(
ExecutorService pool = Executors.newFixedThreadPool(maxParallelChunks);
CompletionService<List<Map<String, Object>>> completion = new ExecutorCompletionService<>(pool);

String centralTenantId = consortiaService.getCentralTenantId(tenantId);
boolean isTenantInConsortia = StringUtils.isNotEmpty(centralTenantId);
boolean isCentralTenant = tenantId.equals(centralTenantId);

Function<String, List<String>> idMapper =
isTenantInConsortia ? id -> List.of(id, tenantId) : List::of;

for (List<UUID> chunk : chunks) {
completion.submit(
() -> {
ContentsRequest req =
new ContentsRequest()
.entityTypeId(entityTypeId)
.fields(entityJsonKeys)
.ids(chunk.stream().map(UUID::toString).map(List::of).toList());
.ids(chunk.stream().map(UUID::toString).map(idMapper).toList());
return queryClient.getContents(req);
});
}
Expand Down
128 changes: 128 additions & 0 deletions src/test/java/org/folio/bulkops/util/FqmContentFetcherEcsTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
package org.folio.bulkops.util;

import static org.awaitility.Awaitility.await;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.atLeastOnce;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import org.folio.bulkops.BaseTest;
import org.folio.bulkops.client.QueryClient;
import org.folio.bulkops.domain.dto.EntityType;
import org.folio.bulkops.service.ConsortiaService;
import org.folio.bulkops.service.EntityTypeService;
import org.folio.querytool.domain.dto.ContentsRequest;
import org.folio.spring.FolioExecutionContext;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.bean.override.mockito.MockitoBean;

@SpringBootTest
@ContextConfiguration(initializers = BaseTest.Initializer.class)
class FqmContentFetcherEcsTest {

@MockitoBean private ConsortiaService consortiaService;
@MockitoBean private EntityTypeService entityTypeService;
@MockitoBean private FolioExecutionContext folioExecutionContext;
@MockitoBean private QueryClient queryClient;

@Value("${application.fqm-fetcher.max_chunk_size}")
private int chunkSize;

@Autowired private FqmContentFetcher fqmContentFetcher;

@Test
void testCentralTenantBehavior() {
String tenantId = "central";
String centralTenantId = "central";
mockCommon(tenantId, centralTenantId);

UUID uuid = UUID.randomUUID();

try (var is =
fqmContentFetcher.contents(
List.of(uuid), EntityType.INSTANCE, List.of(), UUID.randomUUID())) {
ArgumentCaptor<ContentsRequest> captor = ArgumentCaptor.forClass(ContentsRequest.class);

await()
.atMost(2, TimeUnit.SECONDS)
.untilAsserted(() -> verify(queryClient, atLeastOnce()).getContents(captor.capture()));

ContentsRequest req = captor.getValue();
List<List<String>> ids = req.getIds();
Assertions.assertEquals(1, ids.size());
Assertions.assertEquals(List.of(uuid.toString(), tenantId), ids.getFirst());
} catch (IOException e) {
Assertions.fail("Fail reading content");
}
}

@Test
void testConsortiumMemberTenantBehavior() {
String tenantId = "member";
String centralTenantId = "central";
mockCommon(tenantId, centralTenantId);

UUID uuid = UUID.randomUUID();

try (var is =
fqmContentFetcher.contents(
List.of(uuid), EntityType.INSTANCE, List.of(), UUID.randomUUID())) {
ArgumentCaptor<ContentsRequest> captor = ArgumentCaptor.forClass(ContentsRequest.class);

await()
.atMost(2, TimeUnit.SECONDS)
.untilAsserted(() -> verify(queryClient, atLeastOnce()).getContents(captor.capture()));

ContentsRequest req = captor.getValue();
List<List<String>> ids = req.getIds();
Assertions.assertEquals(1, ids.size());
Assertions.assertEquals(List.of(uuid.toString(), tenantId), ids.getFirst());
} catch (IOException e) {
Assertions.fail("Fail reading content");
}
}

@Test
void testNonConsortiaTenantBehavior() {
String tenantId = "regular";
String centralTenantId = "";
mockCommon(tenantId, centralTenantId);

UUID uuid = UUID.randomUUID();

try (var is =
fqmContentFetcher.contents(
List.of(uuid), EntityType.INSTANCE, List.of(), UUID.randomUUID())) {
ArgumentCaptor<ContentsRequest> captor = ArgumentCaptor.forClass(ContentsRequest.class);
await()
.atMost(2, TimeUnit.SECONDS)
.untilAsserted(() -> verify(queryClient, atLeastOnce()).getContents(captor.capture()));

ContentsRequest req = captor.getValue();
List<List<String>> ids = req.getIds();
Assertions.assertEquals(1, ids.size());
Assertions.assertEquals(List.of(uuid.toString()), ids.getFirst());
} catch (IOException e) {
Assertions.fail("Fail reading content");
}
}

private void mockCommon(String tenantId, String centralTenantId) {
when(folioExecutionContext.getTenantId()).thenReturn(tenantId);
when(consortiaService.getCentralTenantId(tenantId)).thenReturn(centralTenantId);
when(entityTypeService.getFqmEntityTypeIdByBulkOpsEntityType(any()))
.thenReturn(UUID.randomUUID());
when(queryClient.getContents(any())).thenReturn(List.of(Map.of()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,7 @@ void fetchShouldReturnAnErrorForNonSharedInstancesInEcs(EntityType entityType) t

when(folioExecutionContext.getTenantId()).thenReturn("tenant");
when(consortiaService.isTenantCentral(anyString())).thenReturn(true);
when(consortiaService.getCentralTenantId(anyString())).thenReturn("tenant");
when(entityTypeService.getFqmEntityTypeIdByBulkOpsEntityType(entityType))
.thenReturn(randomUUID());

Expand Down Expand Up @@ -555,6 +556,7 @@ void fetchShouldReturnAnErrorForShadowUsersInEcs() throws Exception {

when(folioExecutionContext.getTenantId()).thenReturn("tenant");
when(consortiaService.isTenantCentral(anyString())).thenReturn(true);
when(consortiaService.getCentralTenantId(anyString())).thenReturn("tenant");
when(entityTypeService.getFqmEntityTypeIdByBulkOpsEntityType(EntityType.USER))
.thenReturn(randomUUID());

Expand Down
Loading