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
102 changes: 72 additions & 30 deletions src/test/java/org/folio/dcb/service/CalendarServiceTest.java
Original file line number Diff line number Diff line change
@@ -1,31 +1,33 @@
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;
import static org.mockito.Mockito.when;

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;

@ExtendWith(MockitoExtension.class)
class CalendarServiceTest {
@InjectMocks private CalendarServiceImpl calendarService;

@InjectMocks private CalendarServiceImpl calendarService;
@Mock private CalendarClient calendarClient;

@Test
Expand All @@ -37,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());
}

Expand All @@ -88,20 +88,62 @@ 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());
}

@Test
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 findCalendarByName_positive_emptyCollection() {
// TestMate-38edfab7aa6fbd359b174576a85ed904
var calendarCollection = new CalendarCollection().totalRecords(0);
when(calendarClient.getAllCalendars(MAX_VALUE)).thenReturn(calendarCollection);

var response = calendarService.findCalendarByName(DCB_CALENDAR_NAME);

verify(calendarClient).getAllCalendars(MAX_VALUE);
assertThat(response).isNull();
}

@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",
"main calendar",
"'Main Calendar '"
})
void findCalendarByName_positive_notMatched(String inputName) {
var calendarName = "Main Calendar";
when(calendarClient.getAllCalendars(MAX_VALUE)).thenReturn(getCalendarCollection(calendarName));

var response = calendarService.findCalendarByName(inputName);

verify(calendarClient).getAllCalendars(MAX_VALUE);
assertThat(response).isNull();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -28,6 +33,7 @@
class InventoryItemServiceTest {

@InjectMocks private ItemServiceImpl itemService;
@Mock private MaterialTypeClient materialTypeClient;
@Mock private InventoryItemStorageClient inventoryItemStorageClient;

@Test
Expand Down Expand Up @@ -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);
}
}
40 changes: 37 additions & 3 deletions src/test/java/org/folio/dcb/service/PatronGroupServiceTest.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -28,13 +33,42 @@ 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
void fetchPatronGroupIdByEmptyResultsTest() {
// TestMate-b010671f8c95ebf096d763f89b1d27ad
var groupName = "unknown";
var emptyCollection = new UserGroupCollection().totalRecords(0);
when(groupClient.fetchGroupByName(anyString())).thenReturn(emptyCollection);

assertThatThrownBy(() -> patronGroupService.fetchPatronGroupIdByName(groupName))
.isInstanceOf(NotFoundException.class);
verify(groupClient).fetchGroupByName("group==\"unknown\"");
}

@Test
void fetchPatronGroupIdByComplexNameTest() {
// TestMate-53e3140766520ed0043bb0570807c250
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);

var response = patronGroupService.fetchPatronGroupIdByName(groupName);

verify(groupClient).fetchGroupByName("group==\"Faculty Staff\"");
assertEquals(expectedId, response);
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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());
}
Expand Down Expand Up @@ -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);
Expand All @@ -135,4 +136,17 @@ void testLogErrorIfTransactionAuditNotExistsWhenDcbTransactionIsNull() {
assertThat(savedAudit.getErrorMessage()).isEqualTo(
"dcbTransactionId = trn-456; role = null; error message = Null payload.");
}

@Test
void testLogErrorIfTransactionAuditExistsWhenAuditNotFoundShouldNotSave() {
// TestMate-1a0c9f10f62a5a52977c51aeacb5f897
var errorMsg = "error_message";
when(repository.findLatestTransactionAuditEntityByDcbTransactionId(DCB_TRANSACTION_ID)).thenReturn(empty());

transactionAuditService.logErrorIfTransactionAuditExists(DCB_TRANSACTION_ID, errorMsg);

verify(repository).findLatestTransactionAuditEntityByDcbTransactionId(DCB_TRANSACTION_ID);
verify(repository, never()).save(any());
verify(transactionMapper, never()).mapToEntity(any(), any());
}
}
Loading