From 057f6995700c752ef9307e37be28d20aa1c03ea4 Mon Sep 17 00:00:00 2001 From: aitestmate Date: Fri, 3 Jul 2026 13:19:54 +0000 Subject: [PATCH 1/2] [chore] [mod-dcb] unit tests --- .../dcb/service/InventoryItemServiceTest.java | 84 +++++++++++++++++++ .../dcb/service/CalendarServiceTest.java | 44 ++++++++++ .../dcb/service/PatronGroupServiceTest.java | 39 +++++++++ .../service/TransactionAuditServiceTest.java | 14 ++++ 4 files changed, 181 insertions(+) create mode 100644 ** src/test/java/org/folio/dcb/service/InventoryItemServiceTest.java diff --git a/** src/test/java/org/folio/dcb/service/InventoryItemServiceTest.java b/** src/test/java/org/folio/dcb/service/InventoryItemServiceTest.java new file mode 100644 index 00000000..ff32dff2 --- /dev/null +++ b/** src/test/java/org/folio/dcb/service/InventoryItemServiceTest.java @@ -0,0 +1,84 @@ +package org.folio.dcb.service; + +import org.folio.dcb.support.types.UnitTest; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.junit.jupiter.MockitoExtension; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; +import org.folio.dcb.domain.ResultList; +import org.folio.dcb.domain.dto.MaterialType; +import org.folio.dcb.integration.inventory.MaterialTypeClient; +import org.folio.dcb.service.impl.ItemServiceImpl; +import org.junit.jupiter.api.Test; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.folio.spring.exception.NotFoundException; +import static org.junit.jupiter.api.Assertions.assertThrows; + +@UnitTest +@ExtendWith(MockitoExtension.class) +class InventoryItemServiceTest { + + @InjectMocks +private ItemServiceImpl itemService; + + @Mock +private MaterialTypeClient materialTypeClient; + + @Test +void fetchItemMaterialTypeIdByMaterialTypeNameTest() { + // TestMate-0f96f8c42d03650b357196259cdf7e6b + // Given + String materialTypeName = "book"; + String materialTypeId = "1a2b3c4d-5e6f-7g8h-9i0j"; + MaterialType materialType = MaterialType.builder() + .id(materialTypeId) + .name(materialTypeName) + .build(); + ResultList materialTypeResultList = ResultList.asSinglePage(materialType); + when(materialTypeClient.fetchMaterialTypeByQuery(anyString())).thenReturn(materialTypeResultList); + // When + String result = itemService.fetchItemMaterialTypeIdByMaterialTypeName(materialTypeName); + // Then + verify(materialTypeClient).fetchMaterialTypeByQuery("name==\"book\""); + assertEquals(materialTypeId, result); +} + + @Test +void fetchItemMaterialTypeIdByInvalidNameTest() { + // TestMate-772865b75acf5b12a75b8a6a3937b4b1 + // Given + String materialTypeName = "non-existent-type"; + when(materialTypeClient.fetchMaterialTypeByQuery(anyString())).thenReturn(ResultList.empty()); + // When + assertThrows(NotFoundException.class, () -> itemService.fetchItemMaterialTypeIdByMaterialTypeName(materialTypeName)); + // Then + verify(materialTypeClient).fetchMaterialTypeByQuery("name==\"non-existent-type\""); +} + + @Test +void fetchItemMaterialTypeIdByMultipleResultsTest() { + // TestMate-4b6d139cbb3dac517781e88fd0bd5828 + // Given + String materialTypeName = "text"; + String firstMaterialTypeId = "11111111-1111-1111-1111-111111111111"; + String secondMaterialTypeId = "22222222-2222-2222-2222-222222222222"; + MaterialType firstMaterialType = MaterialType.builder() + .id(firstMaterialTypeId) + .name(materialTypeName) + .build(); + MaterialType secondMaterialType = MaterialType.builder() + .id(secondMaterialTypeId) + .name(materialTypeName) + .build(); + ResultList materialTypeResultList = ResultList.asSinglePage(firstMaterialType, secondMaterialType); + when(materialTypeClient.fetchMaterialTypeByQuery(anyString())).thenReturn(materialTypeResultList); + // When + String result = itemService.fetchItemMaterialTypeIdByMaterialTypeName(materialTypeName); + // Then + verify(materialTypeClient).fetchMaterialTypeByQuery("name==\"text\""); + assertEquals(firstMaterialTypeId, result); +} +} diff --git a/src/test/java/org/folio/dcb/service/CalendarServiceTest.java b/src/test/java/org/folio/dcb/service/CalendarServiceTest.java index 29ad94ed..46dc2804 100644 --- a/src/test/java/org/folio/dcb/service/CalendarServiceTest.java +++ b/src/test/java/org/folio/dcb/service/CalendarServiceTest.java @@ -21,6 +21,10 @@ import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; +import java.util.Collections; +import org.folio.dcb.domain.dto.CalendarCollection; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; @ExtendWith(MockitoExtension.class) class CalendarServiceTest { @@ -104,4 +108,44 @@ void testAssociateServicePointIdWithDefaultCalendar_DefaultCalendarNotExists() { assertThrows(IllegalArgumentException.class, () -> calendarService.associateServicePointIdWithDefaultCalendarIfAbsent(servicePointId)); } + + @Test + void testFindCalendarByName_EmptyCollection() { + // TestMate-38edfab7aa6fbd359b174576a85ed904 + // Given + var calendarCollection = new CalendarCollection(); + calendarCollection.setCalendars(Collections.emptyList()); + calendarCollection.setTotalRecords(0); + when(calendarClient.getAllCalendars(Integer.MAX_VALUE)).thenReturn(calendarCollection); + // When + var response = calendarService.findCalendarByName(DCB_CALENDAR_NAME); + // Then + verify(calendarClient).getAllCalendars(Integer.MAX_VALUE); + assertNull(response); + } + + @ParameterizedTest + @CsvSource({ + "Main Calendar, true", + "Main, false", + "main calendar, false", + "'Main Calendar ', false" + }) + void testFindCalendarByName_ExactMatchLogic(String inputName, boolean shouldMatch) { + // TestMate-4b2bf27890ab20d8e60fc02676886bc0 + // Given + String existingCalendarName = "Main Calendar"; + when(calendarClient.getAllCalendars(Integer.MAX_VALUE)) + .thenReturn(getCalendarCollection(existingCalendarName)); + // When + var response = calendarService.findCalendarByName(inputName); + // Then + verify(calendarClient).getAllCalendars(Integer.MAX_VALUE); + if (shouldMatch) { + assertNotNull(response); + assertEquals(existingCalendarName, response.getName()); + } else { + assertNull(response); + } + } } diff --git a/src/test/java/org/folio/dcb/service/PatronGroupServiceTest.java b/src/test/java/org/folio/dcb/service/PatronGroupServiceTest.java index 5c63c089..678be257 100644 --- a/src/test/java/org/folio/dcb/service/PatronGroupServiceTest.java +++ b/src/test/java/org/folio/dcb/service/PatronGroupServiceTest.java @@ -15,6 +15,11 @@ import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; +import java.util.Collections; +import static org.mockito.ArgumentMatchers.anyString; +import org.folio.dcb.domain.dto.UserGroupCollection; +import org.folio.dcb.domain.dto.UserGroup; +import java.util.List; @ExtendWith(MockitoExtension.class) class PatronGroupServiceTest { @@ -37,4 +42,38 @@ void fetchPatronGroupIdByInvalidNameTest() { when(groupClient.fetchGroupByName(any())).thenReturn(userGroupCollection); assertThrows(NotFoundException.class, () -> patronGroupService.fetchPatronGroupIdByName("invalid")); } + + @Test + void fetchPatronGroupIdByEmptyResultsTest() { + // TestMate-b010671f8c95ebf096d763f89b1d27ad + // Given + String groupName = "unknown"; + UserGroupCollection emptyCollection = new UserGroupCollection(); + emptyCollection.setUsergroups(Collections.emptyList()); + emptyCollection.setTotalRecords(0); + when(groupClient.fetchGroupByName(anyString())).thenReturn(emptyCollection); + // When + assertThrows(NotFoundException.class, () -> patronGroupService.fetchPatronGroupIdByName(groupName)); + // Then + verify(groupClient).fetchGroupByName("group==\"unknown\""); + } + + @Test + void fetchPatronGroupIdByComplexNameTest() { + // TestMate-53e3140766520ed0043bb0570807c250 + // Given + String groupName = "Faculty Staff"; + String expectedId = "44963503-625d-453b-9a87-c838e553940c"; + UserGroupCollection userGroupCollection = createUserGroupCollection(); + UserGroup userGroup = new UserGroup(); + userGroup.setGroup(groupName); + userGroup.setId(expectedId); + userGroupCollection.setUsergroups(List.of(userGroup)); + when(groupClient.fetchGroupByName(anyString())).thenReturn(userGroupCollection); + // When + var response = patronGroupService.fetchPatronGroupIdByName(groupName); + // Then + verify(groupClient).fetchGroupByName("group==\"Faculty Staff\""); + assertEquals(expectedId, response); + } } diff --git a/src/test/java/org/folio/dcb/service/TransactionAuditServiceTest.java b/src/test/java/org/folio/dcb/service/TransactionAuditServiceTest.java index 46224ddc..eb764593 100644 --- a/src/test/java/org/folio/dcb/service/TransactionAuditServiceTest.java +++ b/src/test/java/org/folio/dcb/service/TransactionAuditServiceTest.java @@ -135,4 +135,18 @@ void testLogErrorIfTransactionAuditNotExistsWhenDcbTransactionIsNull() { assertThat(savedAudit.getErrorMessage()).isEqualTo( "dcbTransactionId = trn-456; role = null; error message = Null payload."); } + + @Test + void testLogErrorIfTransactionAuditExistsWhenAuditNotFoundShouldNotSave() { + // TestMate-1a0c9f10f62a5a52977c51aeacb5f897 + // Given + var errorMsg = "error_message"; + when(repository.findLatestTransactionAuditEntityByDcbTransactionId(DCB_TRANSACTION_ID)).thenReturn(Optional.empty()); + // When + transactionAuditService.logErrorIfTransactionAuditExists(DCB_TRANSACTION_ID, errorMsg); + // Then + verify(repository).findLatestTransactionAuditEntityByDcbTransactionId(DCB_TRANSACTION_ID); + verify(repository, never()).save(any()); + verify(transactionMapper, never()).mapToEntity(any(), any()); + } } From 2193c37c6a83760978da90e746fce67646304334 Mon Sep 17 00:00:00 2001 From: Pavel Filippov Date: Tue, 7 Jul 2026 11:40:03 +0300 Subject: [PATCH 2/2] chore(unit-tests): Update tests to follow codestyle --- .../dcb/service/InventoryItemServiceTest.java | 84 ------------ .../dcb/service/CalendarServiceTest.java | 128 +++++++++--------- .../dcb/service/InventoryItemServiceTest.java | 52 +++++++ .../dcb/service/PatronGroupServiceTest.java | 49 +++---- .../service/TransactionAuditServiceTest.java | 14 +- 5 files changed, 144 insertions(+), 183 deletions(-) delete mode 100644 ** src/test/java/org/folio/dcb/service/InventoryItemServiceTest.java diff --git a/** src/test/java/org/folio/dcb/service/InventoryItemServiceTest.java b/** src/test/java/org/folio/dcb/service/InventoryItemServiceTest.java deleted file mode 100644 index ff32dff2..00000000 --- a/** src/test/java/org/folio/dcb/service/InventoryItemServiceTest.java +++ /dev/null @@ -1,84 +0,0 @@ -package org.folio.dcb.service; - -import org.folio.dcb.support.types.UnitTest; -import org.junit.jupiter.api.extension.ExtendWith; -import org.mockito.junit.jupiter.MockitoExtension; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.mockito.ArgumentMatchers.anyString; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; -import org.folio.dcb.domain.ResultList; -import org.folio.dcb.domain.dto.MaterialType; -import org.folio.dcb.integration.inventory.MaterialTypeClient; -import org.folio.dcb.service.impl.ItemServiceImpl; -import org.junit.jupiter.api.Test; -import org.mockito.InjectMocks; -import org.mockito.Mock; -import org.folio.spring.exception.NotFoundException; -import static org.junit.jupiter.api.Assertions.assertThrows; - -@UnitTest -@ExtendWith(MockitoExtension.class) -class InventoryItemServiceTest { - - @InjectMocks -private ItemServiceImpl itemService; - - @Mock -private MaterialTypeClient materialTypeClient; - - @Test -void fetchItemMaterialTypeIdByMaterialTypeNameTest() { - // TestMate-0f96f8c42d03650b357196259cdf7e6b - // Given - String materialTypeName = "book"; - String materialTypeId = "1a2b3c4d-5e6f-7g8h-9i0j"; - MaterialType materialType = MaterialType.builder() - .id(materialTypeId) - .name(materialTypeName) - .build(); - ResultList materialTypeResultList = ResultList.asSinglePage(materialType); - when(materialTypeClient.fetchMaterialTypeByQuery(anyString())).thenReturn(materialTypeResultList); - // When - String result = itemService.fetchItemMaterialTypeIdByMaterialTypeName(materialTypeName); - // Then - verify(materialTypeClient).fetchMaterialTypeByQuery("name==\"book\""); - assertEquals(materialTypeId, result); -} - - @Test -void fetchItemMaterialTypeIdByInvalidNameTest() { - // TestMate-772865b75acf5b12a75b8a6a3937b4b1 - // Given - String materialTypeName = "non-existent-type"; - when(materialTypeClient.fetchMaterialTypeByQuery(anyString())).thenReturn(ResultList.empty()); - // When - assertThrows(NotFoundException.class, () -> itemService.fetchItemMaterialTypeIdByMaterialTypeName(materialTypeName)); - // Then - verify(materialTypeClient).fetchMaterialTypeByQuery("name==\"non-existent-type\""); -} - - @Test -void fetchItemMaterialTypeIdByMultipleResultsTest() { - // TestMate-4b6d139cbb3dac517781e88fd0bd5828 - // Given - String materialTypeName = "text"; - String firstMaterialTypeId = "11111111-1111-1111-1111-111111111111"; - String secondMaterialTypeId = "22222222-2222-2222-2222-222222222222"; - MaterialType firstMaterialType = MaterialType.builder() - .id(firstMaterialTypeId) - .name(materialTypeName) - .build(); - MaterialType secondMaterialType = MaterialType.builder() - .id(secondMaterialTypeId) - .name(materialTypeName) - .build(); - ResultList materialTypeResultList = ResultList.asSinglePage(firstMaterialType, secondMaterialType); - when(materialTypeClient.fetchMaterialTypeByQuery(anyString())).thenReturn(materialTypeResultList); - // When - String result = itemService.fetchItemMaterialTypeIdByMaterialTypeName(materialTypeName); - // Then - verify(materialTypeClient).fetchMaterialTypeByQuery("name==\"text\""); - assertEquals(firstMaterialTypeId, result); -} -} diff --git a/src/test/java/org/folio/dcb/service/CalendarServiceTest.java b/src/test/java/org/folio/dcb/service/CalendarServiceTest.java index 46dc2804..a2f26726 100644 --- a/src/test/java/org/folio/dcb/service/CalendarServiceTest.java +++ b/src/test/java/org/folio/dcb/service/CalendarServiceTest.java @@ -1,12 +1,11 @@ package org.folio.dcb.service; +import static java.lang.Integer.MAX_VALUE; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.folio.dcb.utils.DcbConstants.DCB_CALENDAR_NAME; import static org.folio.dcb.utils.DcbConstants.SERVICE_POINT_ID; import static org.folio.dcb.utils.EntityUtils.getCalendarCollection; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.assertNull; -import static org.junit.jupiter.api.Assertions.assertThrows; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.never; import static org.mockito.Mockito.verify; @@ -14,22 +13,21 @@ import java.util.UUID; import org.folio.dcb.domain.dto.Calendar; +import org.folio.dcb.domain.dto.CalendarCollection; import org.folio.dcb.integration.calendar.CalendarClient; import org.folio.dcb.service.impl.CalendarServiceImpl; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; -import java.util.Collections; -import org.folio.dcb.domain.dto.CalendarCollection; -import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.CsvSource; @ExtendWith(MockitoExtension.class) class CalendarServiceTest { - @InjectMocks private CalendarServiceImpl calendarService; + @InjectMocks private CalendarServiceImpl calendarService; @Mock private CalendarClient calendarClient; @Test @@ -41,48 +39,46 @@ void testCreateCalendar() { @Test void testFindCalendarByName() { - when(calendarClient.getAllCalendars(Integer.MAX_VALUE)) - .thenReturn(getCalendarCollection(DCB_CALENDAR_NAME)); + when(calendarClient.getAllCalendars(MAX_VALUE)).thenReturn(getCalendarCollection(DCB_CALENDAR_NAME)); var response = calendarService.findCalendarByName(DCB_CALENDAR_NAME); - verify(calendarClient).getAllCalendars(Integer.MAX_VALUE); - assertNotNull(response); - assertEquals(DCB_CALENDAR_NAME, response.getName()); + verify(calendarClient).getAllCalendars(MAX_VALUE); + assertThat(response).isNotNull(); + assertThat(response.getName()).isEqualTo(DCB_CALENDAR_NAME); } @Test void testFindCalendar_InvalidName() { - when(calendarClient.getAllCalendars(Integer.MAX_VALUE)) - .thenReturn(getCalendarCollection(DCB_CALENDAR_NAME)); + when(calendarClient.getAllCalendars(MAX_VALUE)).thenReturn(getCalendarCollection(DCB_CALENDAR_NAME)); var response = calendarService.findCalendarByName(""); - verify(calendarClient).getAllCalendars(Integer.MAX_VALUE); - assertNull(response); + verify(calendarClient).getAllCalendars(MAX_VALUE); + assertThat(response).isNull(); } @Test void testAddServicePointIdToDefaultCalendar() { - when(calendarClient.getAllCalendars(Integer.MAX_VALUE)) - .thenReturn(getCalendarCollection(DCB_CALENDAR_NAME)); + when(calendarClient.getAllCalendars(MAX_VALUE)).thenReturn(getCalendarCollection(DCB_CALENDAR_NAME)); calendarService.addServicePointIdToDefaultCalendar(UUID.fromString(SERVICE_POINT_ID)); - verify(calendarClient).getAllCalendars(Integer.MAX_VALUE); + verify(calendarClient).getAllCalendars(MAX_VALUE); verify(calendarClient).updateCalendar(any(), any()); } @Test void testAddServicePointIdToDefaultCalendar_DefaultCalendarNotExists() { - when(calendarClient.getAllCalendars(Integer.MAX_VALUE)) - .thenReturn(getCalendarCollection("test")); + when(calendarClient.getAllCalendars(MAX_VALUE)).thenReturn(getCalendarCollection("test")); var servicePointId = UUID.randomUUID(); - assertThrows(IllegalArgumentException.class, - () -> calendarService.addServicePointIdToDefaultCalendar(servicePointId)); + assertThatThrownBy(() -> calendarService.addServicePointIdToDefaultCalendar(servicePointId)) + .isInstanceOf(IllegalArgumentException.class); } @Test void testAssociateServicePointIdWithDefaultCalendar_SpNotAssociated() { - when(calendarClient.getAllCalendars(Integer.MAX_VALUE)) + when(calendarClient.getAllCalendars(MAX_VALUE)) .thenReturn(getCalendarCollection(DCB_CALENDAR_NAME)); var servicePointId = UUID.randomUUID(); + calendarService.associateServicePointIdWithDefaultCalendarIfAbsent(servicePointId); - verify(calendarClient).getAllCalendars(Integer.MAX_VALUE); + + verify(calendarClient).getAllCalendars(MAX_VALUE); verify(calendarClient).updateCalendar(any(), any()); } @@ -92,10 +88,11 @@ void testAssociateServicePointIdWithDefaultCalendar_SpAssociated() { var calendarCollection = getCalendarCollection(DCB_CALENDAR_NAME); // Adding the sp in assignments to make sure that our code will not try to update the calendar again calendarCollection.getCalendars().get(0).getAssignments().add(servicePointId); - when(calendarClient.getAllCalendars(Integer.MAX_VALUE)) - .thenReturn(calendarCollection); + when(calendarClient.getAllCalendars(MAX_VALUE)).thenReturn(calendarCollection); + calendarService.associateServicePointIdWithDefaultCalendarIfAbsent(servicePointId); - verify(calendarClient).getAllCalendars(Integer.MAX_VALUE); + + verify(calendarClient).getAllCalendars(MAX_VALUE); verify(calendarClient, never()).updateCalendar(any(), any()); } @@ -103,49 +100,50 @@ void testAssociateServicePointIdWithDefaultCalendar_SpAssociated() { void testAssociateServicePointIdWithDefaultCalendar_DefaultCalendarNotExists() { var servicePointId = UUID.randomUUID(); var calendarCollection = getCalendarCollection("test"); - when(calendarClient.getAllCalendars(Integer.MAX_VALUE)) - .thenReturn(calendarCollection); - assertThrows(IllegalArgumentException.class, - () -> calendarService.associateServicePointIdWithDefaultCalendarIfAbsent(servicePointId)); + when(calendarClient.getAllCalendars(MAX_VALUE)).thenReturn(calendarCollection); + assertThatThrownBy(() -> calendarService.associateServicePointIdWithDefaultCalendarIfAbsent(servicePointId)) + .isInstanceOf(IllegalArgumentException.class); } - @Test - void testFindCalendarByName_EmptyCollection() { + @Test + void findCalendarByName_positive_emptyCollection() { // TestMate-38edfab7aa6fbd359b174576a85ed904 - // Given - var calendarCollection = new CalendarCollection(); - calendarCollection.setCalendars(Collections.emptyList()); - calendarCollection.setTotalRecords(0); - when(calendarClient.getAllCalendars(Integer.MAX_VALUE)).thenReturn(calendarCollection); - // When + var calendarCollection = new CalendarCollection().totalRecords(0); + when(calendarClient.getAllCalendars(MAX_VALUE)).thenReturn(calendarCollection); + var response = calendarService.findCalendarByName(DCB_CALENDAR_NAME); - // Then - verify(calendarClient).getAllCalendars(Integer.MAX_VALUE); - assertNull(response); + + verify(calendarClient).getAllCalendars(MAX_VALUE); + assertThat(response).isNull(); } - @ParameterizedTest + @Test + void findCalendarByName_positive_exactMatch() { + // TestMate-4b2bf27890ab20d8e60fc02676886bc0 + var calendarName = "Main Calendar"; + when(calendarClient.getAllCalendars(MAX_VALUE)).thenReturn(getCalendarCollection(calendarName)); + + var inputName = "Main Calendar"; + var response = calendarService.findCalendarByName(inputName); + + verify(calendarClient).getAllCalendars(MAX_VALUE); + assertThat(response).isNotNull(); + assertThat(calendarName).isEqualTo(response.getName()); + } + + @ParameterizedTest @CsvSource({ - "Main Calendar, true", - "Main, false", - "main calendar, false", - "'Main Calendar ', false" + "Main", + "main calendar", + "'Main Calendar '" }) - void testFindCalendarByName_ExactMatchLogic(String inputName, boolean shouldMatch) { - // TestMate-4b2bf27890ab20d8e60fc02676886bc0 - // Given - String existingCalendarName = "Main Calendar"; - when(calendarClient.getAllCalendars(Integer.MAX_VALUE)) - .thenReturn(getCalendarCollection(existingCalendarName)); - // When + void findCalendarByName_positive_notMatched(String inputName) { + var calendarName = "Main Calendar"; + when(calendarClient.getAllCalendars(MAX_VALUE)).thenReturn(getCalendarCollection(calendarName)); + var response = calendarService.findCalendarByName(inputName); - // Then - verify(calendarClient).getAllCalendars(Integer.MAX_VALUE); - if (shouldMatch) { - assertNotNull(response); - assertEquals(existingCalendarName, response.getName()); - } else { - assertNull(response); - } + + verify(calendarClient).getAllCalendars(MAX_VALUE); + assertThat(response).isNull(); } } diff --git a/src/test/java/org/folio/dcb/service/InventoryItemServiceTest.java b/src/test/java/org/folio/dcb/service/InventoryItemServiceTest.java index 1362886f..15202482 100644 --- a/src/test/java/org/folio/dcb/service/InventoryItemServiceTest.java +++ b/src/test/java/org/folio/dcb/service/InventoryItemServiceTest.java @@ -7,14 +7,19 @@ import static org.folio.dcb.utils.EntityUtils.createInventoryItem; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; import java.util.List; import java.util.UUID; import org.folio.dcb.domain.ResultList; import org.folio.dcb.domain.dto.ItemLastCheckIn; +import org.folio.dcb.domain.dto.MaterialType; +import org.folio.dcb.domain.dto.MaterialTypeCollection; import org.folio.dcb.exception.InventoryItemNotFound; import org.folio.dcb.integration.invstorage.InventoryItemStorageClient; +import org.folio.dcb.integration.invstorage.MaterialTypeClient; import org.folio.dcb.service.impl.ItemServiceImpl; import org.folio.dcb.utils.CqlQuery; import org.folio.spring.exception.NotFoundException; @@ -28,6 +33,7 @@ class InventoryItemServiceTest { @InjectMocks private ItemServiceImpl itemService; + @Mock private MaterialTypeClient materialTypeClient; @Mock private InventoryItemStorageClient inventoryItemStorageClient; @Test @@ -123,4 +129,50 @@ void findItemByIdAfterCheckInWhenItemIsWithInvalidServicePointId() { var expectedMessage = "Matched item not found: %s, %s".formatted(itemId, expectedServicePointId); assertEquals(expectedMessage, exception.getMessage()); } + + @Test + void fetchItemMaterialTypeIdByMaterialTypeNameTest() { + // TestMate-0f96f8c42d03650b357196259cdf7e6b + var materialTypeName = "book"; + var materialTypeId = "1a2b3c4d-5e6f-7g8h-9i0j"; + var materialType = new MaterialType().id(materialTypeId).name(materialTypeName); + var materialTypeResultList = new MaterialTypeCollection().addMtypesItem(materialType).totalRecords(1); + when(materialTypeClient.fetchMaterialTypeByQuery(anyString())).thenReturn(materialTypeResultList); + + var result = itemService.fetchItemMaterialTypeIdByMaterialTypeName(materialTypeName); + + verify(materialTypeClient).fetchMaterialTypeByQuery("name==\"book\""); + assertEquals(materialTypeId, result); + } + + @Test + void fetchItemMaterialTypeIdByInvalidNameTest() { + // TestMate-772865b75acf5b12a75b8a6a3937b4b1 + var name = "non-existent-type"; + var materialTypes = new MaterialTypeCollection().totalRecords(0); + when(materialTypeClient.fetchMaterialTypeByQuery(anyString())).thenReturn(materialTypes); + assertThrows(NotFoundException.class, () -> itemService.fetchItemMaterialTypeIdByMaterialTypeName(name)); + verify(materialTypeClient).fetchMaterialTypeByQuery("name==\"non-existent-type\""); + } + + @Test + void fetchItemMaterialTypeIdByMultipleResultsTest() { + // TestMate-4b6d139cbb3dac517781e88fd0bd5828 + var materialTypeName = "text"; + var mtypeId1 = "11111111-1111-1111-1111-111111111111"; + var mtypeId2 = "22222222-2222-2222-2222-222222222222"; + var firstMaterialType = new MaterialType().id(mtypeId1).name(materialTypeName); + var secondMaterialType = new MaterialType().id(mtypeId2).name(materialTypeName); + var materialTypeResultList = new MaterialTypeCollection() + .addMtypesItem(firstMaterialType) + .addMtypesItem(secondMaterialType) + .totalRecords(2); + + when(materialTypeClient.fetchMaterialTypeByQuery(anyString())).thenReturn(materialTypeResultList); + + var result = itemService.fetchItemMaterialTypeIdByMaterialTypeName(materialTypeName); + + verify(materialTypeClient).fetchMaterialTypeByQuery("name==\"text\""); + assertEquals(mtypeId1, result); + } } diff --git a/src/test/java/org/folio/dcb/service/PatronGroupServiceTest.java b/src/test/java/org/folio/dcb/service/PatronGroupServiceTest.java index 678be257..ea826964 100644 --- a/src/test/java/org/folio/dcb/service/PatronGroupServiceTest.java +++ b/src/test/java/org/folio/dcb/service/PatronGroupServiceTest.java @@ -1,12 +1,17 @@ package org.folio.dcb.service; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy; import static org.folio.dcb.utils.EntityUtils.createUserGroupCollection; import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertThrows; import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; +import java.util.List; +import org.folio.dcb.domain.dto.UserGroup; +import org.folio.dcb.domain.dto.UserGroupCollection; import org.folio.dcb.integration.users.GroupClient; import org.folio.dcb.service.impl.PatronGroupServiceImpl; import org.folio.spring.exception.NotFoundException; @@ -15,11 +20,6 @@ import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; -import java.util.Collections; -import static org.mockito.ArgumentMatchers.anyString; -import org.folio.dcb.domain.dto.UserGroupCollection; -import org.folio.dcb.domain.dto.UserGroup; -import java.util.List; @ExtendWith(MockitoExtension.class) class PatronGroupServiceTest { @@ -33,46 +33,41 @@ void fetchPatronGroupIdByNameTest() { when(groupClient.fetchGroupByName(any())).thenReturn(userGroupCollection); var response = patronGroupService.fetchPatronGroupIdByName("staff"); verify(groupClient).fetchGroupByName("group==\"staff\""); - assertEquals(userGroupCollection.getUsergroups().getFirst().getId(), response); + assertThat(userGroupCollection.getUsergroups().getFirst().getId()).isEqualTo(response); } @Test void fetchPatronGroupIdByInvalidNameTest() { var userGroupCollection = createUserGroupCollection(); when(groupClient.fetchGroupByName(any())).thenReturn(userGroupCollection); - assertThrows(NotFoundException.class, () -> patronGroupService.fetchPatronGroupIdByName("invalid")); + assertThatThrownBy(() -> patronGroupService.fetchPatronGroupIdByName("invalid")) + .isInstanceOf(NotFoundException.class); } - @Test + @Test void fetchPatronGroupIdByEmptyResultsTest() { // TestMate-b010671f8c95ebf096d763f89b1d27ad - // Given - String groupName = "unknown"; - UserGroupCollection emptyCollection = new UserGroupCollection(); - emptyCollection.setUsergroups(Collections.emptyList()); - emptyCollection.setTotalRecords(0); + var groupName = "unknown"; + var emptyCollection = new UserGroupCollection().totalRecords(0); when(groupClient.fetchGroupByName(anyString())).thenReturn(emptyCollection); - // When - assertThrows(NotFoundException.class, () -> patronGroupService.fetchPatronGroupIdByName(groupName)); - // Then + + assertThatThrownBy(() -> patronGroupService.fetchPatronGroupIdByName(groupName)) + .isInstanceOf(NotFoundException.class); verify(groupClient).fetchGroupByName("group==\"unknown\""); } - @Test + @Test void fetchPatronGroupIdByComplexNameTest() { // TestMate-53e3140766520ed0043bb0570807c250 - // Given - String groupName = "Faculty Staff"; - String expectedId = "44963503-625d-453b-9a87-c838e553940c"; - UserGroupCollection userGroupCollection = createUserGroupCollection(); - UserGroup userGroup = new UserGroup(); - userGroup.setGroup(groupName); - userGroup.setId(expectedId); + var groupName = "Faculty Staff"; + var expectedId = "44963503-625d-453b-9a87-c838e553940c"; + var userGroupCollection = createUserGroupCollection(); + var userGroup = new UserGroup().group(groupName).id(expectedId); userGroupCollection.setUsergroups(List.of(userGroup)); when(groupClient.fetchGroupByName(anyString())).thenReturn(userGroupCollection); - // When + var response = patronGroupService.fetchPatronGroupIdByName(groupName); - // Then + verify(groupClient).fetchGroupByName("group==\"Faculty Staff\""); assertEquals(expectedId, response); } diff --git a/src/test/java/org/folio/dcb/service/TransactionAuditServiceTest.java b/src/test/java/org/folio/dcb/service/TransactionAuditServiceTest.java index eb764593..c1dea282 100644 --- a/src/test/java/org/folio/dcb/service/TransactionAuditServiceTest.java +++ b/src/test/java/org/folio/dcb/service/TransactionAuditServiceTest.java @@ -1,5 +1,6 @@ package org.folio.dcb.service; +import static java.util.Optional.empty; import static org.assertj.core.api.Assertions.assertThat; import static org.folio.dcb.domain.dto.DcbTransaction.RoleEnum.BORROWER; import static org.folio.dcb.domain.dto.DcbTransaction.RoleEnum.LENDER; @@ -45,7 +46,7 @@ void logTheErrorForExistedTransactionAuditTest() { @Test void logTheErrorForNotExistedTransactionAuditTest() { var transaction = createDcbTransactionByRole(LENDER); - when(repository.findLatestTransactionAuditEntityByDcbTransactionId(any())).thenReturn(Optional.empty()); + when(repository.findLatestTransactionAuditEntityByDcbTransactionId(any())).thenReturn(empty()); transactionAuditService.logErrorIfTransactionAuditNotExists(DCB_TRANSACTION_ID, transaction, "error_message"); verify(repository).save(any()); } @@ -122,7 +123,7 @@ void testLogErrorIfTransactionAuditNotExistsWhenDcbTransactionIsNull() { var dcbTransactionId = "trn-456"; var errorMsg = "Null payload"; var auditCaptor = ArgumentCaptor.forClass(TransactionAuditEntity.class); - when(repository.findLatestTransactionAuditEntityByDcbTransactionId(dcbTransactionId)).thenReturn(Optional.empty()); + when(repository.findLatestTransactionAuditEntityByDcbTransactionId(dcbTransactionId)).thenReturn(empty()); when(repository.save(auditCaptor.capture())).then(inv -> inv.getArgument(0)); transactionAuditService.logErrorIfTransactionAuditNotExists(dcbTransactionId, null, errorMsg); @@ -136,15 +137,14 @@ void testLogErrorIfTransactionAuditNotExistsWhenDcbTransactionIsNull() { "dcbTransactionId = trn-456; role = null; error message = Null payload."); } - @Test + @Test void testLogErrorIfTransactionAuditExistsWhenAuditNotFoundShouldNotSave() { // TestMate-1a0c9f10f62a5a52977c51aeacb5f897 - // Given var errorMsg = "error_message"; - when(repository.findLatestTransactionAuditEntityByDcbTransactionId(DCB_TRANSACTION_ID)).thenReturn(Optional.empty()); - // When + when(repository.findLatestTransactionAuditEntityByDcbTransactionId(DCB_TRANSACTION_ID)).thenReturn(empty()); + transactionAuditService.logErrorIfTransactionAuditExists(DCB_TRANSACTION_ID, errorMsg); - // Then + verify(repository).findLatestTransactionAuditEntityByDcbTransactionId(DCB_TRANSACTION_ID); verify(repository, never()).save(any()); verify(transactionMapper, never()).mapToEntity(any(), any());