Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(backend): implemented the send money service #103

Closed
wants to merge 18 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
359ccde
feat(backend): implemented the send money service
Koufan-De-King Mar 22, 2024
938f146
fix(backend): created the transaction object using try and catch
kouamschekina Mar 22, 2024
d29d144
fix: removed TransactionRepository.java
kouamschekina Mar 22, 2024
4af8aaa
Merge pull request #2 from kouamschekina/fix/remove-unnecessary-file
kouamschekina Mar 22, 2024
f6fb62e
fix(backend): modified function signature in the send money implement…
Koufan-De-King Mar 24, 2024
f54f8d0
fix(backend): adjusted the send function of the SendMoneyImpl class
kouamschekina Mar 25, 2024
efafac2
fix(backend): unautowired transaction in SendMoneyImpl class
Koufan-De-King Mar 26, 2024
442b3b0
test(backend): wrote the test for the SendMoneyImpl class
kouamschekina Mar 27, 2024
73da876
test(backend): wrote the test for the SendMoneyImpl class
kouamschekina Mar 27, 2024
c2ceeb9
Merge branch 'feature/20-Send-money-functionality' of github.com:koua…
kouamschekina Mar 27, 2024
1cba9d9
test(backend): wrote the test for the SendMoneyImpl class
kouamschekina Mar 27, 2024
ed5abbe
test(backend): wrote a test for the SendMoneyImpl class
kouamschekina Mar 27, 2024
9583648
Merge branch 'feature/20-Send-money-functionality' of github.com:koua…
Koufan-De-King Apr 5, 2024
16cbcf9
fix(test): reimplemented the SendMoneyImplTest using @MockBean
kouamschekina Apr 6, 2024
6d6fd4a
Merge branch 'feature/20-Send-money-functionality' of github.com:koua…
kouamschekina Apr 6, 2024
266e01c
Merge branch 'feature/20-Send-money-functionality' of github.com:koua…
kouamschekina Apr 6, 2024
b1133bb
Merge branch 'feature/20-Send-money-functionality' of github.com:koua…
Koufan-De-King Apr 9, 2024
880faa5
Merge branch 'ADORSYS-GIS:main' into feature/20-Send-money-functionality
Koufan-De-King Apr 9, 2024
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.adorsys.gis.powerpay.powerpaybackend.errorhandling;

public class InsufficientFundsException extends RuntimeException {

private final String message;

public InsufficientFundsException(String message) {
super(message);
this.message = message;
}

@Override
public String getMessage() {
return message;
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.adorsys.gis.powerpay.powerpaybackend.repository;

import com.adorsys.gis.powerpay.powerpaybackend.domain.Transaction;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface MoneyTransferRepository extends JpaRepository<Transaction, String> {
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.adorsys.gis.powerpay.powerpaybackend.services;

import com.adorsys.gis.powerpay.powerpaybackend.domain.Transaction;

//implementing an empty interface
public interface SendMoney {
}
Transaction send(String phoneNumber, String receiverPhoneNumber, Double amount, String currency, Integer id);
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,55 @@
package com.adorsys.gis.powerpay.powerpaybackend.services;

import com.adorsys.gis.powerpay.powerpaybackend.domain.ProcedureStatus;
import com.adorsys.gis.powerpay.powerpaybackend.domain.Transaction;
import com.adorsys.gis.powerpay.powerpaybackend.errorhandling.InsufficientFundsException;
import com.adorsys.gis.powerpay.powerpaybackend.repository.MoneyTransferRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.TransactionException;

@Service
public class SendMoneyImpl implements SendMoney{

@Autowired
private MoneyTransferRepository moneyTransferRepository;


@Override
public Transaction send(String phoneNumber, String receiverPhoneNumber, Double amount, String currency, Integer id)
throws InsufficientFundsException, TransactionException {
if (phoneNumber == null) {
throw new IllegalArgumentException("Phone number cannot be null.");
}
if (amount <= 0) {
throw new IllegalArgumentException("Transfer amount must be positive.");
}

if (!hasSufficientFunds(phoneNumber, amount)) {
throw new InsufficientFundsException("Insufficient funds for transfer.");
}

Transaction transaction = new Transaction();
try {
if (transaction != null) {
stephane-segning marked this conversation as resolved.
Show resolved Hide resolved
transaction.setReceiverPhoneNumber(receiverPhoneNumber);
transaction.setAmount(amount);
transaction.setCurrency(currency);
transaction.setStatus(ProcedureStatus.WAITING);
transaction.setId(id);
transaction.setPhoneNumber(phoneNumber);

transaction = moneyTransferRepository.save(transaction);
}
} catch (Exception e) {
throw new TransactionException("Failed to save transaction: " + e.getMessage(), e) {
};
}

return transaction;
}

private boolean hasSufficientFunds(String phoneNumber, Double amount) {
return true;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package com.adorsys.gis.powerpay.powerpaybackend.services_tests;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;

import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.context.junit4.SpringRunner;

import com.adorsys.gis.powerpay.powerpaybackend.domain.Transaction;
import com.adorsys.gis.powerpay.powerpaybackend.repository.MoneyTransferRepository;
import com.adorsys.gis.powerpay.powerpaybackend.services.SendMoneyImpl;

@RunWith(SpringRunner.class)
@SpringBootTest
public class SendMoneyImplTest {

@Autowired
private SendMoneyImpl sendMoneyImpl;

@MockBean
private MoneyTransferRepository moneyTransferRepository;
@Test
public void testSend_NullPhoneNumber_ExceptionThrown() {
// Arrange

// Act & Assert
assertThrows(IllegalArgumentException.class, () -> {
sendMoneyImpl.send(null, "1234567890", 100.0, "XAF", 1);
});
}

@Test
public void testSend_NegativeAmount_ExceptionThrown() {
// Arrange

// Act & Assert
assertThrows(IllegalArgumentException.class, () -> {
sendMoneyImpl.send("1234567890", "0987654321", -100.0, "XAF", 1);
});
}

@Test
public void testSend_SuccessfulTransaction() {
// Arrange
Transaction awaitedTransaction = new Transaction();
when(moneyTransferRepository.save(Mockito.any(Transaction.class))).thenReturn(awaitedTransaction);
// Act
Transaction result = sendMoneyImpl.send("1234567890", "0987654321", 100.0, "XAF", 1);

// Assert
assertNotNull(result);

assertEquals(awaitedTransaction, result);

}


}