22
33import static java .util .Collections .emptyList ;
44import 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 ;
58import static org .folio .search .model .types .ResourceType .INSTANCE_CALL_NUMBER ;
69import static org .folio .search .service .reindex .ReindexConstants .CALL_NUMBER_TABLE ;
710import static org .folio .search .service .reindex .ReindexConstants .HOLDING_TABLE ;
1316import static org .folio .support .base .ApiEndpoints .instanceSearchPath ;
1417import static org .folio .support .utils .TestUtils .randomId ;
1518
19+ import java .time .Duration ;
1620import java .util .List ;
1721import java .util .Map ;
22+ import java .util .UUID ;
23+ import java .util .stream .Stream ;
1824import org .folio .search .domain .dto .Holding ;
1925import org .folio .search .domain .dto .Instance ;
2026import org .folio .search .domain .dto .Item ;
2127import org .folio .search .domain .dto .ItemEffectiveCallNumberComponents ;
2228import org .folio .search .domain .dto .TenantConfiguredFeature ;
29+ import org .folio .search .model .event .InstanceSharingCompleteEvent ;
30+ import org .folio .search .model .types .ResourceType ;
2331import org .folio .spring .testing .extension .DatabaseCleanup ;
2432import org .folio .spring .testing .type .IntegrationTest ;
2533import org .folio .support .base .BaseIntegrationTest ;
2634import org .junit .jupiter .api .AfterAll ;
2735import org .junit .jupiter .api .AfterEach ;
2836import org .junit .jupiter .api .BeforeAll ;
2937import 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 ;
3041import 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 (),
0 commit comments