Skip to content

Commit

Permalink
Merge pull request #104 from NkwaTambe/feature/4-Implement-the-checkB…
Browse files Browse the repository at this point in the history
…alance

Implement REST API endpoint, service, and tests for CheckBalance feature
  • Loading branch information
NkwaTambe authored Apr 9, 2024
2 parents 5c0c0be + e4650c1 commit 2868306
Show file tree
Hide file tree
Showing 8 changed files with 143 additions and 5 deletions.
1 change: 1 addition & 0 deletions power-pay-backend/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
package com.adorsys.gis.powerpay.powerpaybackend.domain;

import jakarta.persistence.Entity;
import jakarta.persistence.Table;

@Entity
@Table(name = "Transaction")
public class Transaction extends Procedure {
private String receiverPhoneNumber;
private Double amount;
private String currency;


{
currency = "XAF";
}


public String getReceiverPhoneNumber() {
return receiverPhoneNumber;
}
Expand All @@ -35,4 +39,5 @@ public String getCurrency() {
public void setCurrency(String currency) {
this.currency = currency;
}

}
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);
}
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);
}
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;
}
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");
}
}
}


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;
}
}

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);
}
}

0 comments on commit 2868306

Please sign in to comment.