content: package com.example.demo.web.rest;
  
  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.*;
  
  import com.example.demo.IntegrationTest;
  import com.example.demo.domain.Country;
  import com.example.demo.repository.CountryRepository;
  import java.util.List;
  import java.util.Random;
  import java.util.concurrent.atomic.AtomicLong;
  import javax.persistence.EntityManager;
  import org.junit.jupiter.api.BeforeEach;
  import org.junit.jupiter.api.Test;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
  import org.springframework.http.MediaType;
  import org.springframework.security.test.context.support.WithMockUser;
  import org.springframework.test.web.servlet.MockMvc;
  import org.springframework.transaction.annotation.Transactional;
  
  /**
   * Integration tests for the {@link CountryResource} REST controller.
   */
  @IntegrationTest
  @AutoConfigureMockMvc
  @WithMockUser
  class CountryResourceIT {
  
      private static final String DEFAULT_COUNTRY_NAME = "AAAAAAAAAA";
      private static final String UPDATED_COUNTRY_NAME = "BBBBBBBBBB";
  
      private static final String ENTITY_API_URL = "/api/countries";
      private static final String ENTITY_API_URL_ID = ENTITY_API_URL + "/{id}";
  
      private static Random random = new Random();
      private static AtomicLong count = new AtomicLong(random.nextInt() + (2 * Integer.MAX_VALUE));
  
      @Autowired
      private CountryRepository countryRepository;
  
      @Autowired
      private EntityManager em;
  
      @Autowired
      private MockMvc restCountryMockMvc;
  
      private Country country;
  
      /**
       * 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 Country createEntity(EntityManager em) {
          Country country = new Country().countryName(DEFAULT_COUNTRY_NAME);
          return country;
      }
  
      /**
       * Create an updated 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 Country createUpdatedEntity(EntityManager em) {
          Country country = new Country().countryName(UPDATED_COUNTRY_NAME);
          return country;
      }
  
      @BeforeEach
      public void initTest() {
          country = createEntity(em);
      }
  
      @Test
      @Transactional
      void createCountry() throws Exception {
          int databaseSizeBeforeCreate = countryRepository.findAll().size();
          // Create the Country
          restCountryMockMvc
              .perform(post(ENTITY_API_URL).contentType(MediaType.APPLICATION_JSON).content(TestUtil.convertObjectToJsonBytes(country)))
              .andExpect(status().isCreated());
  
          // Validate the Country in the database
          List<Country> countryList = countryRepository.findAll();
          assertThat(countryList).hasSize(databaseSizeBeforeCreate + 1);
          Country testCountry = countryList.get(countryList.size() - 1);
          assertThat(testCountry.getCountryName()).isEqualTo(DEFAULT_COUNTRY_NAME);
      }
  
      @Test
      @Transactional
      void createCountryWithExistingId() throws Exception {
          // Create the Country with an existing ID
          country.setId(1L);
  
          int databaseSizeBeforeCreate = countryRepository.findAll().size();
  
          // An entity with an existing ID cannot be created, so this API call must fail
          restCountryMockMvc
              .perform(post(ENTITY_API_URL).contentType(MediaType.APPLICATION_JSON).content(TestUtil.convertObjectToJsonBytes(country)))
              .andExpect(status().isBadRequest());
  
          // Validate the Country in the database
          List<Country> countryList = countryRepository.findAll();
          assertThat(countryList).hasSize(databaseSizeBeforeCreate);
      }
  
      @Test
      @Transactional
      void getAllCountries() throws Exception {
          // Initialize the database
          countryRepository.saveAndFlush(country);
  
          // Get all the countryList
          restCountryMockMvc
              .perform(get(ENTITY_API_URL + "?sort=id,desc"))
              .andExpect(status().isOk())
              .andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE))
              .andExpect(jsonPath("$.[*].id").value(hasItem(country.getId().intValue())))
              .andExpect(jsonPath("$.[*].countryName").value(hasItem(DEFAULT_COUNTRY_NAME)));
      }
  
      @Test
      @Transactional
      void getCountry() throws Exception {
          // Initialize the database
          countryRepository.saveAndFlush(country);
  
          // Get the country
          restCountryMockMvc
              .perform(get(ENTITY_API_URL_ID, country.getId()))
              .andExpect(status().isOk())
              .andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE))
              .andExpect(jsonPath("$.id").value(country.getId().intValue()))
              .andExpect(jsonPath("$.countryName").value(DEFAULT_COUNTRY_NAME));
      }
  
      @Test
      @Transactional
      void getNonExistingCountry() throws Exception {
          // Get the country
          restCountryMockMvc.perform(get(ENTITY_API_URL_ID, Long.MAX_VALUE)).andExpect(status().isNotFound());
      }
  
      @Test
      @Transactional
      void putNewCountry() throws Exception {
          // Initialize the database
          countryRepository.saveAndFlush(country);
  
          int databaseSizeBeforeUpdate = countryRepository.findAll().size();
  
          // Update the country
          Country updatedCountry = countryRepository.findById(country.getId()).get();
          // Disconnect from session so that the updates on updatedCountry are not directly saved in db
          em.detach(updatedCountry);
          updatedCountry.countryName(UPDATED_COUNTRY_NAME);
  
          restCountryMockMvc
              .perform(
                  put(ENTITY_API_URL_ID, updatedCountry.getId())
                      .contentType(MediaType.APPLICATION_JSON)
                      .content(TestUtil.convertObjectToJsonBytes(updatedCountry))
              )
              .andExpect(status().isOk());
  
          // Validate the Country in the database
          List<Country> countryList = countryRepository.findAll();
          assertThat(countryList).hasSize(databaseSizeBeforeUpdate);
          Country testCountry = countryList.get(countryList.size() - 1);
          assertThat(testCountry.getCountryName()).isEqualTo(UPDATED_COUNTRY_NAME);
      }
  
      @Test
      @Transactional
      void putNonExistingCountry() throws Exception {
          int databaseSizeBeforeUpdate = countryRepository.findAll().size();
          country.setId(count.incrementAndGet());
  
          // If the entity doesn't have an ID, it will throw BadRequestAlertException
          restCountryMockMvc
              .perform(
                  put(ENTITY_API_URL_ID, country.getId())
                      .contentType(MediaType.APPLICATION_JSON)
                      .content(TestUtil.convertObjectToJsonBytes(country))
              )
              .andExpect(status().isBadRequest());
  
          // Validate the Country in the database
          List<Country> countryList = countryRepository.findAll();
          assertThat(countryList).hasSize(databaseSizeBeforeUpdate);
      }
  
      @Test
      @Transactional
      void putWithIdMismatchCountry() throws Exception {
          int databaseSizeBeforeUpdate = countryRepository.findAll().size();
          country.setId(count.incrementAndGet());
  
          // If url ID doesn't match entity ID, it will throw BadRequestAlertException
          restCountryMockMvc
              .perform(
                  put(ENTITY_API_URL_ID, count.incrementAndGet())
                      .contentType(MediaType.APPLICATION_JSON)
                      .content(TestUtil.convertObjectToJsonBytes(country))
              )
              .andExpect(status().isBadRequest());
  
          // Validate the Country in the database
          List<Country> countryList = countryRepository.findAll();
          assertThat(countryList).hasSize(databaseSizeBeforeUpdate);
      }
  
      @Test
      @Transactional
      void putWithMissingIdPathParamCountry() throws Exception {
          int databaseSizeBeforeUpdate = countryRepository.findAll().size();
          country.setId(count.incrementAndGet());
  
          // If url ID doesn't match entity ID, it will throw BadRequestAlertException
          restCountryMockMvc
              .perform(put(ENTITY_API_URL).contentType(MediaType.APPLICATION_JSON).content(TestUtil.convertObjectToJsonBytes(country)))
              .andExpect(status().isMethodNotAllowed());
  
          // Validate the Country in the database
          List<Country> countryList = countryRepository.findAll();
          assertThat(countryList).hasSize(databaseSizeBeforeUpdate);
      }
  
      @Test
      @Transactional
      void partialUpdateCountryWithPatch() throws Exception {
          // Initialize the database
          countryRepository.saveAndFlush(country);
  
          int databaseSizeBeforeUpdate = countryRepository.findAll().size();
  
          // Update the country using partial update
          Country partialUpdatedCountry = new Country();
          partialUpdatedCountry.setId(country.getId());
  
          partialUpdatedCountry.countryName(UPDATED_COUNTRY_NAME);
  
          restCountryMockMvc
              .perform(
                  patch(ENTITY_API_URL_ID, partialUpdatedCountry.getId())
                      .contentType("application/merge-patch+json")
                      .content(TestUtil.convertObjectToJsonBytes(partialUpdatedCountry))
              )
              .andExpect(status().isOk());
  
          // Validate the Country in the database
          List<Country> countryList = countryRepository.findAll();
          assertThat(countryList).hasSize(databaseSizeBeforeUpdate);
          Country testCountry = countryList.get(countryList.size() - 1);
          assertThat(testCountry.getCountryName()).isEqualTo(UPDATED_COUNTRY_NAME);
      }
  
      @Test
      @Transactional
      void fullUpdateCountryWithPatch() throws Exception {
          // Initialize the database
          countryRepository.saveAndFlush(country);
  
          int databaseSizeBeforeUpdate = countryRepository.findAll().size();
  
          // Update the country using partial update
          Country partialUpdatedCountry = new Country();
          partialUpdatedCountry.setId(country.getId());
  
          partialUpdatedCountry.countryName(UPDATED_COUNTRY_NAME);
  
          restCountryMockMvc
              .perform(
                  patch(ENTITY_API_URL_ID, partialUpdatedCountry.getId())
                      .contentType("application/merge-patch+json")
                      .content(TestUtil.convertObjectToJsonBytes(partialUpdatedCountry))
              )
              .andExpect(status().isOk());
  
          // Validate the Country in the database
          List<Country> countryList = countryRepository.findAll();
          assertThat(countryList).hasSize(databaseSizeBeforeUpdate);
          Country testCountry = countryList.get(countryList.size() - 1);
          assertThat(testCountry.getCountryName()).isEqualTo(UPDATED_COUNTRY_NAME);
      }
  
      @Test
      @Transactional
      void patchNonExistingCountry() throws Exception {
          int databaseSizeBeforeUpdate = countryRepository.findAll().size();
          country.setId(count.incrementAndGet());
  
          // If the entity doesn't have an ID, it will throw BadRequestAlertException
          restCountryMockMvc
              .perform(
                  patch(ENTITY_API_URL_ID, country.getId())
                      .contentType("application/merge-patch+json")
                      .content(TestUtil.convertObjectToJsonBytes(country))
              )
              .andExpect(status().isBadRequest());
  
          // Validate the Country in the database
          List<Country> countryList = countryRepository.findAll();
          assertThat(countryList).hasSize(databaseSizeBeforeUpdate);
      }
  
      @Test
      @Transactional
      void patchWithIdMismatchCountry() throws Exception {
          int databaseSizeBeforeUpdate = countryRepository.findAll().size();
          country.setId(count.incrementAndGet());
  
          // If url ID doesn't match entity ID, it will throw BadRequestAlertException
          restCountryMockMvc
              .perform(
                  patch(ENTITY_API_URL_ID, count.incrementAndGet())
                      .contentType("application/merge-patch+json")
                      .content(TestUtil.convertObjectToJsonBytes(country))
              )
              .andExpect(status().isBadRequest());
  
          // Validate the Country in the database
          List<Country> countryList = countryRepository.findAll();
          assertThat(countryList).hasSize(databaseSizeBeforeUpdate);
      }
  
      @Test
      @Transactional
      void patchWithMissingIdPathParamCountry() throws Exception {
          int databaseSizeBeforeUpdate = countryRepository.findAll().size();
          country.setId(count.incrementAndGet());
  
          // If url ID doesn't match entity ID, it will throw BadRequestAlertException
          restCountryMockMvc
              .perform(patch(ENTITY_API_URL).contentType("application/merge-patch+json").content(TestUtil.convertObjectToJsonBytes(country)))
              .andExpect(status().isMethodNotAllowed());
  
          // Validate the Country in the database
          List<Country> countryList = countryRepository.findAll();
          assertThat(countryList).hasSize(databaseSizeBeforeUpdate);
      }
  
      @Test
      @Transactional
      void deleteCountry() throws Exception {
          // Initialize the database
          countryRepository.saveAndFlush(country);
  
          int databaseSizeBeforeDelete = countryRepository.findAll().size();
  
          // Delete the country
          restCountryMockMvc
              .perform(delete(ENTITY_API_URL_ID, country.getId()).accept(MediaType.APPLICATION_JSON))
              .andExpect(status().isNoContent());
  
          // Validate the database contains one less item
          List<Country> countryList = countryRepository.findAll();
          assertThat(countryList).hasSize(databaseSizeBeforeDelete - 1);
      }
  }
  