Skip to content

Commit 2a48de0

Browse files
test: add tests
1 parent 2a4c2a5 commit 2a48de0

File tree

2 files changed

+78
-15
lines changed

2 files changed

+78
-15
lines changed

src/test/java/org/folio/indexing/IndexingInstanceCallNumberConsortiumIT.java

Lines changed: 75 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
import static java.util.Collections.emptyList;
44
import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat;
5+
import static org.awaitility.Awaitility.await;
6+
import static org.awaitility.Durations.ONE_MINUTE;
7+
import static org.folio.search.model.types.ResourceType.INSTANCE;
58
import static org.folio.search.model.types.ResourceType.INSTANCE_CALL_NUMBER;
69
import static org.folio.search.service.reindex.ReindexConstants.CALL_NUMBER_TABLE;
710
import static org.folio.search.service.reindex.ReindexConstants.HOLDING_TABLE;
@@ -13,20 +16,28 @@
1316
import static org.folio.support.base.ApiEndpoints.instanceSearchPath;
1417
import static org.folio.support.utils.TestUtils.randomId;
1518

19+
import java.time.Duration;
1620
import java.util.List;
1721
import java.util.Map;
22+
import java.util.UUID;
23+
import java.util.stream.Stream;
1824
import org.folio.search.domain.dto.Holding;
1925
import org.folio.search.domain.dto.Instance;
2026
import org.folio.search.domain.dto.Item;
2127
import org.folio.search.domain.dto.ItemEffectiveCallNumberComponents;
2228
import org.folio.search.domain.dto.TenantConfiguredFeature;
29+
import org.folio.search.model.event.InstanceSharingCompleteEvent;
30+
import org.folio.search.model.types.ResourceType;
2331
import org.folio.spring.testing.extension.DatabaseCleanup;
2432
import org.folio.spring.testing.type.IntegrationTest;
2533
import org.folio.support.base.BaseIntegrationTest;
2634
import org.junit.jupiter.api.AfterAll;
2735
import org.junit.jupiter.api.AfterEach;
2836
import org.junit.jupiter.api.BeforeAll;
2937
import org.junit.jupiter.api.Test;
38+
import org.junit.jupiter.params.ParameterizedTest;
39+
import org.junit.jupiter.params.provider.Arguments;
40+
import org.junit.jupiter.params.provider.MethodSource;
3041
import org.springframework.test.context.TestPropertySource;
3142

3243
@IntegrationTest
@@ -46,11 +57,6 @@ static void prepare() {
4657
setUpTenant(MEMBER_TENANT_ID);
4758

4859
enableFeature(CENTRAL_TENANT_ID, TenantConfiguredFeature.BROWSE_CALL_NUMBERS);
49-
50-
setUpTestData();
51-
52-
// fetch call number documents for the instance and check that tenant field contains member tenant id
53-
awaitAssertion(() -> assertInstanceCallNumberTenantId(MEMBER_TENANT_ID, false));
5460
}
5561

5662
@AfterAll
@@ -62,10 +68,16 @@ static void cleanUp() {
6268
@AfterEach
6369
void tearDown() {
6470
deleteAllDocuments(INSTANCE_CALL_NUMBER, CENTRAL_TENANT_ID);
71+
awaitAssertion(() -> assertDocumentsEmpty(INSTANCE_CALL_NUMBER));
72+
deleteAllDocuments(INSTANCE, CENTRAL_TENANT_ID);
73+
awaitAssertion(() -> assertDocumentsEmpty(INSTANCE));
6574
}
6675

6776
@Test
6877
void shouldUpdateInstanceCallNumber_onInstanceSharing() {
78+
// given
79+
createInstanceInMemberTenant(INSTANCE_ID, INSTANCE_TITLE, LOCATION_ID, CALL_NUMBER);
80+
awaitAssertion(() -> assertInstanceCallNumberTenantId(MEMBER_TENANT_ID, false));
6981
// when - create instance in central tenant with the same instance id/title
7082
var centralInstance = new Instance().id(INSTANCE_ID).title(INSTANCE_TITLE).source("FOLIO");
7183
inventoryApi.createInstance(CENTRAL_TENANT_ID, centralInstance);
@@ -77,12 +89,59 @@ void shouldUpdateInstanceCallNumber_onInstanceSharing() {
7789
// then - fetch call number documents for the instance and check if tenant field changed to member tenant id
7890
awaitAssertion(() -> assertInstanceCallNumberTenantId(MEMBER_TENANT_ID, false));
7991

80-
inventoryApi.shareInstance(CENTRAL_TENANT_ID, INSTANCE_ID);
92+
inventoryApi.shareInstance(CENTRAL_TENANT_ID, INSTANCE_ID, InstanceSharingCompleteEvent.Status.COMPLETE, "",
93+
MEMBER_TENANT_ID, CENTRAL_TENANT_ID);
8194

8295
// then - fetch call number documents and check if tenant field changed to central and shared is true
8396
awaitAssertion(() -> assertInstanceCallNumberTenantId(CENTRAL_TENANT_ID, true));
8497
}
8598

99+
@ParameterizedTest(name = "{index} => status={0}, errorMessage={1}, targetTenant={2}")
100+
@MethodSource("negativeSharingScenarios")
101+
void shouldNotUpdateInstanceCallNumber_onInvalidSharingEvent(
102+
InstanceSharingCompleteEvent.Status status,
103+
String errorMessage,
104+
String targetTenantId,
105+
String instanceId, String title, String locationId, String callNumber
106+
) {
107+
// given
108+
createInstanceInMemberTenant(instanceId, title, locationId, callNumber);
109+
110+
awaitAssertion(() -> assertInstanceCallNumberTenantId(MEMBER_TENANT_ID, false));
111+
112+
var centralInstance = new Instance().id(instanceId).title(title).source("FOLIO");
113+
inventoryApi.createInstance(CENTRAL_TENANT_ID, centralInstance);
114+
115+
var memberInstance = new Instance().id(instanceId).title(title).source("CONSORTIUM-FOLIO");
116+
inventoryApi.updateInstance(MEMBER_TENANT_ID, memberInstance);
117+
118+
awaitAssertion(() -> assertInstanceCallNumberTenantId(MEMBER_TENANT_ID, false));
119+
120+
// when
121+
inventoryApi.shareInstance(MEMBER_TENANT_ID, instanceId, status, errorMessage, MEMBER_TENANT_ID, targetTenantId);
122+
123+
// then check that call number document is not updated with central tenant id and shared is false
124+
await()
125+
.pollDelay(Duration.ofSeconds(30))
126+
.atMost(ONE_MINUTE)
127+
.untilAsserted(() ->
128+
assertInstanceCallNumberTenantId(MEMBER_TENANT_ID, false)
129+
);
130+
}
131+
132+
private static Stream<Arguments> negativeSharingScenarios() {
133+
return Stream.of(
134+
// ERROR status
135+
Arguments.of(InstanceSharingCompleteEvent.Status.ERROR, "", CENTRAL_TENANT_ID,
136+
UUID.randomUUID().toString(), "title1", UUID.randomUUID().toString(), "call number1"),
137+
// error message is present
138+
Arguments.of(InstanceSharingCompleteEvent.Status.COMPLETE, "error message", CENTRAL_TENANT_ID,
139+
UUID.randomUUID().toString(), "title2", UUID.randomUUID().toString(), "call number2"),
140+
// target tenant is not central tenant
141+
Arguments.of(InstanceSharingCompleteEvent.Status.COMPLETE, "", MEMBER_TENANT_ID,
142+
UUID.randomUUID().toString(), "title3", UUID.randomUUID().toString(), "call number3"));
143+
}
144+
86145
private static void assertInstanceCallNumberTenantId(String expectedTenantId, boolean shared) {
87146
var hits = fetchAllDocuments(INSTANCE_CALL_NUMBER, CENTRAL_TENANT_ID);
88147
assertThat(hits).hasSize(1);
@@ -97,12 +156,18 @@ private static void assertInstanceCallNumberTenantId(String expectedTenantId, bo
97156
.containsEntry("shared", shared));
98157
}
99158

100-
private static void setUpTestData() {
159+
private static void assertDocumentsEmpty(ResourceType resourceType) {
160+
var hits = fetchAllDocuments(resourceType, CENTRAL_TENANT_ID);
161+
assertThat(hits).hasSize(0);
162+
}
163+
164+
private static void createInstanceInMemberTenant(String instanceId, String instanceTitle,
165+
String locationId, String callNumber) {
101166
var holdings = new Holding().id(randomId());
102167
var item = new Item().id(randomId()).holdingsRecordId(holdings.getId())
103-
.effectiveLocationId(LOCATION_ID)
104-
.effectiveCallNumberComponents(new ItemEffectiveCallNumberComponents().callNumber(CALL_NUMBER));
105-
var instance = new Instance().id(INSTANCE_ID).title(INSTANCE_TITLE).source("FOLIO")
168+
.effectiveLocationId(locationId)
169+
.effectiveCallNumberComponents(new ItemEffectiveCallNumberComponents().callNumber(callNumber));
170+
var instance = new Instance().id(instanceId).title(instanceTitle).source("FOLIO")
106171
.holdings(List.of(holdings))
107172
.items(List.of(item));
108173
saveRecords(MEMBER_TENANT_ID, instanceSearchPath(), List.of(instance), 1, emptyList(),

src/test/java/org/folio/support/api/InventoryApi.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
import static org.folio.search.domain.dto.ResourceEventType.UPDATE;
99
import static org.folio.search.utils.SearchUtils.INSTANCE_HOLDING_FIELD_NAME;
1010
import static org.folio.search.utils.SearchUtils.INSTANCE_ITEM_FIELD_NAME;
11-
import static org.folio.support.TestConstants.CENTRAL_TENANT_ID;
12-
import static org.folio.support.TestConstants.MEMBER_TENANT_ID;
1311
import static org.folio.support.TestConstants.consortiumInstanceSharingCompleteTopic;
1412
import static org.folio.support.TestConstants.inventoryBoundWithTopic;
1513
import static org.folio.support.TestConstants.inventoryHoldingTopic;
@@ -92,9 +90,9 @@ public void deleteInstance(String tenant, String id) {
9290
.whenComplete(onCompleteConsumer());
9391
}
9492

95-
public void shareInstance(String tenantId, String instanceId) {
96-
var instanceEvent = instanceSharingCompleteEvent(instanceId, MEMBER_TENANT_ID, CENTRAL_TENANT_ID,
97-
InstanceSharingCompleteEvent.Status.COMPLETE, "");
93+
public void shareInstance(String tenantId, String instanceId, InstanceSharingCompleteEvent.Status status,
94+
String errorMessage, String sourceTenantId, String targetTenantId) {
95+
var instanceEvent = instanceSharingCompleteEvent(instanceId, sourceTenantId, targetTenantId, status, errorMessage);
9896

9997
kafkaTemplate.send(consortiumInstanceSharingCompleteTopic(tenantId), instanceId,
10098
objectMapper.writeValueAsString(instanceEvent))

0 commit comments

Comments
 (0)