diff --git a/account-management/src/main/java/cz/muni/pa165/banking/application/service/AccountService.java b/account-management/src/main/java/cz/muni/pa165/banking/application/service/AccountService.java index 7966ac928fa96e72dc37f10086edfbcc5c6aba97..7f05cffc21711316aa42e398aceef014779ec57f 100644 --- a/account-management/src/main/java/cz/muni/pa165/banking/application/service/AccountService.java +++ b/account-management/src/main/java/cz/muni/pa165/banking/application/service/AccountService.java @@ -67,10 +67,17 @@ public class AccountService { } @Transactional(rollbackFor = Exception.class) - public ScheduledPayment createNewScheduledPayment(String sender, String receiver, BigDecimal amount, RecurrenceType recurrenceType, Integer day) { + public ScheduledPayment createNewScheduledPayment(String sender, String receiver, BigDecimal amount, RecurrenceType recurrenceType, Integer day) throws EntityNotFoundException { Account senderAccount = findByNumber(sender); Account receiverAccount = findByNumber(receiver); + if (senderAccount == null) { + throw new EntityNotFoundException("Account with id: " + senderAccount + " was not found."); + } + if (receiverAccount == null) { + throw new EntityNotFoundException("Account with id: " + receiverAccount + " was not found."); + } + ScheduledPayment newScheduledPayment = new ScheduledPayment(); newScheduledPayment.setSourceAccountId(senderAccount.getId()); newScheduledPayment.setTargetAccountId(receiverAccount.getId()); diff --git a/account-management/src/main/java/cz/muni/pa165/banking/application/service/UserService.java b/account-management/src/main/java/cz/muni/pa165/banking/application/service/UserService.java index 7c805631a9bf9cf1eab7b7729d55c13db0cee4fd..38a8e1fb925c29e422821595dbd2397434e14a27 100644 --- a/account-management/src/main/java/cz/muni/pa165/banking/application/service/UserService.java +++ b/account-management/src/main/java/cz/muni/pa165/banking/application/service/UserService.java @@ -3,6 +3,7 @@ package cz.muni.pa165.banking.application.service; import cz.muni.pa165.banking.domain.user.User; import cz.muni.pa165.banking.domain.user.repository.UserRepository; import cz.muni.pa165.banking.exception.EntityNotFoundException; +import jakarta.persistence.EntityExistsException; import org.springframework.stereotype.Service; @Service @@ -14,7 +15,10 @@ public class UserService { this.userRepository = userRepository; } - public User createUser(User user){ + public User createUser(User user) throws EntityExistsException { + if (userRepository.existsById(user.getId())){ + throw new EntityExistsException("User with id " + user.getId() + " already exists"); + } return userRepository.save(user); } diff --git a/account-management/src/main/java/cz/muni/pa165/banking/domain/account/Account.java b/account-management/src/main/java/cz/muni/pa165/banking/domain/account/Account.java index 31bb6c7736ed9d2b133595062521322e5a16d66c..72ef64d4fbe91e9b7c9868ba4c629177d1a2a765 100644 --- a/account-management/src/main/java/cz/muni/pa165/banking/domain/account/Account.java +++ b/account-management/src/main/java/cz/muni/pa165/banking/domain/account/Account.java @@ -3,6 +3,7 @@ package cz.muni.pa165.banking.domain.account; import jakarta.persistence.*; import java.util.Currency; +import java.util.Objects; @Entity @Table(name = "bank_account") @@ -73,4 +74,28 @@ public class Account { return currency; } + @Override + public String toString() { + return "Account{" + + "id=" + id + + ", accountNumber='" + accountNumber + '\'' + + ", userId=" + userId + + ", maxSpendingLimit=" + maxSpendingLimit + + ", type=" + type + + ", currency=" + currency + + '}'; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + Account account = (Account) o; + return Objects.equals(id, account.id) && Objects.equals(accountNumber, account.accountNumber) && Objects.equals(userId, account.userId) && Objects.equals(maxSpendingLimit, account.maxSpendingLimit) && type == account.type && Objects.equals(currency, account.currency); + } + + @Override + public int hashCode() { + return Objects.hash(id, accountNumber, userId, maxSpendingLimit, type, currency); + } } diff --git a/account-management/src/main/java/cz/muni/pa165/banking/domain/account/repository/AccountRepository.java b/account-management/src/main/java/cz/muni/pa165/banking/domain/account/repository/AccountRepository.java index 0874a8d6d92304108bf240d8c0fe17bed10f8b45..34f769e5475e3c04922b7f7ff8241db2ba571475 100644 --- a/account-management/src/main/java/cz/muni/pa165/banking/domain/account/repository/AccountRepository.java +++ b/account-management/src/main/java/cz/muni/pa165/banking/domain/account/repository/AccountRepository.java @@ -2,13 +2,13 @@ package cz.muni.pa165.banking.domain.account.repository; import cz.muni.pa165.banking.domain.account.Account; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; import org.springframework.stereotype.Repository; import java.util.Optional; @Repository public interface AccountRepository extends JpaRepository<Account, Long> { - - Optional<Account> findByAccountNumber(String accountNumber); + Optional<Account> findByAccountNumber(String accountNumber); } diff --git a/account-management/src/main/java/cz/muni/pa165/banking/domain/scheduled/ScheduledPayment.java b/account-management/src/main/java/cz/muni/pa165/banking/domain/scheduled/ScheduledPayment.java index ce7404d5380bb1bbe889ee4b445d749844de8cb8..297caf873b856ec5a5e906dce02cacd82759e94a 100644 --- a/account-management/src/main/java/cz/muni/pa165/banking/domain/scheduled/ScheduledPayment.java +++ b/account-management/src/main/java/cz/muni/pa165/banking/domain/scheduled/ScheduledPayment.java @@ -4,6 +4,7 @@ import cz.muni.pa165.banking.domain.scheduled.recurrence.Recurrence; import jakarta.persistence.*; import java.math.BigDecimal; +import java.util.Objects; @Entity @Table(name = "scheduled_payment") @@ -83,4 +84,29 @@ public class ScheduledPayment { public void setRecurrence(Recurrence recurrence) { this.recurrence = recurrence; } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + ScheduledPayment that = (ScheduledPayment) o; + return Objects.equals(id, that.id) && Objects.equals(sourceAccountId, that.sourceAccountId) && Objects.equals(targetAccountId, that.targetAccountId) && Objects.equals(amount, that.amount) && Objects.equals(currencyCode, that.currencyCode) && Objects.equals(recurrence, that.recurrence); + } + + @Override + public int hashCode() { + return Objects.hash(id, sourceAccountId, targetAccountId, amount, currencyCode, recurrence); + } + + @Override + public String toString() { + return "ScheduledPayment{" + + "id=" + id + + ", sourceAccountId=" + sourceAccountId + + ", targetAccountId=" + targetAccountId + + ", amount=" + amount + + ", currencyCode='" + currencyCode + '\'' + + ", recurrence=" + recurrence + + '}'; + } } diff --git a/account-management/src/main/java/cz/muni/pa165/banking/domain/scheduled/repository/ScheduledPaymentRepository.java b/account-management/src/main/java/cz/muni/pa165/banking/domain/scheduled/repository/ScheduledPaymentRepository.java index 1a53f1fe9eee2e349ed7e6ced7937fe848b2206b..14f89b9728656ea20b084aa274689e9d23bd79e0 100644 --- a/account-management/src/main/java/cz/muni/pa165/banking/domain/scheduled/repository/ScheduledPaymentRepository.java +++ b/account-management/src/main/java/cz/muni/pa165/banking/domain/scheduled/repository/ScheduledPaymentRepository.java @@ -3,6 +3,7 @@ package cz.muni.pa165.banking.domain.scheduled.repository; import cz.muni.pa165.banking.domain.scheduled.ScheduledPayment; import org.springframework.data.jpa.domain.Specification; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; import org.springframework.stereotype.Repository; import java.util.List; @@ -11,7 +12,7 @@ import java.util.List; public interface ScheduledPaymentRepository extends JpaRepository<ScheduledPayment, Long> { List<ScheduledPayment> findAll(Specification<ScheduledPayment> spec); - + List<ScheduledPayment> findBySourceAccountId(Long accountId); } diff --git a/account-management/src/main/java/cz/muni/pa165/banking/domain/user/User.java b/account-management/src/main/java/cz/muni/pa165/banking/domain/user/User.java index e9b901c8e1b15485967c6a9437cedc7b3728fed3..a96b9c0d2940e4c9f250c23a0dc4af172ff77b31 100644 --- a/account-management/src/main/java/cz/muni/pa165/banking/domain/user/User.java +++ b/account-management/src/main/java/cz/muni/pa165/banking/domain/user/User.java @@ -3,6 +3,8 @@ package cz.muni.pa165.banking.domain.user; import jakarta.persistence.*; import jakarta.validation.constraints.Email; +import java.util.Objects; + @Entity @Table(name = "bank_user") public class User { @@ -10,6 +12,7 @@ public class User { @GeneratedValue(strategy = GenerationType.SEQUENCE) @Column(name = "id") private Long id; + @Email @Column(name = "email") private String email; @@ -73,4 +76,28 @@ public class User { this.userType = userType; } + @Override + public String toString() { + return "User{" + + "id=" + id + + ", email='" + email + '\'' + + ", password='" + password + '\'' + + ", firstName='" + firstName + '\'' + + ", lastName='" + lastName + '\'' + + ", userType=" + userType + + '}'; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + User user = (User) o; + return Objects.equals(id, user.id) && Objects.equals(email, user.email) && Objects.equals(password, user.password) && Objects.equals(firstName, user.firstName) && Objects.equals(lastName, user.lastName) && userType == user.userType; + } + + @Override + public int hashCode() { + return Objects.hash(id, email, password, firstName, lastName, userType); + } } diff --git a/account-management/src/main/java/cz/muni/pa165/banking/domain/user/repository/UserRepository.java b/account-management/src/main/java/cz/muni/pa165/banking/domain/user/repository/UserRepository.java index 8d2db82e1a981c828bd13ebb26664f9a34a05ddd..c431c235799c4d4b580dc092afd3fbadf1ba6717 100644 --- a/account-management/src/main/java/cz/muni/pa165/banking/domain/user/repository/UserRepository.java +++ b/account-management/src/main/java/cz/muni/pa165/banking/domain/user/repository/UserRepository.java @@ -2,12 +2,7 @@ package cz.muni.pa165.banking.domain.user.repository; import cz.muni.pa165.banking.domain.user.User; import org.springframework.data.jpa.repository.JpaRepository; -import org.springframework.data.jpa.repository.Query; import org.springframework.stereotype.Repository; -import java.util.Optional; - @Repository -public interface UserRepository extends JpaRepository<User, Long> { - -} +public interface UserRepository extends JpaRepository<User, Long> {} diff --git a/account-management/src/test/java/cz/muni/pa165/banking/application/controller/AccountControllerIT.java b/account-management/src/test/java/cz/muni/pa165/banking/application/controller/AccountControllerIT.java index 21a4e41241df5c9e1c6bd9cdde8d8878c8179a11..f3db8259f6d5f576fcb5ac88248c0735afb8ba79 100644 --- a/account-management/src/test/java/cz/muni/pa165/banking/application/controller/AccountControllerIT.java +++ b/account-management/src/test/java/cz/muni/pa165/banking/application/controller/AccountControllerIT.java @@ -76,7 +76,7 @@ class AccountControllerIT { } @Test - void createAccount_userExists_returnsCreated() throws Exception { + void createAccount_UserExists_ReturnsCreated() throws Exception { when(balanceApi.createBalance(anyString())).thenReturn(ResponseEntity.ok().build()); mockMvc.perform(post("/account") @@ -92,7 +92,7 @@ class AccountControllerIT { @Test - void createAccount_userNotExist_returnsNotFound() throws Exception { + void createAccount_UserNotExist_ReturnsNotFound() throws Exception { mockMvc.perform(post("/account") .contentType(MediaType.APPLICATION_JSON) .content("{\"userId\":3,\"type\":\"SAVING\",\"maxSpendingLimit\":1000, \"currency\": \"CZK\"}")) @@ -103,7 +103,7 @@ class AccountControllerIT { } @Test - void findAccountById_accountFound_returnsOk() throws Exception { + void findAccountById_AccountFound_ReturnsOk() throws Exception { mockMvc.perform(get("/account?accountId=1") .contentType(MediaType.APPLICATION_JSON)) .andExpect(status().isOk()) @@ -113,7 +113,7 @@ class AccountControllerIT { } @Test - void findAccountById_accountNotFound_returnsNotFound() throws Exception { + void findAccountById_AccountNotFound_ReturnsNotFound() throws Exception { mockMvc.perform(get("/account?accountId=3") .contentType(MediaType.APPLICATION_JSON)) .andExpect(status().isNotFound()) @@ -124,7 +124,7 @@ class AccountControllerIT { @Test - void findAccountByAccountNumber_accountFound_returnsOk() throws Exception { + void findAccountByAccountNumber_AccountFound_ReturnsOk() throws Exception { mockMvc.perform(get("/account/number?accountNumber=abc") .contentType(MediaType.APPLICATION_JSON)) .andExpect(status().isOk()) @@ -134,7 +134,7 @@ class AccountControllerIT { } @Test - void findAccountByAccountNumber_accountNotFound_returnsNotFound() throws Exception { + void findAccountByAccountNumber_AccountNotFound_ReturnsNotFound() throws Exception { mockMvc.perform(get("/account/number?accountNumber=abcde") .contentType(MediaType.APPLICATION_JSON)) .andExpect(status().isNotFound()) @@ -144,7 +144,7 @@ class AccountControllerIT { } @Test - void getScheduledPayments_accountFound_returnsOk() throws Exception { + void getScheduledPayments_AccountFound_ReturnsOk() throws Exception { mockMvc.perform(get("/account/scheduled?accountNumber=abc") .contentType(MediaType.APPLICATION_JSON)) .andExpect(status().isOk()) @@ -154,7 +154,7 @@ class AccountControllerIT { } @Test - void getScheduledPayments_accountNotFound_returnsNotFound() throws Exception { + void getScheduledPayments_AccountNotFound_ReturnsNotFound() throws Exception { mockMvc.perform(get("/account/scheduled?accountNumber=3") .contentType(MediaType.APPLICATION_JSON)) .andExpect(status().isNotFound()) @@ -164,7 +164,7 @@ class AccountControllerIT { } @Test - void schedulePayment_accountFound_returnsCreated() throws Exception { + void schedulePayment_AccountFound_ReturnsCreated() throws Exception { mockMvc.perform(post("/account/scheduled") .contentType(MediaType.APPLICATION_JSON) .content("{\"senderAccount\":\"abc\", \"receiverAccount\":\"abcd\", \"amount\":1, \"type\":\"WEEKLY\", \"day\":1}")) @@ -175,7 +175,7 @@ class AccountControllerIT { } @Test - void schedulePayment_accountNotFound_returnsNotFound() throws Exception { + void schedulePayment_AccountNotFound_ReturnsNotFound() throws Exception { mockMvc.perform(post("/account/scheduled") .contentType(MediaType.APPLICATION_JSON) .content("{\"senderAccount\":\"abc\", \"receiverAccount\":\"abcde\", \"amount\":1, \"type\":\"WEEKLY\", \"day\":1}")) diff --git a/account-management/src/test/java/cz/muni/pa165/banking/application/controller/UserControllerIT.java b/account-management/src/test/java/cz/muni/pa165/banking/application/controller/UserControllerIT.java index 6185f87743b867f1dc35fd8472227176a5850e10..a1bafe066533a346092bf3ee6b9c6e09b80480fb 100644 --- a/account-management/src/test/java/cz/muni/pa165/banking/application/controller/UserControllerIT.java +++ b/account-management/src/test/java/cz/muni/pa165/banking/application/controller/UserControllerIT.java @@ -39,7 +39,7 @@ class UserControllerIT { } @Test - void createUser_returnsCreated() throws Exception { + void createUser_ReturnsCreated() throws Exception { mockMvc.perform(post("/user") .contentType(MediaType.APPLICATION_JSON) .content("{\"email\":\"123@email.cz\",\"password\":\"passwd123\",\"firstName\":\"Joe\", \"lastName\": \"Mama\", \"userType\": \"REGULAR\"}")) @@ -50,7 +50,7 @@ class UserControllerIT { } @Test - void findUserById_userFound_returnsOk() throws Exception { + void findUserById_UserFound_ReturnsOk() throws Exception { mockMvc.perform(get("/user?userId=1") .contentType(MediaType.APPLICATION_JSON)) .andExpect(status().isOk()) @@ -60,7 +60,7 @@ class UserControllerIT { } @Test - void findUserById_userNotFound_returnsNotFound() throws Exception { + void findUserById_UserNotFound_ReturnsNotFound() throws Exception { mockMvc.perform(get("/user?userId=3") .contentType(MediaType.APPLICATION_JSON)) .andExpect(status().isNotFound()) diff --git a/account-management/src/test/java/cz/muni/pa165/banking/application/service/AccountServiceTest.java b/account-management/src/test/java/cz/muni/pa165/banking/application/service/AccountServiceTest.java index d145506cfad69e694d53f502786af95b34f48dc2..82f3608e06b9fd10e970261bbe327e1e7b37a2bc 100644 --- a/account-management/src/test/java/cz/muni/pa165/banking/application/service/AccountServiceTest.java +++ b/account-management/src/test/java/cz/muni/pa165/banking/application/service/AccountServiceTest.java @@ -5,6 +5,8 @@ import cz.muni.pa165.banking.domain.account.Account; import cz.muni.pa165.banking.domain.account.repository.AccountRepository; import cz.muni.pa165.banking.domain.scheduled.ScheduledPayment; import cz.muni.pa165.banking.domain.scheduled.ScheduledPaymentProjection; +import cz.muni.pa165.banking.domain.scheduled.recurrence.Recurrence; +import cz.muni.pa165.banking.domain.scheduled.recurrence.RecurrenceType; import cz.muni.pa165.banking.domain.scheduled.repository.ScheduledPaymentRepository; import cz.muni.pa165.banking.domain.user.repository.UserRepository; import cz.muni.pa165.banking.exception.EntityNotFoundException; @@ -13,7 +15,12 @@ import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; +import org.springframework.data.jpa.domain.Specification; + +import java.math.BigDecimal; +import java.time.LocalDate; import java.util.ArrayList; +import java.util.Currency; import java.util.List; import java.util.Optional; @@ -39,7 +46,7 @@ class AccountServiceTest { private AccountService accountService; @Test - void createAccount_ValidAccount_ReturnsAccount() { + void createAccount_UserExists_ReturnsAccount() { // Arrange Account account = new Account(); account.setId(1L); @@ -57,6 +64,24 @@ class AccountServiceTest { verify(accountRepository).save(account); } + @Test + void createAccount_UserNotExists_ThrowsException() { + // Arrange + Account account = new Account(); + account.setId(1L); + account.setAccountNumber("123"); + account.setUserId(1L); + + when(userRepository.existsById(anyLong())).thenReturn(false); + + // Act & Assert + assertThrows(Exception.class, () -> { + accountService.createAccount(account); + }); + + verify(accountRepository, never()).save(account); + } + @Test void getAccount_ValidAccountId_ReturnsAccount(){ // Arrange @@ -142,14 +167,76 @@ class AccountServiceTest { assertThrows(EntityNotFoundException.class, () -> { accountService.schedulePayment(scheduledPayment); }); + + verify(scheduledPaymentRepository, never()).save(scheduledPayment); } @Test - void getScheduledPaymentsOfAccount_ValidAccountId_ReturnsScheduledPayments() { + void createNewScheduledPayment_ValidScheduledPayment_ReturnsScheduledPayment() { + // Arrange + String accountNumber1 = "1"; + Account account1 = new Account(); + account1.setAccountNumber(accountNumber1); + account1.setId(1L); + account1.setCurrency(Currency.getInstance("CZK")); + + String accountNumber2 = "2"; + Account account2 = new Account(); + account2.setAccountNumber(accountNumber2); + account2.setId(2L); + + BigDecimal amount = BigDecimal.TEN; + RecurrenceType recurrenceType = RecurrenceType.MONTHLY; + Integer day = 15; + + when(accountRepository.findByAccountNumber(accountNumber1)).thenReturn(Optional.of(account1)); + when(accountRepository.findByAccountNumber(accountNumber2)).thenReturn(Optional.of(account2)); + + ScheduledPayment scheduledPayment = new ScheduledPayment(); + scheduledPayment.setSourceAccountId(account1.getId()); + scheduledPayment.setTargetAccountId(account2.getId()); + scheduledPayment.setAmount(amount); + scheduledPayment.setCurrencyCode(account1.getCurrency().getCurrencyCode()); + + Recurrence recurrence = new Recurrence(); + recurrence.setType(recurrenceType); + recurrence.setPaymentDay(day); + scheduledPayment.setRecurrence(recurrence); + + when(scheduledPaymentRepository.save(any(ScheduledPayment.class))).thenReturn(scheduledPayment); + + // Act + ScheduledPayment result = accountService.createNewScheduledPayment(accountNumber1, accountNumber2, amount, recurrenceType, day); + + // Assert + assertEquals(scheduledPayment, result); + verify(scheduledPaymentRepository).save(any(ScheduledPayment.class)); + } + + @Test + void createNewScheduledPayment_InvalidAccountNumber_ThrowsException() { + // Arrange + String accountNumber1 = "1"; + String accountNumber2 = "2"; + BigDecimal amount = BigDecimal.TEN; + RecurrenceType recurrenceType = RecurrenceType.MONTHLY; + Integer day = 15; + + when(accountRepository.findByAccountNumber(accountNumber1)).thenReturn(Optional.empty()); + + // Act & Assert + assertThrows(Exception.class, () -> { + accountService.createNewScheduledPayment(accountNumber1, accountNumber2, amount, recurrenceType, day); + }); + } + + @Test + void findScheduledPaymentsByAccount_ValidAccountNumber_ReturnsScheduledPayments() { // Arrange String accountNumber = "123123123"; Account account = new Account(); - List<ScheduledPayment> scheduledPayments = new ArrayList<>(); + account.setAccountNumber(accountNumber); + List<ScheduledPaymentProjection> scheduledPayments = new ArrayList<>(); when(accountRepository.findByAccountNumber(accountNumber)).thenReturn(Optional.of(account)); // Act @@ -158,4 +245,35 @@ class AccountServiceTest { // Assert assertEquals(scheduledPayments, result); } + + @Test + void findScheduledPaymentsByAccount_InvalidAccountNumber_ThrowsException() { + // Arrange + String accountNumber = "123123123"; + when(accountRepository.findByAccountNumber(accountNumber)).thenReturn(Optional.empty()); + + // Act & Assert + assertThrows(EntityNotFoundException.class, () -> { + accountService.findScheduledPaymentsByAccount(accountNumber); + }); + } + + @Test + void scheduledPaymentsOfDay_ReturnsScheduledPaymentsForGivenDay() { + // Arrange + LocalDate date = LocalDate.of(2024, 4, 24); + List<ScheduledPayment> expectedPayments = new ArrayList<>(); + when(scheduledPaymentRepository.findAll(any(Specification.class))) + .thenAnswer(invocation -> { + Specification<ScheduledPayment> spec = invocation.getArgument(0); + return expectedPayments; + }); + + // Act + List<ScheduledPayment> result = accountService.scheduledPaymentsOfDay(date); + + // Assert + assertEquals(expectedPayments, result); + verify(scheduledPaymentRepository).findAll(any(Specification.class)); + } } \ No newline at end of file diff --git a/account-management/src/test/java/cz/muni/pa165/banking/application/service/UserServiceTest.java b/account-management/src/test/java/cz/muni/pa165/banking/application/service/UserServiceTest.java index eb88965414a48981894df8d5e8b83d186178954f..4ce112017005b792ec2dd84bd654c27cfa4aa559 100644 --- a/account-management/src/test/java/cz/muni/pa165/banking/application/service/UserServiceTest.java +++ b/account-management/src/test/java/cz/muni/pa165/banking/application/service/UserServiceTest.java @@ -11,8 +11,7 @@ import org.mockito.junit.jupiter.MockitoExtension; import java.util.Optional; import static org.junit.jupiter.api.Assertions.*; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; +import static org.mockito.Mockito.*; @ExtendWith(MockitoExtension.class) class UserServiceTest { @@ -27,6 +26,7 @@ class UserServiceTest { void createUser_ValidUser_ReturnsUser() { // Arrange User user = new User(); + user.setId(1L); when(userRepository.save(user)).thenReturn(user); // Act @@ -37,6 +37,21 @@ class UserServiceTest { verify(userRepository).save(user); } + @Test + void createUser_UserIdExists_ThrowsException() { + // Arrange + User user = new User(); + user.setId(1L); + when(userRepository.existsById(user.getId())).thenReturn(true); + + // Act & Assert + assertThrows(Exception.class, () -> { + userService.createUser(user); + }); + + verify(userRepository, never()).save(user); + } + @Test void getUser_ValidUserId_ReturnsUser(){ // Arrange diff --git a/account-management/src/test/java/cz/muni/pa165/banking/domain/account/repository/AccountRepositoryTest.java b/account-management/src/test/java/cz/muni/pa165/banking/domain/account/repository/AccountRepositoryTest.java new file mode 100644 index 0000000000000000000000000000000000000000..77dee1915d655fc8200d9dd09c75745d03bd636b --- /dev/null +++ b/account-management/src/test/java/cz/muni/pa165/banking/domain/account/repository/AccountRepositoryTest.java @@ -0,0 +1,52 @@ +package cz.muni.pa165.banking.domain.account.repository; + +import cz.muni.pa165.banking.domain.account.Account; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.junit.jupiter.MockitoExtension; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; + +import java.util.Optional; + +import static org.junit.jupiter.api.Assertions.*; + +@ExtendWith(MockitoExtension.class) +@DataJpaTest +class AccountRepositoryTest { + @Autowired + private AccountRepository accountRepository; + + @BeforeAll + public static void initDb(@Autowired AccountRepository repository) { + Account account = new Account(); + account.setId(1L); + account.setAccountNumber("1"); + repository.save(account); + } + + + @Test + void findByAccountNumber_ValidNumber_ReturnsAccount() { + // Arrange + Account expectedAccount = new Account(); + expectedAccount.setId(1L); + expectedAccount.setAccountNumber("1"); + + // Act + Optional<Account> result = accountRepository.findByAccountNumber("1"); + + // Assert + assertEquals(expectedAccount, result.orElse(null)); + } + + @Test + void findByAccountNumber_InvalidNumber_ReturnsEmptyOptional() { + // Act + Optional<Account> result = accountRepository.findByAccountNumber("2"); + + // Assert + assertFalse(result.isPresent()); + } +} \ No newline at end of file diff --git a/account-management/src/test/java/cz/muni/pa165/banking/domain/scheduled/recurrence/RecurrenceQuerySpecificationBuilderTest.java b/account-management/src/test/java/cz/muni/pa165/banking/domain/scheduled/repository/ScheduledPaymentRepositoryTest.java similarity index 61% rename from account-management/src/test/java/cz/muni/pa165/banking/domain/scheduled/recurrence/RecurrenceQuerySpecificationBuilderTest.java rename to account-management/src/test/java/cz/muni/pa165/banking/domain/scheduled/repository/ScheduledPaymentRepositoryTest.java index 790acc28648291e91aa87d5d33f7ef1c4d8ffca2..2db1080ba9f3af968274bb2ddb65075ef3cfe4e3 100644 --- a/account-management/src/test/java/cz/muni/pa165/banking/domain/scheduled/recurrence/RecurrenceQuerySpecificationBuilderTest.java +++ b/account-management/src/test/java/cz/muni/pa165/banking/domain/scheduled/repository/ScheduledPaymentRepositoryTest.java @@ -1,7 +1,9 @@ -package cz.muni.pa165.banking.domain.scheduled.recurrence; +package cz.muni.pa165.banking.domain.scheduled.repository; import cz.muni.pa165.banking.domain.scheduled.ScheduledPayment; -import cz.muni.pa165.banking.domain.scheduled.repository.ScheduledPaymentRepository; +import cz.muni.pa165.banking.domain.scheduled.recurrence.Recurrence; +import cz.muni.pa165.banking.domain.scheduled.recurrence.RecurrenceQuerySpecificationBuilder; +import cz.muni.pa165.banking.domain.scheduled.recurrence.RecurrenceType; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; @@ -16,29 +18,66 @@ import static org.junit.jupiter.api.Assertions.*; @ExtendWith(SpringExtension.class) @DataJpaTest -class RecurrenceQuerySpecificationBuilderTest { +class ScheduledPaymentRepositoryTest { @Autowired private ScheduledPaymentRepository repository; @BeforeAll public static void initDb(@Autowired ScheduledPaymentRepository repository) { - repository.save(ScheduledPaymentBuilder.create(RecurrenceType.MONTHLY, 1)); - repository.save(ScheduledPaymentBuilder.create(RecurrenceType.MONTHLY, 2)); - repository.save(ScheduledPaymentBuilder.create(RecurrenceType.MONTHLY, 3)); - repository.save(ScheduledPaymentBuilder.create(RecurrenceType.MONTHLY, 3)); - repository.save(ScheduledPaymentBuilder.create(RecurrenceType.MONTHLY, 3)); - repository.save(ScheduledPaymentBuilder.create(RecurrenceType.MONTHLY, 31)); - repository.save(ScheduledPaymentBuilder.create(RecurrenceType.MONTHLY, 30)); - repository.save(ScheduledPaymentBuilder.create(RecurrenceType.MONTHLY, 29)); - repository.save(ScheduledPaymentBuilder.create(RecurrenceType.MONTHLY, 28)); - repository.save(ScheduledPaymentBuilder.create(RecurrenceType.MONTHLY, 27)); - - repository.save(ScheduledPaymentBuilder.create(RecurrenceType.WEEKLY, 1)); - repository.save(ScheduledPaymentBuilder.create(RecurrenceType.WEEKLY, 1)); - repository.save(ScheduledPaymentBuilder.create(RecurrenceType.WEEKLY, 3)); - repository.save(ScheduledPaymentBuilder.create(RecurrenceType.WEEKLY, 3)); - repository.save(ScheduledPaymentBuilder.create(RecurrenceType.WEEKLY, 3)); + repository.save(ScheduledPaymentBuilder.create(1L, RecurrenceType.MONTHLY, 1)); + repository.save(ScheduledPaymentBuilder.create(1L, RecurrenceType.MONTHLY, 2)); + repository.save(ScheduledPaymentBuilder.create(2L, RecurrenceType.MONTHLY, 3)); + repository.save(ScheduledPaymentBuilder.create(2L, RecurrenceType.MONTHLY, 3)); + repository.save(ScheduledPaymentBuilder.create(2L, RecurrenceType.MONTHLY, 3)); + repository.save(ScheduledPaymentBuilder.create(2L, RecurrenceType.MONTHLY, 31)); + repository.save(ScheduledPaymentBuilder.create(2L, RecurrenceType.MONTHLY, 30)); + repository.save(ScheduledPaymentBuilder.create(2L, RecurrenceType.MONTHLY, 29)); + repository.save(ScheduledPaymentBuilder.create(2L, RecurrenceType.MONTHLY, 28)); + repository.save(ScheduledPaymentBuilder.create(2L, RecurrenceType.MONTHLY, 27)); + + repository.save(ScheduledPaymentBuilder.create(2L, RecurrenceType.WEEKLY, 1)); + repository.save(ScheduledPaymentBuilder.create(2L, RecurrenceType.WEEKLY, 1)); + repository.save(ScheduledPaymentBuilder.create(2L, RecurrenceType.WEEKLY, 3)); + repository.save(ScheduledPaymentBuilder.create(2L, RecurrenceType.WEEKLY, 3)); + repository.save(ScheduledPaymentBuilder.create(2L, RecurrenceType.WEEKLY, 3)); + } + + @Test + void findAll_ReturnsAllPayments(){ + // When + List<ScheduledPayment> foundPayments = repository.findAll(); + + // Then + assertEquals(15, foundPayments.size()); + } + + @Test + void findBySourceAccountId_ValidId_ReturnsPayment() { + // Given + long validAccountId = 1L; + + // When + List<ScheduledPayment> foundPayments = repository.findBySourceAccountId(validAccountId); + + // Then + assertNotNull(foundPayments); + assertEquals(2, foundPayments.size()); + assertEquals(1L, foundPayments.get(0).getId()); + assertEquals(2L, foundPayments.get(1).getId()); + } + + @Test + void findBySourceAccountId_InvalidId_ReturnsEmptyList() { + // Given + long invalidAccountId = 999L; + + // When + List<ScheduledPayment> foundPayments = repository.findBySourceAccountId(invalidAccountId); + + // Then + assertNotNull(foundPayments); + assertTrue(foundPayments.isEmpty()); } @Test @@ -118,9 +157,10 @@ class RecurrenceQuerySpecificationBuilderTest { private static long id = 1L; - static ScheduledPayment create(RecurrenceType type, int paymentDay) { + static ScheduledPayment create(Long senderAccountId, RecurrenceType type, int paymentDay) { ScheduledPayment result = new ScheduledPayment(); result.setId(id++); + result.setSourceAccountId(senderAccountId); Recurrence recurrence = new Recurrence(); recurrence.setType(type); recurrence.setPaymentDay(paymentDay); @@ -128,7 +168,5 @@ class RecurrenceQuerySpecificationBuilderTest { return result; } - } - } \ No newline at end of file