Skip to content

Commit

Permalink
Merge pull request #139 from kouamschekina/feature/21_Implement_rest_…
Browse files Browse the repository at this point in the history
…api_for_sendMoney

feat(backend): Implemented the rest API for sendMoney
  • Loading branch information
kouamschekina authored Jun 14, 2024
2 parents 172c991 + d610b2c commit 31d7543
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.adorsys.gis.powerpay.powerpaybackend.services;
package com.adorsys.gis.powerpay.powerpaybackend.controller;

import com.adorsys.gis.powerpay.powerpaybackend.domain.User;
import com.adorsys.gis.powerpay.powerpaybackend.repository.UserRepository;
import com.adorsys.gis.powerpay.powerpaybackend.services.CheckBalance;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.adorsys.gis.powerpay.powerpaybackend.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.adorsys.gis.powerpay.powerpaybackend.domain.Transaction;
import com.adorsys.gis.powerpay.powerpaybackend.services.SendMoney;
import com.adorsys.gis.powerpay.powerpaybackend.errorhandling.InsufficientFundsException;

@RestController
@RequestMapping("/api/transaction")
public class SendMoneyController{
@Autowired
private final SendMoney sendMoneyService;

public SendMoneyController(SendMoney sendMoneyService){
this.sendMoneyService = sendMoneyService;
}

@PostMapping()
public Transaction sendMoney(@RequestBody Transaction request){
try {
Transaction transaction = sendMoneyService.send(
request.getPhoneNumber(),
request.getReceiverPhoneNumber(),
request.getAmount(),
request.getCurrency(),
request.getId()
);
return transaction;
} catch (InsufficientFundsException e) {
throw new IllegalArgumentException("Insufficient funds", e);
} catch (Exception e) {
throw new RuntimeException("Internal server error", e);
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class SendMoneyImpl implements SendMoney{
@Override
public Transaction send(String phoneNumber, String receiverPhoneNumber, Double amount, String currency, Integer id)
throws InsufficientFundsException, TransactionException {
if (phoneNumber == null) {
if (receiverPhoneNumber == null) {
throw new IllegalArgumentException("Phone number cannot be null.");
}
if (amount <= 0) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.adorsys.gis.powerpay.powerpaybackend.controllers_tests;

import org.junit.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import com.adorsys.gis.powerpay.powerpaybackend.controller.SendMoneyController;
import com.adorsys.gis.powerpay.powerpaybackend.domain.Transaction;
import com.adorsys.gis.powerpay.powerpaybackend.services.SendMoneyImpl;
import com.fasterxml.jackson.databind.ObjectMapper;

import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@RunWith(SpringRunner.class)
@WebMvcTest(SendMoneyController.class)
public class SendMoneyControllerTest {

@Autowired
private MockMvc mockMvc;
@Autowired
private ObjectMapper objectMapper;
@MockBean private SendMoneyImpl sendMoney;

@BeforeEach
public void setup(){
Transaction transaction = new Transaction();
transaction.setPhoneNumber("1234567890");
transaction.setReceiverPhoneNumber("0987654321");
transaction.setAmount(100.0);
transaction.setCurrency("FCFA");
transaction.setId(1);
when(sendMoney.send("1234567890", "0987654321", 100.0, "FCFA", 1)).thenReturn(transaction);

}
@Test
@WithMockUser(username = "user", password = "password", roles = "USER")
public void testSendMoney() throws Exception {
Transaction request = new Transaction();
request.setPhoneNumber("1234567890");
request.setReceiverPhoneNumber("0987654321");
request.setAmount(100.0);
request.setCurrency("FCFA");
request.setId(1);
String jsonRequest = objectMapper.writeValueAsString(request);

mockMvc.perform(post("/api/transaction")
.contentType("application/json")
.content(jsonRequest)
.with(SecurityMockMvcRequestPostProcessors.csrf().asHeader()))
.andExpect(status().isOk());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public void testSend_NullPhoneNumber_ExceptionThrown() {

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

Expand Down

0 comments on commit 31d7543

Please sign in to comment.