diff --git a/CabManagementSystem/src/main/java/com/safar/service/WalletServices.java b/CabManagementSystem/src/main/java/com/safar/service/WalletServices.java index a841354..7be1d40 100644 --- a/CabManagementSystem/src/main/java/com/safar/service/WalletServices.java +++ b/CabManagementSystem/src/main/java/com/safar/service/WalletServices.java @@ -2,6 +2,7 @@ import com.safar.entity.Transactions; import com.safar.entity.Wallet; +import com.safar.exceptions.WalletException; import java.util.List; @@ -55,7 +56,7 @@ public interface WalletServices { * @param id The ID of the wallet to retrieve. * @return The wallet details. */ - public Wallet getWallet(Integer id); + public Wallet getWallet(Integer id) throws WalletException; /** * Retrieves the wallet associated with a logged-in user by their email. diff --git a/CabManagementSystem/src/main/java/com/safar/service/serviceImpl/WalletServicesImpl.java b/CabManagementSystem/src/main/java/com/safar/service/serviceImpl/WalletServicesImpl.java index 60e07e9..a1812b1 100644 --- a/CabManagementSystem/src/main/java/com/safar/service/serviceImpl/WalletServicesImpl.java +++ b/CabManagementSystem/src/main/java/com/safar/service/serviceImpl/WalletServicesImpl.java @@ -197,7 +197,7 @@ public Wallet createWallet(String email) { * @return The wallet details. */ @Override - public Wallet getWallet(Integer id){ + public Wallet getWallet(Integer id)throws WalletException{ Wallet res= wrepo.findById(id).orElseThrow(()->new WalletException("Wallet not found")); return res; } diff --git a/CabManagementSystem/src/test/java/com/safar/controller/WalletControllerTest.java b/CabManagementSystem/src/test/java/com/safar/controller/WalletControllerTest.java index cd89f47..966f365 100644 --- a/CabManagementSystem/src/test/java/com/safar/controller/WalletControllerTest.java +++ b/CabManagementSystem/src/test/java/com/safar/controller/WalletControllerTest.java @@ -4,6 +4,7 @@ import com.safar.entity.Users; import com.safar.entity.Wallet; import com.safar.entity.WalletStatus; +import com.safar.exceptions.WalletException; import com.safar.service.UserService; import com.safar.service.WalletServices; import org.junit.jupiter.api.Assertions; @@ -125,4 +126,44 @@ public void testChangeStatus_WhenValidUserLoggedIn_thenWalletStatusCanBeChanged( } -} + @Test + @DisplayName("Wallet object can be find") + public void testGetWallet_WhenValidWalletIdProvided_ThenReturnsWalletObject() throws Exception{ + Mockito.when(walletServices.getWallet(anyInt())).thenReturn(responseWallet); + + RequestBuilder requestBuilder = MockMvcRequestBuilders.get("/WALLET/getWallet/{wid}",1).contentType(MediaType.APPLICATION_JSON_VALUE); + MvcResult mvcResult= mockMvc.perform(requestBuilder).andExpect(status().isAccepted()).andReturn(); + String responseBodyAsString= mvcResult.getResponse().getContentAsString(); + Wallet resWallet= new ObjectMapper().readValue(responseBodyAsString,Wallet.class); + Assertions.assertNotNull(resWallet.getWalletId(),"Wallet should have a id"); + Assertions.assertEquals(responseWallet.getWalletId(),resWallet.getWalletId(),"Wallet id should be equal to 1"); + Mockito.verify(walletServices,Mockito.times(1)).getWallet(anyInt()); + + } + + + @Test + @DisplayName("Wallet object can be find for logged user") + public void testGetLoggedUserWallet()throws Exception{ + Mockito.when(walletServices.getLoggedUserWallet(anyString())).thenReturn(responseWallet); + + RequestBuilder requestBuilder = MockMvcRequestBuilders.get("/WALLET/WalletDetails").principal(auth).contentType(MediaType.APPLICATION_JSON_VALUE); + MvcResult mvcResult = mockMvc.perform(requestBuilder).andExpect(status().isAccepted()).andReturn(); + String responseBodyAsString= mvcResult.getResponse().getContentAsString(); + Wallet resWallet= new ObjectMapper().readValue(responseBodyAsString,Wallet.class); + Assertions.assertNotNull(resWallet.getWalletId(),"Wallet should have a id"); + Assertions.assertEquals(responseWallet.getWalletId(),resWallet.getWalletId(),"Wallet id should be equal to 1"); + Mockito.verify(walletServices,Mockito.times(1)).getLoggedUserWallet(anyString()); + + } + @Test + @DisplayName("Wallet Exception can be thrown") + public void testGetLoggedUserWallet_WhenNullAuthenticationPassed_shouldThrowWalletException() throws Exception { + auth= new UsernamePasswordAuthenticationToken(null,null); + Mockito.when(walletServices.getLoggedUserWallet(anyString())).thenReturn(responseWallet); + + RequestBuilder requestBuilder= MockMvcRequestBuilders.get("/WALLET/WalletDetails").principal(auth).contentType(MediaType.APPLICATION_JSON_VALUE); + MvcResult mvcResult= mockMvc.perform(requestBuilder).andExpect(result->Assertions.assertTrue(result.getResolvedException() instanceof WalletException)).andReturn(); + } + } +