package io.github.jhipster.sample.web.rest; import io.github.jhipster.sample.JhipsterMongodbSampleApplicationApp; import io.github.jhipster.sample.domain.BankAccount; import io.github.jhipster.sample.repository.BankAccountRepository; import io.github.jhipster.sample.web.rest.errors.ExceptionTranslator; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.MockitoAnnotations; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.data.web.PageableHandlerMethodArgumentResolver; import org.springframework.http.MediaType; import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.setup.MockMvcBuilders; import java.math.BigDecimal; import java.util.List; import static org.assertj.core.api.Assertions.assertThat; import static org.hamcrest.Matchers.hasItem; 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(SpringRunner.class) @SpringBootTest(classes = JhipsterMongodbSampleApplicationApp.class) public class BankAccountResourceIntTest { private static final String DEFAULT_NAME = "AAAAAAAAAA"; private static final String UPDATED_NAME = "BBBBBBBBBB"; private static final BigDecimal DEFAULT_BALANCE = new BigDecimal(1); private static final BigDecimal UPDATED_BALANCE = new BigDecimal(2); @Autowired private BankAccountRepository bankAccountRepository; @Autowired private MappingJackson2HttpMessageConverter jacksonMessageConverter; @Autowired private PageableHandlerMethodArgumentResolver pageableArgumentResolver; @Autowired private ExceptionTranslator exceptionTranslator; private MockMvc restBankAccountMockMvc; private BankAccount bankAccount; @Before public void setup() { MockitoAnnotations.initMocks(this); BankAccountResource bankAccountResource = new BankAccountResource(bankAccountRepository); this.restBankAccountMockMvc = MockMvcBuilders.standaloneSetup(bankAccountResource) .setCustomArgumentResolvers(pageableArgumentResolver) .setControllerAdvice(exceptionTranslator) .setMessageConverters(jacksonMessageConverter).build(); } /** * Create an entity for this test. * * This is a static method, as tests for other entities might also need it, * if they test an entity which requires the current entity. */ public static BankAccount createEntity() { BankAccount bankAccount = new BankAccount(); bankAccount.setName(DEFAULT_NAME); bankAccount.setBalance(DEFAULT_BALANCE); return bankAccount; } @Before public void initTest() { bankAccountRepository.deleteAll(); bankAccount = createEntity(); } @Test public void createBankAccount() throws Exception { int databaseSizeBeforeCreate = bankAccountRepository.findAll().size(); // Create the BankAccount restBankAccountMockMvc.perform(post("/api/bank-accounts") .contentType(TestUtil.APPLICATION_JSON_UTF8) .content(TestUtil.convertObjectToJsonBytes(bankAccount))) .andExpect(status().isCreated()); // Validate the BankAccount in the database List<BankAccount> bankAccountList = bankAccountRepository.findAll(); assertThat(bankAccountList).hasSize(databaseSizeBeforeCreate + 1); BankAccount testBankAccount = bankAccountList.get(bankAccountList.size() - 1); assertThat(testBankAccount.getName()).isEqualTo(DEFAULT_NAME); assertThat(testBankAccount.getBalance()).isEqualTo(DEFAULT_BALANCE); } @Test public void createBankAccountWithExistingId() throws Exception { int databaseSizeBeforeCreate = bankAccountRepository.findAll().size(); // Create the BankAccount with an existing ID bankAccount.setId("existing_id"); // An entity with an existing ID cannot be created, so this API call must fail restBankAccountMockMvc.perform(post("/api/bank-accounts") .contentType(TestUtil.APPLICATION_JSON_UTF8) .content(TestUtil.convertObjectToJsonBytes(bankAccount))) .andExpect(status().isBadRequest()); // Validate the Alice in the database List<BankAccount> bankAccountList = bankAccountRepository.findAll(); assertThat(bankAccountList).hasSize(databaseSizeBeforeCreate); } @Test 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/bank-accounts") .contentType(TestUtil.APPLICATION_JSON_UTF8) .content(TestUtil.convertObjectToJsonBytes(bankAccount))) .andExpect(status().isBadRequest()); List<BankAccount> bankAccountList = bankAccountRepository.findAll(); assertThat(bankAccountList).hasSize(databaseSizeBeforeTest); } @Test 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/bank-accounts") .contentType(TestUtil.APPLICATION_JSON_UTF8) .content(TestUtil.convertObjectToJsonBytes(bankAccount))) .andExpect(status().isBadRequest()); List<BankAccount> bankAccountList = bankAccountRepository.findAll(); assertThat(bankAccountList).hasSize(databaseSizeBeforeTest); } @Test public void getAllBankAccounts() throws Exception { // Initialize the database bankAccountRepository.save(bankAccount); // Get all the bankAccountList restBankAccountMockMvc.perform(get("/api/bank-accounts?sort=id,desc")) .andExpect(status().isOk()) .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8_VALUE)) .andExpect(jsonPath("$.[*].id").value(hasItem(bankAccount.getId()))) .andExpect(jsonPath("$.[*].name").value(hasItem(DEFAULT_NAME.toString()))) .andExpect(jsonPath("$.[*].balance").value(hasItem(DEFAULT_BALANCE.intValue()))); } @Test public void getBankAccount() throws Exception { // Initialize the database bankAccountRepository.save(bankAccount); // Get the bankAccount restBankAccountMockMvc.perform(get("/api/bank-accounts/{id}", bankAccount.getId())) .andExpect(status().isOk()) .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8_VALUE)) .andExpect(jsonPath("$.id").value(bankAccount.getId())) .andExpect(jsonPath("$.name").value(DEFAULT_NAME.toString())) .andExpect(jsonPath("$.balance").value(DEFAULT_BALANCE.intValue())); } @Test public void getNonExistingBankAccount() throws Exception { // Get the bankAccount restBankAccountMockMvc.perform(get("/api/bank-accounts/{id}", Long.MAX_VALUE)) .andExpect(status().isNotFound()); } @Test public void updateBankAccount() throws Exception { // Initialize the database bankAccountRepository.save(bankAccount); int databaseSizeBeforeUpdate = bankAccountRepository.findAll().size(); // Update the bankAccount BankAccount updatedBankAccount = bankAccountRepository.findOne(bankAccount.getId()); updatedBankAccount.setName(UPDATED_NAME); updatedBankAccount.setBalance(UPDATED_BALANCE); restBankAccountMockMvc.perform(put("/api/bank-accounts") .contentType(TestUtil.APPLICATION_JSON_UTF8) .content(TestUtil.convertObjectToJsonBytes(updatedBankAccount))) .andExpect(status().isOk()); // Validate the BankAccount in the database List<BankAccount> bankAccountList = bankAccountRepository.findAll(); assertThat(bankAccountList).hasSize(databaseSizeBeforeUpdate); BankAccount testBankAccount = bankAccountList.get(bankAccountList.size() - 1); assertThat(testBankAccount.getName()).isEqualTo(UPDATED_NAME); assertThat(testBankAccount.getBalance()).isEqualTo(UPDATED_BALANCE); } @Test public void updateNonExistingBankAccount() throws Exception { int databaseSizeBeforeUpdate = bankAccountRepository.findAll().size(); // Create the BankAccount // If the entity doesn't have an ID, it will be created instead of just being updated restBankAccountMockMvc.perform(put("/api/bank-accounts") .contentType(TestUtil.APPLICATION_JSON_UTF8) .content(TestUtil.convertObjectToJsonBytes(bankAccount))) .andExpect(status().isCreated()); // Validate the BankAccount in the database List<BankAccount> bankAccountList = bankAccountRepository.findAll(); assertThat(bankAccountList).hasSize(databaseSizeBeforeUpdate + 1); } @Test public void deleteBankAccount() throws Exception { // Initialize the database bankAccountRepository.save(bankAccount); int databaseSizeBeforeDelete = bankAccountRepository.findAll().size(); // Get the bankAccount restBankAccountMockMvc.perform(delete("/api/bank-accounts/{id}", bankAccount.getId()) .accept(TestUtil.APPLICATION_JSON_UTF8)) .andExpect(status().isOk()); // Validate the database is empty List<BankAccount> bankAccountList = bankAccountRepository.findAll(); assertThat(bankAccountList).hasSize(databaseSizeBeforeDelete - 1); } @Test public void equalsVerifier() throws Exception { TestUtil.equalsVerifier(BankAccount.class); BankAccount bankAccount1 = new BankAccount(); bankAccount1.setId("id1"); BankAccount bankAccount2 = new BankAccount(); bankAccount2.setId(bankAccount1.getId()); assertThat(bankAccount1).isEqualTo(bankAccount2); bankAccount2.setId("id2"); assertThat(bankAccount1).isNotEqualTo(bankAccount2); bankAccount1.setId(null); assertThat(bankAccount1).isNotEqualTo(bankAccount2); } }