Skip to content

Commit

Permalink
Added basic tests for AccountController
Browse files Browse the repository at this point in the history
  • Loading branch information
arpitkhandelwal1984 committed Jun 27, 2018
1 parent 092416f commit d83ff7b
Show file tree
Hide file tree
Showing 8 changed files with 170 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

import com.bankofspring.api.v1.request.account.CreateAccountRequest;
import com.bankofspring.api.v1.request.account.DepositRequest;
import com.bankofspring.api.v1.request.account.TransferFundRequest;
import com.bankofspring.api.v1.request.account.WithdrawalRequest;
import com.bankofspring.domain.model.Account;
import com.bankofspring.dto.AccountDto;
import com.bankofspring.exception.EntityException;
import com.bankofspring.exception.EntityNotFoundException;
import com.bankofspring.service.account.AccountService;
import com.bankofspring.service.account.exception.InsufficientFundsException;
import com.bankofspring.api.v1.request.account.TransferFundRequest;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
Expand All @@ -27,20 +27,19 @@
public class AccountController {

@Autowired
AccountService accountService;
private AccountService accountService;

@Autowired
ModelMapper modelMapper;
private ModelMapper modelMapper;

@GetMapping(value = "/")
public List<AccountDto> getAccounts() {
List<Account> accounts = accountService.getAllAccounts();
if (accounts != null && !accounts.isEmpty()) {
List<AccountDto> accountDtos = accounts
return accounts
.stream()
.map(account -> modelMapper.map(account, AccountDto.class))
.collect(Collectors.toList());
return accountDtos;
}
return Collections.emptyList();
}
Expand All @@ -49,8 +48,7 @@ public List<AccountDto> getAccounts() {
public AccountDto getAccountByNumber(@PathVariable("accountNumber") Long accountNumber) throws EntityNotFoundException {
Account account = accountService.getAccount(accountNumber);
if (account != null) {
AccountDto accountDto = modelMapper.map(account, AccountDto.class);
return accountDto;
return modelMapper.map(account, AccountDto.class);
}
return null;
}
Expand All @@ -60,8 +58,7 @@ public AccountDto createAccount(@RequestBody @Valid CreateAccountRequest createA
AccountDto accountDto = modelMapper.map(createAccountRequest, AccountDto.class);
Account account = accountService.createAccount(accountDto);
if (account != null) {
AccountDto resultAccount = modelMapper.map(account, AccountDto.class);
return resultAccount;
return modelMapper.map(account, AccountDto.class);
}
return null;
}
Expand All @@ -71,8 +68,7 @@ public AccountDto depositMoney(@RequestBody @Valid DepositRequest depositRequest
AccountDto accountDto = modelMapper.map(depositRequest, AccountDto.class);
Account account = accountService.creditAmount(accountDto, depositRequest.getDepositAmt());
if (account != null) {
AccountDto resultAccount = modelMapper.map(account, AccountDto.class);
return resultAccount;
return modelMapper.map(account, AccountDto.class);
}
return null;
}
Expand All @@ -82,8 +78,7 @@ public AccountDto withdrawMoney(@RequestBody @Valid WithdrawalRequest withdrawal
AccountDto accountDto = modelMapper.map(withdrawalRequest, AccountDto.class);
Account account = accountService.debitAmount(accountDto, withdrawalRequest.getWithdrawlAmt());
if (account != null) {
AccountDto resultAccount = modelMapper.map(account, AccountDto.class);
return resultAccount;
return modelMapper.map(account, AccountDto.class);
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@


import com.bankofspring.exception.ApiError;
import com.bankofspring.exception.BankException;
import com.bankofspring.exception.DuplicateEntityException;
import com.bankofspring.exception.EntityNotFoundException;
import com.bankofspring.exception.BankException;
import org.hibernate.exception.ConstraintViolationException;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package com.bankofspring.service.account;

import com.bankofspring.domain.model.Account;
import com.bankofspring.exception.EntityNotFoundException;
import com.bankofspring.service.account.exception.InsufficientFundsException;
import com.bankofspring.dto.AccountDto;
import com.bankofspring.exception.EntityException;
import com.bankofspring.exception.EntityNotFoundException;
import com.bankofspring.service.account.exception.InsufficientFundsException;

import java.math.BigDecimal;
import java.util.List;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
import com.bankofspring.domain.model.Account;
import com.bankofspring.domain.model.Branch;
import com.bankofspring.domain.model.Customer;
import com.bankofspring.domain.repository.AccountRepository;
import com.bankofspring.domain.repository.BranchRepository;
import com.bankofspring.domain.repository.CustomerRepository;
import com.bankofspring.dto.AccountDto;
import com.bankofspring.exception.EntityException;
import com.bankofspring.exception.EntityNotFoundException;
import com.bankofspring.service.account.exception.InsufficientFundsException;
import com.bankofspring.domain.repository.AccountRepository;
import com.bankofspring.domain.repository.BranchRepository;
import com.bankofspring.dto.AccountDto;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import com.bankofspring.domain.model.Customer;
import com.bankofspring.dto.CustomerDto;
import com.bankofspring.exception.EntityNotFoundException;
import com.bankofspring.exception.EntityException;
import com.bankofspring.exception.EntityNotFoundException;

import java.util.List;

Expand Down
17 changes: 8 additions & 9 deletions src/test/java/com/bankofspring/BankOfSpringApplicationTests.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
package com.bankofspring;

import org.junit.Test;
import com.bankofspring.test.controller.AccountControllerTest;
import com.bankofspring.test.controller.CustomerControllerTest;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.junit.runners.Suite;

@RunWith(SpringRunner.class)
@SpringBootTest
@RunWith(Suite.class)
@Suite.SuiteClasses({
CustomerControllerTest.class,
AccountControllerTest.class
})
public class BankOfSpringApplicationTests {

@Test
public void contextLoads() {
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
package com.bankofspring.test.controller;

import static org.hamcrest.collection.IsCollectionWithSize.hasSize;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import org.junit.Before;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.MethodSorters;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
import com.bankofspring.BankOfSpringApplication;

/**
* Created by Arpit Khandelwal.
*/
@RunWith(SpringRunner.class)
@ContextConfiguration(classes = BankOfSpringApplication.class)
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
@SpringBootTest
public class AccountControllerTest {
private MockMvc mockMvc;

@Autowired
private WebApplicationContext wac;

@Before
public void setup() {
this.mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();
}

@Test
public void ut1_GetAccounts() throws Exception{
mockMvc
.perform(MockMvcRequestBuilders.get("/v1/account/").accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(jsonPath("$", hasSize(10)))
.andDo(print());
}

@Test
public void ut2_GetAccountByNumber() throws Exception {
mockMvc
.perform(MockMvcRequestBuilders.get("/v1/account/1").accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(jsonPath("$.accountNumber").exists())
.andExpect(jsonPath("$.branchId").exists())
.andExpect(jsonPath("$.type").exists())
.andExpect(jsonPath("$.balance").exists())
.andExpect(jsonPath("$.accountOwner.customerId").exists())
.andExpect(jsonPath("$.accountOwner.name").exists())
.andExpect(jsonPath("$.accountOwner.ssn").exists())
.andExpect(jsonPath("$.accountOwner.address1").exists())
.andExpect(jsonPath("$.accountOwner.address2").exists())
.andExpect(jsonPath("$.accountOwner.city").exists())
.andExpect(jsonPath("$.accountOwner.contactNumber").exists())
.andExpect(jsonPath("$.accountNumber").value(1))
.andExpect(jsonPath("$.branchId").value(1))
.andExpect(jsonPath("$.type").value("SAVINGS"))
.andExpect(jsonPath("$.balance").value(100))
.andExpect(jsonPath("$.accountOwner.customerId").value(1))
.andExpect(jsonPath("$.accountOwner.name").value("Arpit K"))
.andExpect(jsonPath("$.accountOwner.ssn").value("AK01"))
.andExpect(jsonPath("$.accountOwner.address1").value("VT1"))
.andExpect(jsonPath("$.accountOwner.address2").value("Marine Bay1"))
.andExpect(jsonPath("$.accountOwner.city").value("Indore"))
.andExpect(jsonPath("$.accountOwner.contactNumber").value("9425094250"))
.andDo(print());
}

@Test
public void ut3_GetAccountByNumber_InvalidAccount() throws Exception {
mockMvc
.perform(MockMvcRequestBuilders.get("/v1/account/100").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=100}"))
.andDo(print());
}

@Test
public void ut4_CreateAccount() throws Exception {
mockMvc
.perform(MockMvcRequestBuilders.post("/v1/account/create")
.contentType(MediaType.APPLICATION_JSON)
.content("{\"customerId\":\"1\", \"branchId\":\"1\", \"type\":\"CURRENT\",\"balance\":\"1000\"}")
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(jsonPath("$.accountNumber").exists())
.andExpect(jsonPath("$.branchId").exists())
.andExpect(jsonPath("$.type").exists())
.andExpect(jsonPath("$.balance").exists())
.andExpect(jsonPath("$.accountOwner.customerId").exists())
.andExpect(jsonPath("$.accountOwner.name").exists())
.andExpect(jsonPath("$.accountOwner.ssn").exists())
.andExpect(jsonPath("$.accountOwner.address1").exists())
.andExpect(jsonPath("$.accountOwner.address2").exists())
.andExpect(jsonPath("$.accountOwner.city").exists())
.andExpect(jsonPath("$.accountOwner.contactNumber").exists())
.andExpect(jsonPath("$.accountNumber").value(11))
.andExpect(jsonPath("$.branchId").value(1))
.andExpect(jsonPath("$.type").value("CURRENT"))
.andExpect(jsonPath("$.balance").value(1000))
.andExpect(jsonPath("$.accountOwner.customerId").value(1))
.andExpect(jsonPath("$.accountOwner.name").value("Arpit K"))
.andExpect(jsonPath("$.accountOwner.ssn").value("AK01"))
.andExpect(jsonPath("$.accountOwner.address1").value("VT1"))
.andExpect(jsonPath("$.accountOwner.address2").value("Marine Bay1"))
.andExpect(jsonPath("$.accountOwner.city").value("Indore"))
.andExpect(jsonPath("$.accountOwner.contactNumber").value("9425094250"))
.andDo(print());
}

@Test
public void ut4_CreateAccount_InvalidCustomer() throws Exception {
mockMvc
.perform(MockMvcRequestBuilders.post("/v1/account/create")
.contentType(MediaType.APPLICATION_JSON)
.content("{\"customerId\":\"100\", \"branchId\":\"1\", \"type\":\"CURRENT\",\"balance\":\"1000\"}")
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isNotFound())
.andExpect(jsonPath("$.apierror.status").value("NOT_FOUND"))
.andExpect(jsonPath("$.apierror.message").value("Customer was not found for parameters {customerId=100}"))
.andDo(print());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,17 @@ public void ut2_GetCustomerBySsn() throws Exception{
}

@Test
public void ut3_CreateCustomer() throws Exception{
public void ut3_GetCustomerBySsn_InvalidSsn() throws Exception {
mockMvc
.perform(MockMvcRequestBuilders.get("/v1/customer/AK02").accept(MediaType.APPLICATION_JSON))
.andExpect(status().isNotFound())
.andExpect(jsonPath("$.apierror.status").value("NOT_FOUND"))
.andExpect(jsonPath("$.apierror.message").value("Customer was not found for parameters {ssn=AK02}"))
.andDo(print());
}

@Test
public void ut4_CreateCustomer() throws Exception{
mockMvc
.perform(MockMvcRequestBuilders.post("/v1/customer/create")
.contentType(MediaType.APPLICATION_JSON)
Expand All @@ -95,15 +105,6 @@ public void ut3_CreateCustomer() throws Exception{
.andDo(print());
}

@Test
public void ut4_GetCustomerBySsn_InvalidSsn() throws Exception {
mockMvc
.perform(MockMvcRequestBuilders.get("/v1/customer/AK02").accept(MediaType.APPLICATION_JSON))
.andExpect(status().isNotFound())
.andExpect(jsonPath("$.apierror.status").value("NOT_FOUND"))
.andExpect(jsonPath("$.apierror.message").value("Customer was not found for parameters {ssn=AK02}"))
.andDo(print());
}

@Test
public void ut5_CreateCustomer_Duplicate() throws Exception {
Expand Down

0 comments on commit d83ff7b

Please sign in to comment.