Skip to content

Commit

Permalink
test cases wallet controller completed
Browse files Browse the repository at this point in the history
  • Loading branch information
akt0001c committed Mar 5, 2024
1 parent 0504b7c commit 1c68029
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.safar.entity.Transactions;
import com.safar.entity.Wallet;
import com.safar.exceptions.WalletException;

import java.util.List;

Expand Down Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
}

0 comments on commit 1c68029

Please sign in to comment.