-
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 #104 from NkwaTambe/feature/4-Implement-the-checkB…
…alance Implement REST API endpoint, service, and tests for CheckBalance feature
- Loading branch information
Showing
8 changed files
with
143 additions
and
5 deletions.
There are no files selected for viewing
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
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
15 changes: 15 additions & 0 deletions
15
.../main/java/com/adorsys/gis/powerpay/powerpaybackend/repository/TransactionRepository.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,15 @@ | ||
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; | ||
import org.springframework.data.jpa.repository.Query; | ||
|
||
@Repository | ||
public interface TransactionRepository extends JpaRepository<Transaction, Long> { | ||
|
||
@Query("SELECT SUM(CASE WHEN t.receiverPhoneNumber = :phoneNumber THEN t.amount ELSE -t.amount END) " + | ||
"FROM Transaction t WHERE t.receiverPhoneNumber = :phoneNumber OR t.phoneNumber = :phoneNumber") | ||
|
||
Double calculateBalaceByPhoneNumber(String phoneNumber); | ||
} |
13 changes: 13 additions & 0 deletions
13
...end/src/main/java/com/adorsys/gis/powerpay/powerpaybackend/repository/UserRepository.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,13 @@ | ||
package com.adorsys.gis.powerpay.powerpaybackend.repository; | ||
|
||
import com.adorsys.gis.powerpay.powerpaybackend.domain.Transaction; | ||
import com.adorsys.gis.powerpay.powerpaybackend.domain.User; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.stereotype.Repository; | ||
|
||
import java.util.Optional; | ||
|
||
@Repository | ||
public interface UserRepository extends JpaRepository<Transaction, Long> { | ||
Optional<User> findByUserId(String userId); | ||
} |
3 changes: 3 additions & 0 deletions
3
...backend/src/main/java/com/adorsys/gis/powerpay/powerpaybackend/services/CheckBalance.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,8 @@ | ||
package com.adorsys.gis.powerpay.powerpaybackend.services; | ||
|
||
import org.springframework.security.core.userdetails.UsernameNotFoundException; | ||
|
||
//implementing an empty interface | ||
public interface CheckBalance { | ||
Double checkBalance(String userId) throws UsernameNotFoundException; | ||
} |
41 changes: 41 additions & 0 deletions
41
...c/main/java/com/adorsys/gis/powerpay/powerpaybackend/services/CheckBalanceController.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,41 @@ | ||
package com.adorsys.gis.powerpay.powerpaybackend.services; | ||
|
||
import com.adorsys.gis.powerpay.powerpaybackend.domain.User; | ||
import com.adorsys.gis.powerpay.powerpaybackend.repository.UserRepository; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import java.util.Optional; | ||
|
||
@RestController | ||
@RequestMapping("/api/balance") | ||
public class CheckBalanceController { | ||
@Autowired | ||
private final CheckBalance checkBalanceService; | ||
private final UserRepository userRepository; | ||
|
||
@Autowired | ||
public CheckBalanceController(CheckBalance checkBalanceService,UserRepository userRepository){ | ||
this.checkBalanceService = checkBalanceService; | ||
this.userRepository = userRepository; | ||
} | ||
|
||
@GetMapping("/{userId}") | ||
public ResponseEntity<String> checkBalance(@PathVariable String userId){ | ||
Optional<User> user = userRepository.findByUserId(userId); | ||
if(user.isPresent()){ | ||
Double balance = checkBalanceService.checkBalance(userId); | ||
return ResponseEntity.ok("Balance for user ID" + " : " +balance); | ||
} | ||
else { | ||
return ResponseEntity.status(HttpStatus.NOT_FOUND).body("User not found"); | ||
} | ||
} | ||
} | ||
|
||
|
18 changes: 17 additions & 1 deletion
18
...end/src/main/java/com/adorsys/gis/powerpay/powerpaybackend/services/CheckBalanceImpl.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,24 @@ | ||
package com.adorsys.gis.powerpay.powerpaybackend.services; | ||
|
||
import com.adorsys.gis.powerpay.powerpaybackend.repository.TransactionRepository; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.security.core.userdetails.UsernameNotFoundException; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service // annotation to ensure classes are recognized as Spring Beans | ||
public class CheckBalanceImpl implements CheckBalance { | ||
|
||
private final TransactionRepository transactionRepository; | ||
|
||
@Autowired | ||
public CheckBalanceImpl(TransactionRepository transactionRepository){ | ||
|
||
this.transactionRepository = transactionRepository; | ||
} | ||
|
||
@Override | ||
public Double checkBalance(String phoneNumber) throws UsernameNotFoundException{ | ||
Double balance = transactionRepository.calculateBalaceByPhoneNumber(phoneNumber); | ||
return balance != null ? balance : 0.0; | ||
} | ||
} | ||
|
52 changes: 48 additions & 4 deletions
52
...c/test/java/com/adorsys/gis/powerpay/powerpaybackend/services_tests/CheckBalanceTest.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,14 +1,58 @@ | ||
package com.adorsys.gis.powerpay.powerpaybackend.services_tests; | ||
|
||
import com.adorsys.gis.powerpay.powerpaybackend.domain.Transaction; | ||
import com.adorsys.gis.powerpay.powerpaybackend.repository.TransactionRepository; | ||
import com.adorsys.gis.powerpay.powerpaybackend.services.CheckBalance; | ||
import com.adorsys.gis.powerpay.powerpaybackend.services.CheckBalanceImpl; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.Mock; | ||
import org.mockito.Mockito; | ||
import org.mockito.MockitoAnnotations; | ||
|
||
import com.adorsys.gis.powerpay.powerpaybackend.services.CheckBalance; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class CheckBalanceTest { | ||
|
||
@Mock | ||
private TransactionRepository transactionRepository; | ||
|
||
private CheckBalance checkBalanceService; | ||
|
||
@BeforeEach | ||
public void setUp() { | ||
MockitoAnnotations.openMocks(this); | ||
checkBalanceService = new CheckBalanceImpl(transactionRepository); | ||
} | ||
|
||
@Test | ||
public void testCheckBalance() { | ||
// Create a mock object of the CheckBalance interface | ||
CheckBalance checkBalance = Mockito.mock(CheckBalance.class); | ||
|
||
List<Transaction> transactions = new ArrayList<>(); | ||
Transaction transaction1 = new Transaction(); | ||
transaction1.setReceiverPhoneNumber("1234567890"); | ||
transaction1.setAmount(500.0); | ||
transactions.add(transaction1); | ||
|
||
Transaction transaction2 = new Transaction(); | ||
transaction2.setPhoneNumber("1234567890"); | ||
transaction2.setAmount(200.0); | ||
transactions.add(transaction2); | ||
|
||
Mockito.when(transactionRepository.calculateBalaceByPhoneNumber(Mockito.anyString())).thenReturn(700.0); | ||
|
||
Double balance = checkBalanceService.checkBalance("1234567890"); | ||
|
||
Assertions.assertEquals(700.0, balance); | ||
} | ||
|
||
@Test | ||
public void testCheckBalanceWithNoTransactions() { | ||
Mockito.when(transactionRepository.calculateBalaceByPhoneNumber(Mockito.anyString())).thenReturn(null); | ||
|
||
Double balance = checkBalanceService.checkBalance("1234567890"); | ||
|
||
Assertions.assertEquals(0.0, balance); | ||
} | ||
} |