-
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 #139 from kouamschekina/feature/21_Implement_rest_…
…api_for_sendMoney feat(backend): Implemented the rest API for sendMoney
- Loading branch information
Showing
5 changed files
with
107 additions
and
3 deletions.
There are no files selected for viewing
4 changes: 3 additions & 1 deletion
4
...kend/services/CheckBalanceController.java → ...nd/controller/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
42 changes: 42 additions & 0 deletions
42
...rc/main/java/com/adorsys/gis/powerpay/powerpaybackend/controller/SendMoneyController.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,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); | ||
} | ||
|
||
} | ||
|
||
} |
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
60 changes: 60 additions & 0 deletions
60
...a/com/adorsys/gis/powerpay/powerpaybackend/controllers_tests/SendMoneyControllerTest.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,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()); | ||
} | ||
} |
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