diff --git a/src/test/java/org/folio/dcb/service/CirculationServiceTest.java b/src/test/java/org/folio/dcb/service/CirculationServiceTest.java index 332785d6..be04ede3 100644 --- a/src/test/java/org/folio/dcb/service/CirculationServiceTest.java +++ b/src/test/java/org/folio/dcb/service/CirculationServiceTest.java @@ -23,6 +23,7 @@ import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; import org.springframework.web.client.HttpClientErrorException; +import static org.mockito.Mockito.never; @ExtendWith(MockitoExtension.class) class CirculationServiceTest { @@ -88,4 +89,45 @@ void shouldThrowExceptionWhenRequestIsNotUpdated() { assertThrows(CirculationRequestException.class, () -> circulationService.cancelRequest(transactionEntity, false)); } + + @Test + void checkInByBarcodeShouldPrioritizeMethodParameterServicePointIdTest() { + // TestMate-8336b6a9423d10f6280b51c55a4066cb + TransactionEntity transactionEntity = createTransactionEntity(); + transactionEntity.setServicePointId("ENTITY_SERVICE_POINT"); + transactionEntity.setItemBarcode("ITEM-123"); + String explicitServicePointId = "EXPLICIT_SERVICE_POINT"; + circulationService.checkInByBarcode(transactionEntity, explicitServicePointId, null); + verify(circulationClient).checkInByBarcode(argThat(req -> + explicitServicePointId.equals(req.getServicePointId()) && + "ITEM-123".equals(req.getItemBarcode()) + )); + } + + @Test + void checkInByBarcodeWhenServicePointIsNullShouldStillDelegate() { + // TestMate-72b46a8ea31a64a588009299c2871c1b + // Given + TransactionEntity transactionEntity = createTransactionEntity(); + transactionEntity.setItemBarcode("99999"); + transactionEntity.setServicePointId(null); + // When + circulationService.checkInByBarcode(transactionEntity); + // Then + verify(circulationClient).checkInByBarcode(argThat(request -> + "99999".equals(request.getItemBarcode()) && request.getServicePointId() == null + )); + } + + @Test + void cancelRequestShouldNotUpdateWhenNoOpenRequestFound() { + // TestMate-b7855e7929105a63d4324d9664993f01 + // Given + TransactionEntity transactionEntity = createTransactionEntity(); + when(circulationRequestService.getCancellationRequestIfOpenOrNull(anyString())).thenReturn(null); + // When + circulationService.cancelRequest(transactionEntity, false); + // Then + verify(circulationClient, never()).updateRequest(anyString(), any()); + } } diff --git a/src/test/java/org/folio/dcb/service/ServicePointServiceTest.java b/src/test/java/org/folio/dcb/service/ServicePointServiceTest.java index 5d0df0f0..b3a233e1 100644 --- a/src/test/java/org/folio/dcb/service/ServicePointServiceTest.java +++ b/src/test/java/org/folio/dcb/service/ServicePointServiceTest.java @@ -23,12 +23,21 @@ import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; +import org.folio.dcb.domain.dto.ServicePointRequest; +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.ArgumentMatchers.eq; @ExtendWith(MockitoExtension.class) class ServicePointServiceTest { private static final String SETTING_KEY = "lender.hold-shelf-expiry-period"; + private static final String FIRST_SP_ID = "550e8400-e29b-41d4-a716-446655440000"; + + private static final String SECOND_SP_ID = "660e8400-e29b-41d4-a716-446655441111"; + @InjectMocks private ServicePointServiceImpl servicePointService; @Mock private ServicePointClient servicePointClient; @Mock private CalendarService calendarService; @@ -68,4 +77,52 @@ void createServicePointIfExistsTest() { UUID.fromString(response.getId())); verify(calendarService, never()).addServicePointIdToDefaultCalendar(any()); } + + @ParameterizedTest + @CsvSource({ + "LENDER, lender.hold-shelf-expiry-period", + "BORROWING_PICKUP, borrowing-pickup.hold-shelf-expiry-period", + "PICKUP, pickup.hold-shelf-expiry-period", + "BORROWER, borrower.hold-shelf-expiry-period" + }) + void testCreateServicePointIfNotExistsShouldUseRoleSpecificSettingKeys(RoleEnum role, String expectedSettingKey) { + // TestMate-0bddc1ec31baedc07ec2ca420ab0335e + // Given + when(servicePointClient.findByQuery(any())).thenReturn(ResultList.empty()); + when(servicePointClient.createServicePoint(any())).thenReturn(createServicePointRequest()); + when(servicePointExpirationPeriodService.getShelfExpiryPeriod(expectedSettingKey)).thenReturn(DcbConstants.DEFAULT_PERIOD); + var dcbTransaction = createDcbTransactionByRole(role); + // When + servicePointService.createServicePointIfNotExists(dcbTransaction); + // Then + verify(servicePointExpirationPeriodService).getShelfExpiryPeriod(expectedSettingKey); + verify(servicePointClient).createServicePoint(any()); + } + + @Test + void testCreateServicePointIfNotExistsWhenMultipleServicePointsFoundShouldProcessFirstOne() { + // TestMate-1e269ad2996e9842558491ac08a9ea65 + // Given + var dcbTransaction = createDcbTransactionByRole(RoleEnum.LENDER); + var firstServicePoint = createServicePointRequest(); + firstServicePoint.setId(FIRST_SP_ID); + var secondServicePoint = createServicePointRequest(); + secondServicePoint.setId(SECOND_SP_ID); + var resultList = ResultList.of(2, List.of(firstServicePoint, secondServicePoint)); + var expiryPeriod = HoldShelfExpiryPeriod.builder() + .duration(5) + .intervalId(IntervalIdEnum.DAYS) + .build(); + when(servicePointClient.findByQuery(any())).thenReturn(resultList); + when(servicePointExpirationPeriodService.getShelfExpiryPeriod(SETTING_KEY)).thenReturn(expiryPeriod); + // When + var result = servicePointService.createServicePointIfNotExists(dcbTransaction); + // Then + assertThat(result.getId()).isEqualTo(FIRST_SP_ID); + verify(servicePointClient).updateServicePointById(eq(FIRST_SP_ID), any()); + verify(servicePointClient, never()).updateServicePointById(eq(SECOND_SP_ID), any()); + verify(calendarService).associateServicePointIdWithDefaultCalendarIfAbsent(UUID.fromString(FIRST_SP_ID)); + verify(calendarService, never()).associateServicePointIdWithDefaultCalendarIfAbsent(UUID.fromString(SECOND_SP_ID)); + verify(calendarService, never()).addServicePointIdToDefaultCalendar(any()); + } } diff --git a/src/test/java/org/folio/dcb/service/TransactionServiceTest.java b/src/test/java/org/folio/dcb/service/TransactionServiceTest.java index 81755fe9..795b1dd3 100644 --- a/src/test/java/org/folio/dcb/service/TransactionServiceTest.java +++ b/src/test/java/org/folio/dcb/service/TransactionServiceTest.java @@ -66,6 +66,7 @@ import org.mockito.Mockito; import org.mockito.junit.jupiter.MockitoExtension; import org.springframework.data.domain.Page; +import java.util.Collections; @ExtendWith(MockitoExtension.class) class TransactionServiceTest { @@ -417,4 +418,22 @@ void updateTransactionStatus_positive_pickupRole() { verifyNoInteractions(borrowingPickupLibraryService, statusProcessorService, lendingLibraryService, borrowingLibraryService); } + + @Test + void testUpdateTransactionStatusShouldHandleEmptyStatusChain() { + // TestMate-1d3c7c174b6e04da85202d9a0b1f332f + // Given + var dcbTransactionEntity = createTransactionEntity(); + dcbTransactionEntity.setStatus(OPEN); + dcbTransactionEntity.setRole(LENDER); + var targetStatus = TransactionStatus.builder().status(CLOSED).build(); + when(transactionRepository.findById(DCB_TRANSACTION_ID)).thenReturn(Optional.of(dcbTransactionEntity)); + when(statusProcessorService.lendingChainProcessor(OPEN, CLOSED)).thenReturn(Collections.emptyList()); + // When + TransactionStatusResponse response = transactionsService.updateTransactionStatus(DCB_TRANSACTION_ID, targetStatus); + // Then + assertThat(response.getStatus()).isEqualTo(TransactionStatusResponse.StatusEnum.CLOSED); + verify(statusProcessorService).lendingChainProcessor(OPEN, CLOSED); + verifyNoInteractions(lendingLibraryService, borrowingLibraryService, pickupLibraryService, borrowingPickupLibraryService); + } } diff --git a/src/test/java/org/folio/dcb/service/UserServiceTest.java b/src/test/java/org/folio/dcb/service/UserServiceTest.java index 56128d3e..69ddaee4 100644 --- a/src/test/java/org/folio/dcb/service/UserServiceTest.java +++ b/src/test/java/org/folio/dcb/service/UserServiceTest.java @@ -29,6 +29,8 @@ import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; +import static org.folio.dcb.service.UserServiceTest.randomUuid; +import static org.folio.dcb.service.UserServiceTest.virtualUser; @ExtendWith(MockitoExtension.class) class UserServiceTest { @@ -235,6 +237,22 @@ void fetchOrCreateUser_positive_existingUserWithGroupAndPersonalInfoChange() { verify(usersClient, never()).createUser(any()); } + @Test + void fetchUser_positive_existingUser() { + // TestMate-73ca1e2922a2f9b42ef5244d75b4a0ce + // Given + var userId = randomUuid(); + var groupId = randomUuid(); + var existingUser = virtualUser(userId, groupId); + var dcbPatron = dcbPatron(userId); + when(usersClient.fetchUserByBarcodeAndId(any())).thenReturn(userCollection(existingUser)); + // When + var result = userService.fetchUser(dcbPatron); + // Then + assertThat(result).isEqualTo(existingUser); + verify(usersClient).fetchUserByBarcodeAndId(any()); + } + private static UserCollection userCollection(User... users) { return new UserCollection().users(List.of(users)).totalRecords(users.length); }