Skip to content

Commit

Permalink
Merge pull request #123 from kouamschekina/redo-implement-and-test-se…
Browse files Browse the repository at this point in the history
…nd-money-service

feat(backend): implemented and wrote test for the send money service
  • Loading branch information
Motouom authored Apr 9, 2024
2 parents c75a7f5 + b53bae8 commit 5c0c0be
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
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,7 @@
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) {
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);

}


}

0 comments on commit 5c0c0be

Please sign in to comment.