diff --git a/power-pay-backend/src/main/java/com/adorsys/gis/powerpay/powerpaybackend/services/CheckBalanceController.java b/power-pay-backend/src/main/java/com/adorsys/gis/powerpay/powerpaybackend/controller/CheckBalanceController.java similarity index 91% rename from power-pay-backend/src/main/java/com/adorsys/gis/powerpay/powerpaybackend/services/CheckBalanceController.java rename to power-pay-backend/src/main/java/com/adorsys/gis/powerpay/powerpaybackend/controller/CheckBalanceController.java index 6fff0e91..ebdb5dbd 100644 --- a/power-pay-backend/src/main/java/com/adorsys/gis/powerpay/powerpaybackend/services/CheckBalanceController.java +++ b/power-pay-backend/src/main/java/com/adorsys/gis/powerpay/powerpaybackend/controller/CheckBalanceController.java @@ -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; diff --git a/power-pay-backend/src/main/java/com/adorsys/gis/powerpay/powerpaybackend/controller/SendMoneyController.java b/power-pay-backend/src/main/java/com/adorsys/gis/powerpay/powerpaybackend/controller/SendMoneyController.java new file mode 100644 index 00000000..4edfab79 --- /dev/null +++ b/power-pay-backend/src/main/java/com/adorsys/gis/powerpay/powerpaybackend/controller/SendMoneyController.java @@ -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); + } + + } + +} \ No newline at end of file diff --git a/power-pay-backend/src/main/java/com/adorsys/gis/powerpay/powerpaybackend/services/SendMoneyImpl.java b/power-pay-backend/src/main/java/com/adorsys/gis/powerpay/powerpaybackend/services/SendMoneyImpl.java index 0f299c9c..38eddb25 100644 --- a/power-pay-backend/src/main/java/com/adorsys/gis/powerpay/powerpaybackend/services/SendMoneyImpl.java +++ b/power-pay-backend/src/main/java/com/adorsys/gis/powerpay/powerpaybackend/services/SendMoneyImpl.java @@ -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) { diff --git a/power-pay-backend/src/test/java/com/adorsys/gis/powerpay/powerpaybackend/controllers_tests/SendMoneyControllerTest.java b/power-pay-backend/src/test/java/com/adorsys/gis/powerpay/powerpaybackend/controllers_tests/SendMoneyControllerTest.java new file mode 100644 index 00000000..486387aa --- /dev/null +++ b/power-pay-backend/src/test/java/com/adorsys/gis/powerpay/powerpaybackend/controllers_tests/SendMoneyControllerTest.java @@ -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()); + } +} diff --git a/power-pay-backend/src/test/java/com/adorsys/gis/powerpay/powerpaybackend/services_tests/SendMoneyImplTest.java b/power-pay-backend/src/test/java/com/adorsys/gis/powerpay/powerpaybackend/services_tests/SendMoneyImplTest.java index 2d49076b..a2f59387 100644 --- a/power-pay-backend/src/test/java/com/adorsys/gis/powerpay/powerpaybackend/services_tests/SendMoneyImplTest.java +++ b/power-pay-backend/src/test/java/com/adorsys/gis/powerpay/powerpaybackend/services_tests/SendMoneyImplTest.java @@ -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); }); }