Skip to content

Commit

Permalink
Updated unit test cases, increased code coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
arpitkhandelwal1984 committed Jun 28, 2018
1 parent d83ff7b commit 318302a
Show file tree
Hide file tree
Showing 2 changed files with 166 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ public void setup() {
this.mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();
}

/**
* Get all accounts test.
* All the accounts in the database should be returned as a result of this test and validation of total 10 accounts should pass.
*
* @throws Exception
*/
@Test
public void ut1_GetAccounts() throws Exception{
mockMvc
Expand All @@ -48,6 +54,12 @@ public void ut1_GetAccounts() throws Exception{
.andDo(print());
}

/**
* Get Account details test.
* Fetching the details for account with id 1 should be allowed and valid details should be returned.
*
* @throws Exception
*/
@Test
public void ut2_GetAccountByNumber() throws Exception {
mockMvc
Expand Down Expand Up @@ -78,6 +90,12 @@ public void ut2_GetAccountByNumber() throws Exception {
.andDo(print());
}

/**
* Get Account by number test for invalid account number.
* Trying to get an account's detail with invalid account number should result in a Http 400 error.
*
* @throws Exception
*/
@Test
public void ut3_GetAccountByNumber_InvalidAccount() throws Exception {
mockMvc
Expand All @@ -88,6 +106,12 @@ public void ut3_GetAccountByNumber_InvalidAccount() throws Exception {
.andDo(print());
}

/**
* Create Account test.
* Account with details given in the test is created as a result of the test execution.
*
* @throws Exception
*/
@Test
public void ut4_CreateAccount() throws Exception {
mockMvc
Expand Down Expand Up @@ -121,6 +145,13 @@ public void ut4_CreateAccount() throws Exception {
.andDo(print());
}

/**
* Create Account test for Invalid Customer.
* Trying to create an account for a customer with id 100 should be disallowed.
* A Http 400 should be thrown with message indicating that customer id 100 doesn't yet exist.
*
* @throws Exception
*/
@Test
public void ut4_CreateAccount_InvalidCustomer() throws Exception {
mockMvc
Expand All @@ -133,4 +164,109 @@ public void ut4_CreateAccount_InvalidCustomer() throws Exception {
.andExpect(jsonPath("$.apierror.message").value("Customer was not found for parameters {customerId=100}"))
.andDo(print());
}

/**
* Deposit Money test.
* Account # 1 : Initial amount is $100
* A deposit of $50 should be allowed and resultant balance should be $150.
*
* @throws Exception
*/
@Test
public void ut5_DepositMoney() throws Exception {
mockMvc
.perform(MockMvcRequestBuilders.post("/v1/account/deposit")
.contentType(MediaType.APPLICATION_JSON)
.content("{\"accountNumber\":\"1\", \"depositAmt\":\"50\"}")
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(jsonPath("$.accountNumber").value(1))
.andExpect(jsonPath("$.balance").value(150))
.andDo(print());
}

/**
* Withdraw Money test.
* Account # 1 : Amount after ut5 is $150
* A withdrawal of 50 should be allowed and new account balance should be $100.
*
* @throws Exception
*/
@Test
public void ut6_WithdrawMoney() throws Exception {
mockMvc
.perform(MockMvcRequestBuilders.post("/v1/account/withdraw")
.contentType(MediaType.APPLICATION_JSON)
.content("{\"accountNumber\":\"1\", \"withdrawlAmt\":\"50\"}")
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(jsonPath("$.accountNumber").value(1))
.andExpect(jsonPath("$.balance").value(100))
.andDo(print());
}

/**
* Withdraw Money test for negative scenario.
* Account # 1 : Amount after ut6 is $100
* A withdrawal of $200 should raise a InsufficientFund Exception and send a Http 400 response.
*
* @throws Exception
*/
@Test
public void ut7_WithdrawMoney_InsufficientFunds() throws Exception {
mockMvc
.perform(MockMvcRequestBuilders.post("/v1/account/withdraw")
.contentType(MediaType.APPLICATION_JSON)
.content("{\"accountNumber\":\"1\", \"withdrawlAmt\":\"200\"}")
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isBadRequest())
.andExpect(jsonPath("$.apierror.status").value("BAD_REQUEST"))
.andExpect(jsonPath("$.apierror.message").value("Insufficient funds in account number - 1. Cannot allow withdrawal of $200."))
.andDo(print());
}

/**
* Transfer Money test.
* Account # 1 : Initial Amount 100
* Account # 2 : Initial Amount 200
* Once the test completes, $50 should be transferred from Account # 1 to Account # 2
*
* @throws Exception
*/
@Test
public void ut8_TransferMoney() throws Exception {
mockMvc
.perform(MockMvcRequestBuilders.post("/v1/account/transfer")
.contentType(MediaType.APPLICATION_JSON)
.content("{\"debitAccountNumber\":\"1\", \"creditAccountNumber\":\"2\", \"amount\":\"50\"}")
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(jsonPath("$.[0].accountNumber").value(1))
.andExpect(jsonPath("$.[1].accountNumber").value(2))
.andExpect(jsonPath("$.[0].balance").value(50))
.andExpect(jsonPath("$.[1].balance").value(250))
.andDo(print());
}

/**
* Transfer Money test to a non existent recipient.
* Account # 1 : Initial Amount 100
* Account # 2 : doesn't exist
* Once the test completes, an Http 404 error should be returned indicating the recipient doesn't exist.
*
* @throws Exception
*/
@Test
public void ut9_TransferMoney_InvalidRecipient() throws Exception {
mockMvc
.perform(MockMvcRequestBuilders.post("/v1/account/transfer")
.contentType(MediaType.APPLICATION_JSON)
.content("{\"debitAccountNumber\":\"1\", \"creditAccountNumber\":\"20\", \"amount\":\"50\"}")
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isNotFound())
.andExpect(jsonPath("$.apierror.status").value("NOT_FOUND"))
.andExpect(jsonPath("$.apierror.message").value("Account was not found for parameters {accountNumber=20}"))
.andDo(print());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ public void setup() {
this.mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();
}

/**
* Get all customers test.
* The test should result in returning the 5 customers which are present in the test database.
*
* @throws Exception
*/
@Test
public void ut1_GetAllCustomers() throws Exception{
mockMvc
Expand All @@ -48,6 +54,12 @@ public void ut1_GetAllCustomers() throws Exception{
.andDo(print());
}

/**
* Get customer by ssn test.
* The test should result in returning customer's details for SSN:AK01
*
* @throws Exception
*/
@Test
public void ut2_GetCustomerBySsn() throws Exception{
mockMvc
Expand All @@ -70,6 +82,12 @@ public void ut2_GetCustomerBySsn() throws Exception{
.andDo(print());
}

/**
* Get customer by invalid ssn test.
* The test should result in returning a Http 404 for a customer with invalid ssn.
*
* @throws Exception
*/
@Test
public void ut3_GetCustomerBySsn_InvalidSsn() throws Exception {
mockMvc
Expand All @@ -80,6 +98,12 @@ public void ut3_GetCustomerBySsn_InvalidSsn() throws Exception {
.andDo(print());
}

/**
* Create customer test.
* The test should result in adding a new customer to the database with SSN:TK01
*
* @throws Exception
*/
@Test
public void ut4_CreateCustomer() throws Exception{
mockMvc
Expand All @@ -105,7 +129,12 @@ public void ut4_CreateCustomer() throws Exception{
.andDo(print());
}


/**
* Create duplicate customer test.
* Since a customer with SSN:TK01 has already been created in ut4, this test should result in Http 404.
*
* @throws Exception
*/
@Test
public void ut5_CreateCustomer_Duplicate() throws Exception {
mockMvc
Expand Down

0 comments on commit 318302a

Please sign in to comment.