package com.mycompany.myapp.web.rest; import com.mycompany.myapp.Application; import com.mycompany.myapp.domain.BankAccount; import com.mycompany.myapp.repository.BankAccountRepository; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import static org.hamcrest.Matchers.hasItem; import org.mockito.MockitoAnnotations; import org.springframework.boot.test.IntegrationTest; import org.springframework.boot.test.SpringApplicationConfiguration; import org.springframework.http.MediaType; import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; import org.springframework.data.web.PageableHandlerMethodArgumentResolver; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.web.WebAppConfiguration; import org.springframework.test.util.ReflectionTestUtils; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.setup.MockMvcBuilders; import org.springframework.transaction.annotation.Transactional; import javax.annotation.PostConstruct; import javax.inject.Inject; import java.math.BigDecimal; import java.util.List; import static org.assertj.core.api.Assertions.assertThat; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; /** * Test class for the BankAccountResource REST controller. * * @see BankAccountResource */ @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = Application.class) @WebAppConfiguration @IntegrationTest public class BankAccountResourceTest { private static final String DEFAULT_NAME = "AAAAA"; private static final String UPDATED_NAME = "BBBBB"; private static final BigDecimal DEFAULT_BALANCE = new BigDecimal(1); private static final BigDecimal UPDATED_BALANCE = new BigDecimal(2); @Inject private BankAccountRepository bankAccountRepository; @Inject private MappingJackson2HttpMessageConverter jacksonMessageConverter; @Inject private PageableHandlerMethodArgumentResolver pageableArgumentResolver; private MockMvc restBankAccountMockMvc; private BankAccount bankAccount; @PostConstruct public void setup() { MockitoAnnotations.initMocks(this); BankAccountResource bankAccountResource = new BankAccountResource(); ReflectionTestUtils.setField(bankAccountResource, "bankAccountRepository", bankAccountRepository); this.restBankAccountMockMvc = MockMvcBuilders.standaloneSetup(bankAccountResource) .setCustomArgumentResolvers(pageableArgumentResolver) .setMessageConverters(jacksonMessageConverter).build(); } @Before public void initTest() { bankAccount = new BankAccount(); bankAccount.setName(DEFAULT_NAME); bankAccount.setBalance(DEFAULT_BALANCE); } @Test @Transactional public void createBankAccount() throws Exception { int databaseSizeBeforeCreate = bankAccountRepository.findAll().size(); // Create the BankAccount restBankAccountMockMvc.perform(post("/api/bankAccounts") .contentType(TestUtil.APPLICATION_JSON_UTF8) .content(TestUtil.convertObjectToJsonBytes(bankAccount))) .andExpect(status().isCreated()); // Validate the BankAccount in the database List<BankAccount> bankAccounts = bankAccountRepository.findAll(); assertThat(bankAccounts).hasSize(databaseSizeBeforeCreate + 1); BankAccount testBankAccount = bankAccounts.get(bankAccounts.size() - 1); assertThat(testBankAccount.getName()).isEqualTo(DEFAULT_NAME); assertThat(testBankAccount.getBalance()).isEqualTo(DEFAULT_BALANCE); } @Test @Transactional public void checkNameIsRequired() throws Exception { int databaseSizeBeforeTest = bankAccountRepository.findAll().size(); // set the field null bankAccount.setName(null); // Create the BankAccount, which fails. restBankAccountMockMvc.perform(post("/api/bankAccounts") .contentType(TestUtil.APPLICATION_JSON_UTF8) .content(TestUtil.convertObjectToJsonBytes(bankAccount))) .andExpect(status().isBadRequest()); List<BankAccount> bankAccounts = bankAccountRepository.findAll(); assertThat(bankAccounts).hasSize(databaseSizeBeforeTest); } @Test @Transactional public void checkBalanceIsRequired() throws Exception { int databaseSizeBeforeTest = bankAccountRepository.findAll().size(); // set the field null bankAccount.setBalance(null); // Create the BankAccount, which fails. restBankAccountMockMvc.perform(post("/api/bankAccounts") .contentType(TestUtil.APPLICATION_JSON_UTF8) .content(TestUtil.convertObjectToJsonBytes(bankAccount))) .andExpect(status().isBadRequest()); List<BankAccount> bankAccounts = bankAccountRepository.findAll(); assertThat(bankAccounts).hasSize(databaseSizeBeforeTest); } @Test @Transactional public void getAllBankAccounts() throws Exception { // Initialize the database bankAccountRepository.saveAndFlush(bankAccount); // Get all the bankAccounts restBankAccountMockMvc.perform(get("/api/bankAccounts")) .andExpect(status().isOk()) .andExpect(content().contentType(MediaType.APPLICATION_JSON)) .andExpect(jsonPath("$.[*].id").value(hasItem(bankAccount.getId().intValue()))) .andExpect(jsonPath("$.[*].name").value(hasItem(DEFAULT_NAME.toString()))) .andExpect(jsonPath("$.[*].balance").value(hasItem(DEFAULT_BALANCE.intValue()))); } @Test @Transactional public void getBankAccount() throws Exception { // Initialize the database bankAccountRepository.saveAndFlush(bankAccount); // Get the bankAccount restBankAccountMockMvc.perform(get("/api/bankAccounts/{id}", bankAccount.getId())) .andExpect(status().isOk()) .andExpect(content().contentType(MediaType.APPLICATION_JSON)) .andExpect(jsonPath("$.id").value(bankAccount.getId().intValue())) .andExpect(jsonPath("$.name").value(DEFAULT_NAME.toString())) .andExpect(jsonPath("$.balance").value(DEFAULT_BALANCE.intValue())); } @Test @Transactional public void getNonExistingBankAccount() throws Exception { // Get the bankAccount restBankAccountMockMvc.perform(get("/api/bankAccounts/{id}", Long.MAX_VALUE)) .andExpect(status().isNotFound()); } @Test @Transactional public void updateBankAccount() throws Exception { // Initialize the database bankAccountRepository.saveAndFlush(bankAccount); int databaseSizeBeforeUpdate = bankAccountRepository.findAll().size(); // Update the bankAccount bankAccount.setName(UPDATED_NAME); bankAccount.setBalance(UPDATED_BALANCE); restBankAccountMockMvc.perform(put("/api/bankAccounts") .contentType(TestUtil.APPLICATION_JSON_UTF8) .content(TestUtil.convertObjectToJsonBytes(bankAccount))) .andExpect(status().isOk()); // Validate the BankAccount in the database List<BankAccount> bankAccounts = bankAccountRepository.findAll(); assertThat(bankAccounts).hasSize(databaseSizeBeforeUpdate); BankAccount testBankAccount = bankAccounts.get(bankAccounts.size() - 1); assertThat(testBankAccount.getName()).isEqualTo(UPDATED_NAME); assertThat(testBankAccount.getBalance()).isEqualTo(UPDATED_BALANCE); } @Test @Transactional public void deleteBankAccount() throws Exception { // Initialize the database bankAccountRepository.saveAndFlush(bankAccount); int databaseSizeBeforeDelete = bankAccountRepository.findAll().size(); // Get the bankAccount restBankAccountMockMvc.perform(delete("/api/bankAccounts/{id}", bankAccount.getId()) .accept(TestUtil.APPLICATION_JSON_UTF8)) .andExpect(status().isOk()); // Validate the database is empty List<BankAccount> bankAccounts = bankAccountRepository.findAll(); assertThat(bankAccounts).hasSize(databaseSizeBeforeDelete - 1); } }