-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #123 from kouamschekina/redo-implement-and-test-se…
…nd-money-service feat(backend): implemented and wrote test for the send money service
- Loading branch information
Showing
5 changed files
with
137 additions
and
0 deletions.
There are no files selected for viewing
16 changes: 16 additions & 0 deletions
16
...va/com/adorsys/gis/powerpay/powerpaybackend/errorhandling/InsufficientFundsException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
...ain/java/com/adorsys/gis/powerpay/powerpaybackend/repository/MoneyTransferRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> { | ||
} |
2 changes: 2 additions & 0 deletions
2
...ay-backend/src/main/java/com/adorsys/gis/powerpay/powerpaybackend/services/SendMoney.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
47 changes: 47 additions & 0 deletions
47
...ackend/src/main/java/com/adorsys/gis/powerpay/powerpaybackend/services/SendMoneyImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
63 changes: 63 additions & 0 deletions
63
.../test/java/com/adorsys/gis/powerpay/powerpaybackend/services_tests/SendMoneyImplTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
||
} | ||
|
||
|
||
} |